| OLD | NEW |
| 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" | |
| 13 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 14 #include "dbus/object_path.h" | 13 #include "dbus/object_path.h" |
| 15 #include "third_party/protobuf/src/google/protobuf/message_lite.h" | 14 #include "third_party/protobuf/src/google/protobuf/message_lite.h" |
| 16 | 15 |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 // Appends the header name and the value to |output|, if the value is | 18 // Appends the header name and the value to |output|, if the value is |
| 20 // not empty. | 19 // not empty. |
| 21 static void AppendStringHeader(const std::string& header_name, | 20 static void AppendStringHeader(const std::string& header_name, |
| 22 const std::string& header_value, | 21 const std::string& header_value, |
| (...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 684 const std::string signature = base::StringPrintf("%c", dbus_type); | 683 const std::string signature = base::StringPrintf("%c", dbus_type); |
| 685 MessageWriter variant_writer(message_); | 684 MessageWriter variant_writer(message_); |
| 686 OpenVariant(signature, &variant_writer); | 685 OpenVariant(signature, &variant_writer); |
| 687 variant_writer.AppendBasic(dbus_type, value); | 686 variant_writer.AppendBasic(dbus_type, value); |
| 688 CloseContainer(&variant_writer); | 687 CloseContainer(&variant_writer); |
| 689 } | 688 } |
| 690 | 689 |
| 691 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) { | 690 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) { |
| 692 CHECK(kDBusTypeUnixFdIsSupported); | 691 CHECK(kDBusTypeUnixFdIsSupported); |
| 693 | 692 |
| 694 base::PlatformFileInfo info; | 693 if (!value.is_valid()) { |
| 695 int fd = value.value(); | |
| 696 bool ok = base::GetPlatformFileInfo(fd, &info); | |
| 697 if (!ok || info.is_directory) { | |
| 698 // NB: sending a directory potentially enables sandbox escape | 694 // NB: sending a directory potentially enables sandbox escape |
| 699 LOG(FATAL) << "Attempt to pass invalid file descriptor"; | 695 LOG(FATAL) << "Attempt to pass invalid file descriptor"; |
| 700 } | 696 } |
| 697 int fd = value.value(); |
| 701 AppendBasic(DBUS_TYPE_UNIX_FD, &fd); | 698 AppendBasic(DBUS_TYPE_UNIX_FD, &fd); |
| 702 } | 699 } |
| 703 | 700 |
| 704 // | 701 // |
| 705 // MessageReader implementation. | 702 // MessageReader implementation. |
| 706 // | 703 // |
| 707 | 704 |
| 708 MessageReader::MessageReader(Message* message) | 705 MessageReader::MessageReader(Message* message) |
| 709 : message_(message) { | 706 : message_(message) { |
| 710 memset(&raw_message_iter_, 0, sizeof(raw_message_iter_)); | 707 memset(&raw_message_iter_, 0, sizeof(raw_message_iter_)); |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 961 } | 958 } |
| 962 | 959 |
| 963 bool MessageReader::PopFileDescriptor(FileDescriptor* value) { | 960 bool MessageReader::PopFileDescriptor(FileDescriptor* value) { |
| 964 CHECK(kDBusTypeUnixFdIsSupported); | 961 CHECK(kDBusTypeUnixFdIsSupported); |
| 965 | 962 |
| 966 int fd = -1; | 963 int fd = -1; |
| 967 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd); | 964 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd); |
| 968 if (!success) | 965 if (!success) |
| 969 return false; | 966 return false; |
| 970 | 967 |
| 971 base::PlatformFileInfo info; | |
| 972 bool ok = base::GetPlatformFileInfo(fd, &info); | |
| 973 if (!ok || info.is_directory) { | |
| 974 base::ClosePlatformFile(fd); | |
| 975 // NB: receiving a directory potentially enables sandbox escape | |
| 976 LOG(FATAL) << "Attempt to receive invalid file descriptor"; | |
| 977 return false; // NB: not reached | |
| 978 } | |
| 979 value->PutValue(fd); | 968 value->PutValue(fd); |
| 969 // NB: the caller must check validity before using the value |
| 980 return true; | 970 return true; |
| 981 } | 971 } |
| 982 | 972 |
| 983 } // namespace dbus | 973 } // namespace dbus |
| OLD | NEW |