Skip to content

Commit 75bb3ab

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 a1a295c commit 75bb3ab

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

157157
// IMPORTANT NOTE: the call from execve_handler_pre WON'T provided correct value for envp and flags in GKI version
158-
int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
158+
static int __ksu_handle_execveat_ksud(int *fd, char *filename,
159159
struct user_arg_ptr *argv,
160160
struct user_arg_ptr *envp, int *flags)
161161
{
162-
if (!ksu_execveat_hook) {
163-
return 0;
164-
}
165-
166-
struct filename *filename;
167-
168162
static const char app_process[] = "/system/bin/app_process";
169163
static bool first_app_process = true;
170164

@@ -174,15 +168,10 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
174168
static const char old_system_init[] = "/init";
175169
static bool init_second_stage_executed = false;
176170

177-
if (!filename_ptr)
171+
if (!filename)
178172
return 0;
179173

180-
filename = *filename_ptr;
181-
if (IS_ERR(filename)) {
182-
return 0;
183-
}
184-
185-
if (unlikely(!memcmp(filename->name, system_bin_init,
174+
if (unlikely(!memcmp(filename, system_bin_init,
186175
sizeof(system_bin_init) - 1) &&
187176
argv)) {
188177
// /system/bin/init executed
@@ -206,7 +195,7 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
206195
pr_err("/system/bin/init parse args err!\n");
207196
}
208197
}
209-
} else if (unlikely(!memcmp(filename->name, old_system_init,
198+
} else if (unlikely(!memcmp(filename, old_system_init,
210199
sizeof(old_system_init) - 1) &&
211200
argv)) {
212201
// /init executed
@@ -269,7 +258,7 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
269258
}
270259
}
271260

272-
if (unlikely(first_app_process && !memcmp(filename->name, app_process,
261+
if (unlikely(first_app_process && !memcmp(filename, app_process,
273262
sizeof(app_process) - 1))) {
274263
first_app_process = false;
275264
pr_info("exec app_process, /data prepared, second_stage: %d\n",
@@ -281,6 +270,26 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
281270
return 0;
282271
}
283272

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

0 commit comments

Comments
 (0)