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