Skip to content

Commit

Permalink
Refine SELinux rules for Xposed files (JingMatrix#149)
Browse files Browse the repository at this point in the history
1. The SELinux context label `magisk_file` is widely used by Zygisk implementation modules. It is improper for LSPosed to abuse this label for its own files. We replace it by `xposed_file`.
2. A new rule added according to the SELinux logs, which is needed to write to the mangaer's SharedPreference.
3. `xposed_data` is a new SELinux context label for XSharedPreference files, it is not meant to provide MAC restricted access but to conform with Android's rule: https://developer.android.com/about/versions/pie/android-9.0-changes-28#per-app-selinux.
4. We add attribute `mlstrustedobject` to ignore the `Multi-Level and Multi-Category Security` enforced on Android.
  • Loading branch information
JingMatrix authored and DanGLES3 committed Jan 16, 2025
1 parent 96a0fde commit e317e71
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ static Path resolveModuleDir(String packageName, String dir, int userId, int uid
if (uid != -1) {
if (path.toFile().mkdirs()) {
try {
SELinux.setFileContext(path.toString(), "u:object_r:magisk_file:s0");
SELinux.setFileContext(path.toString(), "u:object_r:xposed_file:s0");
Os.chown(path.toString(), uid, uid);
Os.chmod(path.toString(), 0755);
} catch (ErrnoException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ private synchronized void updateConfig() {
try {
var perms = PosixFilePermissions.fromString("rwx--x--x");
Files.createDirectories(miscPath, PosixFilePermissions.asFileAttribute(perms));
walkFileTree(miscPath, f -> SELinux.setFileContext(f.toString(), "u:object_r:magisk_file:s0"));
walkFileTree(miscPath, f -> SELinux.setFileContext(f.toString(), "u:object_r:xposed_data:s0"));
} catch (IOException e) {
Log.e(TAG, Log.getStackTraceString(e));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,16 @@ public void run() {
Log.i(TAG, "Dex2oat wrapper daemon start");
var sockPath = getSockPath();
Log.d(TAG, "wrapper path: " + sockPath);
var magisk_file = "u:object_r:magisk_file:s0";
var xposed_file = "u:object_r:xposed_file:s0";
var dex2oat_exec = "u:object_r:dex2oat_exec:s0";
if (SELinux.checkSELinuxAccess("u:r:dex2oat:s0", dex2oat_exec,
"file", "execute_no_trans")) {
SELinux.setFileContext(WRAPPER32, dex2oat_exec);
SELinux.setFileContext(WRAPPER64, dex2oat_exec);
setSockCreateContext("u:r:dex2oat:s0");
} else {
SELinux.setFileContext(WRAPPER32, magisk_file);
SELinux.setFileContext(WRAPPER64, magisk_file);
SELinux.setFileContext(WRAPPER32, xposed_file);
SELinux.setFileContext(WRAPPER64, xposed_file);
setSockCreateContext("u:r:installd:s0");
}
try (var server = new LocalServerSocket(sockPath)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public static void broadcastIntent(Intent inIntent) {

private void ensureWebViewPermission(File f) {
if (!f.exists()) return;
SELinux.setFileContext(f.getAbsolutePath(), "u:object_r:magisk_file:s0");
SELinux.setFileContext(f.getAbsolutePath(), "u:object_r:xposed_file:s0");
try {
Os.chown(f.getAbsolutePath(), BuildConfig.MANAGER_INJECTED_UID, BuildConfig.MANAGER_INJECTED_UID);
} catch (ErrnoException e) {
Expand Down
2 changes: 1 addition & 1 deletion magisk-loader/magisk_module/customize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ else
fi

set_perm_recursive "$MODPATH" 0 0 0755 0644
set_perm_recursive "$MODPATH/bin" 0 2000 0755 0755 u:object_r:magisk_file:s0
set_perm_recursive "$MODPATH/bin" 0 2000 0755 0755 u:object_r:xposed_file:s0
chmod 0744 "$MODPATH/daemon"

if [ "$(grep_prop ro.maple.enable)" == "1" ] && [ "$FLAVOR" == "zygisk" ]; then
Expand Down
10 changes: 10 additions & 0 deletions magisk-loader/magisk_module/sepolicy.rule
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
allow dex2oat dex2oat_exec file execute_no_trans

allow shell shell dir write

type xposed_file file_type
typeattribute xposed_file mlstrustedobject
allow {installd isolated_app shell} xposed_file {file dir} *

type xposed_data file_type
typeattribute xposed_data mlstrustedobject
allow * xposed_data {file dir} *

0 comments on commit e317e71

Please sign in to comment.