| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_VERIFIER_H__ | 5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_VERIFIER_H__ |
| 6 #define SANDBOX_LINUX_SECCOMP_BPF_VERIFIER_H__ | 6 #define SANDBOX_LINUX_SECCOMP_BPF_VERIFIER_H__ |
| 7 | 7 |
| 8 #include <linux/filter.h> | 8 #include <linux/filter.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" | |
| 14 | |
| 15 | 13 |
| 16 namespace playground2 { | 14 namespace playground2 { |
| 17 | 15 |
| 18 class Verifier { | 16 class Verifier { |
| 19 public: | 17 public: |
| 20 // Evaluate the BPF program for all possible inputs and verify that it | 18 // Evaluate the BPF program for all possible inputs and verify that it |
| 21 // computes the correct result. We use the "evaluators" to determine | 19 // computes the correct result. We use the "evaluators" to determine |
| 22 // the full set of possible inputs that we have to iterate over. | 20 // the full set of possible inputs that we have to iterate over. |
| 23 // Returns success, if the BPF filter accurately reflects the rules | 21 // Returns success, if the BPF filter accurately reflects the rules |
| 24 // set by the "evaluators". | 22 // set by the "evaluators". |
| 25 // Upon success, "err" is set to NULL. Upon failure, it contains a static | 23 // Upon success, "err" is set to NULL. Upon failure, it contains a static |
| 26 // error message that does not need to be free()'d. | 24 // error message that does not need to be free()'d. |
| 27 static bool VerifyBPF(const std::vector<struct sock_filter>& program, | 25 static bool VerifyBPF(Sandbox *sandbox, |
| 26 const std::vector<struct sock_filter>& program, |
| 28 const Sandbox::Evaluators& evaluators, | 27 const Sandbox::Evaluators& evaluators, |
| 29 const char **err); | 28 const char **err); |
| 30 | 29 |
| 31 // Evaluate a given BPF program for a particular set of system call | 30 // Evaluate a given BPF program for a particular set of system call |
| 32 // parameters. If evaluation failed for any reason, "err" will be set to | 31 // parameters. If evaluation failed for any reason, "err" will be set to |
| 33 // a non-NULL error string. Otherwise, the BPF program's result will be | 32 // a non-NULL error string. Otherwise, the BPF program's result will be |
| 34 // returned by the function and "err" is NULL. | 33 // returned by the function and "err" is NULL. |
| 35 // We do not actually implement the full BPF state machine, but only the | 34 // We do not actually implement the full BPF state machine, but only the |
| 36 // parts that can actually be generated by our BPF compiler. If this code | 35 // parts that can actually be generated by our BPF compiler. If this code |
| 37 // is used for purposes other than verifying the output of the sandbox's | 36 // is used for purposes other than verifying the output of the sandbox's |
| 38 // BPF compiler, we might have to extend this BPF interpreter. | 37 // BPF compiler, we might have to extend this BPF interpreter. |
| 39 static uint32_t EvaluateBPF(const std::vector<struct sock_filter>& program, | 38 static uint32_t EvaluateBPF(const std::vector<struct sock_filter>& program, |
| 40 const struct arch_seccomp_data& data, | 39 const struct arch_seccomp_data& data, |
| 41 const char **err); | 40 const char **err); |
| 42 | 41 |
| 43 private: | 42 private: |
| 44 struct State { | |
| 45 State(const std::vector<struct sock_filter>& p, | |
| 46 const struct arch_seccomp_data& d) : | |
| 47 program(p), | |
| 48 data(d), | |
| 49 ip(0), | |
| 50 accumulator(0), | |
| 51 acc_is_valid(false) { | |
| 52 } | |
| 53 const std::vector<struct sock_filter>& program; | |
| 54 const struct arch_seccomp_data& data; | |
| 55 unsigned int ip; | |
| 56 uint32_t accumulator; | |
| 57 bool acc_is_valid; | |
| 58 | |
| 59 private: | |
| 60 DISALLOW_IMPLICIT_CONSTRUCTORS(State); | |
| 61 }; | |
| 62 | |
| 63 static uint32_t EvaluateErrorCode(const ErrorCode& code, | |
| 64 const struct arch_seccomp_data& data); | |
| 65 static bool VerifyErrorCode(const std::vector<struct sock_filter>& prg, | |
| 66 struct arch_seccomp_data *data, | |
| 67 const ErrorCode& root_code, | |
| 68 const ErrorCode& code, const char **err); | |
| 69 static void Ld (State *state, const struct sock_filter& insn, | |
| 70 const char **err); | |
| 71 static void Jmp(State *state, const struct sock_filter& insn, | |
| 72 const char **err); | |
| 73 static uint32_t Ret(State *state, const struct sock_filter& insn, | |
| 74 const char **err); | |
| 75 static void Alu(State *state, const struct sock_filter& insn, | |
| 76 const char **err); | |
| 77 | |
| 78 DISALLOW_IMPLICIT_CONSTRUCTORS(Verifier); | 43 DISALLOW_IMPLICIT_CONSTRUCTORS(Verifier); |
| 79 }; | 44 }; |
| 80 | 45 |
| 81 } // namespace | 46 } // namespace |
| 82 | 47 |
| 83 #endif // SANDBOX_LINUX_SECCOMP_BPF_VERIFIER_H__ | 48 #endif // SANDBOX_LINUX_SECCOMP_BPF_VERIFIER_H__ |
| OLD | NEW |