xref: /linux/kernel/params.c (revision 29fe5d50dfa6b61043e2e89206389ae56b5596eb)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Helpers for initial module or kernel cmdline parsing
4  * Copyright (C) 2001 Rusty Russell.
5  */
6 #include <linux/ctype.h>
7 #include <linux/device.h>
8 #include <linux/err.h>
9 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/kstrtox.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/overflow.h>
15 #include <linux/security.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 
19 #ifdef CONFIG_SYSFS
20 /* Protects all built-in parameters, modules use their own param_lock */
21 static DEFINE_MUTEX(param_lock);
22 
23 /* Use the module's mutex, or if built-in use the built-in mutex */
24 #ifdef CONFIG_MODULES
25 #define KPARAM_MUTEX(mod)	((mod) ? &(mod)->param_lock : &param_lock)
26 #else
27 #define KPARAM_MUTEX(mod)	(&param_lock)
28 #endif
29 
check_kparam_locked(struct module * mod)30 static inline void check_kparam_locked(struct module *mod)
31 {
32 	BUG_ON(!mutex_is_locked(KPARAM_MUTEX(mod)));
33 }
34 #else
check_kparam_locked(struct module * mod)35 static inline void check_kparam_locked(struct module *mod)
36 {
37 }
38 #endif /* !CONFIG_SYSFS */
39 
40 /* This just allows us to keep track of which parameters are kmalloced. */
41 struct kmalloced_param {
42 	struct list_head list;
43 	char val[];
44 };
45 static LIST_HEAD(kmalloced_params);
46 static DEFINE_SPINLOCK(kmalloced_params_lock);
47 
kmalloc_parameter(unsigned int size)48 static void *kmalloc_parameter(unsigned int size)
49 {
50 	struct kmalloced_param *p;
51 
52 	p = kmalloc(size_add(sizeof(*p), size), GFP_KERNEL);
53 	if (!p)
54 		return NULL;
55 
56 	spin_lock(&kmalloced_params_lock);
57 	list_add(&p->list, &kmalloced_params);
58 	spin_unlock(&kmalloced_params_lock);
59 
60 	return p->val;
61 }
62 
63 /* Does nothing if parameter wasn't kmalloced above. */
maybe_kfree_parameter(void * param)64 static void maybe_kfree_parameter(void *param)
65 {
66 	struct kmalloced_param *p;
67 
68 	spin_lock(&kmalloced_params_lock);
69 	list_for_each_entry(p, &kmalloced_params, list) {
70 		if (p->val == param) {
71 			list_del(&p->list);
72 			kfree(p);
73 			break;
74 		}
75 	}
76 	spin_unlock(&kmalloced_params_lock);
77 }
78 
dash2underscore(char c)79 static char dash2underscore(char c)
80 {
81 	if (c == '-')
82 		return '_';
83 	return c;
84 }
85 
parameqn(const char * a,const char * b,size_t n)86 bool parameqn(const char *a, const char *b, size_t n)
87 {
88 	size_t i;
89 
90 	for (i = 0; i < n; i++) {
91 		if (dash2underscore(a[i]) != dash2underscore(b[i]))
92 			return false;
93 	}
94 	return true;
95 }
96 
parameq(const char * a,const char * b)97 bool parameq(const char *a, const char *b)
98 {
99 	return parameqn(a, b, strlen(a)+1);
100 }
101 
param_check_unsafe(const struct kernel_param * kp)102 static bool param_check_unsafe(const struct kernel_param *kp)
103 {
104 	if (kp->flags & KERNEL_PARAM_FL_HWPARAM &&
105 	    security_locked_down(LOCKDOWN_MODULE_PARAMETERS))
106 		return false;
107 
108 	if (kp->flags & KERNEL_PARAM_FL_UNSAFE) {
109 		pr_notice("Setting dangerous option %s - tainting kernel\n",
110 			  kp->name);
111 		add_taint(TAINT_USER, LOCKDEP_STILL_OK);
112 	}
113 
114 	return true;
115 }
116 
parse_one(char * param,char * val,const char * doing,const struct kernel_param * params,unsigned num_params,s16 min_level,s16 max_level,void * arg,parse_unknown_fn handle_unknown)117 static int parse_one(char *param,
118 		     char *val,
119 		     const char *doing,
120 		     const struct kernel_param *params,
121 		     unsigned num_params,
122 		     s16 min_level,
123 		     s16 max_level,
124 		     void *arg, parse_unknown_fn handle_unknown)
125 {
126 	unsigned int i;
127 	int err;
128 
129 	/* Find parameter */
130 	for (i = 0; i < num_params; i++) {
131 		if (parameq(param, params[i].name)) {
132 			if (params[i].level < min_level
133 			    || params[i].level > max_level)
134 				return 0;
135 			/* No one handled NULL, so do it here. */
136 			if (!val &&
137 			    !(params[i].ops->flags & KERNEL_PARAM_OPS_FL_NOARG))
138 				return -EINVAL;
139 			pr_debug("handling %s with %p\n", param,
140 				params[i].ops->set);
141 			kernel_param_lock(params[i].mod);
142 			if (param_check_unsafe(&params[i]))
143 				err = params[i].ops->set(val, &params[i]);
144 			else
145 				err = -EPERM;
146 			kernel_param_unlock(params[i].mod);
147 			return err;
148 		}
149 	}
150 
151 	if (handle_unknown) {
152 		pr_debug("doing %s: %s='%s'\n", doing, param, val);
153 		return handle_unknown(param, val, doing, arg);
154 	}
155 
156 	pr_debug("Unknown argument '%s'\n", param);
157 	return -ENOENT;
158 }
159 
160 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
parse_args(const char * doing,char * args,const struct kernel_param * params,unsigned num,s16 min_level,s16 max_level,void * arg,parse_unknown_fn unknown)161 char *parse_args(const char *doing,
162 		 char *args,
163 		 const struct kernel_param *params,
164 		 unsigned num,
165 		 s16 min_level,
166 		 s16 max_level,
167 		 void *arg, parse_unknown_fn unknown)
168 {
169 	char *param, *val, *err = NULL;
170 
171 	/* Chew leading spaces */
172 	args = skip_spaces(args);
173 
174 	if (*args)
175 		pr_debug("doing %s, parsing ARGS: '%s'\n", doing, args);
176 
177 	while (*args) {
178 		int ret;
179 		int irq_was_disabled;
180 
181 		args = next_arg(args, &param, &val);
182 		/* Stop at -- */
183 		if (!val && strcmp(param, "--") == 0)
184 			return err ?: args;
185 		irq_was_disabled = irqs_disabled();
186 		ret = parse_one(param, val, doing, params, num,
187 				min_level, max_level, arg, unknown);
188 		if (irq_was_disabled && !irqs_disabled())
189 			pr_warn("%s: option '%s' enabled irq's!\n",
190 				doing, param);
191 
192 		switch (ret) {
193 		case 0:
194 			continue;
195 		case -ENOENT:
196 			pr_err("%s: Unknown parameter `%s'\n", doing, param);
197 			break;
198 		case -ENOSPC:
199 			pr_err("%s: `%s' too large for parameter `%s'\n",
200 			       doing, val ?: "", param);
201 			break;
202 		default:
203 			pr_err("%s: `%s' invalid for parameter `%s'\n",
204 			       doing, val ?: "", param);
205 			break;
206 		}
207 
208 		err = ERR_PTR(ret);
209 	}
210 
211 	return err;
212 }
213 
214 /* Lazy bastard, eh? */
215 #define STANDARD_PARAM_DEF(name, type, format, strtolfn)      		\
216 	int param_set_##name(const char *val, const struct kernel_param *kp) \
217 	{								\
218 		return strtolfn(val, 0, (type *)kp->arg);		\
219 	}								\
220 	int param_get_##name(char *buffer, const struct kernel_param *kp) \
221 	{								\
222 		return scnprintf(buffer, PAGE_SIZE, format "\n",	\
223 				*((type *)kp->arg));			\
224 	}								\
225 	const struct kernel_param_ops param_ops_##name = {			\
226 		.set = param_set_##name,				\
227 		.get = param_get_##name,				\
228 	};								\
229 	EXPORT_SYMBOL(param_set_##name);				\
230 	EXPORT_SYMBOL(param_get_##name);				\
231 	EXPORT_SYMBOL(param_ops_##name)
232 
233 
234 STANDARD_PARAM_DEF(byte,	unsigned char,		"%hhu",		kstrtou8);
235 STANDARD_PARAM_DEF(short,	short,			"%hi",		kstrtos16);
236 STANDARD_PARAM_DEF(ushort,	unsigned short,		"%hu",		kstrtou16);
237 STANDARD_PARAM_DEF(int,		int,			"%i",		kstrtoint);
238 STANDARD_PARAM_DEF(uint,	unsigned int,		"%u",		kstrtouint);
239 STANDARD_PARAM_DEF(long,	long,			"%li",		kstrtol);
240 STANDARD_PARAM_DEF(ulong,	unsigned long,		"%lu",		kstrtoul);
241 STANDARD_PARAM_DEF(ullong,	unsigned long long,	"%llu",		kstrtoull);
242 STANDARD_PARAM_DEF(hexint,	unsigned int,		"%#08x", 	kstrtouint);
243 
param_set_uint_minmax(const char * val,const struct kernel_param * kp,unsigned int min,unsigned int max)244 int param_set_uint_minmax(const char *val, const struct kernel_param *kp,
245 		unsigned int min, unsigned int max)
246 {
247 	unsigned int num;
248 	int ret;
249 
250 	if (!val)
251 		return -EINVAL;
252 	ret = kstrtouint(val, 0, &num);
253 	if (ret)
254 		return ret;
255 	if (num < min || num > max)
256 		return -EINVAL;
257 	*((unsigned int *)kp->arg) = num;
258 	return 0;
259 }
260 EXPORT_SYMBOL_GPL(param_set_uint_minmax);
261 
param_set_charp(const char * val,const struct kernel_param * kp)262 int param_set_charp(const char *val, const struct kernel_param *kp)
263 {
264 	size_t len, maxlen = 1024;
265 
266 	len = strnlen(val, maxlen + 1);
267 	if (len == maxlen + 1) {
268 		pr_err("%s: string parameter too long\n", kp->name);
269 		return -ENOSPC;
270 	}
271 
272 	maybe_kfree_parameter(*(char **)kp->arg);
273 
274 	/*
275 	 * This is a hack. We can't kmalloc() in early boot, and we
276 	 * don't need to; this mangled commandline is preserved.
277 	 */
278 	if (slab_is_available()) {
279 		*(char **)kp->arg = kmalloc_parameter(len + 1);
280 		if (!*(char **)kp->arg)
281 			return -ENOMEM;
282 		strcpy(*(char **)kp->arg, val);
283 	} else
284 		*(const char **)kp->arg = val;
285 
286 	return 0;
287 }
288 EXPORT_SYMBOL(param_set_charp);
289 
param_get_charp(char * buffer,const struct kernel_param * kp)290 int param_get_charp(char *buffer, const struct kernel_param *kp)
291 {
292 	return scnprintf(buffer, PAGE_SIZE, "%s\n", *((char **)kp->arg));
293 }
294 EXPORT_SYMBOL(param_get_charp);
295 
param_free_charp(void * arg)296 void param_free_charp(void *arg)
297 {
298 	maybe_kfree_parameter(*((char **)arg));
299 }
300 EXPORT_SYMBOL(param_free_charp);
301 
302 const struct kernel_param_ops param_ops_charp = {
303 	.set = param_set_charp,
304 	.get = param_get_charp,
305 	.free = param_free_charp,
306 };
307 EXPORT_SYMBOL(param_ops_charp);
308 
309 /* Actually could be a bool or an int, for historical reasons. */
param_set_bool(const char * val,const struct kernel_param * kp)310 int param_set_bool(const char *val, const struct kernel_param *kp)
311 {
312 	/* No equals means "set"... */
313 	if (!val) val = "1";
314 
315 	/* One of =[yYnN01] */
316 	return kstrtobool(val, kp->arg);
317 }
318 EXPORT_SYMBOL(param_set_bool);
319 
param_get_bool(char * buffer,const struct kernel_param * kp)320 int param_get_bool(char *buffer, const struct kernel_param *kp)
321 {
322 	/* Y and N chosen as being relatively non-coder friendly */
323 	return sprintf(buffer, "%c\n", *(bool *)kp->arg ? 'Y' : 'N');
324 }
325 EXPORT_SYMBOL(param_get_bool);
326 
327 const struct kernel_param_ops param_ops_bool = {
328 	.flags = KERNEL_PARAM_OPS_FL_NOARG,
329 	.set = param_set_bool,
330 	.get = param_get_bool,
331 };
332 EXPORT_SYMBOL(param_ops_bool);
333 
param_set_bool_enable_only(const char * val,const struct kernel_param * kp)334 int param_set_bool_enable_only(const char *val, const struct kernel_param *kp)
335 {
336 	int err;
337 	bool new_value;
338 	bool orig_value = *(bool *)kp->arg;
339 	struct kernel_param dummy_kp = *kp;
340 
341 	dummy_kp.arg = &new_value;
342 
343 	err = param_set_bool(val, &dummy_kp);
344 	if (err)
345 		return err;
346 
347 	/* Don't let them unset it once it's set! */
348 	if (!new_value && orig_value)
349 		return -EROFS;
350 
351 	if (new_value)
352 		err = param_set_bool(val, kp);
353 
354 	return err;
355 }
356 EXPORT_SYMBOL_GPL(param_set_bool_enable_only);
357 
358 const struct kernel_param_ops param_ops_bool_enable_only = {
359 	.flags = KERNEL_PARAM_OPS_FL_NOARG,
360 	.set = param_set_bool_enable_only,
361 	.get = param_get_bool,
362 };
363 EXPORT_SYMBOL_GPL(param_ops_bool_enable_only);
364 
365 /* This one must be bool. */
param_set_invbool(const char * val,const struct kernel_param * kp)366 int param_set_invbool(const char *val, const struct kernel_param *kp)
367 {
368 	int ret;
369 	bool boolval;
370 	struct kernel_param dummy;
371 
372 	dummy.arg = &boolval;
373 	ret = param_set_bool(val, &dummy);
374 	if (ret == 0)
375 		*(bool *)kp->arg = !boolval;
376 	return ret;
377 }
378 EXPORT_SYMBOL(param_set_invbool);
379 
param_get_invbool(char * buffer,const struct kernel_param * kp)380 int param_get_invbool(char *buffer, const struct kernel_param *kp)
381 {
382 	return sprintf(buffer, "%c\n", (*(bool *)kp->arg) ? 'N' : 'Y');
383 }
384 EXPORT_SYMBOL(param_get_invbool);
385 
386 const struct kernel_param_ops param_ops_invbool = {
387 	.set = param_set_invbool,
388 	.get = param_get_invbool,
389 };
390 EXPORT_SYMBOL(param_ops_invbool);
391 
param_set_bint(const char * val,const struct kernel_param * kp)392 int param_set_bint(const char *val, const struct kernel_param *kp)
393 {
394 	/* Match bool exactly, by re-using it. */
395 	struct kernel_param boolkp = *kp;
396 	bool v;
397 	int ret;
398 
399 	boolkp.arg = &v;
400 
401 	ret = param_set_bool(val, &boolkp);
402 	if (ret == 0)
403 		*(int *)kp->arg = v;
404 	return ret;
405 }
406 EXPORT_SYMBOL(param_set_bint);
407 
408 const struct kernel_param_ops param_ops_bint = {
409 	.flags = KERNEL_PARAM_OPS_FL_NOARG,
410 	.set = param_set_bint,
411 	.get = param_get_int,
412 };
413 EXPORT_SYMBOL(param_ops_bint);
414 
415 /* We break the rule and mangle the string. */
param_array(struct module * mod,const char * name,const char * val,unsigned int min,unsigned int max,void * elem,int elemsize,int (* set)(const char *,const struct kernel_param * kp),s16 level,unsigned int * num)416 static int param_array(struct module *mod,
417 		       const char *name,
418 		       const char *val,
419 		       unsigned int min, unsigned int max,
420 		       void *elem, int elemsize,
421 		       int (*set)(const char *, const struct kernel_param *kp),
422 		       s16 level,
423 		       unsigned int *num)
424 {
425 	int ret;
426 	struct kernel_param kp;
427 	char save;
428 
429 	/* Get the name right for errors. */
430 	kp.name = name;
431 	kp.arg = elem;
432 	kp.level = level;
433 
434 	*num = 0;
435 	/* We expect a comma-separated list of values. */
436 	do {
437 		int len;
438 
439 		if (*num == max) {
440 			pr_err("%s: can only take %i arguments\n", name, max);
441 			return -EINVAL;
442 		}
443 		len = strcspn(val, ",");
444 
445 		/* nul-terminate and parse */
446 		save = val[len];
447 		((char *)val)[len] = '\0';
448 		check_kparam_locked(mod);
449 		ret = set(val, &kp);
450 
451 		if (ret != 0)
452 			return ret;
453 		kp.arg += elemsize;
454 		val += len+1;
455 		(*num)++;
456 	} while (save == ',');
457 
458 	if (*num < min) {
459 		pr_err("%s: needs at least %i arguments\n", name, min);
460 		return -EINVAL;
461 	}
462 	return 0;
463 }
464 
param_array_set(const char * val,const struct kernel_param * kp)465 static int param_array_set(const char *val, const struct kernel_param *kp)
466 {
467 	const struct kparam_array *arr = kp->arr;
468 	unsigned int temp_num;
469 
470 	return param_array(kp->mod, kp->name, val, 1, arr->max, arr->elem,
471 			   arr->elemsize, arr->ops->set, kp->level,
472 			   arr->num ?: &temp_num);
473 }
474 
param_array_get(char * buffer,const struct kernel_param * kp)475 static int param_array_get(char *buffer, const struct kernel_param *kp)
476 {
477 	int i, off, ret;
478 	const struct kparam_array *arr = kp->arr;
479 	struct kernel_param p = *kp;
480 
481 	for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
482 		/* Replace \n with comma */
483 		if (i)
484 			buffer[off - 1] = ',';
485 		p.arg = arr->elem + arr->elemsize * i;
486 		check_kparam_locked(p.mod);
487 		ret = arr->ops->get(buffer + off, &p);
488 		if (ret < 0)
489 			return ret;
490 		off += ret;
491 	}
492 	buffer[off] = '\0';
493 	return off;
494 }
495 
param_array_free(void * arg)496 static void param_array_free(void *arg)
497 {
498 	unsigned int i;
499 	const struct kparam_array *arr = arg;
500 
501 	if (arr->ops->free)
502 		for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
503 			arr->ops->free(arr->elem + arr->elemsize * i);
504 }
505 
506 const struct kernel_param_ops param_array_ops = {
507 	.set = param_array_set,
508 	.get = param_array_get,
509 	.free = param_array_free,
510 };
511 EXPORT_SYMBOL(param_array_ops);
512 
param_set_copystring(const char * val,const struct kernel_param * kp)513 int param_set_copystring(const char *val, const struct kernel_param *kp)
514 {
515 	const struct kparam_string *kps = kp->str;
516 
517 	if (strnlen(val, kps->maxlen) == kps->maxlen) {
518 		pr_err("%s: string doesn't fit in %u chars.\n",
519 		       kp->name, kps->maxlen-1);
520 		return -ENOSPC;
521 	}
522 	strcpy(kps->string, val);
523 	return 0;
524 }
525 EXPORT_SYMBOL(param_set_copystring);
526 
param_get_string(char * buffer,const struct kernel_param * kp)527 int param_get_string(char *buffer, const struct kernel_param *kp)
528 {
529 	const struct kparam_string *kps = kp->str;
530 	return scnprintf(buffer, PAGE_SIZE, "%s\n", kps->string);
531 }
532 EXPORT_SYMBOL(param_get_string);
533 
534 const struct kernel_param_ops param_ops_string = {
535 	.set = param_set_copystring,
536 	.get = param_get_string,
537 };
538 EXPORT_SYMBOL(param_ops_string);
539 
540 /* sysfs output in /sys/modules/XYZ/parameters/ */
541 #define to_module_attr(n) container_of_const(n, struct module_attribute, attr)
542 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
543 
544 struct param_attribute
545 {
546 	struct module_attribute mattr;
547 	const struct kernel_param *param;
548 };
549 
550 struct module_param_attrs
551 {
552 	unsigned int num;
553 	struct attribute_group grp;
554 	struct param_attribute attrs[] __counted_by(num);
555 };
556 
557 #ifdef CONFIG_SYSFS
558 #define to_param_attr(n) container_of_const(n, struct param_attribute, mattr)
559 
param_attr_show(const struct module_attribute * mattr,struct module_kobject * mk,char * buf)560 static ssize_t param_attr_show(const struct module_attribute *mattr,
561 			       struct module_kobject *mk, char *buf)
562 {
563 	int count;
564 	const struct param_attribute *attribute = to_param_attr(mattr);
565 
566 	if (!attribute->param->ops->get)
567 		return -EPERM;
568 
569 	kernel_param_lock(mk->mod);
570 	count = attribute->param->ops->get(buf, attribute->param);
571 	kernel_param_unlock(mk->mod);
572 	return count;
573 }
574 
575 /* sysfs always hands a nul-terminated string in buf.  We rely on that. */
param_attr_store(const struct module_attribute * mattr,struct module_kobject * mk,const char * buf,size_t len)576 static ssize_t param_attr_store(const struct module_attribute *mattr,
577 				struct module_kobject *mk,
578 				const char *buf, size_t len)
579 {
580  	int err;
581 	const struct param_attribute *attribute = to_param_attr(mattr);
582 
583 	if (!attribute->param->ops->set)
584 		return -EPERM;
585 
586 	kernel_param_lock(mk->mod);
587 	if (param_check_unsafe(attribute->param))
588 		err = attribute->param->ops->set(buf, attribute->param);
589 	else
590 		err = -EPERM;
591 	kernel_param_unlock(mk->mod);
592 	if (!err)
593 		return len;
594 	return err;
595 }
596 #endif
597 
598 #ifdef CONFIG_MODULES
599 #define __modinit
600 #else
601 #define __modinit __init
602 #endif
603 
604 #ifdef CONFIG_SYSFS
kernel_param_lock(struct module * mod)605 void kernel_param_lock(struct module *mod)
606 {
607 	mutex_lock(KPARAM_MUTEX(mod));
608 }
609 
kernel_param_unlock(struct module * mod)610 void kernel_param_unlock(struct module *mod)
611 {
612 	mutex_unlock(KPARAM_MUTEX(mod));
613 }
614 
615 EXPORT_SYMBOL(kernel_param_lock);
616 EXPORT_SYMBOL(kernel_param_unlock);
617 
618 /*
619  * add_sysfs_param - add a parameter to sysfs
620  * @mk: struct module_kobject
621  * @kp: the actual parameter definition to add to sysfs
622  * @name: name of parameter
623  *
624  * Create a kobject if for a (per-module) parameter if mp NULL, and
625  * create file in sysfs.  Returns an error on out of memory.  Always cleans up
626  * if there's an error.
627  */
add_sysfs_param(struct module_kobject * mk,const struct kernel_param * kp,const char * name)628 static __modinit int add_sysfs_param(struct module_kobject *mk,
629 				     const struct kernel_param *kp,
630 				     const char *name)
631 {
632 	struct module_param_attrs *new_mp;
633 	struct attribute **new_attrs;
634 	unsigned int i;
635 
636 	/* We don't bother calling this with invisible parameters. */
637 	BUG_ON(!kp->perm);
638 
639 	if (!mk->mp) {
640 		/* First allocation. */
641 		mk->mp = kzalloc(sizeof(*mk->mp), GFP_KERNEL);
642 		if (!mk->mp)
643 			return -ENOMEM;
644 		mk->mp->grp.name = "parameters";
645 		/* NULL-terminated attribute array. */
646 		mk->mp->grp.attrs = kzalloc(sizeof(mk->mp->grp.attrs[0]),
647 					    GFP_KERNEL);
648 		/* Caller will cleanup via free_module_param_attrs */
649 		if (!mk->mp->grp.attrs)
650 			return -ENOMEM;
651 	}
652 
653 	/* Enlarge allocations. */
654 	new_mp = krealloc(mk->mp, struct_size(mk->mp, attrs, mk->mp->num + 1),
655 			  GFP_KERNEL);
656 	if (!new_mp)
657 		return -ENOMEM;
658 	mk->mp = new_mp;
659 	mk->mp->num++;
660 
661 	/* Extra pointer for NULL terminator */
662 	new_attrs = krealloc_array(mk->mp->grp.attrs, mk->mp->num + 1,
663 				   sizeof(mk->mp->grp.attrs[0]), GFP_KERNEL);
664 	if (!new_attrs)
665 		return -ENOMEM;
666 	mk->mp->grp.attrs = new_attrs;
667 
668 	/* Tack new one on the end. */
669 	memset(&mk->mp->attrs[mk->mp->num - 1], 0, sizeof(mk->mp->attrs[0]));
670 	sysfs_attr_init(&mk->mp->attrs[mk->mp->num - 1].mattr.attr);
671 	mk->mp->attrs[mk->mp->num - 1].param = kp;
672 	mk->mp->attrs[mk->mp->num - 1].mattr.show = param_attr_show;
673 	/* Do not allow runtime DAC changes to make param writable. */
674 	if ((kp->perm & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0)
675 		mk->mp->attrs[mk->mp->num - 1].mattr.store = param_attr_store;
676 	else
677 		mk->mp->attrs[mk->mp->num - 1].mattr.store = NULL;
678 	mk->mp->attrs[mk->mp->num - 1].mattr.attr.name = (char *)name;
679 	mk->mp->attrs[mk->mp->num - 1].mattr.attr.mode = kp->perm;
680 
681 	/* Fix up all the pointers, since krealloc can move us */
682 	for (i = 0; i < mk->mp->num; i++)
683 		mk->mp->grp.attrs[i] = &mk->mp->attrs[i].mattr.attr;
684 	mk->mp->grp.attrs[mk->mp->num] = NULL;
685 	return 0;
686 }
687 
688 #ifdef CONFIG_MODULES
free_module_param_attrs(struct module_kobject * mk)689 static void free_module_param_attrs(struct module_kobject *mk)
690 {
691 	if (mk->mp)
692 		kfree(mk->mp->grp.attrs);
693 	kfree(mk->mp);
694 	mk->mp = NULL;
695 }
696 
697 /*
698  * module_param_sysfs_setup - setup sysfs support for one module
699  * @mod: module
700  * @kparam: module parameters (array)
701  * @num_params: number of module parameters
702  *
703  * Adds sysfs entries for module parameters under
704  * /sys/module/[mod->name]/parameters/
705  */
module_param_sysfs_setup(struct module * mod,const struct kernel_param * kparam,unsigned int num_params)706 int module_param_sysfs_setup(struct module *mod,
707 			     const struct kernel_param *kparam,
708 			     unsigned int num_params)
709 {
710 	int i, err;
711 	bool params = false;
712 
713 	for (i = 0; i < num_params; i++) {
714 		if (kparam[i].perm == 0)
715 			continue;
716 		err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);
717 		if (err) {
718 			free_module_param_attrs(&mod->mkobj);
719 			return err;
720 		}
721 		params = true;
722 	}
723 
724 	if (!params)
725 		return 0;
726 
727 	/* Create the param group. */
728 	err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
729 	if (err)
730 		free_module_param_attrs(&mod->mkobj);
731 	return err;
732 }
733 
734 /*
735  * module_param_sysfs_remove - remove sysfs support for one module
736  * @mod: module
737  *
738  * Remove sysfs entries for module parameters and the corresponding
739  * kobject.
740  */
module_param_sysfs_remove(struct module * mod)741 void module_param_sysfs_remove(struct module *mod)
742 {
743 	if (mod->mkobj.mp) {
744 		sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
745 		/*
746 		 * We are positive that no one is using any param
747 		 * attrs at this point. Deallocate immediately.
748 		 */
749 		free_module_param_attrs(&mod->mkobj);
750 	}
751 }
752 #endif
753 
destroy_params(const struct kernel_param * params,unsigned num)754 void destroy_params(const struct kernel_param *params, unsigned num)
755 {
756 	unsigned int i;
757 
758 	for (i = 0; i < num; i++)
759 		if (params[i].ops->free)
760 			params[i].ops->free(params[i].arg);
761 }
762 
lookup_or_create_module_kobject(const char * name)763 struct module_kobject __modinit * lookup_or_create_module_kobject(const char *name)
764 {
765 	struct module_kobject *mk;
766 	struct kobject *kobj;
767 	int err;
768 
769 	kobj = kset_find_obj(module_kset, name);
770 	if (kobj)
771 		return to_module_kobject(kobj);
772 
773 	mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
774 	if (!mk)
775 		return NULL;
776 
777 	mk->mod = THIS_MODULE;
778 	mk->kobj.kset = module_kset;
779 	err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL, "%s", name);
780 	if (IS_ENABLED(CONFIG_MODULES) && !err)
781 		err = sysfs_create_file(&mk->kobj, &module_uevent.attr);
782 	if (err) {
783 		kobject_put(&mk->kobj);
784 		pr_crit("Adding module '%s' to sysfs failed (%d), the system may be unstable.\n",
785 			name, err);
786 		return NULL;
787 	}
788 
789 	/* So that we hold reference in both cases. */
790 	kobject_get(&mk->kobj);
791 
792 	return mk;
793 }
794 
kernel_add_sysfs_param(const char * name,const struct kernel_param * kparam,unsigned int name_skip)795 static void __init kernel_add_sysfs_param(const char *name,
796 					  const struct kernel_param *kparam,
797 					  unsigned int name_skip)
798 {
799 	struct module_kobject *mk;
800 	int err;
801 
802 	mk = lookup_or_create_module_kobject(name);
803 	if (!mk)
804 		return;
805 
806 	/* We need to remove old parameters before adding more. */
807 	if (mk->mp)
808 		sysfs_remove_group(&mk->kobj, &mk->mp->grp);
809 
810 	/* These should not fail at boot. */
811 	err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
812 	BUG_ON(err);
813 	err = sysfs_create_group(&mk->kobj, &mk->mp->grp);
814 	BUG_ON(err);
815 	kobject_uevent(&mk->kobj, KOBJ_ADD);
816 	kobject_put(&mk->kobj);
817 }
818 
819 /*
820  * param_sysfs_builtin - add sysfs parameters for built-in modules
821  *
822  * Add module_parameters to sysfs for "modules" built into the kernel.
823  *
824  * The "module" name (KBUILD_MODNAME) is stored before a dot, the
825  * "parameter" name is stored behind a dot in kernel_param->name. So,
826  * extract the "module" name for all built-in kernel_param-eters,
827  * and for all who have the same, call kernel_add_sysfs_param.
828  */
param_sysfs_builtin(void)829 static void __init param_sysfs_builtin(void)
830 {
831 	const struct kernel_param *kp;
832 	unsigned int name_len;
833 	char modname[MODULE_NAME_LEN];
834 
835 	for (kp = __start___param; kp < __stop___param; kp++) {
836 		char *dot;
837 
838 		if (kp->perm == 0)
839 			continue;
840 
841 		dot = strchr(kp->name, '.');
842 		if (!dot) {
843 			/* This happens for core_param() */
844 			strcpy(modname, "kernel");
845 			name_len = 0;
846 		} else {
847 			name_len = dot - kp->name + 1;
848 			strscpy(modname, kp->name, name_len);
849 		}
850 		kernel_add_sysfs_param(modname, kp, name_len);
851 	}
852 }
853 
__modver_version_show(const struct module_attribute * mattr,struct module_kobject * mk,char * buf)854 ssize_t __modver_version_show(const struct module_attribute *mattr,
855 			      struct module_kobject *mk, char *buf)
856 {
857 	const struct module_version_attribute *vattr =
858 		container_of_const(mattr, struct module_version_attribute, mattr);
859 
860 	return scnprintf(buf, PAGE_SIZE, "%s\n", vattr->version);
861 }
862 
863 extern const struct module_version_attribute __start___modver[];
864 extern const struct module_version_attribute __stop___modver[];
865 
version_sysfs_builtin(void)866 static void __init version_sysfs_builtin(void)
867 {
868 	const struct module_version_attribute *vattr;
869 	struct module_kobject *mk;
870 	int err;
871 
872 	for (vattr = __start___modver; vattr < __stop___modver; vattr++) {
873 		mk = lookup_or_create_module_kobject(vattr->module_name);
874 		if (mk) {
875 			err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
876 			WARN_ON_ONCE(err);
877 			kobject_uevent(&mk->kobj, KOBJ_ADD);
878 			kobject_put(&mk->kobj);
879 		}
880 	}
881 }
882 
883 /* module-related sysfs stuff */
884 
module_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)885 static ssize_t module_attr_show(struct kobject *kobj,
886 				struct attribute *attr,
887 				char *buf)
888 {
889 	const struct module_attribute *attribute;
890 	struct module_kobject *mk;
891 	int ret;
892 
893 	attribute = to_module_attr(attr);
894 	mk = to_module_kobject(kobj);
895 
896 	if (!attribute->show)
897 		return -EIO;
898 
899 	ret = attribute->show(attribute, mk, buf);
900 
901 	return ret;
902 }
903 
module_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)904 static ssize_t module_attr_store(struct kobject *kobj,
905 				struct attribute *attr,
906 				const char *buf, size_t len)
907 {
908 	const struct module_attribute *attribute;
909 	struct module_kobject *mk;
910 	int ret;
911 
912 	attribute = to_module_attr(attr);
913 	mk = to_module_kobject(kobj);
914 
915 	if (!attribute->store)
916 		return -EIO;
917 
918 	ret = attribute->store(attribute, mk, buf, len);
919 
920 	return ret;
921 }
922 
923 static const struct sysfs_ops module_sysfs_ops = {
924 	.show = module_attr_show,
925 	.store = module_attr_store,
926 };
927 
uevent_filter(const struct kobject * kobj)928 static int uevent_filter(const struct kobject *kobj)
929 {
930 	const struct kobj_type *ktype = get_ktype(kobj);
931 
932 	if (ktype == &module_ktype)
933 		return 1;
934 	return 0;
935 }
936 
937 static const struct kset_uevent_ops module_uevent_ops = {
938 	.filter = uevent_filter,
939 };
940 
941 struct kset *module_kset;
942 
module_kobj_release(struct kobject * kobj)943 static void module_kobj_release(struct kobject *kobj)
944 {
945 	struct module_kobject *mk = to_module_kobject(kobj);
946 
947 	if (mk->kobj_completion)
948 		complete(mk->kobj_completion);
949 }
950 
951 const struct kobj_type module_ktype = {
952 	.release   =	module_kobj_release,
953 	.sysfs_ops =	&module_sysfs_ops,
954 };
955 
956 /*
957  * param_sysfs_init - create "module" kset
958  *
959  * This must be done before the initramfs is unpacked and
960  * request_module() thus becomes possible, because otherwise the
961  * module load would fail in mod_sysfs_init.
962  */
param_sysfs_init(void)963 static int __init param_sysfs_init(void)
964 {
965 	module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);
966 	if (!module_kset) {
967 		printk(KERN_WARNING "%s (%d): error creating kset\n",
968 			__FILE__, __LINE__);
969 		return -ENOMEM;
970 	}
971 
972 	return 0;
973 }
974 subsys_initcall(param_sysfs_init);
975 
976 /*
977  * param_sysfs_builtin_init - add sysfs version and parameter
978  * attributes for built-in modules
979  */
param_sysfs_builtin_init(void)980 static int __init param_sysfs_builtin_init(void)
981 {
982 	if (!module_kset)
983 		return -ENOMEM;
984 
985 	version_sysfs_builtin();
986 	param_sysfs_builtin();
987 
988 	return 0;
989 }
990 late_initcall(param_sysfs_builtin_init);
991 
992 #endif /* CONFIG_SYSFS */
993