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/policybuilder.h"
|
|
|
|
|
|
|
|
#include <syscall.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-10-12 20:22:51 +08:00
|
|
|
#include <memory>
|
2019-03-26 22:54:02 +08:00
|
|
|
#include <string>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/log/log.h"
|
2021-01-22 22:01:05 +08:00
|
|
|
#include "absl/status/status.h"
|
2020-09-02 23:46:48 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/util/bpf_helper.h"
|
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
|
|
|
|
class PolicyBuilderPeer {
|
|
|
|
public:
|
|
|
|
explicit PolicyBuilderPeer(PolicyBuilder* builder) : builder_{builder} {}
|
|
|
|
|
2019-10-09 01:45:07 +08:00
|
|
|
int policy_size() const { return builder_->user_policy_.size(); }
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
static absl::StatusOr<std::string> ValidateAbsolutePath(
|
2019-03-26 22:54:02 +08:00
|
|
|
absl::string_view path) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return PolicyBuilder::ValidateAbsolutePath(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PolicyBuilder* builder_;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
using ::testing::Eq;
|
|
|
|
using ::testing::Lt;
|
|
|
|
using ::testing::StartsWith;
|
|
|
|
using ::testing::StrEq;
|
2022-02-16 21:58:43 +08:00
|
|
|
using ::sapi::IsOk;
|
2021-01-22 22:01:05 +08:00
|
|
|
using ::sapi::StatusIs;
|
|
|
|
|
2023-03-06 16:27:06 +08:00
|
|
|
TEST(PolicyBuilderTest, Testpolicy_size) {
|
2019-03-19 00:21:48 +08:00
|
|
|
ssize_t last_size = 0;
|
|
|
|
PolicyBuilder builder;
|
|
|
|
PolicyBuilderPeer builder_peer{&builder};
|
|
|
|
|
|
|
|
auto assert_increased = [&last_size, &builder_peer]() {
|
|
|
|
ASSERT_THAT(last_size, Lt(builder_peer.policy_size()));
|
|
|
|
last_size = builder_peer.policy_size();
|
|
|
|
};
|
|
|
|
|
|
|
|
auto assert_same = [&last_size, &builder_peer]() {
|
|
|
|
ASSERT_THAT(last_size, Eq(builder_peer.policy_size()));
|
|
|
|
};
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
assert_same();
|
|
|
|
|
|
|
|
builder.AllowSyscall(__NR_chroot); assert_increased();
|
|
|
|
builder.AllowSyscall(__NR_chroot); assert_same();
|
2020-12-17 01:17:53 +08:00
|
|
|
builder.AllowSyscall(__NR_umask); assert_increased();
|
|
|
|
builder.AllowSyscall(__NR_umask); assert_same();
|
2019-03-19 00:21:48 +08:00
|
|
|
builder.AllowSyscall(__NR_chroot); assert_same();
|
|
|
|
builder.AllowSyscall(__NR_chroot); assert_same();
|
|
|
|
|
|
|
|
builder.AllowSystemMalloc(); assert_increased();
|
|
|
|
builder.AllowSyscall(__NR_munmap); assert_same();
|
|
|
|
builder.BlockSyscallWithErrno(__NR_munmap, 1); assert_same();
|
2020-09-11 21:33:57 +08:00
|
|
|
builder.BlockSyscallWithErrno(__NR_openat, 1);
|
2019-03-19 00:21:48 +08:00
|
|
|
assert_increased();
|
|
|
|
|
|
|
|
builder.AllowTCGETS(); assert_increased();
|
|
|
|
builder.AllowTCGETS(); assert_increased();
|
|
|
|
builder.AllowTCGETS(); assert_increased();
|
|
|
|
|
|
|
|
builder.AddPolicyOnSyscall(__NR_fchmod, { ALLOW }); assert_increased();
|
|
|
|
builder.AddPolicyOnSyscall(__NR_fchmod, { ALLOW }); assert_increased();
|
|
|
|
|
|
|
|
builder.AddPolicyOnSyscalls({ __NR_fchmod, __NR_chdir }, { ALLOW });
|
|
|
|
assert_increased();
|
|
|
|
builder.AddPolicyOnSyscalls({ __NR_fchmod, __NR_chdir }, { ALLOW });
|
|
|
|
assert_increased();
|
|
|
|
|
|
|
|
// This might change in the future if we implement an optimization.
|
2020-12-17 01:17:53 +08:00
|
|
|
builder.AddPolicyOnSyscall(__NR_umask, { ALLOW }); assert_increased();
|
|
|
|
builder.AddPolicyOnSyscall(__NR_umask, { ALLOW }); assert_increased();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// None of the namespace functions should alter the seccomp policy.
|
|
|
|
builder.AddFile("/usr/bin/find"); assert_same();
|
|
|
|
builder.AddDirectory("/bin"); assert_same();
|
2021-12-03 20:56:01 +08:00
|
|
|
builder.AddTmpfs("/tmp", /*size=*/4ULL << 20 /* 4 MiB */); assert_same();
|
2019-03-19 00:21:48 +08:00
|
|
|
builder.AllowUnrestrictedNetworking(); assert_same();
|
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:27:06 +08:00
|
|
|
TEST(PolicyBuilderTest, TestValidateAbsolutePath) {
|
2019-03-19 00:21:48 +08:00
|
|
|
for (auto const& bad_path : {
|
|
|
|
"..",
|
|
|
|
"a",
|
|
|
|
"a/b",
|
|
|
|
"a/b/c",
|
|
|
|
"/a/b/c/../d",
|
|
|
|
"/a/b/c/./d",
|
|
|
|
"/a/b/c//d",
|
|
|
|
"/a/b/c/d/",
|
|
|
|
"/a/bAAAAAAAAAAAAAAAAAAAAAA/c/d/",
|
|
|
|
}) {
|
2020-04-02 22:42:17 +08:00
|
|
|
EXPECT_THAT(PolicyBuilderPeer::ValidateAbsolutePath(bad_path),
|
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto const& good_path :
|
|
|
|
{"/", "/a/b/c/d", "/a/b/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"}) {
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
std::string path, PolicyBuilderPeer::ValidateAbsolutePath(good_path));
|
2020-04-02 22:42:17 +08:00
|
|
|
EXPECT_THAT(path, StrEq(good_path));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:27:06 +08:00
|
|
|
TEST(PolicyBuilderTest, TestCanOnlyBuildOnce) {
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder b;
|
2022-02-16 21:58:43 +08:00
|
|
|
ASSERT_THAT(b.TryBuild(), IsOk());
|
|
|
|
EXPECT_THAT(b.TryBuild(), StatusIs(absl::StatusCode::kFailedPrecondition,
|
|
|
|
"Can only build policy once."));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:27:06 +08:00
|
|
|
TEST(PolicyBuilderTest, TestIsCopyable) {
|
2019-10-09 01:45:07 +08:00
|
|
|
PolicyBuilder builder;
|
2023-03-01 21:35:51 +08:00
|
|
|
builder.AllowSyscall(__NR_getpid);
|
2019-10-09 01:45:07 +08:00
|
|
|
|
|
|
|
PolicyBuilder copy = builder;
|
2023-03-01 21:35:51 +08:00
|
|
|
ASSERT_EQ(PolicyBuilderPeer(©).policy_size(),
|
|
|
|
PolicyBuilderPeer(&builder).policy_size());
|
2019-10-09 01:45:07 +08:00
|
|
|
|
2023-03-01 21:35:51 +08:00
|
|
|
// Both can be built.
|
|
|
|
EXPECT_THAT(builder.TryBuild(), IsOk());
|
|
|
|
EXPECT_THAT(copy.TryBuild(), IsOk());
|
2019-10-09 01:45:07 +08:00
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace sandbox2
|