1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "sandbox/linux/syscall_broker/remote_syscall_arg_handler.h"
#include <sys/mman.h>
#include <sys/types.h>
#include <algorithm>
#include <cstring>
#include <tuple>
#include "base/files/scoped_file.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/page_size.h"
#include "base/posix/unix_domain_socket.h"
#include "base/test/bind.h"
#include "sandbox/linux/tests/test_utils.h"
#include "sandbox/linux/tests/unit_tests.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace sandbox {
namespace syscall_broker {
namespace {
const char kPathPart[] = "/i/am/path";
void FillBufferWithPath(char* buf, size_t size, bool null_terminate) {
SANDBOX_ASSERT_LE(size, static_cast<size_t>(PATH_MAX));
size_t str_len = strlen(kPathPart);
size_t len_left_to_write = size;
char* curr_buf_pos = buf;
while (len_left_to_write > 0) {
size_t bytes_to_write = std::min(str_len, len_left_to_write);
memcpy(curr_buf_pos, kPathPart, bytes_to_write);
curr_buf_pos += bytes_to_write;
len_left_to_write -= bytes_to_write;
}
if (null_terminate) {
buf[size - 1] = '\0';
}
}
void VerifyCorrectString(std::string str, size_t size) {
SANDBOX_ASSERT_EQ(str.size(), size);
size_t curr_path_part_pos = 0;
for (char ch : str) {
SANDBOX_ASSERT(ch == kPathPart[curr_path_part_pos]);
curr_path_part_pos++;
curr_path_part_pos %= strlen(kPathPart);
}
}
pid_t ForkWaitingChild(base::OnceCallback<void(int)>
after_parent_signals_callback = base::DoNothing(),
base::ScopedFD* parent_sync_fd = nullptr) {
base::ScopedFD parent_sync, child_sync;
base::CreateSocketPair(&parent_sync, &child_sync);
pid_t pid = fork();
if (!pid) {
parent_sync.reset();
char dummy_char = 'a';
std::vector<base::ScopedFD> empty_fd_vec;
// Wait for parent to exit before exiting ourselves.
base::UnixDomainSocket::RecvMsg(child_sync.get(), &dummy_char, 1,
&empty_fd_vec);
std::move(after_parent_signals_callback).Run(child_sync.get());
_exit(1);
}
child_sync.reset();
if (parent_sync_fd)
*parent_sync_fd = std::move(parent_sync);
else
std::ignore = parent_sync.release(); // Closes when parent dies.
return pid;
}
struct ReadTestConfig {
size_t start_at = 0;
size_t total_size = strlen(kPathPart) + 1;
bool include_null_byte = true;
bool last_page_inaccessible = false;
RemoteProcessIOResult result = RemoteProcessIOResult::kSuccess;
};
void ReadTest(const ReadTestConfig& test_config) {
// Map exactly the right number of pages for the config parameters.
size_t total_pages = (test_config.start_at + test_config.total_size +
base::GetPageSize() - 1) /
base::GetPageSize();
char* mmap_addr = static_cast<char*>(TestUtils::MapPagesOrDie(total_pages));
char* addr = mmap_addr + test_config.start_at;
FillBufferWithPath(addr, test_config.total_size,
test_config.include_null_byte);
if (test_config.last_page_inaccessible)
TestUtils::MprotectLastPageOrDie(mmap_addr, total_pages);
pid_t pid = ForkWaitingChild();
munmap(mmap_addr, base::GetPageSize() * total_pages);
std::string out_str;
SANDBOX_ASSERT_EQ(ReadFilePathFromRemoteProcess(pid, addr, &out_str),
test_config.result);
if (test_config.result == RemoteProcessIOResult::kSuccess) {
VerifyCorrectString(std::move(out_str), test_config.total_size - 1);
}
}
} // namespace
// | path + null_byte |
SANDBOX_TEST(BrokerRemoteSyscallArgHandler, BasicRead) {
ReadTest(ReadTestConfig());
}
// | zero + path... | ...path + null_byte + zero |
SANDBOX_TEST(BrokerRemoteSyscallArgHandler, MultipageRead) {
ReadTestConfig config;
CHECK(PATH_MAX / 2 <= base::GetPageSize());
config.start_at = base::GetPageSize() - (PATH_MAX / 2);
config.total_size = PATH_MAX;
ReadTest(config);
}
// | path... | ...path |
SANDBOX_TEST_ALLOW_NOISE(BrokerRemoteSyscallArgHandler, ReadExceededPathMax) {
ReadTestConfig config;
config.total_size = PATH_MAX * 2;
config.result = RemoteProcessIOResult::kExceededPathMax;
}
// | path... | null_byte + zero |
SANDBOX_TEST_ALLOW_NOISE(BrokerRemoteSyscallArgHandler,
ReadBarelyExceededPathMax) {
ReadTestConfig config;
config.total_size = PATH_MAX + 1;
config.result = RemoteProcessIOResult::kExceededPathMax;
}
// | zero + path... | INACCESSIBLE |
SANDBOX_TEST(BrokerRemoteSyscallArgHandler, ReadUnreadablePage) {
ReadTestConfig config;
config.start_at = base::GetPageSize() - (PATH_MAX / 2);
config.total_size = PATH_MAX / 2;
config.last_page_inaccessible = true;
config.include_null_byte = false;
config.result = RemoteProcessIOResult::kRemoteMemoryInvalid;
ReadTest(config);
}
SANDBOX_TEST(BrokerRemoteSyscallArgHandler, ReadChunkMinus1) {
ReadTestConfig config;
config.total_size = internal::kNumBytesPerChunk - 1;
ReadTest(config);
}
SANDBOX_TEST(BrokerRemoteSyscallArgHandler, ReadChunk) {
ReadTestConfig config;
config.total_size = internal::kNumBytesPerChunk;
ReadTest(config);
}
SANDBOX_TEST(BrokerRemoteSyscallArgHandler, ReadChunkPlus1) {
ReadTestConfig config;
config.total_size = internal::kNumBytesPerChunk + 1;
ReadTest(config);
}
SANDBOX_TEST(BrokerRemoteSyscallArgHandler, ReadChunkEndingAtPage) {
ReadTestConfig config;
config.start_at = base::GetPageSize() - internal::kNumBytesPerChunk;
config.total_size = internal::kNumBytesPerChunk;
ReadTest(config);
}
SANDBOX_TEST(BrokerRemoteSyscallArgHandler, ReadChunkEndingOnePastPage) {
ReadTestConfig config;
config.start_at = base::GetPageSize() - internal::kNumBytesPerChunk + 1;
config.total_size = internal::kNumBytesPerChunk;
ReadTest(config);
}
SANDBOX_TEST(BrokerRemoteSyscallArgHandler, ReadChunkPlus1EndingOnePastPage) {
ReadTestConfig config;
config.start_at = base::GetPageSize() - internal::kNumBytesPerChunk;
config.total_size = internal::kNumBytesPerChunk + 1;
ReadTest(config);
}
SANDBOX_TEST(BrokerRemoteSyscallArgHandler, ReadChildExited) {
void* addr = TestUtils::MapPagesOrDie(1);
FillBufferWithPath(static_cast<char*>(addr), strlen(kPathPart) + 1, true);
base::ScopedFD parent_sync, child_sync;
base::CreateSocketPair(&parent_sync, &child_sync);
pid_t pid = fork();
if (!pid) {
parent_sync.reset();
_exit(1);
}
child_sync.reset();
// Wait for child to exit before reading memory.
char dummy_char = 'a';
std::vector<base::ScopedFD> empty_fd_vec;
base::UnixDomainSocket::RecvMsg(parent_sync.get(), &dummy_char, 1,
&empty_fd_vec);
munmap(addr, base::GetPageSize());
std::string out_str;
SANDBOX_ASSERT_EQ(ReadFilePathFromRemoteProcess(pid, addr, &out_str),
RemoteProcessIOResult::kRemoteExited);
}
SANDBOX_TEST(BrokerRemoteSyscallArgHandler, BasicWrite) {
void* read_from = TestUtils::MapPagesOrDie(1);
const size_t write_size = base::GetPageSize();
FillBufferWithPath(static_cast<char*>(read_from), write_size, false);
char* write_to = static_cast<char*>(TestUtils::MapPagesOrDie(1));
base::ScopedFD parent_signal_fd;
const std::vector<int> empty_fd_vec;
pid_t pid =
ForkWaitingChild(base::BindLambdaForTesting([=](int child_sync_fd) {
// Check correct result received and tell parent about
// success.
int res = memcmp(read_from, write_to, write_size);
base::UnixDomainSocket::SendMsg(
child_sync_fd, &res, sizeof(res), empty_fd_vec);
_exit(1);
}),
&parent_signal_fd);
RemoteProcessIOResult result = WriteRemoteData(
pid, reinterpret_cast<uintptr_t>(write_to), write_size,
base::span<char>(static_cast<char*>(read_from), write_size));
SANDBOX_ASSERT_EQ(result, RemoteProcessIOResult::kSuccess);
// Release child.
char dummy_char = 'a';
base::UnixDomainSocket::SendMsg(parent_signal_fd.get(), &dummy_char, 1,
empty_fd_vec);
// Read result of memcmp and assert.
int memcmp_res;
std::vector<base::ScopedFD> dummy_fd_vec;
base::UnixDomainSocket::RecvMsg(parent_signal_fd.get(), &memcmp_res,
sizeof(memcmp_res), &dummy_fd_vec);
SANDBOX_ASSERT_EQ(memcmp_res, 0);
}
SANDBOX_TEST(BrokerRemoteSyscallArgHandler, WriteToInvalidAddress) {
char* write_to = static_cast<char*>(TestUtils::MapPagesOrDie(1));
TestUtils::MprotectLastPageOrDie(write_to, 1);
base::ScopedFD parent_signal_fd;
const std::vector<int> empty_fd_vec;
pid_t pid = ForkWaitingChild();
munmap(write_to, base::GetPageSize());
char buf[5];
memset(buf, 'a', sizeof(buf));
RemoteProcessIOResult result =
WriteRemoteData(pid, reinterpret_cast<uintptr_t>(write_to), sizeof(buf),
base::span<char>(buf, sizeof(buf)));
SANDBOX_ASSERT_EQ(result, RemoteProcessIOResult::kRemoteMemoryInvalid);
}
SANDBOX_TEST(BrokerRemoteSyscallArgHandler, WritePartiallyToInvalidAddress) {
char* read_from = static_cast<char*>(TestUtils::MapPagesOrDie(2));
const size_t write_size = base::GetPageSize();
FillBufferWithPath(static_cast<char*>(read_from), write_size, false);
char* write_to = static_cast<char*>(TestUtils::MapPagesOrDie(2));
TestUtils::MprotectLastPageOrDie(write_to, 2);
write_to += base::GetPageSize() / 2;
base::ScopedFD parent_signal_fd;
const std::vector<int> empty_fd_vec;
pid_t pid = ForkWaitingChild();
munmap(write_to, base::GetPageSize());
RemoteProcessIOResult result =
WriteRemoteData(pid, reinterpret_cast<uintptr_t>(write_to), write_size,
base::span<char>(read_from, write_size));
SANDBOX_ASSERT_EQ(result, RemoteProcessIOResult::kRemoteMemoryInvalid);
}
SANDBOX_TEST(BrokerRemoteSyscallArgHandler, WriteChildExited) {
char* addr = static_cast<char*>(TestUtils::MapPagesOrDie(1));
FillBufferWithPath(static_cast<char*>(addr), strlen(kPathPart) + 1, true);
base::ScopedFD parent_sync, child_sync;
base::CreateSocketPair(&parent_sync, &child_sync);
pid_t pid = fork();
if (!pid) {
parent_sync.reset();
_exit(1);
}
child_sync.reset();
// Wait for child to exit before writing memory.
char dummy_char = 'a';
std::vector<base::ScopedFD> empty_fd_vec;
base::UnixDomainSocket::RecvMsg(parent_sync.get(), &dummy_char, 1,
&empty_fd_vec);
std::string out_str;
SANDBOX_ASSERT_EQ(
WriteRemoteData(pid, reinterpret_cast<uintptr_t>(addr), strlen(kPathPart),
base::span<char>(addr, strlen(kPathPart))),
RemoteProcessIOResult::kRemoteExited);
}
} // namespace syscall_broker
} // namespace sandbox
|