Tools

magiskpolicy

magiskpolicy patches Android's compiled SELinux policy, either a policy file or the policy loaded in the kernel. Magisk uses it to add its own rules at every boot, and modules use its statement format in sepolicy.rule files. This page covers every option and statement in v30.7 and v31.1, with a worked example from a real denial to a shipped rule.

Last updated 7 sourcesHow we check pages

What magiskpolicy does

On a desktop Linux system you would edit SELinux policy sources and recompile them. On Android, magiskpolicy patches the compiled binary policy instead. It loads a policy (by default the one running in the kernel), applies statements such as allow or type_transition, and either loads the result into the kernel or saves it to a file. Magisk's documentation sums up the policy on every Magisk install as the stock sepolicy plus magiskpolicy --magisk 'allow magisk * * *'.

Options

Usage: magiskpolicy [--options...] [policy statements...]. The options are the same in v30.7 and v31.1 (--print-rules is in the v30.7 binary even though v30.7's documentation left it out). Run as root.

OptionEffect
--load FILELoad a monolithic policy from a file
--load-splitLoad the precompiled policy, or compile the split CIL policies (Android 8.0 and later)
--compile-splitCompile the split CIL policies
--save FILEWrite the resulting monolithic policy to a file
--liveLoad the result into the kernel immediately
--magiskApply Magisk's built-in rules
--apply FILEApply statements from a file, one per line; blank lines and lines starting with # are skipped. Can be repeated.
--print-rulesPrint every rule in the loaded policy, then exit
--helpShow the options and the full statement help

With none of the load options it starts from the live policy at /sys/fs/selinux/policy. Only one load option may be given (Multiple load source supplied otherwise). The order of work is: load, --magisk, each --apply file, the statements on the command line, then --live and --save. Run with no arguments it prints the usage and exits with code 1.

Policy statements

Each statement is one argument, so quote it. Arguments marked ^ accept one value or a space-separated list in braces ({ a b }); arguments marked * also accept * for "all". A brace list expands into one rule per combination, so "allow { s1 s2 } { t1 t2 } class *" adds four rules, each with every permission of the class.

StatementForm (from the help text)Example
allow, deny, auditallow, dontaudit*source_type *target_type *class *perm_set"allow shell my_file file { read open getattr }"
allowxperm, auditallowxperm, dontauditxperm*source_type *target_type *class operation xperm_set (operation is always ioctl)"allowxperm my_app my_dev chr_file ioctl 0x8910"
permissive, enforce*type"permissive my_domain"
typeattribute^type ^attribute"typeattribute my_file file_type"
typetype_name ^(attribute); attribute defaults to domain"type my_file file_type"
attributeattribute_name"attribute my_attr"
type_transitionsource_type target_type class default_type (object_name)"type_transition my_domain app_data_file file my_file"
type_change, type_membersource_type target_type class default_type"type_change my_domain devpts chr_file my_pts"
genfsconfs_name partial_path fs_context"genfscon sysfs /devices/my_node u:object_r:my_sysfs:s0"

Extended permissions (xperm_set) are 16-bit hex values for ioctl commands: a single value (0x8910), a brace list of values and ranges ({ 0x8910-0x8926 0x892A-0x8935 }), a complement (~0x8910 or ~{ 0x8910 0x892A }), * for 0x0000 to 0xFFFF, or 0 to clear all rules for that target. The names in the examples above (my_file, my_domain and so on) are placeholders. Run magiskpolicy --help on the device for the statement list of your installed version.

Worked example: from a denial to a rule

From a denial to a rule that survives reboots

  1. 1Find the denialdmesg | grep 'avc: '
  2. 2Write the statementallow scontext tcontext tclass perm
  3. 3Test it livemagiskpolicy --live "..."
  4. 4Put it in sepolicy.ruleIn the module folder
  5. 5Rebootmagiskinit applies it before Android loads the policy
Denial format from AOSP's SELinux validation guide; rule handling from Magisk's module installer and magiskinit (native/src/init/selinux.rs) at tag v31.1, checked 26 October 2026.

1. Find the denial. SELinux logs each refusal to the kernel log and logcat with the prefix avc:. AOSP's guide shows this one:

Read denials as root
$adb shell su -c "dmesg | grep 'avc: '" avc: denied { connectto } for pid=2671 comm="ping" path="/dev/socket/dnsproxyd" scontext=u:r:shell:s0 tcontext=u:r:netd:s0 tclass=unix_stream_socket

2. Translate it. Take the type from scontext (the part after u:r:), the type from tcontext, the tclass, and the permission in braces. AOSP's audit2allow would print allow shell netd:unix_stream_socket connectto;; in magiskpolicy's format the class is a separate word and there is no semicolon:

3. Test it on the running system
#magiskpolicy --live "allow shell netd unix_stream_socket connectto" # several at once: one quoted argument each, or a file #magiskpolicy --live --apply /data/local/tmp/rules.txt

Repeat the action that failed. A new denial for a different permission is common; add it and try again. Permissions with the same source, target and class can share one statement: { read open getattr }.

4. Ship it. Put the working statements, one per line, in your module's sepolicy.rule (next section) and reboot. Live changes disappear at the next reboot.

In modules: sepolicy.rule

Modules shouldn't call magiskpolicy from boot scripts. Put statements in sepolicy.rule in the module folder, one per line. Here is what happens to them, from Magisk's installer and magiskinit source:

  1. When a module is installed, enabled, disabled or marked for removal, the installer function copy_preinit_files concatenates the sepolicy.rule of every enabled module into one file in Magisk's pre-init storage ($MAGISKTMP/.magisk/preinit/sepolicy.rule).
  2. At the next boot, magiskinit reads that file from the pre-init partition and applies it together with Magisk's built-in rules, before Android loads the policy. That is why rules work even for services that start before /data is decrypted.

If a phone has no usable pre-init partition, the installer prints Unable to find preinit dir and module rules cannot be applied at boot. Grant only what the module needs. Broad rules weaken the security model that keeps root contained. For the rest of the module format, see module development; for where pre-init storage lives, see how Magisk works.

Inspecting and patching policy files

Read-only checks and offline patching (as root)
# list the rules in the running policy that mention a type #magiskpolicy --print-rules | grep untrusted_app # save the running policy to a file #magiskpolicy --save /data/local/tmp/policy.bin # patch a policy file without touching the kernel #magiskpolicy --load sepolicy --save sepolicy.patched --magisk "allow my_domain my_file file read"

To see what a given process runs as, use ps -AZ; for files, ls -Z. Root commands need su first.

Version history

  • v30.3: support for the new binary policy format in Android 16 QPR2 (v30.7 added the matching support in magiskinit).
  • v31.0: fixed parsing of names that contain hyphens.
  • v31.1 (current stable): no option or statement changes; the documentation now lists --print-rules and the full xperm syntax that the binary already supported.

Errors and questions

Syntax error in: "..." followed by Hint:

The statement didn't parse. The hint line shows the expected form for that statement, for example allow *source_type *target_type *class *perm_set. Common causes: the whole statement wasn't quoted as one argument, a brace list is missing a space or a closing }, or the rule name is misspelled (that gives Unknown action). Hyphenated names failed to parse before v31.0.

