Use standalone magiskpolicy

This commit is contained in:
topjohnwu 2022-03-17 03:15:39 -07:00
parent 69c2f407d6
commit fa8900be65
7 changed files with 27 additions and 34 deletions

View File

@ -59,8 +59,8 @@ except FileNotFoundError:
cpu_count = multiprocessing.cpu_count()
archs = ['armeabi-v7a', 'x86', 'arm64-v8a', 'x86_64']
default_targets = ['magisk', 'magiskinit', 'magiskboot', 'busybox']
support_targets = default_targets + ['magiskpolicy', 'resetprop', 'test']
default_targets = ['magisk', 'magiskinit', 'magiskboot', 'magiskpolicy', 'busybox']
support_targets = default_targets + ['resetprop', 'test']
sdk_path = os.environ['ANDROID_SDK_ROOT']
ndk_root = op.join(sdk_path, 'ndk')
@ -199,8 +199,8 @@ def clean_elf():
execv(['g++', '-std=c++11', 'tools/termux-elf-cleaner/termux-elf-cleaner.cpp',
'-o', elf_cleaner])
args = [elf_cleaner]
args.extend(op.join('native', 'out', arch, 'magisk')
for arch in archs)
args.extend(op.join('native', 'out', arch, bin)
for arch in archs for bin in ['magisk', 'magiskpolicy'])
execv(args)
@ -327,13 +327,14 @@ def build_binary(args):
if 'magisk' in args.target:
flag += ' B_MAGISK=1'
if 'magiskpolicy' in args.target:
flag += ' B_POLICY=1'
if 'test' in args.target:
flag += ' B_TEST=1'
if flag:
run_ndk_build(flag + ' B_SHARED=1')
if 'magisk' in args.target:
clean_elf()
flag = ''
@ -342,9 +343,6 @@ def build_binary(args):
dump_bin_header()
flag += ' B_INIT=1'
if 'magiskpolicy' in args.target:
flag += ' B_POLICY=1'
if 'resetprop' in args.target:
flag += ' B_PROP=1'

View File

@ -88,30 +88,30 @@ fun Project.setupApp() {
into("src/main/jniLibs")
into("armeabi-v7a") {
from(rootProject.file("native/out/armeabi-v7a")) {
include("busybox", "magiskboot", "magiskinit", "magisk")
include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk")
rename { if (it == "magisk") "libmagisk32.so" else "lib$it.so" }
}
}
into("x86") {
from(rootProject.file("native/out/x86")) {
include("busybox", "magiskboot", "magiskinit", "magisk")
include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk")
rename { if (it == "magisk") "libmagisk32.so" else "lib$it.so" }
}
}
into("arm64-v8a") {
from(rootProject.file("native/out/arm64-v8a")) {
include("busybox", "magiskboot", "magiskinit", "magisk")
include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk")
rename { if (it == "magisk") "libmagisk64.so" else "lib$it.so" }
}
}
into("x86_64") {
from(rootProject.file("native/out/x86_64")) {
include("busybox", "magiskboot", "magiskinit", "magisk")
include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk")
rename { if (it == "magisk") "libmagisk64.so" else "lib$it.so" }
}
}
onlyIf {
if (inputs.sourceFiles.files.size != 16)
if (inputs.sourceFiles.files.size != 20)
throw StopExecutionException("Please build binaries first! (./build.py binary)")
true
}

View File

@ -110,7 +110,7 @@ ifdef B_POLICY
include $(CLEAR_VARS)
LOCAL_MODULE := magiskpolicy
LOCAL_STATIC_LIBRARIES := \
libutilx \
libutils \
libsepol
LOCAL_SRC_FILES := \
@ -122,7 +122,6 @@ LOCAL_SRC_FILES := \
magiskpolicy/statement.cpp
LOCAL_CFLAGS := -DAPPLET_STUB_MAIN=magiskpolicy_main
LOCAL_LDFLAGS := -static
include $(BUILD_EXECUTABLE)
endif

View File

@ -167,6 +167,11 @@ static bool magisk_env() {
cp_afc(DATABIN "/busybox", buf);
exec_command_async(buf, "--install", "-s", dirname(buf));
if (access(DATABIN "/magiskpolicy", X_OK) == 0) {
sprintf(buf, "%s/magiskpolicy", MAGISKTMP.data());
cp_afc(DATABIN "/magiskpolicy", buf);
}
return true;
}

View File

@ -513,11 +513,9 @@ public:
xsymlink("./magisk", dest.data());
}
} else {
for (int i = 0; init_applet[i]; ++i) {
string dest = dir_name + "/" + init_applet[i];
VLOGD("create", "./magiskinit", dest.data());
xsymlink("./magiskinit", dest.data());
}
string dest = dir_name + "/supolicy";
VLOGD("create", "./magiskpolicy", dest.data());
xsymlink("./magiskpolicy", dest.data());
}
create_and_mount(MAGISKTMP + "/" + name());
}
@ -532,13 +530,12 @@ static void inject_magisk_bins(root_node *system) {
// Insert binaries
bin->insert(new magisk_node("magisk"));
bin->insert(new magisk_node("magiskinit"));
bin->insert(new magisk_node("magiskpolicy"));
// Also delete all applets to make sure no modules can override it
for (int i = 0; applet_names[i]; ++i)
delete bin->extract(applet_names[i]);
for (int i = 0; init_applet[i]; ++i)
delete bin->extract(init_applet[i]);
delete bin->extract("supolicy");
}
vector<module_info> *module_list;

