1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "berberis/kernel_api/sys_prctl_emulation.h"
18
19 #include <linux/filter.h>
20 #include <linux/seccomp.h>
21 #include <sys/prctl.h>
22 #include <sys/syscall.h>
23 #include <unistd.h>
24
25 #include "berberis/base/bit_util.h"
26 #include "berberis/base/checks.h"
27 #include "berberis/kernel_api/syscall_numbers.h"
28
29 namespace berberis {
30
PrctlForGuest(int option,unsigned long arg2,unsigned long arg3,unsigned long arg4,unsigned long arg5)31 int PrctlForGuest(int option,
32 unsigned long arg2,
33 unsigned long arg3,
34 unsigned long arg4,
35 unsigned long arg5) {
36 if (option == PR_SET_SECCOMP) {
37 auto prog = bit_cast<struct sock_fprog*>(arg3);
38 for (int i = 0; i < prog->len; i++) {
39 struct sock_filter& filter = prog->filter[i];
40 if (BPF_CLASS(filter.code) != BPF_JMP) {
41 continue;
42 }
43 // TODO(b/110423578): Even if we block host syscall this may not block
44 // emulated guest syscall.
45 filter.k = ToHostSyscallNumber(filter.k);
46 LOG_ALWAYS_FATAL_IF(filter.k == unsigned(-1),
47 "Unsupported guest syscall number in PR_SET_SECCOMP");
48 }
49 }
50
51 return prctl(option, arg2, arg3, arg4, arg5);
52 }
53
54 } // namespace berberis
55