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

Side by Side Diff: components/nacl/loader/nonsfi/nonsfi_sandbox.cc

Issue 239703011: Reland: Add seccomp sandbox for non-SFI NaCl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix asan test Created 6 years, 8 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/nacl/loader/nonsfi/nonsfi_sandbox.h"
6
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <linux/net.h>
10 #include <sys/prctl.h>
11 #include <sys/ptrace.h>
12 #include <sys/mman.h>
13 #include <sys/socket.h>
14 #include <sys/syscall.h>
15
16 #include "base/basictypes.h"
17 #include "base/logging.h"
18 #include "build/build_config.h"
19 #include "content/public/common/sandbox_init.h"
20 #include "sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h"
21 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
22 #include "sandbox/linux/seccomp-bpf/sandbox_bpf_policy.h"
23 #include "sandbox/linux/seccomp-bpf/trap.h"
24 #include "sandbox/linux/services/linux_syscalls.h"
25
26 #if defined(__arm__) && !defined(MAP_STACK)
27 // Chrome OS Daisy (ARM) build environment has old headers.
28 #define MAP_STACK 0x20000
29 #endif
30
31 using sandbox::ErrorCode;
32 using sandbox::SandboxBPF;
33
34 namespace nacl {
35 namespace nonsfi {
36 namespace {
37
38 inline bool IsRunningOnASAN() {
39 #if defined(ADDRESS_SANITIZER)
40 return true;
41 #else
42 return false;
43 #endif
44 }
45
46 ErrorCode RestrictFcntlCommands(SandboxBPF* sb) {
47 ErrorCode::ArgType mask_long_type;
48 if (sizeof(long) == 8) {
49 mask_long_type = ErrorCode::TP_64BIT;
50 } else if (sizeof(long) == 4) {
51 mask_long_type = ErrorCode::TP_32BIT;
52 } else {
53 NOTREACHED();
54 }
55 // We allow following cases:
56 // 1. F_SETFD + FD_CLOEXEC: libevent's epoll_init uses this.
57 // 2. F_GETFL: Used by SetNonBlocking in
58 // message_pump_libevent.cc and Channel::ChannelImpl::CreatePipe
59 // in ipc_channel_posix.cc. Note that the latter does not work
60 // with EPERM.
61 // 3. F_SETFL: Used by evutil_make_socket_nonblocking in
62 // libevent and SetNonBlocking. As the latter mix O_NONBLOCK to
63 // the return value of F_GETFL, so we need to allow O_ACCMODE in
64 // addition to O_NONBLOCK.
65 const unsigned long denied_mask = ~(O_ACCMODE | O_NONBLOCK);
66 return sb->Cond(1, ErrorCode::TP_32BIT,
67 ErrorCode::OP_EQUAL, F_SETFD,
68 sb->Cond(2, mask_long_type,
69 ErrorCode::OP_EQUAL, FD_CLOEXEC,
70 ErrorCode(ErrorCode::ERR_ALLOWED),
71 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL)),
72 sb->Cond(1, ErrorCode::TP_32BIT,
73 ErrorCode::OP_EQUAL, F_GETFL,
74 ErrorCode(ErrorCode::ERR_ALLOWED),
75 sb->Cond(1, ErrorCode::TP_32BIT,
76 ErrorCode::OP_EQUAL, F_SETFL,
77 sb->Cond(2, mask_long_type,
78 ErrorCode::OP_HAS_ANY_BITS, denied_mask,
79 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL),
80 ErrorCode(ErrorCode::ERR_ALLOWED)),
81 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL))));
82 }
83
84 ErrorCode RestrictClone(SandboxBPF* sb) {
85 // We allow clone only for new thread creation.
86 return sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL,
87 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
88 CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS |
89 CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID,
90 ErrorCode(ErrorCode::ERR_ALLOWED),
91 sb->Trap(sandbox::SIGSYSCloneFailure, NULL));
92 }
93
94 ErrorCode RestrictPrctl(SandboxBPF* sb) {
95 // base::PlatformThread::SetName() uses PR_SET_NAME so we return
96 // EPERM for it. Otherwise, we will raise SIGSYS.
97 return sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL,
98 PR_SET_NAME, ErrorCode(EPERM),
99 sb->Trap(sandbox::SIGSYSPrctlFailure, NULL));
100 }
101
102 #if defined(__i386__)
103 ErrorCode RestrictSocketcall(SandboxBPF* sb) {
104 // We only allow socketpair, sendmsg, and recvmsg.
105 return sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL,
106 SYS_SOCKETPAIR,
107 ErrorCode(ErrorCode::ERR_ALLOWED),
108 sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL,
109 SYS_SENDMSG,
110 ErrorCode(ErrorCode::ERR_ALLOWED),
111 sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL,
112 SYS_RECVMSG,
113 ErrorCode(ErrorCode::ERR_ALLOWED),
114 sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL,
115 SYS_SHUTDOWN,
116 ErrorCode(ErrorCode::ERR_ALLOWED),
117 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL)))));
118 }
119 #endif
120
121 ErrorCode RestrictMemoryProtection(SandboxBPF* sb, int argno) {
122 // TODO(jln, keescook, drewry): Limit the use of mmap/mprotect by
123 // adding some features to linux kernel.
124 const uint32_t denied_mask = ~(PROT_READ | PROT_WRITE | PROT_EXEC);
125 return sb->Cond(argno, ErrorCode::TP_32BIT,
126 ErrorCode::OP_HAS_ANY_BITS,
127 denied_mask,
128 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL),
129 ErrorCode(ErrorCode::ERR_ALLOWED));
130 }
131
132 ErrorCode RestrictMmap(SandboxBPF* sb) {
133 const uint32_t denied_flag_mask = ~(MAP_SHARED | MAP_PRIVATE |
134 MAP_ANONYMOUS | MAP_STACK | MAP_FIXED);
135 // TODO(hamaji): Disallow RWX mmap.
136 return sb->Cond(3, ErrorCode::TP_32BIT,
137 ErrorCode::OP_HAS_ANY_BITS,
138 denied_flag_mask,
139 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL),
140 RestrictMemoryProtection(sb, 2));
141 }
142
143 ErrorCode RestrictSocketpair(SandboxBPF* sb) {
144 // Only allow AF_UNIX, PF_UNIX. Crash if anything else is seen.
145 COMPILE_ASSERT(AF_UNIX == PF_UNIX, af_unix_pf_unix_different);
146 return sb->Cond(0, ErrorCode::TP_32BIT,
147 ErrorCode::OP_EQUAL, AF_UNIX,
148 ErrorCode(ErrorCode::ERR_ALLOWED),
149 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL));
150 }
151
152 bool IsGracefullyDenied(int sysno) {
153 if (IsRunningOnASAN()) {
154 // ASan internally uses them, but returning EPERM is fine.
155 if (sysno == __NR_getpid ||
156 sysno == __NR_ioctl ||
157 sysno == __NR_readlink) {
158 return true;
159 }
160 }
161 switch (sysno) {
162 // third_party/libevent uses them, but we can just return -1 from
163 // them as it is just checking getuid() != geteuid() and
164 // getgid() != getegid()
165 #if defined(__i386__) || defined(__arm__)
166 case __NR_getegid32:
167 case __NR_geteuid32:
168 case __NR_getgid32:
169 case __NR_getuid32:
170 #elif defined(__x86_64__)
171 case __NR_getegid:
172 case __NR_geteuid:
173 case __NR_getgid:
174 case __NR_getuid:
175 #endif
176 // tcmalloc calls madvise in TCMalloc_SystemRelease.
177 case __NR_madvise:
178 // EPERM instead of SIGSYS as glibc tries to open files in /proc.
179 // TODO(hamaji): Remove this when we switch to newlib.
180 case __NR_open:
181 // For RunSandboxSanityChecks().
182 case __NR_ptrace:
183 // glibc uses this for its pthread implementation. If we return
184 // EPERM for this, glibc will stop using this.
185 // TODO(hamaji): newlib does not use this. Make this SIGTRAP once
186 // we have switched to newlib.
187 case __NR_set_robust_list:
188 // This is obsolete in ARM EABI, but x86 glibc indirectly calls
189 // this in sysconf.
190 #if defined(__i386__) || defined(__x86_64__)
191 case __NR_time:
192 #endif
193 return true;
194
195 default:
196 return false;
197 }
198 }
199
200 void RunSandboxSanityChecks() {
201 errno = 0;
202 // Make a ptrace request with an invalid PID.
203 long ptrace_ret = ptrace(PTRACE_PEEKUSER, -1 /* pid */, NULL, NULL);
204 CHECK_EQ(-1, ptrace_ret);
205 // Without the sandbox on, this ptrace call would ESRCH instead.
206 CHECK_EQ(EPERM, errno);
207 }
208
209 } // namespace
210
211 ErrorCode NaClNonSfiBPFSandboxPolicy::EvaluateSyscall(
212 SandboxBPF* sb, int sysno) const {
213 return EvaluateSyscallImpl(sb, sysno, NULL);
214 }
215
216 ErrorCode NaClNonSfiBPFSandboxPolicy::EvaluateSyscallImpl(
217 SandboxBPF* sb, int sysno, void*) {
218 switch (sysno) {
219 // Allowed syscalls.
220 #if defined(__i386__) || defined(__arm__)
221 case __NR__llseek:
222 #elif defined(__x86_64__)
223 case __NR_lseek:
224 #endif
225 // NaCl runtime exposes clock_gettime and clock_getres to untrusted code.
226 case __NR_clock_getres:
227 case __NR_clock_gettime:
228 case __NR_close:
229 case __NR_dup:
230 case __NR_dup2:
231 case __NR_epoll_create:
232 case __NR_epoll_ctl:
233 case __NR_epoll_wait:
234 case __NR_exit:
235 case __NR_exit_group:
236 #if defined(__i386__) || defined(__arm__)
237 case __NR_fstat64:
238 #elif defined(__x86_64__)
239 case __NR_fstat:
240 #endif
241 // TODO(hamaji): Allow only FUTEX_PRIVATE_FLAG.
242 case __NR_futex:
243 // TODO(hamaji): Remove the need of gettid. Currently, this is
244 // called from PlatformThread::CurrentId().
245 case __NR_gettid:
246 case __NR_gettimeofday:
247 case __NR_munmap:
248 case __NR_nanosleep:
249 // TODO(hamaji): Remove the need of pipe. Currently, this is
250 // called from base::MessagePumpLibevent::Init().
251 case __NR_pipe:
252 case __NR_pread64:
253 case __NR_pwrite64:
254 case __NR_read:
255 case __NR_restart_syscall:
256 case __NR_sched_yield:
257 // __NR_times needed as clock() is called by CommandBufferHelper, which is
258 // used by NaCl applications that use Pepper's 3D interfaces.
259 // See crbug.com/264856 for details.
260 case __NR_times:
261 case __NR_write:
262 #if defined(__arm__)
263 case __ARM_NR_cacheflush:
264 #endif
265 return ErrorCode(ErrorCode::ERR_ALLOWED);
266
267 case __NR_clone:
268 return RestrictClone(sb);
269
270 #if defined(__x86_64__)
271 case __NR_fcntl:
272 #endif
273 #if defined(__i386__) || defined(__arm__)
274 case __NR_fcntl64:
275 #endif
276 return RestrictFcntlCommands(sb);
277
278 #if defined(__x86_64__)
279 case __NR_mmap:
280 #endif
281 #if defined(__i386__) || defined(__arm__)
282 case __NR_mmap2:
283 #endif
284 return RestrictMmap(sb);
285 case __NR_mprotect:
286 return RestrictMemoryProtection(sb, 2);
287
288 case __NR_prctl:
289 return RestrictPrctl(sb);
290
291 #if defined(__i386__)
292 case __NR_socketcall:
293 return RestrictSocketcall(sb);
294 #endif
295 #if defined(__x86_64__) || defined(__arm__)
296 case __NR_recvmsg:
297 case __NR_sendmsg:
298 case __NR_shutdown:
299 return ErrorCode(ErrorCode::ERR_ALLOWED);
300 case __NR_socketpair:
301 return RestrictSocketpair(sb);
302 #endif
303
304 case __NR_brk:
305 // The behavior of brk on Linux is different from other system
306 // calls. It does not return errno but the current break on
307 // failure. glibc thinks brk failed if the return value of brk
308 // is less than the requested address (i.e., brk(addr) < addr).
309 // So, glibc thinks brk succeeded if we return -EPERM and we
310 // need to return zero instead.
311 return ErrorCode(0);
312
313 default:
314 if (IsGracefullyDenied(sysno))
315 return ErrorCode(EPERM);
316 return sb->Trap(sandbox::CrashSIGSYS_Handler, NULL);
317 }
318 }
319
320 bool InitializeBPFSandbox() {
321 bool sandbox_is_initialized = content::InitializeSandbox(
322 scoped_ptr<sandbox::SandboxBPFPolicy>(
323 new nacl::nonsfi::NaClNonSfiBPFSandboxPolicy()));
324 if (!sandbox_is_initialized)
325 return false;
326 RunSandboxSanityChecks();
327 return true;
328 }
329
330 } // namespace nonsfi
331 } // namespace nacl
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698