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