xref: /linux-6.15/kernel/sysctl.c (revision fa89dbda)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * sysctl.c: General linux system control interface
4  *
5  * Begun 24 March 1995, Stephen Tweedie
6  * Added /proc support, Dec 1995
7  * Added bdflush entry and intvec min/max checking, 2/23/96, Tom Dyas.
8  * Added hooks for /proc/sys/net (minor, minor patch), 96/4/1, Mike Shaver.
9  * Added kernel/java-{interpreter,appletviewer}, 96/5/10, Mike Shaver.
10  * Dynamic registration fixes, Stephen Tweedie.
11  * Added kswapd-interval, ctrl-alt-del, printk stuff, 1/8/97, Chris Horn.
12  * Made sysctl support optional via CONFIG_SYSCTL, 1/10/97, Chris
13  *  Horn.
14  * Added proc_doulongvec_ms_jiffies_minmax, 09/08/99, Carlos H. Bauer.
15  * Added proc_doulongvec_minmax, 09/08/99, Carlos H. Bauer.
16  * Changed linked lists to use list.h instead of lists.h, 02/24/00, Bill
17  *  Wendling.
18  * The list_for_each() macro wasn't appropriate for the sysctl loop.
19  *  Removed it and replaced it with older style, 03/23/00, Bill Wendling
20  */
21 
22 #include <linux/module.h>
23 #include <linux/mm.h>
24 #include <linux/slab.h>
25 #include <linux/sysctl.h>
26 #include <linux/bitmap.h>
27 #include <linux/signal.h>
28 #include <linux/panic.h>
29 #include <linux/printk.h>
30 #include <linux/proc_fs.h>
31 #include <linux/security.h>
32 #include <linux/ctype.h>
33 #include <linux/kmemleak.h>
34 #include <linux/filter.h>
35 #include <linux/fs.h>
36 #include <linux/init.h>
37 #include <linux/kernel.h>
38 #include <linux/kobject.h>
39 #include <linux/net.h>
40 #include <linux/sysrq.h>
41 #include <linux/highuid.h>
42 #include <linux/writeback.h>
43 #include <linux/ratelimit.h>
44 #include <linux/hugetlb.h>
45 #include <linux/initrd.h>
46 #include <linux/key.h>
47 #include <linux/times.h>
48 #include <linux/limits.h>
49 #include <linux/syscalls.h>
50 #include <linux/nfs_fs.h>
51 #include <linux/acpi.h>
52 #include <linux/reboot.h>
53 #include <linux/ftrace.h>
54 #include <linux/perf_event.h>
55 #include <linux/oom.h>
56 #include <linux/kmod.h>
57 #include <linux/capability.h>
58 #include <linux/binfmts.h>
59 #include <linux/sched/sysctl.h>
60 #include <linux/mount.h>
61 #include <linux/userfaultfd_k.h>
62 #include <linux/pid.h>
63 
64 #include "../lib/kstrtox.h"
65 
66 #include <linux/uaccess.h>
67 #include <asm/processor.h>
68 
69 #ifdef CONFIG_X86
70 #include <asm/nmi.h>
71 #include <asm/stacktrace.h>
72 #include <asm/io.h>
73 #endif
74 #ifdef CONFIG_SPARC
75 #include <asm/setup.h>
76 #endif
77 #ifdef CONFIG_RT_MUTEXES
78 #include <linux/rtmutex.h>
79 #endif
80 
81 /* shared constants to be used in various sysctls */
82 const int sysctl_vals[] = { 0, 1, 2, 3, 4, 100, 200, 1000, 3000, INT_MAX, 65535, -1 };
83 EXPORT_SYMBOL(sysctl_vals);
84 
85 const unsigned long sysctl_long_vals[] = { 0, 1, LONG_MAX };
86 EXPORT_SYMBOL_GPL(sysctl_long_vals);
87 
88 #if defined(CONFIG_SYSCTL)
89 
90 /* Constants used for minimum and maximum */
91 
92 #ifdef CONFIG_PERF_EVENTS
93 static const int six_hundred_forty_kb = 640 * 1024;
94 #endif
95 
96 
97 static const int ngroups_max = NGROUPS_MAX;
98 static const int cap_last_cap = CAP_LAST_CAP;
99 
100 #ifdef CONFIG_PROC_SYSCTL
101 
102 /**
103  * enum sysctl_writes_mode - supported sysctl write modes
104  *
105  * @SYSCTL_WRITES_LEGACY: each write syscall must fully contain the sysctl value
106  *	to be written, and multiple writes on the same sysctl file descriptor
107  *	will rewrite the sysctl value, regardless of file position. No warning
108  *	is issued when the initial position is not 0.
109  * @SYSCTL_WRITES_WARN: same as above but warn when the initial file position is
110  *	not 0.
111  * @SYSCTL_WRITES_STRICT: writes to numeric sysctl entries must always be at
112  *	file position 0 and the value must be fully contained in the buffer
113  *	sent to the write syscall. If dealing with strings respect the file
114  *	position, but restrict this to the max length of the buffer, anything
115  *	passed the max length will be ignored. Multiple writes will append
116  *	to the buffer.
117  *
118  * These write modes control how current file position affects the behavior of
119  * updating sysctl values through the proc interface on each write.
120  */
121 enum sysctl_writes_mode {
122 	SYSCTL_WRITES_LEGACY		= -1,
123 	SYSCTL_WRITES_WARN		= 0,
124 	SYSCTL_WRITES_STRICT		= 1,
125 };
126 
127 static enum sysctl_writes_mode sysctl_writes_strict = SYSCTL_WRITES_STRICT;
128 #endif /* CONFIG_PROC_SYSCTL */
129 #endif /* CONFIG_SYSCTL */
130 
131 /*
132  * /proc/sys support
133  */
134 
135 #ifdef CONFIG_PROC_SYSCTL
136 
137 static int _proc_do_string(char *data, int maxlen, int write,
138 		char *buffer, size_t *lenp, loff_t *ppos)
139 {
140 	size_t len;
141 	char c, *p;
142 
143 	if (!data || !maxlen || !*lenp) {
144 		*lenp = 0;
145 		return 0;
146 	}
147 
148 	if (write) {
149 		if (sysctl_writes_strict == SYSCTL_WRITES_STRICT) {
150 			/* Only continue writes not past the end of buffer. */
151 			len = strlen(data);
152 			if (len > maxlen - 1)
153 				len = maxlen - 1;
154 
155 			if (*ppos > len)
156 				return 0;
157 			len = *ppos;
158 		} else {
159 			/* Start writing from beginning of buffer. */
160 			len = 0;
161 		}
162 
163 		*ppos += *lenp;
164 		p = buffer;
165 		while ((p - buffer) < *lenp && len < maxlen - 1) {
166 			c = *(p++);
167 			if (c == 0 || c == '\n')
168 				break;
169 			data[len++] = c;
170 		}
171 		data[len] = 0;
172 	} else {
173 		len = strlen(data);
174 		if (len > maxlen)
175 			len = maxlen;
176 
177 		if (*ppos > len) {
178 			*lenp = 0;
179 			return 0;
180 		}
181 
182 		data += *ppos;
183 		len  -= *ppos;
184 
185 		if (len > *lenp)
186 			len = *lenp;
187 		if (len)
188 			memcpy(buffer, data, len);
189 		if (len < *lenp) {
190 			buffer[len] = '\n';
191 			len++;
192 		}
193 		*lenp = len;
194 		*ppos += len;
195 	}
196 	return 0;
197 }
198 
199 static void warn_sysctl_write(const struct ctl_table *table)
200 {
201 	pr_warn_once("%s wrote to %s when file position was not 0!\n"
202 		"This will not be supported in the future. To silence this\n"
203 		"warning, set kernel.sysctl_writes_strict = -1\n",
204 		current->comm, table->procname);
205 }
206 
207 /**
208  * proc_first_pos_non_zero_ignore - check if first position is allowed
209  * @ppos: file position
210  * @table: the sysctl table
211  *
212  * Returns true if the first position is non-zero and the sysctl_writes_strict
213  * mode indicates this is not allowed for numeric input types. String proc
214  * handlers can ignore the return value.
215  */
216 static bool proc_first_pos_non_zero_ignore(loff_t *ppos,
217 					   const struct ctl_table *table)
218 {
219 	if (!*ppos)
220 		return false;
221 
222 	switch (sysctl_writes_strict) {
223 	case SYSCTL_WRITES_STRICT:
224 		return true;
225 	case SYSCTL_WRITES_WARN:
226 		warn_sysctl_write(table);
227 		return false;
228 	default:
229 		return false;
230 	}
231 }
232 
233 /**
234  * proc_dostring - read a string sysctl
235  * @table: the sysctl table
236  * @write: %TRUE if this is a write to the sysctl file
237  * @buffer: the user buffer
238  * @lenp: the size of the user buffer
239  * @ppos: file position
240  *
241  * Reads/writes a string from/to the user buffer. If the kernel
242  * buffer provided is not large enough to hold the string, the
243  * string is truncated. The copied string is %NULL-terminated.
244  * If the string is being read by the user process, it is copied
245  * and a newline '\n' is added. It is truncated if the buffer is
246  * not large enough.
247  *
248  * Returns 0 on success.
249  */
250 int proc_dostring(const struct ctl_table *table, int write,
251 		  void *buffer, size_t *lenp, loff_t *ppos)
252 {
253 	if (write)
254 		proc_first_pos_non_zero_ignore(ppos, table);
255 
256 	return _proc_do_string(table->data, table->maxlen, write, buffer, lenp,
257 			ppos);
258 }
259 
260 static void proc_skip_spaces(char **buf, size_t *size)
261 {
262 	while (*size) {
263 		if (!isspace(**buf))
264 			break;
265 		(*size)--;
266 		(*buf)++;
267 	}
268 }
269 
270 static void proc_skip_char(char **buf, size_t *size, const char v)
271 {
272 	while (*size) {
273 		if (**buf != v)
274 			break;
275 		(*size)--;
276 		(*buf)++;
277 	}
278 }
279 
280 /**
281  * strtoul_lenient - parse an ASCII formatted integer from a buffer and only
282  *                   fail on overflow
283  *
284  * @cp: kernel buffer containing the string to parse
285  * @endp: pointer to store the trailing characters
286  * @base: the base to use
287  * @res: where the parsed integer will be stored
288  *
289  * In case of success 0 is returned and @res will contain the parsed integer,
290  * @endp will hold any trailing characters.
291  * This function will fail the parse on overflow. If there wasn't an overflow
292  * the function will defer the decision what characters count as invalid to the
293  * caller.
294  */
295 static int strtoul_lenient(const char *cp, char **endp, unsigned int base,
296 			   unsigned long *res)
297 {
298 	unsigned long long result;
299 	unsigned int rv;
300 
301 	cp = _parse_integer_fixup_radix(cp, &base);
302 	rv = _parse_integer(cp, base, &result);
303 	if ((rv & KSTRTOX_OVERFLOW) || (result != (unsigned long)result))
304 		return -ERANGE;
305 
306 	cp += rv;
307 
308 	if (endp)
309 		*endp = (char *)cp;
310 
311 	*res = (unsigned long)result;
312 	return 0;
313 }
314 
315 #define TMPBUFLEN 22
316 /**
317  * proc_get_long - reads an ASCII formatted integer from a user buffer
318  *
319  * @buf: a kernel buffer
320  * @size: size of the kernel buffer
321  * @val: this is where the number will be stored
322  * @neg: set to %TRUE if number is negative
323  * @perm_tr: a vector which contains the allowed trailers
324  * @perm_tr_len: size of the perm_tr vector
325  * @tr: pointer to store the trailer character
326  *
327  * In case of success %0 is returned and @buf and @size are updated with
328  * the amount of bytes read. If @tr is non-NULL and a trailing
329  * character exists (size is non-zero after returning from this
330  * function), @tr is updated with the trailing character.
331  */
332 static int proc_get_long(char **buf, size_t *size,
333 			  unsigned long *val, bool *neg,
334 			  const char *perm_tr, unsigned perm_tr_len, char *tr)
335 {
336 	char *p, tmp[TMPBUFLEN];
337 	ssize_t len = *size;
338 
339 	if (len <= 0)
340 		return -EINVAL;
341 
342 	if (len > TMPBUFLEN - 1)
343 		len = TMPBUFLEN - 1;
344 
345 	memcpy(tmp, *buf, len);
346 
347 	tmp[len] = 0;
348 	p = tmp;
349 	if (*p == '-' && *size > 1) {
350 		*neg = true;
351 		p++;
352 	} else
353 		*neg = false;
354 	if (!isdigit(*p))
355 		return -EINVAL;
356 
357 	if (strtoul_lenient(p, &p, 0, val))
358 		return -EINVAL;
359 
360 	len = p - tmp;
361 
362 	/* We don't know if the next char is whitespace thus we may accept
363 	 * invalid integers (e.g. 1234...a) or two integers instead of one
364 	 * (e.g. 123...1). So lets not allow such large numbers. */
365 	if (len == TMPBUFLEN - 1)
366 		return -EINVAL;
367 
368 	if (len < *size && perm_tr_len && !memchr(perm_tr, *p, perm_tr_len))
369 		return -EINVAL;
370 
371 	if (tr && (len < *size))
372 		*tr = *p;
373 
374 	*buf += len;
375 	*size -= len;
376 
377 	return 0;
378 }
379 
380 /**
381  * proc_put_long - converts an integer to a decimal ASCII formatted string
382  *
383  * @buf: the user buffer
384  * @size: the size of the user buffer
385  * @val: the integer to be converted
386  * @neg: sign of the number, %TRUE for negative
387  *
388  * In case of success @buf and @size are updated with the amount of bytes
389  * written.
390  */
391 static void proc_put_long(void **buf, size_t *size, unsigned long val, bool neg)
392 {
393 	int len;
394 	char tmp[TMPBUFLEN], *p = tmp;
395 
396 	sprintf(p, "%s%lu", neg ? "-" : "", val);
397 	len = strlen(tmp);
398 	if (len > *size)
399 		len = *size;
400 	memcpy(*buf, tmp, len);
401 	*size -= len;
402 	*buf += len;
403 }
404 #undef TMPBUFLEN
405 
406 static void proc_put_char(void **buf, size_t *size, char c)
407 {
408 	if (*size) {
409 		char **buffer = (char **)buf;
410 		**buffer = c;
411 
412 		(*size)--;
413 		(*buffer)++;
414 		*buf = *buffer;
415 	}
416 }
417 
418 static int do_proc_dointvec_conv(bool *negp, unsigned long *lvalp,
419 				 int *valp,
420 				 int write, void *data)
421 {
422 	if (write) {
423 		if (*negp) {
424 			if (*lvalp > (unsigned long) INT_MAX + 1)
425 				return -EINVAL;
426 			WRITE_ONCE(*valp, -*lvalp);
427 		} else {
428 			if (*lvalp > (unsigned long) INT_MAX)
429 				return -EINVAL;
430 			WRITE_ONCE(*valp, *lvalp);
431 		}
432 	} else {
433 		int val = READ_ONCE(*valp);
434 		if (val < 0) {
435 			*negp = true;
436 			*lvalp = -(unsigned long)val;
437 		} else {
438 			*negp = false;
439 			*lvalp = (unsigned long)val;
440 		}
441 	}
442 	return 0;
443 }
444 
445 static int do_proc_douintvec_conv(unsigned long *lvalp,
446 				  unsigned int *valp,
447 				  int write, void *data)
448 {
449 	if (write) {
450 		if (*lvalp > UINT_MAX)
451 			return -EINVAL;
452 		WRITE_ONCE(*valp, *lvalp);
453 	} else {
454 		unsigned int val = READ_ONCE(*valp);
455 		*lvalp = (unsigned long)val;
456 	}
457 	return 0;
458 }
459 
460 static const char proc_wspace_sep[] = { ' ', '\t', '\n' };
461 
462 static int __do_proc_dointvec(void *tbl_data, const struct ctl_table *table,
463 		  int write, void *buffer,
464 		  size_t *lenp, loff_t *ppos,
465 		  int (*conv)(bool *negp, unsigned long *lvalp, int *valp,
466 			      int write, void *data),
467 		  void *data)
468 {
469 	int *i, vleft, first = 1, err = 0;
470 	size_t left;
471 	char *p;
472 
473 	if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) {
474 		*lenp = 0;
475 		return 0;
476 	}
477 
478 	i = (int *) tbl_data;
479 	vleft = table->maxlen / sizeof(*i);
480 	left = *lenp;
481 
482 	if (!conv)
483 		conv = do_proc_dointvec_conv;
484 
485 	if (write) {
486 		if (proc_first_pos_non_zero_ignore(ppos, table))
487 			goto out;
488 
489 		if (left > PAGE_SIZE - 1)
490 			left = PAGE_SIZE - 1;
491 		p = buffer;
492 	}
493 
494 	for (; left && vleft--; i++, first=0) {
495 		unsigned long lval;
496 		bool neg;
497 
498 		if (write) {
499 			proc_skip_spaces(&p, &left);
500 
501 			if (!left)
502 				break;
503 			err = proc_get_long(&p, &left, &lval, &neg,
504 					     proc_wspace_sep,
505 					     sizeof(proc_wspace_sep), NULL);
506 			if (err)
507 				break;
508 			if (conv(&neg, &lval, i, 1, data)) {
509 				err = -EINVAL;
510 				break;
511 			}
512 		} else {
513 			if (conv(&neg, &lval, i, 0, data)) {
514 				err = -EINVAL;
515 				break;
516 			}
517 			if (!first)
518 				proc_put_char(&buffer, &left, '\t');
519 			proc_put_long(&buffer, &left, lval, neg);
520 		}
521 	}
522 
523 	if (!write && !first && left && !err)
524 		proc_put_char(&buffer, &left, '\n');
525 	if (write && !err && left)
526 		proc_skip_spaces(&p, &left);
527 	if (write && first)
528 		return err ? : -EINVAL;
529 	*lenp -= left;
530 out:
531 	*ppos += *lenp;
532 	return err;
533 }
534 
535 static int do_proc_dointvec(const struct ctl_table *table, int write,
536 		  void *buffer, size_t *lenp, loff_t *ppos,
537 		  int (*conv)(bool *negp, unsigned long *lvalp, int *valp,
538 			      int write, void *data),
539 		  void *data)
540 {
541 	return __do_proc_dointvec(table->data, table, write,
542 			buffer, lenp, ppos, conv, data);
543 }
544 
545 static int do_proc_douintvec_w(unsigned int *tbl_data,
546 			       const struct ctl_table *table,
547 			       void *buffer,
548 			       size_t *lenp, loff_t *ppos,
549 			       int (*conv)(unsigned long *lvalp,
550 					   unsigned int *valp,
551 					   int write, void *data),
552 			       void *data)
553 {
554 	unsigned long lval;
555 	int err = 0;
556 	size_t left;
557 	bool neg;
558 	char *p = buffer;
559 
560 	left = *lenp;
561 
562 	if (proc_first_pos_non_zero_ignore(ppos, table))
563 		goto bail_early;
564 
565 	if (left > PAGE_SIZE - 1)
566 		left = PAGE_SIZE - 1;
567 
568 	proc_skip_spaces(&p, &left);
569 	if (!left) {
570 		err = -EINVAL;
571 		goto out_free;
572 	}
573 
574 	err = proc_get_long(&p, &left, &lval, &neg,
575 			     proc_wspace_sep,
576 			     sizeof(proc_wspace_sep), NULL);
577 	if (err || neg) {
578 		err = -EINVAL;
579 		goto out_free;
580 	}
581 
582 	if (conv(&lval, tbl_data, 1, data)) {
583 		err = -EINVAL;
584 		goto out_free;
585 	}
586 
587 	if (!err && left)
588 		proc_skip_spaces(&p, &left);
589 
590 out_free:
591 	if (err)
592 		return -EINVAL;
593 
594 	return 0;
595 
596 	/* This is in keeping with old __do_proc_dointvec() */
597 bail_early:
598 	*ppos += *lenp;
599 	return err;
600 }
601 
602 static int do_proc_douintvec_r(unsigned int *tbl_data, void *buffer,
603 			       size_t *lenp, loff_t *ppos,
604 			       int (*conv)(unsigned long *lvalp,
605 					   unsigned int *valp,
606 					   int write, void *data),
607 			       void *data)
608 {
609 	unsigned long lval;
610 	int err = 0;
611 	size_t left;
612 
613 	left = *lenp;
614 
615 	if (conv(&lval, tbl_data, 0, data)) {
616 		err = -EINVAL;
617 		goto out;
618 	}
619 
620 	proc_put_long(&buffer, &left, lval, false);
621 	if (!left)
622 		goto out;
623 
624 	proc_put_char(&buffer, &left, '\n');
625 
626 out:
627 	*lenp -= left;
628 	*ppos += *lenp;
629 
630 	return err;
631 }
632 
633 static int __do_proc_douintvec(void *tbl_data, const struct ctl_table *table,
634 			       int write, void *buffer,
635 			       size_t *lenp, loff_t *ppos,
636 			       int (*conv)(unsigned long *lvalp,
637 					   unsigned int *valp,
638 					   int write, void *data),
639 			       void *data)
640 {
641 	unsigned int *i, vleft;
642 
643 	if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) {
644 		*lenp = 0;
645 		return 0;
646 	}
647 
648 	i = (unsigned int *) tbl_data;
649 	vleft = table->maxlen / sizeof(*i);
650 
651 	/*
652 	 * Arrays are not supported, keep this simple. *Do not* add
653 	 * support for them.
654 	 */
655 	if (vleft != 1) {
656 		*lenp = 0;
657 		return -EINVAL;
658 	}
659 
660 	if (!conv)
661 		conv = do_proc_douintvec_conv;
662 
663 	if (write)
664 		return do_proc_douintvec_w(i, table, buffer, lenp, ppos,
665 					   conv, data);
666 	return do_proc_douintvec_r(i, buffer, lenp, ppos, conv, data);
667 }
668 
669 int do_proc_douintvec(const struct ctl_table *table, int write,
670 		      void *buffer, size_t *lenp, loff_t *ppos,
671 		      int (*conv)(unsigned long *lvalp,
672 				  unsigned int *valp,
673 				  int write, void *data),
674 		      void *data)
675 {
676 	return __do_proc_douintvec(table->data, table, write,
677 				   buffer, lenp, ppos, conv, data);
678 }
679 
680 /**
681  * proc_dobool - read/write a bool
682  * @table: the sysctl table
683  * @write: %TRUE if this is a write to the sysctl file
684  * @buffer: the user buffer
685  * @lenp: the size of the user buffer
686  * @ppos: file position
687  *
688  * Reads/writes one integer value from/to the user buffer,
689  * treated as an ASCII string.
690  *
691  * table->data must point to a bool variable and table->maxlen must
692  * be sizeof(bool).
693  *
694  * Returns 0 on success.
695  */
696 int proc_dobool(const struct ctl_table *table, int write, void *buffer,
697 		size_t *lenp, loff_t *ppos)
698 {
699 	struct ctl_table tmp;
700 	bool *data = table->data;
701 	int res, val;
702 
703 	/* Do not support arrays yet. */
704 	if (table->maxlen != sizeof(bool))
705 		return -EINVAL;
706 
707 	tmp = *table;
708 	tmp.maxlen = sizeof(val);
709 	tmp.data = &val;
710 
711 	val = READ_ONCE(*data);
712 	res = proc_dointvec(&tmp, write, buffer, lenp, ppos);
713 	if (res)
714 		return res;
715 	if (write)
716 		WRITE_ONCE(*data, val);
717 	return 0;
718 }
719 
720 /**
721  * proc_dointvec - read a vector of integers
722  * @table: the sysctl table
723  * @write: %TRUE if this is a write to the sysctl file
724  * @buffer: the user buffer
725  * @lenp: the size of the user buffer
726  * @ppos: file position
727  *
728  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
729  * values from/to the user buffer, treated as an ASCII string.
730  *
731  * Returns 0 on success.
732  */
733 int proc_dointvec(const struct ctl_table *table, int write, void *buffer,
734 		  size_t *lenp, loff_t *ppos)
735 {
736 	return do_proc_dointvec(table, write, buffer, lenp, ppos, NULL, NULL);
737 }
738 
739 /**
740  * proc_douintvec - read a vector of unsigned integers
741  * @table: the sysctl table
742  * @write: %TRUE if this is a write to the sysctl file
743  * @buffer: the user buffer
744  * @lenp: the size of the user buffer
745  * @ppos: file position
746  *
747  * Reads/writes up to table->maxlen/sizeof(unsigned int) unsigned integer
748  * values from/to the user buffer, treated as an ASCII string.
749  *
750  * Returns 0 on success.
751  */
752 int proc_douintvec(const struct ctl_table *table, int write, void *buffer,
753 		size_t *lenp, loff_t *ppos)
754 {
755 	return do_proc_douintvec(table, write, buffer, lenp, ppos,
756 				 do_proc_douintvec_conv, NULL);
757 }
758 
759 /*
760  * Taint values can only be increased
761  * This means we can safely use a temporary.
762  */
763 static int proc_taint(const struct ctl_table *table, int write,
764 			       void *buffer, size_t *lenp, loff_t *ppos)
765 {
766 	struct ctl_table t;
767 	unsigned long tmptaint = get_taint();
768 	int err;
769 
770 	if (write && !capable(CAP_SYS_ADMIN))
771 		return -EPERM;
772 
773 	t = *table;
774 	t.data = &tmptaint;
775 	err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
776 	if (err < 0)
777 		return err;
778 
779 	if (write) {
780 		int i;
781 
782 		/*
783 		 * If we are relying on panic_on_taint not producing
784 		 * false positives due to userspace input, bail out
785 		 * before setting the requested taint flags.
786 		 */
787 		if (panic_on_taint_nousertaint && (tmptaint & panic_on_taint))
788 			return -EINVAL;
789 
790 		/*
791 		 * Poor man's atomic or. Not worth adding a primitive
792 		 * to everyone's atomic.h for this
793 		 */
794 		for (i = 0; i < TAINT_FLAGS_COUNT; i++)
795 			if ((1UL << i) & tmptaint)
796 				add_taint(i, LOCKDEP_STILL_OK);
797 	}
798 
799 	return err;
800 }
801 
802 /**
803  * struct do_proc_dointvec_minmax_conv_param - proc_dointvec_minmax() range checking structure
804  * @min: pointer to minimum allowable value
805  * @max: pointer to maximum allowable value
806  *
807  * The do_proc_dointvec_minmax_conv_param structure provides the
808  * minimum and maximum values for doing range checking for those sysctl
809  * parameters that use the proc_dointvec_minmax() handler.
810  */
811 struct do_proc_dointvec_minmax_conv_param {
812 	int *min;
813 	int *max;
814 };
815 
816 static int do_proc_dointvec_minmax_conv(bool *negp, unsigned long *lvalp,
817 					int *valp,
818 					int write, void *data)
819 {
820 	int tmp, ret;
821 	struct do_proc_dointvec_minmax_conv_param *param = data;
822 	/*
823 	 * If writing, first do so via a temporary local int so we can
824 	 * bounds-check it before touching *valp.
825 	 */
826 	int *ip = write ? &tmp : valp;
827 
828 	ret = do_proc_dointvec_conv(negp, lvalp, ip, write, data);
829 	if (ret)
830 		return ret;
831 
832 	if (write) {
833 		if ((param->min && *param->min > tmp) ||
834 		    (param->max && *param->max < tmp))
835 			return -EINVAL;
836 		WRITE_ONCE(*valp, tmp);
837 	}
838 
839 	return 0;
840 }
841 
842 /**
843  * proc_dointvec_minmax - read a vector of integers with min/max values
844  * @table: the sysctl table
845  * @write: %TRUE if this is a write to the sysctl file
846  * @buffer: the user buffer
847  * @lenp: the size of the user buffer
848  * @ppos: file position
849  *
850  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
851  * values from/to the user buffer, treated as an ASCII string.
852  *
853  * This routine will ensure the values are within the range specified by
854  * table->extra1 (min) and table->extra2 (max).
855  *
856  * Returns 0 on success or -EINVAL on write when the range check fails.
857  */
858 int proc_dointvec_minmax(const struct ctl_table *table, int write,
859 		  void *buffer, size_t *lenp, loff_t *ppos)
860 {
861 	struct do_proc_dointvec_minmax_conv_param param = {
862 		.min = (int *) table->extra1,
863 		.max = (int *) table->extra2,
864 	};
865 	return do_proc_dointvec(table, write, buffer, lenp, ppos,
866 				do_proc_dointvec_minmax_conv, &param);
867 }
868 
869 /**
870  * struct do_proc_douintvec_minmax_conv_param - proc_douintvec_minmax() range checking structure
871  * @min: pointer to minimum allowable value
872  * @max: pointer to maximum allowable value
873  *
874  * The do_proc_douintvec_minmax_conv_param structure provides the
875  * minimum and maximum values for doing range checking for those sysctl
876  * parameters that use the proc_douintvec_minmax() handler.
877  */
878 struct do_proc_douintvec_minmax_conv_param {
879 	unsigned int *min;
880 	unsigned int *max;
881 };
882 
883 static int do_proc_douintvec_minmax_conv(unsigned long *lvalp,
884 					 unsigned int *valp,
885 					 int write, void *data)
886 {
887 	int ret;
888 	unsigned int tmp;
889 	struct do_proc_douintvec_minmax_conv_param *param = data;
890 	/* write via temporary local uint for bounds-checking */
891 	unsigned int *up = write ? &tmp : valp;
892 
893 	ret = do_proc_douintvec_conv(lvalp, up, write, data);
894 	if (ret)
895 		return ret;
896 
897 	if (write) {
898 		if ((param->min && *param->min > tmp) ||
899 		    (param->max && *param->max < tmp))
900 			return -ERANGE;
901 
902 		WRITE_ONCE(*valp, tmp);
903 	}
904 
905 	return 0;
906 }
907 
908 /**
909  * proc_douintvec_minmax - read a vector of unsigned ints with min/max values
910  * @table: the sysctl table
911  * @write: %TRUE if this is a write to the sysctl file
912  * @buffer: the user buffer
913  * @lenp: the size of the user buffer
914  * @ppos: file position
915  *
916  * Reads/writes up to table->maxlen/sizeof(unsigned int) unsigned integer
917  * values from/to the user buffer, treated as an ASCII string. Negative
918  * strings are not allowed.
919  *
920  * This routine will ensure the values are within the range specified by
921  * table->extra1 (min) and table->extra2 (max). There is a final sanity
922  * check for UINT_MAX to avoid having to support wrap around uses from
923  * userspace.
924  *
925  * Returns 0 on success or -ERANGE on write when the range check fails.
926  */
927 int proc_douintvec_minmax(const struct ctl_table *table, int write,
928 			  void *buffer, size_t *lenp, loff_t *ppos)
929 {
930 	struct do_proc_douintvec_minmax_conv_param param = {
931 		.min = (unsigned int *) table->extra1,
932 		.max = (unsigned int *) table->extra2,
933 	};
934 	return do_proc_douintvec(table, write, buffer, lenp, ppos,
935 				 do_proc_douintvec_minmax_conv, &param);
936 }
937 
938 /**
939  * proc_dou8vec_minmax - read a vector of unsigned chars with min/max values
940  * @table: the sysctl table
941  * @write: %TRUE if this is a write to the sysctl file
942  * @buffer: the user buffer
943  * @lenp: the size of the user buffer
944  * @ppos: file position
945  *
946  * Reads/writes up to table->maxlen/sizeof(u8) unsigned chars
947  * values from/to the user buffer, treated as an ASCII string. Negative
948  * strings are not allowed.
949  *
950  * This routine will ensure the values are within the range specified by
951  * table->extra1 (min) and table->extra2 (max).
952  *
953  * Returns 0 on success or an error on write when the range check fails.
954  */
955 int proc_dou8vec_minmax(const struct ctl_table *table, int write,
956 			void *buffer, size_t *lenp, loff_t *ppos)
957 {
958 	struct ctl_table tmp;
959 	unsigned int min = 0, max = 255U, val;
960 	u8 *data = table->data;
961 	struct do_proc_douintvec_minmax_conv_param param = {
962 		.min = &min,
963 		.max = &max,
964 	};
965 	int res;
966 
967 	/* Do not support arrays yet. */
968 	if (table->maxlen != sizeof(u8))
969 		return -EINVAL;
970 
971 	if (table->extra1)
972 		min = *(unsigned int *) table->extra1;
973 	if (table->extra2)
974 		max = *(unsigned int *) table->extra2;
975 
976 	tmp = *table;
977 
978 	tmp.maxlen = sizeof(val);
979 	tmp.data = &val;
980 	val = READ_ONCE(*data);
981 	res = do_proc_douintvec(&tmp, write, buffer, lenp, ppos,
982 				do_proc_douintvec_minmax_conv, &param);
983 	if (res)
984 		return res;
985 	if (write)
986 		WRITE_ONCE(*data, val);
987 	return 0;
988 }
989 EXPORT_SYMBOL_GPL(proc_dou8vec_minmax);
990 
991 #ifdef CONFIG_MAGIC_SYSRQ
992 static int sysrq_sysctl_handler(const struct ctl_table *table, int write,
993 				void *buffer, size_t *lenp, loff_t *ppos)
994 {
995 	int tmp, ret;
996 
997 	tmp = sysrq_mask();
998 
999 	ret = __do_proc_dointvec(&tmp, table, write, buffer,
1000 			       lenp, ppos, NULL, NULL);
1001 	if (ret || !write)
1002 		return ret;
1003 
1004 	if (write)
1005 		sysrq_toggle_support(tmp);
1006 
1007 	return 0;
1008 }
1009 #endif
1010 
1011 static int __do_proc_doulongvec_minmax(void *data,
1012 		const struct ctl_table *table, int write,
1013 		void *buffer, size_t *lenp, loff_t *ppos,
1014 		unsigned long convmul, unsigned long convdiv)
1015 {
1016 	unsigned long *i, *min, *max;
1017 	int vleft, first = 1, err = 0;
1018 	size_t left;
1019 	char *p;
1020 
1021 	if (!data || !table->maxlen || !*lenp || (*ppos && !write)) {
1022 		*lenp = 0;
1023 		return 0;
1024 	}
1025 
1026 	i = data;
1027 	min = table->extra1;
1028 	max = table->extra2;
1029 	vleft = table->maxlen / sizeof(unsigned long);
1030 	left = *lenp;
1031 
1032 	if (write) {
1033 		if (proc_first_pos_non_zero_ignore(ppos, table))
1034 			goto out;
1035 
1036 		if (left > PAGE_SIZE - 1)
1037 			left = PAGE_SIZE - 1;
1038 		p = buffer;
1039 	}
1040 
1041 	for (; left && vleft--; i++, first = 0) {
1042 		unsigned long val;
1043 
1044 		if (write) {
1045 			bool neg;
1046 
1047 			proc_skip_spaces(&p, &left);
1048 			if (!left)
1049 				break;
1050 
1051 			err = proc_get_long(&p, &left, &val, &neg,
1052 					     proc_wspace_sep,
1053 					     sizeof(proc_wspace_sep), NULL);
1054 			if (err || neg) {
1055 				err = -EINVAL;
1056 				break;
1057 			}
1058 
1059 			val = convmul * val / convdiv;
1060 			if ((min && val < *min) || (max && val > *max)) {
1061 				err = -EINVAL;
1062 				break;
1063 			}
1064 			WRITE_ONCE(*i, val);
1065 		} else {
1066 			val = convdiv * READ_ONCE(*i) / convmul;
1067 			if (!first)
1068 				proc_put_char(&buffer, &left, '\t');
1069 			proc_put_long(&buffer, &left, val, false);
1070 		}
1071 	}
1072 
1073 	if (!write && !first && left && !err)
1074 		proc_put_char(&buffer, &left, '\n');
1075 	if (write && !err)
1076 		proc_skip_spaces(&p, &left);
1077 	if (write && first)
1078 		return err ? : -EINVAL;
1079 	*lenp -= left;
1080 out:
1081 	*ppos += *lenp;
1082 	return err;
1083 }
1084 
1085 static int do_proc_doulongvec_minmax(const struct ctl_table *table, int write,
1086 		void *buffer, size_t *lenp, loff_t *ppos, unsigned long convmul,
1087 		unsigned long convdiv)
1088 {
1089 	return __do_proc_doulongvec_minmax(table->data, table, write,
1090 			buffer, lenp, ppos, convmul, convdiv);
1091 }
1092 
1093 /**
1094  * proc_doulongvec_minmax - read a vector of long integers with min/max values
1095  * @table: the sysctl table
1096  * @write: %TRUE if this is a write to the sysctl file
1097  * @buffer: the user buffer
1098  * @lenp: the size of the user buffer
1099  * @ppos: file position
1100  *
1101  * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
1102  * values from/to the user buffer, treated as an ASCII string.
1103  *
1104  * This routine will ensure the values are within the range specified by
1105  * table->extra1 (min) and table->extra2 (max).
1106  *
1107  * Returns 0 on success.
1108  */
1109 int proc_doulongvec_minmax(const struct ctl_table *table, int write,
1110 			   void *buffer, size_t *lenp, loff_t *ppos)
1111 {
1112     return do_proc_doulongvec_minmax(table, write, buffer, lenp, ppos, 1l, 1l);
1113 }
1114 
1115 /**
1116  * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
1117  * @table: the sysctl table
1118  * @write: %TRUE if this is a write to the sysctl file
1119  * @buffer: the user buffer
1120  * @lenp: the size of the user buffer
1121  * @ppos: file position
1122  *
1123  * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
1124  * values from/to the user buffer, treated as an ASCII string. The values
1125  * are treated as milliseconds, and converted to jiffies when they are stored.
1126  *
1127  * This routine will ensure the values are within the range specified by
1128  * table->extra1 (min) and table->extra2 (max).
1129  *
1130  * Returns 0 on success.
1131  */
1132 int proc_doulongvec_ms_jiffies_minmax(const struct ctl_table *table, int write,
1133 				      void *buffer, size_t *lenp, loff_t *ppos)
1134 {
1135     return do_proc_doulongvec_minmax(table, write, buffer,
1136 				     lenp, ppos, HZ, 1000l);
1137 }
1138 
1139 
1140 static int do_proc_dointvec_jiffies_conv(bool *negp, unsigned long *lvalp,
1141 					 int *valp,
1142 					 int write, void *data)
1143 {
1144 	if (write) {
1145 		if (*lvalp > INT_MAX / HZ)
1146 			return 1;
1147 		if (*negp)
1148 			WRITE_ONCE(*valp, -*lvalp * HZ);
1149 		else
1150 			WRITE_ONCE(*valp, *lvalp * HZ);
1151 	} else {
1152 		int val = READ_ONCE(*valp);
1153 		unsigned long lval;
1154 		if (val < 0) {
1155 			*negp = true;
1156 			lval = -(unsigned long)val;
1157 		} else {
1158 			*negp = false;
1159 			lval = (unsigned long)val;
1160 		}
1161 		*lvalp = lval / HZ;
1162 	}
1163 	return 0;
1164 }
1165 
1166 static int do_proc_dointvec_userhz_jiffies_conv(bool *negp, unsigned long *lvalp,
1167 						int *valp,
1168 						int write, void *data)
1169 {
1170 	if (write) {
1171 		if (USER_HZ < HZ && *lvalp > (LONG_MAX / HZ) * USER_HZ)
1172 			return 1;
1173 		*valp = clock_t_to_jiffies(*negp ? -*lvalp : *lvalp);
1174 	} else {
1175 		int val = *valp;
1176 		unsigned long lval;
1177 		if (val < 0) {
1178 			*negp = true;
1179 			lval = -(unsigned long)val;
1180 		} else {
1181 			*negp = false;
1182 			lval = (unsigned long)val;
1183 		}
1184 		*lvalp = jiffies_to_clock_t(lval);
1185 	}
1186 	return 0;
1187 }
1188 
1189 static int do_proc_dointvec_ms_jiffies_conv(bool *negp, unsigned long *lvalp,
1190 					    int *valp,
1191 					    int write, void *data)
1192 {
1193 	if (write) {
1194 		unsigned long jif = msecs_to_jiffies(*negp ? -*lvalp : *lvalp);
1195 
1196 		if (jif > INT_MAX)
1197 			return 1;
1198 		WRITE_ONCE(*valp, (int)jif);
1199 	} else {
1200 		int val = READ_ONCE(*valp);
1201 		unsigned long lval;
1202 		if (val < 0) {
1203 			*negp = true;
1204 			lval = -(unsigned long)val;
1205 		} else {
1206 			*negp = false;
1207 			lval = (unsigned long)val;
1208 		}
1209 		*lvalp = jiffies_to_msecs(lval);
1210 	}
1211 	return 0;
1212 }
1213 
1214 static int do_proc_dointvec_ms_jiffies_minmax_conv(bool *negp, unsigned long *lvalp,
1215 						int *valp, int write, void *data)
1216 {
1217 	int tmp, ret;
1218 	struct do_proc_dointvec_minmax_conv_param *param = data;
1219 	/*
1220 	 * If writing, first do so via a temporary local int so we can
1221 	 * bounds-check it before touching *valp.
1222 	 */
1223 	int *ip = write ? &tmp : valp;
1224 
1225 	ret = do_proc_dointvec_ms_jiffies_conv(negp, lvalp, ip, write, data);
1226 	if (ret)
1227 		return ret;
1228 
1229 	if (write) {
1230 		if ((param->min && *param->min > tmp) ||
1231 				(param->max && *param->max < tmp))
1232 			return -EINVAL;
1233 		*valp = tmp;
1234 	}
1235 	return 0;
1236 }
1237 
1238 /**
1239  * proc_dointvec_jiffies - read a vector of integers as seconds
1240  * @table: the sysctl table
1241  * @write: %TRUE if this is a write to the sysctl file
1242  * @buffer: the user buffer
1243  * @lenp: the size of the user buffer
1244  * @ppos: file position
1245  *
1246  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1247  * values from/to the user buffer, treated as an ASCII string.
1248  * The values read are assumed to be in seconds, and are converted into
1249  * jiffies.
1250  *
1251  * Returns 0 on success.
1252  */
1253 int proc_dointvec_jiffies(const struct ctl_table *table, int write,
1254 			  void *buffer, size_t *lenp, loff_t *ppos)
1255 {
1256     return do_proc_dointvec(table,write,buffer,lenp,ppos,
1257 		    	    do_proc_dointvec_jiffies_conv,NULL);
1258 }
1259 
1260 int proc_dointvec_ms_jiffies_minmax(const struct ctl_table *table, int write,
1261 			  void *buffer, size_t *lenp, loff_t *ppos)
1262 {
1263 	struct do_proc_dointvec_minmax_conv_param param = {
1264 		.min = (int *) table->extra1,
1265 		.max = (int *) table->extra2,
1266 	};
1267 	return do_proc_dointvec(table, write, buffer, lenp, ppos,
1268 			do_proc_dointvec_ms_jiffies_minmax_conv, &param);
1269 }
1270 
1271 /**
1272  * proc_dointvec_userhz_jiffies - read a vector of integers as 1/USER_HZ seconds
1273  * @table: the sysctl table
1274  * @write: %TRUE if this is a write to the sysctl file
1275  * @buffer: the user buffer
1276  * @lenp: the size of the user buffer
1277  * @ppos: pointer to the file position
1278  *
1279  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1280  * values from/to the user buffer, treated as an ASCII string.
1281  * The values read are assumed to be in 1/USER_HZ seconds, and
1282  * are converted into jiffies.
1283  *
1284  * Returns 0 on success.
1285  */
1286 int proc_dointvec_userhz_jiffies(const struct ctl_table *table, int write,
1287 				 void *buffer, size_t *lenp, loff_t *ppos)
1288 {
1289 	return do_proc_dointvec(table, write, buffer, lenp, ppos,
1290 				do_proc_dointvec_userhz_jiffies_conv, NULL);
1291 }
1292 
1293 /**
1294  * proc_dointvec_ms_jiffies - read a vector of integers as 1 milliseconds
1295  * @table: the sysctl table
1296  * @write: %TRUE if this is a write to the sysctl file
1297  * @buffer: the user buffer
1298  * @lenp: the size of the user buffer
1299  * @ppos: the current position in the file
1300  *
1301  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1302  * values from/to the user buffer, treated as an ASCII string.
1303  * The values read are assumed to be in 1/1000 seconds, and
1304  * are converted into jiffies.
1305  *
1306  * Returns 0 on success.
1307  */
1308 int proc_dointvec_ms_jiffies(const struct ctl_table *table, int write, void *buffer,
1309 		size_t *lenp, loff_t *ppos)
1310 {
1311 	return do_proc_dointvec(table, write, buffer, lenp, ppos,
1312 				do_proc_dointvec_ms_jiffies_conv, NULL);
1313 }
1314 
1315 static int proc_do_cad_pid(const struct ctl_table *table, int write, void *buffer,
1316 		size_t *lenp, loff_t *ppos)
1317 {
1318 	struct pid *new_pid;
1319 	pid_t tmp;
1320 	int r;
1321 
1322 	tmp = pid_vnr(cad_pid);
1323 
1324 	r = __do_proc_dointvec(&tmp, table, write, buffer,
1325 			       lenp, ppos, NULL, NULL);
1326 	if (r || !write)
1327 		return r;
1328 
1329 	new_pid = find_get_pid(tmp);
1330 	if (!new_pid)
1331 		return -ESRCH;
1332 
1333 	put_pid(xchg(&cad_pid, new_pid));
1334 	return 0;
1335 }
1336 
1337 /**
1338  * proc_do_large_bitmap - read/write from/to a large bitmap
1339  * @table: the sysctl table
1340  * @write: %TRUE if this is a write to the sysctl file
1341  * @buffer: the user buffer
1342  * @lenp: the size of the user buffer
1343  * @ppos: file position
1344  *
1345  * The bitmap is stored at table->data and the bitmap length (in bits)
1346  * in table->maxlen.
1347  *
1348  * We use a range comma separated format (e.g. 1,3-4,10-10) so that
1349  * large bitmaps may be represented in a compact manner. Writing into
1350  * the file will clear the bitmap then update it with the given input.
1351  *
1352  * Returns 0 on success.
1353  */
1354 int proc_do_large_bitmap(const struct ctl_table *table, int write,
1355 			 void *buffer, size_t *lenp, loff_t *ppos)
1356 {
1357 	int err = 0;
1358 	size_t left = *lenp;
1359 	unsigned long bitmap_len = table->maxlen;
1360 	unsigned long *bitmap = *(unsigned long **) table->data;
1361 	unsigned long *tmp_bitmap = NULL;
1362 	char tr_a[] = { '-', ',', '\n' }, tr_b[] = { ',', '\n', 0 }, c;
1363 
1364 	if (!bitmap || !bitmap_len || !left || (*ppos && !write)) {
1365 		*lenp = 0;
1366 		return 0;
1367 	}
1368 
1369 	if (write) {
1370 		char *p = buffer;
1371 		size_t skipped = 0;
1372 
1373 		if (left > PAGE_SIZE - 1) {
1374 			left = PAGE_SIZE - 1;
1375 			/* How much of the buffer we'll skip this pass */
1376 			skipped = *lenp - left;
1377 		}
1378 
1379 		tmp_bitmap = bitmap_zalloc(bitmap_len, GFP_KERNEL);
1380 		if (!tmp_bitmap)
1381 			return -ENOMEM;
1382 		proc_skip_char(&p, &left, '\n');
1383 		while (!err && left) {
1384 			unsigned long val_a, val_b;
1385 			bool neg;
1386 			size_t saved_left;
1387 
1388 			/* In case we stop parsing mid-number, we can reset */
1389 			saved_left = left;
1390 			err = proc_get_long(&p, &left, &val_a, &neg, tr_a,
1391 					     sizeof(tr_a), &c);
1392 			/*
1393 			 * If we consumed the entirety of a truncated buffer or
1394 			 * only one char is left (may be a "-"), then stop here,
1395 			 * reset, & come back for more.
1396 			 */
1397 			if ((left <= 1) && skipped) {
1398 				left = saved_left;
1399 				break;
1400 			}
1401 
1402 			if (err)
1403 				break;
1404 			if (val_a >= bitmap_len || neg) {
1405 				err = -EINVAL;
1406 				break;
1407 			}
1408 
1409 			val_b = val_a;
1410 			if (left) {
1411 				p++;
1412 				left--;
1413 			}
1414 
1415 			if (c == '-') {
1416 				err = proc_get_long(&p, &left, &val_b,
1417 						     &neg, tr_b, sizeof(tr_b),
1418 						     &c);
1419 				/*
1420 				 * If we consumed all of a truncated buffer or
1421 				 * then stop here, reset, & come back for more.
1422 				 */
1423 				if (!left && skipped) {
1424 					left = saved_left;
1425 					break;
1426 				}
1427 
1428 				if (err)
1429 					break;
1430 				if (val_b >= bitmap_len || neg ||
1431 				    val_a > val_b) {
1432 					err = -EINVAL;
1433 					break;
1434 				}
1435 				if (left) {
1436 					p++;
1437 					left--;
1438 				}
1439 			}
1440 
1441 			bitmap_set(tmp_bitmap, val_a, val_b - val_a + 1);
1442 			proc_skip_char(&p, &left, '\n');
1443 		}
1444 		left += skipped;
1445 	} else {
1446 		unsigned long bit_a, bit_b = 0;
1447 		bool first = 1;
1448 
1449 		while (left) {
1450 			bit_a = find_next_bit(bitmap, bitmap_len, bit_b);
1451 			if (bit_a >= bitmap_len)
1452 				break;
1453 			bit_b = find_next_zero_bit(bitmap, bitmap_len,
1454 						   bit_a + 1) - 1;
1455 
1456 			if (!first)
1457 				proc_put_char(&buffer, &left, ',');
1458 			proc_put_long(&buffer, &left, bit_a, false);
1459 			if (bit_a != bit_b) {
1460 				proc_put_char(&buffer, &left, '-');
1461 				proc_put_long(&buffer, &left, bit_b, false);
1462 			}
1463 
1464 			first = 0; bit_b++;
1465 		}
1466 		proc_put_char(&buffer, &left, '\n');
1467 	}
1468 
1469 	if (!err) {
1470 		if (write) {
1471 			if (*ppos)
1472 				bitmap_or(bitmap, bitmap, tmp_bitmap, bitmap_len);
1473 			else
1474 				bitmap_copy(bitmap, tmp_bitmap, bitmap_len);
1475 		}
1476 		*lenp -= left;
1477 		*ppos += *lenp;
1478 	}
1479 
1480 	bitmap_free(tmp_bitmap);
1481 	return err;
1482 }
1483 
1484 #else /* CONFIG_PROC_SYSCTL */
1485 
1486 int proc_dostring(const struct ctl_table *table, int write,
1487 		  void *buffer, size_t *lenp, loff_t *ppos)
1488 {
1489 	return -ENOSYS;
1490 }
1491 
1492 int proc_dobool(const struct ctl_table *table, int write,
1493 		void *buffer, size_t *lenp, loff_t *ppos)
1494 {
1495 	return -ENOSYS;
1496 }
1497 
1498 int proc_dointvec(const struct ctl_table *table, int write,
1499 		  void *buffer, size_t *lenp, loff_t *ppos)
1500 {
1501 	return -ENOSYS;
1502 }
1503 
1504 int proc_douintvec(const struct ctl_table *table, int write,
1505 		  void *buffer, size_t *lenp, loff_t *ppos)
1506 {
1507 	return -ENOSYS;
1508 }
1509 
1510 int proc_dointvec_minmax(const struct ctl_table *table, int write,
1511 		    void *buffer, size_t *lenp, loff_t *ppos)
1512 {
1513 	return -ENOSYS;
1514 }
1515 
1516 int proc_douintvec_minmax(const struct ctl_table *table, int write,
1517 			  void *buffer, size_t *lenp, loff_t *ppos)
1518 {
1519 	return -ENOSYS;
1520 }
1521 
1522 int proc_dou8vec_minmax(const struct ctl_table *table, int write,
1523 			void *buffer, size_t *lenp, loff_t *ppos)
1524 {
1525 	return -ENOSYS;
1526 }
1527 
1528 int proc_dointvec_jiffies(const struct ctl_table *table, int write,
1529 		    void *buffer, size_t *lenp, loff_t *ppos)
1530 {
1531 	return -ENOSYS;
1532 }
1533 
1534 int proc_dointvec_ms_jiffies_minmax(const struct ctl_table *table, int write,
1535 				    void *buffer, size_t *lenp, loff_t *ppos)
1536 {
1537 	return -ENOSYS;
1538 }
1539 
1540 int proc_dointvec_userhz_jiffies(const struct ctl_table *table, int write,
1541 		    void *buffer, size_t *lenp, loff_t *ppos)
1542 {
1543 	return -ENOSYS;
1544 }
1545 
1546 int proc_dointvec_ms_jiffies(const struct ctl_table *table, int write,
1547 			     void *buffer, size_t *lenp, loff_t *ppos)
1548 {
1549 	return -ENOSYS;
1550 }
1551 
1552 int proc_doulongvec_minmax(const struct ctl_table *table, int write,
1553 		    void *buffer, size_t *lenp, loff_t *ppos)
1554 {
1555 	return -ENOSYS;
1556 }
1557 
1558 int proc_doulongvec_ms_jiffies_minmax(const struct ctl_table *table, int write,
1559 				      void *buffer, size_t *lenp, loff_t *ppos)
1560 {
1561 	return -ENOSYS;
1562 }
1563 
1564 int proc_do_large_bitmap(const struct ctl_table *table, int write,
1565 			 void *buffer, size_t *lenp, loff_t *ppos)
1566 {
1567 	return -ENOSYS;
1568 }
1569 
1570 #endif /* CONFIG_PROC_SYSCTL */
1571 
1572 #if defined(CONFIG_SYSCTL)
1573 int proc_do_static_key(const struct ctl_table *table, int write,
1574 		       void *buffer, size_t *lenp, loff_t *ppos)
1575 {
1576 	struct static_key *key = (struct static_key *)table->data;
1577 	static DEFINE_MUTEX(static_key_mutex);
1578 	int val, ret;
1579 	struct ctl_table tmp = {
1580 		.data   = &val,
1581 		.maxlen = sizeof(val),
1582 		.mode   = table->mode,
1583 		.extra1 = SYSCTL_ZERO,
1584 		.extra2 = SYSCTL_ONE,
1585 	};
1586 
1587 	if (write && !capable(CAP_SYS_ADMIN))
1588 		return -EPERM;
1589 
1590 	mutex_lock(&static_key_mutex);
1591 	val = static_key_enabled(key);
1592 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1593 	if (write && !ret) {
1594 		if (val)
1595 			static_key_enable(key);
1596 		else
1597 			static_key_disable(key);
1598 	}
1599 	mutex_unlock(&static_key_mutex);
1600 	return ret;
1601 }
1602 
1603 static const struct ctl_table kern_table[] = {
1604 	{
1605 		.procname	= "panic",
1606 		.data		= &panic_timeout,
1607 		.maxlen		= sizeof(int),
1608 		.mode		= 0644,
1609 		.proc_handler	= proc_dointvec,
1610 	},
1611 #ifdef CONFIG_PROC_SYSCTL
1612 	{
1613 		.procname	= "tainted",
1614 		.maxlen 	= sizeof(long),
1615 		.mode		= 0644,
1616 		.proc_handler	= proc_taint,
1617 	},
1618 	{
1619 		.procname	= "sysctl_writes_strict",
1620 		.data		= &sysctl_writes_strict,
1621 		.maxlen		= sizeof(int),
1622 		.mode		= 0644,
1623 		.proc_handler	= proc_dointvec_minmax,
1624 		.extra1		= SYSCTL_NEG_ONE,
1625 		.extra2		= SYSCTL_ONE,
1626 	},
1627 #endif
1628 	{
1629 		.procname	= "print-fatal-signals",
1630 		.data		= &print_fatal_signals,
1631 		.maxlen		= sizeof(int),
1632 		.mode		= 0644,
1633 		.proc_handler	= proc_dointvec,
1634 	},
1635 #ifdef CONFIG_SPARC
1636 	{
1637 		.procname	= "reboot-cmd",
1638 		.data		= reboot_command,
1639 		.maxlen		= 256,
1640 		.mode		= 0644,
1641 		.proc_handler	= proc_dostring,
1642 	},
1643 	{
1644 		.procname	= "stop-a",
1645 		.data		= &stop_a_enabled,
1646 		.maxlen		= sizeof (int),
1647 		.mode		= 0644,
1648 		.proc_handler	= proc_dointvec,
1649 	},
1650 	{
1651 		.procname	= "scons-poweroff",
1652 		.data		= &scons_pwroff,
1653 		.maxlen		= sizeof (int),
1654 		.mode		= 0644,
1655 		.proc_handler	= proc_dointvec,
1656 	},
1657 #endif
1658 #ifdef CONFIG_SPARC64
1659 	{
1660 		.procname	= "tsb-ratio",
1661 		.data		= &sysctl_tsb_ratio,
1662 		.maxlen		= sizeof (int),
1663 		.mode		= 0644,
1664 		.proc_handler	= proc_dointvec,
1665 	},
1666 #endif
1667 #ifdef CONFIG_PARISC
1668 	{
1669 		.procname	= "soft-power",
1670 		.data		= &pwrsw_enabled,
1671 		.maxlen		= sizeof (int),
1672 		.mode		= 0644,
1673 		.proc_handler	= proc_dointvec,
1674 	},
1675 #endif
1676 #ifdef CONFIG_SYSCTL_ARCH_UNALIGN_ALLOW
1677 	{
1678 		.procname	= "unaligned-trap",
1679 		.data		= &unaligned_enabled,
1680 		.maxlen		= sizeof (int),
1681 		.mode		= 0644,
1682 		.proc_handler	= proc_dointvec,
1683 	},
1684 #endif
1685 #ifdef CONFIG_STACK_TRACER
1686 	{
1687 		.procname	= "stack_tracer_enabled",
1688 		.data		= &stack_tracer_enabled,
1689 		.maxlen		= sizeof(int),
1690 		.mode		= 0644,
1691 		.proc_handler	= stack_trace_sysctl,
1692 	},
1693 #endif
1694 #ifdef CONFIG_TRACING
1695 	{
1696 		.procname	= "ftrace_dump_on_oops",
1697 		.data		= &ftrace_dump_on_oops,
1698 		.maxlen		= MAX_TRACER_SIZE,
1699 		.mode		= 0644,
1700 		.proc_handler	= proc_dostring,
1701 	},
1702 	{
1703 		.procname	= "traceoff_on_warning",
1704 		.data		= &__disable_trace_on_warning,
1705 		.maxlen		= sizeof(__disable_trace_on_warning),
1706 		.mode		= 0644,
1707 		.proc_handler	= proc_dointvec,
1708 	},
1709 	{
1710 		.procname	= "tracepoint_printk",
1711 		.data		= &tracepoint_printk,
1712 		.maxlen		= sizeof(tracepoint_printk),
1713 		.mode		= 0644,
1714 		.proc_handler	= tracepoint_printk_sysctl,
1715 	},
1716 #endif
1717 #ifdef CONFIG_MODULES
1718 	{
1719 		.procname	= "modprobe",
1720 		.data		= &modprobe_path,
1721 		.maxlen		= KMOD_PATH_LEN,
1722 		.mode		= 0644,
1723 		.proc_handler	= proc_dostring,
1724 	},
1725 	{
1726 		.procname	= "modules_disabled",
1727 		.data		= &modules_disabled,
1728 		.maxlen		= sizeof(int),
1729 		.mode		= 0644,
1730 		/* only handle a transition from default "0" to "1" */
1731 		.proc_handler	= proc_dointvec_minmax,
1732 		.extra1		= SYSCTL_ONE,
1733 		.extra2		= SYSCTL_ONE,
1734 	},
1735 #endif
1736 #ifdef CONFIG_UEVENT_HELPER
1737 	{
1738 		.procname	= "hotplug",
1739 		.data		= &uevent_helper,
1740 		.maxlen		= UEVENT_HELPER_PATH_LEN,
1741 		.mode		= 0644,
1742 		.proc_handler	= proc_dostring,
1743 	},
1744 #endif
1745 #ifdef CONFIG_MAGIC_SYSRQ
1746 	{
1747 		.procname	= "sysrq",
1748 		.data		= NULL,
1749 		.maxlen		= sizeof (int),
1750 		.mode		= 0644,
1751 		.proc_handler	= sysrq_sysctl_handler,
1752 	},
1753 #endif
1754 #ifdef CONFIG_PROC_SYSCTL
1755 	{
1756 		.procname	= "cad_pid",
1757 		.data		= NULL,
1758 		.maxlen		= sizeof (int),
1759 		.mode		= 0600,
1760 		.proc_handler	= proc_do_cad_pid,
1761 	},
1762 #endif
1763 	{
1764 		.procname	= "threads-max",
1765 		.data		= NULL,
1766 		.maxlen		= sizeof(int),
1767 		.mode		= 0644,
1768 		.proc_handler	= sysctl_max_threads,
1769 	},
1770 	{
1771 		.procname	= "overflowuid",
1772 		.data		= &overflowuid,
1773 		.maxlen		= sizeof(int),
1774 		.mode		= 0644,
1775 		.proc_handler	= proc_dointvec_minmax,
1776 		.extra1		= SYSCTL_ZERO,
1777 		.extra2		= SYSCTL_MAXOLDUID,
1778 	},
1779 	{
1780 		.procname	= "overflowgid",
1781 		.data		= &overflowgid,
1782 		.maxlen		= sizeof(int),
1783 		.mode		= 0644,
1784 		.proc_handler	= proc_dointvec_minmax,
1785 		.extra1		= SYSCTL_ZERO,
1786 		.extra2		= SYSCTL_MAXOLDUID,
1787 	},
1788 #ifdef CONFIG_S390
1789 	{
1790 		.procname	= "userprocess_debug",
1791 		.data		= &show_unhandled_signals,
1792 		.maxlen		= sizeof(int),
1793 		.mode		= 0644,
1794 		.proc_handler	= proc_dointvec,
1795 	},
1796 #endif
1797 	{
1798 		.procname	= "panic_on_oops",
1799 		.data		= &panic_on_oops,
1800 		.maxlen		= sizeof(int),
1801 		.mode		= 0644,
1802 		.proc_handler	= proc_dointvec,
1803 	},
1804 	{
1805 		.procname	= "panic_print",
1806 		.data		= &panic_print,
1807 		.maxlen		= sizeof(unsigned long),
1808 		.mode		= 0644,
1809 		.proc_handler	= proc_doulongvec_minmax,
1810 	},
1811 	{
1812 		.procname	= "ngroups_max",
1813 		.data		= (void *)&ngroups_max,
1814 		.maxlen		= sizeof (int),
1815 		.mode		= 0444,
1816 		.proc_handler	= proc_dointvec,
1817 	},
1818 	{
1819 		.procname	= "cap_last_cap",
1820 		.data		= (void *)&cap_last_cap,
1821 		.maxlen		= sizeof(int),
1822 		.mode		= 0444,
1823 		.proc_handler	= proc_dointvec,
1824 	},
1825 #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86)
1826 	{
1827 		.procname       = "unknown_nmi_panic",
1828 		.data           = &unknown_nmi_panic,
1829 		.maxlen         = sizeof (int),
1830 		.mode           = 0644,
1831 		.proc_handler   = proc_dointvec,
1832 	},
1833 #endif
1834 
1835 #if (defined(CONFIG_X86_32) || defined(CONFIG_PARISC)) && \
1836 	defined(CONFIG_DEBUG_STACKOVERFLOW)
1837 	{
1838 		.procname	= "panic_on_stackoverflow",
1839 		.data		= &sysctl_panic_on_stackoverflow,
1840 		.maxlen		= sizeof(int),
1841 		.mode		= 0644,
1842 		.proc_handler	= proc_dointvec,
1843 	},
1844 #endif
1845 #if defined(CONFIG_X86)
1846 	{
1847 		.procname	= "panic_on_unrecovered_nmi",
1848 		.data		= &panic_on_unrecovered_nmi,
1849 		.maxlen		= sizeof(int),
1850 		.mode		= 0644,
1851 		.proc_handler	= proc_dointvec,
1852 	},
1853 	{
1854 		.procname	= "panic_on_io_nmi",
1855 		.data		= &panic_on_io_nmi,
1856 		.maxlen		= sizeof(int),
1857 		.mode		= 0644,
1858 		.proc_handler	= proc_dointvec,
1859 	},
1860 	{
1861 		.procname	= "bootloader_type",
1862 		.data		= &bootloader_type,
1863 		.maxlen		= sizeof (int),
1864 		.mode		= 0444,
1865 		.proc_handler	= proc_dointvec,
1866 	},
1867 	{
1868 		.procname	= "bootloader_version",
1869 		.data		= &bootloader_version,
1870 		.maxlen		= sizeof (int),
1871 		.mode		= 0444,
1872 		.proc_handler	= proc_dointvec,
1873 	},
1874 	{
1875 		.procname	= "io_delay_type",
1876 		.data		= &io_delay_type,
1877 		.maxlen		= sizeof(int),
1878 		.mode		= 0644,
1879 		.proc_handler	= proc_dointvec,
1880 	},
1881 #endif
1882 #if defined(CONFIG_MMU)
1883 	{
1884 		.procname	= "randomize_va_space",
1885 		.data		= &randomize_va_space,
1886 		.maxlen		= sizeof(int),
1887 		.mode		= 0644,
1888 		.proc_handler	= proc_dointvec,
1889 	},
1890 #endif
1891 #if defined(CONFIG_S390) && defined(CONFIG_SMP)
1892 	{
1893 		.procname	= "spin_retry",
1894 		.data		= &spin_retry,
1895 		.maxlen		= sizeof (int),
1896 		.mode		= 0644,
1897 		.proc_handler	= proc_dointvec,
1898 	},
1899 #endif
1900 #if	defined(CONFIG_ACPI_SLEEP) && defined(CONFIG_X86)
1901 	{
1902 		.procname	= "acpi_video_flags",
1903 		.data		= &acpi_realmode_flags,
1904 		.maxlen		= sizeof (unsigned long),
1905 		.mode		= 0644,
1906 		.proc_handler	= proc_doulongvec_minmax,
1907 	},
1908 #endif
1909 #ifdef CONFIG_SYSCTL_ARCH_UNALIGN_NO_WARN
1910 	{
1911 		.procname	= "ignore-unaligned-usertrap",
1912 		.data		= &no_unaligned_warning,
1913 		.maxlen		= sizeof (int),
1914 		.mode		= 0644,
1915 		.proc_handler	= proc_dointvec,
1916 	},
1917 #endif
1918 #ifdef CONFIG_RT_MUTEXES
1919 	{
1920 		.procname	= "max_lock_depth",
1921 		.data		= &max_lock_depth,
1922 		.maxlen		= sizeof(int),
1923 		.mode		= 0644,
1924 		.proc_handler	= proc_dointvec,
1925 	},
1926 #endif
1927 #ifdef CONFIG_PERF_EVENTS
1928 	/*
1929 	 * User-space scripts rely on the existence of this file
1930 	 * as a feature check for perf_events being enabled.
1931 	 *
1932 	 * So it's an ABI, do not remove!
1933 	 */
1934 	{
1935 		.procname	= "perf_event_paranoid",
1936 		.data		= &sysctl_perf_event_paranoid,
1937 		.maxlen		= sizeof(sysctl_perf_event_paranoid),
1938 		.mode		= 0644,
1939 		.proc_handler	= proc_dointvec,
1940 	},
1941 	{
1942 		.procname	= "perf_event_mlock_kb",
1943 		.data		= &sysctl_perf_event_mlock,
1944 		.maxlen		= sizeof(sysctl_perf_event_mlock),
1945 		.mode		= 0644,
1946 		.proc_handler	= proc_dointvec,
1947 	},
1948 	{
1949 		.procname	= "perf_event_max_sample_rate",
1950 		.data		= &sysctl_perf_event_sample_rate,
1951 		.maxlen		= sizeof(sysctl_perf_event_sample_rate),
1952 		.mode		= 0644,
1953 		.proc_handler	= perf_event_max_sample_rate_handler,
1954 		.extra1		= SYSCTL_ONE,
1955 	},
1956 	{
1957 		.procname	= "perf_cpu_time_max_percent",
1958 		.data		= &sysctl_perf_cpu_time_max_percent,
1959 		.maxlen		= sizeof(sysctl_perf_cpu_time_max_percent),
1960 		.mode		= 0644,
1961 		.proc_handler	= perf_cpu_time_max_percent_handler,
1962 		.extra1		= SYSCTL_ZERO,
1963 		.extra2		= SYSCTL_ONE_HUNDRED,
1964 	},
1965 	{
1966 		.procname	= "perf_event_max_stack",
1967 		.data		= &sysctl_perf_event_max_stack,
1968 		.maxlen		= sizeof(sysctl_perf_event_max_stack),
1969 		.mode		= 0644,
1970 		.proc_handler	= perf_event_max_stack_handler,
1971 		.extra1		= SYSCTL_ZERO,
1972 		.extra2		= (void *)&six_hundred_forty_kb,
1973 	},
1974 	{
1975 		.procname	= "perf_event_max_contexts_per_stack",
1976 		.data		= &sysctl_perf_event_max_contexts_per_stack,
1977 		.maxlen		= sizeof(sysctl_perf_event_max_contexts_per_stack),
1978 		.mode		= 0644,
1979 		.proc_handler	= perf_event_max_stack_handler,
1980 		.extra1		= SYSCTL_ZERO,
1981 		.extra2		= SYSCTL_ONE_THOUSAND,
1982 	},
1983 #endif
1984 	{
1985 		.procname	= "panic_on_warn",
1986 		.data		= &panic_on_warn,
1987 		.maxlen		= sizeof(int),
1988 		.mode		= 0644,
1989 		.proc_handler	= proc_dointvec_minmax,
1990 		.extra1		= SYSCTL_ZERO,
1991 		.extra2		= SYSCTL_ONE,
1992 	},
1993 #ifdef CONFIG_TREE_RCU
1994 	{
1995 		.procname	= "panic_on_rcu_stall",
1996 		.data		= &sysctl_panic_on_rcu_stall,
1997 		.maxlen		= sizeof(sysctl_panic_on_rcu_stall),
1998 		.mode		= 0644,
1999 		.proc_handler	= proc_dointvec_minmax,
2000 		.extra1		= SYSCTL_ZERO,
2001 		.extra2		= SYSCTL_ONE,
2002 	},
2003 	{
2004 		.procname	= "max_rcu_stall_to_panic",
2005 		.data		= &sysctl_max_rcu_stall_to_panic,
2006 		.maxlen		= sizeof(sysctl_max_rcu_stall_to_panic),
2007 		.mode		= 0644,
2008 		.proc_handler	= proc_dointvec_minmax,
2009 		.extra1		= SYSCTL_ONE,
2010 		.extra2		= SYSCTL_INT_MAX,
2011 	},
2012 #endif
2013 };
2014 
2015 int __init sysctl_init_bases(void)
2016 {
2017 	register_sysctl_init("kernel", kern_table);
2018 
2019 	return 0;
2020 }
2021 #endif /* CONFIG_SYSCTL */
2022 /*
2023  * No sense putting this after each symbol definition, twice,
2024  * exception granted :-)
2025  */
2026 EXPORT_SYMBOL(proc_dobool);
2027 EXPORT_SYMBOL(proc_dointvec);
2028 EXPORT_SYMBOL(proc_douintvec);
2029 EXPORT_SYMBOL(proc_dointvec_jiffies);
2030 EXPORT_SYMBOL(proc_dointvec_minmax);
2031 EXPORT_SYMBOL_GPL(proc_douintvec_minmax);
2032 EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
2033 EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
2034 EXPORT_SYMBOL(proc_dostring);
2035 EXPORT_SYMBOL(proc_doulongvec_minmax);
2036 EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);
2037 EXPORT_SYMBOL(proc_do_large_bitmap);
2038