module: make it possible to have unsafe, tainting module params
[deliverable/linux.git] / include / linux / moduleparam.h
CommitLineData
1da177e4
LT
1#ifndef _LINUX_MODULE_PARAMS_H
2#define _LINUX_MODULE_PARAMS_H
3/* (C) Copyright 2001, 2002 Rusty Russell IBM Corporation */
4#include <linux/init.h>
5#include <linux/stringify.h>
6#include <linux/kernel.h>
7
8/* You can override this manually, but generally this should match the
9 module name. */
10#ifdef MODULE
11#define MODULE_PARAM_PREFIX /* empty */
12#else
367cb704 13#define MODULE_PARAM_PREFIX KBUILD_MODNAME "."
1da177e4
LT
14#endif
15
730b69d2
RR
16/* Chosen so that structs with an unsigned long line up. */
17#define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
18
b75be420 19#ifdef MODULE
1da177e4 20#define __MODULE_INFO(tag, name, info) \
34182eea 21static const char __UNIQUE_ID(name)[] \
b6472776
JB
22 __used __attribute__((section(".modinfo"), unused, aligned(1))) \
23 = __stringify(tag) "=" info
1da177e4 24#else /* !MODULE */
b75be420
LW
25/* This struct is here for syntactic coherency, it is not used */
26#define __MODULE_INFO(tag, name, info) \
34182eea 27 struct __UNIQUE_ID(name) {}
1da177e4
LT
28#endif
29#define __MODULE_PARM_TYPE(name, _type) \
30 __MODULE_INFO(parmtype, name##type, #name ":" _type)
31
639938eb
PG
32/* One for each parameter, describing how to use it. Some files do
33 multiple of these per line, so can't just use MODULE_INFO. */
34#define MODULE_PARM_DESC(_parm, desc) \
35 __MODULE_INFO(parm, _parm, #_parm ":" desc)
36
1da177e4
LT
37struct kernel_param;
38
ab013c5f
SR
39/*
40 * Flags available for kernel_param_ops
41 *
42 * NOARG - the parameter allows for no argument (foo instead of foo=1)
43 */
44enum {
6a4c2643 45 KERNEL_PARAM_OPS_FL_NOARG = (1 << 0)
ab013c5f
SR
46};
47
9bbb9e5a 48struct kernel_param_ops {
ab013c5f
SR
49 /* How the ops should behave */
50 unsigned int flags;
9bbb9e5a
RR
51 /* Returns 0, or -errno. arg is in kp->arg. */
52 int (*set)(const char *val, const struct kernel_param *kp);
53 /* Returns length written or -errno. Buffer is 4k (ie. be short!) */
54 int (*get)(char *buffer, const struct kernel_param *kp);
e6df34a4
RR
55 /* Optional function to free kp->arg when module unloaded. */
56 void (*free)(void *arg);
9bbb9e5a 57};
1da177e4 58
91f9d330
JN
59/*
60 * Flags available for kernel_param
61 *
62 * UNSAFE - the parameter is dangerous and setting it will taint the kernel
63 */
64enum {
65 KERNEL_PARAM_FL_UNSAFE = (1 << 0)
66};
67
1da177e4
LT
68struct kernel_param {
69 const char *name;
9bbb9e5a 70 const struct kernel_param_ops *ops;
45fcc70c 71 u16 perm;
91f9d330
JN
72 s8 level;
73 u8 flags;
22e48eaf
JB
74 union {
75 void *arg;
76 const struct kparam_string *str;
77 const struct kparam_array *arr;
78 };
1da177e4
LT
79};
80
81/* Special one for strings we want to copy into */
82struct kparam_string {
83 unsigned int maxlen;
84 char *string;
85};
86
87/* Special one for arrays */
88struct kparam_array
89{
90 unsigned int max;
c5be0b2e 91 unsigned int elemsize;
1da177e4 92 unsigned int *num;
9bbb9e5a 93 const struct kernel_param_ops *ops;
1da177e4
LT
94 void *elem;
95};
96
546970bc
RR
97/**
98 * module_param - typesafe helper for a module/cmdline parameter
99 * @value: the variable to alter, and exposed parameter name.
100 * @type: the type of the parameter
101 * @perm: visibility in sysfs.
102 *
103 * @value becomes the module parameter, or (prefixed by KBUILD_MODNAME and a
104 * ".") the kernel commandline parameter. Note that - is changed to _, so
105 * the user can use "foo-bar=1" even for variable "foo_bar".
106 *
107 * @perm is 0 if the the variable is not to appear in sysfs, or 0444
108 * for world-readable, 0644 for root-writable, etc. Note that if it
109 * is writable, you may need to use kparam_block_sysfs_write() around
110 * accesses (esp. charp, which can be kfreed when it changes).
111 *
112 * The @type is simply pasted to refer to a param_ops_##type and a
113 * param_check_##type: for convenience many standard types are provided but
114 * you can create your own by defining those variables.
115 *
116 * Standard types are:
117 * byte, short, ushort, int, uint, long, ulong
118 * charp: a character pointer
119 * bool: a bool, values 0/1, y/n, Y/N.
120 * invbool: the above, only sense-reversed (N = true).
121 */
122#define module_param(name, type, perm) \
123 module_param_named(name, name, type, perm)
124
125/**
126 * module_param_named - typesafe helper for a renamed module/cmdline parameter
127 * @name: a valid C identifier which is the parameter name.
128 * @value: the actual lvalue to alter.
129 * @type: the type of the parameter
130 * @perm: visibility in sysfs.
131 *
132 * Usually it's a good idea to have variable names and user-exposed names the
133 * same, but that's harder if the variable must be non-static or is inside a
134 * structure. This allows exposure under a different name.
135 */
136#define module_param_named(name, value, type, perm) \
137 param_check_##type(name, &(value)); \
138 module_param_cb(name, &param_ops_##type, &value, perm); \
139 __MODULE_PARM_TYPE(name, #type)
140
141/**
142 * module_param_cb - general callback for a module/cmdline parameter
143 * @name: a valid C identifier which is the parameter name.
144 * @ops: the set & get operations for this parameter.
145 * @perm: visibility in sysfs.
146 *
147 * The ops can have NULL set or get functions.
148 */
149#define module_param_cb(name, ops, arg, perm) \
91f9d330 150 __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, -1, 0)
026cee00
PM
151
152/**
153 * <level>_param_cb - general callback for a module/cmdline parameter
154 * to be evaluated before certain initcall level
155 * @name: a valid C identifier which is the parameter name.
156 * @ops: the set & get operations for this parameter.
157 * @perm: visibility in sysfs.
158 *
159 * The ops can have NULL set or get functions.
160 */
161#define __level_param_cb(name, ops, arg, perm, level) \
91f9d330 162 __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, level, 0)
026cee00
PM
163
164#define core_param_cb(name, ops, arg, perm) \
165 __level_param_cb(name, ops, arg, perm, 1)
166
167#define postcore_param_cb(name, ops, arg, perm) \
168 __level_param_cb(name, ops, arg, perm, 2)
169
170#define arch_param_cb(name, ops, arg, perm) \
171 __level_param_cb(name, ops, arg, perm, 3)
172
173#define subsys_param_cb(name, ops, arg, perm) \
174 __level_param_cb(name, ops, arg, perm, 4)
175
176#define fs_param_cb(name, ops, arg, perm) \
177 __level_param_cb(name, ops, arg, perm, 5)
178
179#define device_param_cb(name, ops, arg, perm) \
180 __level_param_cb(name, ops, arg, perm, 6)
181
182#define late_param_cb(name, ops, arg, perm) \
183 __level_param_cb(name, ops, arg, perm, 7)
546970bc 184
91d35dd9
IK
185/* On alpha, ia64 and ppc64 relocations to global data cannot go into
186 read-only sections (which is part of respective UNIX ABI on these
187 platforms). So 'const' makes no sense and even causes compile failures
188 with some compilers. */
189#if defined(CONFIG_ALPHA) || defined(CONFIG_IA64) || defined(CONFIG_PPC64)
190#define __moduleparam_const
191#else
192#define __moduleparam_const const
193#endif
194
1da177e4 195/* This is the fundamental function for registering boot/module
546970bc 196 parameters. */
91f9d330 197#define __module_param_call(prefix, name, ops, arg, perm, level, flags) \
9774a1f5 198 /* Default value instead of permissions? */ \
58f86cc8 199 static const char __param_str_##name[] = prefix #name; \
91d35dd9 200 static struct kernel_param __moduleparam_const __param_##name \
3ff6eecc 201 __used \
1da177e4 202 __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \
58f86cc8 203 = { __param_str_##name, ops, VERIFY_OCTAL_PERMISSIONS(perm), \
91f9d330 204 level, flags, { arg } }
9bbb9e5a
RR
205
206/* Obsolete - use module_param_cb() */
207#define module_param_call(name, set, get, arg, perm) \
208 static struct kernel_param_ops __param_ops_##name = \
ab013c5f 209 { 0, (void *)set, (void *)get }; \
9bbb9e5a
RR
210 __module_param_call(MODULE_PARAM_PREFIX, \
211 name, &__param_ops_##name, arg, \
91f9d330 212 (perm) + sizeof(__check_old_set_param(set))*0, -1, 0)
9bbb9e5a
RR
213
214/* We don't get oldget: it's often a new-style param_get_uint, etc. */
215static inline int
216__check_old_set_param(int (*oldset)(const char *, struct kernel_param *))
217{
218 return 0;
219}
1da177e4 220
907b29eb
RR
221/**
222 * kparam_block_sysfs_write - make sure a parameter isn't written via sysfs.
223 * @name: the name of the parameter
224 *
225 * There's no point blocking write on a paramter that isn't writable via sysfs!
226 */
227#define kparam_block_sysfs_write(name) \
228 do { \
229 BUG_ON(!(__param_##name.perm & 0222)); \
230 __kernel_param_lock(); \
231 } while (0)
232
233/**
234 * kparam_unblock_sysfs_write - allows sysfs to write to a parameter again.
235 * @name: the name of the parameter
236 */
237#define kparam_unblock_sysfs_write(name) \
238 do { \
239 BUG_ON(!(__param_##name.perm & 0222)); \
240 __kernel_param_unlock(); \
241 } while (0)
242
243/**
244 * kparam_block_sysfs_read - make sure a parameter isn't read via sysfs.
245 * @name: the name of the parameter
246 *
247 * This also blocks sysfs writes.
248 */
249#define kparam_block_sysfs_read(name) \
250 do { \
251 BUG_ON(!(__param_##name.perm & 0444)); \
252 __kernel_param_lock(); \
253 } while (0)
254
255/**
256 * kparam_unblock_sysfs_read - allows sysfs to read a parameter again.
257 * @name: the name of the parameter
258 */
259#define kparam_unblock_sysfs_read(name) \
260 do { \
261 BUG_ON(!(__param_##name.perm & 0444)); \
262 __kernel_param_unlock(); \
263 } while (0)
264
265#ifdef CONFIG_SYSFS
266extern void __kernel_param_lock(void);
267extern void __kernel_param_unlock(void);
268#else
269static inline void __kernel_param_lock(void)
270{
271}
272static inline void __kernel_param_unlock(void)
273{
274}
275#endif
276
67e67cea
RR
277#ifndef MODULE
278/**
279 * core_param - define a historical core kernel parameter.
280 * @name: the name of the cmdline and sysfs parameter (often the same as var)
281 * @var: the variable
546970bc 282 * @type: the type of the parameter
67e67cea
RR
283 * @perm: visibility in sysfs
284 *
285 * core_param is just like module_param(), but cannot be modular and
286 * doesn't add a prefix (such as "printk."). This is for compatibility
287 * with __setup(), and it makes sense as truly core parameters aren't
288 * tied to the particular file they're in.
289 */
290#define core_param(name, var, type, perm) \
291 param_check_##type(name, &(var)); \
91f9d330 292 __module_param_call("", name, &param_ops_##type, &var, perm, -1, 0)
67e67cea
RR
293#endif /* !MODULE */
294
546970bc
RR
295/**
296 * module_param_string - a char array parameter
297 * @name: the name of the parameter
298 * @string: the string variable
299 * @len: the maximum length of the string, incl. terminator
300 * @perm: visibility in sysfs.
301 *
302 * This actually copies the string when it's set (unlike type charp).
303 * @len is usually just sizeof(string).
304 */
1da177e4 305#define module_param_string(name, string, len, perm) \
22e48eaf 306 static const struct kparam_string __param_string_##name \
1da177e4 307 = { len, string }; \
fddd5201 308 __module_param_call(MODULE_PARAM_PREFIX, name, \
9bbb9e5a 309 &param_ops_string, \
91f9d330 310 .str = &__param_string_##name, perm, -1, 0);\
1da177e4
LT
311 __MODULE_PARM_TYPE(name, "string")
312
b1e4d20c
MS
313/**
314 * parameq - checks if two parameter names match
315 * @name1: parameter name 1
316 * @name2: parameter name 2
317 *
318 * Returns true if the two parameter names are equal.
319 * Dashes (-) are considered equal to underscores (_).
320 */
321extern bool parameq(const char *name1, const char *name2);
322
323/**
324 * parameqn - checks if two parameter names match
325 * @name1: parameter name 1
326 * @name2: parameter name 2
327 * @n: the length to compare
328 *
329 * Similar to parameq(), except it compares @n characters.
330 */
331extern bool parameqn(const char *name1, const char *name2, size_t n);
332
1da177e4 333/* Called on module insert or kernel boot */
51e158c1 334extern char *parse_args(const char *name,
1da177e4 335 char *args,
914dcaa8 336 const struct kernel_param *params,
1da177e4 337 unsigned num,
026cee00
PM
338 s16 level_min,
339 s16 level_max,
9fb48c74
JC
340 int (*unknown)(char *param, char *val,
341 const char *doing));
1da177e4 342
e180a6b7
RR
343/* Called by module remove. */
344#ifdef CONFIG_SYSFS
345extern void destroy_params(const struct kernel_param *params, unsigned num);
346#else
347static inline void destroy_params(const struct kernel_param *params,
348 unsigned num)
349{
350}
351#endif /* !CONFIG_SYSFS */
352
1da177e4
LT
353/* All the helper functions */
354/* The macros to do compile-time type checking stolen from Jakub
355 Jelinek, who IIRC came up with this idea for the 2.4 module init code. */
356#define __param_check(name, p, type) \
0283f9a5 357 static inline type __always_unused *__check_##name(void) { return(p); }
1da177e4 358
91f9d330
JN
359/**
360 * param_check_unsafe - Warn and taint the kernel if setting dangerous options.
361 *
362 * This gets called from all the standard param setters, but can be used from
363 * custom setters as well.
364 */
365static inline void
366param_check_unsafe(const struct kernel_param *kp)
367{
368 if (kp->flags & KERNEL_PARAM_FL_UNSAFE) {
369 pr_warn("Setting dangerous option %s - tainting kernel\n",
370 kp->name);
371 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
372 }
373}
374
9bbb9e5a
RR
375extern struct kernel_param_ops param_ops_byte;
376extern int param_set_byte(const char *val, const struct kernel_param *kp);
377extern int param_get_byte(char *buffer, const struct kernel_param *kp);
1da177e4
LT
378#define param_check_byte(name, p) __param_check(name, p, unsigned char)
379
9bbb9e5a
RR
380extern struct kernel_param_ops param_ops_short;
381extern int param_set_short(const char *val, const struct kernel_param *kp);
382extern int param_get_short(char *buffer, const struct kernel_param *kp);
1da177e4
LT
383#define param_check_short(name, p) __param_check(name, p, short)
384
9bbb9e5a
RR
385extern struct kernel_param_ops param_ops_ushort;
386extern int param_set_ushort(const char *val, const struct kernel_param *kp);
387extern int param_get_ushort(char *buffer, const struct kernel_param *kp);
1da177e4
LT
388#define param_check_ushort(name, p) __param_check(name, p, unsigned short)
389
9bbb9e5a
RR
390extern struct kernel_param_ops param_ops_int;
391extern int param_set_int(const char *val, const struct kernel_param *kp);
392extern int param_get_int(char *buffer, const struct kernel_param *kp);
1da177e4
LT
393#define param_check_int(name, p) __param_check(name, p, int)
394
9bbb9e5a
RR
395extern struct kernel_param_ops param_ops_uint;
396extern int param_set_uint(const char *val, const struct kernel_param *kp);
397extern int param_get_uint(char *buffer, const struct kernel_param *kp);
1da177e4
LT
398#define param_check_uint(name, p) __param_check(name, p, unsigned int)
399
9bbb9e5a
RR
400extern struct kernel_param_ops param_ops_long;
401extern int param_set_long(const char *val, const struct kernel_param *kp);
402extern int param_get_long(char *buffer, const struct kernel_param *kp);
1da177e4
LT
403#define param_check_long(name, p) __param_check(name, p, long)
404
9bbb9e5a
RR
405extern struct kernel_param_ops param_ops_ulong;
406extern int param_set_ulong(const char *val, const struct kernel_param *kp);
407extern int param_get_ulong(char *buffer, const struct kernel_param *kp);
1da177e4
LT
408#define param_check_ulong(name, p) __param_check(name, p, unsigned long)
409
b4210b81
HR
410extern struct kernel_param_ops param_ops_ullong;
411extern int param_set_ullong(const char *val, const struct kernel_param *kp);
412extern int param_get_ullong(char *buffer, const struct kernel_param *kp);
413#define param_check_ullong(name, p) __param_check(name, p, unsigned long long)
414
9bbb9e5a
RR
415extern struct kernel_param_ops param_ops_charp;
416extern int param_set_charp(const char *val, const struct kernel_param *kp);
417extern int param_get_charp(char *buffer, const struct kernel_param *kp);
1da177e4
LT
418#define param_check_charp(name, p) __param_check(name, p, char *)
419
72db395f 420/* We used to allow int as well as bool. We're taking that away! */
9bbb9e5a
RR
421extern struct kernel_param_ops param_ops_bool;
422extern int param_set_bool(const char *val, const struct kernel_param *kp);
423extern int param_get_bool(char *buffer, const struct kernel_param *kp);
72db395f 424#define param_check_bool(name, p) __param_check(name, p, bool)
1da177e4 425
9bbb9e5a
RR
426extern struct kernel_param_ops param_ops_invbool;
427extern int param_set_invbool(const char *val, const struct kernel_param *kp);
428extern int param_get_invbool(char *buffer, const struct kernel_param *kp);
9a71af2c 429#define param_check_invbool(name, p) __param_check(name, p, bool)
1da177e4 430
69116f27
RR
431/* An int, which can only be set like a bool (though it shows as an int). */
432extern struct kernel_param_ops param_ops_bint;
433extern int param_set_bint(const char *val, const struct kernel_param *kp);
434#define param_get_bint param_get_int
435#define param_check_bint param_check_int
436
546970bc
RR
437/**
438 * module_param_array - a parameter which is an array of some type
439 * @name: the name of the array variable
440 * @type: the type, as per module_param()
441 * @nump: optional pointer filled in with the number written
442 * @perm: visibility in sysfs
443 *
444 * Input and output are as comma-separated values. Commas inside values
445 * don't work properly (eg. an array of charp).
446 *
447 * ARRAY_SIZE(@name) is used to determine the number of elements in the
448 * array, so the definition must be visible.
449 */
450#define module_param_array(name, type, nump, perm) \
451 module_param_array_named(name, name, type, nump, perm)
452
453/**
454 * module_param_array_named - renamed parameter which is an array of some type
455 * @name: a valid C identifier which is the parameter name
456 * @array: the name of the array variable
457 * @type: the type, as per module_param()
458 * @nump: optional pointer filled in with the number written
459 * @perm: visibility in sysfs
460 *
461 * This exposes a different name than the actual variable name. See
462 * module_param_named() for why this might be necessary.
463 */
1da177e4 464#define module_param_array_named(name, array, type, nump, perm) \
bafeafea 465 param_check_##type(name, &(array)[0]); \
22e48eaf 466 static const struct kparam_array __param_arr_##name \
c5be0b2e
RK
467 = { .max = ARRAY_SIZE(array), .num = nump, \
468 .ops = &param_ops_##type, \
469 .elemsize = sizeof(array[0]), .elem = array }; \
fddd5201 470 __module_param_call(MODULE_PARAM_PREFIX, name, \
9bbb9e5a 471 &param_array_ops, \
fddd5201 472 .arr = &__param_arr_##name, \
91f9d330 473 perm, -1, 0); \
1da177e4
LT
474 __MODULE_PARM_TYPE(name, "array of " #type)
475
9bbb9e5a 476extern struct kernel_param_ops param_array_ops;
1da177e4 477
9bbb9e5a
RR
478extern struct kernel_param_ops param_ops_string;
479extern int param_set_copystring(const char *val, const struct kernel_param *);
480extern int param_get_string(char *buffer, const struct kernel_param *kp);
1da177e4 481
b634d130 482/* for exporting parameters in /sys/module/.../parameters */
1da177e4
LT
483
484struct module;
485
ef665c1a 486#if defined(CONFIG_SYSFS) && defined(CONFIG_MODULES)
1da177e4 487extern int module_param_sysfs_setup(struct module *mod,
9bbb9e5a 488 const struct kernel_param *kparam,
1da177e4
LT
489 unsigned int num_params);
490
491extern void module_param_sysfs_remove(struct module *mod);
ef665c1a
RD
492#else
493static inline int module_param_sysfs_setup(struct module *mod,
9bbb9e5a 494 const struct kernel_param *kparam,
ef665c1a
RD
495 unsigned int num_params)
496{
497 return 0;
498}
499
500static inline void module_param_sysfs_remove(struct module *mod)
501{ }
502#endif
1da177e4
LT
503
504#endif /* _LINUX_MODULE_PARAMS_H */
This page took 1.05427 seconds and 5 git commands to generate.