Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(437)

Side by Side Diff: dbus/message.cc

Issue 10382021: dbus: revamp fd passing for i/o restrictions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "dbus/message.h" 5 #include "dbus/message.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/format_macros.h" 10 #include "base/format_macros.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/platform_file.h" 12 #include "base/platform_file.h"
13 #include "base/stringprintf.h" 13 #include "base/stringprintf.h"
14 #include "base/threading/thread_restrictions.h"
14 #include "dbus/object_path.h" 15 #include "dbus/object_path.h"
15 #include "third_party/protobuf/src/google/protobuf/message_lite.h" 16 #include "third_party/protobuf/src/google/protobuf/message_lite.h"
16 17
17 namespace { 18 namespace {
18 19
19 // Appends the header name and the value to |output|, if the value is 20 // Appends the header name and the value to |output|, if the value is
20 // not empty. 21 // not empty.
21 static void AppendStringHeader(const std::string& header_name, 22 static void AppendStringHeader(const std::string& header_name,
22 const std::string& header_value, 23 const std::string& header_value,
23 std::string* output) { 24 std::string* output) {
24 if (!header_value.empty()) { 25 if (!header_value.empty()) {
25 *output += header_name + ": " + header_value + "\n"; 26 *output += header_name + ": " + header_value + "\n";
26 } 27 }
27 } 28 }
28 29
29 // Appends the header name and the value to |output|, if the value is 30 // Appends the header name and the value to |output|, if the value is
30 // nonzero. 31 // nonzero.
31 static void AppendUint32Header(const std::string& header_name, 32 static void AppendUint32Header(const std::string& header_name,
32 uint32 header_value, 33 uint32 header_value,
33 std::string* output) { 34 std::string* output) {
34 if (header_value != 0) { 35 if (header_value != 0) {
35 *output += (header_name + ": " + base::StringPrintf("%u", header_value) + 36 *output += (header_name + ": " + base::StringPrintf("%u", header_value) +
36 "\n"); 37 "\n");
37 } 38 }
38 } 39 }
39 40
41 // Checks if |fd| is suitable for sending/receiving. We disallow
42 // directories to avoid potential sandbox escapes.
43 static bool CheckFileDescriptor(int fd) {
44 // NB: bypass i/o restrictions, current uses will not block
45 bool prev = base::ThreadRestrictions::SetIOAllowed(true);
satorux1 2012/05/04 22:31:22 ThreadRestrictions::ScopedAllowIO would be simpler
46 base::PlatformFileInfo info;
47 bool ok = base::GetPlatformFileInfo(fd, &info);
48 base::ThreadRestrictions::SetIOAllowed(prev);
49 return (ok && !info.is_directory);
50 }
51
40 } // namespace 52 } // namespace
41 53
42 namespace dbus { 54 namespace dbus {
43 55
44 Message::Message() 56 Message::Message()
45 : raw_message_(NULL) { 57 : raw_message_(NULL) {
46 } 58 }
47 59
48 Message::~Message() { 60 Message::~Message() {
49 if (raw_message_) 61 if (raw_message_)
(...skipping 634 matching lines...) Expand 10 before | Expand all | Expand 10 after
684 const std::string signature = base::StringPrintf("%c", dbus_type); 696 const std::string signature = base::StringPrintf("%c", dbus_type);
685 MessageWriter variant_writer(message_); 697 MessageWriter variant_writer(message_);
686 OpenVariant(signature, &variant_writer); 698 OpenVariant(signature, &variant_writer);
687 variant_writer.AppendBasic(dbus_type, value); 699 variant_writer.AppendBasic(dbus_type, value);
688 CloseContainer(&variant_writer); 700 CloseContainer(&variant_writer);
689 } 701 }
690 702
691 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) { 703 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) {
692 CHECK(kDBusTypeUnixFdIsSupported); 704 CHECK(kDBusTypeUnixFdIsSupported);
693 705
694 base::PlatformFileInfo info;
695 int fd = value.value(); 706 int fd = value.value();
696 bool ok = base::GetPlatformFileInfo(fd, &info); 707 if (!CheckFileDescriptor(fd)) {
satorux1 2012/05/04 22:31:22 Maybe this wasn't the right place to validate this
697 if (!ok || info.is_directory) {
698 // NB: sending a directory potentially enables sandbox escape 708 // NB: sending a directory potentially enables sandbox escape
699 LOG(FATAL) << "Attempt to pass invalid file descriptor"; 709 LOG(FATAL) << "Attempt to pass invalid file descriptor";
700 } 710 }
701 AppendBasic(DBUS_TYPE_UNIX_FD, &fd); 711 AppendBasic(DBUS_TYPE_UNIX_FD, &fd);
702 } 712 }
703 713
704 // 714 //
705 // MessageReader implementation. 715 // MessageReader implementation.
706 // 716 //
707 717
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
961 } 971 }
962 972
963 bool MessageReader::PopFileDescriptor(FileDescriptor* value) { 973 bool MessageReader::PopFileDescriptor(FileDescriptor* value) {
964 CHECK(kDBusTypeUnixFdIsSupported); 974 CHECK(kDBusTypeUnixFdIsSupported);
965 975
966 int fd = -1; 976 int fd = -1;
967 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd); 977 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd);
968 if (!success) 978 if (!success)
969 return false; 979 return false;
970 980
971 base::PlatformFileInfo info; 981 if (!CheckFileDescriptor(fd)) {
972 bool ok = base::GetPlatformFileInfo(fd, &info);
973 if (!ok || info.is_directory) {
974 base::ClosePlatformFile(fd); 982 base::ClosePlatformFile(fd);
975 // NB: receiving a directory potentially enables sandbox escape 983 // NB: receiving a directory potentially enables sandbox escape
976 LOG(FATAL) << "Attempt to receive invalid file descriptor"; 984 LOG(FATAL) << "Attempt to receive invalid file descriptor";
977 return false; // NB: not reached 985 return false; // NB: not reached
978 } 986 }
979 value->PutValue(fd); 987 value->PutValue(fd);
980 return true; 988 return true;
981 } 989 }
982 990
983 } // namespace dbus 991 } // namespace dbus
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698