View File

@ -300,17 +300,11 @@ void MagiskInit::setup_tmp(const char *path) {
int fd = xopen(INTLROOT "/config", O_WRONLY | O_CREAT, 0);
xwrite(fd, magisk_cfg.buf, magisk_cfg.sz);
close(fd);
fd = xopen("magiskinit", O_WRONLY | O_CREAT, 0755);
xwrite(fd, self.buf, self.sz);
close(fd);
// The magisk binary will be handled later
// Create applet symlinks
for (int i = 0; applet_names[i]; ++i)
xsymlink("./magisk", applet_names[i]);
xsymlink("./magiskinit", "magiskpolicy");
xsymlink("./magiskinit", "supolicy");
xsymlink("./magiskpolicy", "supolicy");
chdir("/");
}

View File

@ -69,7 +69,6 @@ if [ -d /dev/avd-magisk ]; then
fi
# SELinux stuffs
ln -sf ./magiskinit magiskpolicy
if [ -f /vendor/etc/selinux/precompiled_sepolicy ]; then
./magiskpolicy --load /vendor/etc/selinux/precompiled_sepolicy --live --magisk 2>&1
elif [ -f /sepolicy ]; then
@ -125,19 +124,20 @@ mkdir $NVBASE/modules 2>/dev/null
mkdir $POSTFSDATAD 2>/dev/null
mkdir $SERVICED 2>/dev/null
for file in magisk32 magisk64 magiskinit; do
for file in magisk32 magisk64 magiskpolicy; do
chmod 755 ./$file
cp -af ./$file $MAGISKTMP/$file
cp -af ./$file $MAGISKBIN/$file
done
cp -af ./magiskboot $MAGISKBIN/magiskboot
cp -af ./magiskinit $MAGISKBIN/magiskinit
cp -af ./busybox $MAGISKBIN/busybox
ln -s ./magisk64 $MAGISKTMP/magisk
ln -s ./magisk $MAGISKTMP/su
ln -s ./magisk $MAGISKTMP/resetprop
ln -s ./magisk $MAGISKTMP/magiskhide
ln -s ./magiskinit $MAGISKTMP/magiskpolicy
ln -s ./magiskpolicy $MAGISKTMP/supolicy
mkdir -p $MAGISKTMP/.magisk/mirror
mkdir $MAGISKTMP/.magisk/block