1f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Use of this source code is governed by a BSD-style license that can be
3f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// found in the LICENSE file.
4f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
5f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko#ifndef SANDBOX_LINUX_SECCOMP_BPF_HELPERS_SYSCALL_PARAMETERS_RESTRICTIONS_H_
6f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko#define SANDBOX_LINUX_SECCOMP_BPF_HELPERS_SYSCALL_PARAMETERS_RESTRICTIONS_H_
7f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
8f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko#include <unistd.h>
9f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
10f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko#include "build/build_config.h"
11f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko#include "sandbox/linux/bpf_dsl/bpf_dsl_forward.h"
12f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko#include "sandbox/sandbox_export.h"
13f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
14f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// These are helpers to build seccomp-bpf policies, i.e. policies for a
15f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// sandbox that reduces the Linux kernel's attack surface. They return a
16f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// bpf_dsl::ResultExpr suitable to restrict certain system call parameters.
17f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
18f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenkonamespace sandbox {
19f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
20f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Allow clone(2) for threads.
21f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Reject fork(2) attempts with EPERM.
22f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Don't restrict on ASAN.
23f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Crash if anything else is attempted.
24f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex VakulenkoSANDBOX_EXPORT bpf_dsl::ResultExpr RestrictCloneToThreadsAndEPERMFork();
25f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
26f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Allow PR_SET_NAME, PR_SET_DUMPABLE, PR_GET_DUMPABLE.
27f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Crash if anything else is attempted.
28f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex VakulenkoSANDBOX_EXPORT bpf_dsl::ResultExpr RestrictPrctl();
29f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
30f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Allow TCGETS and FIONREAD.
31f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Crash if anything else is attempted.
32f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex VakulenkoSANDBOX_EXPORT bpf_dsl::ResultExpr RestrictIoctl();
33f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
34f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Restrict the flags argument in mmap(2).
35f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Only allow: MAP_SHARED | MAP_PRIVATE | MAP_ANONYMOUS |
36f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// MAP_STACK | MAP_NORESERVE | MAP_FIXED | MAP_DENYWRITE.
37f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Crash if any other flag is used.
38f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex VakulenkoSANDBOX_EXPORT bpf_dsl::ResultExpr RestrictMmapFlags();
39f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
40f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Restrict the prot argument in mprotect(2).
41f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Only allow: PROT_READ | PROT_WRITE | PROT_EXEC.
42f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex VakulenkoSANDBOX_EXPORT bpf_dsl::ResultExpr RestrictMprotectFlags();
43f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
44f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Restrict fcntl(2) cmd argument to:
45f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// We allow F_GETFL, F_SETFL, F_GETFD, F_SETFD, F_DUPFD, F_DUPFD_CLOEXEC,
46f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// F_SETLK, F_SETLKW and F_GETLK.
47f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Also, in F_SETFL, restrict the allowed flags to: O_ACCMODE | O_APPEND |
48f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// O_NONBLOCK | O_SYNC | O_LARGEFILE | O_CLOEXEC | O_NOATIME.
49f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex VakulenkoSANDBOX_EXPORT bpf_dsl::ResultExpr RestrictFcntlCommands();
50f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
51f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko#if defined(__i386__) || defined(__mips__)
52f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Restrict socketcall(2) to only allow socketpair(2), send(2), recv(2),
53f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// sendto(2), recvfrom(2), shutdown(2), sendmsg(2) and recvmsg(2).
54f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex VakulenkoSANDBOX_EXPORT bpf_dsl::ResultExpr RestrictSocketcallCommand();
55f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko#endif
56f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
57f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Restrict |sysno| (which must be kill, tkill or tgkill) by allowing tgkill or
58f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// kill iff the first parameter is |target_pid|, crashing otherwise or if
59f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// |sysno| is tkill.
60f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex VakulenkoSANDBOX_EXPORT bpf_dsl::ResultExpr RestrictKillTarget(pid_t target_pid,
61f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko                                                      int sysno);
62f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
63f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Crash if FUTEX_CMP_REQUEUE_PI is used in the second argument of futex(2).
64f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex VakulenkoSANDBOX_EXPORT bpf_dsl::ResultExpr RestrictFutex();
65f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
66f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Crash if |which| is not PRIO_PROCESS. EPERM if |who| is not 0, neither
67f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// |target_pid| while calling setpriority(2) / getpriority(2).
68f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex VakulenkoSANDBOX_EXPORT bpf_dsl::ResultExpr RestrictGetSetpriority(pid_t target_pid);
69f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
70f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Restricts |pid| for sched_* syscalls which take a pid as the first argument.
71f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// We only allow calling these syscalls if the pid argument is equal to the pid
72f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// of the sandboxed process or 0 (indicating the current thread).  The following
73f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// syscalls are supported:
74f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko//
75f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// sched_getaffinity(), sched_getattr(), sched_getparam(), sched_getscheduler(),
76f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// sched_rr_get_interval(), sched_setaffinity(), sched_setattr(),
77f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// sched_setparam(), sched_setscheduler()
78f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex VakulenkoSANDBOX_EXPORT bpf_dsl::ResultExpr RestrictSchedTarget(pid_t target_pid,
79f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko                                                       int sysno);
80f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
81f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Restricts the |pid| argument of prlimit64 to 0 (meaning the calling process)
82f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// or target_pid.
83f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex VakulenkoSANDBOX_EXPORT bpf_dsl::ResultExpr RestrictPrlimit64(pid_t target_pid);
84f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
85f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Restricts the |who| argument of getrusage to RUSAGE_SELF (meaning the calling
86f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// process).
87f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex VakulenkoSANDBOX_EXPORT bpf_dsl::ResultExpr RestrictGetrusage();
88f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
89f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// Restrict |clk_id| for clock_getres(), clock_gettime() and clock_settime().
90f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// We allow accessing only CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID,
91f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// CLOCK_REALTIME, and CLOCK_THREAD_CPUTIME_ID.  In particular, this disallows
92f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// access to arbitrary per-{process,thread} CPU-time clock IDs (such as those
93f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// returned by {clock,pthread}_getcpuclockid), which can leak information
94f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko// about the state of the host OS.
95f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex VakulenkoSANDBOX_EXPORT bpf_dsl::ResultExpr RestrictClockID();
96f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
97f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko}  // namespace sandbox.
98f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko
99f6024733c0d1eed88f68520b5e6a20b96e212ad6Alex Vakulenko#endif  // SANDBOX_LINUX_SECCOMP_BPF_HELPERS_SYSCALL_PARAMETERS_RESTRICTIONS_H_
100