diff options
author | mdempsky <mdempsky@chromium.org> | 2015-08-25 17:45:52 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-08-26 00:46:42 +0000 |
commit | a3fd0ecb49a769e187952a3ab372edebd4e4ab30 (patch) | |
tree | 452fb7bdee85ecd5f43379ab2613c505f90fb52f /sandbox/linux | |
parent | 5ea5463d16d76e322fee82ad69f93f1e8da1d7b4 (diff) | |
download | chromium_src-a3fd0ecb49a769e187952a3ab372edebd4e4ab30.zip chromium_src-a3fd0ecb49a769e187952a3ab372edebd4e4ab30.tar.gz chromium_src-a3fd0ecb49a769e187952a3ab372edebd4e4ab30.tar.bz2 |
sandbox/linux/bpf_dsl: add golden files for compiled test policies
In preparation for removing the ad-hoc verifier, this CL adds a basic
golden-files regression framework for the bpf_dsl unit tests. Policies
are compiled to byte code and expected to exactly match the pre-compiled
copies. This means future changes to the policies or compiler may
require regenerating golden files, but that's not expected to be very
frequent.
The golden files are currently only for i386 and x86-64, but as most
of bpf_dsl is agnostic to the target platform except for word size,
that should suffice for now. Followup CL will add golden files
for ARM.
BUG=414363
Review URL: https://codereview.chromium.org/1306723002
Cr-Commit-Position: refs/heads/master@{#345508}
Diffstat (limited to 'sandbox/linux')
20 files changed, 627 insertions, 28 deletions
diff --git a/sandbox/linux/BUILD.gn b/sandbox/linux/BUILD.gn index d632bd4..1499739 100644 --- a/sandbox/linux/BUILD.gn +++ b/sandbox/linux/BUILD.gn @@ -131,6 +131,7 @@ source_set("sandbox_linux_unittests_sources") { "seccomp-bpf/syscall_unittest.cc", "seccomp-bpf/trap_unittest.cc", ] + deps += [ ":bpf_dsl_golden" ] } if (compile_credentials) { sources += [ @@ -149,6 +150,32 @@ source_set("sandbox_linux_unittests_sources") { } } +action("bpf_dsl_golden") { + script = "bpf_dsl/golden/generate.py" + inputs = [ + "bpf_dsl/golden/i386/ArgSizePolicy.txt", + "bpf_dsl/golden/i386/BasicPolicy.txt", + "bpf_dsl/golden/i386/ElseIfPolicy.txt", + "bpf_dsl/golden/i386/MaskingPolicy.txt", + "bpf_dsl/golden/i386/MoreBooleanLogicPolicy.txt", + "bpf_dsl/golden/i386/NegativeConstantsPolicy.txt", + "bpf_dsl/golden/i386/SwitchPolicy.txt", + "bpf_dsl/golden/x86-64/ArgSizePolicy.txt", + "bpf_dsl/golden/x86-64/BasicPolicy.txt", + "bpf_dsl/golden/x86-64/BooleanLogicPolicy.txt", + "bpf_dsl/golden/x86-64/ElseIfPolicy.txt", + "bpf_dsl/golden/x86-64/MaskingPolicy.txt", + "bpf_dsl/golden/x86-64/MoreBooleanLogicPolicy.txt", + "bpf_dsl/golden/x86-64/NegativeConstantsPolicy.txt", + "bpf_dsl/golden/x86-64/SwitchPolicy.txt", + ] + outputs = [ + "$target_gen_dir/bpf_dsl/golden/golden_files.h", + ] + args = + rebase_path(outputs, root_build_dir) + rebase_path(inputs, root_build_dir) +} + # TODO(GYP): Delete this after we've converted everything to GN. # The _run targets exist only for compatibility w/ GYP. group("sandbox_linux_unittests_run") { diff --git a/sandbox/linux/bpf_dsl/bpf_dsl_unittest.cc b/sandbox/linux/bpf_dsl/bpf_dsl_unittest.cc index 5da1e57..f1d8b3e 100644 --- a/sandbox/linux/bpf_dsl/bpf_dsl_unittest.cc +++ b/sandbox/linux/bpf_dsl/bpf_dsl_unittest.cc @@ -20,6 +20,8 @@ #include "build/build_config.h" #include "sandbox/linux/bpf_dsl/bpf_dsl_impl.h" #include "sandbox/linux/bpf_dsl/codegen.h" +#include "sandbox/linux/bpf_dsl/dump_bpf.h" +#include "sandbox/linux/bpf_dsl/golden/golden_files.h" #include "sandbox/linux/bpf_dsl/policy.h" #include "sandbox/linux/bpf_dsl/policy_compiler.h" #include "sandbox/linux/bpf_dsl/seccomp_macros.h" @@ -59,21 +61,29 @@ struct arch_seccomp_data FakeSyscall(int nr, class PolicyEmulator { public: - explicit PolicyEmulator(const Policy* policy) : program_(), traps_() { - program_ = *PolicyCompiler(policy, &traps_).Compile(true /* verify */); - } - ~PolicyEmulator() {} + PolicyEmulator(const golden::Golden& golden, const Policy& policy) + : program_() { + TestTrapRegistry traps; + program_ = *PolicyCompiler(&policy, &traps).Compile(true /* verify */); + + // TODO(mdempsky): Generalize to more arches. + const char* expected = nullptr; +#if defined(ARCH_CPU_X86) + expected = golden.i386_dump; +#elif defined(ARCH_CPU_X86_64) + expected = golden.x86_64_dump; +#endif - uint32_t Emulate(const struct arch_seccomp_data& data) const { - const char* err = nullptr; - uint32_t res = Verifier::EvaluateBPF(program_, data, &err); - if (err) { - ADD_FAILURE() << err; - return 0; + if (expected != nullptr) { + const std::string actual = DumpBPF::StringPrintProgram(program_); + EXPECT_EQ(expected, actual); + } else { + LOG(WARNING) << "Missing golden file data entry"; } - return res; } + ~PolicyEmulator() {} + void ExpectAllow(const struct arch_seccomp_data& data) const { EXPECT_EQ(SECCOMP_RET_ALLOW, Emulate(data)); } @@ -87,8 +97,17 @@ class PolicyEmulator { } private: + uint32_t Emulate(const struct arch_seccomp_data& data) const { + const char* err = nullptr; + uint32_t res = Verifier::EvaluateBPF(program_, data, &err); + if (err) { + ADD_FAILURE() << err; + return 0; + } + return res; + } + CodeGen::Program program_; - TestTrapRegistry traps_; DISALLOW_COPY_AND_ASSIGN(PolicyEmulator); }; @@ -114,8 +133,7 @@ class BasicPolicy : public Policy { }; TEST(BPFDSL, Basic) { - BasicPolicy policy; - PolicyEmulator emulator(&policy); + PolicyEmulator emulator(golden::kBasicPolicy, BasicPolicy()); emulator.ExpectErrno(EPERM, FakeSyscall(__NR_getpgid, 0)); emulator.ExpectErrno(EINVAL, FakeSyscall(__NR_getpgid, 1)); @@ -146,8 +164,7 @@ class BooleanLogicPolicy : public Policy { }; TEST(BPFDSL, BooleanLogic) { - BooleanLogicPolicy policy; - PolicyEmulator emulator(&policy); + PolicyEmulator emulator(golden::kBooleanLogicPolicy, BooleanLogicPolicy()); const intptr_t kFakeSV = 0x12345; @@ -191,8 +208,8 @@ class MoreBooleanLogicPolicy : public Policy { }; TEST(BPFDSL, MoreBooleanLogic) { - MoreBooleanLogicPolicy policy; - PolicyEmulator emulator(&policy); + PolicyEmulator emulator(golden::kMoreBooleanLogicPolicy, + MoreBooleanLogicPolicy()); // Expect EPERM if any set to 0. emulator.ExpectErrno(EPERM, FakeSyscall(__NR_setresuid, 0, 5, 5)); @@ -229,8 +246,7 @@ class ArgSizePolicy : public Policy { }; TEST(BPFDSL, ArgSizeTest) { - ArgSizePolicy policy; - PolicyEmulator emulator(&policy); + PolicyEmulator emulator(golden::kArgSizePolicy, ArgSizePolicy()); emulator.ExpectAllow(FakeSyscall(__NR_uname, 0)); emulator.ExpectErrno(EPERM, FakeSyscall(__NR_uname, kDeadBeefAddr)); @@ -253,8 +269,8 @@ class NegativeConstantsPolicy : public Policy { }; TEST(BPFDSL, NegativeConstantsTest) { - NegativeConstantsPolicy policy; - PolicyEmulator emulator(&policy); + PolicyEmulator emulator(golden::kNegativeConstantsPolicy, + NegativeConstantsPolicy()); emulator.ExpectAllow(FakeSyscall(__NR_fcntl, -5, F_DUPFD)); emulator.ExpectAllow(FakeSyscall(__NR_fcntl, 20, F_DUPFD)); @@ -320,8 +336,7 @@ class MaskingPolicy : public Policy { }; TEST(BPFDSL, MaskTest) { - MaskingPolicy policy; - PolicyEmulator emulator(&policy); + PolicyEmulator emulator(golden::kMaskingPolicy, MaskingPolicy()); for (uid_t uid = 0; uid < 0x100; ++uid) { const int expect_errno = (uid & 0xf) == 0 ? EINVAL : EACCES; @@ -359,8 +374,7 @@ class ElseIfPolicy : public Policy { }; TEST(BPFDSL, ElseIfTest) { - ElseIfPolicy policy; - PolicyEmulator emulator(&policy); + PolicyEmulator emulator(golden::kElseIfPolicy, ElseIfPolicy()); emulator.ExpectErrno(0, FakeSyscall(__NR_setuid, 0)); @@ -396,8 +410,7 @@ class SwitchPolicy : public Policy { }; TEST(BPFDSL, SwitchTest) { - SwitchPolicy policy; - PolicyEmulator emulator(&policy); + PolicyEmulator emulator(golden::kSwitchPolicy, SwitchPolicy()); const int kFakeSockFD = 42; diff --git a/sandbox/linux/bpf_dsl/golden/generate.py b/sandbox/linux/bpf_dsl/golden/generate.py new file mode 100644 index 0000000..c3ed1d9 --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/generate.py @@ -0,0 +1,51 @@ +# Copyright 2015 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import os +import sys + +arches = ['i386', 'x86-64'] + +goldens = {} + +for fn in sys.argv[2:]: + dir, name = fn.split('/')[-2:] + name = name.rstrip('.txt') + golden = goldens.setdefault(name, [None] * len(arches)) + idx = arches.index(dir) + golden[idx] = open(fn).read() + +with open(sys.argv[1], 'w') as f: + f.write("""// Generated by sandbox/linux/bpf_dsl/golden/generate.py + +#ifndef SANDBOX_LINUX_BPF_DSL_GOLDEN_GOLDEN_FILES_H_ +#define SANDBOX_LINUX_BPF_DSL_GOLDEN_GOLDEN_FILES_H_ + +namespace sandbox { +namespace bpf_dsl { +namespace golden { + +struct Golden { + const char* i386_dump; + const char* x86_64_dump; +}; + +""") + + for name, datas in sorted(goldens.items()): + f.write("const Golden k%s = {\n" % name) + for data in datas: + if data is None: + f.write(" nullptr,\n") + else: + f.write(" \"%s\",\n" % data.replace("\n", "\\n\\\n")) + f.write("};\n\n") + + f.write("""\ +} // namespace golden +} // namespace bpf_dsl +} // namespace sandbox + +#endif // SANDBOX_LINUX_BPF_DSL_GOLDEN_GOLDEN_FILES_H_ +""") diff --git a/sandbox/linux/bpf_dsl/golden/i386/ArgSizePolicy.txt b/sandbox/linux/bpf_dsl/golden/i386/ArgSizePolicy.txt new file mode 100644 index 0000000..a4ab2a8 --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/i386/ArgSizePolicy.txt @@ -0,0 +1,15 @@ + 1) LOAD 4 // Architecture + 2) if A == 0x40000003; then JMP 3 else JMP 10 + 3) LOAD 0 // System call number + 4) if A & 0x40000000; then JMP 10 else JMP 5 + 5) if A >= 0x7b; then JMP 6 else JMP 7 + 6) if A >= 0x401; then JMP 15 else JMP 14 + 7) if A >= 0x7a; then JMP 8 else JMP 14 + 8) LOAD 20 // Argument 0 (MSB) + 9) if A == 0x0; then JMP 11 else JMP 10 + 10) RET 0x0 // Kill + 11) LOAD 16 // Argument 0 (LSB) + 12) if A == 0xdeadbeef; then JMP 13 else JMP 14 + 13) RET 0x50001 // errno = 1 + 14) RET 0x7fff0000 // Allowed + 15) RET 0x50026 // errno = 38 diff --git a/sandbox/linux/bpf_dsl/golden/i386/BasicPolicy.txt b/sandbox/linux/bpf_dsl/golden/i386/BasicPolicy.txt new file mode 100644 index 0000000..a05cfb5 --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/i386/BasicPolicy.txt @@ -0,0 +1,22 @@ + 1) LOAD 4 // Architecture + 2) if A == 0x40000003; then JMP 3 else JMP 20 + 3) LOAD 0 // System call number + 4) if A & 0x40000000; then JMP 20 else JMP 5 + 5) if A >= 0x84; then JMP 6 else JMP 8 + 6) if A >= 0x85; then JMP 7 else JMP 10 + 7) if A >= 0x401; then JMP 22 else JMP 21 + 8) if A >= 0x17; then JMP 9 else JMP 21 + 9) if A >= 0x18; then JMP 21 else JMP 16 + 10) LOAD 20 // Argument 0 (MSB) + 11) if A == 0x0; then JMP 12 else JMP 20 + 12) LOAD 16 // Argument 0 (LSB) + 13) if A == 0x0; then JMP 15 else JMP 14 + 14) RET 0x50016 // errno = 22 + 15) RET 0x50001 // errno = 1 + 16) LOAD 20 // Argument 0 (MSB) + 17) if A == 0x0; then JMP 18 else JMP 20 + 18) LOAD 16 // Argument 0 (LSB) + 19) if A == 0x2a; then JMP 21 else JMP 20 + 20) RET 0x0 // Kill + 21) RET 0x7fff0000 // Allowed + 22) RET 0x50026 // errno = 38 diff --git a/sandbox/linux/bpf_dsl/golden/i386/ElseIfPolicy.txt b/sandbox/linux/bpf_dsl/golden/i386/ElseIfPolicy.txt new file mode 100644 index 0000000..c199147 --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/i386/ElseIfPolicy.txt @@ -0,0 +1,26 @@ + 1) LOAD 4 // Architecture + 2) if A == 0x40000003; then JMP 3 else JMP 18 + 3) LOAD 0 // System call number + 4) if A & 0x40000000; then JMP 18 else JMP 5 + 5) if A >= 0x18; then JMP 6 else JMP 7 + 6) if A >= 0x401; then JMP 26 else JMP 25 + 7) if A >= 0x17; then JMP 8 else JMP 25 + 8) LOAD 20 // Argument 0 (MSB) + 9) if A == 0x0; then JMP 10 else JMP 18 + 10) LOAD 16 // Argument 0 (LSB) + 11) if A & 0xfff; then JMP 12 else JMP 24 + 12) LOAD 20 // Argument 0 (MSB) + 13) if A == 0x0; then JMP 14 else JMP 18 + 14) LOAD 16 // Argument 0 (LSB) + 15) if A & 0xff0; then JMP 16 else JMP 23 + 16) LOAD 20 // Argument 0 (MSB) + 17) if A == 0x0; then JMP 19 else JMP 18 + 18) RET 0x0 // Kill + 19) LOAD 16 // Argument 0 (LSB) + 20) if A & 0xf00; then JMP 21 else JMP 22 + 21) RET 0x5000d // errno = 13 + 22) RET 0x50011 // errno = 17 + 23) RET 0x50016 // errno = 22 + 24) RET 0x50000 // errno = 0 + 25) RET 0x7fff0000 // Allowed + 26) RET 0x50026 // errno = 38 diff --git a/sandbox/linux/bpf_dsl/golden/i386/MaskingPolicy.txt b/sandbox/linux/bpf_dsl/golden/i386/MaskingPolicy.txt new file mode 100644 index 0000000..e594576 --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/i386/MaskingPolicy.txt @@ -0,0 +1,30 @@ + 1) LOAD 4 // Architecture + 2) if A == 0x40000003; then JMP 3 else JMP 24 + 3) LOAD 0 // System call number + 4) if A & 0x40000000; then JMP 24 else JMP 5 + 5) if A >= 0x2f; then JMP 6 else JMP 9 + 6) if A >= 0x3a; then JMP 7 else JMP 8 + 7) if A >= 0x401; then JMP 30 else JMP 29 + 8) if A >= 0x39; then JMP 12 else JMP 29 + 9) if A >= 0x18; then JMP 10 else JMP 11 + 10) if A >= 0x2e; then JMP 17 else JMP 29 + 11) if A >= 0x17; then JMP 22 else JMP 29 + 12) LOAD 20 // Argument 0 (MSB) + 13) if A == 0x0; then JMP 14 else JMP 24 + 14) LOAD 16 // Argument 0 (LSB) + 15) A := A & 0xa5 + 16) if A == 0xa0; then JMP 28 else JMP 27 + 17) LOAD 20 // Argument 0 (MSB) + 18) if A == 0x0; then JMP 19 else JMP 24 + 19) LOAD 16 // Argument 0 (LSB) + 20) A := A & 0xf0 + 21) if A == 0xf0; then JMP 28 else JMP 27 + 22) LOAD 20 // Argument 0 (MSB) + 23) if A == 0x0; then JMP 25 else JMP 24 + 24) RET 0x0 // Kill + 25) LOAD 16 // Argument 0 (LSB) + 26) if A & 0xf; then JMP 27 else JMP 28 + 27) RET 0x5000d // errno = 13 + 28) RET 0x50016 // errno = 22 + 29) RET 0x7fff0000 // Allowed + 30) RET 0x50026 // errno = 38 diff --git a/sandbox/linux/bpf_dsl/golden/i386/MoreBooleanLogicPolicy.txt b/sandbox/linux/bpf_dsl/golden/i386/MoreBooleanLogicPolicy.txt new file mode 100644 index 0000000..0e91064 --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/i386/MoreBooleanLogicPolicy.txt @@ -0,0 +1,37 @@ + 1) LOAD 4 // Architecture + 2) if A == 0x40000003; then JMP 3 else JMP 30 + 3) LOAD 0 // System call number + 4) if A & 0x40000000; then JMP 30 else JMP 5 + 5) if A >= 0xa5; then JMP 6 else JMP 7 + 6) if A >= 0x401; then JMP 37 else JMP 36 + 7) if A >= 0xa4; then JMP 8 else JMP 36 + 8) LOAD 20 // Argument 0 (MSB) + 9) if A == 0x0; then JMP 10 else JMP 30 + 10) LOAD 16 // Argument 0 (LSB) + 11) if A == 0x0; then JMP 35 else JMP 12 + 12) LOAD 28 // Argument 1 (MSB) + 13) if A == 0x0; then JMP 14 else JMP 30 + 14) LOAD 24 // Argument 1 (LSB) + 15) if A == 0x0; then JMP 35 else JMP 16 + 16) LOAD 36 // Argument 2 (MSB) + 17) if A == 0x0; then JMP 18 else JMP 30 + 18) LOAD 32 // Argument 2 (LSB) + 19) if A == 0x0; then JMP 35 else JMP 20 + 20) LOAD 20 // Argument 0 (MSB) + 21) if A == 0x0; then JMP 22 else JMP 30 + 22) LOAD 16 // Argument 0 (LSB) + 23) if A == 0x1; then JMP 24 else JMP 33 + 24) LOAD 28 // Argument 1 (MSB) + 25) if A == 0x0; then JMP 26 else JMP 30 + 26) LOAD 24 // Argument 1 (LSB) + 27) if A == 0x1; then JMP 28 else JMP 33 + 28) LOAD 36 // Argument 2 (MSB) + 29) if A == 0x0; then JMP 31 else JMP 30 + 30) RET 0x0 // Kill + 31) LOAD 32 // Argument 2 (LSB) + 32) if A == 0x1; then JMP 34 else JMP 33 + 33) RET 0x50016 // errno = 22 + 34) RET 0x5000b // errno = 11 + 35) RET 0x50001 // errno = 1 + 36) RET 0x7fff0000 // Allowed + 37) RET 0x50026 // errno = 38 diff --git a/sandbox/linux/bpf_dsl/golden/i386/NegativeConstantsPolicy.txt b/sandbox/linux/bpf_dsl/golden/i386/NegativeConstantsPolicy.txt new file mode 100644 index 0000000..38707e3 --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/i386/NegativeConstantsPolicy.txt @@ -0,0 +1,15 @@ + 1) LOAD 4 // Architecture + 2) if A == 0x40000003; then JMP 3 else JMP 10 + 3) LOAD 0 // System call number + 4) if A & 0x40000000; then JMP 10 else JMP 5 + 5) if A >= 0x38; then JMP 6 else JMP 7 + 6) if A >= 0x401; then JMP 15 else JMP 14 + 7) if A >= 0x37; then JMP 8 else JMP 14 + 8) LOAD 20 // Argument 0 (MSB) + 9) if A == 0x0; then JMP 11 else JMP 10 + 10) RET 0x0 // Kill + 11) LOAD 16 // Argument 0 (LSB) + 12) if A == 0xfffffec6; then JMP 13 else JMP 14 + 13) RET 0x50001 // errno = 1 + 14) RET 0x7fff0000 // Allowed + 15) RET 0x50026 // errno = 38 diff --git a/sandbox/linux/bpf_dsl/golden/i386/SwitchPolicy.txt b/sandbox/linux/bpf_dsl/golden/i386/SwitchPolicy.txt new file mode 100644 index 0000000..22093c4 --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/i386/SwitchPolicy.txt @@ -0,0 +1,34 @@ + 1) LOAD 4 // Architecture + 2) if A == 0x40000003; then JMP 3 else JMP 28 + 3) LOAD 0 // System call number + 4) if A & 0x40000000; then JMP 28 else JMP 5 + 5) if A >= 0x38; then JMP 6 else JMP 7 + 6) if A >= 0x401; then JMP 34 else JMP 33 + 7) if A >= 0x37; then JMP 8 else JMP 33 + 8) LOAD 28 // Argument 1 (MSB) + 9) if A == 0x0; then JMP 10 else JMP 28 + 10) LOAD 24 // Argument 1 (LSB) + 11) if A == 0x3; then JMP 32 else JMP 12 + 12) LOAD 28 // Argument 1 (MSB) + 13) if A == 0x0; then JMP 14 else JMP 28 + 14) LOAD 24 // Argument 1 (LSB) + 15) if A == 0x1; then JMP 32 else JMP 16 + 16) LOAD 28 // Argument 1 (MSB) + 17) if A == 0x0; then JMP 18 else JMP 28 + 18) LOAD 24 // Argument 1 (LSB) + 19) if A == 0x2; then JMP 26 else JMP 20 + 20) LOAD 28 // Argument 1 (MSB) + 21) if A == 0x0; then JMP 22 else JMP 28 + 22) LOAD 24 // Argument 1 (LSB) + 23) if A == 0x4; then JMP 25 else JMP 24 + 24) RET 0x5000d // errno = 13 + 25) RET 0x50001 // errno = 1 + 26) LOAD 36 // Argument 2 (MSB) + 27) if A == 0x0; then JMP 29 else JMP 28 + 28) RET 0x0 // Kill + 29) LOAD 32 // Argument 2 (LSB) + 30) if A == 0x80000; then JMP 33 else JMP 31 + 31) RET 0x50016 // errno = 22 + 32) RET 0x50002 // errno = 2 + 33) RET 0x7fff0000 // Allowed + 34) RET 0x50026 // errno = 38 diff --git a/sandbox/linux/bpf_dsl/golden/x86-64/ArgSizePolicy.txt b/sandbox/linux/bpf_dsl/golden/x86-64/ArgSizePolicy.txt new file mode 100644 index 0000000..ca1f610 --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/x86-64/ArgSizePolicy.txt @@ -0,0 +1,15 @@ + 1) LOAD 4 // Architecture + 2) if A == 0xc000003e; then JMP 3 else JMP 5 + 3) LOAD 0 // System call number + 4) if A & 0x40000000; then JMP 5 else JMP 6 + 5) RET 0x0 // Kill + 6) if A >= 0x40; then JMP 7 else JMP 8 + 7) if A >= 0x401; then JMP 15 else JMP 14 + 8) if A >= 0x3f; then JMP 9 else JMP 14 + 9) LOAD 20 // Argument 0 (MSB) + 10) if A == 0xdeadbeef; then JMP 11 else JMP 14 + 11) LOAD 16 // Argument 0 (LSB) + 12) if A == 0xdeadbeef; then JMP 13 else JMP 14 + 13) RET 0x50001 // errno = 1 + 14) RET 0x7fff0000 // Allowed + 15) RET 0x50026 // errno = 38 diff --git a/sandbox/linux/bpf_dsl/golden/x86-64/BasicPolicy.txt b/sandbox/linux/bpf_dsl/golden/x86-64/BasicPolicy.txt new file mode 100644 index 0000000..f0f37e9 --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/x86-64/BasicPolicy.txt @@ -0,0 +1,28 @@ + 1) LOAD 4 // Architecture + 2) if A == 0xc000003e; then JMP 3 else JMP 26 + 3) LOAD 0 // System call number + 4) if A & 0x40000000; then JMP 26 else JMP 5 + 5) if A >= 0x79; then JMP 6 else JMP 8 + 6) if A >= 0x7a; then JMP 7 else JMP 10 + 7) if A >= 0x401; then JMP 28 else JMP 27 + 8) if A >= 0x69; then JMP 9 else JMP 27 + 9) if A >= 0x6a; then JMP 27 else JMP 19 + 10) LOAD 20 // Argument 0 (MSB) + 11) if A == 0x0; then JMP 15 else JMP 12 + 12) if A == 0xffffffff; then JMP 13 else JMP 26 + 13) LOAD 16 // Argument 0 (LSB) + 14) if A & 0x80000000; then JMP 15 else JMP 26 + 15) LOAD 16 // Argument 0 (LSB) + 16) if A == 0x0; then JMP 18 else JMP 17 + 17) RET 0x50016 // errno = 22 + 18) RET 0x50001 // errno = 1 + 19) LOAD 20 // Argument 0 (MSB) + 20) if A == 0x0; then JMP 24 else JMP 21 + 21) if A == 0xffffffff; then JMP 22 else JMP 26 + 22) LOAD 16 // Argument 0 (LSB) + 23) if A & 0x80000000; then JMP 24 else JMP 26 + 24) LOAD 16 // Argument 0 (LSB) + 25) if A == 0x2a; then JMP 27 else JMP 26 + 26) RET 0x0 // Kill + 27) RET 0x7fff0000 // Allowed + 28) RET 0x50026 // errno = 38 diff --git a/sandbox/linux/bpf_dsl/golden/x86-64/BooleanLogicPolicy.txt b/sandbox/linux/bpf_dsl/golden/x86-64/BooleanLogicPolicy.txt new file mode 100644 index 0000000..d6123f1 --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/x86-64/BooleanLogicPolicy.txt @@ -0,0 +1,40 @@ + 1) LOAD 4 // Architecture + 2) if A == 0xc000003e; then JMP 3 else JMP 34 + 3) LOAD 0 // System call number + 4) if A & 0x40000000; then JMP 34 else JMP 5 + 5) if A >= 0x36; then JMP 6 else JMP 7 + 6) if A >= 0x401; then JMP 40 else JMP 39 + 7) if A >= 0x35; then JMP 8 else JMP 39 + 8) LOAD 20 // Argument 0 (MSB) + 9) if A == 0x0; then JMP 13 else JMP 10 + 10) if A == 0xffffffff; then JMP 11 else JMP 34 + 11) LOAD 16 // Argument 0 (LSB) + 12) if A & 0x80000000; then JMP 13 else JMP 34 + 13) LOAD 16 // Argument 0 (LSB) + 14) if A == 0x1; then JMP 15 else JMP 37 + 15) LOAD 28 // Argument 1 (MSB) + 16) if A == 0x0; then JMP 20 else JMP 17 + 17) if A == 0xffffffff; then JMP 18 else JMP 34 + 18) LOAD 24 // Argument 1 (LSB) + 19) if A & 0x80000000; then JMP 20 else JMP 34 + 20) LOAD 24 // Argument 1 (LSB) + 21) if A == 0x1; then JMP 29 else JMP 22 + 22) LOAD 28 // Argument 1 (MSB) + 23) if A == 0x0; then JMP 27 else JMP 24 + 24) if A == 0xffffffff; then JMP 25 else JMP 34 + 25) LOAD 24 // Argument 1 (LSB) + 26) if A & 0x80000000; then JMP 27 else JMP 34 + 27) LOAD 24 // Argument 1 (LSB) + 28) if A == 0x2; then JMP 29 else JMP 37 + 29) LOAD 36 // Argument 2 (MSB) + 30) if A == 0x0; then JMP 35 else JMP 31 + 31) if A == 0xffffffff; then JMP 32 else JMP 34 + 32) LOAD 32 // Argument 2 (LSB) + 33) if A & 0x80000000; then JMP 35 else JMP 34 + 34) RET 0x0 // Kill + 35) LOAD 32 // Argument 2 (LSB) + 36) if A == 0x0; then JMP 38 else JMP 37 + 37) RET 0x50016 // errno = 22 + 38) RET 0x50001 // errno = 1 + 39) RET 0x7fff0000 // Allowed + 40) RET 0x50026 // errno = 38 diff --git a/sandbox/linux/bpf_dsl/golden/x86-64/ElseIfPolicy.txt b/sandbox/linux/bpf_dsl/golden/x86-64/ElseIfPolicy.txt new file mode 100644 index 0000000..5dfc136 --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/x86-64/ElseIfPolicy.txt @@ -0,0 +1,35 @@ + 1) LOAD 4 // Architecture + 2) if A == 0xc000003e; then JMP 3 else JMP 27 + 3) LOAD 0 // System call number + 4) if A & 0x40000000; then JMP 27 else JMP 5 + 5) if A >= 0x6a; then JMP 6 else JMP 7 + 6) if A >= 0x401; then JMP 35 else JMP 34 + 7) if A >= 0x69; then JMP 8 else JMP 34 + 8) LOAD 20 // Argument 0 (MSB) + 9) if A == 0x0; then JMP 13 else JMP 10 + 10) if A == 0xffffffff; then JMP 11 else JMP 27 + 11) LOAD 16 // Argument 0 (LSB) + 12) if A & 0x80000000; then JMP 13 else JMP 27 + 13) LOAD 16 // Argument 0 (LSB) + 14) if A & 0xfff; then JMP 15 else JMP 33 + 15) LOAD 20 // Argument 0 (MSB) + 16) if A == 0x0; then JMP 20 else JMP 17 + 17) if A == 0xffffffff; then JMP 18 else JMP 27 + 18) LOAD 16 // Argument 0 (LSB) + 19) if A & 0x80000000; then JMP 20 else JMP 27 + 20) LOAD 16 // Argument 0 (LSB) + 21) if A & 0xff0; then JMP 22 else JMP 32 + 22) LOAD 20 // Argument 0 (MSB) + 23) if A == 0x0; then JMP 28 else JMP 24 + 24) if A == 0xffffffff; then JMP 25 else JMP 27 + 25) LOAD 16 // Argument 0 (LSB) + 26) if A & 0x80000000; then JMP 28 else JMP 27 + 27) RET 0x0 // Kill + 28) LOAD 16 // Argument 0 (LSB) + 29) if A & 0xf00; then JMP 30 else JMP 31 + 30) RET 0x5000d // errno = 13 + 31) RET 0x50011 // errno = 17 + 32) RET 0x50016 // errno = 22 + 33) RET 0x50000 // errno = 0 + 34) RET 0x7fff0000 // Allowed + 35) RET 0x50026 // errno = 38 diff --git a/sandbox/linux/bpf_dsl/golden/x86-64/MaskingPolicy.txt b/sandbox/linux/bpf_dsl/golden/x86-64/MaskingPolicy.txt new file mode 100644 index 0000000..b6892c0 --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/x86-64/MaskingPolicy.txt @@ -0,0 +1,38 @@ + 1) LOAD 4 // Architecture + 2) if A == 0xc000003e; then JMP 3 else JMP 32 + 3) LOAD 0 // System call number + 4) if A & 0x40000000; then JMP 32 else JMP 5 + 5) if A >= 0x6b; then JMP 6 else JMP 9 + 6) if A >= 0x6e; then JMP 7 else JMP 8 + 7) if A >= 0x401; then JMP 38 else JMP 37 + 8) if A >= 0x6d; then JMP 11 else JMP 37 + 9) if A >= 0x69; then JMP 10 else JMP 37 + 10) if A >= 0x6a; then JMP 19 else JMP 27 + 11) LOAD 20 // Argument 0 (MSB) + 12) if A == 0x0; then JMP 16 else JMP 13 + 13) if A == 0xffffffff; then JMP 14 else JMP 32 + 14) LOAD 16 // Argument 0 (LSB) + 15) if A & 0x80000000; then JMP 16 else JMP 32 + 16) LOAD 16 // Argument 0 (LSB) + 17) A := A & 0xa5 + 18) if A == 0xa0; then JMP 36 else JMP 35 + 19) LOAD 20 // Argument 0 (MSB) + 20) if A == 0x0; then JMP 24 else JMP 21 + 21) if A == 0xffffffff; then JMP 22 else JMP 32 + 22) LOAD 16 // Argument 0 (LSB) + 23) if A & 0x80000000; then JMP 24 else JMP 32 + 24) LOAD 16 // Argument 0 (LSB) + 25) A := A & 0xf0 + 26) if A == 0xf0; then JMP 36 else JMP 35 + 27) LOAD 20 // Argument 0 (MSB) + 28) if A == 0x0; then JMP 33 else JMP 29 + 29) if A == 0xffffffff; then JMP 30 else JMP 32 + 30) LOAD 16 // Argument 0 (LSB) + 31) if A & 0x80000000; then JMP 33 else JMP 32 + 32) RET 0x0 // Kill + 33) LOAD 16 // Argument 0 (LSB) + 34) if A & 0xf; then JMP 35 else JMP 36 + 35) RET 0x5000d // errno = 13 + 36) RET 0x50016 // errno = 22 + 37) RET 0x7fff0000 // Allowed + 38) RET 0x50026 // errno = 38 diff --git a/sandbox/linux/bpf_dsl/golden/x86-64/MoreBooleanLogicPolicy.txt b/sandbox/linux/bpf_dsl/golden/x86-64/MoreBooleanLogicPolicy.txt new file mode 100644 index 0000000..b42e35d --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/x86-64/MoreBooleanLogicPolicy.txt @@ -0,0 +1,55 @@ + 1) LOAD 4 // Architecture + 2) if A == 0xc000003e; then JMP 3 else JMP 48 + 3) LOAD 0 // System call number + 4) if A & 0x40000000; then JMP 48 else JMP 5 + 5) if A >= 0x76; then JMP 6 else JMP 7 + 6) if A >= 0x401; then JMP 55 else JMP 54 + 7) if A >= 0x75; then JMP 8 else JMP 54 + 8) LOAD 20 // Argument 0 (MSB) + 9) if A == 0x0; then JMP 13 else JMP 10 + 10) if A == 0xffffffff; then JMP 11 else JMP 48 + 11) LOAD 16 // Argument 0 (LSB) + 12) if A & 0x80000000; then JMP 13 else JMP 48 + 13) LOAD 16 // Argument 0 (LSB) + 14) if A == 0x0; then JMP 53 else JMP 15 + 15) LOAD 28 // Argument 1 (MSB) + 16) if A == 0x0; then JMP 20 else JMP 17 + 17) if A == 0xffffffff; then JMP 18 else JMP 48 + 18) LOAD 24 // Argument 1 (LSB) + 19) if A & 0x80000000; then JMP 20 else JMP 48 + 20) LOAD 24 // Argument 1 (LSB) + 21) if A == 0x0; then JMP 53 else JMP 22 + 22) LOAD 36 // Argument 2 (MSB) + 23) if A == 0x0; then JMP 27 else JMP 24 + 24) if A == 0xffffffff; then JMP 25 else JMP 48 + 25) LOAD 32 // Argument 2 (LSB) + 26) if A & 0x80000000; then JMP 27 else JMP 48 + 27) LOAD 32 // Argument 2 (LSB) + 28) if A == 0x0; then JMP 53 else JMP 29 + 29) LOAD 20 // Argument 0 (MSB) + 30) if A == 0x0; then JMP 34 else JMP 31 + 31) if A == 0xffffffff; then JMP 32 else JMP 48 + 32) LOAD 16 // Argument 0 (LSB) + 33) if A & 0x80000000; then JMP 34 else JMP 48 + 34) LOAD 16 // Argument 0 (LSB) + 35) if A == 0x1; then JMP 36 else JMP 51 + 36) LOAD 28 // Argument 1 (MSB) + 37) if A == 0x0; then JMP 41 else JMP 38 + 38) if A == 0xffffffff; then JMP 39 else JMP 48 + 39) LOAD 24 // Argument 1 (LSB) + 40) if A & 0x80000000; then JMP 41 else JMP 48 + 41) LOAD 24 // Argument 1 (LSB) + 42) if A == 0x1; then JMP 43 else JMP 51 + 43) LOAD 36 // Argument 2 (MSB) + 44) if A == 0x0; then JMP 49 else JMP 45 + 45) if A == 0xffffffff; then JMP 46 else JMP 48 + 46) LOAD 32 // Argument 2 (LSB) + 47) if A & 0x80000000; then JMP 49 else JMP 48 + 48) RET 0x0 // Kill + 49) LOAD 32 // Argument 2 (LSB) + 50) if A == 0x1; then JMP 52 else JMP 51 + 51) RET 0x50016 // errno = 22 + 52) RET 0x5000b // errno = 11 + 53) RET 0x50001 // errno = 1 + 54) RET 0x7fff0000 // Allowed + 55) RET 0x50026 // errno = 38 diff --git a/sandbox/linux/bpf_dsl/golden/x86-64/NegativeConstantsPolicy.txt b/sandbox/linux/bpf_dsl/golden/x86-64/NegativeConstantsPolicy.txt new file mode 100644 index 0000000..6c0c7c1 --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/x86-64/NegativeConstantsPolicy.txt @@ -0,0 +1,18 @@ + 1) LOAD 4 // Architecture + 2) if A == 0xc000003e; then JMP 3 else JMP 13 + 3) LOAD 0 // System call number + 4) if A & 0x40000000; then JMP 13 else JMP 5 + 5) if A >= 0x49; then JMP 6 else JMP 7 + 6) if A >= 0x401; then JMP 18 else JMP 17 + 7) if A >= 0x48; then JMP 8 else JMP 17 + 8) LOAD 20 // Argument 0 (MSB) + 9) if A == 0x0; then JMP 14 else JMP 10 + 10) if A == 0xffffffff; then JMP 11 else JMP 13 + 11) LOAD 16 // Argument 0 (LSB) + 12) if A & 0x80000000; then JMP 14 else JMP 13 + 13) RET 0x0 // Kill + 14) LOAD 16 // Argument 0 (LSB) + 15) if A == 0xfffffec6; then JMP 16 else JMP 17 + 16) RET 0x50001 // errno = 1 + 17) RET 0x7fff0000 // Allowed + 18) RET 0x50026 // errno = 38 diff --git a/sandbox/linux/bpf_dsl/golden/x86-64/SwitchPolicy.txt b/sandbox/linux/bpf_dsl/golden/x86-64/SwitchPolicy.txt new file mode 100644 index 0000000..21055bb --- /dev/null +++ b/sandbox/linux/bpf_dsl/golden/x86-64/SwitchPolicy.txt @@ -0,0 +1,46 @@ + 1) LOAD 4 // Architecture + 2) if A == 0xc000003e; then JMP 3 else JMP 34 + 3) LOAD 0 // System call number + 4) if A & 0x40000000; then JMP 34 else JMP 5 + 5) if A >= 0x49; then JMP 6 else JMP 7 + 6) if A >= 0x401; then JMP 46 else JMP 45 + 7) if A >= 0x48; then JMP 8 else JMP 45 + 8) LOAD 28 // Argument 1 (MSB) + 9) if A == 0x0; then JMP 13 else JMP 10 + 10) if A == 0xffffffff; then JMP 11 else JMP 34 + 11) LOAD 24 // Argument 1 (LSB) + 12) if A & 0x80000000; then JMP 13 else JMP 34 + 13) LOAD 24 // Argument 1 (LSB) + 14) if A == 0x3; then JMP 44 else JMP 15 + 15) LOAD 28 // Argument 1 (MSB) + 16) if A == 0x0; then JMP 20 else JMP 17 + 17) if A == 0xffffffff; then JMP 18 else JMP 34 + 18) LOAD 24 // Argument 1 (LSB) + 19) if A & 0x80000000; then JMP 20 else JMP 34 + 20) LOAD 24 // Argument 1 (LSB) + 21) if A == 0x1; then JMP 44 else JMP 22 + 22) LOAD 28 // Argument 1 (MSB) + 23) if A == 0x0; then JMP 27 else JMP 24 + 24) if A == 0xffffffff; then JMP 25 else JMP 34 + 25) LOAD 24 // Argument 1 (LSB) + 26) if A & 0x80000000; then JMP 27 else JMP 34 + 27) LOAD 24 // Argument 1 (LSB) + 28) if A == 0x2; then JMP 39 else JMP 29 + 29) LOAD 28 // Argument 1 (MSB) + 30) if A == 0x0; then JMP 35 else JMP 31 + 31) if A == 0xffffffff; then JMP 32 else JMP 34 + 32) LOAD 24 // Argument 1 (LSB) + 33) if A & 0x80000000; then JMP 35 else JMP 34 + 34) RET 0x0 // Kill + 35) LOAD 24 // Argument 1 (LSB) + 36) if A == 0x4; then JMP 38 else JMP 37 + 37) RET 0x5000d // errno = 13 + 38) RET 0x50001 // errno = 1 + 39) LOAD 36 // Argument 2 (MSB) + 40) if A == 0x0; then JMP 41 else JMP 43 + 41) LOAD 32 // Argument 2 (LSB) + 42) if A == 0x80000; then JMP 45 else JMP 43 + 43) RET 0x50016 // errno = 22 + 44) RET 0x50002 // errno = 2 + 45) RET 0x7fff0000 // Allowed + 46) RET 0x50026 // errno = 38 diff --git a/sandbox/linux/sandbox_linux.gypi b/sandbox/linux/sandbox_linux.gypi index 6ab991c..7f521bf 100644 --- a/sandbox/linux/sandbox_linux.gypi +++ b/sandbox/linux/sandbox_linux.gypi @@ -347,6 +347,57 @@ '..', ], }, + { + 'target_name': 'bpf_dsl_golden', + 'type': 'none', + 'actions': [ + { + 'action_name': 'generate', + 'inputs': [ + 'bpf_dsl/golden/generate.py', + 'bpf_dsl/golden/i386/ArgSizePolicy.txt', + 'bpf_dsl/golden/i386/BasicPolicy.txt', + 'bpf_dsl/golden/i386/ElseIfPolicy.txt', + 'bpf_dsl/golden/i386/MaskingPolicy.txt', + 'bpf_dsl/golden/i386/MoreBooleanLogicPolicy.txt', + 'bpf_dsl/golden/i386/NegativeConstantsPolicy.txt', + 'bpf_dsl/golden/i386/SwitchPolicy.txt', + 'bpf_dsl/golden/x86-64/ArgSizePolicy.txt', + 'bpf_dsl/golden/x86-64/BasicPolicy.txt', + 'bpf_dsl/golden/x86-64/BooleanLogicPolicy.txt', + 'bpf_dsl/golden/x86-64/ElseIfPolicy.txt', + 'bpf_dsl/golden/x86-64/MaskingPolicy.txt', + 'bpf_dsl/golden/x86-64/MoreBooleanLogicPolicy.txt', + 'bpf_dsl/golden/x86-64/NegativeConstantsPolicy.txt', + 'bpf_dsl/golden/x86-64/SwitchPolicy.txt', + ], + 'outputs': [ + '<(SHARED_INTERMEDIATE_DIR)/sandbox/linux/bpf_dsl/golden/golden_files.h', + ], + 'action': [ + 'python', + 'linux/bpf_dsl/golden/generate.py', + '<(SHARED_INTERMEDIATE_DIR)/sandbox/linux/bpf_dsl/golden/golden_files.h', + 'linux/bpf_dsl/golden/i386/ArgSizePolicy.txt', + 'linux/bpf_dsl/golden/i386/BasicPolicy.txt', + 'linux/bpf_dsl/golden/i386/ElseIfPolicy.txt', + 'linux/bpf_dsl/golden/i386/MaskingPolicy.txt', + 'linux/bpf_dsl/golden/i386/MoreBooleanLogicPolicy.txt', + 'linux/bpf_dsl/golden/i386/NegativeConstantsPolicy.txt', + 'linux/bpf_dsl/golden/i386/SwitchPolicy.txt', + 'linux/bpf_dsl/golden/x86-64/ArgSizePolicy.txt', + 'linux/bpf_dsl/golden/x86-64/BasicPolicy.txt', + 'linux/bpf_dsl/golden/x86-64/BooleanLogicPolicy.txt', + 'linux/bpf_dsl/golden/x86-64/ElseIfPolicy.txt', + 'linux/bpf_dsl/golden/x86-64/MaskingPolicy.txt', + 'linux/bpf_dsl/golden/x86-64/MoreBooleanLogicPolicy.txt', + 'linux/bpf_dsl/golden/x86-64/NegativeConstantsPolicy.txt', + 'linux/bpf_dsl/golden/x86-64/SwitchPolicy.txt', + ], + 'message': 'Generating header from golden files ...', + }, + ], + }, ], 'conditions': [ [ 'OS=="android"', { diff --git a/sandbox/linux/sandbox_linux_test_sources.gypi b/sandbox/linux/sandbox_linux_test_sources.gypi index 531649e..a0666d2 100644 --- a/sandbox/linux/sandbox_linux_test_sources.gypi +++ b/sandbox/linux/sandbox_linux_test_sources.gypi @@ -57,6 +57,9 @@ 'seccomp-bpf/syscall_unittest.cc', 'seccomp-bpf/trap_unittest.cc', ], + 'dependencies': [ + 'bpf_dsl_golden', + ], }], [ 'compile_credentials==1', { 'sources': [ |