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