Skip to content

Commit bc35239

Browse files
committed
kernel: remove ksu_compat_{open,read,write} because we're in the right context now
1 parent e4aca5f commit bc35239

File tree

6 files changed

+94
-194
lines changed

6 files changed

+94
-194
lines changed

kernel/allowlist.c

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ static DEFINE_MUTEX(allowlist_mutex);
2929
static struct root_profile default_root_profile;
3030
static struct non_root_profile default_non_root_profile;
3131

32-
static int allow_list_arr[PAGE_SIZE / sizeof(int)] __read_mostly __aligned(PAGE_SIZE);
32+
static int allow_list_arr[PAGE_SIZE / sizeof(int)] __read_mostly
33+
__aligned(PAGE_SIZE);
3334
static int allow_list_pointer __read_mostly = 0;
3435

3536
static void remove_uid_from_arr(uid_t uid)
@@ -70,7 +71,7 @@ static void init_default_profiles()
7071
default_root_profile.groups_count = 1;
7172
default_root_profile.groups[0] = 0;
7273
memcpy(&default_root_profile.capabilities.effective, &full_cap,
73-
sizeof(default_root_profile.capabilities.effective));
74+
sizeof(default_root_profile.capabilities.effective));
7475
default_root_profile.namespaces = 0;
7576
strcpy(default_root_profile.selinux_domain, KSU_DEFAULT_SELINUX_DOMAIN);
7677

@@ -100,7 +101,7 @@ void ksu_show_allow_list(void)
100101
list_for_each (pos, &allow_list) {
101102
p = list_entry(pos, struct perm_data, list);
102103
pr_info("uid :%d, allow: %d\n", p->profile.current_uid,
103-
p->profile.allow_su);
104+
p->profile.allow_su);
104105
}
105106
}
106107

@@ -113,7 +114,8 @@ static void ksu_grant_root_to_shell()
113114
.current_uid = 2000,
114115
};
115116
strcpy(profile.key, "com.android.shell");
116-
strcpy(profile.rp_config.profile.selinux_domain, KSU_DEFAULT_SELINUX_DOMAIN);
117+
strcpy(profile.rp_config.profile.selinux_domain,
118+
KSU_DEFAULT_SELINUX_DOMAIN);
117119
ksu_set_app_profile(&profile, false);
118120
}
119121
#endif
@@ -139,9 +141,10 @@ bool ksu_get_app_profile(struct app_profile *profile)
139141
return found;
140142
}
141143

142-
static inline bool forbid_system_uid(uid_t uid) {
143-
#define SHELL_UID 2000
144-
#define SYSTEM_UID 1000
144+
static inline bool forbid_system_uid(uid_t uid)
145+
{
146+
#define SHELL_UID 2000
147+
#define SYSTEM_UID 1000
145148
return uid < SHELL_UID && uid != SYSTEM_UID;
146149
}
147150

@@ -202,22 +205,24 @@ bool ksu_set_app_profile(struct app_profile *profile, bool persist)
202205
memcpy(&p->profile, profile, sizeof(*profile));
203206
if (profile->allow_su) {
204207
pr_info("set root profile, key: %s, uid: %d, gid: %d, context: %s\n",
205-
profile->key, profile->current_uid,
206-
profile->rp_config.profile.gid,
207-
profile->rp_config.profile.selinux_domain);
208+
profile->key, profile->current_uid,
209+
profile->rp_config.profile.gid,
210+
profile->rp_config.profile.selinux_domain);
208211
} else {
209212
pr_info("set app profile, key: %s, uid: %d, umount modules: %d\n",
210-
profile->key, profile->current_uid,
211-
profile->nrp_config.profile.umount_modules);
213+
profile->key, profile->current_uid,
214+
profile->nrp_config.profile.umount_modules);
212215
}
213216
list_add_tail(&p->list, &allow_list);
214217

