termux-app

Форк
0
96 строк · 3.8 Кб
1
package com.termux.shared.android;
2

3
import android.annotation.SuppressLint;
4

5
import androidx.annotation.NonNull;
6
import androidx.annotation.Nullable;
7

8
import com.termux.shared.logger.Logger;
9
import com.termux.shared.reflection.ReflectionUtils;
10

11
import java.lang.reflect.Method;
12

13
public class SELinuxUtils {
14

15
    public static final String ANDROID_OS_SELINUX_CLASS = "android.os.SELinux";
16

17
    private 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
    @Nullable
26
    public static String getContext() {
27
        ReflectionUtils.bypassHiddenAPIReflectionRestrictions();
28
        String methodName = "getContext";
29
        try {
30
            @SuppressLint("PrivateApi") Class<?> clazz = Class.forName(ANDROID_OS_SELINUX_CLASS);
31
            Method method = ReflectionUtils.getDeclaredMethod(clazz, methodName);
32
            if (method == null) {
33
                Logger.logError(LOG_TAG, "Failed to get " + methodName + "() method of " + ANDROID_OS_SELINUX_CLASS + " class");
34
                return null;
35
            }
36

37
            return (String) ReflectionUtils.invokeMethod(method, null).value;
38
        } catch (Exception e) {
39
            Logger.logStackTraceWithMessage(LOG_TAG, "Failed to call " + methodName + "() method of " + ANDROID_OS_SELINUX_CLASS + " class", e);
40
            return 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
    @Nullable
52
    public static String getPidContext(int pid) {
53
        ReflectionUtils.bypassHiddenAPIReflectionRestrictions();
54
        String methodName = "getPidContext";
55
        try {
56
            @SuppressLint("PrivateApi") Class<?> clazz = Class.forName(ANDROID_OS_SELINUX_CLASS);
57
            Method method = ReflectionUtils.getDeclaredMethod(clazz, methodName, int.class);
58
            if (method == null) {
59
                Logger.logError(LOG_TAG, "Failed to get " + methodName + "() method of " + ANDROID_OS_SELINUX_CLASS + " class");
60
                return null;
61
            }
62

63
            return (String) ReflectionUtils.invokeMethod(method, null, pid).value;
64
        } catch (Exception e) {
65
            Logger.logStackTraceWithMessage(LOG_TAG, "Failed to call " + methodName + "() method of " + ANDROID_OS_SELINUX_CLASS + " class", e);
66
            return 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
    @Nullable
78
    public static String getFileContext(@NonNull String path) {
79
        ReflectionUtils.bypassHiddenAPIReflectionRestrictions();
80
        String methodName = "getFileContext";
81
        try {
82
            @SuppressLint("PrivateApi") Class<?> clazz = Class.forName(ANDROID_OS_SELINUX_CLASS);
83
            Method method = ReflectionUtils.getDeclaredMethod(clazz, methodName, String.class);
84
            if (method == null) {
85
                Logger.logError(LOG_TAG, "Failed to get " + methodName + "() method of " + ANDROID_OS_SELINUX_CLASS + " class");
86
                return null;
87
            }
88

89
            return (String) ReflectionUtils.invokeMethod(method, null, path).value;
90
        } catch (Exception e) {
91
            Logger.logStackTraceWithMessage(LOG_TAG, "Failed to call " + methodName + "() method of " + ANDROID_OS_SELINUX_CLASS + " class", e);
92
            return null;
93
        }
94
    }
95

96
}
97

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.