|
8 | 8 | import android.content.pm.ApplicationInfo;
|
9 | 9 | import android.content.pm.PackageInfo;
|
10 | 10 | import android.content.pm.PackageManager;
|
| 11 | +import android.os.Build; |
11 | 12 | import android.os.UserManager;
|
12 | 13 |
|
13 | 14 | import androidx.annotation.NonNull;
|
|
17 | 18 | import com.termux.shared.data.DataUtils;
|
18 | 19 | import com.termux.shared.interact.MessageDialogUtils;
|
19 | 20 | import com.termux.shared.logger.Logger;
|
| 21 | +import com.termux.shared.reflection.ReflectionUtils; |
20 | 22 | import com.termux.shared.termux.TermuxConstants;
|
21 | 23 |
|
| 24 | +import java.lang.reflect.Field; |
22 | 25 | import java.security.MessageDigest;
|
23 | 26 | import java.util.List;
|
24 | 27 |
|
@@ -94,6 +97,55 @@ public static PackageInfo getPackageInfoForPackage(@NonNull final Context contex
|
94 | 97 | }
|
95 | 98 | }
|
96 | 99 |
|
| 100 | + /** |
| 101 | + * Get the {@code seInfo} {@link Field} of the {@link ApplicationInfo} class. |
| 102 | + * |
| 103 | + * String retrieved from the seinfo tag found in selinux policy. This value can be set through |
| 104 | + * the mac_permissions.xml policy construct. This value is used for setting an SELinux security |
| 105 | + * context on the process as well as its data directory. |
| 106 | + * |
| 107 | + * https://cs.android.com/android/platform/superproject/+/android-7.1.0_r1:frameworks/base/core/java/android/content/pm/ApplicationInfo.java;l=609 |
| 108 | + * https://cs.android.com/android/platform/superproject/+/android-12.0.0_r32:frameworks/base/core/java/android/content/pm/ApplicationInfo.java;l=981 |
| 109 | + * https://cs.android.com/android/platform/superproject/+/android-7.0.0_r1:frameworks/base/services/core/java/com/android/server/pm/SELinuxMMAC.java;l=282 |
| 110 | + * https://cs.android.com/android/platform/superproject/+/android-12.0.0_r32:frameworks/base/services/core/java/com/android/server/pm/SELinuxMMAC.java;l=375 |
| 111 | + * https://cs.android.com/android/_/android/platform/frameworks/base/+/be0b8896d1bc385d4c8fb54c21929745935dcbea |
| 112 | + * |
| 113 | + * @param applicationInfo The {@link ApplicationInfo} for the package. |
| 114 | + * @return Returns the selinux info or {@code null} if an exception was raised. |
| 115 | + */ |
| 116 | + @Nullable |
| 117 | + public static String getApplicationInfoSeInfoForPackage(@NonNull final ApplicationInfo applicationInfo) { |
| 118 | + ReflectionUtils.bypassHiddenAPIReflectionRestrictions(); |
| 119 | + try { |
| 120 | + return (String) ReflectionUtils.invokeField(ApplicationInfo.class, Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? "seinfo" : "seInfo", applicationInfo).value; |
| 121 | + } catch (Exception e) { |
| 122 | + // ClassCastException may be thrown |
| 123 | + Logger.logStackTraceWithMessage(LOG_TAG, "Failed to get seInfo field value for ApplicationInfo class", e); |
| 124 | + return null; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Get the {@code seInfoUser} {@link Field} of the {@link ApplicationInfo} class. |
| 130 | + * |
| 131 | + * Also check {@link #getApplicationInfoSeInfoForPackage(ApplicationInfo)}. |
| 132 | + * |
| 133 | + * @param applicationInfo The {@link ApplicationInfo} for the package. |
| 134 | + * @return Returns the selinux info user or {@code null} if an exception was raised. |
| 135 | + */ |
| 136 | + @Nullable |
| 137 | + public static String getApplicationInfoSeInfoUserForPackage(@NonNull final ApplicationInfo applicationInfo) { |
| 138 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return null; |
| 139 | + ReflectionUtils.bypassHiddenAPIReflectionRestrictions(); |
| 140 | + try { |
| 141 | + return (String) ReflectionUtils.invokeField(ApplicationInfo.class, "seInfoUser", applicationInfo).value; |
| 142 | + } catch (Exception e) { |
| 143 | + // ClassCastException may be thrown |
| 144 | + Logger.logStackTraceWithMessage(LOG_TAG, "Failed to get seInfoUser field value for ApplicationInfo class", e); |
| 145 | + return null; |
| 146 | + } |
| 147 | + } |
| 148 | + |
97 | 149 | /**
|
98 | 150 | * Get the app name for the package associated with the {@code context}.
|
99 | 151 | *
|
|
0 commit comments