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

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

Issue 196793023: Add seccomp sandbox for non-SFI NaCl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove one more unittest for shutdown 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 | Annotate | Revision Log
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 #define MAP_STACK 0x20000 // Daisy build environment has old headers.
Mark Seaborn 2014/04/15 23:03:14 "Daisy" -> "cros_daisy (ARM)", perhaps? The term
hamaji 2014/04/16 02:10:48 I changed this to "Chrome OS Daisy (ARM)" to let t
28 #endif
29
30 using sandbox::ErrorCode;
31 using sandbox::SandboxBPF;
32
33 namespace nacl {
34 namespace nonsfi {
35 namespace {
36
37 ErrorCode RestrictFcntlCommandsForNaClNonSfi(SandboxBPF* sb) {
Mark Seaborn 2014/04/15 23:03:14 Now that the Non-SFI sandbox policy is in its own
hamaji 2014/04/16 02:10:48 Done.
38 ErrorCode::ArgType mask_long_type;
39 if (sizeof(long) == 8)
Mark Seaborn 2014/04/15 23:03:14 Nit: when if/elses are non-trivial (e.g. multi-lin
hamaji 2014/04/16 02:10:48 Done, also modified syscall_parameters_restriction
40 mask_long_type = ErrorCode::TP_64BIT;
41 else if (sizeof(long) == 4)
42 mask_long_type = ErrorCode::TP_32BIT;
43 else
44 NOTREACHED();
45 // We allow following cases:
46 // 1. F_SETFD + FD_CLOEXEC: libevent's epoll_init uses this.
47 // 2. F_GETFL: Used by SetNonBlocking in
48 // message_pump_libevent.cc and Channel::ChannelImpl::CreatePipe
49 // in ipc_channel_posix.cc. Note that the latter does not work
50 // with EPERM.
51 // 3. F_SETFL: Used by evutil_make_socket_nonblocking in
52 // libevent and SetNonBlocking. As the latter mix O_NONBLOCK to
53 // the return value of F_GETFL, so we need to allow O_ACCMODE in
54 // addition to O_NONBLOCK.
55 const unsigned long denied_mask = ~(O_ACCMODE | O_NONBLOCK);
56 return sb->Cond(1, ErrorCode::TP_32BIT,
57 ErrorCode::OP_EQUAL, F_SETFD,
58 sb->Cond(2, mask_long_type,
59 ErrorCode::OP_EQUAL, FD_CLOEXEC,
60 ErrorCode(ErrorCode::ERR_ALLOWED),
61 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL)),
62 sb->Cond(1, ErrorCode::TP_32BIT,
63 ErrorCode::OP_EQUAL, F_GETFL,
64 ErrorCode(ErrorCode::ERR_ALLOWED),
65 sb->Cond(1, ErrorCode::TP_32BIT,
66 ErrorCode::OP_EQUAL, F_SETFL,
67 sb->Cond(2, mask_long_type,
68 ErrorCode::OP_HAS_ANY_BITS, denied_mask,
69 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL),
70 ErrorCode(ErrorCode::ERR_ALLOWED)),
71 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL))));
72 }
73
74 ErrorCode RestrictCloneForNaClNonSfi(SandboxBPF* sb) {
75 // We allow clone only for new thread creation.
76 return sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL,
77 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
78 CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS |
79 CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID,
80 ErrorCode(ErrorCode::ERR_ALLOWED),
81 sb->Trap(sandbox::SIGSYSCloneFailure, NULL));
82 }
83
84 ErrorCode RestrictPrctlForNaClNonSfi(SandboxBPF* sb) {
85 // base::PlatformThread::SetName() uses PR_SET_NAME so we return
86 // EPERM for it. Otherwise, we will raise SIGSYS.
87 return sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL,
88 PR_SET_NAME, ErrorCode(EPERM),
89 sb->Trap(sandbox::SIGSYSPrctlFailure, NULL));
90 }
91
92 #if defined(__i386__)
93 ErrorCode RestrictSocketcallForNaClNonSfi(SandboxBPF* sb) {
94 // We only allow socketpair, sendmsg, and recvmsg.
95 return sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL,
96 SYS_SOCKETPAIR,
97 ErrorCode(ErrorCode::ERR_ALLOWED),
98 sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL,
99 SYS_SENDMSG,
100 ErrorCode(ErrorCode::ERR_ALLOWED),
101 sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL,
102 SYS_RECVMSG,
103 ErrorCode(ErrorCode::ERR_ALLOWED),
104 sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL,
105 SYS_SHUTDOWN,
106 ErrorCode(ErrorCode::ERR_ALLOWED),
107 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL)))));
108 }
109 #endif
110
111 ErrorCode RestrictMemoryProtectionForNaClNonSfi(SandboxBPF* sb, int argno) {
112 // TODO(jln, keescook, drewry): Limit the use of mmap/mprotect by
113 // adding some features to linux kernel.
114 const uint32_t denied_mask = ~(PROT_READ | PROT_WRITE | PROT_EXEC);
115 return sb->Cond(argno, ErrorCode::TP_32BIT,
116 ErrorCode::OP_HAS_ANY_BITS,
117 denied_mask,
118 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL),
119 ErrorCode(ErrorCode::ERR_ALLOWED));
120 }
121
122 ErrorCode RestrictMmapForNaClNonSfi(SandboxBPF* sb) {
123 const uint32_t denied_flag_mask = ~(MAP_SHARED | MAP_PRIVATE |
124 MAP_ANONYMOUS | MAP_STACK | MAP_FIXED);
125 // TODO(hamaji): Disallow RWX mmap.
126 return sb->Cond(3, ErrorCode::TP_32BIT,
127 ErrorCode::OP_HAS_ANY_BITS,
128 denied_flag_mask,
129 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL),
130 RestrictMemoryProtectionForNaClNonSfi(sb, 2));
131 }
132
133 ErrorCode RestrictSocketpairForNaClNonSfi(SandboxBPF* sb) {
134 // Only allow AF_UNIX, PF_UNIX. Crash if anything else is seen.
135 COMPILE_ASSERT(AF_UNIX == PF_UNIX, af_unix_pf_unix_different);
136 return sb->Cond(0, ErrorCode::TP_32BIT,
137 ErrorCode::OP_EQUAL, AF_UNIX,
138 ErrorCode(ErrorCode::ERR_ALLOWED),
139 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL));
140 }
141
142 bool IsGracefullyDenied(int sysno) {
143 switch (sysno) {
144 // third_party/libevent uses them, but we can just return -1 from
145 // them as it is just checking getuid() != geteuid() and
146 // getgid() != getegid()
147 #if defined(__i386__) || defined(__arm__)
148 case __NR_getegid32:
149 case __NR_geteuid32:
150 case __NR_getgid32:
151 case __NR_getuid32:
152 #endif
153 #if defined(__x86_64__)
154 case __NR_getegid:
155 case __NR_geteuid:
156 case __NR_getgid:
157 case __NR_getuid:
158 #endif
159 // tcmalloc calls madvise in TCMalloc_SystemRelease.
160 case __NR_madvise:
161 // EPERM instead of SIGSYS as glibc tries to open files in /proc.
162 // TODO(hamaji): Remove this when we switch to newlib.
163 case __NR_open:
164 // For RunSandboxSanityChecks().
165 case __NR_ptrace:
166 // glibc uses this for its pthread implementation. If we return
167 // EPERM for this, glibc will stop using this.
168 // TODO(hamaji): newlib does not use this. Make this SIGTRAP once
169 // we have switched to newlib.
170 case __NR_set_robust_list:
171 // This is obsolete in ARM EABI, but x86 glibc indirectly calls
172 // this in sysconf.
173 #if defined(__i386__) || defined(__x86_64__)
174 case __NR_time:
175 #endif
176 return true;
177
178 default:
179 return false;
180 }
181 }
182
183 void RunSandboxSanityChecks() {
184 errno = 0;
185 // Make a ptrace request with an invalid PID.
186 long ptrace_ret = ptrace(PTRACE_PEEKUSER, -1 /* pid */, NULL, NULL);
187 CHECK_EQ(-1, ptrace_ret);
188 // Without the sandbox on, this ptrace call would ESRCH instead.
189 CHECK_EQ(EPERM, errno);
190 }
191
192 } // namespace
193
194 ErrorCode NaClNonSfiBPFSandboxPolicy::EvaluateSyscall(
195 SandboxBPF* sb, int sysno) const {
196 return EvaluateSyscallImpl(sb, sysno, NULL);
197 }
198
199 ErrorCode NaClNonSfiBPFSandboxPolicy::EvaluateSyscallImpl(
200 SandboxBPF* sb, int sysno, void*) {
201 switch (sysno) {
202 // Allowed syscalls.
203 #if defined(__i386__) || defined(__arm__)
204 case __NR__llseek:
205 #endif
206 #if defined(__x86_64__)
Mark Seaborn 2014/04/15 23:03:14 Nit: maybe use #elif for brevity/readability in al
hamaji 2014/04/16 02:10:48 Done.
207 case __NR_lseek:
208 #endif
209 case __NR_clock_gettime:
210 case __NR_close:
211 case __NR_dup:
212 case __NR_dup2:
213 case __NR_epoll_create:
214 case __NR_epoll_ctl:
215 case __NR_epoll_wait:
216 case __NR_exit:
217 case __NR_exit_group:
218 #if defined(__i386__) || defined(__arm__)
219 case __NR_fstat64:
220 #endif
221 #if defined(__x86_64__)
Mark Seaborn 2014/04/15 23:03:14 Similarly, #elif?
hamaji 2014/04/16 02:10:48 Done.
222 case __NR_fstat:
223 #endif
224 case __NR_futex:
Mark Seaborn 2014/04/15 23:03:14 [For future] We need to restrict this to only allo
hamaji 2014/04/16 02:10:48 Got it. I assume we want TODOs for them? Added som
225 case __NR_gettid:
Mark Seaborn 2014/04/15 23:03:14 [For future] Do we actually need this? Ideally we
hamaji 2014/04/16 02:10:48 FYI: I tried to change this to EPERM and a test wa
226 case __NR_gettimeofday:
227 case __NR_munmap:
228 case __NR_nanosleep:
229 case __NR_pipe:
Mark Seaborn 2014/04/15 23:03:14 [For future] Do we need pipe()? We should aim to
hamaji 2014/04/16 02:10:48 FYI: base::MessagePumpLibevent::Init crashes witho
230 case __NR_pread64:
231 case __NR_read:
232 case __NR_restart_syscall:
233 case __NR_sched_yield:
234 case __NR_sigaltstack:
Mark Seaborn 2014/04/15 23:03:14 We shouldn't be using sigaltstack at the moment.
jln (very slow on Chromium) 2014/04/16 01:36:11 Seconded, I would love to not have it.
hamaji 2014/04/16 02:10:48 Done.
235 case __NR_write:
236 case __NR_pwrite64:
jln (very slow on Chromium) 2014/04/16 01:36:11 I missed this: let's just keep them sorted.
hamaji 2014/04/16 02:10:48 Done.
237 #if defined(__arm__)
238 case __ARM_NR_cacheflush:
239 #endif
240 // NaCl runtime exposes clock_getres to untrusted code.
241 case __NR_clock_getres:
Mark Seaborn 2014/04/15 23:03:14 Nit: put next to clock_gettime?
jln (very slow on Chromium) 2014/04/16 01:36:11 Could you actually re-sort? There are a few which
hamaji 2014/04/16 02:10:48 Done.
hamaji 2014/04/16 02:10:48 Done.
242 // __NR_times needed as clock() is called by CommandBufferHelper, which is
243 // used by NaCl applications that use Pepper's 3D interfaces.
244 // See crbug.com/264856 for details.
245 case __NR_times:
246 return ErrorCode(ErrorCode::ERR_ALLOWED);
247
248 case __NR_clone:
249 return RestrictCloneForNaClNonSfi(sb);
250
251 #if defined(__x86_64__)
252 case __NR_fcntl:
253 #endif
254 #if defined(__i386__) || defined(__arm__)
255 case __NR_fcntl64:
256 #endif
257 return RestrictFcntlCommandsForNaClNonSfi(sb);
258
259 #if defined(__x86_64__)
260 case __NR_mmap:
261 #endif
262 #if defined(__i386__) || defined(__arm__)
263 case __NR_mmap2:
264 #endif
265 return RestrictMmapForNaClNonSfi(sb);
266 case __NR_mprotect:
267 return RestrictMemoryProtectionForNaClNonSfi(sb, 2);
268
269 case __NR_prctl:
270 return RestrictPrctlForNaClNonSfi(sb);
271
272 #if defined(__i386__)
273 case __NR_socketcall:
274 return RestrictSocketcallForNaClNonSfi(sb);
275 #endif
276 #if defined(__x86_64__) || defined(__arm__)
277 case __NR_recvmsg:
278 case __NR_sendmsg:
279 case __NR_shutdown:
280 return ErrorCode(ErrorCode::ERR_ALLOWED);
281 case __NR_socketpair:
282 return RestrictSocketpairForNaClNonSfi(sb);
283 #endif
284
285 case __NR_brk:
286 // The behavior of brk on Linux is different from other system
287 // calls. It does not return errno but the current break on
288 // failure. glibc thinks brk failed the return value of brk
Mark Seaborn 2014/04/15 23:03:14 Missing "if": "brk failed if"
hamaji 2014/04/16 02:10:48 Done.
289 // is lesser than the requested address (i.e., brk(addr) < addr)
Mark Seaborn 2014/04/15 23:03:14 "less than". Also add "." at end of line.
hamaji 2014/04/16 02:10:48 Done.
290 // So, glibc thinks brk succeeded if we return -EPERM and we
291 // need to return zero instead.
292 return ErrorCode(0);
293
294 default:
295 if (IsGracefullyDenied(sysno))
296 return ErrorCode(EPERM);
297 return sb->Trap(sandbox::CrashSIGSYS_Handler, NULL);
298 }
299 }
300
301 bool InitializeBPFSandbox() {
302 bool sandbox_is_initialized = content::InitializeSandbox(
303 scoped_ptr<sandbox::SandboxBPFPolicy>(
304 new nacl::nonsfi::NaClNonSfiBPFSandboxPolicy()));
305 if (!sandbox_is_initialized)
306 return false;
307 RunSandboxSanityChecks();
308 return true;
309 }
310
311 } // namespace nonsfi
312 } // namespace nacl
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698