Avi Drissman | ea1be23 | 2022-09-14 23:29:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Xiaohan Wang | 0ecc876d | 2022-01-07 19:57:15 | [diff] [blame] | 5 | #include "build/build_config.h" |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 6 | |
Greg Thompson | bec327a7 | 2021-11-10 16:31:48 | [diff] [blame] | 7 | #if BUILDFLAG(IS_MAC) |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 8 | extern "C" { |
| 9 | #include <sandbox.h> |
Nico Weber | e86ed9b3 | 2019-02-22 18:11:12 | [diff] [blame] | 10 | } |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 11 | #endif |
Greg Thompson | bec327a7 | 2021-11-10 16:31:48 | [diff] [blame] | 12 | |
[email protected] | d67c249 | 2009-03-20 17:26:02 | [diff] [blame] | 13 | #include <fcntl.h> |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 14 | #include <stddef.h> |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 15 | #include <sys/socket.h> |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 16 | #include <sys/stat.h> |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 17 | #include <unistd.h> |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 18 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 19 | #include <memory> |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 20 | #include <queue> |
| 21 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 22 | #include "base/file_descriptor_posix.h" |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 23 | #include "base/pickle.h" |
| 24 | #include "base/posix/eintr_wrapper.h" |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 25 | #include "base/run_loop.h" |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 26 | #include "base/synchronization/waitable_event.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 27 | #include "base/task/single_thread_task_runner.h" |
gab | 4d92485f | 2016-09-26 21:00:45 | [diff] [blame] | 28 | #include "base/threading/thread.h" |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 29 | #include "ipc/ipc_message_attachment_set.h" |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 30 | #include "ipc/ipc_message_utils.h" |
| 31 | #include "ipc/ipc_test_base.h" |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 32 | |
Greg Thompson | bec327a7 | 2021-11-10 16:31:48 | [diff] [blame] | 33 | #if BUILDFLAG(IS_MAC) |
kerrnel | dc059fe | 2016-04-12 16:39:06 | [diff] [blame] | 34 | #include "sandbox/mac/seatbelt.h" |
Greg Thompson | 9360c5d4 | 2021-12-01 17:55:34 | [diff] [blame] | 35 | #elif BUILDFLAG(IS_FUCHSIA) |
| 36 | #include "base/memory/scoped_refptr.h" |
| 37 | #include "base/test/scoped_dev_zero_fuchsia.h" |
kerrnel | dc059fe | 2016-04-12 16:39:06 | [diff] [blame] | 38 | #endif |
| 39 | |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 40 | namespace { |
| 41 | |
yusukes | c986c543 | 2015-05-06 19:45:45 | [diff] [blame] | 42 | const unsigned kNumFDsToSend = 7; // per message |
| 43 | const unsigned kNumMessages = 20; |
[email protected] | 92639446 | 2009-02-11 23:23:12 | [diff] [blame] | 44 | const char* kDevZeroPath = "/dev/zero"; |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 45 | |
anujk.sharma | 5a7ffe2f | 2015-01-22 05:39:37 | [diff] [blame] | 46 | static_assert(kNumFDsToSend == |
| 47 | IPC::MessageAttachmentSet::kMaxDescriptorsPerMessage, |
| 48 | "The number of FDs to send must be kMaxDescriptorsPerMessage."); |
yusukes | d0329fc | 2015-01-06 06:56:34 | [diff] [blame] | 49 | |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 50 | class MyChannelDescriptorListenerBase : public IPC::Listener { |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 51 | public: |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 52 | bool OnMessageReceived(const IPC::Message& message) override { |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 53 | base::PickleIterator iter(message); |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 54 | base::FileDescriptor descriptor; |
yusukes | d0329fc | 2015-01-06 06:56:34 | [diff] [blame] | 55 | while (IPC::ParamTraits<base::FileDescriptor>::Read( |
| 56 | &message, &iter, &descriptor)) { |
| 57 | HandleFD(descriptor.fd); |
| 58 | } |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 59 | return true; |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 60 | } |
| 61 | |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 62 | protected: |
| 63 | virtual void HandleFD(int fd) = 0; |
| 64 | }; |
| 65 | |
| 66 | class MyChannelDescriptorListener : public MyChannelDescriptorListenerBase { |
| 67 | public: |
| 68 | explicit MyChannelDescriptorListener(ino_t expected_inode_num) |
| 69 | : MyChannelDescriptorListenerBase(), |
| 70 | expected_inode_num_(expected_inode_num), |
| 71 | num_fds_received_(0) { |
[email protected] | 6b47b4d | 2013-10-10 21:12:14 | [diff] [blame] | 72 | } |
| 73 | |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 74 | unsigned num_fds_received() const { |
| 75 | return num_fds_received_; |
[email protected] | 6b47b4d | 2013-10-10 21:12:14 | [diff] [blame] | 76 | } |
Helmut Januschka | 448ebd7 | 2024-01-09 00:47:46 | [diff] [blame] | 77 | void Run() { loop_.Run(); } |
| 78 | void OnChannelError() override { loop_.QuitWhenIdle(); } |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 79 | |
| 80 | protected: |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 81 | void HandleFD(int fd) override { |
yusukes | d0329fc | 2015-01-06 06:56:34 | [diff] [blame] | 82 | ASSERT_GE(fd, 0); |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 83 | // Check that we can read from the FD. |
| 84 | char buf; |
| 85 | ssize_t amt_read = read(fd, &buf, 1); |
| 86 | ASSERT_EQ(amt_read, 1); |
| 87 | ASSERT_EQ(buf, 0); // /dev/zero always reads 0 bytes. |
| 88 | |
| 89 | struct stat st; |
| 90 | ASSERT_EQ(fstat(fd, &st), 0); |
| 91 | |
| 92 | ASSERT_EQ(close(fd), 0); |
| 93 | |
| 94 | // Compare inode numbers to check that the file sent over the wire is |
| 95 | // actually the one expected. |
| 96 | ASSERT_EQ(expected_inode_num_, st.st_ino); |
| 97 | |
| 98 | ++num_fds_received_; |
Helmut Januschka | 448ebd7 | 2024-01-09 00:47:46 | [diff] [blame] | 99 | if (num_fds_received_ == kNumFDsToSend * kNumMessages) { |
| 100 | loop_.QuitWhenIdle(); |
| 101 | } |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 102 | } |
| 103 | |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 104 | private: |
| 105 | ino_t expected_inode_num_; |
[email protected] | 92639446 | 2009-02-11 23:23:12 | [diff] [blame] | 106 | unsigned num_fds_received_; |
Helmut Januschka | 448ebd7 | 2024-01-09 00:47:46 | [diff] [blame] | 107 | base::RunLoop loop_; |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 108 | }; |
| 109 | |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 110 | class IPCSendFdsTest : public IPCChannelMojoTestBase { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 111 | protected: |
Greg Thompson | 9360c5d4 | 2021-12-01 17:55:34 | [diff] [blame] | 112 | void SetUp() override { |
| 113 | #if BUILDFLAG(IS_FUCHSIA) |
| 114 | ASSERT_TRUE(dev_zero_); |
| 115 | #endif |
| 116 | } |
| 117 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 118 | void RunServer() { |
| 119 | // Set up IPC channel and start client. |
| 120 | MyChannelDescriptorListener listener(-1); |
| 121 | CreateChannel(&listener); |
| 122 | ASSERT_TRUE(ConnectChannel()); |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 123 | |
yusukes | d0329fc | 2015-01-06 06:56:34 | [diff] [blame] | 124 | for (unsigned i = 0; i < kNumMessages; ++i) { |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 125 | IPC::Message* message = |
| 126 | new IPC::Message(0, 3, IPC::Message::PRIORITY_NORMAL); |
yusukes | d0329fc | 2015-01-06 06:56:34 | [diff] [blame] | 127 | for (unsigned j = 0; j < kNumFDsToSend; ++j) { |
| 128 | const int fd = open(kDevZeroPath, O_RDONLY); |
| 129 | ASSERT_GE(fd, 0); |
| 130 | base::FileDescriptor descriptor(fd, true); |
| 131 | IPC::ParamTraits<base::FileDescriptor>::Write(message, descriptor); |
| 132 | } |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 133 | ASSERT_TRUE(sender()->Send(message)); |
| 134 | } |
| 135 | |
| 136 | // Run message loop. |
Helmut Januschka | 448ebd7 | 2024-01-09 00:47:46 | [diff] [blame] | 137 | listener.Run(); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 138 | |
| 139 | // Close the channel so the client's OnChannelError() gets fired. |
| 140 | channel()->Close(); |
| 141 | |
| 142 | EXPECT_TRUE(WaitForClientShutdown()); |
| 143 | DestroyChannel(); |
[email protected] | 92639446 | 2009-02-11 23:23:12 | [diff] [blame] | 144 | } |
Greg Thompson | 9360c5d4 | 2021-12-01 17:55:34 | [diff] [blame] | 145 | |
| 146 | private: |
| 147 | #if BUILDFLAG(IS_FUCHSIA) |
| 148 | scoped_refptr<base::ScopedDevZero> dev_zero_ = base::ScopedDevZero::Get(); |
| 149 | #endif |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 150 | }; |
| 151 | |
Greg Thompson | 9360c5d4 | 2021-12-01 17:55:34 | [diff] [blame] | 152 | // Disabled on Fuchsia due to failures; see https://crbug.com/1272424. |
| 153 | #if BUILDFLAG(IS_FUCHSIA) |
| 154 | #define MAYBE_DescriptorTest DISABLED_DescriptorTest |
| 155 | #else |
| 156 | #define MAYBE_DescriptorTest DescriptorTest |
| 157 | #endif |
| 158 | TEST_F(IPCSendFdsTest, MAYBE_DescriptorTest) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 159 | Init("SendFdsClient"); |
| 160 | RunServer(); |
| 161 | } |
| 162 | |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 163 | class SendFdsTestClientFixture : public IpcChannelMojoTestClient { |
| 164 | protected: |
| 165 | void SendFdsClientCommon(const std::string& test_client_name, |
| 166 | ino_t expected_inode_num) { |
| 167 | MyChannelDescriptorListener listener(expected_inode_num); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 168 | |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 169 | // Set up IPC channel. |
| 170 | Connect(&listener); |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 171 | |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 172 | // Run message loop. |
Helmut Januschka | 448ebd7 | 2024-01-09 00:47:46 | [diff] [blame] | 173 | listener.Run(); |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 174 | |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 175 | // Verify that the message loop was exited due to getting the correct number |
| 176 | // of descriptors, and not because of the channel closing unexpectedly. |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 177 | EXPECT_EQ(kNumFDsToSend * kNumMessages, listener.num_fds_received()); |
[email protected] | d484ab5 | 2010-12-09 01:12:20 | [diff] [blame] | 178 | |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 179 | Close(); |
| 180 | } |
| 181 | }; |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 182 | |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 183 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE( |
| 184 | SendFdsClient, |
| 185 | SendFdsTestClientFixture) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 186 | struct stat st; |
| 187 | int fd = open(kDevZeroPath, O_RDONLY); |
| 188 | fstat(fd, &st); |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 189 | EXPECT_GE(IGNORE_EINTR(close(fd)), 0); |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 190 | SendFdsClientCommon("SendFdsClient", st.st_ino); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 191 | } |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 192 | |
Greg Thompson | bec327a7 | 2021-11-10 16:31:48 | [diff] [blame] | 193 | #if BUILDFLAG(IS_MAC) |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 194 | // Test that FDs are correctly sent to a sandboxed process. |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 195 | // TODO(port): Make this test cross-platform. |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 196 | TEST_F(IPCSendFdsTest, DescriptorTestSandboxed) { |
| 197 | Init("SendFdsSandboxedClient"); |
| 198 | RunServer(); |
| 199 | } |
| 200 | |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 201 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE( |
| 202 | SendFdsSandboxedClient, |
| 203 | SendFdsTestClientFixture) { |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 204 | struct stat st; |
[email protected] | 92639446 | 2009-02-11 23:23:12 | [diff] [blame] | 205 | const int fd = open(kDevZeroPath, O_RDONLY); |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 206 | fstat(fd, &st); |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 207 | ASSERT_LE(0, IGNORE_EINTR(close(fd))); |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 208 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 209 | // Enable the sandbox. |
Robert Sesek | a4faabc | 2022-12-01 18:19:58 | [diff] [blame] | 210 | std::string error; |
| 211 | ASSERT_TRUE(sandbox::Seatbelt::Init( |
| 212 | sandbox::Seatbelt::kProfilePureComputation, SANDBOX_NAMED, &error)) |
| 213 | << error; |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 214 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 215 | // Make sure sandbox is really enabled. |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 216 | ASSERT_EQ(-1, open(kDevZeroPath, O_RDONLY)) |
| 217 | << "Sandbox wasn't properly enabled"; |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 218 | |
| 219 | // See if we can receive a file descriptor. |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 220 | SendFdsClientCommon("SendFdsSandboxedClient", st.st_ino); |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 221 | } |
Greg Thompson | bec327a7 | 2021-11-10 16:31:48 | [diff] [blame] | 222 | #endif // BUILDFLAG(IS_MAC) |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 223 | |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 224 | } // namespace |