Skip to content

Commit a2affd3

Browse files
committed
kernel: ksud: dont create structs just to deconstruct them for a string (tiann#2595)
sys_execve_handler_pre was building a fake struct filename just to pass it to ksu_handle_execveat_ksud, which immediately does... filename->name. ?? All we ever needed was filename->name, but we kept doing this meme where we manually built a struct filename, passed it around, then immediately ripped out the string again. ?? refactor this so that __ksu_handle_execveat_ksud, takes plain char *. The old ksu_handle_execveat_ksud is now a shim that unpacks the struct and hands off the string like we should’ve been doing from the start. Also mark ksu_handle_execveat_ksud as maybe unused as this will actually be unused on syscall-only builds. This also makes integration easier on kernels that don’t have struct filename. Rejected: tiann#2595 Signed-off-by: backslashxx <[email protected]>
1 parent 4e6fe9f commit a2affd3

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

kernel/ksud.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,10 @@ static int __maybe_unused count(struct user_arg_ptr argv, int max)
150150
}
151151

152152
// IMPORTANT NOTE: the call from execve_handler_pre WON'T provided correct value for envp and flags in GKI version
153-
int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
153+
static int __ksu_handle_execveat_ksud(int *fd, char *filename,
154154
struct user_arg_ptr *argv,
155155
struct user_arg_ptr *envp, int *flags)
156156
{
157-
if (!ksu_execveat_hook) {
158-
return 0;
159-
}
160-
161-
struct filename *filename;
162-
163157
static const char app_process[] = "/system/bin/app_process";
164158
static bool first_app_process = true;
165159

@@ -169,15 +163,10 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
169163
static const char old_system_init[] = "/init";
170164
static bool init_second_stage_executed = false;
171165

172-
if (!filename_ptr)
166+
if (!filename)
173167
return 0;
174168

175-
filename = *filename_ptr;
176-
if (IS_ERR(filename)) {
177-
return 0;
178-
}
179-
180-
if (unlikely(!memcmp(filename->name, system_bin_init,
169+
if (unlikely(!memcmp(filename, system_bin_init,
181170
sizeof(system_bin_init) - 1) &&
182171
argv)) {
183172
// /system/bin/init executed
@@ -201,7 +190,7 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
201190
pr_err("/system/bin/init parse args err!\n");
202191
}
203192
}
204-
} else if (unlikely(!memcmp(filename->name, old_system_init,
193+
} else if (unlikely(!memcmp(filename, old_system_init,
205194
sizeof(old_system_init) - 1) &&
206195
argv)) {
207196
// /init executed
@@ -264,7 +253,7 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
264253
}
265254
}
266255

267-
if (unlikely(first_app_process && !memcmp(filename->name, app_process,
256+
if (unlikely(first_app_process && !memcmp(filename, app_process,
268257
sizeof(app_process) - 1))) {
269258
first_app_process = false;
270259
pr_info("exec app_process, /data prepared, second_stage: %d\n",
@@ -276,6 +265,26 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
276265
return 0;
277266
}
278267

268+
// keep this for manually hooked builds
269+
__maybe_unused int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
270+
struct user_arg_ptr *argv, struct user_arg_ptr *envp,
271+
int *flags)
272+
{
273+
// return early when disabled
274+
if (!ksu_execveat_hook) {
275+
return 0;
276+
}
277+
278+
if (!filename_ptr)
279+
return 0;
280+
281+
struct filename *filename = *filename_ptr;
282+
if (IS_ERR(filename))
283+
return 0;
284+
285+
return __ksu_handle_execveat_ksud(fd, (char *)filename->name, argv, envp, flags);
286+
}
287+
279288
static ssize_t (*orig_read)(struct file *, char __user *, size_t, loff_t *);
280289
static ssize_t (*orig_read_iter)(struct kiocb *, struct iov_iter *);
281290
static struct file_operations fops_proxy;

0 commit comments

Comments
 (0)