2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/notify.h"
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <sys/types.h>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <syscall.h>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/log/log.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/str_join.h"
|
2023-03-03 22:50:17 +08:00
|
|
|
#include "absl/strings/string_view.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/comms.h"
|
|
|
|
#include "sandboxed_api/sandbox2/executor.h"
|
|
|
|
#include "sandboxed_api/sandbox2/policy.h"
|
|
|
|
#include "sandboxed_api/sandbox2/policybuilder.h"
|
|
|
|
#include "sandboxed_api/sandbox2/sandbox2.h"
|
|
|
|
#include "sandboxed_api/sandbox2/syscall.h"
|
2024-02-03 05:00:50 +08:00
|
|
|
#include "sandboxed_api/sandbox2/trace_all_syscalls.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/testing.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
namespace {
|
|
|
|
|
2023-03-03 22:50:17 +08:00
|
|
|
using ::sapi::CreateDefaultPermissiveTestPolicy;
|
2021-01-14 01:25:25 +08:00
|
|
|
using ::sapi::GetTestSourcePath;
|
2022-02-16 21:58:43 +08:00
|
|
|
using ::testing::Eq;
|
2021-01-14 01:25:25 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Allow typical syscalls and call SECCOMP_RET_TRACE for personality syscall,
|
|
|
|
// chosen because unlikely to be called by a regular program.
|
2023-03-03 22:50:17 +08:00
|
|
|
std::unique_ptr<Policy> NotifyTestcasePolicy(absl::string_view path) {
|
|
|
|
return CreateDefaultPermissiveTestPolicy(path)
|
2019-03-19 00:21:48 +08:00
|
|
|
.AddPolicyOnSyscall(__NR_personality, {SANDBOX2_TRACE})
|
|
|
|
.BuildOrDie();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If syscall and its arguments don't match the expected ones, return the
|
|
|
|
// opposite of the requested values (allow/disallow) to indicate an error.
|
|
|
|
class PersonalityNotify : public Notify {
|
|
|
|
public:
|
|
|
|
explicit PersonalityNotify(bool allow) : allow_(allow) {}
|
|
|
|
|
|
|
|
bool EventSyscallTrap(const Syscall& syscall) override {
|
|
|
|
if (syscall.nr() != __NR_personality) {
|
|
|
|
LOG(ERROR) << "kSyscall==" << syscall.nr();
|
|
|
|
return (!allow_);
|
|
|
|
}
|
|
|
|
Syscall::Args expected_args = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6};
|
|
|
|
if (syscall.args() != expected_args) {
|
|
|
|
LOG(ERROR) << "args=={" << absl::StrJoin(syscall.args(), ", ") << "}";
|
|
|
|
return (!allow_);
|
|
|
|
}
|
|
|
|
return allow_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// The intended return value from EventSyscallTrap in case all registers
|
|
|
|
// match.
|
|
|
|
bool allow_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Print the newly created PID, and exchange data over Comms before sandboxing.
|
|
|
|
class PidCommsNotify : public Notify {
|
|
|
|
public:
|
|
|
|
bool EventStarted(pid_t pid, Comms* comms) final {
|
|
|
|
LOG(INFO) << "The newly created PID: " << pid;
|
|
|
|
bool v;
|
|
|
|
return comms->RecvBool(&v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Test EventSyscallTrap on personality syscall and allow it.
|
|
|
|
TEST(NotifyTest, AllowPersonality) {
|
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/personality");
|
|
|
|
std::vector<std::string> args = {path};
|
2023-03-03 22:50:17 +08:00
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args),
|
|
|
|
NotifyTestcasePolicy(path),
|
2022-10-12 20:22:51 +08:00
|
|
|
std::make_unique<PersonalityNotify>(/*allow=*/true));
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result = s2.Run();
|
|
|
|
|
2022-02-16 21:58:43 +08:00
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::OK));
|
|
|
|
EXPECT_THAT(result.reason_code(), Eq(22));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test EventSyscallTrap on personality syscall and disallow it.
|
|
|
|
TEST(NotifyTest, DisallowPersonality) {
|
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/personality");
|
|
|
|
std::vector<std::string> args = {path};
|
2023-03-03 22:50:17 +08:00
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args),
|
|
|
|
NotifyTestcasePolicy(path),
|
2022-10-12 20:22:51 +08:00
|
|
|
std::make_unique<PersonalityNotify>(/*allow=*/false));
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result = s2.Run();
|
|
|
|
|
2022-02-16 21:58:43 +08:00
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::VIOLATION));
|
|
|
|
EXPECT_THAT(result.reason_code(), Eq(__NR_personality));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test EventStarted by exchanging data after started but before sandboxed.
|
|
|
|
TEST(NotifyTest, PrintPidAndComms) {
|
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/pidcomms");
|
|
|
|
std::vector<std::string> args = {path};
|
2022-10-12 20:22:51 +08:00
|
|
|
auto executor = std::make_unique<Executor>(path, args);
|
2019-03-19 00:21:48 +08:00
|
|
|
executor->set_enable_sandbox_before_exec(false);
|
|
|
|
|
2023-03-03 22:50:17 +08:00
|
|
|
Sandbox2 s2(std::move(executor), NotifyTestcasePolicy(path),
|
2022-10-12 20:22:51 +08:00
|
|
|
std::make_unique<PidCommsNotify>());
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result = s2.Run();
|
|
|
|
|
2022-02-16 21:58:43 +08:00
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::OK));
|
|
|
|
EXPECT_THAT(result.reason_code(), Eq(33));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2024-02-03 05:00:50 +08:00
|
|
|
// Test EventSyscallTrap on personality syscall through TraceAllSyscalls
|
|
|
|
TEST(NotifyTest, TraceAllAllowPersonality) {
|
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/personality");
|
|
|
|
std::vector<std::string> args = {path};
|
|
|
|
auto policy = CreateDefaultPermissiveTestPolicy(path)
|
|
|
|
.DefaultAction(TraceAllSyscalls())
|
|
|
|
.BuildOrDie();
|
|
|
|
Sandbox2 s2(std::make_unique<Executor>(path, args),
|
|
|
|
NotifyTestcasePolicy(path),
|
|
|
|
std::make_unique<PersonalityNotify>(/*allow=*/true));
|
|
|
|
auto result = s2.Run();
|
|
|
|
|
|
|
|
ASSERT_THAT(result.final_status(), Eq(Result::OK));
|
|
|
|
EXPECT_THAT(result.reason_code(), Eq(22));
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace sandbox2
|