Skip to content

kernel: prune redundant avtab nodes after deny rules#3439

Open
XiaoTong6666 wants to merge 3 commits intotiann:mainfrom
XiaoTong6666:pr
Open

kernel: prune redundant avtab nodes after deny rules#3439
XiaoTong6666 wants to merge 3 commits intotiann:mainfrom
XiaoTong6666:pr

Conversation

@XiaoTong6666
Copy link
Copy Markdown

Runtime deny rules clear permission bits from AVTAB_ALLOWED entries. When all permissions are removed from an existing entry, the update can leave an AVTAB_ALLOWED node with data == 0.

Such zero-permission avtab nodes are redundant and can make external policy parsers, such as sepatch, reject /sys/fs/selinux/policy with errors like:

Invalid access vector
Invalid avtab
Invalid policydb
policy image is invalid

This was observed when applying:

deny appdomain cgroup_v2 dir search

The rule itself is valid, but the runtime patch left a redundant avtab entry behind.

Avoid creating new entries for deny updates when the target entry does not exist, and prune redundant nodes after access-vector rule updates by rebuilding the avtab and destroying the old table through avtab_destroy().

This preserves deny semantics while keeping the live policy parseable by policydb-based tools.

Runtime deny rules clear permission bits from AVTAB_ALLOWED entries. When
all permissions are removed from an existing entry, the update can leave
an AVTAB_ALLOWED node with data == 0.

Such zero-permission avtab nodes are redundant and can make external
policy parsers, such as sepatch, reject /sys/fs/selinux/policy with
errors like:

  Invalid access vector
  Invalid avtab
  Invalid policydb
  policy image is invalid

This was observed when applying:

  deny appdomain cgroup_v2 dir search

The rule itself is valid, but the runtime patch left a redundant avtab
entry behind.

Avoid creating new entries for deny updates when the target entry does
not exist, and prune redundant nodes after access-vector rule updates by
rebuilding the avtab and destroying the old table through avtab_destroy().

This preserves deny semantics while keeping the live policy parseable by
policydb-based tools.
@XiaoTong6666
Copy link
Copy Markdown
Author

Modules that use sepatch, such as https://github.com/chenxiaolong/MSD, can trigger this issue.

@XiaoTong6666
Copy link
Copy Markdown
Author

XiaoTong6666 commented May 1, 2026

Magisk's sepolicy implementation appears to handle this case as well: it defines redundant avtab node detection and removes such nodes after rule updates when necessary.

See:
https://github.com/topjohnwu/Magisk/blob/master/native/src/sepolicy/sepolicy.cpp#L142-L151
https://github.com/topjohnwu/Magisk/blob/master/native/src/sepolicy/sepolicy.cpp#L226-L240

backslashxx pushed a commit to backslashxx/KernelSU that referenced this pull request May 2, 2026
Runtime deny rules clear permission bits from AVTAB_ALLOWED entries. When
all permissions are removed from an existing entry, the update can leave
an AVTAB_ALLOWED node with data == 0.

Such zero-permission avtab nodes are redundant and can make external
policy parsers, such as sepatch, reject /sys/fs/selinux/policy with
errors like:

  Invalid access vector
  Invalid avtab
  Invalid policydb
  policy image is invalid

This was observed when applying:

  deny appdomain cgroup_v2 dir search

The rule itself is valid, but the runtime patch left a redundant avtab
entry behind.

Avoid creating new entries for deny updates when the target entry does
not exist, and prune redundant nodes after access-vector rule updates by
rebuilding the avtab and destroying the old table through avtab_destroy().

This preserves deny semantics while keeping the live policy parseable by
policydb-based tools.
backslashxx pushed a commit to backslashxx/KernelSU that referenced this pull request May 2, 2026
Runtime deny rules clear permission bits from AVTAB_ALLOWED entries. When
all permissions are removed from an existing entry, the update can leave
an AVTAB_ALLOWED node with data == 0.

Such zero-permission avtab nodes are redundant and can make external
policy parsers, such as sepatch, reject /sys/fs/selinux/policy with
errors like:

  Invalid access vector
  Invalid avtab
  Invalid policydb
  policy image is invalid

This was observed when applying:

  deny appdomain cgroup_v2 dir search

The rule itself is valid, but the runtime patch left a redundant avtab
entry behind.

Avoid creating new entries for deny updates when the target entry does
not exist, and prune redundant nodes after access-vector rule updates by
rebuilding the avtab and destroying the old table through avtab_destroy().

This preserves deny semantics while keeping the live policy parseable by
policydb-based tools.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates KernelSU's live SELinux policy patching so runtime deny-style AV rule changes do not leave redundant zero-permission AVTAB entries behind, which helps keep the in-kernel policy export parseable by policydb-based tools.

Changes:

  • add helpers to detect and remove redundant AVTAB nodes after access-vector updates
  • change inverted rule handling so missing target entries are not created for deny-style updates
  • add failure checks when inserting AVTAB nodes for normal and type-rule updates

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread kernel/selinux/sepolicy.c Outdated
Comment thread kernel/selinux/sepolicy.c Outdated
Comment thread kernel/selinux/sepolicy.c Outdated
Comment thread kernel/selinux/sepolicy.c Outdated
Prune redundant avtab nodes by detaching the target node and releasing it
through a temporary avtab with avtab_destroy(), instead of rebuilding the
entire te_avtab.

Propagate add_rule_raw() and avtab removal failures to callers so failed
insertions or prune operations are not reported as successful updates.

Keep AVTAB_AUDITDENY updates able to create missing entries, while
missing deny-style AVTAB_ALLOWED entries remain no-ops.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread kernel/selinux/sepolicy.c
Comment thread kernel/selinux/sepolicy.c
Comment thread kernel/selinux/sepolicy.c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants