Skip to content

Commit 464c782

Browse files
backslashxxLeCmnGend
authored andcommitted
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 c360c6c commit 464c782

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
@@ -159,16 +159,10 @@ static int __maybe_unused count(struct user_arg_ptr argv, int max)
159159
}
160160

161161
// IMPORTANT NOTE: the call from execve_handler_pre WON'T provided correct value for envp and flags in GKI version
162-
int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
162+
static int __ksu_handle_execveat_ksud(int *fd, char *filename,
163163
struct user_arg_ptr *argv,
164164
struct user_arg_ptr *envp, int *flags)
165165
{
166-
if (!ksu_execveat_hook) {
167-
return 0;
168-
}
169-
170-
struct filename *filename;
171-
172166
static const char app_process[] = "/system/bin/app_process";
173167
static bool first_app_process = true;
174168

@@ -178,15 +172,10 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
178172
static const char old_system_init[] = "/init";
179173
static bool init_second_stage_executed = false;
180174

181-
if (!filename_ptr)
175+
if (!filename)
182176
return 0;
183177

184-
filename = *filename_ptr;
185-
if (IS_ERR(filename)) {
186-
return 0;
187-
}
188-
189-
if (unlikely(!memcmp(filename->name, system_bin_init,
178+
if (unlikely(!memcmp(filename, system_bin_init,
190179
sizeof(system_bin_init) - 1) &&
191180
argv)) {
192181
// /system/bin/init executed
@@ -211,7 +200,7 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
211200
pr_err("/system/bin/init parse args err!\n");
212201
}
213202
}
214-
} else if (unlikely(!memcmp(filename->name, old_system_init,
203+
} else if (unlikely(!memcmp(filename, old_system_init,
215204
sizeof(old_system_init) - 1) &&
216205
argv)) {
217206
// /init executed
@@ -274,7 +263,7 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
274263
}
275264
}
276265

277-
if (unlikely(first_app_process && !memcmp(filename->name, app_process,
266+
if (unlikely(first_app_process && !memcmp(filename, app_process,
278267
sizeof(app_process) - 1))) {
279268
first_app_process = false;
280269
pr_info("exec app_process, /data prepared, second_stage: %d\n",
@@ -286,6 +275,26 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
286275
return 0;
287276
}
288277

278+
// keep this for manually hooked builds
279+
__maybe_unused int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
280+
struct user_arg_ptr *argv, struct user_arg_ptr *envp,
281+
int *flags)
282+
{
283+
// return early when disabled
284+
if (!ksu_execveat_hook) {
285+
return 0;
286+
}
287+
288+
if (!filename_ptr)
289+
return 0;
290+
291+
struct filename *filename = *filename_ptr;
292+
if (IS_ERR(filename))
293+
return 0;
294+
295+
return __ksu_handle_execveat_ksud(fd, (char *)filename->name, argv, envp, flags);
296+
}
297+
289298
static ssize_t (*orig_read)(struct file *, char __user *, size_t, loff_t *);
290299
static ssize_t (*orig_read_iter)(struct kiocb *, struct iov_iter *);
291300
static struct file_operations fops_proxy;

0 commit comments

Comments
 (0)