| // Copyright 2025 The Fuchsia Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include <fcntl.h> | |
| #include <lib/fit/defer.h> | |
| #include <lib/fit/function.h> | |
| #include <poll.h> | |
| #include <stdint.h> | |
| #include <string.h> | |
| #include <sys/ioctl.h> | |
| #include <sys/mman.h> | |
| #include <sys/mount.h> | |
| #include <sys/socket.h> | |
| #include <sys/stat.h> | |
| #include <sys/sysmacros.h> | |
| #include <unistd.h> | |
| #include <algorithm> | |
| #include <array> | |
| #include <atomic> | |
| #include <format> | |
| #include <string> | |
| #include <thread> | |
| #include <vector> | |
| #include <fbl/unique_fd.h> | |
| #include <gmock/gmock.h> | |
| #include <gtest/gtest.h> | |
| #include <linux/android/binder.h> | |
| #include <linux/android/binderfs.h> | |
| #include <linux/netlink.h> | |
| #include "src/starnix/tests/syscalls/cpp/binder/common.h" | |
| #include "src/starnix/tests/syscalls/cpp/binder/manager_provider_client_test.h" | |
| #include "src/starnix/tests/syscalls/cpp/binder_helper.h" | |
| #include "src/starnix/tests/syscalls/cpp/syscall_matchers.h" | |
| #include "src/starnix/tests/syscalls/cpp/test_helper.h" | |
| namespace { | |
| #ifndef BINDER_FREEZE | |
| struct binder_freeze_info { | |
| __u32 pid; | |
| __u32 enable; | |
| __u32 timeout_ms; | |
| }; | |
| struct binder_frozen_status_info { | |
| __u32 pid; | |
| __u32 sync_recv; | |
| __u32 async_recv; | |
| }; | |
| #define BINDER_FREEZE _IOW('b', 14, struct binder_freeze_info) | |
| #define BINDER_GET_FROZEN_INFO _IOWR('b', 15, struct binder_frozen_status_info) | |
| constexpr auto BR_FROZEN_REPLY = static_cast<binder_driver_return_protocol>(_IO('r', 18)); | |
| constexpr auto BR_TRANSACTION_PENDING_FROZEN = | |
| static_cast<binder_driver_return_protocol>(_IO('r', 20)); | |
| #endif | |
| bool skip_binder_tests = false; | |
| class BinderTest : public ::testing::Test { | |
| public: | |
| static void SetUpTestSuite() { | |
| // The unshare() call will isolate the mount namespaces for the running | |
| // test process. This allows the Linux-based tests to execute syscalls with | |
| // root permissions, without fear of messing the environment up. While the | |
| // Starnix tests don't strictly need to unshare, it's beneficial to run the | |
| // same test binaries on Linux and on Starnix so we can be sure the semantics | |
| // match. As a side effect, this means that the mounted directories will not | |
| // be viewable in traditional ways, e.g. ffx component explore. | |
| // TODO(https://fxbug.dev/317285180) don't skip on baseline | |
| int rv = unshare(CLONE_NEWNS); | |
| if (rv == -1 && errno == EPERM) { | |
| // GTest does not support GTEST_SKIP() from a suite setup, so record that we want to skip | |
| // every test here and skip in SetUp(). | |
| skip_binder_tests = true; | |
| return; | |
| } | |
| ASSERT_EQ(rv, 0) << "unshare(CLONE_NEWNS) failed: " << strerror(errno) << "(" << errno << ")"; | |
| } | |
| void SetUp() override { | |
| if (skip_binder_tests) { | |
| GTEST_SKIP() << "Permission denied for unshare(CLONE_NEWNS), skipping suite."; | |
| } | |
| ASSERT_FALSE(temp_dir_.path().empty()); | |
| // Listen to uevents during mount. | |
| uevent_fd = fbl::unique_fd(socket(AF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT)); | |
| ASSERT_TRUE(uevent_fd) << strerror(errno); | |
| int buf_sz = 16 * 1024 * 1024; | |
| ASSERT_THAT(setsockopt(uevent_fd.get(), SOL_SOCKET, SO_RCVBUF, &buf_sz, sizeof(buf_sz)), | |
| SyscallSucceeds()); | |
| struct sockaddr_nl addr = { | |
| .nl_family = AF_NETLINK, | |
| .nl_pid = 0, | |
| .nl_groups = 0xffffffff, | |
| }; | |
| ASSERT_THAT(bind(uevent_fd.get(), (struct sockaddr*)&addr, sizeof(addr)), SyscallSucceeds()); | |
| // Mount sysfs to check for device presence. | |
| ASSERT_THAT(mkdir(TestPath("sys").c_str(), 0o700), SyscallSucceeds()); | |
| ASSERT_THAT(mount("sysfs", TestPath("sys").c_str(), "sysfs", 0, nullptr), SyscallSucceeds()); | |
| ASSERT_THAT(mkdir(TestPath("binderfs").c_str(), 0o700), SyscallSucceeds()); | |
| if (mount("binder", TestPath("binderfs").c_str(), "binder", 0, nullptr) < 0) { | |
| ASSERT_EQ(errno, ENODEV); | |
| GTEST_SKIP() << "binderfs is not available, skipping test."; | |
| } | |
| } | |
| std::string TestPath(const char* path) const { return temp_dir_.path() + "/" + path; } | |
| ::testing::AssertionResult NoUeventReceived() { | |
| char buffer[4096]; | |
| ssize_t bytes = recv(uevent_fd.get(), buffer, sizeof(buffer), MSG_DONTWAIT); | |
| if (bytes != -1) { | |
| return ::testing::AssertionFailure() << "Received uevent"; | |
| } else if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
| return ::testing::AssertionFailure() << "Recv failed: " << strerror(errno); | |
| } else { | |
| return ::testing::AssertionSuccess(); | |
| } | |
| } | |
| private: | |
| test_helper::ScopedTempDir temp_dir_; | |
| fbl::unique_fd uevent_fd; | |
| }; | |
| TEST_F(BinderTest, NoUeventOnMount) { ASSERT_TRUE(NoUeventReceived()); } | |
| TEST_F(BinderTest, SetContextMgrWithNull) { | |
| fbl::unique_fd binder = | |
| fbl::unique_fd(open(TestPath("binderfs/binder").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder) << strerror(errno); | |
| EXPECT_THAT(ioctl(binder.get(), BINDER_SET_CONTEXT_MGR, 0), SyscallSucceeds()); | |
| } | |
| TEST_F(BinderTest, InvalidCommandError) { | |
| fbl::unique_fd binder = | |
| fbl::unique_fd(open(TestPath("binderfs/binder").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder) << strerror(errno); | |
| uint32_t writebuf[1]; | |
| writebuf[0] = -1; | |
| struct binder_write_read bwr = {}; | |
| bwr.write_buffer = (binder_uintptr_t)writebuf; | |
| bwr.write_size = sizeof(uint32_t); | |
| bwr.write_consumed = 0; | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_WRITE_READ, &bwr), SyscallFailsWithErrno(EINVAL)); | |
| // The failing command is not consumed. | |
| EXPECT_EQ(bwr.write_consumed, size_t(0)); | |
| } | |
| TEST_F(BinderTest, ValidThenInvalidCommand) { | |
| fbl::unique_fd binder = | |
| fbl::unique_fd(open(TestPath("binderfs/binder").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder) << strerror(errno); | |
| uint32_t writebuf[2]; | |
| writebuf[0] = BC_ENTER_LOOPER; | |
| writebuf[1] = -1; | |
| struct binder_write_read bwr = {}; | |
| bwr.write_buffer = (binder_uintptr_t)writebuf; | |
| bwr.write_size = 2 * sizeof(uint32_t); | |
| bwr.write_consumed = 0; | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_WRITE_READ, &bwr), SyscallFailsWithErrno(EINVAL)); | |
| // The first command is consumed. | |
| EXPECT_EQ(bwr.write_consumed, sizeof(uint32_t)); | |
| } | |
| TEST_F(BinderTest, IgnoreAlreadyConsumed) { | |
| fbl::unique_fd binder = | |
| fbl::unique_fd(open(TestPath("binderfs/binder").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder) << strerror(errno); | |
| uint32_t writebuf[2]; | |
| writebuf[0] = -1; | |
| writebuf[1] = BC_ENTER_LOOPER; | |
| struct binder_write_read bwr = {}; | |
| bwr.write_buffer = (binder_uintptr_t)writebuf; | |
| bwr.write_size = 2 * sizeof(uint32_t); | |
| bwr.write_consumed = sizeof(uint32_t); | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_WRITE_READ, &bwr), SyscallSucceeds()); | |
| EXPECT_EQ(bwr.write_consumed, 2 * sizeof(uint32_t)); | |
| EXPECT_EQ(bwr.write_size, 2 * sizeof(uint32_t)); | |
| // We can reuse the structure for the next write, no command will be executed. | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_WRITE_READ, &bwr), SyscallSucceeds()); | |
| } | |
| TEST_F(BinderTest, BinderControl) { | |
| fbl::unique_fd binder_control = | |
| fbl::unique_fd(open(TestPath("binderfs/binder-control").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder_control) << strerror(errno); | |
| struct binderfs_device new_device = {}; | |
| std::string kBinderName = "binder-test"; | |
| std::ranges::copy(kBinderName, new_device.name); | |
| EXPECT_THAT(ioctl(binder_control.get(), BINDER_CTL_ADD, &new_device), SyscallSucceeds()); | |
| // The ioctl set the correct major and minor numbers. | |
| struct stat sb = {}; | |
| EXPECT_THAT(stat(TestPath("binderfs/binder-test").c_str(), &sb), SyscallSucceeds()); | |
| EXPECT_EQ(sb.st_mode, S_IFCHR | 0o600u); | |
| EXPECT_EQ(major(sb.st_rdev), new_device.major); | |
| EXPECT_EQ(minor(sb.st_rdev), new_device.minor); | |
| // The result is a usable binder device. | |
| fbl::unique_fd binder = | |
| fbl::unique_fd(open(TestPath("binderfs/binder-test").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder) << strerror(errno); | |
| struct binder_version version = {}; | |
| EXPECT_THAT(ioctl(binder.get(), BINDER_VERSION, &version), SyscallSucceeds()); | |
| EXPECT_TRUE(NoUeventReceived()); | |
| // sysfs has entries for usual devices (1:3 is /dev/null) | |
| EXPECT_THAT(access(TestPath("sys/dev/char/1:3/").c_str(), F_OK), SyscallSucceeds()); | |
| // It doesn't have an entry for our new binder device. | |
| EXPECT_THAT( | |
| access(std::format("{}/{}:{}", TestPath("sys/dev/char"), new_device.major, new_device.minor) | |
| .c_str(), | |
| F_OK), | |
| SyscallFailsWithErrno(ENOENT)); | |
| // It doesn't have an entry for binder-control. | |
| EXPECT_THAT(fstat(binder_control.get(), &sb), SyscallSucceeds()); | |
| EXPECT_THAT( | |
| access(std::format("{}/{}:{}", TestPath("sys/dev/char"), major(sb.st_rdev), minor(sb.st_rdev)) | |
| .c_str(), | |
| F_OK), | |
| SyscallFailsWithErrno(ENOENT)); | |
| } | |
| TEST_F(BinderTest, BinderControlExists) { | |
| fbl::unique_fd binder_control = | |
| fbl::unique_fd(open(TestPath("binderfs/binder-control").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder_control) << strerror(errno); | |
| struct binderfs_device new_device = {}; | |
| std::string kBinderName = "binder"; | |
| std::ranges::copy(kBinderName, new_device.name); | |
| EXPECT_THAT(ioctl(binder_control.get(), BINDER_CTL_ADD, &new_device), | |
| SyscallFailsWithErrno(EEXIST)); | |
| } | |
| TEST_F(BinderTest, BinderControlInvalidPathDot) { | |
| fbl::unique_fd binder_control = | |
| fbl::unique_fd(open(TestPath("binderfs/binder-control").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder_control) << strerror(errno); | |
| struct binderfs_device new_device = {}; | |
| std::string kBinderName = "."; | |
| std::ranges::copy(kBinderName, new_device.name); | |
| EXPECT_THAT(ioctl(binder_control.get(), BINDER_CTL_ADD, &new_device), | |
| SyscallFailsWithErrno(EACCES)); | |
| } | |
| TEST_F(BinderTest, BinderControlInvalidPathDotDot) { | |
| fbl::unique_fd binder_control = | |
| fbl::unique_fd(open(TestPath("binderfs/binder-control").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder_control) << strerror(errno); | |
| struct binderfs_device new_device = {}; | |
| std::string kBinderName = ".."; | |
| std::ranges::copy(kBinderName, new_device.name); | |
| EXPECT_THAT(ioctl(binder_control.get(), BINDER_CTL_ADD, &new_device), | |
| SyscallFailsWithErrno(EACCES)); | |
| } | |
| TEST_F(BinderTest, BinderControlInvalidPathEmpty) { | |
| fbl::unique_fd binder_control = | |
| fbl::unique_fd(open(TestPath("binderfs/binder-control").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder_control) << strerror(errno); | |
| struct binderfs_device new_device = {}; | |
| std::string kBinderName; | |
| std::ranges::copy(kBinderName, new_device.name); | |
| EXPECT_THAT(ioctl(binder_control.get(), BINDER_CTL_ADD, &new_device), | |
| SyscallFailsWithErrno(EACCES)); | |
| } | |
| TEST_F(BinderTest, BinderControlInvalidPathReserved) { | |
| fbl::unique_fd binder_control = | |
| fbl::unique_fd(open(TestPath("binderfs/binder-control").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder_control) << strerror(errno); | |
| struct binderfs_device new_device = {}; | |
| std::string kBinderName = "binder-control"; | |
| std::ranges::copy(kBinderName, new_device.name); | |
| EXPECT_THAT(ioctl(binder_control.get(), BINDER_CTL_ADD, &new_device), | |
| SyscallFailsWithErrno(EEXIST)); | |
| } | |
| TEST_F(BinderTest, BinderControlInvalidPathSlash) { | |
| fbl::unique_fd binder_control = | |
| fbl::unique_fd(open(TestPath("binderfs/binder-control").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder_control) << strerror(errno); | |
| struct binderfs_device new_device = {}; | |
| std::string kBinderName = "my/binder"; | |
| std::ranges::copy(kBinderName, new_device.name); | |
| EXPECT_THAT(ioctl(binder_control.get(), BINDER_CTL_ADD, &new_device), | |
| SyscallFailsWithErrno(EACCES)); | |
| } | |
| // This test verifies that sending a file descriptor to a process that is in the middle of exiting | |
| // does not crash the kernel or return a protocol error. Instead, it should fail gracefully, | |
| // eventually returning BR_DEAD_REPLY or BR_FAILED_REPLY. | |
| TEST_F(BinderTest, SendFdToExitingProcess) { | |
| using namespace starnix_binder; | |
| auto manager_ready = test_helper::MakeRendezvous(); | |
| pid_t manager_pid = 0; | |
| // Spawn the manager using the framework helper. | |
| auto manager = ManagerProcess( | |
| TestPath("binderfs"), | |
| [&](test_helper::ForkHelper& fork_helper, fit::closure manager_behavior) { | |
| manager_pid = fork_helper.RunInForkedProcess(std::move(manager_behavior)); | |
| return manager_pid; | |
| }, | |
| std::move(manager_ready.poker)); | |
| manager_ready.holder.hold(); | |
| auto binder_and_map = OpenBinderAndMap(TestPath("binderfs")); | |
| ASSERT_TRUE(binder_and_map.fd_); | |
| ASSERT_THAT(binder_and_map.mapping_, SyscallResultIsOk()); | |
| const auto& binder = binder_and_map.fd_; | |
| // Prepare transaction with FD. | |
| fbl::unique_fd fd_to_send(SAFE_SYSCALL(open("/dev/null", O_RDONLY))); | |
| FdTransaction fd_transaction(0, kServiceSendFd, fd_to_send.get()); | |
| const auto& write_buffer = fd_transaction.write_buffer; | |
| // Spawn a background thread that continuously sends the transaction with the FD to the manager. | |
| // The transaction is two-way and the manager does not reply, so the thread will block in ioctl() | |
| // until the manager process is killed. | |
| auto send_started = test_helper::MakeRendezvous(); | |
| std::atomic<int> ioctl_errno(0); | |
| std::atomic<bool> got_error(false); | |
| std::atomic<bool> got_dead_or_failed_reply(false); | |
| std::thread send_thread([&, send_started = std::move(send_started.poker)]() mutable { | |
| bool starting = true; | |
| while (true) { | |
| struct binder_write_read bwr = {}; | |
| bwr.write_buffer = (binder_uintptr_t)&write_buffer; | |
| bwr.write_size = sizeof(write_buffer); | |
| bwr.write_consumed = 0; | |
| uint32_t read_buf[32] = {}; | |
| bwr.read_buffer = (binder_uintptr_t)read_buf; | |
| bwr.read_size = sizeof(read_buf); | |
| bwr.read_consumed = 0; | |
| // Wait to poke send_started until we are about to enter ioctl(). | |
| if (starting) { | |
| starting = false; | |
| send_started.poke(); | |
| } | |
| if (ioctl(binder.get(), BINDER_WRITE_READ, &bwr) < 0) { | |
| ioctl_errno = errno; | |
| break; | |
| } | |
| ParsedMessage pm = ParseMessage((binder_uintptr_t)read_buf, bwr.read_consumed); | |
| for (auto cmd : pm.returns_) { | |
| if (cmd == BR_ERROR) { | |
| got_error = true; | |
| return; | |
| } | |
| if (cmd == BR_DEAD_REPLY || cmd == BR_FAILED_REPLY) { | |
| got_dead_or_failed_reply = true; | |
| return; | |
| } | |
| } | |
| } | |
| }); | |
| send_started.holder.hold(); | |
| // Kill the manager and wait for the send thread to finish. | |
| manager.call(); | |
| send_thread.join(); | |
| // We expect the transactions to fail with BR_DEAD_REPLY or BR_FAILED_REPLY after the child died. | |
| EXPECT_FALSE(got_error.load()); | |
| EXPECT_TRUE(got_dead_or_failed_reply.load()); | |
| EXPECT_THAT(ioctl_errno.load(), SyscallSucceedsWithValue(0)); | |
| } | |
| // Linux's binder driver rejects mmap with PROT_WRITE and clears VM_MAYWRITE to | |
| // prevent mprotect from adding write permission. Verify Starnix matches this | |
| // behavior. | |
| TEST_F(BinderTest, MmapRejectsProtWrite) { | |
| using namespace starnix_binder; | |
| fbl::unique_fd binder = | |
| fbl::unique_fd(open(TestPath("binderfs/binder").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder) << strerror(errno); | |
| auto mapping = test_helper::ScopedMMap::MMap(nullptr, kBinderMMapSize, PROT_READ | PROT_WRITE, | |
| MAP_PRIVATE, binder.get(), 0); | |
| ASSERT_TRUE(mapping.is_error()) << "mmap with PROT_WRITE should fail with EPERM"; | |
| EXPECT_EQ(mapping.error_value(), EPERM) << strerror(mapping.error_value()); | |
| } | |
| TEST_F(BinderTest, MprotectCannotAddWriteToBinder) { | |
| using namespace starnix_binder; | |
| fbl::unique_fd binder = | |
| fbl::unique_fd(open(TestPath("binderfs/binder").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder) << strerror(errno); | |
| auto mapping = test_helper::ScopedMMap::MMap(nullptr, kBinderMMapSize, PROT_READ, MAP_PRIVATE, | |
| binder.get(), 0); | |
| ASSERT_TRUE(mapping.is_ok()) << mapping.error_value(); | |
| EXPECT_THAT(mprotect(mapping->mapping(), kBinderMMapSize, PROT_READ | PROT_WRITE), | |
| SyscallFailsWithErrno(EACCES)) | |
| << "mprotect should not be able to add PROT_WRITE to a binder mapping"; | |
| } | |
| // In Linux Binder, binder objects are uniquely identified by the userspace pointer (ptr). | |
| // If userspace sends a binder object with an address that is already registered with a | |
| // different cookie, the driver detects a cookie mismatch and fails the transaction with | |
| // BR_FAILED_REPLY. | |
| TEST_F(BinderTest, BinderObjectCookieMismatch) { | |
| using namespace starnix_binder; | |
| auto receiver_ready = test_helper::MakeRendezvous(); | |
| test_helper::ForkHelper fork_helper; | |
| fork_helper.OnlyWaitForForkedChildren(); | |
| pid_t receiver_pid = fork_helper.RunInForkedProcess([&] { | |
| auto fd_and_mapping = OpenBinderAndMap(TestPath("binderfs")); | |
| ASSERT_THAT(ioctl(fd_and_mapping.fd_.get(), BINDER_SET_CONTEXT_MGR, 0), SyscallSucceeds()); | |
| EnterLooper(fd_and_mapping.fd_); | |
| receiver_ready.poker.poke(); | |
| while (true) { | |
| std::array<uint32_t, 32> read_buffer = {}; | |
| struct binder_write_read write_read = { | |
| .read_size = sizeof(read_buffer), | |
| .read_consumed = 0, | |
| .read_buffer = (binder_uintptr_t)read_buffer.data(), | |
| }; | |
| if (ioctl(fd_and_mapping.fd_.get(), BINDER_WRITE_READ, &write_read) < 0) { | |
| break; | |
| } | |
| } | |
| }); | |
| receiver_ready.holder.hold(); | |
| auto cleanup_receiver = fit::defer([&] { | |
| ASSERT_THAT(kill(receiver_pid, SIGKILL), SyscallSucceeds()); | |
| fork_helper.ExpectSignal(SIGKILL); | |
| ASSERT_TRUE(fork_helper.WaitForChildren()); | |
| }); | |
| auto binder_and_map = OpenBinderAndMap(TestPath("binderfs")); | |
| ASSERT_TRUE(binder_and_map.fd_); | |
| ASSERT_THAT(binder_and_map.mapping_, SyscallResultIsOk()); | |
| const auto& binder = binder_and_map.fd_; | |
| uintptr_t binder_ptr = 0x12345000; | |
| uintptr_t cookie1 = 0xaaaa0000; | |
| uintptr_t cookie2 = 0xbbbb0000; | |
| auto send_binder_object = [&](uintptr_t ptr, uintptr_t cookie) { | |
| struct flat_binder_object obj = { | |
| .hdr = {.type = BINDER_TYPE_BINDER}, | |
| .flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS, | |
| .binder = ptr, | |
| .cookie = cookie, | |
| }; | |
| binder_size_t offset = 0; | |
| TransactionWriteBuffer write_buffer = { | |
| .command = BC_TRANSACTION, | |
| .data = | |
| { | |
| .target = {.handle = kServiceManagerHandle}, | |
| .cookie = 0, | |
| .code = 1, | |
| .flags = TF_ONE_WAY, | |
| .data_size = sizeof(obj), | |
| .offsets_size = sizeof(offset), | |
| .data = | |
| { | |
| .ptr = | |
| { | |
| .buffer = (binder_uintptr_t)&obj, | |
| .offsets = (binder_uintptr_t)&offset, | |
| }, | |
| }, | |
| }, | |
| }; | |
| uint32_t read_buf[32] = {}; | |
| struct binder_write_read bwr = { | |
| .write_size = sizeof(write_buffer), | |
| .write_buffer = (binder_uintptr_t)&write_buffer, | |
| .read_size = sizeof(read_buf), | |
| .read_buffer = (binder_uintptr_t)read_buf, | |
| }; | |
| EXPECT_THAT(ioctl(binder.get(), BINDER_WRITE_READ, &bwr), SyscallSucceeds()); | |
| return ParseMessage((binder_uintptr_t)read_buf, bwr.read_consumed); | |
| }; | |
| // 1. Send first transaction with (binder_ptr, cookie1) to the receiver. | |
| ParsedMessage pm1 = send_binder_object(binder_ptr, cookie1); | |
| // When a new binder object is registered, the driver informs userspace that a reference | |
| // was acquired (BR_ACQUIRE), followed by transaction completion (BR_TRANSACTION_COMPLETE). | |
| if (std::find(pm1.returns_.begin(), pm1.returns_.end(), BR_TRANSACTION_COMPLETE) == | |
| pm1.returns_.end()) { | |
| EXPECT_THAT(pm1.returns_, testing::Contains(BR_ACQUIRE)); | |
| uint32_t read_buf1_comp[32] = {}; | |
| struct binder_write_read bwr1_comp = { | |
| .read_size = sizeof(read_buf1_comp), | |
| .read_buffer = (binder_uintptr_t)read_buf1_comp, | |
| }; | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_WRITE_READ, &bwr1_comp), SyscallSucceeds()); | |
| ParsedMessage pm1_comp = | |
| ParseMessage((binder_uintptr_t)read_buf1_comp, bwr1_comp.read_consumed); | |
| EXPECT_THAT(pm1_comp.returns_, testing::Contains(BR_TRANSACTION_COMPLETE)); | |
| } else { | |
| EXPECT_THAT(pm1.returns_, testing::Contains(BR_TRANSACTION_COMPLETE)); | |
| } | |
| EXPECT_THAT(pm1.returns_, testing::Not(testing::Contains(BR_FAILED_REPLY))); | |
| // 2. Send second transaction with the SAME binder_ptr, but DIFFERENT cookie2. | |
| // This must fail with BR_FAILED_REPLY due to cookie mismatch. | |
| ParsedMessage pm2 = send_binder_object(binder_ptr, cookie2); | |
| EXPECT_THAT(pm2.returns_, testing::Contains(BR_FAILED_REPLY)); | |
| EXPECT_THAT(pm2.returns_, testing::Not(testing::Contains(BR_TRANSACTION_COMPLETE))); | |
| // 3. Send third transaction with the SAME binder_ptr and matching cookie1. | |
| // This should succeed. | |
| ParsedMessage pm3 = send_binder_object(binder_ptr, cookie1); | |
| EXPECT_THAT(pm3.returns_, testing::Contains(BR_TRANSACTION_COMPLETE)); | |
| EXPECT_THAT(pm3.returns_, testing::Not(testing::Contains(BR_FAILED_REPLY))); | |
| } | |
| // Only the process that opened a binder FD may mmap it, regardless of whether the mapping is | |
| // private or shared. | |
| TEST_F(BinderTest, CrossProcessMmapFails) { | |
| using namespace starnix_binder; | |
| for (int flags : {MAP_PRIVATE, MAP_SHARED}) { | |
| SCOPED_TRACE(flags == MAP_PRIVATE ? "MAP_PRIVATE" : "MAP_SHARED"); | |
| // Each binder FD can only be mapped once, so open a new one for each mapping type. | |
| fbl::unique_fd binder = | |
| fbl::unique_fd(open(TestPath("binderfs/binder").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder) << strerror(errno); | |
| test_helper::ForkHelper helper; | |
| helper.RunInForkedProcess([&] { | |
| // Mapping a binder FD from a process other than the one that opened it must fail with | |
| // EINVAL. | |
| EXPECT_THAT(test_helper::ScopedMMap::MMap(nullptr, kBinderMMapSize, PROT_READ, flags, | |
| binder.get(), 0), | |
| SyscallResultIsErrno(EINVAL)); | |
| }); | |
| ASSERT_TRUE(helper.WaitForChildren()); | |
| // The process that originally opened the binder FD can still mmap it. | |
| EXPECT_THAT( | |
| test_helper::ScopedMMap::MMap(nullptr, kBinderMMapSize, PROT_READ, flags, binder.get(), 0), | |
| SyscallResultIsOk()); | |
| } | |
| } | |
| // Unlike mmap, ioctls on a binder FD are permitted from processes other than the one that opened | |
| // it, e.g. after the FD is inherited across fork() or passed to another process. | |
| TEST_F(BinderTest, CrossProcessIoctlSucceeds) { | |
| fbl::unique_fd binder = | |
| fbl::unique_fd(open(TestPath("binderfs/binder").c_str(), O_RDWR | O_CLOEXEC)); | |
| ASSERT_TRUE(binder) << strerror(errno); | |
| test_helper::ForkHelper helper; | |
| helper.RunInForkedProcess([&] { | |
| struct binder_version version = {}; | |
| EXPECT_THAT(ioctl(binder.get(), BINDER_VERSION, &version), SyscallSucceeds()); | |
| }); | |
| ASSERT_TRUE(helper.WaitForChildren()); | |
| } | |
| // The commands returned by binder reads, except BR_NOOP, and the payloads of the returned | |
| // BR_TRANSACTION commands. | |
| struct BinderReadResult { | |
| std::vector<binder_driver_return_protocol> commands; | |
| std::vector<binder_transaction_data> transactions; | |
| bool Contains(binder_driver_return_protocol command) const { | |
| return std::ranges::find(commands, command) != commands.end(); | |
| } | |
| }; | |
| // Writes the `write_size` bytes of `write_buffer` to `binder`, without reading from it. | |
| void Write(const fbl::unique_fd& binder, const void* write_buffer, size_t write_size) { | |
| struct binder_write_read write_read = { | |
| .write_size = write_size, | |
| .write_consumed = 0, | |
| .write_buffer = (binder_uintptr_t)write_buffer, | |
| }; | |
| EXPECT_THAT(ioctl(binder.get(), BINDER_WRITE_READ, &write_read), SyscallSucceeds()); | |
| EXPECT_EQ(write_read.write_consumed, write_size); | |
| } | |
| // Writes the `write_size` bytes of `write_buffer` to `binder`, and then reads from it, with a | |
| // single BINDER_WRITE_READ. | |
| BinderReadResult WriteRead(const fbl::unique_fd& binder, const void* write_buffer, | |
| size_t write_size) { | |
| std::array<uint32_t, 32> read_buffer = {}; | |
| struct binder_write_read write_read = { | |
| .write_size = write_size, | |
| .write_consumed = 0, | |
| .write_buffer = (binder_uintptr_t)write_buffer, | |
| .read_size = sizeof(read_buffer), | |
| .read_consumed = 0, | |
| .read_buffer = (binder_uintptr_t)read_buffer.data(), | |
| }; | |
| EXPECT_THAT(ioctl(binder.get(), BINDER_WRITE_READ, &write_read), SyscallSucceeds()); | |
| BinderReadResult result; | |
| const char* const data = reinterpret_cast<const char*>(read_buffer.data()); | |
| size_t offset = 0; | |
| while (offset < write_read.read_consumed) { | |
| binder_driver_return_protocol command; | |
| memcpy(&command, data + offset, sizeof(command)); | |
| offset += sizeof(command); | |
| if (command == BR_TRANSACTION) { | |
| binder_transaction_data transaction; | |
| memcpy(&transaction, data + offset, sizeof(transaction)); | |
| result.transactions.push_back(transaction); | |
| } | |
| if (command != BR_NOOP) { | |
| result.commands.push_back(command); | |
| } | |
| // The size of the parameters of a command is encoded in its value. | |
| offset += _IOC_SIZE(command); | |
| } | |
| EXPECT_EQ(offset, write_read.read_consumed) << "binder read buffer did not parse cleanly"; | |
| return result; | |
| } | |
| // Reads from `binder` until `command`, or a transaction failure, has been returned. Returns all | |
| // the commands returned, starting with those of `result`. | |
| BinderReadResult ReadUntil(const fbl::unique_fd& binder, BinderReadResult result, | |
| binder_driver_return_protocol command) { | |
| while (!result.Contains(command) && !result.Contains(BR_DEAD_REPLY) && | |
| !result.Contains(BR_FAILED_REPLY) && !testing::Test::HasFailure()) { | |
| BinderReadResult next = WriteRead(binder, nullptr, 0); | |
| result.commands.insert(result.commands.end(), next.commands.begin(), next.commands.end()); | |
| result.transactions.insert(result.transactions.end(), next.transactions.begin(), | |
| next.transactions.end()); | |
| } | |
| return result; | |
| } | |
| BinderReadResult ReadUntil(const fbl::unique_fd& binder, binder_driver_return_protocol command) { | |
| return ReadUntil(binder, BinderReadResult(), command); | |
| } | |
| // Sends a two-way transaction without data to `handle`, and returns the commands read afterwards. | |
| BinderReadResult SendTwoWayTransaction(const fbl::unique_fd& binder, uint32_t handle) { | |
| const starnix_binder::TransactionWriteBuffer transaction = { | |
| .command = BC_TRANSACTION, | |
| .data = {.target = {.handle = handle}}, | |
| }; | |
| return WriteRead(binder, &transaction, sizeof(transaction)); | |
| } | |
| // Replies without data to the transaction being handled by the calling thread, and returns the | |
| // commands read afterwards. | |
| BinderReadResult SendEmptyReply(const fbl::unique_fd& binder) { | |
| const starnix_binder::ReplyWriteBuffer reply = {.command = BC_REPLY, .data = {}}; | |
| return WriteRead(binder, &reply, sizeof(reply)); | |
| } | |
| // A binder object owned by the calling process. | |
| constexpr flat_binder_object kLocalObject = { | |
| .hdr = {.type = BINDER_TYPE_BINDER}, | |
| .flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS, | |
| .binder = 0x1000, | |
| .cookie = 0x2000, | |
| }; | |
| // Returns the data of a transaction to `handle` that only contains `*object`, at offset `*offset`. | |
| binder_transaction_data TransactionDataWithObject(uint32_t handle, uint32_t flags, | |
| const flat_binder_object* object, | |
| const binder_size_t* offset) { | |
| return { | |
| .target = {.handle = handle}, | |
| .flags = flags, | |
| .data_size = sizeof(*object), | |
| .offsets_size = sizeof(*offset), | |
| .data = {.ptr = {.buffer = (binder_uintptr_t)object, .offsets = (binder_uintptr_t)offset}}, | |
| }; | |
| } | |
| // Forks a process that becomes the context manager of the binder device in `binder_dir`, and then | |
| // runs `serve` with its binder fd. Returns the child PID once the context manager has been set. | |
| pid_t ForkContextManager(test_helper::ForkHelper& fork_helper, const std::string& binder_dir, | |
| fit::function<void(const fbl::unique_fd&)> serve) { | |
| auto ready = test_helper::MakeRendezvous(); | |
| pid_t pid = fork_helper.RunInForkedProcess([&] { | |
| auto fd_and_mapping = starnix_binder::OpenBinderAndMap(binder_dir); | |
| ASSERT_TRUE(fd_and_mapping.fd_); | |
| ASSERT_THAT(fd_and_mapping.mapping_, SyscallResultIsOk()); | |
| ASSERT_THAT(ioctl(fd_and_mapping.fd_.get(), BINDER_SET_CONTEXT_MGR, 0), SyscallSucceeds()); | |
| starnix_binder::EnterLooper(fd_and_mapping.fd_); | |
| ready.poker.poke(); | |
| serve(fd_and_mapping.fd_); | |
| }); | |
| // Close the parent's copy of the pipe's write side, so that `hold()` returns if the child exits | |
| // without poking. | |
| ready.poker = test_helper::Poker(); | |
| ready.holder.hold(); | |
| return pid; | |
| } | |
| // Waits until `fd` is readable, for at most 10 seconds. | |
| void WaitUntilReadable(const fbl::unique_fd& fd) { | |
| struct pollfd pfd = {.fd = fd.get(), .events = POLLIN, .revents = 0}; | |
| EXPECT_THAT(HANDLE_EINTR(poll(&pfd, 1, 10'000)), SyscallSucceedsWithValue(1)); | |
| } | |
| // A single write/read of a two-way transaction returns both its BR_TRANSACTION_COMPLETE and its | |
| // BR_REPLY. The BR_TRANSACTION_COMPLETE of the reply is returned on its own. | |
| TEST_F(BinderTest, TwoWayTransactionCompleteIsReadWithReply) { | |
| using namespace starnix_binder; | |
| test_helper::ForkHelper fork_helper; | |
| fork_helper.OnlyWaitForForkedChildren(); | |
| ForkContextManager(fork_helper, TestPath("binderfs"), [](const fbl::unique_fd& binder) { | |
| ASSERT_TRUE(ReadUntil(binder, BR_TRANSACTION).Contains(BR_TRANSACTION)); | |
| EXPECT_THAT(SendEmptyReply(binder).commands, testing::ElementsAre(BR_TRANSACTION_COMPLETE)); | |
| }); | |
| auto binder_and_map = OpenBinderAndMap(TestPath("binderfs")); | |
| ASSERT_TRUE(binder_and_map.fd_); | |
| ASSERT_THAT(binder_and_map.mapping_, SyscallResultIsOk()); | |
| EXPECT_THAT(SendTwoWayTransaction(binder_and_map.fd_, kServiceManagerHandle).commands, | |
| testing::ElementsAre(BR_TRANSACTION_COMPLETE, BR_REPLY)); | |
| EXPECT_TRUE(fork_helper.WaitForChildren()); | |
| } | |
| // A single write/read of a two-way transaction returns both its BR_TRANSACTION_COMPLETE and its | |
| // BR_DEAD_REPLY, if the recipient exits without replying. | |
| TEST_F(BinderTest, TwoWayTransactionCompleteIsReadWithDeadReply) { | |
| using namespace starnix_binder; | |
| test_helper::ForkHelper fork_helper; | |
| fork_helper.OnlyWaitForForkedChildren(); | |
| // The context manager exits as soon as it has received the transaction. | |
| ForkContextManager(fork_helper, TestPath("binderfs"), [](const fbl::unique_fd& binder) { | |
| EXPECT_TRUE(ReadUntil(binder, BR_TRANSACTION).Contains(BR_TRANSACTION)); | |
| }); | |
| auto binder_and_map = OpenBinderAndMap(TestPath("binderfs")); | |
| ASSERT_TRUE(binder_and_map.fd_); | |
| ASSERT_THAT(binder_and_map.mapping_, SyscallResultIsOk()); | |
| EXPECT_THAT(SendTwoWayTransaction(binder_and_map.fd_, kServiceManagerHandle).commands, | |
| testing::ElementsAre(BR_TRANSACTION_COMPLETE, BR_DEAD_REPLY)); | |
| EXPECT_TRUE(fork_helper.WaitForChildren()); | |
| } | |
| // When a thread replies to a transaction nested in its own two-way transaction (A -> B -> A), the | |
| // BR_TRANSACTION_COMPLETE of its reply is returned on its own: the read does not wait for the | |
| // reply to its own transaction. | |
| TEST_F(BinderTest, TransactionCompleteForNestedReplyIsReadAlone) { | |
| using namespace starnix_binder; | |
| test_helper::ForkHelper fork_helper; | |
| fork_helper.OnlyWaitForForkedChildren(); | |
| // Written to by A once its reply to the nested transaction has returned. | |
| test_helper::ScopedPipe nested_reply_sent; | |
| // B, the context manager. | |
| ForkContextManager(fork_helper, TestPath("binderfs"), [&](const fbl::unique_fd& binder) { | |
| // Receive A's transaction, which carries a binder object owned by A. | |
| BinderReadResult result = ReadUntil(binder, BR_TRANSACTION); | |
| ASSERT_EQ(result.transactions.size(), 1u); | |
| const binder_transaction_data& transaction = result.transactions[0]; | |
| ASSERT_EQ(transaction.offsets_size, sizeof(binder_size_t)); | |
| binder_size_t offset; | |
| memcpy(&offset, (const void*)transaction.data.ptr.offsets, sizeof(offset)); | |
| flat_binder_object object; | |
| memcpy(&object, (const void*)(transaction.data.ptr.buffer + offset), sizeof(object)); | |
| ASSERT_EQ(object.hdr.type, BINDER_TYPE_HANDLE); | |
| // Send a transaction to A's object. It is nested in A's transaction, so A's thread receives it. | |
| EXPECT_TRUE(ReadUntil(binder, SendTwoWayTransaction(binder, object.handle), BR_REPLY) | |
| .Contains(BR_REPLY)); | |
| // Only reply to A's transaction once A's reply to the nested transaction has returned. If that | |
| // reply waited for this one, the wait times out instead of deadlocking, and A reads both. | |
| WaitUntilReadable(nested_reply_sent.ReadSide()); | |
| SendEmptyReply(binder); | |
| }); | |
| // A. | |
| auto binder_and_map = OpenBinderAndMap(TestPath("binderfs")); | |
| ASSERT_TRUE(binder_and_map.fd_); | |
| ASSERT_THAT(binder_and_map.mapping_, SyscallResultIsOk()); | |
| const auto& binder = binder_and_map.fd_; | |
| // Send a transaction carrying a binder object owned by A to B, and receive B's nested | |
| // transaction. | |
| const flat_binder_object object = kLocalObject; | |
| const binder_size_t offset = 0; | |
| const TransactionWriteBuffer transaction = { | |
| .command = BC_TRANSACTION, | |
| .data = TransactionDataWithObject(kServiceManagerHandle, 0, &object, &offset), | |
| }; | |
| ASSERT_TRUE( | |
| ReadUntil(binder, WriteRead(binder, &transaction, sizeof(transaction)), BR_TRANSACTION) | |
| .Contains(BR_TRANSACTION)); | |
| // Reply to the nested transaction. Only its BR_TRANSACTION_COMPLETE is returned, even though A's | |
| // own transaction is still waiting for its reply. | |
| BinderReadResult result = SendEmptyReply(binder); | |
| EXPECT_THAT(result.commands, testing::ElementsAre(BR_TRANSACTION_COMPLETE)); | |
| // Let B reply to A's transaction. | |
| ASSERT_THAT(write(nested_reply_sent.WriteSide().get(), "", 1), SyscallSucceedsWithValue(1)); | |
| EXPECT_TRUE(ReadUntil(binder, std::move(result), BR_REPLY).Contains(BR_REPLY)); | |
| EXPECT_TRUE(fork_helper.WaitForChildren()); | |
| } | |
| // The BR_TRANSACTION_COMPLETE of a two-way transaction is not lost when the same read also returns | |
| // a refcount command queued after it. | |
| TEST_F(BinderTest, TwoWayTransactionCompleteIsNotOverwrittenByRefcountCommand) { | |
| using namespace starnix_binder; | |
| test_helper::ForkHelper fork_helper; | |
| fork_helper.OnlyWaitForForkedChildren(); | |
| // Written to once the first read has returned. | |
| test_helper::ScopedPipe first_read_done; | |
| ForkContextManager(fork_helper, TestPath("binderfs"), [&](const fbl::unique_fd& binder) { | |
| ASSERT_TRUE(ReadUntil(binder, BR_TRANSACTION).Contains(BR_TRANSACTION)); | |
| // Only reply once the first read has returned, so that it cannot return the reply. | |
| WaitUntilReadable(first_read_done.ReadSide()); | |
| SendEmptyReply(binder); | |
| }); | |
| auto binder_and_map = OpenBinderAndMap(TestPath("binderfs")); | |
| ASSERT_TRUE(binder_and_map.fd_); | |
| ASSERT_THAT(binder_and_map.mapping_, SyscallResultIsOk()); | |
| const auto& binder = binder_and_map.fd_; | |
| // With a single write, send a two-way transaction, and then a oneway transaction carrying a | |
| // binder object sent for the first time. The BR_ACQUIRE for the object is queued after the | |
| // BR_TRANSACTION_COMPLETE of the two-way transaction. | |
| const flat_binder_object object = kLocalObject; | |
| const binder_size_t offset = 0; | |
| const struct __attribute__((packed)) { | |
| TransactionWriteBuffer two_way; | |
| TransactionWriteBuffer oneway; | |
| } write_buffer = { | |
| .two_way = {.command = BC_TRANSACTION, .data = {.target = {.handle = kServiceManagerHandle}}}, | |
| .oneway = {.command = BC_TRANSACTION, | |
| .data = TransactionDataWithObject(kServiceManagerHandle, TF_ONE_WAY, &object, | |
| &offset)}, | |
| }; | |
| BinderReadResult result = WriteRead(binder, &write_buffer, sizeof(write_buffer)); | |
| ASSERT_THAT(write(first_read_done.WriteSide().get(), "", 1), SyscallSucceedsWithValue(1)); | |
| result = ReadUntil(binder, std::move(result), BR_REPLY); | |
| // Each transaction has its BR_TRANSACTION_COMPLETE. | |
| EXPECT_EQ(std::ranges::count(result.commands, BR_TRANSACTION_COMPLETE), 2); | |
| EXPECT_TRUE(result.Contains(BR_ACQUIRE)); | |
| EXPECT_TRUE(result.Contains(BR_REPLY)); | |
| EXPECT_TRUE(fork_helper.WaitForChildren()); | |
| } | |
| // If the sender of a two-way transaction has exited, the reply cannot be delivered, but the replier | |
| // still only receives the BR_TRANSACTION_COMPLETE of its reply. libbinder may defer writing a | |
| // BC_REPLY until its looper reads the next command, and aborts if it receives a failure instead. | |
| TEST_F(BinderTest, ReplyToExitedSenderIsCompleted) { | |
| using namespace starnix_binder; | |
| test_helper::ForkHelper fork_helper; | |
| fork_helper.OnlyWaitForForkedChildren(); | |
| // Written to by the context manager once it has received the transaction. | |
| test_helper::ScopedPipe transaction_received; | |
| // Written to once the sender of the transaction has exited. | |
| test_helper::ScopedPipe sender_exited; | |
| ForkContextManager(fork_helper, TestPath("binderfs"), [&](const fbl::unique_fd& binder) { | |
| ASSERT_TRUE(ReadUntil(binder, BR_TRANSACTION).Contains(BR_TRANSACTION)); | |
| ASSERT_THAT(write(transaction_received.WriteSide().get(), "", 1), SyscallSucceedsWithValue(1)); | |
| WaitUntilReadable(sender_exited.ReadSide()); | |
| EXPECT_THAT(SendEmptyReply(binder).commands, testing::ElementsAre(BR_TRANSACTION_COMPLETE)); | |
| }); | |
| // The sender sends a two-way transaction to the context manager, and exits once the context | |
| // manager has received it, without waiting for the reply. | |
| pid_t sender_pid = fork_helper.RunInForkedProcess([&] { | |
| auto binder_and_map = OpenBinderAndMap(TestPath("binderfs")); | |
| ASSERT_TRUE(binder_and_map.fd_); | |
| ASSERT_THAT(binder_and_map.mapping_, SyscallResultIsOk()); | |
| const TransactionWriteBuffer transaction = { | |
| .command = BC_TRANSACTION, | |
| .data = {.target = {.handle = kServiceManagerHandle}}, | |
| }; | |
| Write(binder_and_map.fd_, &transaction, sizeof(transaction)); | |
| WaitUntilReadable(transaction_received.ReadSide()); | |
| }); | |
| EXPECT_TRUE(fork_helper.WaitForChild(sender_pid).determined_result); | |
| // Let the context manager reply. | |
| ASSERT_THAT(write(sender_exited.WriteSide().get(), "", 1), SyscallSucceedsWithValue(1)); | |
| EXPECT_TRUE(fork_helper.WaitForChildren()); | |
| } | |
| TEST_F(BinderTest, FreezeWaitsForPendingTransactions) { | |
| using namespace starnix_binder; | |
| test_helper::ForkHelper fork_helper; | |
| fork_helper.OnlyWaitForForkedChildren(); | |
| test_helper::ScopedPipe oneway_received; | |
| test_helper::ScopedPipe allow_oneway_free; | |
| test_helper::ScopedPipe txn1_buffer_freed; | |
| test_helper::ScopedPipe allow_reply_1; | |
| test_helper::ScopedPipe txn2_buffer_freed; | |
| test_helper::ScopedPipe allow_reply_2; | |
| test_helper::ScopedPipe done; | |
| struct __attribute__((packed)) FreeBufferCmd { | |
| binder_driver_command_protocol free_cmd = BC_FREE_BUFFER; | |
| binder_uintptr_t buffer; | |
| }; | |
| pid_t manager_pid = | |
| ForkContextManager(fork_helper, TestPath("binderfs"), [&](const fbl::unique_fd& binder) { | |
| // Phase 1: Receive the first of two oneway transactions and hold its buffer before | |
| // BC_FREE_BUFFER so the second oneway transaction remains queued and unread. | |
| BinderReadResult oneway1 = ReadUntil(binder, BR_TRANSACTION); | |
| ASSERT_EQ(oneway1.transactions.size(), 1u); | |
| ASSERT_THAT(write(oneway_received.WriteSide().get(), "", 1), SyscallSucceedsWithValue(1)); | |
| WaitUntilReadable(allow_oneway_free.ReadSide()); | |
| FreeBufferCmd free_oneway1 = {.buffer = oneway1.transactions[0].data.ptr.buffer}; | |
| Write(binder, &free_oneway1, sizeof(free_oneway1)); | |
| BinderReadResult oneway2 = ReadUntil(binder, BR_TRANSACTION); | |
| ASSERT_EQ(oneway2.transactions.size(), 1u); | |
| FreeBufferCmd free_oneway2 = {.buffer = oneway2.transactions[0].data.ptr.buffer}; | |
| Write(binder, &free_oneway2, sizeof(free_oneway2)); | |
| // Phase 2: Receive a two-way transaction, free its buffer first via a standalone | |
| // BC_FREE_BUFFER while keeping the transaction on the thread stack, then wait before | |
| // sending BC_REPLY in a separate WriteRead call. | |
| BinderReadResult txn1 = ReadUntil(binder, BR_TRANSACTION); | |
| ASSERT_EQ(txn1.transactions.size(), 1u); | |
| FreeBufferCmd free_txn1 = {.buffer = txn1.transactions[0].data.ptr.buffer}; | |
| Write(binder, &free_txn1, sizeof(free_txn1)); | |
| ASSERT_THAT(write(txn1_buffer_freed.WriteSide().get(), "", 1), SyscallSucceedsWithValue(1)); | |
| WaitUntilReadable(allow_reply_1.ReadSide()); | |
| EXPECT_THAT(SendEmptyReply(binder).commands, testing::ElementsAre(BR_TRANSACTION_COMPLETE)); | |
| // Phase 3: Receive a second two-way transaction (verifying EINTR rolled back the freeze), | |
| // free its buffer first, and reply when signaled. | |
| BinderReadResult txn2 = ReadUntil(binder, BR_TRANSACTION); | |
| ASSERT_EQ(txn2.transactions.size(), 1u); | |
| FreeBufferCmd free_txn2 = {.buffer = txn2.transactions[0].data.ptr.buffer}; | |
| Write(binder, &free_txn2, sizeof(free_txn2)); | |
| ASSERT_THAT(write(txn2_buffer_freed.WriteSide().get(), "", 1), SyscallSucceedsWithValue(1)); | |
| WaitUntilReadable(allow_reply_2.ReadSide()); | |
| EXPECT_THAT(SendEmptyReply(binder).commands, testing::ElementsAre(BR_TRANSACTION_COMPLETE)); | |
| WaitUntilReadable(done.ReadSide()); | |
| BinderReadResult oneway3 = ReadUntil(binder, BR_TRANSACTION); | |
| ASSERT_EQ(oneway3.transactions.size(), 1u); | |
| FreeBufferCmd free_oneway3 = {.buffer = oneway3.transactions[0].data.ptr.buffer}; | |
| Write(binder, &free_oneway3, sizeof(free_oneway3)); | |
| }); | |
| auto binder_and_map = OpenBinderAndMap(TestPath("binderfs")); | |
| ASSERT_TRUE(binder_and_map.fd_); | |
| ASSERT_THAT(binder_and_map.mapping_, SyscallResultIsOk()); | |
| const auto& binder = binder_and_map.fd_; | |
| // Phase 1: Send two oneway transactions so an unread buffer remains pending in | |
| // active_transactions while the context manager's thread transaction stack is empty. | |
| const TransactionWriteBuffer oneway_txn = { | |
| .command = BC_TRANSACTION, | |
| .data = | |
| { | |
| .target = {.handle = kServiceManagerHandle}, | |
| .flags = TF_ONE_WAY, | |
| }, | |
| }; | |
| EXPECT_THAT(WriteRead(binder, &oneway_txn, sizeof(oneway_txn)).commands, | |
| testing::ElementsAre(BR_TRANSACTION_COMPLETE)); | |
| EXPECT_THAT(WriteRead(binder, &oneway_txn, sizeof(oneway_txn)).commands, | |
| testing::ElementsAre(BR_TRANSACTION_COMPLETE)); | |
| WaitUntilReadable(oneway_received.ReadSide()); | |
| binder_frozen_status_info status = {.pid = static_cast<uint32_t>(manager_pid)}; | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_GET_FROZEN_INFO, &status), SyscallSucceeds()); | |
| EXPECT_EQ(status.sync_recv, 2u); | |
| EXPECT_EQ(status.async_recv, 0u); | |
| std::thread delayed_oneway_free([&] { | |
| usleep(50'000); | |
| EXPECT_THAT(write(allow_oneway_free.WriteSide().get(), "", 1), SyscallSucceedsWithValue(1)); | |
| }); | |
| binder_freeze_info freeze_info = { | |
| .pid = static_cast<uint32_t>(manager_pid), | |
| .enable = 1, | |
| .timeout_ms = 2000, | |
| }; | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_FREEZE, &freeze_info), SyscallSucceeds()); | |
| delayed_oneway_free.join(); | |
| freeze_info.enable = 0; | |
| freeze_info.timeout_ms = 0; | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_FREEZE, &freeze_info), SyscallSucceeds()); | |
| // Phase 2: Start an in-flight two-way transaction where the receiver frees the buffer via | |
| // BC_FREE_BUFFER before sending BC_REPLY. | |
| std::thread client_thread_1([&] { | |
| BinderReadResult res = | |
| ReadUntil(binder, SendTwoWayTransaction(binder, kServiceManagerHandle), BR_REPLY); | |
| EXPECT_TRUE(res.Contains(BR_REPLY)); | |
| }); | |
| WaitUntilReadable(txn1_buffer_freed.ReadSide()); | |
| // Even after BC_FREE_BUFFER, while the two-way transaction is still on the receiver's thread | |
| // stack, BINDER_GET_FROZEN_INFO reports bit 1 of sync_recv set on both receiver and sender. | |
| status = {.pid = static_cast<uint32_t>(manager_pid)}; | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_GET_FROZEN_INFO, &status), SyscallSucceeds()); | |
| EXPECT_EQ(status.sync_recv, 2u); | |
| EXPECT_EQ(status.async_recv, 0u); | |
| binder_frozen_status_info client_status = {.pid = static_cast<uint32_t>(getpid())}; | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_GET_FROZEN_INFO, &client_status), SyscallSucceeds()); | |
| EXPECT_EQ(client_status.sync_recv, 2u); | |
| EXPECT_EQ(client_status.async_recv, 0u); | |
| // Earlier validation checks (invalid enable value and non-existent binder PID) take precedence | |
| // over EAGAIN even while a transaction is pending. | |
| uint32_t nonexistent_pid = 0x7fffffff; | |
| { | |
| fbl::unique_fd pid_max_fd(open("/proc/sys/kernel/pid_max", O_RDONLY)); | |
| char buf[32] = {}; | |
| if (pid_max_fd.is_valid() && read(pid_max_fd.get(), buf, sizeof(buf) - 1) > 0) { | |
| unsigned long pid_max = strtoul(buf, nullptr, 10); | |
| if (pid_max > 0) { | |
| nonexistent_pid = static_cast<uint32_t>(pid_max + 1); | |
| } | |
| } | |
| } | |
| binder_frozen_status_info bad_status = {.pid = nonexistent_pid}; | |
| EXPECT_THAT(ioctl(binder.get(), BINDER_GET_FROZEN_INFO, &bad_status), | |
| SyscallFailsWithErrno(EINVAL)); | |
| binder_freeze_info bad_freeze = { | |
| .pid = static_cast<uint32_t>(manager_pid), | |
| .enable = 2, | |
| .timeout_ms = 0, | |
| }; | |
| EXPECT_THAT(ioctl(binder.get(), BINDER_FREEZE, &bad_freeze), SyscallFailsWithErrno(EINVAL)); | |
| bad_freeze.pid = nonexistent_pid; | |
| bad_freeze.enable = 1; | |
| EXPECT_THAT(ioctl(binder.get(), BINDER_FREEZE, &bad_freeze), SyscallFailsWithErrno(EINVAL)); | |
| // Non-blocking freeze (timeout_ms == 0) and short-timeout freeze (timeout_ms == 20) still fail | |
| // with EAGAIN after BC_FREE_BUFFER because BC_REPLY has not been sent yet. | |
| freeze_info = { | |
| .pid = static_cast<uint32_t>(manager_pid), | |
| .enable = 1, | |
| .timeout_ms = 0, | |
| }; | |
| EXPECT_THAT(ioctl(binder.get(), BINDER_FREEZE, &freeze_info), SyscallFailsWithErrno(EAGAIN)); | |
| freeze_info.timeout_ms = 20; | |
| EXPECT_THAT(ioctl(binder.get(), BINDER_FREEZE, &freeze_info), SyscallFailsWithErrno(EAGAIN)); | |
| // Interrupting a blocking BINDER_FREEZE with a signal fails with EINTR and rolls back the | |
| // freeze state. | |
| struct sigaction sa = {}; | |
| struct sigaction old_sa = {}; | |
| sa.sa_handler = +[](int) {}; | |
| ASSERT_THAT(sigaction(SIGUSR1, &sa, &old_sa), SyscallSucceeds()); | |
| auto restore_sigaction = fit::defer([&] { sigaction(SIGUSR1, &old_sa, nullptr); }); | |
| std::atomic<bool> freeze_returned{false}; | |
| pthread_t main_thread = pthread_self(); | |
| std::thread interrupt_thread([main_thread, &freeze_returned] { | |
| while (!freeze_returned.load(std::memory_order_acquire)) { | |
| usleep(10'000); | |
| if (freeze_returned.load(std::memory_order_acquire)) { | |
| break; | |
| } | |
| EXPECT_EQ(0, pthread_kill(main_thread, SIGUSR1)); | |
| } | |
| }); | |
| freeze_info.timeout_ms = 5000; | |
| EXPECT_THAT(ioctl(binder.get(), BINDER_FREEZE, &freeze_info), SyscallFailsWithErrno(EINTR)); | |
| freeze_returned.store(true, std::memory_order_release); | |
| interrupt_thread.join(); | |
| ASSERT_THAT(write(allow_reply_1.WriteSide().get(), "", 1), SyscallSucceedsWithValue(1)); | |
| client_thread_1.join(); | |
| // Phase 3: Without calling BINDER_FREEZE(enable = 0), start another two-way transaction. Because | |
| // the interrupted BINDER_FREEZE rolled back the freeze state, this transaction is delivered | |
| // rather than rejected with BR_FROZEN_REPLY. | |
| std::thread client_thread_2([&] { | |
| BinderReadResult res = | |
| ReadUntil(binder, SendTwoWayTransaction(binder, kServiceManagerHandle), BR_REPLY); | |
| EXPECT_TRUE(res.Contains(BR_REPLY)); | |
| }); | |
| WaitUntilReadable(txn2_buffer_freed.ReadSide()); | |
| // Unblock the context manager's BC_REPLY slightly after starting a blocking BINDER_FREEZE. | |
| std::thread delayed_reply([&] { | |
| usleep(50'000); | |
| EXPECT_THAT(write(allow_reply_2.WriteSide().get(), "", 1), SyscallSucceedsWithValue(1)); | |
| }); | |
| freeze_info.timeout_ms = 2000; | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_FREEZE, &freeze_info), SyscallSucceeds()); | |
| delayed_reply.join(); | |
| client_thread_2.join(); | |
| // Once frozen with no pending transactions, BINDER_GET_FROZEN_INFO reports sync_recv == 0 on | |
| // both the receiver and the sender (even before the sender frees its BR_REPLY buffer). | |
| status = {.pid = static_cast<uint32_t>(manager_pid)}; | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_GET_FROZEN_INFO, &status), SyscallSucceeds()); | |
| EXPECT_EQ(status.sync_recv, 0u); | |
| EXPECT_EQ(status.async_recv, 0u); | |
| client_status = {.pid = static_cast<uint32_t>(getpid())}; | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_GET_FROZEN_INFO, &client_status), SyscallSucceeds()); | |
| EXPECT_EQ(client_status.sync_recv, 0u); | |
| EXPECT_EQ(client_status.async_recv, 0u); | |
| // A synchronous transaction to the frozen process fails with BR_FROZEN_REPLY and sets bit 0 of | |
| // sync_recv. | |
| EXPECT_TRUE( | |
| ReadUntil(binder, SendTwoWayTransaction(binder, kServiceManagerHandle), BR_FROZEN_REPLY) | |
| .Contains(BR_FROZEN_REPLY)); | |
| status = {.pid = static_cast<uint32_t>(manager_pid)}; | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_GET_FROZEN_INFO, &status), SyscallSucceeds()); | |
| EXPECT_EQ(status.sync_recv, 1u); | |
| EXPECT_EQ(status.async_recv, 0u); | |
| // A oneway transaction to the frozen process returns BR_TRANSACTION_PENDING_FROZEN and sets | |
| // async_recv. Once the process is thawed, the buffered oneway transaction is treated as pending | |
| // until the receiver reads and frees its buffer. | |
| EXPECT_THAT(WriteRead(binder, &oneway_txn, sizeof(oneway_txn)).commands, | |
| testing::ElementsAre(BR_TRANSACTION_PENDING_FROZEN)); | |
| status = {.pid = static_cast<uint32_t>(manager_pid)}; | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_GET_FROZEN_INFO, &status), SyscallSucceeds()); | |
| EXPECT_EQ(status.async_recv, 1u); | |
| // Unfreeze the process. | |
| freeze_info.enable = 0; | |
| freeze_info.timeout_ms = 0; | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_FREEZE, &freeze_info), SyscallSucceeds()); | |
| status = {.pid = static_cast<uint32_t>(manager_pid)}; | |
| ASSERT_THAT(ioctl(binder.get(), BINDER_GET_FROZEN_INFO, &status), SyscallSucceeds()); | |
| EXPECT_EQ(status.sync_recv, 2u); | |
| EXPECT_EQ(status.async_recv, 0u); | |
| freeze_info.enable = 1; | |
| freeze_info.timeout_ms = 0; | |
| EXPECT_THAT(ioctl(binder.get(), BINDER_FREEZE, &freeze_info), SyscallFailsWithErrno(EAGAIN)); | |
| ASSERT_THAT(write(done.WriteSide().get(), "", 1), SyscallSucceedsWithValue(1)); | |
| EXPECT_TRUE(fork_helper.WaitForChildren()); | |
| } | |
| TEST_F(BinderTest, ScatterGatherOverlappingAndUnalignedOffsetsRejected) { | |
| using namespace starnix_binder; | |
| test_helper::ForkHelper fork_helper; | |
| fork_helper.OnlyWaitForForkedChildren(); | |
| test_helper::ScopedPipe done_pipe; | |
| ForkContextManager(fork_helper, TestPath("binderfs"), [&](const fbl::unique_fd& binder) { | |
| WaitUntilReadable(done_pipe.ReadSide()); | |
| }); | |
| auto binder_and_map = OpenBinderAndMap(TestPath("binderfs")); | |
| ASSERT_TRUE(binder_and_map.fd_); | |
| ASSERT_THAT(binder_and_map.mapping_, SyscallResultIsOk()); | |
| const auto& binder = binder_and_map.fd_; | |
| struct __attribute__((packed)) SgTransactionWriteBuffer { | |
| binder_driver_command_protocol command = BC_TRANSACTION_SG; | |
| binder_transaction_data_sg sg_data; | |
| }; | |
| auto expect_tx_rejected = [&](const SgTransactionWriteBuffer& tx) { | |
| BinderReadResult result = WriteRead(binder, &tx, sizeof(tx)); | |
| while (!result.Contains(BR_FAILED_REPLY) && !result.Contains(BR_ERROR) && | |
| !result.Contains(BR_DEAD_REPLY) && !testing::Test::HasFailure()) { | |
| BinderReadResult next = WriteRead(binder, nullptr, 0); | |
| result.commands.insert(result.commands.end(), next.commands.begin(), next.commands.end()); | |
| } | |
| EXPECT_TRUE(result.Contains(BR_FAILED_REPLY) || result.Contains(BR_ERROR)); | |
| }; | |
| uint8_t child_buf[64] = {}; | |
| // 1. Non-monotonic / overlapping object offsets: offsets[1] < offsets[0] + sizeof(object). | |
| { | |
| binder_buffer_object objects[2] = {}; | |
| objects[0].hdr.type = BINDER_TYPE_PTR; | |
| objects[0].buffer = reinterpret_cast<binder_uintptr_t>(child_buf); | |
| objects[0].length = 16; | |
| objects[1].hdr.type = BINDER_TYPE_PTR; | |
| objects[1].buffer = reinterpret_cast<binder_uintptr_t>(child_buf); | |
| objects[1].length = 16; | |
| binder_size_t offsets[2] = {sizeof(binder_buffer_object), 0}; | |
| SgTransactionWriteBuffer tx = {}; | |
| tx.sg_data.transaction_data.target.handle = kServiceManagerHandle; | |
| tx.sg_data.transaction_data.data_size = sizeof(objects); | |
| tx.sg_data.transaction_data.offsets_size = sizeof(offsets); | |
| tx.sg_data.transaction_data.data.ptr.buffer = reinterpret_cast<binder_uintptr_t>(objects); | |
| tx.sg_data.transaction_data.data.ptr.offsets = reinterpret_cast<binder_uintptr_t>(offsets); | |
| tx.sg_data.buffers_size = 64; | |
| expect_tx_rejected(tx); | |
| } | |
| // 2. Unaligned object offset in offsets buffer. | |
| { | |
| alignas(8) uint8_t raw_data[sizeof(binder_buffer_object) + 8] = {}; | |
| binder_buffer_object obj = {}; | |
| obj.hdr.type = BINDER_TYPE_PTR; | |
| obj.buffer = reinterpret_cast<binder_uintptr_t>(child_buf); | |
| obj.length = 16; | |
| memcpy(raw_data + 1, &obj, sizeof(obj)); | |
| binder_size_t offset = 1; | |
| SgTransactionWriteBuffer tx = {}; | |
| tx.sg_data.transaction_data.target.handle = kServiceManagerHandle; | |
| tx.sg_data.transaction_data.data_size = sizeof(raw_data); | |
| tx.sg_data.transaction_data.offsets_size = sizeof(offset); | |
| tx.sg_data.transaction_data.data.ptr.buffer = reinterpret_cast<binder_uintptr_t>(raw_data); | |
| tx.sg_data.transaction_data.data.ptr.offsets = reinterpret_cast<binder_uintptr_t>(&offset); | |
| tx.sg_data.buffers_size = 64; | |
| expect_tx_rejected(tx); | |
| } | |
| // 3. Scatter-gather buffer length exceeding declared buffers_size. | |
| { | |
| binder_buffer_object obj = {}; | |
| obj.hdr.type = BINDER_TYPE_PTR; | |
| obj.buffer = reinterpret_cast<binder_uintptr_t>(child_buf); | |
| obj.length = sizeof(child_buf); | |
| binder_size_t offset = 0; | |
| SgTransactionWriteBuffer tx = {}; | |
| tx.sg_data.transaction_data.target.handle = kServiceManagerHandle; | |
| tx.sg_data.transaction_data.data_size = sizeof(obj); | |
| tx.sg_data.transaction_data.offsets_size = sizeof(offset); | |
| tx.sg_data.transaction_data.data.ptr.buffer = reinterpret_cast<binder_uintptr_t>(&obj); | |
| tx.sg_data.transaction_data.data.ptr.offsets = reinterpret_cast<binder_uintptr_t>(&offset); | |
| tx.sg_data.buffers_size = 8; | |
| expect_tx_rejected(tx); | |
| } | |
| ASSERT_THAT(write(done_pipe.WriteSide().get(), "", 1), SyscallSucceedsWithValue(1)); | |
| EXPECT_TRUE(fork_helper.WaitForChildren()); | |
| } | |
| // Flag of a BC_REPLY requesting that its BR_TRANSACTION_COMPLETE is returned along with the next | |
| // command for the replying thread, rather than on its own. It is not in the UAPI headers yet. | |
| constexpr uint32_t kTfDeferComplete = 0x80; | |
| // The BR_TRANSACTION_COMPLETE of a reply sent with TF_DEFER_COMPLETE is returned along with the | |
| // next transaction, so that a looper replies and receives the next transaction with a single | |
| // write/read. | |
| TEST_F(BinderTest, DeferredTransactionCompleteIsReadWithNextTransaction) { | |
| if (!test_helper::IsStarnix()) { | |
| GTEST_SKIP() << "TF_DEFER_COMPLETE is not supported by Linux yet."; | |
| } | |
| using namespace starnix_binder; | |
| test_helper::ForkHelper fork_helper; | |
| fork_helper.OnlyWaitForForkedChildren(); | |
| ForkContextManager(fork_helper, TestPath("binderfs"), [](const fbl::unique_fd& binder) { | |
| ASSERT_TRUE(ReadUntil(binder, BR_TRANSACTION).Contains(BR_TRANSACTION)); | |
| const ReplyWriteBuffer reply = {.command = BC_REPLY, .data = {.flags = kTfDeferComplete}}; | |
| EXPECT_THAT(WriteRead(binder, &reply, sizeof(reply)).commands, | |
| testing::ElementsAre(BR_TRANSACTION_COMPLETE, BR_TRANSACTION)); | |
| EXPECT_THAT(SendEmptyReply(binder).commands, testing::ElementsAre(BR_TRANSACTION_COMPLETE)); | |
| }); | |
| auto binder_and_map = OpenBinderAndMap(TestPath("binderfs")); | |
| ASSERT_TRUE(binder_and_map.fd_); | |
| ASSERT_THAT(binder_and_map.mapping_, SyscallResultIsOk()); | |
| for (int i = 0; i < 2; ++i) { | |
| EXPECT_THAT(SendTwoWayTransaction(binder_and_map.fd_, kServiceManagerHandle).commands, | |
| testing::ElementsAre(BR_TRANSACTION_COMPLETE, BR_REPLY)); | |
| } | |
| EXPECT_TRUE(fork_helper.WaitForChildren()); | |
| } | |
| // A BR_TRANSACTION_COMPLETE deferred with TF_DEFER_COMPLETE does not make the binder readable on | |
| // its own: it is only read along with the next command for the replying thread. | |
| TEST_F(BinderTest, DeferredTransactionCompleteDoesNotMakeBinderReadable) { | |
| if (!test_helper::IsStarnix()) { | |
| GTEST_SKIP() << "TF_DEFER_COMPLETE is not supported by Linux yet."; | |
| } | |
| using namespace starnix_binder; | |
| test_helper::ForkHelper fork_helper; | |
| fork_helper.OnlyWaitForForkedChildren(); | |
| // Written to by the context manager once it has checked that its binder is not readable. | |
| test_helper::ScopedPipe not_readable; | |
| ForkContextManager(fork_helper, TestPath("binderfs"), [&](const fbl::unique_fd& binder) { | |
| ASSERT_TRUE(ReadUntil(binder, BR_TRANSACTION).Contains(BR_TRANSACTION)); | |
| // Reply without reading. | |
| const ReplyWriteBuffer reply = {.command = BC_REPLY, .data = {.flags = kTfDeferComplete}}; | |
| Write(binder, &reply, sizeof(reply)); | |
| struct pollfd pfd = {.fd = binder.get(), .events = POLLIN, .revents = 0}; | |
| EXPECT_THAT(poll(&pfd, 1, 0), SyscallSucceedsWithValue(0)); | |
| ASSERT_THAT(write(not_readable.WriteSide().get(), "", 1), SyscallSucceedsWithValue(1)); | |
| // The next transaction makes the binder readable, and is read along with the | |
| // BR_TRANSACTION_COMPLETE. | |
| WaitUntilReadable(binder); | |
| EXPECT_THAT(WriteRead(binder, nullptr, 0).commands, | |
| testing::ElementsAre(BR_TRANSACTION_COMPLETE, BR_TRANSACTION)); | |
| EXPECT_THAT(SendEmptyReply(binder).commands, testing::ElementsAre(BR_TRANSACTION_COMPLETE)); | |
| }); | |
| auto binder_and_map = OpenBinderAndMap(TestPath("binderfs")); | |
| ASSERT_TRUE(binder_and_map.fd_); | |
| ASSERT_THAT(binder_and_map.mapping_, SyscallResultIsOk()); | |
| const auto& binder = binder_and_map.fd_; | |
| EXPECT_THAT(SendTwoWayTransaction(binder, kServiceManagerHandle).commands, | |
| testing::ElementsAre(BR_TRANSACTION_COMPLETE, BR_REPLY)); | |
| // Only send the next transaction once the context manager has checked that its binder is not | |
| // readable. | |
| WaitUntilReadable(not_readable.ReadSide()); | |
| EXPECT_THAT(SendTwoWayTransaction(binder, kServiceManagerHandle).commands, | |
| testing::ElementsAre(BR_TRANSACTION_COMPLETE, BR_REPLY)); | |
| EXPECT_TRUE(fork_helper.WaitForChildren()); | |
| } | |
| } // namespace | |
| namespace starnix_binder { | |
| class WithoutSEStarnix : public WithOrWithoutSEStarnix { | |
| public: | |
| pid_t SpawnManager(test_helper::ForkHelper& fork_helper, fit::closure manager_behavior) override { | |
| return fork_helper.RunInForkedProcess(std::move(manager_behavior)); | |
| } | |
| pid_t SpawnProvider(test_helper::ForkHelper& fork_helper, | |
| fit::closure provider_behavior) override { | |
| return fork_helper.RunInForkedProcess(std::move(provider_behavior)); | |
| } | |
| pid_t SpawnClient(test_helper::ForkHelper& fork_helper, fit::closure client_behavior) override { | |
| return fork_helper.RunInForkedProcess(std::move(client_behavior)); | |
| } | |
| void ValidateClientSecctxSeenByProvider(std::string_view secctx) override { | |
| // When SELinux is not active, the fallback security context is a single NUL byte, | |
| // which appears as an empty string. | |
| EXPECT_TRUE(secctx.empty()); | |
| } | |
| bool SkipEntirely() override { return !test_helper::IsStarnix(); } | |
| }; | |
| INSTANTIATE_TYPED_TEST_SUITE_P(BinderWithoutSEStarnix, ManagerProviderClientTest, WithoutSEStarnix); | |
| } // namespace starnix_binder |