termux-app
96 строк · 3.8 Кб
1package com.termux.shared.android;2
3import android.annotation.SuppressLint;4
5import androidx.annotation.NonNull;6import androidx.annotation.Nullable;7
8import com.termux.shared.logger.Logger;9import com.termux.shared.reflection.ReflectionUtils;10
11import java.lang.reflect.Method;12
13public class SELinuxUtils {14
15public static final String ANDROID_OS_SELINUX_CLASS = "android.os.SELinux";16
17private static final String LOG_TAG = "SELinuxUtils";18
19/**20* Gets the security context of the current process.
21*
22* @return Returns a {@link String} representing the security context of the current process.
23* This will be {@code null} if an exception is raised.
24*/
25@Nullable26public static String getContext() {27ReflectionUtils.bypassHiddenAPIReflectionRestrictions();28String methodName = "getContext";29try {30@SuppressLint("PrivateApi") Class<?> clazz = Class.forName(ANDROID_OS_SELINUX_CLASS);31Method method = ReflectionUtils.getDeclaredMethod(clazz, methodName);32if (method == null) {33Logger.logError(LOG_TAG, "Failed to get " + methodName + "() method of " + ANDROID_OS_SELINUX_CLASS + " class");34return null;35}36
37return (String) ReflectionUtils.invokeMethod(method, null).value;38} catch (Exception e) {39Logger.logStackTraceWithMessage(LOG_TAG, "Failed to call " + methodName + "() method of " + ANDROID_OS_SELINUX_CLASS + " class", e);40return null;41}42}43
44/**45* Get the security context of a given process id.
46*
47* @param pid The pid of process.
48* @return Returns a {@link String} representing the security context of the given pid.
49* This will be {@code null} if an exception is raised.
50*/
51@Nullable52public static String getPidContext(int pid) {53ReflectionUtils.bypassHiddenAPIReflectionRestrictions();54String methodName = "getPidContext";55try {56@SuppressLint("PrivateApi") Class<?> clazz = Class.forName(ANDROID_OS_SELINUX_CLASS);57Method method = ReflectionUtils.getDeclaredMethod(clazz, methodName, int.class);58if (method == null) {59Logger.logError(LOG_TAG, "Failed to get " + methodName + "() method of " + ANDROID_OS_SELINUX_CLASS + " class");60return null;61}62
63return (String) ReflectionUtils.invokeMethod(method, null, pid).value;64} catch (Exception e) {65Logger.logStackTraceWithMessage(LOG_TAG, "Failed to call " + methodName + "() method of " + ANDROID_OS_SELINUX_CLASS + " class", e);66return null;67}68}69
70/**71* Get the security context of a file object.
72*
73* @param path The pathname of the file object.
74* @return Returns a {@link String} representing the security context of the file.
75* This will be {@code null} if an exception is raised.
76*/
77@Nullable78public static String getFileContext(@NonNull String path) {79ReflectionUtils.bypassHiddenAPIReflectionRestrictions();80String methodName = "getFileContext";81try {82@SuppressLint("PrivateApi") Class<?> clazz = Class.forName(ANDROID_OS_SELINUX_CLASS);83Method method = ReflectionUtils.getDeclaredMethod(clazz, methodName, String.class);84if (method == null) {85Logger.logError(LOG_TAG, "Failed to get " + methodName + "() method of " + ANDROID_OS_SELINUX_CLASS + " class");86return null;87}88
89return (String) ReflectionUtils.invokeMethod(method, null, path).value;90} catch (Exception e) {91Logger.logStackTraceWithMessage(LOG_TAG, "Failed to call " + methodName + "() method of " + ANDROID_OS_SELINUX_CLASS + " class", e);92return null;93}94}95
96}
97