1 /*
2  * Copyright (C) 2015 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 <android/log.h>
18 #include <jni.h>
19 #include <string.h>
20 #include <time.h>
21 
22 #if defined(ARCH_SUPPORTS_SECCOMP)
23 #include <linux/filter.h>
24 #include <linux/seccomp.h>
25 #include <sys/syscall.h>
26 #endif
27 
28 #include "seccomp_sample_program.h"
29 #include "seccomp-tests/tests/test_harness.h"
30 
31 // Forward declare from seccomp_bpf_tests.c.
32 extern "C" {
33 struct __test_metadata* get_seccomp_test_list();
34 }
35 
36 static const char TAG[] = "SeccompBpfTest-Native";
37 
android_security_cts_SeccompBpfTest_runKernelUnitTest(JNIEnv * env,jobject thiz __unused,jstring name)38 jboolean android_security_cts_SeccompBpfTest_runKernelUnitTest(
39       JNIEnv* env, jobject thiz __unused, jstring name) {
40 #if defined(ARCH_SUPPORTS_SECCOMP)
41     const char* nameStr = env->GetStringUTFChars(name, nullptr);
42 
43     for (struct __test_metadata* t = get_seccomp_test_list(); t; t = t->next) {
44         if (strcmp(t->name, nameStr) == 0) {
45             __android_log_print(ANDROID_LOG_INFO, TAG, "Start: %s", t->name);
46             __run_test(t);
47             __android_log_print(ANDROID_LOG_INFO, TAG, "%s: %s",
48                 t->passed ? "PASS" : "FAIL", t->name);
49             return t->passed;
50         }
51     }
52 #endif  // ARCH_SUPPORTS_SECCOMP
53 
54     return false;
55 }
56 
android_security_cts_SeccompBpfTest_installTestFilter(JNIEnv *,jclass)57 jboolean android_security_cts_SeccompBpfTest_installTestFilter(JNIEnv*, jclass) {
58 #if !defined(ARCH_SUPPORTS_SECCOMP)
59   return false;
60 #else
61   struct sock_fprog prog = GetTestSeccompFilterProgram();
62 
63   if (prog.len == 0)
64     return false;
65 
66   int rv = syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, &prog);
67   return rv == 0;
68 #endif
69 }
70 
android_security_cts_SeccompBpfTest_getClockBootTime(JNIEnv *,jclass)71 jint android_security_cts_SeccompBpfTest_getClockBootTime(JNIEnv*, jclass) {
72   struct timespec ts;
73   int rv = clock_gettime(CLOCK_BOOTTIME, &ts);
74   return rv;
75 }
76 
77 static JNINativeMethod methods[] = {
78     { "runKernelUnitTest", "(Ljava/lang/String;)Z",
79         (void*)android_security_cts_SeccompBpfTest_runKernelUnitTest },
80     { "installTestFilter", "()Z",
81         (void*)android_security_cts_SeccompBpfTest_installTestFilter },
82     { "getClockBootTime", "()I",
83         (void*)android_security_cts_SeccompBpfTest_getClockBootTime },
84 };
85 
register_android_os_cts_SeccompTest(JNIEnv * env)86 int register_android_os_cts_SeccompTest(JNIEnv* env) {
87     jclass clazz = env->FindClass("android/os/cts/SeccompTest");
88     return env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(JNINativeMethod));
89 }
90