File: baseline_policy_android.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (336 lines) | stat: -rw-r--r-- 11,548 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sandbox/linux/seccomp-bpf-helpers/baseline_policy_android.h"

#include <errno.h>
#include <fcntl.h>
#include <linux/android/binder.h>
#include <linux/ashmem.h>
#include <linux/incrementalfs.h>
#include <linux/nbd.h>
#include <linux/net.h>
#include <linux/userfaultfd.h>
#include <sched.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/types.h>

#include "build/build_config.h"
#include "sandbox/linux/bpf_dsl/bpf_dsl.h"
#include "sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h"
#include "sandbox/linux/seccomp-bpf-helpers/syscall_parameters_restrictions.h"
#include "sandbox/linux/seccomp-bpf-helpers/syscall_sets.h"
#include "sandbox/linux/system_headers/linux_syscalls.h"

#if defined(__x86_64__)
#include <asm/prctl.h>
#endif

using sandbox::bpf_dsl::AllOf;
using sandbox::bpf_dsl::Allow;
using sandbox::bpf_dsl::AnyOf;
using sandbox::bpf_dsl::Arg;
using sandbox::bpf_dsl::BoolConst;
using sandbox::bpf_dsl::BoolExpr;
using sandbox::bpf_dsl::Error;
using sandbox::bpf_dsl::If;
using sandbox::bpf_dsl::ResultExpr;

