1 /*
2 * Copyright (C) 2013 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
18 #include "jni.h"
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <linux/filter.h>
27 #include <linux/seccomp.h>
28
29 #include <sys/prctl.h>
30 #include <sys/types.h>
31 #include <sys/utsname.h>
32 #include <sys/wait.h>
33
android_os_cts_OSFeatures_getNoNewPrivs(JNIEnv * env,jobject thiz)34 jint android_os_cts_OSFeatures_getNoNewPrivs(JNIEnv* env, jobject thiz)
35 {
36 return prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
37 }
38
android_os_cts_OSFeatures_prctlCapBsetRead(JNIEnv * env,jobject thiz,jint i)39 jint android_os_cts_OSFeatures_prctlCapBsetRead(JNIEnv* env, jobject thiz, jint i)
40 {
41 return prctl(PR_CAPBSET_READ, i, 0, 0, 0);
42 }
43
44 #define DENY BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL)
45
test_seccomp()46 static void test_seccomp() {
47 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
48 _exit(0);
49 }
50
51 struct sock_filter filter[] = { DENY };
52 struct sock_fprog prog;
53 memset(&prog, 0, sizeof(prog));
54 prog.len = sizeof(filter) / sizeof(filter[0]);
55 prog.filter = filter;
56
57 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) {
58 _exit(0);
59 }
60
61 while(1) {
62 _exit(0); // should crash with SIGSYS
63 }
64 }
65
android_os_cts_OSFeatures_hasSeccompSupport(JNIEnv * env,jobject)66 jboolean android_os_cts_OSFeatures_hasSeccompSupport(JNIEnv* env, jobject)
67 {
68 pid_t pid = fork();
69 if (pid == -1) {
70 jclass cls = env->FindClass("java/lang/RuntimeException");
71 env->ThrowNew(cls, "fork failed");
72 return false;
73 }
74 if (pid == 0) {
75 // child
76 test_seccomp();
77 _exit(0);
78 }
79
80 int status;
81 TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
82 return WIFSIGNALED(status) && (WTERMSIG(status) == SIGSYS);
83 }
84
android_os_cts_OSFeatures_needsSeccompSupport(JNIEnv *,jobject)85 jboolean android_os_cts_OSFeatures_needsSeccompSupport(JNIEnv*, jobject)
86 {
87 #if !defined(ARCH_SUPPORTS_SECCOMP)
88 // Seccomp support is only available for ARM, x86, x86_64.
89 // This define is controlled by the Android.mk.
90 return false;
91 #endif
92
93 int major;
94 int minor;
95 struct utsname uts;
96 if (uname(&uts) == -1) {
97 return false;
98 }
99
100 if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) {
101 return false;
102 }
103
104 // Kernels before 3.8 don't have seccomp
105 if ((major < 3) || ((major == 3) && (minor < 8))) {
106 return false;
107 }
108
109 return true;
110 }
111
112 static JNINativeMethod gMethods[] = {
113 { "getNoNewPrivs", "()I",
114 (void *) android_os_cts_OSFeatures_getNoNewPrivs },
115 { "prctlCapBsetRead", "(I)I",
116 (void *) android_os_cts_OSFeatures_prctlCapBsetRead },
117 { "hasSeccompSupport", "()Z",
118 (void *) android_os_cts_OSFeatures_hasSeccompSupport },
119 { "needsSeccompSupport", "()Z",
120 (void *) android_os_cts_OSFeatures_needsSeccompSupport }
121 };
122
register_android_os_cts_OSFeatures(JNIEnv * env)123 int register_android_os_cts_OSFeatures(JNIEnv* env)
124 {
125 jclass clazz = env->FindClass("android/os/cts/OSFeatures");
126
127 return env->RegisterNatives(clazz, gMethods,
128 sizeof(gMethods) / sizeof(JNINativeMethod));
129 }
130