Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(196)

Side by Side Diff: sandbox/linux/bpf_dsl/bpf_dsl.cc

Issue 530133003: bpf_dsl: support arbitrary (arg & mask) == val expressions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add unit tests to sandbox_bpf_unittest.cc Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #include "sandbox/linux/bpf_dsl/bpf_dsl.h" 5 #include "sandbox/linux/bpf_dsl/bpf_dsl.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 8
9 #include <limits> 9 #include <limits>
10 10
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 ResultExpr then_result_; 87 ResultExpr then_result_;
88 ResultExpr else_result_; 88 ResultExpr else_result_;
89 89
90 DISALLOW_COPY_AND_ASSIGN(IfThenResultExprImpl); 90 DISALLOW_COPY_AND_ASSIGN(IfThenResultExprImpl);
91 }; 91 };
92 92
93 class PrimitiveBoolExprImpl : public internal::BoolExprImpl { 93 class PrimitiveBoolExprImpl : public internal::BoolExprImpl {
94 public: 94 public:
95 PrimitiveBoolExprImpl(int argno, 95 PrimitiveBoolExprImpl(int argno,
96 ErrorCode::ArgType is_32bit, 96 ErrorCode::ArgType is_32bit,
97 ErrorCode::Operation op, 97 uint64_t mask,
98 uint64_t value) 98 uint64_t value)
99 : argno_(argno), is_32bit_(is_32bit), op_(op), value_(value) {} 99 : argno_(argno), is_32bit_(is_32bit), mask_(mask), value_(value) {}
100 100
101 virtual ErrorCode Compile(SandboxBPF* sb, 101 virtual ErrorCode Compile(SandboxBPF* sb,
102 ErrorCode true_ec, 102 ErrorCode true_ec,
103 ErrorCode false_ec) const OVERRIDE { 103 ErrorCode false_ec) const OVERRIDE {
104 return sb->Cond(argno_, is_32bit_, op_, value_, true_ec, false_ec); 104 return sb->CondMaskedEqual(
105 argno_, is_32bit_, mask_, value_, true_ec, false_ec);
105 } 106 }
106 107
107 private: 108 private:
108 virtual ~PrimitiveBoolExprImpl() {} 109 virtual ~PrimitiveBoolExprImpl() {}
109 110
110 int argno_; 111 int argno_;
111 ErrorCode::ArgType is_32bit_; 112 ErrorCode::ArgType is_32bit_;
112 ErrorCode::Operation op_; 113 uint64_t mask_;
113 uint64_t value_; 114 uint64_t value_;
114 115
115 DISALLOW_COPY_AND_ASSIGN(PrimitiveBoolExprImpl); 116 DISALLOW_COPY_AND_ASSIGN(PrimitiveBoolExprImpl);
116 }; 117 };
117 118
118 class NegateBoolExprImpl : public internal::BoolExprImpl { 119 class NegateBoolExprImpl : public internal::BoolExprImpl {
119 public: 120 public:
120 explicit NegateBoolExprImpl(const BoolExpr& cond) : cond_(cond) {} 121 explicit NegateBoolExprImpl(const BoolExpr& cond) : cond_(cond) {}
121 122
122 virtual ErrorCode Compile(SandboxBPF* sb, 123 virtual ErrorCode Compile(SandboxBPF* sb,
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 BoolExpr lhs_; 171 BoolExpr lhs_;
171 BoolExpr rhs_; 172 BoolExpr rhs_;
172 173
173 DISALLOW_COPY_AND_ASSIGN(OrBoolExprImpl); 174 DISALLOW_COPY_AND_ASSIGN(OrBoolExprImpl);
174 }; 175 };
175 176
176 } // namespace 177 } // namespace
177 178
178 namespace internal { 179 namespace internal {
179 180
181 uint64_t DefaultMask(size_t size) {
182 switch (size) {
183 case 4:
184 return std::numeric_limits<uint32_t>::max();
185 case 8:
186 return std::numeric_limits<uint64_t>::max();
187 default:
188 CHECK(false) << "Unimplemented DefaultMask case";
189 return 0;
190 }
191 }
192
180 BoolExpr ArgEq(int num, size_t size, uint64_t mask, uint64_t val) { 193 BoolExpr ArgEq(int num, size_t size, uint64_t mask, uint64_t val) {
181 CHECK(num >= 0 && num < 6); 194 CHECK(size == 4 || size == 8);
182 CHECK(size >= 1 && size <= 8);
183 CHECK_NE(0U, mask) << "zero mask doesn't make sense";
184 CHECK_EQ(val, val & mask) << "val contains masked out bits";
185 195
186 // TODO(mdempsky): Should we just always use TP_64BIT? 196 // TODO(mdempsky): Should we just always use TP_64BIT?
187 const ErrorCode::ArgType arg_type = 197 const ErrorCode::ArgType arg_type =
188 (size <= 4) ? ErrorCode::TP_32BIT : ErrorCode::TP_64BIT; 198 (size == 4) ? ErrorCode::TP_32BIT : ErrorCode::TP_64BIT;
189 199
190 if (mask == std::numeric_limits<uint64_t>::max()) { 200 return BoolExpr(new const PrimitiveBoolExprImpl(num, arg_type, mask, val));
191 // Arg == Val
192 return BoolExpr(new const PrimitiveBoolExprImpl(
193 num, arg_type, ErrorCode::OP_EQUAL, val));
194 }
195
196 if (mask == val) {
197 // (Arg & Mask) == Mask
198 return BoolExpr(new const PrimitiveBoolExprImpl(
199 num, arg_type, ErrorCode::OP_HAS_ALL_BITS, mask));
200 }
201
202 if (val == 0) {
203 // (Arg & Mask) == 0, which is semantically equivalent to !((arg & mask) !=
204 // 0).
205 return !BoolExpr(new const PrimitiveBoolExprImpl(
206 num, arg_type, ErrorCode::OP_HAS_ANY_BITS, mask));
207 }
208
209 CHECK(false) << "Unimplemented ArgEq case";
210 return BoolExpr();
211 } 201 }
212 202
213 } // namespace internal 203 } // namespace internal
214 204
215 ResultExpr Allow() { 205 ResultExpr Allow() {
216 return ResultExpr(new const AllowResultExprImpl()); 206 return ResultExpr(new const AllowResultExprImpl());
217 } 207 }
218 208
219 ResultExpr Error(int err) { 209 ResultExpr Error(int err) {
220 return ResultExpr(new const ErrorResultExprImpl(err)); 210 return ResultExpr(new const ErrorResultExprImpl(err));
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 ErrorCode SandboxBPFDSLPolicy::InvalidSyscall(SandboxBPF* sb) const { 288 ErrorCode SandboxBPFDSLPolicy::InvalidSyscall(SandboxBPF* sb) const {
299 return InvalidSyscall()->Compile(sb); 289 return InvalidSyscall()->Compile(sb);
300 } 290 }
301 291
302 ResultExpr SandboxBPFDSLPolicy::Trap(Trap::TrapFnc trap_func, void* aux) { 292 ResultExpr SandboxBPFDSLPolicy::Trap(Trap::TrapFnc trap_func, void* aux) {
303 return bpf_dsl::Trap(trap_func, aux); 293 return bpf_dsl::Trap(trap_func, aux);
304 } 294 }
305 295
306 } // namespace bpf_dsl 296 } // namespace bpf_dsl
307 } // namespace sandbox 297 } // namespace sandbox
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698