namespace sandbox {

namespace {

// Restricts the arguments to sys_socket() to AF_UNIX. Returns a BoolExpr that
// evaluates to true if the syscall should be allowed.
BoolExpr RestrictSocketArguments(const Arg<int>& domain,
                                 const Arg<int>& type,
                                 const Arg<int>& protocol) {
  const int kSockFlags = SOCK_CLOEXEC | SOCK_NONBLOCK;
  return AllOf(domain == AF_UNIX,
               AnyOf((type & ~kSockFlags) == SOCK_DGRAM,
                     (type & ~kSockFlags) == SOCK_STREAM),
               protocol == 0);
}

ResultExpr RestrictAndroidIoctl(bool allow_userfaultfd_ioctls) {
  const Arg<unsigned int> request(1);

  // There is no way at runtime to test if the system is running with
  // BINDER_IPC_32BIT. Instead, compute the corresponding bitness' ioctl
  // request number, so that either are allowed in the case of mixed-bitness
  // systems.
#ifdef BINDER_IPC_32BIT
  const unsigned int kBinderWriteRead32 = BINDER_WRITE_READ;
  const unsigned int kBinderWriteRead64 =
      (BINDER_WRITE_READ & ~IOCSIZE_MASK) |
      ((sizeof(binder_write_read) * 2) << _IOC_SIZESHIFT);
#else
  const unsigned int kBinderWriteRead64 = BINDER_WRITE_READ;
  const unsigned int kBinderWriteRead32 =
      (BINDER_WRITE_READ & ~IOCSIZE_MASK) |
      ((sizeof(binder_write_read) / 2) << _IOC_SIZESHIFT);
#endif

  // ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), a legacy interface
  // for getting clock information from /dev/alarm. It was removed in Android O
  // (https://android-review.googlesource.com/c/221812), and it can be safely
  // blocked in earlier releases because there is a fallback. Constant expanded
  // from
  // https://cs.android.com/android/platform/superproject/+/android-7.0.0_r1:external/kernel-headers/original/uapi/linux/android_alarm.h;l=57.
  // The size is a `struct timespec`, which has a different width on 32- and
  // 64-bit systems, so handle both.
  const unsigned int kAndroidAlarmGetTimeElapsedRealtime32 = 0x40086134;
  const unsigned int kAndroidAlarmGetTimeElapsedRealtime64 = 0x40106134;
  return Switch(request)
      .Cases(
          {// Android shared memory.
           ASHMEM_SET_NAME, ASHMEM_GET_NAME, ASHMEM_SET_SIZE, ASHMEM_GET_SIZE,
           ASHMEM_SET_PROT_MASK, ASHMEM_GET_PROT_MASK, ASHMEM_PIN, ASHMEM_UNPIN,
           ASHMEM_GET_PIN_STATUS,
           // Binder.
           kBinderWriteRead32, kBinderWriteRead64, BINDER_SET_MAX_THREADS,
           BINDER_THREAD_EXIT, BINDER_VERSION,
           BINDER_ENABLE_ONEWAY_SPAM_DETECTION, BINDER_GET_EXTENDED_ERROR,
           // incfs read ops.
           INCFS_IOC_READ_FILE_SIGNATURE, INCFS_IOC_GET_FILLED_BLOCKS,
           INCFS_IOC_GET_READ_TIMEOUTS, INCFS_IOC_GET_LAST_READ_ERROR,
           INCFS_IOC_GET_BLOCK_COUNT, INCFS_IOC_SET_READ_TIMEOUTS},
          Allow())
      .Cases(
          {// userfaultfd ART GC (https://crbug.com/1300653).
           UFFDIO_REGISTER, UFFDIO_UNREGISTER, UFFDIO_WAKE, UFFDIO_COPY,
           UFFDIO_ZEROPAGE, UFFDIO_CONTINUE,
           // crbug.com/393204193
           UFFDIO_MOVE},
          If(BoolConst(allow_userfaultfd_ioctls), Allow())
              .Else(RestrictIoctl()))
      .Cases(
          {// Deprecated Android /dev/alarm interface.
           kAndroidAlarmGetTimeElapsedRealtime32,
           kAndroidAlarmGetTimeElapsedRealtime64,
           // Linux Network Block Device requests observed in the field
           // https://crbug.com/1314105.
           NBD_CLEAR_SOCK, NBD_SET_BLKSIZE},
          Error(EINVAL))
      .Default(RestrictIoctl());
}

ResultExpr RestrictCloneParameters() {
  const Arg<unsigned long> flags(0);

  const uint64_t kForkFlags =
      CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | SIGCHLD;
  const uint64_t kPthreadCreateFlags =
      CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD |
      CLONE_SYSVSEM | CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID;

  const BoolExpr is_fork_or_pthread =
      AnyOf(flags == kForkFlags, flags == kPthreadCreateFlags);
  return If(is_fork_or_pthread, Allow()).Else(CrashSIGSYSClone());
}

bool IsBaselinePolicyAllowed(int sysno) {
  // The following syscalls are used in the renderer policy on Android but still
  // need to be allowed on Android and should not be filtered out of other
  // processes: mremap, ftruncate, ftruncate64, pwrite64, getcpu, fdatasync,
  // fsync, getrlimit, ugetrlimit, setrlimit.

  switch (sysno) {
    case __NR_epoll_pwait:
    case __NR_fdatasync:
    case __NR_flock:
    case __NR_fsync:
#if defined(__LP64__)
    case __NR_ftruncate:
    case __NR_newfstatat:
    case __NR_fstatfs:
#else
    case __NR_ftruncate64:
    case __NR_fstatat64:
    case __NR_fstatfs64:
#endif
#if defined(__arm__) || defined(__aarch64__)
    // getcpu() is allowed on ARM chips because it is used in
    // //third_party/cpuinfo/ on those chips.
    case __NR_getcpu:
#endif
    case __NR_getdents64:
    case __NR_getpriority:
    case __NR_membarrier:  // https://crbug.com/966433
#if defined(__i386__)
    // Used on pre-N to initialize threads in ART.
    case __NR_modify_ldt:
#endif
    case __NR_mremap:
    case __NR_msync:
      // File system access cannot be restricted with seccomp-bpf on Android,
      // since the JVM classloader and other Framework features require file
      // access. It may be possible to restrict the filesystem with SELinux.
      // Currently we rely on the app/service UID isolation to create a
      // filesystem "sandbox".
    case __NR_openat:
    case __NR_pwrite64:
    case __NR_rt_sigtimedwait:
#if !defined(__LP64__)
    // TODO(crbug.com/40528912): bionic has no plans to support 64-bit time_t on ILP32.
    case __NR_rt_sigtimedwait_time64:
#endif
    case __NR_sched_getparam:
    case __NR_sched_getscheduler:
    case __NR_sched_setscheduler:
    case __NR_setpriority:
#if defined(__i386__)
    // Used on N+ instead of __NR_modify_ldt to initialize threads in ART.
    case __NR_set_thread_area:
#endif
    case __NR_set_tid_address:
#if defined(__LP64__)
    case __NR_getrlimit:
#else
    case __NR_ugetrlimit:
#endif

      // Permit socket operations so that renderers can connect to logd and
      // debuggerd. The arguments to socket() are further restricted below.
      // Note that on i386 (until API level 38), both of these calls mapped
      // to __NR_socketcall, which is demultiplexed below.
    case __NR_getsockopt:
    case __NR_connect:

      return true;
    default:
      return false;
  }
}

}  // namespace

BaselinePolicyAndroid::BaselinePolicyAndroid() = default;

BaselinePolicyAndroid::BaselinePolicyAndroid(const RuntimeOptions& options)
    : options_(options) {}

BaselinePolicyAndroid::~BaselinePolicyAndroid() = default;

ResultExpr BaselinePolicyAndroid::EvaluateSyscall(int sysno) const {
  if (sysno == __NR_clone) {
    if (options_.should_restrict_clone_params) {
      return RestrictCloneParameters();
    }
    return Allow();
  }
  if (sysno == __NR_sched_setaffinity || sysno == __NR_sched_getaffinity) {
    return Error(EPERM);
  }

  if (sysno == __NR_ioctl) {
    return RestrictAndroidIoctl(options_.allow_userfaultfd_ioctls);
  }

  if (sysno == __NR_madvise) {
    // Allow MADV_PAGEOUT
    const Arg<int> advice(2);
    return If(advice == MADV_PAGEOUT, Allow())
        .Else(BaselinePolicy::EvaluateSyscall(sysno));
  }

  // Ptrace is allowed so the crash reporter can fork in a renderer
  // and then ptrace the parent. https://crbug.com/933418
  if (sysno == __NR_ptrace) {
    return RestrictPtrace();
  }

  // https://crbug.com/766245
  if (sysno == __NR_process_vm_readv) {
    const Arg<pid_t> pid(0);
    return If(pid == policy_pid(), Allow())
           .Else(Error(EPERM));
  }

  if (!options_.should_restrict_renderer_syscalls) {
    if (sysno == __NR_sysinfo) {
      return Allow();
    }
    // https://crbug.com/655299
    if (sysno == __NR_clock_getres
#if !defined(__LP64__)
      // TODO(crbug.com/40528912): bionic has no plans to support 64-bit time_t on ILP32.
      || sysno == __NR_clock_getres_time64
#endif
    ) {
    return RestrictClockID();
    }
  }

  // https://crbug.com/826289
  if (sysno == __NR_getrusage) {
    return RestrictGetrusage();
  }

#if defined(__x86_64__)
  if (sysno == __NR_arch_prctl) {
    const Arg<int> code(0);
    return If(code == ARCH_SET_GS, Allow()).Else(Error(EPERM));
  }
#endif

  // Restrict socket-related operations.
  if (sysno == __NR_socket) {
    const Arg<int> domain(0);
    const Arg<int> type(1);
    const Arg<int> protocol(2);
    return If(RestrictSocketArguments(domain, type, protocol), Allow())
           .Else(Error(EPERM));
  }

  // https://crbug.com/655300
  if (sysno == __NR_getsockname) {
    // Rather than blocking with SIGSYS, just return an error. This is not
    // documented to be a valid errno, but we will use it anyways.
    return Error(EPERM);
  }

  // https://crbug.com/682488, https://crbug.com/701137
  if (sysno == __NR_setsockopt) {
    // The baseline policy applies other restrictions to setsockopt.
    const Arg<int> level(1);
    const Arg<int> option(2);
    return If(AllOf(level == SOL_SOCKET,
                    AnyOf(option == SO_SNDTIMEO,
                          option == SO_RCVTIMEO,
                          option == SO_SNDBUF,
                          option == SO_REUSEADDR,
                          option == SO_PASSCRED)),
              Allow())
           .Else(BaselinePolicy::EvaluateSyscall(sysno));
  }

#if defined(__i386__)
  // On i386 (until API level 38), the socketcall syscall demultiplexes socket
  // operations and the individual system calls above aren't used.
  // TODO(crbug.com/40528912): disallow and rewrite socketcall()s if individual
  // syscalls like socket() are usable in the current environment.
  if (sysno == __NR_socketcall) {
    // The baseline policy allows other socketcall sub-calls.
    const Arg<int> socketcall(0);
    return Switch(socketcall)
        .Cases({SYS_CONNECT,
                SYS_SOCKET,
                SYS_SETSOCKOPT,
                SYS_GETSOCKOPT},
               Allow())
        .Default(BaselinePolicy::EvaluateSyscall(sysno));
  }
#endif

  if (IsBaselinePolicyAllowed(sysno)) {
    return Allow();
  }

  return BaselinePolicy::EvaluateSyscall(sysno);
}

}  // namespace sandbox