215218
out:
216219
if (profile->current_uid <= BITMAP_UID_MAX) {
217220
if (profile->allow_su)
218-
allow_list_bitmap[profile->current_uid / BITS_PER_BYTE] |= 1 << (profile->current_uid % BITS_PER_BYTE);
221+
allow_list_bitmap[profile->current_uid / BITS_PER_BYTE] |=
222+
1 << (profile->current_uid % BITS_PER_BYTE);
219223
else
220-
allow_list_bitmap[profile->current_uid / BITS_PER_BYTE] &= ~(1 << (profile->current_uid % BITS_PER_BYTE));
224+
allow_list_bitmap[profile->current_uid / BITS_PER_BYTE] &=
225+
~(1 << (profile->current_uid % BITS_PER_BYTE));
221226
} else {
222227
if (profile->allow_su) {
223228
/*
@@ -264,13 +269,15 @@ bool __ksu_is_allow_uid(uid_t uid)
264269
return false;
265270
}
266271

267-
if (likely(ksu_is_manager_uid_valid()) && unlikely(ksu_get_manager_uid() == uid)) {
272+
if (likely(ksu_is_manager_uid_valid()) &&
273+
unlikely(ksu_get_manager_uid() == uid)) {
268274
// manager is always allowed!
269275
return true;
270276
}
271277

272278
if (likely(uid <= BITMAP_UID_MAX)) {
273-
return !!(allow_list_bitmap[uid / BITS_PER_BYTE] & (1 << (uid % BITS_PER_BYTE)));
279+
return !!(allow_list_bitmap[uid / BITS_PER_BYTE] &
280+
(1 << (uid % BITS_PER_BYTE)));
274281
} else {
275282
for (i = 0; i < allow_list_pointer; i++) {
276283
if (allow_list_arr[i] == uid)
@@ -293,7 +300,8 @@ bool __ksu_is_allow_uid_for_current(uid_t uid)
293300
bool ksu_uid_should_umount(uid_t uid)
294301
{
295302
struct app_profile profile = { .current_uid = uid };
296-
if (likely(ksu_is_manager_uid_valid()) && unlikely(ksu_get_manager_uid() == uid)) {
303+
if (likely(ksu_is_manager_uid_valid()) &&
304+
unlikely(ksu_get_manager_uid() == uid)) {
297305
// we should not umount on manager!
298306
return false;
299307
}
@@ -366,26 +374,22 @@ void persistent_allow_list()
366374
}
367375

368376
// store magic and version
369-
if (ksu_kernel_write_compat(fp, &magic, sizeof(magic), &off) !=
370-
sizeof(magic)) {
377+
if (kernel_write(fp, &magic, sizeof(magic), &off) != sizeof(magic)) {
371378
pr_err("save_allow_list write magic failed.\n");
372379
goto exit;
373380
}
374381

375-
if (ksu_kernel_write_compat(fp, &version, sizeof(version), &off) !=
376-
sizeof(version)) {
382+
if (kernel_write(fp, &version, sizeof(version), &off) != sizeof(version)) {
377383
pr_err("save_allow_list write version failed.\n");
378384
goto exit;
379385
}
380386

381387
list_for_each (pos, &allow_list) {
382388
p = list_entry(pos, struct perm_data, list);
383389
pr_info("save allow list, name: %s uid :%d, allow: %d\n",
384-
p->profile.key, p->profile.current_uid,
385-
p->profile.allow_su);
390+
p->profile.key, p->profile.current_uid, p->profile.allow_su);
386391

387-
ksu_kernel_write_compat(fp, &p->profile, sizeof(p->profile),
388-
&off);
392+
kernel_write(fp, &p->profile, sizeof(p->profile), &off);
389393
}
390394

391395
exit:
@@ -413,15 +417,13 @@ void ksu_load_allow_list()
413417
}
414418

415419
// verify magic
416-
if (ksu_kernel_read_compat(fp, &magic, sizeof(magic), &off) !=
417-
sizeof(magic) ||
420+
if (kernel_read(fp, &magic, sizeof(magic), &off) != sizeof(magic) ||
418421
magic != FILE_MAGIC) {
419422
pr_err("allowlist file invalid: %d!\n", magic);
420423
goto exit;
421424
}
422425

423-
if (ksu_kernel_read_compat(fp, &version, sizeof(version), &off) !=
424-
sizeof(version)) {
426+
if (kernel_read(fp, &version, sizeof(version), &off) != sizeof(version)) {
425427
pr_err("allowlist read version: %d failed\n", version);
426428
goto exit;
427429
}
@@ -431,16 +433,15 @@ void ksu_load_allow_list()
431433
while (true) {
432434
struct app_profile profile;
433435

434-
ret = ksu_kernel_read_compat(fp, &profile, sizeof(profile),
435-
&off);
436+
ret = kernel_read(fp, &profile, sizeof(profile), &off);
436437

437438
if (ret <= 0) {
438439
pr_info("load_allow_list read err: %zd\n", ret);
439440
break;
440441
}
441442

442-
pr_info("load_allow_uid, name: %s, uid: %d, allow: %d\n",
443-
profile.key, profile.current_uid, profile.allow_su);
443+
pr_info("load_allow_uid, name: %s, uid: %d, allow: %d\n", profile.key,
444+
profile.current_uid, profile.allow_su);
444445
ksu_set_app_profile(&profile, false);
445446
}
446447

@@ -449,7 +450,8 @@ void ksu_load_allow_list()
449450
filp_close(fp, 0);
450451
}
451452

452-
void ksu_prune_allowlist(bool (*is_uid_valid)(uid_t, char *, void *), void *data)
453+
void ksu_prune_allowlist(bool (*is_uid_valid)(uid_t, char *, void *),
454+
void *data)
453455
{
454456
struct perm_data *np = NULL;
455457
struct perm_data *n = NULL;
@@ -467,7 +469,8 @@ void ksu_prune_allowlist(bool (*is_uid_valid)(uid_t, char *, void *), void *data
467469
pr_info("prune uid: %d, package: %s\n", uid, package);
468470
list_del(&np->list);
469471
if (likely(uid <= BITMAP_UID_MAX)) {
470-
allow_list_bitmap[uid / BITS_PER_BYTE] &= ~(1 << (uid % BITS_PER_BYTE));
472+
allow_list_bitmap[uid / BITS_PER_BYTE] &=
473+
~(1 << (uid % BITS_PER_BYTE));
471474
}
472475
remove_uid_from_arr(uid);
473476
smp_mb();

kernel/apk_sign.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,19 @@ static int ksu_sha256(const unsigned char *data, unsigned int datalen,
7474
static bool check_block(struct file *fp, u32 *size4, loff_t *pos, u32 *offset,
7575
unsigned expected_size, const char *expected_sha256)
7676
{
77-
ksu_kernel_read_compat(fp, size4, 0x4, pos); // signer-sequence length
78-
ksu_kernel_read_compat(fp, size4, 0x4, pos); // signer length
79-
ksu_kernel_read_compat(fp, size4, 0x4, pos); // signed data length
77+
kernel_read(fp, size4, 0x4, pos); // signer-sequence length
78+
kernel_read(fp, size4, 0x4, pos); // signer length
79+
kernel_read(fp, size4, 0x4, pos); // signed data length
8080

8181
*offset += 0x4 * 3;
8282

83-
ksu_kernel_read_compat(fp, size4, 0x4, pos); // digests-sequence length
83+
kernel_read(fp, size4, 0x4, pos); // digests-sequence length
8484

8585
*pos += *size4;
8686
*offset += 0x4 + *size4;
8787

88-
ksu_kernel_read_compat(fp, size4, 0x4, pos); // certificates length
89-
ksu_kernel_read_compat(fp, size4, 0x4, pos); // certificate length
88+
kernel_read(fp, size4, 0x4, pos); // certificates length
89+
kernel_read(fp, size4, 0x4, pos); // certificate length
9090
*offset += 0x4 * 2;
9191

9292
if (*size4 == expected_size) {
@@ -98,7 +98,7 @@ static bool check_block(struct file *fp, u32 *size4, loff_t *pos, u32 *offset,
9898
pr_info("cert length overlimit\n");
9999
return false;
100100
}
101-
ksu_kernel_read_compat(fp, cert, *size4, pos);
101+
kernel_read(fp, cert, *size4, pos);
102102
unsigned char digest[SHA256_DIGEST_SIZE];
103103
if (IS_ERR(ksu_sha256(cert, *size4, digest))) {
104104
pr_info("sha256 error\n");
@@ -140,7 +140,7 @@ static bool has_v1_signature_file(struct file *fp)
140140

141141
loff_t pos = 0;
142142

143-
while (ksu_kernel_read_compat(fp, &header,
143+
while (kernel_read(fp, &header,
144144
sizeof(struct zip_entry_header), &pos) ==
145145
sizeof(struct zip_entry_header)) {
146146
if (header.signature != 0x04034b50) {
@@ -150,7 +150,7 @@ static bool has_v1_signature_file(struct file *fp)
150150
// Read the entry file name
151151
if (header.file_name_length == sizeof(MANIFEST) - 1) {
152152
char fileName[sizeof(MANIFEST)];
153-
ksu_kernel_read_compat(fp, fileName,
153+
kernel_read(fp, fileName,
154154
header.file_name_length, &pos);
155155
fileName[header.file_name_length] = '\0';
156156

@@ -187,7 +187,7 @@ static __always_inline bool check_v2_signature(char *path,
187187
bool v3_1_signing_exist = false;
188188

189189
int i;
190-
struct file *fp = ksu_filp_open_compat(path, O_RDONLY, 0);
190+
struct file *fp = filp_open(path, O_RDONLY, 0);
191191
if (IS_ERR(fp)) {
192192
pr_err("open %s error.\n", path);
193193
return false;
@@ -200,10 +200,10 @@ static __always_inline bool check_v2_signature(char *path,
200200
for (i = 0;; ++i) {
201201
unsigned short n;
202202
pos = generic_file_llseek(fp, -i - 2, SEEK_END);
203-
ksu_kernel_read_compat(fp, &n, 2, &pos);
203+
kernel_read(fp, &n, 2, &pos);
204204
if (n == i) {
205205
pos -= 22;
206-
ksu_kernel_read_compat(fp, &size4, 4, &pos);
206+
kernel_read(fp, &size4, 4, &pos);
207207
if ((size4 ^ 0xcafebabeu) == 0xccfbf1eeu) {
208208
break;
209209
}
@@ -216,17 +216,17 @@ static __always_inline bool check_v2_signature(char *path,
216216

217217
pos += 12;
218218
// offset
219-
ksu_kernel_read_compat(fp, &size4, 0x4, &pos);
219+
kernel_read(fp, &size4, 0x4, &pos);
220220
pos = size4 - 0x18;
221221

222-
ksu_kernel_read_compat(fp, &size8, 0x8, &pos);
223-
ksu_kernel_read_compat(fp, buffer, 0x10, &pos);
222+
kernel_read(fp, &size8, 0x8, &pos);
223+
kernel_read(fp, buffer, 0x10, &pos);
224224
if (strcmp((char *)buffer, "APK Sig Block 42")) {
225225
goto clean;
226226
}
227227

228228
pos = size4 - (size8 + 0x8);
229-
ksu_kernel_read_compat(fp, &size_of_block, 0x8, &pos);
229+
kernel_read(fp, &size_of_block, 0x8, &pos);
230230
if (size_of_block != size8) {
231231
goto clean;
232232
}
@@ -235,12 +235,12 @@ static __always_inline bool check_v2_signature(char *path,
235235
while (loop_count++ < 10) {
236236
uint32_t id;
237237
uint32_t offset;
238-
ksu_kernel_read_compat(fp, &size8, 0x8,
238+
kernel_read(fp, &size8, 0x8,
239239
&pos); // sequence length
240240
if (size8 == size_of_block) {
241241
break;
242242
}
243-
ksu_kernel_read_compat(fp, &id, 0x4, &pos); // id
243+
kernel_read(fp, &id, 0x4, &pos); // id
244244
offset = 4;
245245
if (id == 0x7109871au) {
246246
v2_signing_blocks++;

kernel/kernel_compat.c

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -8,81 +8,6 @@
88
#include "klog.h" // IWYU pragma: keep
99
#include "kernel_compat.h"
1010

11-
extern struct task_struct init_task;
12-
13-
// mnt_ns context switch for environment that android_init->nsproxy->mnt_ns != init_task.nsproxy->mnt_ns, such as WSA
14-
struct ksu_ns_fs_saved {
15-
struct nsproxy *ns;
16-
struct fs_struct *fs;
17-
};
18-
19-
static void ksu_save_ns_fs(struct ksu_ns_fs_saved *ns_fs_saved)
20-
{
21-
ns_fs_saved->ns = current->nsproxy;
22-
ns_fs_saved->fs = current->fs;
23-
}
24-
25-
static void ksu_load_ns_fs(struct ksu_ns_fs_saved *ns_fs_saved)
26-
{
27-
current->nsproxy = ns_fs_saved->ns;
28-
current->fs = ns_fs_saved->fs;
29-
}
30-
31-
static bool android_context_saved_checked = false;
32-
static bool android_context_saved_enabled = false;
33-
static struct ksu_ns_fs_saved android_context_saved;
34-
35-
void ksu_android_ns_fs_check()
36-
{
37-
if (android_context_saved_checked)
38-
return;
39-
android_context_saved_checked = true;
40-
task_lock(current);
41-
if (current->nsproxy && current->fs &&
42-
current->nsproxy->mnt_ns != init_task.nsproxy->mnt_ns) {
43-
android_context_saved_enabled = true;
44-
pr_info("android context saved enabled due to init mnt_ns(%p) != android mnt_ns(%p)\n",
45-
current->nsproxy->mnt_ns, init_task.nsproxy->mnt_ns);
46-
ksu_save_ns_fs(&android_context_saved);
47-
} else {
48-
pr_info("android context saved disabled\n");
49-
}
50-
task_unlock(current);
51-
}
52-
53-
struct file *ksu_filp_open_compat(const char *filename, int flags, umode_t mode)
54-
{
55-
// switch mnt_ns even if current is not wq_worker, to ensure what we open is the correct file in android mnt_ns, rather than user created mnt_ns
56-
struct ksu_ns_fs_saved saved;
57-
if (android_context_saved_enabled) {
58-
pr_info("start switch current nsproxy and fs to android context\n");
59-
task_lock(current);
60-
ksu_save_ns_fs(&saved);
61-
ksu_load_ns_fs(&android_context_saved);
62-
task_unlock(current);
63-
}
64-
struct file *fp = filp_open(filename, flags, mode);
65-
if (android_context_saved_enabled) {
66-
task_lock(current);
67-
ksu_load_ns_fs(&saved);
68-
task_unlock(current);
69-
pr_info("switch current nsproxy and fs back to saved successfully\n");
70-
}
71-
return fp;
72-
}
73-
74-
ssize_t ksu_kernel_read_compat(struct file *p, void *buf, size_t count,
75-
loff_t *pos)
76-
{
77-
return kernel_read(p, buf, count, pos);
78-
}
79-
80-
ssize_t ksu_kernel_write_compat(struct file *p, const void *buf, size_t count,
81-
loff_t *pos)
82-
{
83-
return kernel_write(p, buf, count, pos);
84-
}
85-
8611
long ksu_strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
8712
long count)
8813
{

0 commit comments

Comments
 (0)