Skip to content

Commit 9a9ce84

Browse files
committed
kernel: ksud, sucompat: micro-opt, drop redundant memset before strncpy
its not like were doing memset(dest, 0, sizeof(dest)); strncat(dest, var, bytes); for that copy. that memset seems unneeded we also use strncpy itself to do proper error and oob check and we null term when successful Signed-off-by: backslashxx <[email protected]>
1 parent e00337c commit 9a9ce84

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

kernel/ksud.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,11 @@ __maybe_unused static int __ksu_handle_execve_ksud(const char __user *filename_u
495495
if (!filename_user)
496496
return 0;
497497

498-
memset(path, 0, sizeof(path));
499-
ksu_strncpy_from_user_nofault(path, filename_user, 32);
498+
long len = ksu_strncpy_from_user_nofault(path, filename_user, 32);
499+
if (len <= 0 || len > 32)
500+
return 0;
501+
502+
path[sizeof(path) - 1] = '\0';
500503

501504
return __ksu_handle_execveat_ksud(AT_FDCWD, path, argv, NULL, NULL);
502505
}

kernel/sucompat.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ int ksu_handle_faccessat(int *dfd, const char __user **filename_user, int *mode,
6464
}
6565

6666
char path[sizeof(su) + 1];
67-
memset(path, 0, sizeof(path));
68-
ksu_strncpy_from_user_nofault(path, *filename_user, sizeof(path));
67+
long len = ksu_strncpy_from_user_nofault(path, *filename_user, sizeof(path));
68+
if (len <= 0 || len > sizeof(path))
69+
return 0;
70+
71+
path[sizeof(path) - 1] = '\0';
6972

7073
if (unlikely(!memcmp(path, su, sizeof(su)))) {
7174
pr_info("faccessat su->sh!\n");
@@ -93,8 +96,11 @@ int ksu_handle_stat(int *dfd, const char __user **filename_user, int *flags)
9396
}
9497

9598
char path[sizeof(su) + 1];
96-
memset(path, 0, sizeof(path));
97-
ksu_strncpy_from_user_nofault(path, *filename_user, sizeof(path));
99+
long len = ksu_strncpy_from_user_nofault(path, *filename_user, sizeof(path));
100+
if (len <= 0 || len > sizeof(path))
101+
return 0;
102+
103+
path[sizeof(path) - 1] = '\0';
98104

99105
if (unlikely(!memcmp(path, su, sizeof(su)))) {
100106
pr_info("newfstatat su->sh!\n");

0 commit comments

Comments
 (0)