Skip to content

fix(kernel): observer init error handling and sucompat error overwrite#3597

Open
EasyGuo114514 wants to merge 3 commits into
tiann:mainfrom
EasyGuo114514:fix/kernel-error-handling
Open

fix(kernel): observer init error handling and sucompat error overwrite#3597
EasyGuo114514 wants to merge 3 commits into
tiann:mainfrom
EasyGuo114514:fix/kernel-error-handling

Conversation

@EasyGuo114514

Copy link
Copy Markdown

Summary

  • Fix ksu_observer_init() silently ignoring watch_one_dir() failure
  • Fix escape_with_root_profile() error overwritten by execveat result

Details

1. ksu_observer_init returns error instead of always 0

ksu_observer_init() called watch_one_dir() and stored the return value in ret, but unconditionally returned 0 regardless of the result. Callers in init.c and boot_event.c had no way to detect the failure. Also fixes fsnotify_group leak on the error path.

2. escape_with_root_profile error overwritten by execveat result

In ksu_handle_execve_sucompat(), the return value of escape_with_root_profile() was stored in ret, which was immediately overwritten by the execveat result. A scoped local variable now keeps the profile error separate, ensuring sulog captures the correct status.

Changed files

  • kernel/manager/pkg_observer.c
  • kernel/feature/sucompat.c

Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 improves KernelSU kernel-side robustness and observability by fixing two error-handling issues: (1) propagating failures from the package observer initialization, and (2) preventing escape_with_root_profile() status from being overwritten before sucompat logging.

Changes:

  • Return and log watch_one_dir() failures from ksu_observer_init() instead of always returning success.
  • Preserve escape_with_root_profile()’s return value for sulog emission, independent of the later execveat() result.

Reviewed changes

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

File Description
kernel/manager/pkg_observer.c Adds error handling for observer init; needs additional safety to avoid double-put/UAF on module exit when init fails.
kernel/feature/sucompat.c Separates profile-escape status from execveat result for correct sucompat sulog reporting; minor type cleanup recommended.

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

Comment on lines +115 to +119
if (ret) {
pr_err("observer init failed: %d\n", ret);
fsnotify_put_group(g);
return ret;
}
Comment thread kernel/feature/sucompat.c
ksu_observer_init() called watch_one_dir() and stored the return value
in 'ret', but unconditionally returned 0 regardless of the result.
If the /data/system directory could not be watched (e.g., path not
ready, inode mark registration failed), the caller had no way to
detect the failure and would assume the observer was operational.

Return the actual error code and properly release the fsnotify group
on failure to prevent a resource leak.

Signed-off-by: EasyGuo114514 <2025520491@qq.com>
…itten

In ksu_handle_execve_sucompat(), the return value of
escape_with_root_profile() was stored in 'ret', which was then
immediately overwritten by the execveat result. If the profile
escape failed, the su execution would still proceed as if it
succeeded, potentially running with incorrect privileges.

Use a scoped local variable to keep the profile error separate from
the execveat result, ensuring the sulog captures the correct status
without interfering with execution flow.

Signed-off-by: EasyGuo114514 <2025520491@qq.com>
… mismatch

- Set g = NULL after fsnotify_put_group in ksu_observer_init error path
  to prevent use-after-free in ksu_observer_exit on module unload
- Change profile_ret from long to int to match escape_with_root_profile
  return type and ksu_sulog_emit_pending parameter type

Signed-off-by: EasyGuo114514 <2025520491@qq.com>
@EasyGuo114514
EasyGuo114514 force-pushed the fix/kernel-error-handling branch from 9fd27ff to b98dc04 Compare July 23, 2026 11:25
@EasyGuo114514

Copy link
Copy Markdown
Author

fix it

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.

2 participants