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 * * *'.
- Also called
supolicyAlias kept for SuperSU compatibility - Default inputLive policy
/sys/fs/ selinux/ policy - Output
--liveor--saveWithout one, nothing is applied - Live changes lastUntil rebootShip them in
sepolicy.rule - Root shells run as
u:r:magisk:s0Already unrestricted
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.
| Option | Effect |
|---|---|
--load FILE | Load a monolithic policy from a file |
--load-split | Load the precompiled policy, or compile the split CIL policies (Android 8.0 and later) |
--compile-split | Compile the split CIL policies |
--save FILE | Write the resulting monolithic policy to a file |
--live | Load the result into the kernel immediately |
--magisk | Apply Magisk's built-in rules |
--apply FILE | Apply statements from a file, one per line; blank lines and lines starting with # are skipped. Can be repeated. |
--print-rules | Print every rule in the loaded policy, then exit |
--help | Show 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.
| Statement | Form (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" |
type | type_name ^(attribute); attribute defaults to domain | "type my_file file_type" |
attribute | attribute_name | "attribute my_attr" |
type_transition | source_type target_type class default_type (object_name) | "type_transition my_domain app_data_file file my_file" |
type_change, type_member | source_type target_type class default_type | "type_change my_domain devpts chr_file my_pts" |
genfscon | fs_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
- 1Find the denial
dmesg | grep 'avc: ' - 2Write the statement
allow scontext tcontext tclass perm - 3Test it live
magiskpolicy --live "..." - 4Put it in sepolicy.ruleIn the module folder
- 5Rebootmagiskinit applies it before Android loads the policy
1. Find the denial. SELinux logs each refusal to the kernel log and logcat with the prefix avc:. AOSP's guide shows this one:
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:
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:
- When a module is installed, enabled, disabled or marked for removal, the installer function
copy_preinit_filesconcatenates thesepolicy.ruleof every enabled module into one file in Magisk's pre-init storage ($MAGISKTMP/)..magisk/ preinit/ sepolicy.rule - At the next boot,
magiskinitreads 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/datais 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
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-rulesand 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.
Cannot print rules with other options
--print-rules only reads. It can be combined with a load option (--load, --load-split, --compile-split) but not with --magisk, --apply, statements, --live or --save.
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.
No match. Try another word from the error message.
Sources
This page is written from the sources below. If anything here disagrees with them, the upstream source is correct.
- Magisk tools reference: magiskpolicy (topjohnwu)
- Magisk internal details: SELinux policies (topjohnwu)
- Magisk developer guide: sepolicy.rule (topjohnwu)
- Magisk source: magiskpolicy options, native/src/sepolicy/cli.rs (topjohnwu)
- Magisk source: policy statement parser and help text, native/src/sepolicy/statement.rs (topjohnwu)
- Magisk source: module installer, copy_preinit_files in scripts/util_functions.sh (topjohnwu)
- Validating SELinux: reading denials (AOSP)
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