Skip to content

Commit a105432

Browse files
committed
param: use free hook for charp (fix leak of charp parameters)
Instead of using a "I kmalloced this" flag, we keep track of the kmalloced strings and use that list to check if we need to kfree (in practice, the list is very short). This means that kparams can be const again, and plugs a leak. This is important for drivers/usb/gadget/nokia.c which gets modprobe/rmmod'ed frequently on the N9000. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Reviewed-by: Takashi Iwai <tiwai@suse.de> Cc: Artem Bityutskiy <dedekind1@gmail.com> Tested-by: Phil Carmody <ext-phil.2.carmody@nokia.com>
1 parent e6df34a commit a105432

1 file changed

Lines changed: 50 additions & 2 deletions

File tree

‎kernel/params.c‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,45 @@
3131
#define DEBUGP(fmt, a...)
3232
#endif
3333

34+
/* This just allows us to keep track of which parameters are kmalloced. */
35+
struct kmalloced_param {
36+
struct list_head list;
37+
char val[];
38+
};
39+
static DEFINE_MUTEX(param_lock);
40+
static LIST_HEAD(kmalloced_params);
41+
42+
static void *kmalloc_parameter(unsigned int size)
43+
{
44+
struct kmalloced_param *p;
45+
46+
p = kmalloc(sizeof(*p) + size, GFP_KERNEL);
47+
if (!p)
48+
return NULL;
49+
50+
mutex_lock(&param_lock);
51+
list_add(&p->list, &kmalloced_params);
52+
mutex_unlock(&param_lock);
53+
54+
return p->val;
55+
}
56+
57+
/* Does nothing if parameter wasn't kmalloced above. */
58+
static void maybe_kfree_parameter(void *param)
59+
{
60+
struct kmalloced_param *p;
61+
62+
mutex_lock(&param_lock);
63+
list_for_each_entry(p, &kmalloced_params, list) {
64+
if (p->val == param) {
65+
list_del(&p->list);
66+
kfree(p);
67+
break;
68+
}
69+
}
70+
mutex_unlock(&param_lock);
71+
}
72+
3473
static inline char dash2underscore(char c)
3574
{
3675
if (c == '-')
@@ -219,12 +258,15 @@ int param_set_charp(const char *val, const struct kernel_param *kp)
219258
return -ENOSPC;
220259
}
221260

222-
/* This is a hack. We can't need to strdup in early boot, and we
261+
maybe_kfree_parameter(*(char **)kp->arg);
262+
263+
/* This is a hack. We can't kmalloc in early boot, and we
223264
* don't need to; this mangled commandline is preserved. */
224265
if (slab_is_available()) {
225-
*(char **)kp->arg = kstrdup(val, GFP_KERNEL);
266+
*(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
226267
if (!*(char **)kp->arg)
227268
return -ENOMEM;
269+
strcpy(*(char **)kp->arg, val);
228270
} else
229271
*(const char **)kp->arg = val;
230272

@@ -238,9 +280,15 @@ int param_get_charp(char *buffer, const struct kernel_param *kp)
238280
}
239281
EXPORT_SYMBOL(param_get_charp);
240282

283+
static void param_free_charp(void *arg)
284+
{
285+
maybe_kfree_parameter(*((char **)arg));
286+
}
287+
241288
struct kernel_param_ops param_ops_charp = {
242289
.set = param_set_charp,
243290
.get = param_get_charp,
291+
.free = param_free_charp,
244292
};
245293
EXPORT_SYMBOL(param_ops_charp);
246294

0 commit comments

Comments
 (0)