source type ... does not exist or perm ... does not exist in class ...

The statement parsed, but the policy you loaded has no such type, class or permission, so that rule is skipped. Type names differ between Android versions and vendors. Check what exists with magiskpolicy --print-rules | grep name, or create the type first with a type statement.

I ran magiskpolicy and nothing changed

Without --live (load into the kernel) or --save FILE, magiskpolicy patches a copy in memory and throws it away. Add --live to apply immediately.

My module's sepolicy.rule isn't applied

Rules are applied at boot, so reboot first. Magisk does not read the file from the module folder at boot: it reads one combined copy in pre-init storage, which is rebuilt when a module is installed, enabled, disabled or marked for removal in the app. If you edited sepolicy.rule by hand, disable and re-enable the module (or reinstall it), then reboot. Disabled modules and modules pending removal or update are left out. Lines that fail to parse are skipped, so check them with --live first.

Should I just make a domain permissive, or set SELinux to permissive?

Neither is a good fix. permissive lifts every restriction on that domain, and setenforce 0 lifts them for the whole device. Both weaken the security model that keeps root contained and can be noticed by apps. Allow only the specific permission the denial names.

Can I run magiskpolicy on a PC?

Not the official build. The APK carries it as lib/<abi>/libmagiskpolicy.so, a dynamically linked Android executable, so it runs on the phone. To patch a policy file, copy it to the device and use --load and --save.

Why don't root shells hit SELinux denials?

Everything magiskd starts, including root shells, runs as u:r:magisk:s0, which Magisk's built-in rules make permissive and unconstrained. Denials you see come from other domains: the app, a system service or the files your module adds. See how Magisk works.

Sources

This page is written from the sources below. If anything here disagrees with them, the upstream source is correct.

Found a mistake or something out of date? Tell us and we will check it against the source and fix the page. Confirmed corrections are listed in the corrections log.

Report a problem with this page