Skip to content

Commit 0cdbe98

Browse files
Added: Add functions to PackageUtils to get seInfo and seInfoUser of package
1 parent f84853f commit 0cdbe98

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

termux-shared/src/main/java/com/termux/shared/packages/PackageUtils.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.content.pm.ApplicationInfo;
99
import android.content.pm.PackageInfo;
1010
import android.content.pm.PackageManager;
11+
import android.os.Build;
1112
import android.os.UserManager;
1213

1314
import androidx.annotation.NonNull;
@@ -17,8 +18,10 @@
1718
import com.termux.shared.data.DataUtils;
1819
import com.termux.shared.interact.MessageDialogUtils;
1920
import com.termux.shared.logger.Logger;
21+
import com.termux.shared.reflection.ReflectionUtils;
2022
import com.termux.shared.termux.TermuxConstants;
2123

24+
import java.lang.reflect.Field;
2225
import java.security.MessageDigest;
2326
import java.util.List;
2427

@@ -94,6 +97,55 @@ public static PackageInfo getPackageInfoForPackage(@NonNull final Context contex
9497
}
9598
}
9699

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+
97149
/**
98150
* Get the app name for the package associated with the {@code context}.
99151
*

0 commit comments

Comments
 (0)