Skip to content

Commit cb4fa30

Browse files
committed
fs: implement scope-minimized ksu hooks
References: tiann/KernelSU#1657 and tiann/KernelSU#2084 Signed-off-by: Samuel Pascua <[email protected]>
1 parent 7ffa397 commit cb4fa30

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

fs/exec.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,8 +1878,6 @@ static int do_execveat_common(int fd, struct filename *filename,
18781878
#ifdef CONFIG_KSU
18791879
if (unlikely(ksu_execveat_hook))
18801880
ksu_handle_execveat(&fd, &filename, &argv, &envp, &flags);
1881-
else
1882-
ksu_handle_execveat_sucompat(&fd, &filename, &argv, &envp, &flags);
18831881
#endif
18841882

18851883
if (IS_ERR(filename))
@@ -2123,11 +2121,23 @@ void set_dumpable(struct mm_struct *mm, int value)
21232121
} while (cmpxchg(&mm->flags, old, new) != old);
21242122
}
21252123

2124+
#ifdef CONFIG_KSU
2125+
extern int ksu_handle_execve_sucompat(int *fd, const char __user **filename_user,
2126+
void *__never_use_argv, void *__never_use_envp,
2127+
int *__never_use_flags);
2128+
int at_fdcwd = AT_FDCWD;
2129+
#endif
2130+
21262131
SYSCALL_DEFINE3(execve,
21272132
const char __user *, filename,
21282133
const char __user *const __user *, argv,
21292134
const char __user *const __user *, envp)
21302135
{
2136+
#ifdef CONFIG_KSU
2137+
if (!ksu_execveat_hook)
2138+
ksu_handle_execve_sucompat(&at_fdcwd, &filename, NULL, NULL, NULL);
2139+
#endif
2140+
21312141
#ifdef CONFIG_RKP_KDP
21322142
struct filename *path = getname(filename);
21332143
int error = PTR_ERR(path);
@@ -2163,6 +2173,10 @@ COMPAT_SYSCALL_DEFINE3(execve, const char __user *, filename,
21632173
const compat_uptr_t __user *, argv,
21642174
const compat_uptr_t __user *, envp)
21652175
{
2176+
#ifdef CONFIG_KSU
2177+
if (!ksu_execveat_hook)
2178+
ksu_handle_execve_sucompat(&at_fdcwd, &filename, NULL, NULL, NULL); /* 32-bit su support */
2179+
#endif
21662180
return compat_do_execve(getname(filename), argv, envp);
21672181
}
21682182

fs/read_write.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,18 +439,13 @@ EXPORT_SYMBOL(kernel_read);
439439

440440
#ifdef CONFIG_KSU
441441
extern bool ksu_vfs_read_hook __read_mostly;
442-
extern int ksu_handle_vfs_read(struct file **file_ptr, char __user **buf_ptr,
443-
size_t *count_ptr, loff_t **pos);
442+
extern int ksu_handle_sys_read(unsigned int fd, char __user **buf_ptr,
443+
size_t *count_ptr);
444444
#endif
445445
ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
446446
{
447447
ssize_t ret;
448448

449-
#ifdef CONFIG_KSU
450-
if (unlikely(ksu_vfs_read_hook))
451-
ksu_handle_vfs_read(&file, &buf, &count, &pos);
452-
#endif
453-
454449
if (!(file->f_mode & FMODE_READ))
455450
return -EBADF;
456451
if (!(file->f_mode & FMODE_CAN_READ))
@@ -614,6 +609,10 @@ SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
614609

615610
if (f.file) {
616611
loff_t pos = file_pos_read(f.file);
612+
#ifdef CONFIG_KSU
613+
if (unlikely(ksu_vfs_read_hook))
614+
ksu_handle_sys_read(fd, &buf, &count);
615+
#endif
617616
ret = vfs_read(f.file, buf, count, &pos);
618617
if (ret >= 0)
619618
file_pos_write(f.file, pos);

fs/stat.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,6 @@ int vfs_statx(int dfd, const char __user *filename, int flags,
174174
int error = -EINVAL;
175175
unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT;
176176

177-
#ifdef CONFIG_KSU
178-
ksu_handle_stat(&dfd, &filename, &flags);
179-
#endif
180177
if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
181178
AT_EMPTY_PATH | KSTAT_QUERY_FLAGS)) != 0)
182179
return -EINVAL;
@@ -367,6 +364,9 @@ SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
367364
struct kstat stat;
368365
int error;
369366

367+
#ifdef CONFIG_KSU
368+
ksu_handle_stat(&dfd, &filename, &flag);
369+
#endif
370370
error = vfs_fstatat(dfd, filename, &stat, flag);
371371
if (error)
372372
return error;
@@ -511,6 +511,9 @@ SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
511511
struct kstat stat;
512512
int error;
513513

514+
#ifdef CONFIG_KSU
515+
ksu_handle_stat(&dfd, &filename, &flag); /* 32-bit su support */
516+
#endif
514517
error = vfs_fstatat(dfd, filename, &stat, flag);
515518
if (error)
516519
return error;

0 commit comments

Comments
 (0)