This source file includes following definitions.
- fd
- full_file_name
- NoOpCallback
- TEST
- TEST
- TestOpenFilePerms
- TEST
- TEST
- TEST
- TEST
- TestOpenCpuinfo
- TEST
- TEST
- TEST
- SANDBOX_TEST
- TestOpenComplexFlags
- TEST
- TEST
#include "sandbox/linux/services/broker_process.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/file_util.h"
#include "base/files/scoped_file.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/posix/eintr_wrapper.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 {
class ScopedTemporaryFile {
public:
ScopedTemporaryFile()
: fd_(-1) {
#if defined(OS_ANDROID)
static const char file_template[] = "/data/local/tmp/ScopedTempFileXXXXXX";
#else
static const char file_template[] = "/tmp/ScopedTempFileXXXXXX";
#endif
COMPILE_ASSERT(sizeof(full_file_name_) >= sizeof(file_template),
full_file_name_is_large_enough);
memcpy(full_file_name_, file_template, sizeof(file_template));
fd_ = mkstemp(full_file_name_);
CHECK_LE(0, fd_);
}
~ScopedTemporaryFile() {
CHECK_EQ(0, unlink(full_file_name_));
CHECK_EQ(0, IGNORE_EINTR(close(fd_)));
}
int fd() const { return fd_; }
const char* full_file_name() const { return full_file_name_; }
private:
int fd_;
char full_file_name_[128];
DISALLOW_COPY_AND_ASSIGN(ScopedTemporaryFile);
};
bool NoOpCallback() { return true; }
}
TEST(BrokerProcess, CreateAndDestroy) {
std::vector<std::string> read_whitelist;
read_whitelist.push_back("/proc/cpuinfo");
scoped_ptr<BrokerProcess> open_broker(
new BrokerProcess(EPERM, read_whitelist, std::vector<std::string>()));
ASSERT_TRUE(open_broker->Init(base::Bind(&NoOpCallback)));
ASSERT_TRUE(TestUtils::CurrentProcessHasChildren());
open_broker.reset();
ASSERT_FALSE(TestUtils::CurrentProcessHasChildren());
}
TEST(BrokerProcess, TestOpenAccessNull) {
const std::vector<std::string> empty;
BrokerProcess open_broker(EPERM, empty, empty);
ASSERT_TRUE(open_broker.Init(base::Bind(&NoOpCallback)));
int fd = open_broker.Open(NULL, O_RDONLY);
ASSERT_EQ(fd, -EFAULT);
int ret = open_broker.Access(NULL, F_OK);
ASSERT_EQ(ret, -EFAULT);
}
void TestOpenFilePerms(bool fast_check_in_client, int denied_errno) {
const char kR_WhiteListed[] = "/proc/DOESNOTEXIST1";
const char kR_WhiteListedButDenied[] = "/proc/1/auxv";
const char kW_WhiteListed[] = "/proc/DOESNOTEXIST2";
const char kRW_WhiteListed[] = "/proc/DOESNOTEXIST3";
const char k_NotWhitelisted[] = "/proc/DOESNOTEXIST4";
std::vector<std::string> read_whitelist;
read_whitelist.push_back(kR_WhiteListed);
read_whitelist.push_back(kR_WhiteListedButDenied);
read_whitelist.push_back(kRW_WhiteListed);
std::vector<std::string> write_whitelist;
write_whitelist.push_back(kW_WhiteListed);
write_whitelist.push_back(kRW_WhiteListed);
BrokerProcess open_broker(denied_errno,
read_whitelist,
write_whitelist,
fast_check_in_client);
ASSERT_TRUE(open_broker.Init(base::Bind(&NoOpCallback)));
int fd = -1;
fd = open_broker.Open(kR_WhiteListed, O_RDONLY);
ASSERT_EQ(fd, -ENOENT);
fd = open_broker.Open(kR_WhiteListed, O_WRONLY);
ASSERT_EQ(fd, -denied_errno);
fd = open_broker.Open(kR_WhiteListed, O_RDWR);
ASSERT_EQ(fd, -denied_errno);
int ret = -1;
ret = open_broker.Access(kR_WhiteListed, F_OK);
ASSERT_EQ(ret, -ENOENT);
ret = open_broker.Access(kR_WhiteListed, R_OK);
ASSERT_EQ(ret, -ENOENT);
ret = open_broker.Access(kR_WhiteListed, W_OK);
ASSERT_EQ(ret, -denied_errno);
ret = open_broker.Access(kR_WhiteListed, R_OK | W_OK);
ASSERT_EQ(ret, -denied_errno);
ret = open_broker.Access(kR_WhiteListed, X_OK);
ASSERT_EQ(ret, -denied_errno);
ret = open_broker.Access(kR_WhiteListed, R_OK | X_OK);
ASSERT_EQ(ret, -denied_errno);
if (geteuid()) {
fd = open_broker.Open(kR_WhiteListedButDenied, O_RDONLY);
ASSERT_EQ(fd, -EACCES);
fd = open_broker.Open(kR_WhiteListedButDenied, O_WRONLY);
ASSERT_EQ(fd, -denied_errno);
fd = open_broker.Open(kR_WhiteListedButDenied, O_RDWR);
ASSERT_EQ(fd, -denied_errno);
ret = open_broker.Access(kR_WhiteListedButDenied, F_OK);
ASSERT_EQ(ret, 0);
ret = open_broker.Access(kR_WhiteListedButDenied, R_OK);
ASSERT_EQ(ret, -EACCES);
ret = open_broker.Access(kR_WhiteListedButDenied, W_OK);
ASSERT_EQ(ret, -denied_errno);
ret = open_broker.Access(kR_WhiteListedButDenied, R_OK | W_OK);
ASSERT_EQ(ret, -denied_errno);
ret = open_broker.Access(kR_WhiteListedButDenied, X_OK);
ASSERT_EQ(ret, -denied_errno);
ret = open_broker.Access(kR_WhiteListedButDenied, R_OK | X_OK);
ASSERT_EQ(ret, -denied_errno);
}
fd = open_broker.Open(kW_WhiteListed, O_RDONLY);
ASSERT_EQ(fd, -denied_errno);
fd = open_broker.Open(kW_WhiteListed, O_WRONLY);
ASSERT_EQ(fd, -ENOENT);
fd = open_broker.Open(kW_WhiteListed, O_RDWR);
ASSERT_EQ(fd, -denied_errno);
ret = open_broker.Access(kW_WhiteListed, F_OK);
ASSERT_EQ(ret, -ENOENT);
ret = open_broker.Access(kW_WhiteListed, R_OK);
ASSERT_EQ(ret, -denied_errno);
ret = open_broker.Access(kW_WhiteListed, W_OK);
ASSERT_EQ(ret, -ENOENT);
ret = open_broker.Access(kW_WhiteListed, R_OK | W_OK);
ASSERT_EQ(ret, -denied_errno);
ret = open_broker.Access(kW_WhiteListed, X_OK);
ASSERT_EQ(ret, -denied_errno);
ret = open_broker.Access(kW_WhiteListed, R_OK | X_OK);
ASSERT_EQ(ret, -denied_errno);
fd = open_broker.Open(kRW_WhiteListed, O_RDONLY);
ASSERT_EQ(fd, -ENOENT);
fd = open_broker.Open(kRW_WhiteListed, O_WRONLY);
ASSERT_EQ(fd, -ENOENT);
fd = open_broker.Open(kRW_WhiteListed, O_RDWR);
ASSERT_EQ(fd, -ENOENT);
ret = open_broker.Access(kRW_WhiteListed, F_OK);
ASSERT_EQ(ret, -ENOENT);
ret = open_broker.Access(kRW_WhiteListed, R_OK);
ASSERT_EQ(ret, -ENOENT);
ret = open_broker.Access(kRW_WhiteListed, W_OK);
ASSERT_EQ(ret, -ENOENT);
ret = open_broker.Access(kRW_WhiteListed, R_OK | W_OK);
ASSERT_EQ(ret, -ENOENT);
ret = open_broker.Access(kRW_WhiteListed, X_OK);
ASSERT_EQ(ret, -denied_errno);
ret = open_broker.Access(kRW_WhiteListed, R_OK | X_OK);
ASSERT_EQ(ret, -denied_errno);
fd = open_broker.Open(k_NotWhitelisted, O_RDONLY);
ASSERT_EQ(fd, -denied_errno);
fd = open_broker.Open(k_NotWhitelisted, O_WRONLY);
ASSERT_EQ(fd, -denied_errno);
fd = open_broker.Open(k_NotWhitelisted, O_RDWR);
ASSERT_EQ(fd, -denied_errno);
ret = open_broker.Access(k_NotWhitelisted, F_OK);
ASSERT_EQ(ret, -denied_errno);
ret = open_broker.Access(k_NotWhitelisted, R_OK);
ASSERT_EQ(ret, -denied_errno);
ret = open_broker.Access(k_NotWhitelisted, W_OK);
ASSERT_EQ(ret, -denied_errno);
ret = open_broker.Access(k_NotWhitelisted, R_OK | W_OK);
ASSERT_EQ(ret, -denied_errno);
ret = open_broker.Access(k_NotWhitelisted, X_OK);
ASSERT_EQ(ret, -denied_errno);
ret = open_broker.Access(k_NotWhitelisted, R_OK | X_OK);
ASSERT_EQ(ret, -denied_errno);
fd = open_broker.Open(kRW_WhiteListed, O_RDONLY | O_WRONLY | O_RDWR);
ASSERT_EQ(fd, -denied_errno);
fd = open_broker.Open(kRW_WhiteListed, O_RDWR | O_CREAT);
ASSERT_EQ(fd, -denied_errno);
}
TEST(BrokerProcess, OpenFilePermsWithClientCheck) {
TestOpenFilePerms(true , EPERM);
}
TEST(BrokerProcess, OpenOpenFilePermsNoClientCheck) {
TestOpenFilePerms(false , EPERM);
}
TEST(BrokerProcess, OpenFilePermsWithClientCheckNoEnt) {
TestOpenFilePerms(true , ENOENT);
}
TEST(BrokerProcess, OpenOpenFilePermsNoClientCheckNoEnt) {
TestOpenFilePerms(false , ENOENT);
}
void TestOpenCpuinfo(bool fast_check_in_client) {
const char kFileCpuInfo[] = "/proc/cpuinfo";
std::vector<std::string> read_whitelist;
read_whitelist.push_back(kFileCpuInfo);
scoped_ptr<BrokerProcess> open_broker(new BrokerProcess(
EPERM, read_whitelist, std::vector<std::string>(), fast_check_in_client));
ASSERT_TRUE(open_broker->Init(base::Bind(&NoOpCallback)));
int fd = -1;
fd = open_broker->Open(kFileCpuInfo, O_RDWR);
base::ScopedFD fd_closer(fd);
ASSERT_EQ(fd, -EPERM);
int can_access = open_broker->Access(kFileCpuInfo, R_OK);
ASSERT_EQ(can_access, 0);
can_access = open_broker->Access(kFileCpuInfo, W_OK);
ASSERT_EQ(can_access, -EPERM);
int cpuinfo_fd = open_broker->Open(kFileCpuInfo, O_RDONLY);
base::ScopedFD cpuinfo_fd_closer(cpuinfo_fd);
ASSERT_GE(cpuinfo_fd, 0);
char buf[3];
memset(buf, 0, sizeof(buf));
int read_len1 = read(cpuinfo_fd, buf, sizeof(buf));
ASSERT_GT(read_len1, 0);
int cpuinfo_fd2 = open(kFileCpuInfo, O_RDONLY);
base::ScopedFD cpuinfo_fd2_closer(cpuinfo_fd2);
ASSERT_GE(cpuinfo_fd2, 0);
char buf2[3];
memset(buf2, 1, sizeof(buf2));
int read_len2 = read(cpuinfo_fd2, buf2, sizeof(buf2));
ASSERT_GT(read_len1, 0);
ASSERT_EQ(read_len1, read_len2);
ASSERT_EQ(memcmp(buf, buf2, read_len1), 0);
ASSERT_TRUE(TestUtils::CurrentProcessHasChildren());
open_broker.reset();
ASSERT_FALSE(TestUtils::CurrentProcessHasChildren());
}
TEST(BrokerProcess, OpenCpuinfoWithClientCheck) {
TestOpenCpuinfo(true );
}
TEST(BrokerProcess, OpenCpuinfoNoClientCheck) {
TestOpenCpuinfo(false );
}
TEST(BrokerProcess, OpenFileRW) {
ScopedTemporaryFile tempfile;
const char* tempfile_name = tempfile.full_file_name();
std::vector<std::string> whitelist;
whitelist.push_back(tempfile_name);
BrokerProcess open_broker(EPERM, whitelist, whitelist);
ASSERT_TRUE(open_broker.Init(base::Bind(&NoOpCallback)));
int can_access = open_broker.Access(tempfile_name, R_OK | W_OK);
ASSERT_EQ(can_access, 0);
int tempfile2 = -1;
tempfile2 = open_broker.Open(tempfile_name, O_RDWR);
ASSERT_GE(tempfile2, 0);
char test_text[] = "TESTTESTTEST";
ssize_t len = write(tempfile2, test_text, sizeof(test_text));
ASSERT_EQ(len, static_cast<ssize_t>(sizeof(test_text)));
char buf[1024];
len = read(tempfile.fd(), buf, sizeof(buf));
ASSERT_EQ(len, static_cast<ssize_t>(sizeof(test_text)));
ASSERT_EQ(memcmp(test_text, buf, sizeof(test_text)), 0);
ASSERT_EQ(close(tempfile2), 0);
}
SANDBOX_TEST(BrokerProcess, BrokerDied) {
std::vector<std::string> read_whitelist;
read_whitelist.push_back("/proc/cpuinfo");
BrokerProcess open_broker(EPERM,
read_whitelist,
std::vector<std::string>(),
true ,
true );
SANDBOX_ASSERT(open_broker.Init(base::Bind(&NoOpCallback)));
const pid_t broker_pid = open_broker.broker_pid();
SANDBOX_ASSERT(kill(broker_pid, SIGKILL) == 0);
siginfo_t process_info;
SANDBOX_ASSERT(HANDLE_EINTR(waitid(
P_PID, broker_pid, &process_info, WEXITED | WNOWAIT)) ==
0);
SANDBOX_ASSERT(broker_pid == process_info.si_pid);
SANDBOX_ASSERT(CLD_KILLED == process_info.si_code);
SANDBOX_ASSERT(SIGKILL == process_info.si_status);
SANDBOX_ASSERT(open_broker.Open("/proc/cpuinfo", O_RDONLY) == -ENOMEM);
SANDBOX_ASSERT(open_broker.Access("/proc/cpuinfo", O_RDONLY) == -ENOMEM);
}
void TestOpenComplexFlags(bool fast_check_in_client) {
const char kCpuInfo[] = "/proc/cpuinfo";
std::vector<std::string> whitelist;
whitelist.push_back(kCpuInfo);
BrokerProcess open_broker(EPERM,
whitelist,
whitelist,
fast_check_in_client);
ASSERT_TRUE(open_broker.Init(base::Bind(&NoOpCallback)));
int fd = -1;
int ret = 0;
fd = open_broker.Open(kCpuInfo, O_RDONLY);
ASSERT_GE(fd, 0);
ret = fcntl(fd, F_GETFL);
ASSERT_NE(-1, ret);
ASSERT_EQ(0, ret & (O_CLOEXEC | O_NONBLOCK));
ASSERT_EQ(0, close(fd));
fd = open_broker.Open(kCpuInfo, O_RDONLY | O_CLOEXEC);
ASSERT_GE(fd, 0);
ret = fcntl(fd, F_GETFD);
ASSERT_NE(-1, ret);
ASSERT_TRUE(FD_CLOEXEC & ret);
ASSERT_EQ(0, close(fd));
fd = open_broker.Open(kCpuInfo, O_RDONLY | O_NONBLOCK);
ASSERT_GE(fd, 0);
ret = fcntl(fd, F_GETFL);
ASSERT_NE(-1, ret);
ASSERT_TRUE(O_NONBLOCK & ret);
ASSERT_EQ(0, close(fd));
}
TEST(BrokerProcess, OpenComplexFlagsWithClientCheck) {
TestOpenComplexFlags(true );
}
TEST(BrokerProcess, OpenComplexFlagsNoClientCheck) {
TestOpenComplexFlags(false );
}
}