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/ipc.h"
|
|
|
|
|
2022-10-12 20:22:51 +08:00
|
|
|
#include <memory>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <string>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <utility>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <vector>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "sandboxed_api/sandbox2/comms.h"
|
|
|
|
#include "sandboxed_api/sandbox2/executor.h"
|
|
|
|
#include "sandboxed_api/sandbox2/policy.h"
|
|
|
|
#include "sandboxed_api/sandbox2/result.h"
|
|
|
|
#include "sandboxed_api/sandbox2/sandbox2.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/testing.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
constexpr int kPreferredIpcFd = 812;
|
|
|
|
|
2022-10-11 00:38:14 +08:00
|
|
|
class IPCTest : public testing::Test,
|
|
|
|
public testing::WithParamInterface<int> {};
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// This test verifies that mapping fds by name works if the sandbox is enabled
|
|
|
|
// before execve.
|
2022-10-11 00:38:14 +08:00
|
|
|
TEST_P(IPCTest, MapFDByNamePreExecve) {
|
|
|
|
const int fd = GetParam();
|
2019-03-19 00:21:48 +08:00
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/ipc");
|
2022-10-11 00:38:14 +08:00
|
|
|
std::vector<std::string> args = {path, "1", std::to_string(fd)};
|
2022-10-12 20:22:51 +08:00
|
|
|
auto executor = std::make_unique<Executor>(path, args);
|
2022-10-11 00:38:14 +08:00
|
|
|
Comms comms(executor->ipc()->ReceiveFd(fd, "ipc_test"));
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
2023-03-03 22:50:17 +08:00
|
|
|
CreateDefaultPermissiveTestPolicy(path).TryBuild());
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
Sandbox2 s2(std::move(executor), std::move(policy));
|
|
|
|
s2.RunAsync();
|
|
|
|
|
|
|
|
ASSERT_TRUE(comms.SendString("hello"));
|
|
|
|
std::string resp;
|
2022-10-11 00:38:14 +08:00
|
|
|
ASSERT_TRUE(s2.comms()->RecvString(&resp));
|
|
|
|
ASSERT_EQ(resp, "start");
|
|
|
|
ASSERT_TRUE(s2.comms()->SendString("started"));
|
2019-03-19 00:21:48 +08:00
|
|
|
ASSERT_TRUE(comms.RecvString(&resp));
|
|
|
|
ASSERT_EQ(resp, "world");
|
2022-10-11 00:38:14 +08:00
|
|
|
ASSERT_TRUE(s2.comms()->RecvString(&resp));
|
|
|
|
ASSERT_EQ(resp, "finish");
|
|
|
|
ASSERT_TRUE(s2.comms()->SendString("finished"));
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
auto result = s2.AwaitResult();
|
|
|
|
|
|
|
|
ASSERT_EQ(result.final_status(), Result::OK);
|
|
|
|
ASSERT_EQ(result.reason_code(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test verifies that mapping fds by name works if SandboxMeHere() is
|
|
|
|
// called by the sandboxee.
|
2022-10-11 00:38:14 +08:00
|
|
|
TEST_P(IPCTest, MapFDByNamePostExecve) {
|
|
|
|
const int fd = GetParam();
|
2019-03-19 00:21:48 +08:00
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/ipc");
|
2022-10-11 00:38:14 +08:00
|
|
|
std::vector<std::string> args = {path, "2", std::to_string(fd)};
|
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);
|
2022-10-11 00:38:14 +08:00
|
|
|
Comms comms(executor->ipc()->ReceiveFd(fd, "ipc_test"));
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
2023-03-03 22:50:17 +08:00
|
|
|
CreateDefaultPermissiveTestPolicy(path).TryBuild());
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
Sandbox2 s2(std::move(executor), std::move(policy));
|
|
|
|
s2.RunAsync();
|
|
|
|
|
|
|
|
ASSERT_TRUE(comms.SendString("hello"));
|
|
|
|
std::string resp;
|
2022-10-11 00:38:14 +08:00
|
|
|
ASSERT_TRUE(s2.comms()->RecvString(&resp));
|
|
|
|
ASSERT_EQ(resp, "start");
|
|
|
|
ASSERT_TRUE(s2.comms()->SendString("started"));
|
2019-03-19 00:21:48 +08:00
|
|
|
ASSERT_TRUE(comms.RecvString(&resp));
|
|
|
|
ASSERT_EQ(resp, "world");
|
2022-10-11 00:38:14 +08:00
|
|
|
ASSERT_TRUE(s2.comms()->RecvString(&resp));
|
|
|
|
ASSERT_EQ(resp, "finish");
|
|
|
|
ASSERT_TRUE(s2.comms()->SendString("finished"));
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
auto result = s2.AwaitResult();
|
|
|
|
|
|
|
|
ASSERT_EQ(result.final_status(), Result::OK);
|
|
|
|
ASSERT_EQ(result.reason_code(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IPCTest, NoMappedFDsPreExecve) {
|
|
|
|
const std::string path = GetTestSourcePath("sandbox2/testcases/ipc");
|
|
|
|
std::vector<std::string> args = {path, "3"};
|
2022-10-12 20:22:51 +08:00
|
|
|
auto executor = std::make_unique<Executor>(path, args);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
|
2023-03-03 22:50:17 +08:00
|
|
|
CreateDefaultPermissiveTestPolicy(path).TryBuild());
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
Sandbox2 s2(std::move(executor), std::move(policy));
|
|
|
|
auto result = s2.Run();
|
|
|
|
|
|
|
|
ASSERT_EQ(result.final_status(), Result::OK);
|
|
|
|
ASSERT_EQ(result.reason_code(), 0);
|
|
|
|
}
|
|
|
|
|
2022-10-11 00:38:14 +08:00
|
|
|
INSTANTIATE_TEST_SUITE_P(NormalFds, IPCTest, testing::Values(kPreferredIpcFd));
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_SUITE_P(RestrictedFds, IPCTest,
|
|
|
|
testing::Values(Comms::kSandbox2ClientCommsFD,
|
|
|
|
Comms::kSandbox2TargetExecFD));
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace sandbox2
|