kernel: precise trigger timing of post-fs-data (#118)

* kernel: add report_event cmd

* ksud: report event

* kernel: trigger on_post_fs_data

* ksud: comment unused code

* [skip ci] run clang-format

Signed-off-by: Ylarod <me@ylarod.cn>

* ci: use custom key to sign official bootimgs

* format ksud

* reject non root

* remove

Signed-off-by: Ylarod <me@ylarod.cn>
This commit is contained in:
Ylarod
2023-01-26 11:29:02 +08:00
committed by GitHub
parent 22b66b6672
commit db600d5ea0
19 changed files with 170 additions and 37 deletions

View File

@@ -5,8 +5,9 @@
#include "linux/printk.h"
#include "linux/slab.h"
#include "selinux/selinux.h"
#include "klog.h" // IWYU pragma: keep
#include "selinux/selinux.h"
#define FILE_MAGIC 0x7f4b5355 // ' KSU', u32
#define FILE_FORMAT_VERSION 1 // u32
@@ -27,7 +28,8 @@ static struct work_struct ksu_load_work;
bool persistent_allow_list(void);
void ksu_show_allow_list(void){
void ksu_show_allow_list(void)
{
struct perm_data *p = NULL;
struct list_head *pos = NULL;
pr_info("ksu_show_allow_list");
@@ -179,7 +181,8 @@ void do_load_allow_list(struct work_struct *work)
#ifdef CONFIG_KSU_DEBUG
int errno = PTR_ERR(fp);
if (errno == -ENOENT) {
ksu_allow_uid(2000, true, true); // allow adb shell by default
ksu_allow_uid(2000, true,
true); // allow adb shell by default
} else {
pr_err("load_allow_list open file failed: %d\n",
PTR_ERR(fp));

View File

@@ -4,6 +4,7 @@
#include "linux/kernel.h"
#include "linux/kprobes.h"
#include "linux/lsm_hooks.h"
#include "linux/printk.h"
#include "linux/uaccess.h"
#include "linux/uidgid.h"
#include "linux/version.h"
@@ -15,11 +16,12 @@
#include "allowlist.h"
#include "arch.h"
#include "core_hook.h"
#include "klog.h" // IWYU pragma: keep
#include "ksu.h"
#include "ksud.h"
#include "manager.h"
#include "selinux/selinux.h"
#include "uid_observer.h"
#include "klog.h" // IWYU pragma: keep
static inline bool is_allow_su()
{
@@ -200,6 +202,34 @@ int ksu_handle_prctl(int option, unsigned long arg2, unsigned long arg3,
}
}
if (arg2 == CMD_REPORT_EVENT) {
if (0 != current_uid().val) {
return 0;
}
switch (arg3) {
case EVENT_POST_FS_DATA: {
static bool post_fs_data_lock = false;
if (!post_fs_data_lock) {
post_fs_data_lock = true;
pr_info("post-fs-data triggered");
on_post_fs_data();
}
break;
}
case EVENT_BOOT_COMPLETED: {
static bool boot_complete_lock = false;
if (!boot_complete_lock) {
boot_complete_lock = true;
pr_info("boot_complete triggered");
}
break;
}
default:
break;
}
return 0;
}
// all other cmds are for 'root manager'
if (!is_manager()) {
pr_info("Only manager can do cmd: %d\n", arg2);

View File

@@ -1,13 +1,13 @@
#include "linux/fs.h"
#include "linux/module.h"
#include "linux/workqueue.h"
#include "linux/fs.h"
#include "allowlist.h"
#include "arch.h"
#include "core_hook.h"
#include "klog.h" // IWYU pragma: keep
#include "ksu.h"
#include "uid_observer.h"
#include "klog.h" // IWYU pragma: keep
static struct workqueue_struct *ksu_workqueue;

View File

@@ -8,13 +8,16 @@
#define KERNEL_SU_OPTION 0xDEADBEEF
#define CMD_GRANT_ROOT 0
#define CMD_BECOME_MANAGER 1
#define CMD_GET_VERSION 2
#define CMD_ALLOW_SU 3
#define CMD_DENY_SU 4
#define CMD_GET_ALLOW_LIST 5
#define CMD_GET_DENY_LIST 6
#define CMD_REPORT_EVENT 7
#define EVENT_POST_FS_DATA 1
#define EVENT_BOOT_COMPLETED 2
bool ksu_queue_work(struct work_struct *work);

View File

@@ -4,6 +4,7 @@
#include "linux/err.h"
#include "linux/fs.h"
#include "linux/kprobes.h"
#include "linux/printk.h"
#include "linux/types.h"
#include "linux/uaccess.h"
#include "linux/version.h"
@@ -11,8 +12,8 @@
#include "allowlist.h"
#include "arch.h"
#include "selinux/selinux.h"
#include "klog.h" // IWYU pragma: keep
#include "selinux/selinux.h"
static const char KERNEL_SU_RC[] =
"\n"
@@ -47,6 +48,18 @@ static bool vfs_read_hook = true;
static bool execveat_hook = true;
#endif
void on_post_fs_data(void)
{
static bool done = false;
if (done) {
pr_info("on_post_fs_data already done");
return;
}
done = true;
pr_info("ksu_load_allow_list");
ksu_load_allow_list();
}
int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
void *argv, void *envp, int *flags)
{
@@ -85,7 +98,7 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
!memcmp(filename->name, app_process, sizeof(app_process) - 1)) {
first_app_process = false;
pr_info("exec app_process, /data prepared!\n");
ksu_load_allow_list();
on_post_fs_data(); // we keep this for old ksud
stop_execve_hook();
}

6
kernel/ksud.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef __KSU_H_KSUD
#define __KSU_H_KSUD
void on_post_fs_data(void);
#endif

View File

@@ -9,9 +9,9 @@
#include "linux/rcupdate.h"
#include "apk_sign.h"
#include "klog.h" // IWYU pragma: keep
#include "ksu.h"
#include "manager.h"
#include "klog.h" // IWYU pragma: keep
uid_t ksu_manager_uid = INVALID_UID;

View File

@@ -1,9 +1,9 @@
#include "linux/version.h"
#include "../klog.h" // IWYU pragma: keep
#include "selinux.h"
#include "sepolicy.h"
#include "ss/services.h"
#include "../klog.h" // IWYU pragma: keep
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0)
#define SELINUX_POLICY_INSTEAD_SELINUX_SS

View File

@@ -1,5 +1,5 @@
#include "objsec.h"
#include "selinux.h"
#include "objsec.h"
#include "../klog.h" // IWYU pragma: keep

View File

@@ -8,10 +8,10 @@
#include "linux/workqueue.h"
#include "allowlist.h"
#include "klog.h" // IWYU pragma: keep
#include "ksu.h"
#include "manager.h"
#include "uid_observer.h"
#include "klog.h" // IWYU pragma: keep
#define SYSTEM_PACKAGES_LIST_PATH "/data/system/packages.list"
static struct work_struct ksu_update_uid_work;