FAILED: patch "[PATCH] params: fix charp corruption on allocation failure" failed to apply to 6.6-stable tree
[email protected]
maintainerThe patch below does not apply to the 6.6-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.6.y
git checkout FETCH_HEAD
git cherry-pick -x 3dfaae04243cde460d82dfc2a7dd0bb6664d20ae
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026090331-stays-casually-f049@gregkh' --subject-prefix 'PATCH 6.6.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 3dfaae04243cde460d82dfc2a7dd0bb6664d20ae Mon Sep 17 00:00:00 2001
From: Jiacheng Yu <[email protected]>
Date: Wed, 29 Jul 2026 12:32:43 +0000
Subject: [PATCH] params: fix charp corruption on allocation failure
param_set_charp() stores charp parameters in allocated memory after slab is available, and releases the previous value when the parameter is updated.
The previous value is released before the replacement allocation succeeds. If kmalloc_parameter() fails, the setter returns -ENOMEM with the parameter left as NULL.
Failing zswap's compressor update before zswap is initialized can later trigger:
BUG: kernel NULL pointer dereference, address: 0000000000000000
RIP: 0010:strcmp+0x10/0x30
Call Trace:
zswap_setup+0x3b1/0x490
zswap_enabled_param_set+0x5b/0xa0
param_attr_store+0x93/0xe0
module_attr_store+0x1c/0x30
kernfs_fop_write_iter+0x116/0x1f0Allocate and copy the replacement first, then replace the parameter value only after allocation succeeds.
kernel/params.c +8 -6
@@ -261,6 +261,7 @@ EXPORT_SYMBOL_GPL(param_set_uint_minmax);int param_set_charp (const char *val,const struct kernel_param *kp) { +char *tmp;size_t len, maxlen =1024 ; len =strnlen (val, maxlen +1 );
@@ -269,19 +270,20 @@ int param_set_charp(const char *val, const struct kernel_param *kp)return -ENOSPC ; } -maybe_kfree_parameter (*(char **)kp->arg); -/* * This is a hack. We can't kmalloc() in early boot, and we * don't need to; this mangled commandline is preserved. */ if (slab_is_available ()) { - *(char **)kp->arg =kmalloc_parameter (len +1 ); -if (!*(char **)kp->arg) + tmp =kmalloc_parameter (len +1 ); +if (!tmp)return -ENOMEM ; -strcpy (*(char **)kp->arg, val); +memcpy (tmp, val, len +1 ); }else - *(const char **)kp->arg = val; + tmp = (char *)val; + +maybe_kfree_parameter (*(char **)kp->arg); + *(char **)kp->arg = tmp;return 0 ; }
Sasha Levin
maintainerPATCH 1/5 params: Do not go over the limit when getting the string length
From: Andy Shevchenko <[email protected]>
[ Upstream commit fd0cd057a1b7351604daa6ffc91dfe28adf7225d ]
We can use strnlen() even on early stages and it prevents from going over the string boundaries in case it's already too long.
Stable-dep-of: 3dfaae04243c ("params: fix charp corruption on allocation failure")
--- kernel/params.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
kernel/params.c +6 -3
@@ -264,7 +264,10 @@ EXPORT_SYMBOL_GPL(param_set_uint_minmax);int param_set_charp (const char *val,const struct kernel_param *kp) { -if (strlen (val) >1024 ) { +size_t len, maxlen =1024 ; + + len =strnlen (val, maxlen +1 ); +if (len == maxlen +1 ) {pr_err ("%s: string parameter too long\n" , kp->name);return -ENOSPC ; }
@@ -274,7 +277,7 @@ int param_set_charp(const char *val, const struct kernel_param *kp)/* This is a hack. We can't kmalloc in early boot, and we * don't need to; this mangled commandline is preserved. */ if (slab_is_available ()) { - *(char **)kp->arg =kmalloc_parameter (strlen (val)+1 ); + *(char **)kp->arg =kmalloc_parameter (len +1 );if (!*(char **)kp->arg)return -ENOMEM ;strcpy (*(char **)kp->arg, val);
@@ -512,7 +515,7 @@ int param_set_copystring(const char *val, const struct kernel_param *kp) {const struct kparam_string *kps = kp->str; -if (strlen (val)+1 > kps->maxlen) { +if (strnlen (val, kps->maxlen) == kps->maxlen) {pr_err ("%s: string doesn't fit in %u chars.\n" , kp->name, kps->maxlen-1 );return -ENOSPC ;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/5 params: Use size_add() for kmalloc()
From: Andy Shevchenko <[email protected]>
[ Upstream commit 0fc79cbc937f2a754a302a710a94b68c61d0a89a ]
Prevent allocations from integer overflow by using size_add().
Stable-dep-of: 3dfaae04243c ("params: fix charp corruption on allocation failure")
--- kernel/params.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/params.c +2 -1
@@ -11,6 +11,7 @@#include <linux/moduleparam.h> #include <linux/device.h> #include <linux/err.h> +#include <linux/overflow.h> #include <linux/slab.h> #include <linux/ctype.h> #include <linux/security.h>
@@ -48,7 +49,7 @@ static void *kmalloc_parameter(unsigned int size) {struct kmalloced_param *p; - p =kmalloc (sizeof (*p) + size,GFP_KERNEL ); + p =kmalloc (size_add (sizeof (*p), size),GFP_KERNEL );if (!p)return NULL ;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/5 params: Sort headers
In reply to Sasha LevinFrom: Andy Shevchenko <[email protected]>
[ Upstream commit a05f096c2c0ca52e8fd34740c7d4b53ab3e7123e ]
Sort the headers in alphabetic order in order to ease the maintenance for this part.
Stable-dep-of: 3dfaae04243c ("params: fix charp corruption on allocation failure")
--- kernel/params.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
kernel/params.c +6 -6
@@ -3,18 +3,18 @@Copyright (C) 2001 Rusty Russell. */ +#include <linux/ctype.h> +#include <linux/device.h> +#include <linux/err.h> +#include <linux/errno.h> #include <linux/kernel.h> #include <linux/kstrtox.h> -#include <linux/string.h> -#include <linux/errno.h> #include <linux/module.h> #include <linux/moduleparam.h> -#include <linux/device.h> -#include <linux/err.h> #include <linux/overflow.h> -#include <linux/slab.h> -#include <linux/ctype.h> #include <linux/security.h> +#include <linux/slab.h> +#include <linux/string.h> #ifdef CONFIG_SYSFS /* Protects all built-in parameters, modules use their own param_lock */
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/5 params: Fix multi-line comment style
In reply to Sasha LevinFrom: Andy Shevchenko <[email protected]>
[ Upstream commit b5e3f86a47d34f7b8af899f8cc70520f6daf8b53 ]
The multi-line comment style in the file is rather arbitrary. Make it follow the standard one.
Stable-dep-of: 3dfaae04243c ("params: fix charp corruption on allocation failure")
--- kernel/params.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
kernel/params.c +12 -8
@@ -1,8 +1,8 @@// SPDX-License-Identifier: GPL-2.0-or-later -/* Helpers for initial module or kernel cmdline parsing -Copyright (C) 2001 Rusty Russell. - -*/ +/* +* Helpers for initial module or kernel cmdline parsing +* Copyright (C) 2001 Rusty Russell. +*/ #include <linux/ctype.h> #include <linux/device.h> #include <linux/err.h>
@@ -275,8 +275,10 @@ int param_set_charp(const char *val, const struct kernel_param *kp)maybe_kfree_parameter (*(char **)kp->arg); -/* This is a hack. We can't kmalloc in early boot, and we -* don't need to; this mangled commandline is preserved. */ +/* +* This is a hack. We can't kmalloc() in early boot, and we +* don't need to; this mangled commandline is preserved. +*/ if (slab_is_available ()) { *(char **)kp->arg =kmalloc_parameter (len +1 );if (!*(char **)kp->arg)
@@ -741,8 +743,10 @@ void module_param_sysfs_remove(struct module *mod) {if (mod->mkobj.mp) {sysfs_remove_group (&mod->mkobj.kobj, &mod->mkobj.mp->grp); -/* We are positive that no one is using any param -* attrs at this point. Deallocate immediately. */ +/* +* We are positive that no one is using any param +* attrs at this point. Deallocate immediately. +*/ free_module_param_attrs (&mod->mkobj); } }
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 5/5 params: fix charp corruption on allocation failure
In reply to Sasha LevinFrom: Jiacheng Yu <[email protected]>
[ Upstream commit 3dfaae04243cde460d82dfc2a7dd0bb6664d20ae ]
param_set_charp() stores charp parameters in allocated memory after slab is available, and releases the previous value when the parameter is updated.
The previous value is released before the replacement allocation succeeds. If kmalloc_parameter() fails, the setter returns -ENOMEM with the parameter left as NULL.
Failing zswap's compressor update before zswap is initialized can later trigger:
BUG: kernel NULL pointer dereference, address: 0000000000000000
RIP: 0010:strcmp+0x10/0x30
Call Trace:
zswap_setup+0x3b1/0x490
zswap_enabled_param_set+0x5b/0xa0
param_attr_store+0x93/0xe0
module_attr_store+0x1c/0x30
kernfs_fop_write_iter+0x116/0x1f0Allocate and copy the replacement first, then replace the parameter value only after allocation succeeds.
--- kernel/params.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
kernel/params.c +8 -6
@@ -265,6 +265,7 @@ EXPORT_SYMBOL_GPL(param_set_uint_minmax);int param_set_charp (const char *val,const struct kernel_param *kp) { +char *tmp;size_t len, maxlen =1024 ; len =strnlen (val, maxlen +1 );
@@ -273,19 +274,20 @@ int param_set_charp(const char *val, const struct kernel_param *kp)return -ENOSPC ; } -maybe_kfree_parameter (*(char **)kp->arg); -/* * This is a hack. We can't kmalloc() in early boot, and we * don't need to; this mangled commandline is preserved. */ if (slab_is_available ()) { - *(char **)kp->arg =kmalloc_parameter (len +1 ); -if (!*(char **)kp->arg) + tmp =kmalloc_parameter (len +1 ); +if (!tmp)return -ENOMEM ; -strcpy (*(char **)kp->arg, val); +memcpy (tmp, val, len +1 ); }else - *(const char **)kp->arg = val; + tmp = (char *)val; + +maybe_kfree_parameter (*(char **)kp->arg); + *(char **)kp->arg = tmp;return 0 ; }
Signature · 1 line
2.53.0