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
3 // found in the LICENSE file.
5 #include <asm/unistd.h>
9 #include <linux/audit.h>
10 #include <linux/filter.h>
13 #include <sys/prctl.h>
15 #include <sys/types.h>
21 #include "base/command_line.h"
22 #include "base/logging.h"
23 #include "content/common/sandbox_linux.h"
24 #include "content/common/sandbox_seccomp_bpf_linux.h"
25 #include "content/public/common/content_switches.h"
26 #include "sandbox/linux/services/broker_process.h"
28 // These are the only architectures supported for now.
29 #if defined(__i386__) || defined(__x86_64__) || \
30 (defined(__arm__) && (defined(__thumb__) || defined(__ARM_EABI__)))
31 #define SECCOMP_BPF_SANDBOX
34 #if defined(SECCOMP_BPF_SANDBOX)
35 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
36 #include "sandbox/linux/services/linux_syscalls.h"
38 using playground2::arch_seccomp_data
;
39 using playground2::ErrorCode
;
40 using playground2::Sandbox
;
41 using sandbox::BrokerProcess
;
45 void StartSandboxWithPolicy(Sandbox::EvaluateSyscall syscall_policy
,
46 BrokerProcess
* broker_process
);
48 inline bool IsChromeOS() {
49 #if defined(OS_CHROMEOS)
56 inline bool IsArchitectureX86_64() {
57 #if defined(__x86_64__)
64 inline bool IsArchitectureI386() {
72 inline bool IsArchitectureArm() {
80 intptr_t CrashSIGSYS_Handler(const struct arch_seccomp_data
& args
, void* aux
) {
81 int syscall
= args
.nr
;
84 // Encode 8-bits of the 1st two arguments too, so we can discern which socket
85 // type, which fcntl, ... etc., without being likely to hit a mapped
87 // Do not encode more bits here without thinking about increasing the
88 // likelihood of collision with mapped pages.
89 syscall
|= ((args
.args
[0] & 0xffUL
) << 12);
90 syscall
|= ((args
.args
[1] & 0xffUL
) << 20);
91 // Purposefully dereference the syscall as an address so it'll show up very
92 // clearly and easily in crash dumps.
93 volatile char* addr
= reinterpret_cast<volatile char*>(syscall
);
95 // In case we hit a mapped address, hit the null page with just the syscall,
98 addr
= reinterpret_cast<volatile char*>(syscall
);
104 bool IsAcceleratedVideoDecodeEnabled() {
105 // Accelerated video decode is currently enabled on Chrome OS,
106 // but not on Linux: crbug.com/137247.
107 bool is_enabled
= IsChromeOS();
109 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
110 is_enabled
= is_enabled
&&
111 !command_line
.HasSwitch(switches::kDisableAcceleratedVideoDecode
);
116 intptr_t GpuOpenSIGSYS_Handler(const struct arch_seccomp_data
& args
,
117 void* aux_broker_process
) {
118 RAW_CHECK(aux_broker_process
);
119 BrokerProcess
* broker_process
=
120 static_cast<BrokerProcess
*>(aux_broker_process
);
123 return broker_process
->Open(reinterpret_cast<const char*>(args
.args
[0]),
124 static_cast<int>(args
.args
[1]));
126 // Allow using openat() as open().
127 if (static_cast<int>(args
.args
[0]) == AT_FDCWD
) {
129 broker_process
->Open(reinterpret_cast<const char*>(args
.args
[1]),
130 static_cast<int>(args
.args
[2]));
140 // The functions below cover all existing i386, x86_64, and ARM system calls;
141 // excluding syscalls made obsolete in ARM EABI.
142 // The implicitly defined sets form a partition of the sets of
145 // TODO(jln) we need to restrict the first parameter!
146 bool IsKill(int sysno
) {
157 bool IsAllowedGettime(int sysno
) {
159 case __NR_clock_gettime
:
160 case __NR_gettimeofday
:
161 #if defined(__i386__) || defined(__x86_64__)
165 case __NR_adjtimex
: // Privileged.
166 case __NR_clock_adjtime
: // Privileged.
167 case __NR_clock_getres
: // Could be allowed.
168 case __NR_clock_nanosleep
: // Could be allowed.
169 case __NR_clock_settime
: // Privileged.
170 #if defined(__i386__)
171 case __NR_ftime
: // Obsolete.
173 case __NR_settimeofday
: // Privileged.
174 #if defined(__i386__)
182 bool IsCurrentDirectory(int sysno
) {
193 bool IsUmask(int sysno
) {
202 // System calls that directly access the file system. They might acquire
203 // a new file descriptor or otherwise perform an operation directly
205 // Both EPERM and ENOENT are valid errno unless otherwise noted in comment.
206 bool IsFileSystem(int sysno
) {
208 case __NR_access
: // EPERM not a valid errno.
211 #if defined(__i386__) || defined(__arm__)
216 case __NR_faccessat
: // EPERM not a valid errno.
218 case __NR_fchownat
: // Should be called chownat ?
219 #if defined(__x86_64__)
220 case __NR_newfstatat
: // fstatat(). EPERM not a valid errno.
221 #elif defined(__i386__) || defined(__arm__)
224 case __NR_futimesat
: // Should be called utimesat ?
226 #if defined(__i386__) || defined(__arm__)
231 case __NR_lookup_dcookie
: // ENOENT not a valid errno.
232 case __NR_lstat
: // EPERM not a valid errno.
233 #if defined(__i386__)
236 #if defined(__i386__) || defined(__arm__)
245 case __NR_readlink
: // EPERM not a valid errno.
246 case __NR_readlinkat
:
250 case __NR_stat
: // EPERM not a valid errno.
251 #if defined(__i386__)
254 #if defined(__i386__) || defined(__arm__)
257 case __NR_statfs
: // EPERM not a valid errno.
258 #if defined(__i386__) || defined(__arm__)
264 #if defined(__i386__) || defined(__arm__)
265 case __NR_truncate64
:
269 case __NR_uselib
: // Neither EPERM, nor ENOENT are valid errno.
270 case __NR_ustat
: // Same as above. Deprecated.
271 #if defined(__i386__) || defined(__x86_64__)
274 case __NR_utimensat
: // New.
282 bool IsAllowedFileSystemAccessViaFd(int sysno
) {
285 #if defined(__i386__) || defined(__arm__)
289 // TODO(jln): these should be denied gracefully as well (moved below).
290 #if defined(__i386__) || defined(__x86_64__)
291 case __NR_fadvise64
: // EPERM not a valid errno.
293 #if defined(__i386__)
294 case __NR_fadvise64_64
:
297 case __NR_arm_fadvise64_64
:
299 case __NR_fdatasync
: // EPERM not a valid errno.
300 case __NR_flock
: // EPERM not a valid errno.
301 case __NR_fstatfs
: // Give information about the whole filesystem.
302 #if defined(__i386__) || defined(__arm__)
305 case __NR_fsync
: // EPERM not a valid errno.
306 #if defined(__i386__)
309 #if defined(__i386__) || defined(__x86_64__)
310 case __NR_sync_file_range
: // EPERM not a valid errno.
311 #elif defined(__arm__)
312 case __NR_arm_sync_file_range
: // EPERM not a valid errno.
319 // EPERM is a good errno for any of these.
320 bool IsDeniedFileSystemAccessViaFd(int sysno
) {
326 #if defined(__i386__) || defined(__arm__)
328 case __NR_ftruncate64
:
330 case __NR_getdents
: // EPERM not a valid errno.
331 case __NR_getdents64
: // EPERM not a valid errno.
332 #if defined(__i386__)
341 bool IsGetSimpleId(int sysno
) {
355 #if defined(__i386__) || defined(__arm__)
359 case __NR_getgroups32
:
360 case __NR_getresgid32
:
361 case __NR_getresuid32
:
370 bool IsProcessPrivilegeChange(int sysno
) {
373 #if defined(__i386__) || defined(__x86_64__)
374 case __NR_ioperm
: // Intel privilege.
375 case __NR_iopl
: // Intel privilege.
386 #if defined(__i386__) || defined(__arm__)
387 case __NR_setfsgid32
:
388 case __NR_setfsuid32
:
390 case __NR_setgroups32
:
391 case __NR_setregid32
:
392 case __NR_setresgid32
:
393 case __NR_setresuid32
:
394 case __NR_setreuid32
:
403 bool IsProcessGroupOrSession(int sysno
) {
415 bool IsAllowedSignalHandling(int sysno
) {
417 case __NR_rt_sigaction
:
418 case __NR_rt_sigprocmask
:
419 case __NR_rt_sigreturn
:
420 #if defined(__i386__) || defined(__arm__)
422 case __NR_sigprocmask
:
426 case __NR_rt_sigpending
:
427 case __NR_rt_sigqueueinfo
:
428 case __NR_rt_sigsuspend
:
429 case __NR_rt_sigtimedwait
:
430 case __NR_rt_tgsigqueueinfo
:
431 case __NR_sigaltstack
:
434 #if defined(__i386__) || defined(__arm__)
435 case __NR_sigpending
:
436 case __NR_sigsuspend
:
438 #if defined(__i386__)
440 case __NR_sgetmask
: // Obsolete.
448 bool IsOperationOnFd(int sysno
) {
454 case __NR_fcntl
: // TODO(jln): we may want to restrict arguments.
455 #if defined(__i386__) || defined(__arm__)
458 #if defined(__x86_64__) || defined(__arm__)
467 bool IsKernelInternalApi(int sysno
) {
469 case __NR_restart_syscall
:
471 case __ARM_NR_cmpxchg
:
479 // This should be thought through in conjunction with IsFutex().
480 bool IsAllowedProcessStartOrDeath(int sysno
) {
482 case __NR_clone
: // TODO(jln): restrict flags.
484 case __NR_exit_group
:
487 #if defined(__i386__)
491 case __NR_setns
: // Privileged.
493 #if defined(__i386__) || defined(__x86_64__)
494 case __NR_get_thread_area
:
495 case __NR_set_thread_area
:
497 case __NR_set_tid_address
:
505 // It's difficult to restrict those, but there is attack surface here.
506 bool IsFutex(int sysno
) {
509 case __NR_get_robust_list
:
510 case __NR_set_robust_list
:
517 bool IsAllowedEpoll(int sysno
) {
519 case __NR_epoll_create
:
520 case __NR_epoll_create1
:
522 case __NR_epoll_wait
:
525 #if defined(__x86_64__)
526 case __NR_epoll_ctl_old
:
528 case __NR_epoll_pwait
:
529 #if defined(__x86_64__)
530 case __NR_epoll_wait_old
:
536 bool IsAllowedGetOrModifySocket(int sysno
) {
540 #if defined(__x86_64__) || defined(__arm__)
541 case __NR_socketpair
: // We will want to inspect its argument.
549 bool IsDeniedGetOrModifySocket(int sysno
) {
551 #if defined(__x86_64__) || defined(__arm__)
565 #if defined(__i386__)
566 // Big multiplexing system call for sockets.
567 bool IsSocketCall(int sysno
) {
569 case __NR_socketcall
:
577 #if defined(__x86_64__) || defined(__arm__)
578 bool IsNetworkSocketInformation(int sysno
) {
580 case __NR_getpeername
:
581 case __NR_getsockname
:
582 case __NR_getsockopt
:
583 case __NR_setsockopt
:
591 bool IsAllowedAddressSpaceAccess(int sysno
) {
596 #if defined(__i386__) || defined(__x86_64__)
597 case __NR_mmap
: // TODO(jln): to restrict flags.
599 #if defined(__i386__) || defined(__arm__)
608 #if defined(__i386__) || defined(__x86_64__)
609 case __NR_modify_ldt
:
613 case __NR_munlockall
:
615 case __NR_remap_file_pages
:
616 #if defined(__i386__)
625 bool IsAllowedGeneralIo(int sysno
) {
628 #if defined(__i386__) || defined(__arm__)
639 #if defined(__x86_64__) || defined(__arm__)
640 case __NR_recvfrom
: // Could specify source.
641 case __NR_recvmsg
: // Could specify source.
643 #if defined(__i386__) || defined(__x86_64__)
646 #if defined(__i386__) || defined(__arm__)
647 case __NR__newselect
:
652 #if defined(__x86_64__) || defined(__arm__)
653 case __NR_sendmsg
: // Could specify destination.
654 case __NR_sendto
: // Could specify destination.
659 case __NR_ioctl
: // Can be very powerful.
664 case __NR_recvmmsg
: // Could specify source.
666 #if defined(__i386__) || defined(__arm__)
667 case __NR_sendfile64
:
669 case __NR_sendmmsg
: // Could specify destination.
678 bool IsAllowedPrctl(int sysno
) {
683 #if defined(__x86_64__)
684 case __NR_arch_prctl
:
690 bool IsAllowedBasicScheduler(int sysno
) {
692 case __NR_sched_yield
:
696 case __NR_getpriority
:
697 #if defined(__i386__) || defined(__arm__)
700 case __NR_setpriority
:
706 bool IsAdminOperation(int sysno
) {
708 #if defined(__i386__) || defined(__arm__)
711 case __NR_kexec_load
:
713 case __NR_setdomainname
:
714 case __NR_sethostname
:
722 bool IsKernelModule(int sysno
) {
724 #if defined(__i386__) || defined(__x86_64__)
725 case __NR_create_module
:
726 case __NR_get_kernel_syms
: // Should ENOSYS.
727 case __NR_query_module
:
729 case __NR_delete_module
:
730 case __NR_init_module
:
737 bool IsGlobalFSViewChange(int sysno
) {
739 case __NR_pivot_root
:
748 bool IsFsControl(int sysno
) {
751 case __NR_nfsservctl
:
755 #if defined(__i386__)
765 bool IsNuma(int sysno
) {
767 case __NR_get_mempolicy
:
770 #if defined(__i386__) || defined(__x86_64__)
771 case __NR_migrate_pages
:
773 case __NR_move_pages
:
774 case __NR_set_mempolicy
:
781 bool IsMessageQueue(int sysno
) {
783 case __NR_mq_getsetattr
:
786 case __NR_mq_timedreceive
:
787 case __NR_mq_timedsend
:
795 bool IsGlobalProcessEnvironment(int sysno
) {
797 case __NR_acct
: // Privileged.
798 #if defined(__i386__) || defined(__x86_64__)
801 #if defined(__i386__) || defined(__arm__)
802 case __NR_ugetrlimit
:
804 #if defined(__i386__)
808 case __NR_personality
: // Can change its personality as well.
809 case __NR_prlimit64
: // Like setrlimit / getrlimit.
818 bool IsDebug(int sysno
) {
821 case __NR_process_vm_readv
:
822 case __NR_process_vm_writev
:
823 #if defined(__i386__) || defined(__x86_64__)
832 bool IsGlobalSystemStatus(int sysno
) {
838 #if defined(__i386__)
840 case __NR_oldolduname
:
848 bool IsEventFd(int sysno
) {
858 // Asynchronous I/O API.
859 bool IsAsyncIo(int sysno
) {
862 case __NR_io_destroy
:
863 case __NR_io_getevents
:
872 bool IsKeyManagement(int sysno
) {
876 case __NR_request_key
:
883 #if defined(__x86_64__) || defined(__arm__)
884 bool IsSystemVSemaphores(int sysno
) {
889 case __NR_semtimedop
:
897 #if defined(__x86_64__) || defined(__arm__)
898 // These give a lot of ambient authority and bypass the setuid sandbox.
899 bool IsSystemVSharedMemory(int sysno
) {
912 #if defined(__x86_64__) || defined(__arm__)
913 bool IsSystemVMessageQueue(int sysno
) {
926 #if defined(__i386__)
927 // Big system V multiplexing system call.
928 bool IsSystemVIpc(int sysno
) {
938 bool IsAdvancedScheduler(int sysno
) {
940 case __NR_ioprio_get
: // IO scheduler.
941 case __NR_ioprio_set
:
942 case __NR_sched_get_priority_max
:
943 case __NR_sched_get_priority_min
:
944 case __NR_sched_getaffinity
:
945 case __NR_sched_getparam
:
946 case __NR_sched_getscheduler
:
947 case __NR_sched_rr_get_interval
:
948 case __NR_sched_setaffinity
:
949 case __NR_sched_setparam
:
950 case __NR_sched_setscheduler
:
957 bool IsInotify(int sysno
) {
959 case __NR_inotify_add_watch
:
960 case __NR_inotify_init
:
961 case __NR_inotify_init1
:
962 case __NR_inotify_rm_watch
:
969 bool IsFaNotify(int sysno
) {
971 case __NR_fanotify_init
:
972 case __NR_fanotify_mark
:
979 bool IsTimer(int sysno
) {
982 #if defined(__i386__) || defined(__x86_64__)
992 bool IsAdvancedTimer(int sysno
) {
994 case __NR_timer_create
:
995 case __NR_timer_delete
:
996 case __NR_timer_getoverrun
:
997 case __NR_timer_gettime
:
998 case __NR_timer_settime
:
999 case __NR_timerfd_create
:
1000 case __NR_timerfd_gettime
:
1001 case __NR_timerfd_settime
:
1008 bool IsExtendedAttributes(int sysno
) {
1010 case __NR_fgetxattr
:
1011 case __NR_flistxattr
:
1012 case __NR_fremovexattr
:
1013 case __NR_fsetxattr
:
1015 case __NR_lgetxattr
:
1016 case __NR_listxattr
:
1017 case __NR_llistxattr
:
1018 case __NR_lremovexattr
:
1019 case __NR_lsetxattr
:
1020 case __NR_removexattr
:
1028 // Various system calls that need to be researched.
1029 // TODO(jln): classify this better.
1030 bool IsMisc(int sysno
) {
1032 case __NR_name_to_handle_at
:
1033 case __NR_open_by_handle_at
:
1034 case __NR_perf_event_open
:
1037 // The system calls below are not implemented.
1038 #if defined(__i386__) || defined(__x86_64__)
1039 case __NR_afs_syscall
:
1041 #if defined(__i386__)
1044 #if defined(__i386__) || defined(__x86_64__)
1047 #if defined(__i386__)
1055 #if defined(__i386__) || defined(__x86_64__)
1058 #if defined(__x86_64__)
1061 #if defined(__i386__)
1064 #if defined(__x86_64__)
1074 #if defined(__arm__)
1075 bool IsArmPciConfig(int sysno
) {
1077 case __NR_pciconfig_iobase
:
1078 case __NR_pciconfig_read
:
1079 case __NR_pciconfig_write
:
1086 bool IsArmPrivate(int sysno
) {
1088 case __ARM_NR_breakpoint
:
1089 case __ARM_NR_cacheflush
:
1090 case __ARM_NR_set_tls
:
1091 case __ARM_NR_usr26
:
1092 case __ARM_NR_usr32
:
1098 #endif // defined(__arm__)
1100 // End of the system call sets section.
1102 bool IsBaselinePolicyAllowed(int sysno
) {
1103 if (IsAllowedAddressSpaceAccess(sysno
) ||
1104 IsAllowedBasicScheduler(sysno
) ||
1105 IsAllowedEpoll(sysno
) ||
1106 IsAllowedFileSystemAccessViaFd(sysno
) ||
1107 IsAllowedGeneralIo(sysno
) ||
1108 IsAllowedGetOrModifySocket(sysno
) ||
1109 IsAllowedGettime(sysno
) ||
1110 IsAllowedPrctl(sysno
) ||
1111 IsAllowedProcessStartOrDeath(sysno
) ||
1112 IsAllowedSignalHandling(sysno
) ||
1114 IsGetSimpleId(sysno
) ||
1115 IsKernelInternalApi(sysno
) ||
1116 #if defined(__arm__)
1117 IsArmPrivate(sysno
) ||
1120 IsOperationOnFd(sysno
)) {
1127 // System calls that will trigger the crashing SIGSYS handler.
1128 bool IsBaselinePolicyWatched(int sysno
) {
1129 if (IsAdminOperation(sysno
) ||
1130 IsAdvancedScheduler(sysno
) ||
1131 IsAdvancedTimer(sysno
) ||
1135 IsExtendedAttributes(sysno
) ||
1136 IsFaNotify(sysno
) ||
1137 IsFsControl(sysno
) ||
1138 IsGlobalFSViewChange(sysno
) ||
1139 IsGlobalProcessEnvironment(sysno
) ||
1140 IsGlobalSystemStatus(sysno
) ||
1142 IsKernelModule(sysno
) ||
1143 IsKeyManagement(sysno
) ||
1144 IsMessageQueue(sysno
) ||
1146 #if defined(__x86_64__)
1147 IsNetworkSocketInformation(sysno
) ||
1150 IsProcessGroupOrSession(sysno
) ||
1151 IsProcessPrivilegeChange(sysno
) ||
1152 #if defined(__i386__)
1153 IsSocketCall(sysno
) || // We'll need to handle this properly to build
1156 #if defined(__x86_64__) || defined(__arm__)
1157 IsSystemVMessageQueue(sysno
) ||
1158 IsSystemVSemaphores(sysno
) ||
1159 IsSystemVSharedMemory(sysno
) ||
1160 #elif defined(__i386__)
1161 IsSystemVIpc(sysno
) ||
1163 #if defined(__arm__)
1164 IsArmPciConfig(sysno
) ||
1173 ErrorCode
BaselinePolicy(int sysno
) {
1174 if (IsBaselinePolicyAllowed(sysno
)) {
1175 return ErrorCode(ErrorCode::ERR_ALLOWED
);
1178 #if defined(__i386__)
1179 // socketcall(2) should be tightened.
1180 if (IsSocketCall(sysno
)) {
1181 return ErrorCode(ErrorCode::ERR_ALLOWED
);
1185 // TODO(jln): some system calls in those sets are not supposed to
1186 // return ENOENT. Return the appropriate error.
1187 if (IsFileSystem(sysno
) || IsCurrentDirectory(sysno
)) {
1188 return ErrorCode(ENOENT
);
1191 if (IsUmask(sysno
) || IsDeniedFileSystemAccessViaFd(sysno
) ||
1192 IsDeniedGetOrModifySocket(sysno
)) {
1193 return ErrorCode(EPERM
);
1196 if (IsBaselinePolicyWatched(sysno
)) {
1197 // Previously unseen syscalls. TODO(jln): some of these should
1198 // be denied gracefully right away.
1199 return Sandbox::Trap(CrashSIGSYS_Handler
, NULL
);
1201 // In any other case crash the program with our SIGSYS handler
1202 return Sandbox::Trap(CrashSIGSYS_Handler
, NULL
);
1205 // x86_64/i386 for now. Needs to be adapted and tested for ARM.
1206 ErrorCode
GpuProcessPolicy(int sysno
, void *broker_process
) {
1209 #if defined(ADDRESS_SANITIZER)
1210 // Allow to call sched_getaffinity under AddressSanitizer.
1211 case __NR_sched_getaffinity
:
1213 return ErrorCode(ErrorCode::ERR_ALLOWED
);
1216 return Sandbox::Trap(GpuOpenSIGSYS_Handler
, broker_process
);
1218 if (IsEventFd(sysno
))
1219 return ErrorCode(ErrorCode::ERR_ALLOWED
);
1221 // Default on the baseline policy.
1222 return BaselinePolicy(sysno
);
1226 // x86_64/i386 for now. Needs to be adapted and tested for ARM.
1227 // A GPU broker policy is the same as a GPU policy with open and
1229 ErrorCode
GpuBrokerProcessPolicy(int sysno
, void*) {
1233 return ErrorCode(ErrorCode::ERR_ALLOWED
);
1235 return GpuProcessPolicy(sysno
, NULL
);
1239 ErrorCode
RendererOrWorkerProcessPolicy(int sysno
, void *) {
1241 case __NR_ioctl
: // TODO(jln) investigate legitimate use in the renderer
1242 // and see if alternatives can be used.
1243 case __NR_fdatasync
:
1245 #if defined(__i386__) || defined(__x86_64__)
1246 case __NR_getrlimit
:
1248 case __NR_mremap
: // See crbug.com/149834.
1251 #if defined(ADDRESS_SANITIZER)
1252 // Allow to call sched_getaffinity() under AddressSanitizer.
1253 case __NR_sched_getaffinity
:
1255 case __NR_sched_get_priority_max
:
1256 case __NR_sched_get_priority_min
:
1257 case __NR_sched_getparam
:
1258 case __NR_sched_getscheduler
:
1259 case __NR_sched_setscheduler
:
1260 case __NR_setpriority
:
1264 return ErrorCode(ErrorCode::ERR_ALLOWED
);
1265 case __NR_prlimit64
:
1266 return ErrorCode(EPERM
); // See crbug.com/160157.
1268 // These need further tightening.
1269 #if defined(__x86_64__) || defined(__arm__)
1270 if (IsSystemVSharedMemory(sysno
))
1271 return ErrorCode(ErrorCode::ERR_ALLOWED
);
1273 #if defined(__i386__)
1274 if (IsSystemVIpc(sysno
))
1275 return ErrorCode(ErrorCode::ERR_ALLOWED
);
1278 // Default on the baseline policy.
1279 return BaselinePolicy(sysno
);
1283 ErrorCode
FlashProcessPolicy(int sysno
, void *) {
1285 case __NR_sched_getaffinity
:
1286 case __NR_sched_setscheduler
:
1288 return ErrorCode(ErrorCode::ERR_ALLOWED
);
1290 return ErrorCode(ENOTTY
); // Flash Access.
1292 // These need further tightening.
1293 #if defined(__x86_64__) || defined(__arm__)
1294 if (IsSystemVSharedMemory(sysno
))
1295 return ErrorCode(ErrorCode::ERR_ALLOWED
);
1297 #if defined(__i386__)
1298 if (IsSystemVIpc(sysno
))
1299 return ErrorCode(ErrorCode::ERR_ALLOWED
);
1302 // Default on the baseline policy.
1303 return BaselinePolicy(sysno
);
1307 ErrorCode
BlacklistDebugAndNumaPolicy(int sysno
, void *) {
1308 if (!Sandbox::IsValidSyscallNumber(sysno
)) {
1309 // TODO(jln) we should not have to do that in a trivial policy.
1310 return ErrorCode(ENOSYS
);
1313 if (IsDebug(sysno
) || IsNuma(sysno
))
1314 return Sandbox::Trap(CrashSIGSYS_Handler
, NULL
);
1316 return ErrorCode(ErrorCode::ERR_ALLOWED
);
1319 // Allow all syscalls.
1320 // This will still deny x32 or IA32 calls in 64 bits mode or
1321 // 64 bits system calls in compatibility mode.
1322 ErrorCode
AllowAllPolicy(int sysno
, void *) {
1323 if (!Sandbox::IsValidSyscallNumber(sysno
)) {
1324 // TODO(jln) we should not have to do that in a trivial policy.
1325 return ErrorCode(ENOSYS
);
1327 return ErrorCode(ErrorCode::ERR_ALLOWED
);
1331 bool EnableGpuBrokerPolicyCallBack() {
1332 StartSandboxWithPolicy(GpuBrokerProcessPolicy
, NULL
);
1336 // Start a broker process to handle open() inside the sandbox.
1337 void InitGpuBrokerProcess(BrokerProcess
** broker_process
) {
1338 static const char kDriRcPath
[] = "/etc/drirc";
1339 static const char kDriCard0Path
[] = "/dev/dri/card0";
1341 CHECK(broker_process
);
1342 CHECK(*broker_process
== NULL
);
1344 std::vector
<std::string
> read_whitelist
;
1345 read_whitelist
.push_back(kDriCard0Path
);
1346 read_whitelist
.push_back(kDriRcPath
);
1347 std::vector
<std::string
> write_whitelist
;
1348 write_whitelist
.push_back(kDriCard0Path
);
1350 *broker_process
= new BrokerProcess(read_whitelist
, write_whitelist
);
1351 // Initialize the broker process and give it a sandbox call back.
1352 CHECK((*broker_process
)->Init(EnableGpuBrokerPolicyCallBack
));
1355 // Warms up/preloads resources needed by the policies.
1356 // Eventually start a broker process and return it in broker_process.
1357 void WarmupPolicy(Sandbox::EvaluateSyscall policy
,
1358 BrokerProcess
** broker_process
) {
1359 if (policy
== GpuProcessPolicy
) {
1360 if (IsArchitectureX86_64() || IsArchitectureI386()) {
1361 // Create a new broker process.
1362 InitGpuBrokerProcess(broker_process
);
1364 // Accelerated video decode dlopen()'s a shared object
1365 // inside the sandbox, so preload it now.
1366 if (IsAcceleratedVideoDecodeEnabled()) {
1367 const char* I965DrvVideoPath
= NULL
;
1369 if (IsArchitectureX86_64()) {
1370 I965DrvVideoPath
= "/usr/lib64/va/drivers/i965_drv_video.so";
1371 } else if (IsArchitectureI386()) {
1372 I965DrvVideoPath
= "/usr/lib/va/drivers/i965_drv_video.so";
1375 dlopen(I965DrvVideoPath
, RTLD_NOW
|RTLD_GLOBAL
|RTLD_NODELETE
);
1381 Sandbox::EvaluateSyscall
GetProcessSyscallPolicy(
1382 const CommandLine
& command_line
,
1383 const std::string
& process_type
) {
1384 if (process_type
== switches::kGpuProcess
) {
1385 // On Chrome OS, --enable-gpu-sandbox enables the more restrictive policy.
1386 // However, we don't yet enable the more restrictive GPU process policy
1388 if (IsArchitectureArm() ||
1389 (IsChromeOS() && !command_line
.HasSwitch(switches::kEnableGpuSandbox
)))
1390 return BlacklistDebugAndNumaPolicy
;
1392 return GpuProcessPolicy
;
1395 if (process_type
== switches::kPpapiPluginProcess
) {
1396 // TODO(jln): figure out what to do with non-Flash PPAPI
1397 // out-of-process plug-ins.
1398 return FlashProcessPolicy
;
1401 if (process_type
== switches::kRendererProcess
||
1402 process_type
== switches::kWorkerProcess
) {
1403 return RendererOrWorkerProcessPolicy
;
1406 if (process_type
== switches::kUtilityProcess
) {
1407 return BlacklistDebugAndNumaPolicy
;
1411 // This will be our default if we need one.
1412 return AllowAllPolicy
;
1415 // broker_process can be NULL if there is no need for one.
1416 void StartSandboxWithPolicy(Sandbox::EvaluateSyscall syscall_policy
,
1417 BrokerProcess
* broker_process
) {
1419 Sandbox::SetSandboxPolicy(syscall_policy
, broker_process
);
1420 Sandbox::StartSandbox();
1423 // Initialize the seccomp-bpf sandbox.
1424 bool StartBpfSandbox(const CommandLine
& command_line
,
1425 const std::string
& process_type
) {
1426 Sandbox::EvaluateSyscall syscall_policy
=
1427 GetProcessSyscallPolicy(command_line
, process_type
);
1429 BrokerProcess
* broker_process
= NULL
;
1430 // Warm up resources needed by the policy we're about to enable and
1431 // eventually start a broker process.
1432 WarmupPolicy(syscall_policy
, &broker_process
);
1434 StartSandboxWithPolicy(syscall_policy
, broker_process
);
1441 #endif // SECCOMP_BPF_SANDBOX
1445 // Is seccomp BPF globally enabled?
1446 bool SandboxSeccompBpf::IsSeccompBpfDesired() {
1447 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
1448 if (!command_line
.HasSwitch(switches::kNoSandbox
) &&
1449 !command_line
.HasSwitch(switches::kDisableSeccompFilterSandbox
)) {
1456 bool SandboxSeccompBpf::ShouldEnableSeccompBpf(
1457 const std::string
& process_type
) {
1458 #if defined(SECCOMP_BPF_SANDBOX)
1459 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
1460 if (process_type
== switches::kGpuProcess
)
1461 return !command_line
.HasSwitch(switches::kDisableGpuSandbox
);
1464 #endif // SECCOMP_BPF_SANDBOX
1468 bool SandboxSeccompBpf::SupportsSandbox() {
1469 #if defined(SECCOMP_BPF_SANDBOX)
1470 // TODO(jln): pass the saved proc_fd_ from the LinuxSandbox singleton
1472 Sandbox::SandboxStatus bpf_sandbox_status
=
1473 Sandbox::SupportsSeccompSandbox(-1);
1474 // Kernel support is what we are interested in here. Other status
1475 // such as STATUS_UNAVAILABLE (has threads) still indicate kernel support.
1476 // We make this a negative check, since if there is a bug, we would rather
1477 // "fail closed" (expect a sandbox to be available and try to start it).
1478 if (bpf_sandbox_status
!= Sandbox::STATUS_UNSUPPORTED
) {
1485 bool SandboxSeccompBpf::StartSandbox(const std::string
& process_type
) {
1486 #if defined(SECCOMP_BPF_SANDBOX)
1487 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
1489 if (IsSeccompBpfDesired() && // Global switches policy.
1490 ShouldEnableSeccompBpf(process_type
) && // Process-specific policy.
1491 SupportsSandbox()) {
1492 // If the kernel supports the sandbox, and if the command line says we
1493 // should enable it, enable it or die.
1494 bool started_sandbox
= StartBpfSandbox(command_line
, process_type
);
1495 CHECK(started_sandbox
);
1502 } // namespace content