-
-
Notifications
You must be signed in to change notification settings - Fork 3k
selinux: Cache SID lookups for domain checks #3128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The is_ksu_domain(), is_zygote(), and is_init() functions are called frequently throughout KernelSU's operation - on every prctl(), execve(), and setresuid() hook invocation. Previously, each call performed: 1. __security_secid_to_secctx() to convert task's SID to context string 2. Memory allocation for the context buffer 3. strncmp() for string comparison against domain names 4. __security_release_secctx() to free the allocated buffer This is inefficient for hot paths that may be called hundreds of times per boot cycle. Optimize by caching the SELinux SIDs for su, zygote, and init domains once during ksu_selinux_init() (called at post-fs-data). Subsequent checks use simple u32 integer comparison against tsec->sid, which is approximately 100x faster than string-based comparison. The implementation includes a fallback to string comparison if the cache hasn't been initialized yet (early boot edge case). Performance impact: - Before: O(n) string comparison + memory allocation per check - After: O(1) integer comparison with zero allocations Signed-off-by: 0xSecureByte <[email protected]>
There was a problem hiding this 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 optimizes SELinux domain checking by caching Security ID (SID) values for frequently checked contexts (kernel su, zygote, and init) at initialization time. This eliminates expensive string-based context lookups on every domain check, replacing them with fast integer comparisons.
Key changes:
- Introduces cached SID variables that are resolved once during post-fs-data initialization
- Refactors domain checking functions to use a common
is_sid_match()helper with fast-path SID comparison - Maintains backward compatibility with a fallback to string comparison if caching fails
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
| kernel/selinux/selinux.h | Adds ZYGOTE_CONTEXT and INIT_CONTEXT macro definitions and declares the new ksu_selinux_init() function |
| kernel/selinux/selinux.c | Implements SID caching mechanism with cached variables, initialization function, and refactored comparison logic using is_sid_match() helper |
| kernel/ksud.c | Integrates ksu_selinux_init() call into the post-fs-data initialization sequence |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
|
|
Bill sheet |
|
What if the sepolicy is reloaded? |
|
sidtab_convert_tree() keeps original sid order, so no need to worry about it. |
Signed-off-by: 0xSecureByte <[email protected]>
|
Mostly fine now, but can you rename ksu_selinux_init to sth like cache_sid()? |
|
Also, can you migrate ksu_file_sid = ksu_get_ksu_file_sid() to the new function too? |
|
Sure, let me do the changes in sometime |
Signed-off-by: 0xSecureByte <[email protected]>
Cache SELinux SIDs for su/zygote/init contexts at init time instead of resolving them on every domain check. Reduces overhead from string-based lookups to simple integer comparisons. --------- Signed-off-by: 0xSecureByte <[email protected]> Co-authored-by: Copilot <[email protected]>
Cache SELinux SIDs for su/zygote/init contexts at init time instead of resolving them on every domain check. Reduces overhead from string-based lookups to simple integer comparisons. --------- Signed-off-by: 0xSecureByte <[email protected]> Co-authored-by: Copilot <[email protected]>
Cache SELinux SIDs for su/zygote/init contexts at init time instead of resolving them on every domain check. Reduces overhead from string-based lookups to simple integer comparisons. --------- Signed-off-by: 0xSecureByte <[email protected]> Co-authored-by: Copilot <[email protected]>
…1051) Cache SELinux SIDs for su/zygote/init contexts at init time instead of resolving them on every domain check. Reduces overhead from string-based lookups to simple integer comparisons. --------- Signed-off-by: 0xSecureByte <[email protected]> Co-authored-by: 0xSecureByte <[email protected]> Co-authored-by: Copilot <[email protected]>
…1051) Cache SELinux SIDs for su/zygote/init contexts at init time instead of resolving them on every domain check. Reduces overhead from string-based lookups to simple integer comparisons. --------- Signed-off-by: 0xSecureByte <[email protected]> Co-authored-by: Copilot <[email protected]>
…ernelSU-Next#1051) Cache SELinux SIDs for su/zygote/init contexts at init time instead of resolving them on every domain check. Reduces overhead from string-based lookups to simple integer comparisons. --------- Signed-off-by: 0xSecureByte <[email protected]> Co-authored-by: Copilot <[email protected]>
…ernelSU-Next#1051) Cache SELinux SIDs for su/zygote/init contexts at init time instead of resolving them on every domain check. Reduces overhead from string-based lookups to simple integer comparisons. --------- Signed-off-by: 0xSecureByte <[email protected]> Co-authored-by: Copilot <[email protected]>
Cache SELinux SIDs for su/zygote/init contexts at init time instead of resolving them on every domain check. Reduces overhead from string-based lookups to simple integer comparisons. --------- Signed-off-by: 0xSecureByte <[email protected]> Co-authored-by: Copilot <[email protected]>
Cache SELinux SIDs for su/zygote/init contexts at init time instead of resolving them on every domain check. Reduces overhead from string-based lookups to simple integer comparisons. --------- Signed-off-by: 0xSecureByte <[email protected]> Co-authored-by: Copilot <[email protected]>
Cache SELinux SIDs for su/zygote/init contexts at init time instead of resolving them on every domain check. Reduces overhead from string-based lookups to simple integer comparisons. --------- Signed-off-by: 0xSecureByte <[email protected]> Co-authored-by: Copilot <[email protected]>
Cache SELinux SIDs for su/zygote/init contexts at init time instead of resolving them on every domain check. Reduces overhead from string-based lookups to simple integer comparisons. --------- Signed-off-by: 0xSecureByte <[email protected]> Co-authored-by: Copilot <[email protected]>
Cache SELinux SIDs for su/zygote/init contexts at init time instead of resolving them on every domain check. Reduces overhead from string-based lookups to simple integer comparisons. --------- Signed-off-by: 0xSecureByte <[email protected]> Co-authored-by: Copilot <[email protected]>
Cache SELinux SIDs for su/zygote/init contexts at init time instead of resolving them on every domain check. Reduces overhead from string-based lookups to simple integer comparisons. --------- Signed-off-by: 0xSecureByte <[email protected]> Co-authored-by: Copilot <[email protected]>
Cache SELinux SIDs for su/zygote/init contexts at init time instead of resolving them on every domain check. Reduces overhead from string-based lookups to simple integer comparisons.