| OLD | NEW |
| 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 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <netinet/in.h> | 9 #include <netinet/in.h> |
| 10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 #if !defined(ARCH_CPU_X86) | 57 #if !defined(ARCH_CPU_X86) |
| 58 static int socketpair(int domain, int type, int protocol, int sv[2]) { | 58 static int socketpair(int domain, int type, int protocol, int sv[2]) { |
| 59 return Syscall::Call(__NR_socketpair, domain, type, protocol, sv); | 59 return Syscall::Call(__NR_socketpair, domain, type, protocol, sv); |
| 60 } | 60 } |
| 61 #endif | 61 #endif |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 class BasicPolicy : public Policy { | 64 class BasicPolicy : public Policy { |
| 65 public: | 65 public: |
| 66 BasicPolicy() {} | 66 BasicPolicy() {} |
| 67 virtual ~BasicPolicy() {} | 67 ~BasicPolicy() override {} |
| 68 virtual ResultExpr EvaluateSyscall(int sysno) const override { | 68 ResultExpr EvaluateSyscall(int sysno) const override { |
| 69 if (sysno == __NR_getpgid) { | 69 if (sysno == __NR_getpgid) { |
| 70 const Arg<pid_t> pid(0); | 70 const Arg<pid_t> pid(0); |
| 71 return If(pid == 0, Error(EPERM)).Else(Error(EINVAL)); | 71 return If(pid == 0, Error(EPERM)).Else(Error(EINVAL)); |
| 72 } | 72 } |
| 73 if (sysno == __NR_setuid) { | 73 if (sysno == __NR_setuid) { |
| 74 const Arg<uid_t> uid(0); | 74 const Arg<uid_t> uid(0); |
| 75 return If(uid != 42, Error(ESRCH)).Else(Error(ENOMEM)); | 75 return If(uid != 42, Error(ESRCH)).Else(Error(ENOMEM)); |
| 76 } | 76 } |
| 77 return Allow(); | 77 return Allow(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 private: | 80 private: |
| 81 DISALLOW_COPY_AND_ASSIGN(BasicPolicy); | 81 DISALLOW_COPY_AND_ASSIGN(BasicPolicy); |
| 82 }; | 82 }; |
| 83 | 83 |
| 84 BPF_TEST_C(BPFDSL, Basic, BasicPolicy) { | 84 BPF_TEST_C(BPFDSL, Basic, BasicPolicy) { |
| 85 ASSERT_SYSCALL_RESULT(-EPERM, getpgid, 0); | 85 ASSERT_SYSCALL_RESULT(-EPERM, getpgid, 0); |
| 86 ASSERT_SYSCALL_RESULT(-EINVAL, getpgid, 1); | 86 ASSERT_SYSCALL_RESULT(-EINVAL, getpgid, 1); |
| 87 | 87 |
| 88 ASSERT_SYSCALL_RESULT(-ENOMEM, setuid, 42); | 88 ASSERT_SYSCALL_RESULT(-ENOMEM, setuid, 42); |
| 89 ASSERT_SYSCALL_RESULT(-ESRCH, setuid, 43); | 89 ASSERT_SYSCALL_RESULT(-ESRCH, setuid, 43); |
| 90 } | 90 } |
| 91 | 91 |
| 92 /* On IA-32, socketpair() is implemented via socketcall(). :-( */ | 92 /* On IA-32, socketpair() is implemented via socketcall(). :-( */ |
| 93 #if !defined(ARCH_CPU_X86) | 93 #if !defined(ARCH_CPU_X86) |
| 94 class BooleanLogicPolicy : public Policy { | 94 class BooleanLogicPolicy : public Policy { |
| 95 public: | 95 public: |
| 96 BooleanLogicPolicy() {} | 96 BooleanLogicPolicy() {} |
| 97 virtual ~BooleanLogicPolicy() {} | 97 ~BooleanLogicPolicy() override {} |
| 98 virtual ResultExpr EvaluateSyscall(int sysno) const override { | 98 ResultExpr EvaluateSyscall(int sysno) const override { |
| 99 if (sysno == __NR_socketpair) { | 99 if (sysno == __NR_socketpair) { |
| 100 const Arg<int> domain(0), type(1), protocol(2); | 100 const Arg<int> domain(0), type(1), protocol(2); |
| 101 return If(domain == AF_UNIX && | 101 return If(domain == AF_UNIX && |
| 102 (type == SOCK_STREAM || type == SOCK_DGRAM) && | 102 (type == SOCK_STREAM || type == SOCK_DGRAM) && |
| 103 protocol == 0, | 103 protocol == 0, |
| 104 Error(EPERM)).Else(Error(EINVAL)); | 104 Error(EPERM)).Else(Error(EINVAL)); |
| 105 } | 105 } |
| 106 return Allow(); | 106 return Allow(); |
| 107 } | 107 } |
| 108 | 108 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 125 | 125 |
| 126 // Completely unacceptable combination; should also return EINVAL. | 126 // Completely unacceptable combination; should also return EINVAL. |
| 127 ASSERT_SYSCALL_RESULT( | 127 ASSERT_SYSCALL_RESULT( |
| 128 -EINVAL, socketpair, AF_INET, SOCK_SEQPACKET, IPPROTO_UDP, sv); | 128 -EINVAL, socketpair, AF_INET, SOCK_SEQPACKET, IPPROTO_UDP, sv); |
| 129 } | 129 } |
| 130 #endif // !ARCH_CPU_X86 | 130 #endif // !ARCH_CPU_X86 |
| 131 | 131 |
| 132 class MoreBooleanLogicPolicy : public Policy { | 132 class MoreBooleanLogicPolicy : public Policy { |
| 133 public: | 133 public: |
| 134 MoreBooleanLogicPolicy() {} | 134 MoreBooleanLogicPolicy() {} |
| 135 virtual ~MoreBooleanLogicPolicy() {} | 135 ~MoreBooleanLogicPolicy() override {} |
| 136 virtual ResultExpr EvaluateSyscall(int sysno) const override { | 136 ResultExpr EvaluateSyscall(int sysno) const override { |
| 137 if (sysno == __NR_setresuid) { | 137 if (sysno == __NR_setresuid) { |
| 138 const Arg<uid_t> ruid(0), euid(1), suid(2); | 138 const Arg<uid_t> ruid(0), euid(1), suid(2); |
| 139 return If(ruid == 0 || euid == 0 || suid == 0, Error(EPERM)) | 139 return If(ruid == 0 || euid == 0 || suid == 0, Error(EPERM)) |
| 140 .ElseIf(ruid == 1 && euid == 1 && suid == 1, Error(EAGAIN)) | 140 .ElseIf(ruid == 1 && euid == 1 && suid == 1, Error(EAGAIN)) |
| 141 .Else(Error(EINVAL)); | 141 .Else(Error(EINVAL)); |
| 142 } | 142 } |
| 143 return Allow(); | 143 return Allow(); |
| 144 } | 144 } |
| 145 | 145 |
| 146 private: | 146 private: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 162 ASSERT_SYSCALL_RESULT(-EINVAL, setresuid, 1, 1, 5); | 162 ASSERT_SYSCALL_RESULT(-EINVAL, setresuid, 1, 1, 5); |
| 163 ASSERT_SYSCALL_RESULT(-EINVAL, setresuid, 3, 4, 5); | 163 ASSERT_SYSCALL_RESULT(-EINVAL, setresuid, 3, 4, 5); |
| 164 } | 164 } |
| 165 | 165 |
| 166 static const uintptr_t kDeadBeefAddr = | 166 static const uintptr_t kDeadBeefAddr = |
| 167 static_cast<uintptr_t>(0xdeadbeefdeadbeefULL); | 167 static_cast<uintptr_t>(0xdeadbeefdeadbeefULL); |
| 168 | 168 |
| 169 class ArgSizePolicy : public Policy { | 169 class ArgSizePolicy : public Policy { |
| 170 public: | 170 public: |
| 171 ArgSizePolicy() {} | 171 ArgSizePolicy() {} |
| 172 virtual ~ArgSizePolicy() {} | 172 ~ArgSizePolicy() override {} |
| 173 virtual ResultExpr EvaluateSyscall(int sysno) const override { | 173 ResultExpr EvaluateSyscall(int sysno) const override { |
| 174 if (sysno == __NR_uname) { | 174 if (sysno == __NR_uname) { |
| 175 const Arg<uintptr_t> addr(0); | 175 const Arg<uintptr_t> addr(0); |
| 176 return If(addr == kDeadBeefAddr, Error(EPERM)).Else(Allow()); | 176 return If(addr == kDeadBeefAddr, Error(EPERM)).Else(Allow()); |
| 177 } | 177 } |
| 178 return Allow(); | 178 return Allow(); |
| 179 } | 179 } |
| 180 | 180 |
| 181 private: | 181 private: |
| 182 DISALLOW_COPY_AND_ASSIGN(ArgSizePolicy); | 182 DISALLOW_COPY_AND_ASSIGN(ArgSizePolicy); |
| 183 }; | 183 }; |
| 184 | 184 |
| 185 BPF_TEST_C(BPFDSL, ArgSizeTest, ArgSizePolicy) { | 185 BPF_TEST_C(BPFDSL, ArgSizeTest, ArgSizePolicy) { |
| 186 struct utsname buf; | 186 struct utsname buf; |
| 187 ASSERT_SYSCALL_RESULT(0, uname, &buf); | 187 ASSERT_SYSCALL_RESULT(0, uname, &buf); |
| 188 ASSERT_SYSCALL_RESULT( | 188 ASSERT_SYSCALL_RESULT( |
| 189 -EPERM, uname, reinterpret_cast<struct utsname*>(kDeadBeefAddr)); | 189 -EPERM, uname, reinterpret_cast<struct utsname*>(kDeadBeefAddr)); |
| 190 } | 190 } |
| 191 | 191 |
| 192 class TrappingPolicy : public Policy { | 192 class TrappingPolicy : public Policy { |
| 193 public: | 193 public: |
| 194 TrappingPolicy() {} | 194 TrappingPolicy() {} |
| 195 virtual ~TrappingPolicy() {} | 195 ~TrappingPolicy() override {} |
| 196 virtual ResultExpr EvaluateSyscall(int sysno) const override { | 196 ResultExpr EvaluateSyscall(int sysno) const override { |
| 197 if (sysno == __NR_uname) { | 197 if (sysno == __NR_uname) { |
| 198 return Trap(UnameTrap, &count_); | 198 return Trap(UnameTrap, &count_); |
| 199 } | 199 } |
| 200 return Allow(); | 200 return Allow(); |
| 201 } | 201 } |
| 202 | 202 |
| 203 private: | 203 private: |
| 204 static intptr_t count_; | 204 static intptr_t count_; |
| 205 | 205 |
| 206 static intptr_t UnameTrap(const struct arch_seccomp_data& data, void* aux) { | 206 static intptr_t UnameTrap(const struct arch_seccomp_data& data, void* aux) { |
| 207 BPF_ASSERT_EQ(&count_, aux); | 207 BPF_ASSERT_EQ(&count_, aux); |
| 208 return ++count_; | 208 return ++count_; |
| 209 } | 209 } |
| 210 | 210 |
| 211 DISALLOW_COPY_AND_ASSIGN(TrappingPolicy); | 211 DISALLOW_COPY_AND_ASSIGN(TrappingPolicy); |
| 212 }; | 212 }; |
| 213 | 213 |
| 214 intptr_t TrappingPolicy::count_; | 214 intptr_t TrappingPolicy::count_; |
| 215 | 215 |
| 216 BPF_TEST_C(BPFDSL, TrapTest, TrappingPolicy) { | 216 BPF_TEST_C(BPFDSL, TrapTest, TrappingPolicy) { |
| 217 ASSERT_SYSCALL_RESULT(1, uname, NULL); | 217 ASSERT_SYSCALL_RESULT(1, uname, NULL); |
| 218 ASSERT_SYSCALL_RESULT(2, uname, NULL); | 218 ASSERT_SYSCALL_RESULT(2, uname, NULL); |
| 219 ASSERT_SYSCALL_RESULT(3, uname, NULL); | 219 ASSERT_SYSCALL_RESULT(3, uname, NULL); |
| 220 } | 220 } |
| 221 | 221 |
| 222 class MaskingPolicy : public Policy { | 222 class MaskingPolicy : public Policy { |
| 223 public: | 223 public: |
| 224 MaskingPolicy() {} | 224 MaskingPolicy() {} |
| 225 virtual ~MaskingPolicy() {} | 225 ~MaskingPolicy() override {} |
| 226 virtual ResultExpr EvaluateSyscall(int sysno) const override { | 226 ResultExpr EvaluateSyscall(int sysno) const override { |
| 227 if (sysno == __NR_setuid) { | 227 if (sysno == __NR_setuid) { |
| 228 const Arg<uid_t> uid(0); | 228 const Arg<uid_t> uid(0); |
| 229 return If((uid & 0xf) == 0, Error(EINVAL)).Else(Error(EACCES)); | 229 return If((uid & 0xf) == 0, Error(EINVAL)).Else(Error(EACCES)); |
| 230 } | 230 } |
| 231 if (sysno == __NR_setgid) { | 231 if (sysno == __NR_setgid) { |
| 232 const Arg<gid_t> gid(0); | 232 const Arg<gid_t> gid(0); |
| 233 return If((gid & 0xf0) == 0xf0, Error(EINVAL)).Else(Error(EACCES)); | 233 return If((gid & 0xf0) == 0xf0, Error(EINVAL)).Else(Error(EACCES)); |
| 234 } | 234 } |
| 235 if (sysno == __NR_setpgid) { | 235 if (sysno == __NR_setpgid) { |
| 236 const Arg<pid_t> pid(0); | 236 const Arg<pid_t> pid(0); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 256 | 256 |
| 257 for (pid_t pid = 0; pid < 0x100; ++pid) { | 257 for (pid_t pid = 0; pid < 0x100; ++pid) { |
| 258 const int expect_errno = (pid & 0xa5) == 0xa0 ? EINVAL : EACCES; | 258 const int expect_errno = (pid & 0xa5) == 0xa0 ? EINVAL : EACCES; |
| 259 ASSERT_SYSCALL_RESULT(-expect_errno, setpgid, pid, 0); | 259 ASSERT_SYSCALL_RESULT(-expect_errno, setpgid, pid, 0); |
| 260 } | 260 } |
| 261 } | 261 } |
| 262 | 262 |
| 263 class ElseIfPolicy : public Policy { | 263 class ElseIfPolicy : public Policy { |
| 264 public: | 264 public: |
| 265 ElseIfPolicy() {} | 265 ElseIfPolicy() {} |
| 266 virtual ~ElseIfPolicy() {} | 266 ~ElseIfPolicy() override {} |
| 267 virtual ResultExpr EvaluateSyscall(int sysno) const override { | 267 ResultExpr EvaluateSyscall(int sysno) const override { |
| 268 if (sysno == __NR_setuid) { | 268 if (sysno == __NR_setuid) { |
| 269 const Arg<uid_t> uid(0); | 269 const Arg<uid_t> uid(0); |
| 270 return If((uid & 0xfff) == 0, Error(0)) | 270 return If((uid & 0xfff) == 0, Error(0)) |
| 271 .ElseIf((uid & 0xff0) == 0, Error(EINVAL)) | 271 .ElseIf((uid & 0xff0) == 0, Error(EINVAL)) |
| 272 .ElseIf((uid & 0xf00) == 0, Error(EEXIST)) | 272 .ElseIf((uid & 0xf00) == 0, Error(EEXIST)) |
| 273 .Else(Error(EACCES)); | 273 .Else(Error(EACCES)); |
| 274 } | 274 } |
| 275 return Allow(); | 275 return Allow(); |
| 276 } | 276 } |
| 277 | 277 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 288 ASSERT_SYSCALL_RESULT(-EEXIST, setuid, 0x0011); | 288 ASSERT_SYSCALL_RESULT(-EEXIST, setuid, 0x0011); |
| 289 ASSERT_SYSCALL_RESULT(-EEXIST, setuid, 0x0022); | 289 ASSERT_SYSCALL_RESULT(-EEXIST, setuid, 0x0022); |
| 290 | 290 |
| 291 ASSERT_SYSCALL_RESULT(-EACCES, setuid, 0x0111); | 291 ASSERT_SYSCALL_RESULT(-EACCES, setuid, 0x0111); |
| 292 ASSERT_SYSCALL_RESULT(-EACCES, setuid, 0x0222); | 292 ASSERT_SYSCALL_RESULT(-EACCES, setuid, 0x0222); |
| 293 } | 293 } |
| 294 | 294 |
| 295 class SwitchPolicy : public Policy { | 295 class SwitchPolicy : public Policy { |
| 296 public: | 296 public: |
| 297 SwitchPolicy() {} | 297 SwitchPolicy() {} |
| 298 virtual ~SwitchPolicy() {} | 298 ~SwitchPolicy() override {} |
| 299 virtual ResultExpr EvaluateSyscall(int sysno) const override { | 299 ResultExpr EvaluateSyscall(int sysno) const override { |
| 300 if (sysno == __NR_fcntl) { | 300 if (sysno == __NR_fcntl) { |
| 301 const Arg<int> cmd(1); | 301 const Arg<int> cmd(1); |
| 302 const Arg<unsigned long> long_arg(2); | 302 const Arg<unsigned long> long_arg(2); |
| 303 return Switch(cmd) | 303 return Switch(cmd) |
| 304 .CASES((F_GETFL, F_GETFD), Error(ENOENT)) | 304 .CASES((F_GETFL, F_GETFD), Error(ENOENT)) |
| 305 .Case(F_SETFD, If(long_arg == O_CLOEXEC, Allow()).Else(Error(EINVAL))) | 305 .Case(F_SETFD, If(long_arg == O_CLOEXEC, Allow()).Else(Error(EINVAL))) |
| 306 .Case(F_SETFL, Error(EPERM)) | 306 .Case(F_SETFL, Error(EPERM)) |
| 307 .Default(Error(EACCES)); | 307 .Default(Error(EACCES)); |
| 308 } | 308 } |
| 309 return Allow(); | 309 return Allow(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 324 ASSERT_SYSCALL_RESULT(-EINVAL, fcntl, sock_fd.get(), F_SETFD, 0); | 324 ASSERT_SYSCALL_RESULT(-EINVAL, fcntl, sock_fd.get(), F_SETFD, 0); |
| 325 | 325 |
| 326 ASSERT_SYSCALL_RESULT(-EPERM, fcntl, sock_fd.get(), F_SETFL, O_RDONLY); | 326 ASSERT_SYSCALL_RESULT(-EPERM, fcntl, sock_fd.get(), F_SETFL, O_RDONLY); |
| 327 | 327 |
| 328 ASSERT_SYSCALL_RESULT(-EACCES, fcntl, sock_fd.get(), F_DUPFD, 0); | 328 ASSERT_SYSCALL_RESULT(-EACCES, fcntl, sock_fd.get(), F_DUPFD, 0); |
| 329 } | 329 } |
| 330 | 330 |
| 331 } // namespace | 331 } // namespace |
| 332 } // namespace bpf_dsl | 332 } // namespace bpf_dsl |
| 333 } // namespace sandbox | 333 } // namespace sandbox |
| OLD | NEW |