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