xref: /aosp_15_r20/cts/tests/tests/os/jni/android_os_cts_SeccompTest.cpp (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
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 #include <unistd.h>
22 
23 #include <libminijail.h>
24 #include <seccomp_bpf_tests.h>
25 
26 #include "android-base/macros.h"
27 
android_security_cts_SeccompBpfTest_runKernelUnitTest(JNIEnv * env,jobject thiz __unused,jstring name)28 jboolean android_security_cts_SeccompBpfTest_runKernelUnitTest(
29       JNIEnv* env, jobject thiz __unused, jstring name) {
30     const char* nameStr = env->GetStringUTFChars(name, nullptr);
31     return run_seccomp_test(nameStr);
32 }
33 
android_security_cts_SeccompBpfTest_nativeInstallTestFilter(JNIEnv *,jclass,jint policyFd)34 jboolean android_security_cts_SeccompBpfTest_nativeInstallTestFilter(
35         JNIEnv*, jclass, jint policyFd) {
36     minijail* j = minijail_new();
37     minijail_no_new_privs(j);
38     minijail_use_seccomp_filter(j);
39     minijail_set_seccomp_filter_tsync(j);
40     minijail_parse_seccomp_filters_from_fd(j, policyFd);
41     minijail_enter(j);
42     minijail_destroy(j);
43     return true;
44 }
45 
android_security_cts_SeccompBpfTest_getPolicyAbiString(JNIEnv * env,jclass)46 jstring android_security_cts_SeccompBpfTest_getPolicyAbiString(JNIEnv* env, jclass) {
47     return env->NewStringUTF(ABI_STRING);
48 }
49 
android_security_cts_SeccompBpfTest_getClockBootTime(JNIEnv *,jclass)50 jint android_security_cts_SeccompBpfTest_getClockBootTime(JNIEnv*, jclass) {
51     struct timespec ts;
52     int rv = clock_gettime(CLOCK_BOOTTIME_ALARM, &ts);
53     return rv;
54 }
55 
56 static JNINativeMethod methods[] = {
57     { "runKernelUnitTest", "(Ljava/lang/String;)Z",
58         (void*)android_security_cts_SeccompBpfTest_runKernelUnitTest },
59     { "nativeInstallTestFilter", "(I)Z",
60         (void*)android_security_cts_SeccompBpfTest_nativeInstallTestFilter },
61     { "getPolicyAbiString", "()Ljava/lang/String;",
62         (void*)android_security_cts_SeccompBpfTest_getPolicyAbiString },
63     { "getClockBootTime", "()I",
64         (void*)android_security_cts_SeccompBpfTest_getClockBootTime },
65 };
66 
register_android_os_cts_SeccompTest(JNIEnv * env)67 int register_android_os_cts_SeccompTest(JNIEnv* env) {
68     jclass clazz = env->FindClass("android/os/cts/SeccompTest");
69     return env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(JNINativeMethod));
70 }
71