1 /*
2  * Copyright (C) 2017 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 "minijail.h"
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <signal.h>
21 
22 #include <libminijail.h>
23 #include <scoped_minijail.h>
24 #include <android/log.h>
25 
26 #ifndef LOG_TAG
27 #define LOG_TAG "minijail"
28 #endif
29 
30 #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR  , LOG_TAG, __VA_ARGS__)
31 
32 
33 /*
34  * Class:     com_android_tv_tuner_exoplayer_ffmpeg_FfmpegDecoderService
35  * Method:    nativeSetupMinijail
36  * Signature: (I)V
37  */
38 JNIEXPORT void JNICALL
Java_com_android_tv_tuner_exoplayer_ffmpeg_FfmpegDecoderService_nativeSetupMinijail(JNIEnv *,jobject,jint policyFd)39 Java_com_android_tv_tuner_exoplayer_ffmpeg_FfmpegDecoderService_nativeSetupMinijail
40 (JNIEnv *, jobject, jint policyFd) {
41     ScopedMinijail jail{minijail_new()};
42     if (!jail) {
43         ALOGE("Failed to create minijail");
44     }
45 
46     minijail_no_new_privs(jail.get());
47     minijail_log_seccomp_filter_failures(jail.get());
48     minijail_use_seccomp_filter(jail.get());
49     minijail_set_seccomp_filter_tsync(jail.get());
50     // Transfer ownership of |policy_fd|.
51     minijail_parse_seccomp_filters_from_fd(jail.get(), policyFd);
52     minijail_enter(jail.get());
53     close(policyFd);
54 }
55 
56 /*
57  * Class:     com_android_tv_tuner_exoplayer_ffmpeg_FfmpegDecoderService
58  * Method:    nativeTestMinijail
59  * Signature: ()V
60  */
61 JNIEXPORT void JNICALL
Java_com_android_tv_tuner_exoplayer_ffmpeg_FfmpegDecoderService_nativeTestMinijail(JNIEnv *,jobject)62 Java_com_android_tv_tuner_exoplayer_ffmpeg_FfmpegDecoderService_nativeTestMinijail
63 (JNIEnv *, jobject) {
64     kill(getpid(), SIGUSR1);
65 }