Skip to content

Commit fd0cd05

Browse files
andy-shevkees
authored andcommitted
params: Do not go over the limit when getting the string length
We can use strnlen() even on early stages and it prevents from going over the string boundaries in case it's already too long. Reviewed-by: Luis Chamberlain <[email protected]> Reviewed-by: Kees Cook <[email protected]> Signed-off-by: Andy Shevchenko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 12cd3cd commit fd0cd05

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

kernel/params.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,10 @@ EXPORT_SYMBOL_GPL(param_set_uint_minmax);
260260

261261
int param_set_charp(const char *val, const struct kernel_param *kp)
262262
{
263-
if (strlen(val) > 1024) {
263+
size_t len, maxlen = 1024;
264+
265+
len = strnlen(val, maxlen + 1);
266+
if (len == maxlen + 1) {
264267
pr_err("%s: string parameter too long\n", kp->name);
265268
return -ENOSPC;
266269
}
@@ -270,7 +273,7 @@ int param_set_charp(const char *val, const struct kernel_param *kp)
270273
/* This is a hack. We can't kmalloc in early boot, and we
271274
* don't need to; this mangled commandline is preserved. */
272275
if (slab_is_available()) {
273-
*(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
276+
*(char **)kp->arg = kmalloc_parameter(len + 1);
274277
if (!*(char **)kp->arg)
275278
return -ENOMEM;
276279
strcpy(*(char **)kp->arg, val);
@@ -508,7 +511,7 @@ int param_set_copystring(const char *val, const struct kernel_param *kp)
508511
{
509512
const struct kparam_string *kps = kp->str;
510513

511-
if (strlen(val)+1 > kps->maxlen) {
514+
if (strnlen(val, kps->maxlen) == kps->maxlen) {
512515
pr_err("%s: string doesn't fit in %u chars.\n",
513516
kp->name, kps->maxlen-1);
514517
return -ENOSPC;

0 commit comments

Comments
 (0)