kernel: fmt

This commit is contained in:
weishu
2022-12-11 21:59:57 +07:00
parent a0d91bcf8b
commit 730a522f87

View File

@@ -1,30 +1,30 @@
#include "linux/uidgid.h"
#include <linux/cpu.h> #include <linux/cpu.h>
#include <linux/memory.h> #include <linux/errno.h>
#include <linux/uaccess.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/module.h>
#include <linux/kprobes.h>
#include <linux/printk.h>
#include <linux/string.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/kprobes.h>
#include <linux/memory.h>
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <asm-generic/errno-base.h> #include <linux/string.h>
#include <linux/uaccess.h>
#include <linux/uidgid.h>
#include <linux/rcupdate.h>
#include <linux/fdtable.h> #include <linux/fdtable.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/fs_struct.h> #include <linux/fs_struct.h>
#include <linux/namei.h> #include <linux/namei.h>
#include <linux/rcupdate.h>
#include <linux/delay.h> // msleep #include <linux/delay.h> // msleep
#include "klog.h" #include "klog.h"
struct perm_data { struct perm_data {
struct list_head list; struct list_head list;
uid_t uid; uid_t uid;
bool allow; bool allow;
}; };
static struct list_head allow_list; static struct list_head allow_list;
@@ -35,177 +35,172 @@ static struct workqueue_struct *ksu_workqueue;
static struct work_struct ksu_save_work; static struct work_struct ksu_save_work;
static struct work_struct ksu_load_work; static struct work_struct ksu_load_work;
bool persistent_allow_list(); bool persistent_allow_list(void);
bool ksu_allow_uid(uid_t uid, bool allow) { bool ksu_allow_uid(uid_t uid, bool allow) {
// find the node first! // find the node first!
struct perm_data* p; struct perm_data *p = NULL;
struct list_head* pos; struct list_head *pos = NULL;
bool result; bool result = false;
list_for_each(pos, &allow_list) { list_for_each(pos, &allow_list) {
p = list_entry(pos, struct perm_data, list); p = list_entry(pos, struct perm_data, list);
pr_info("ksu_allow_uid :%d, allow: %d\n", p->uid, p->allow); pr_info("ksu_allow_uid :%d, allow: %d\n", p->uid, p->allow);
if (uid == p->uid) { if (uid == p->uid) {
p->allow = allow; p->allow = allow;
result = true; result = true;
goto exit; goto exit;
}
} }
}
// not found, alloc a new node! // not found, alloc a new node!
p = (struct perm_data*) kmalloc(sizeof(struct perm_data), GFP_KERNEL); p = (struct perm_data *)kmalloc(sizeof(struct perm_data), GFP_KERNEL);
if (!p) { if (!p) {
pr_err("alloc allow node failed.\n"); pr_err("alloc allow node failed.\n");
return false; return false;
} }
p->uid = uid; p->uid = uid;
p->allow = allow; p->allow = allow;
list_add_tail(&p->list, &allow_list); list_add_tail(&p->list, &allow_list);
result = true; result = true;
exit: exit:
persistent_allow_list(); persistent_allow_list();
return result; return result;
} }
bool ksu_is_allow_uid(uid_t uid) { bool ksu_is_allow_uid(uid_t uid) {
struct perm_data* p; struct perm_data *p = NULL;
struct list_head* pos; struct list_head *pos = NULL;
list_for_each(pos, &allow_list) { list_for_each(pos, &allow_list) {
p = list_entry(pos, struct perm_data, list); p = list_entry(pos, struct perm_data, list);
pr_info("uid :%d, allow: %d\n", p->uid, p->allow); pr_info("uid :%d, allow: %d\n", p->uid, p->allow);
if (uid == p->uid) { if (uid == p->uid) {
return p->allow; return p->allow;
}
} }
}
return false; return false;
} }
bool ksu_get_allow_list(int* array, int* length, bool allow) { bool ksu_get_allow_list(int *array, int *length, bool allow) {
struct perm_data* p; struct perm_data *p = NULL;
struct list_head* pos; struct list_head *pos = NULL;
int i = 0; int i = 0;
list_for_each(pos, &allow_list) { list_for_each(pos, &allow_list) {
p = list_entry(pos, struct perm_data, list); p = list_entry(pos, struct perm_data, list);
pr_info("uid: %d allow: %d\n", p->uid, p->allow); pr_info("uid: %d allow: %d\n", p->uid, p->allow);
if (p->allow == allow) { if (p->allow == allow) {
array[i++] = p->uid; array[i++] = p->uid;
}
} }
*length = i; }
*length = i;
return true; return true;
} }
void do_persistent_allow_list(struct work_struct *work) {
struct perm_data *p = NULL;
struct list_head *pos = NULL;
loff_t off = 0;
struct file *fp = filp_open("/data/adb/ksu_list", O_WRONLY | O_CREAT, 0644);
if (IS_ERR(fp)) {
pr_err("work creat file failed: %d\n", PTR_ERR(fp));
return;
}
pr_info("work create file success!\n");
list_for_each(pos, &allow_list) {
p = list_entry(pos, struct perm_data, list);
pr_info("uid :%d, allow: %d\n", p->uid, p->allow);
kernel_write(fp, &p->uid, sizeof(p->uid), &off);
kernel_write(fp, &p->allow, sizeof(p->allow), &off);
}
void do_persistent_allow_list(struct work_struct *work) filp_close(fp, 0);
{
struct perm_data* p;
struct list_head* pos;
loff_t off;
struct file* fp = filp_open("/data/adb/ksu_list", O_WRONLY|O_CREAT, 0644);
if (IS_ERR(fp)) {
pr_err("work creat file failed: %d\n", PTR_ERR(fp));
return;
}
pr_info("work create file success!\n");
list_for_each(pos, &allow_list) {
p = list_entry(pos, struct perm_data, list);
pr_info("uid :%d, allow: %d\n", p->uid, p->allow);
kernel_write(fp, &p->uid, sizeof(p->uid), &off);
kernel_write(fp, &p->allow, sizeof(p->allow), &off);
}
filp_close(fp, 0);
} }
void do_load_allow_list(struct work_struct *work) { void do_load_allow_list(struct work_struct *work) {
loff_t off; loff_t off = 0;
ssize_t ret; ssize_t ret = 0;
struct file* fp; struct file *fp = NULL;
fp = filp_open("/data/adb/", O_RDONLY, 0); fp = filp_open("/data/adb/", O_RDONLY, 0);
if (IS_ERR(fp)) { if (IS_ERR(fp)) {
pr_err("work open '/data/adb' failed: %d\n", PTR_ERR(fp)); pr_err("work open '/data/adb' failed: %d\n", PTR_ERR(fp));
mdelay(2000); mdelay(2000);
queue_work(ksu_workqueue, &ksu_load_work); queue_work(ksu_workqueue, &ksu_load_work);
return; return;
} }
filp_close(fp, 0); filp_close(fp, 0);
// load allowlist now! // load allowlist now!
fp = filp_open("/data/adb/ksu_list", O_RDONLY, 0); fp = filp_open("/data/adb/ksu_list", O_RDONLY, 0);
if (IS_ERR(fp)) { if (IS_ERR(fp)) {
pr_err("work open file failed: %d\n", PTR_ERR(fp)); pr_err("work open file failed: %d\n", PTR_ERR(fp));
return; return;
} }
pr_info("work open file success!\n"); pr_info("work open file success!\n");
while (true) { while (true) {
u32 uid; u32 uid;
bool allow; bool allow = false;
ret = kernel_read(fp, &uid, sizeof(uid), &off); ret = kernel_read(fp, &uid, sizeof(uid), &off);
pr_info("kernel read ret: %d, off: %ld\n", ret, off); pr_info("kernel read ret: %d, off: %ld\n", ret, off);
if (ret <= 0) { if (ret <= 0) {
pr_info("read err: %d\n", ret); pr_info("read err: %d\n", ret);
break; break;
} }
ret = kernel_read(fp, &allow, sizeof(allow), &off); ret = kernel_read(fp, &allow, sizeof(allow), &off);
pr_info("load_allow_uid: %d, allow: %d\n", uid, allow); pr_info("load_allow_uid: %d, allow: %d\n", uid, allow);
ksu_allow_uid(uid, allow); ksu_allow_uid(uid, allow);
} }
filp_close(fp, 0); filp_close(fp, 0);
} }
static int init_work(void) { static int init_work(void) {
ksu_workqueue = alloc_workqueue("kernelsu_work", 0, 0); ksu_workqueue = alloc_workqueue("kernelsu_work", 0, 0);
INIT_WORK(&ksu_save_work, do_persistent_allow_list); INIT_WORK(&ksu_save_work, do_persistent_allow_list);
INIT_WORK(&ksu_load_work, do_load_allow_list); INIT_WORK(&ksu_load_work, do_load_allow_list);
return 0; return 0;
} }
// make sure allow list works cross boot // make sure allow list works cross boot
bool persistent_allow_list() { bool persistent_allow_list(void) {
queue_work(ksu_workqueue, &ksu_save_work); queue_work(ksu_workqueue, &ksu_save_work);
return true; return true;
} }
bool load_allow_list() { bool load_allow_list(void) {
queue_work(ksu_workqueue, &ksu_load_work); queue_work(ksu_workqueue, &ksu_load_work);
return true; return true;
} }
bool ksu_allowlist_init() { bool ksu_allowlist_init(void) {
INIT_LIST_HEAD(&allow_list); INIT_LIST_HEAD(&allow_list);
init_work(); init_work();
// load_allow_list(); // load_allow_list();
return true; return true;
} }
bool ksu_allowlist_exit() { bool ksu_allowlist_exit(void) {
destroy_workqueue(ksu_workqueue); destroy_workqueue(ksu_workqueue);
return true; return true;
} }