blob: 354a2d294f526ad6688168443913385eda101fa1 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/mm.h>
24#include <linux/swap.h>
25#include <linux/slab.h>
26#include <linux/sysctl.h>
Akinobu Mita5a04cca2012-03-28 14:42:50 -070027#include <linux/bitmap.h>
Dave Youngd33ed522010-03-10 15:23:59 -080028#include <linux/signal.h>
Andy Shevchenkof39650d2021-06-30 18:54:59 -070029#include <linux/panic.h>
Dan Rosenberg455cd5a2011-01-12 16:59:41 -080030#include <linux/printk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/proc_fs.h>
Andrew Morgan72c2d582007-10-18 03:05:59 -070032#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/ctype.h>
Steven Rostedtfd4b6162012-07-30 14:42:48 -070034#include <linux/kmemleak.h>
Jakub Kicinskib6459412021-12-28 16:49:13 -080035#include <linux/filter.h>
Adrian Bunk62239ac2007-07-17 04:03:45 -070036#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
38#include <linux/kernel.h>
Kay Sievers0296b222005-11-11 05:33:52 +010039#include <linux/kobject.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030040#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/sysrq.h>
42#include <linux/highuid.h>
43#include <linux/writeback.h>
Ingo Molnar3fff4c42009-09-22 16:18:09 +020044#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/hugetlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/initrd.h>
David Howells0b77f5b2008-04-29 01:01:32 -070047#include <linux/key.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/times.h>
49#include <linux/limits.h>
50#include <linux/dcache.h>
51#include <linux/syscalls.h>
Adrian Bunkc748e132008-07-23 21:27:03 -070052#include <linux/vmstat.h>
Pavel Machekc255d842006-02-20 18:27:58 -080053#include <linux/nfs_fs.h>
54#include <linux/acpi.h>
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -070055#include <linux/reboot.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020056#include <linux/ftrace.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020057#include <linux/perf_event.h>
David Rientjes8e4228e2010-08-09 17:18:56 -070058#include <linux/oom.h>
Eric Paris17f60a72011-04-01 17:07:50 -040059#include <linux/kmod.h>
Dan Ballard73efc032011-10-31 17:11:20 -070060#include <linux/capability.h>
Al Viro40401532012-02-13 03:58:52 +000061#include <linux/binfmts.h>
Clark Williamscf4aebc22013-02-07 09:46:59 -060062#include <linux/sched/sysctl.h>
Eric W. Biedermand2921682016-09-28 00:27:17 -050063#include <linux/mount.h>
Peter Xucefdca02019-05-13 17:16:41 -070064#include <linux/userfaultfd_k.h>
Christoph Hellwig2374c09b2020-04-24 08:43:36 +020065#include <linux/pid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Christian Brauner7f2923c2019-03-07 16:29:40 -080067#include "../lib/kstrtox.h"
68
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080069#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <asm/processor.h>
71
Andi Kleen29cbc782006-09-30 01:47:55 +020072#ifdef CONFIG_X86
73#include <asm/nmi.h>
Chuck Ebbert0741f4d2006-12-07 02:14:11 +010074#include <asm/stacktrace.h>
Ingo Molnar6e7c4022008-01-30 13:30:05 +010075#include <asm/io.h>
Andi Kleen29cbc782006-09-30 01:47:55 +020076#endif
David Howellsd550bbd2012-03-28 18:30:03 +010077#ifdef CONFIG_SPARC
78#include <asm/setup.h>
79#endif
Dave Young4f0e0562010-03-10 15:24:09 -080080#ifdef CONFIG_RT_MUTEXES
81#include <linux/rtmutex.h>
82#endif
Don Zickus504d7cf2010-02-12 17:19:19 -050083
Liu Shixinb13bc7c2022-09-08 16:29:46 +080084/* shared constants to be used in various sysctls */
85const int sysctl_vals[] = { 0, 1, 2, 3, 4, 100, 200, 1000, 3000, INT_MAX, 65535, -1 };
86EXPORT_SYMBOL(sysctl_vals);
87
88const unsigned long sysctl_long_vals[] = { 0, 1, LONG_MAX };
89EXPORT_SYMBOL_GPL(sysctl_long_vals);
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#if defined(CONFIG_SYSCTL)
92
Liu Shixinb13bc7c2022-09-08 16:29:46 +080093/* Constants used for minimum and maximum */
Ravikiran G Thirumalaic4f3b632007-10-16 23:26:09 -070094
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -030095#ifdef CONFIG_PERF_EVENTS
Xiaoming Nid73840e2022-01-21 22:11:14 -080096static const int six_hundred_forty_kb = 640 * 1024;
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -030097#endif
Ravikiran G Thirumalaic4f3b632007-10-16 23:26:09 -070098
Andrea Righi9e4a5bd2009-04-30 15:08:57 -070099
Stephen Kittf6288672022-01-21 22:11:09 -0800100static const int ngroups_max = NGROUPS_MAX;
Dan Ballard73efc032011-10-31 17:11:20 -0700101static const int cap_last_cap = CAP_LAST_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Randy Dunlapd6f8ff72006-10-19 23:28:34 -0700103#ifdef CONFIG_PROC_SYSCTL
Kees Cookf4aacea2014-06-06 14:37:19 -0700104
Luis R. Rodrigueza19ac332017-07-12 14:33:30 -0700105/**
106 * enum sysctl_writes_mode - supported sysctl write modes
107 *
108 * @SYSCTL_WRITES_LEGACY: each write syscall must fully contain the sysctl value
Weitao Hou65f50f22019-07-16 16:26:54 -0700109 * to be written, and multiple writes on the same sysctl file descriptor
110 * will rewrite the sysctl value, regardless of file position. No warning
111 * is issued when the initial position is not 0.
Luis R. Rodrigueza19ac332017-07-12 14:33:30 -0700112 * @SYSCTL_WRITES_WARN: same as above but warn when the initial file position is
Weitao Hou65f50f22019-07-16 16:26:54 -0700113 * not 0.
Luis R. Rodrigueza19ac332017-07-12 14:33:30 -0700114 * @SYSCTL_WRITES_STRICT: writes to numeric sysctl entries must always be at
Weitao Hou65f50f22019-07-16 16:26:54 -0700115 * file position 0 and the value must be fully contained in the buffer
116 * sent to the write syscall. If dealing with strings respect the file
117 * position, but restrict this to the max length of the buffer, anything
118 * passed the max length will be ignored. Multiple writes will append
119 * to the buffer.
Luis R. Rodrigueza19ac332017-07-12 14:33:30 -0700120 *
121 * These write modes control how current file position affects the behavior of
122 * updating sysctl values through the proc interface on each write.
123 */
124enum sysctl_writes_mode {
125 SYSCTL_WRITES_LEGACY = -1,
126 SYSCTL_WRITES_WARN = 0,
127 SYSCTL_WRITES_STRICT = 1,
128};
Kees Cookf4aacea2014-06-06 14:37:19 -0700129
Luis R. Rodrigueza19ac332017-07-12 14:33:30 -0700130static enum sysctl_writes_mode sysctl_writes_strict = SYSCTL_WRITES_STRICT;
Christoph Hellwigf461d2d2020-04-24 08:43:37 +0200131#endif /* CONFIG_PROC_SYSCTL */
Luis R. Rodriguezceb18132018-03-10 06:14:51 -0800132
Alexandre Ghiti67f39772019-09-23 15:38:47 -0700133#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \
134 defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135int sysctl_legacy_va_layout;
136#endif
137
Eric W. Biedermanb89a8172006-09-27 01:51:04 -0700138#endif /* CONFIG_SYSCTL */
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
141 * /proc/sys support
142 */
143
Eric W. Biedermanb89a8172006-09-27 01:51:04 -0700144#ifdef CONFIG_PROC_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Kees Cookf8808302014-06-06 14:37:17 -0700146static int _proc_do_string(char *data, int maxlen, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200147 char *buffer, size_t *lenp, loff_t *ppos)
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700148{
149 size_t len;
Christoph Hellwig32927392020-04-24 08:43:38 +0200150 char c, *p;
Oleg Nesterov8d060872007-02-10 01:46:38 -0800151
152 if (!data || !maxlen || !*lenp) {
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700153 *lenp = 0;
154 return 0;
155 }
Oleg Nesterov8d060872007-02-10 01:46:38 -0800156
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700157 if (write) {
Kees Cookf4aacea2014-06-06 14:37:19 -0700158 if (sysctl_writes_strict == SYSCTL_WRITES_STRICT) {
159 /* Only continue writes not past the end of buffer. */
160 len = strlen(data);
161 if (len > maxlen - 1)
162 len = maxlen - 1;
163
164 if (*ppos > len)
165 return 0;
166 len = *ppos;
167 } else {
168 /* Start writing from beginning of buffer. */
169 len = 0;
170 }
171
Kees Cook2ca9bb42014-06-06 14:37:18 -0700172 *ppos += *lenp;
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700173 p = buffer;
Kees Cook2ca9bb42014-06-06 14:37:18 -0700174 while ((p - buffer) < *lenp && len < maxlen - 1) {
Christoph Hellwig32927392020-04-24 08:43:38 +0200175 c = *(p++);
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700176 if (c == 0 || c == '\n')
177 break;
Kees Cook2ca9bb42014-06-06 14:37:18 -0700178 data[len++] = c;
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700179 }
Kees Cookf8808302014-06-06 14:37:17 -0700180 data[len] = 0;
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700181 } else {
182 len = strlen(data);
183 if (len > maxlen)
184 len = maxlen;
Oleg Nesterov8d060872007-02-10 01:46:38 -0800185
186 if (*ppos > len) {
187 *lenp = 0;
188 return 0;
189 }
190
191 data += *ppos;
192 len -= *ppos;
193
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700194 if (len > *lenp)
195 len = *lenp;
196 if (len)
Christoph Hellwig32927392020-04-24 08:43:38 +0200197 memcpy(buffer, data, len);
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700198 if (len < *lenp) {
Christoph Hellwig32927392020-04-24 08:43:38 +0200199 buffer[len] = '\n';
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700200 len++;
201 }
202 *lenp = len;
203 *ppos += len;
204 }
205 return 0;
206}
207
Kees Cookf4aacea2014-06-06 14:37:19 -0700208static void warn_sysctl_write(struct ctl_table *table)
209{
210 pr_warn_once("%s wrote to %s when file position was not 0!\n"
211 "This will not be supported in the future. To silence this\n"
212 "warning, set kernel.sysctl_writes_strict = -1\n",
213 current->comm, table->procname);
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/**
Randy Dunlap5f733e82018-08-21 22:01:06 -0700217 * proc_first_pos_non_zero_ignore - check if first position is allowed
Luis R. Rodriguezd383d482017-07-12 14:33:33 -0700218 * @ppos: file position
219 * @table: the sysctl table
220 *
221 * Returns true if the first position is non-zero and the sysctl_writes_strict
222 * mode indicates this is not allowed for numeric input types. String proc
Randy Dunlap5f733e82018-08-21 22:01:06 -0700223 * handlers can ignore the return value.
Luis R. Rodriguezd383d482017-07-12 14:33:33 -0700224 */
225static bool proc_first_pos_non_zero_ignore(loff_t *ppos,
226 struct ctl_table *table)
227{
228 if (!*ppos)
229 return false;
230
231 switch (sysctl_writes_strict) {
232 case SYSCTL_WRITES_STRICT:
233 return true;
234 case SYSCTL_WRITES_WARN:
235 warn_sysctl_write(table);
236 return false;
237 default:
238 return false;
239 }
240}
241
242/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * proc_dostring - read a string sysctl
244 * @table: the sysctl table
245 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * @buffer: the user buffer
247 * @lenp: the size of the user buffer
248 * @ppos: file position
249 *
250 * Reads/writes a string from/to the user buffer. If the kernel
251 * buffer provided is not large enough to hold the string, the
252 * string is truncated. The copied string is %NULL-terminated.
253 * If the string is being read by the user process, it is copied
254 * and a newline '\n' is added. It is truncated if the buffer is
255 * not large enough.
256 *
257 * Returns 0 on success.
258 */
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700259int proc_dostring(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200260 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Luis R. Rodriguezd383d482017-07-12 14:33:33 -0700262 if (write)
263 proc_first_pos_non_zero_ignore(ppos, table);
Kees Cookf4aacea2014-06-06 14:37:19 -0700264
Christoph Hellwig32927392020-04-24 08:43:38 +0200265 return _proc_do_string(table->data, table->maxlen, write, buffer, lenp,
266 ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Linus Torvaldsbce93322022-12-05 12:09:06 -0800269static void proc_skip_spaces(char **buf, size_t *size)
Amerigo Wang00b7c332010-05-05 00:26:45 +0000270{
Linus Torvaldsbce93322022-12-05 12:09:06 -0800271 while (*size) {
272 if (!isspace(**buf))
273 break;
274 (*size)--;
275 (*buf)++;
276 }
Amerigo Wang00b7c332010-05-05 00:26:45 +0000277}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Octavian Purdila9f977fb2010-05-05 00:26:55 +0000279static void proc_skip_char(char **buf, size_t *size, const char v)
280{
281 while (*size) {
282 if (**buf != v)
283 break;
284 (*size)--;
285 (*buf)++;
286 }
287}
288
Christian Brauner7f2923c2019-03-07 16:29:40 -0800289/**
290 * strtoul_lenient - parse an ASCII formatted integer from a buffer and only
291 * fail on overflow
292 *
293 * @cp: kernel buffer containing the string to parse
294 * @endp: pointer to store the trailing characters
295 * @base: the base to use
296 * @res: where the parsed integer will be stored
297 *
298 * In case of success 0 is returned and @res will contain the parsed integer,
299 * @endp will hold any trailing characters.
300 * This function will fail the parse on overflow. If there wasn't an overflow
301 * the function will defer the decision what characters count as invalid to the
302 * caller.
303 */
304static int strtoul_lenient(const char *cp, char **endp, unsigned int base,
305 unsigned long *res)
306{
307 unsigned long long result;
308 unsigned int rv;
309
310 cp = _parse_integer_fixup_radix(cp, &base);
311 rv = _parse_integer(cp, base, &result);
312 if ((rv & KSTRTOX_OVERFLOW) || (result != (unsigned long)result))
313 return -ERANGE;
314
315 cp += rv;
316
317 if (endp)
318 *endp = (char *)cp;
319
320 *res = (unsigned long)result;
321 return 0;
322}
323
Amerigo Wang00b7c332010-05-05 00:26:45 +0000324#define TMPBUFLEN 22
325/**
Randy Dunlap0fc377b2010-05-21 11:29:53 -0700326 * proc_get_long - reads an ASCII formatted integer from a user buffer
Amerigo Wang00b7c332010-05-05 00:26:45 +0000327 *
Randy Dunlap0fc377b2010-05-21 11:29:53 -0700328 * @buf: a kernel buffer
329 * @size: size of the kernel buffer
330 * @val: this is where the number will be stored
331 * @neg: set to %TRUE if number is negative
332 * @perm_tr: a vector which contains the allowed trailers
333 * @perm_tr_len: size of the perm_tr vector
334 * @tr: pointer to store the trailer character
Amerigo Wang00b7c332010-05-05 00:26:45 +0000335 *
Randy Dunlap0fc377b2010-05-21 11:29:53 -0700336 * In case of success %0 is returned and @buf and @size are updated with
337 * the amount of bytes read. If @tr is non-NULL and a trailing
338 * character exists (size is non-zero after returning from this
339 * function), @tr is updated with the trailing character.
Amerigo Wang00b7c332010-05-05 00:26:45 +0000340 */
341static int proc_get_long(char **buf, size_t *size,
342 unsigned long *val, bool *neg,
343 const char *perm_tr, unsigned perm_tr_len, char *tr)
344{
Amerigo Wang00b7c332010-05-05 00:26:45 +0000345 char *p, tmp[TMPBUFLEN];
Linus Torvaldse6cfaf32022-12-05 11:33:40 -0800346 ssize_t len = *size;
Amerigo Wang00b7c332010-05-05 00:26:45 +0000347
Linus Torvaldse6cfaf32022-12-05 11:33:40 -0800348 if (len <= 0)
Amerigo Wang00b7c332010-05-05 00:26:45 +0000349 return -EINVAL;
350
Amerigo Wang00b7c332010-05-05 00:26:45 +0000351 if (len > TMPBUFLEN - 1)
352 len = TMPBUFLEN - 1;
353
354 memcpy(tmp, *buf, len);
355
356 tmp[len] = 0;
357 p = tmp;
358 if (*p == '-' && *size > 1) {
359 *neg = true;
360 p++;
361 } else
362 *neg = false;
363 if (!isdigit(*p))
364 return -EINVAL;
365
Christian Brauner7f2923c2019-03-07 16:29:40 -0800366 if (strtoul_lenient(p, &p, 0, val))
367 return -EINVAL;
Amerigo Wang00b7c332010-05-05 00:26:45 +0000368
369 len = p - tmp;
370
371 /* We don't know if the next char is whitespace thus we may accept
372 * invalid integers (e.g. 1234...a) or two integers instead of one
373 * (e.g. 123...1). So lets not allow such large numbers. */
374 if (len == TMPBUFLEN - 1)
375 return -EINVAL;
376
377 if (len < *size && perm_tr_len && !memchr(perm_tr, *p, perm_tr_len))
378 return -EINVAL;
379
380 if (tr && (len < *size))
381 *tr = *p;
382
383 *buf += len;
384 *size -= len;
385
386 return 0;
387}
388
389/**
Randy Dunlap0fc377b2010-05-21 11:29:53 -0700390 * proc_put_long - converts an integer to a decimal ASCII formatted string
Amerigo Wang00b7c332010-05-05 00:26:45 +0000391 *
Randy Dunlap0fc377b2010-05-21 11:29:53 -0700392 * @buf: the user buffer
393 * @size: the size of the user buffer
394 * @val: the integer to be converted
395 * @neg: sign of the number, %TRUE for negative
Amerigo Wang00b7c332010-05-05 00:26:45 +0000396 *
Christoph Hellwig32927392020-04-24 08:43:38 +0200397 * In case of success @buf and @size are updated with the amount of bytes
398 * written.
Amerigo Wang00b7c332010-05-05 00:26:45 +0000399 */
Christoph Hellwig32927392020-04-24 08:43:38 +0200400static void proc_put_long(void **buf, size_t *size, unsigned long val, bool neg)
Amerigo Wang00b7c332010-05-05 00:26:45 +0000401{
402 int len;
403 char tmp[TMPBUFLEN], *p = tmp;
404
405 sprintf(p, "%s%lu", neg ? "-" : "", val);
406 len = strlen(tmp);
407 if (len > *size)
408 len = *size;
Christoph Hellwig32927392020-04-24 08:43:38 +0200409 memcpy(*buf, tmp, len);
Amerigo Wang00b7c332010-05-05 00:26:45 +0000410 *size -= len;
411 *buf += len;
Amerigo Wang00b7c332010-05-05 00:26:45 +0000412}
413#undef TMPBUFLEN
414
Christoph Hellwig32927392020-04-24 08:43:38 +0200415static void proc_put_char(void **buf, size_t *size, char c)
Amerigo Wang00b7c332010-05-05 00:26:45 +0000416{
417 if (*size) {
Christoph Hellwig32927392020-04-24 08:43:38 +0200418 char **buffer = (char **)buf;
419 **buffer = c;
420
421 (*size)--;
422 (*buffer)++;
Amerigo Wang00b7c332010-05-05 00:26:45 +0000423 *buf = *buffer;
424 }
Amerigo Wang00b7c332010-05-05 00:26:45 +0000425}
426
427static int do_proc_dointvec_conv(bool *negp, unsigned long *lvalp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 int *valp,
429 int write, void *data)
430{
431 if (write) {
Heinrich Schuchardt230633d2015-04-16 12:48:07 -0700432 if (*negp) {
433 if (*lvalp > (unsigned long) INT_MAX + 1)
434 return -EINVAL;
Kuniyuki Iwashima1f1be042022-07-06 16:39:52 -0700435 WRITE_ONCE(*valp, -*lvalp);
Heinrich Schuchardt230633d2015-04-16 12:48:07 -0700436 } else {
437 if (*lvalp > (unsigned long) INT_MAX)
438 return -EINVAL;
Kuniyuki Iwashima1f1be042022-07-06 16:39:52 -0700439 WRITE_ONCE(*valp, *lvalp);
Heinrich Schuchardt230633d2015-04-16 12:48:07 -0700440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 } else {
Kuniyuki Iwashima1f1be042022-07-06 16:39:52 -0700442 int val = READ_ONCE(*valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (val < 0) {
Amerigo Wang00b7c332010-05-05 00:26:45 +0000444 *negp = true;
Ilya Dryomov9a5bc722015-09-09 15:39:06 -0700445 *lvalp = -(unsigned long)val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 } else {
Amerigo Wang00b7c332010-05-05 00:26:45 +0000447 *negp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 *lvalp = (unsigned long)val;
449 }
450 }
451 return 0;
452}
453
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700454static int do_proc_douintvec_conv(unsigned long *lvalp,
455 unsigned int *valp,
456 int write, void *data)
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -0700457{
458 if (write) {
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700459 if (*lvalp > UINT_MAX)
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -0700460 return -EINVAL;
Kuniyuki Iwashima4762b532022-07-06 16:39:53 -0700461 WRITE_ONCE(*valp, *lvalp);
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -0700462 } else {
Kuniyuki Iwashima4762b532022-07-06 16:39:53 -0700463 unsigned int val = READ_ONCE(*valp);
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -0700464 *lvalp = (unsigned long)val;
465 }
466 return 0;
467}
468
Amerigo Wang00b7c332010-05-05 00:26:45 +0000469static const char proc_wspace_sep[] = { ' ', '\t', '\n' };
470
Eric W. Biedermand8217f02007-10-18 03:05:22 -0700471static int __do_proc_dointvec(void *tbl_data, struct ctl_table *table,
Christoph Hellwig32927392020-04-24 08:43:38 +0200472 int write, void *buffer,
Kirill Korotaevfcfbd542006-10-02 02:18:23 -0700473 size_t *lenp, loff_t *ppos,
Amerigo Wang00b7c332010-05-05 00:26:45 +0000474 int (*conv)(bool *negp, unsigned long *lvalp, int *valp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 int write, void *data),
476 void *data)
477{
Amerigo Wang00b7c332010-05-05 00:26:45 +0000478 int *i, vleft, first = 1, err = 0;
Amerigo Wang00b7c332010-05-05 00:26:45 +0000479 size_t left;
Christoph Hellwig32927392020-04-24 08:43:38 +0200480 char *p;
Fanjun Kong374a7232022-05-16 17:07:53 +0800481
Amerigo Wang00b7c332010-05-05 00:26:45 +0000482 if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 *lenp = 0;
484 return 0;
485 }
Fanjun Kong374a7232022-05-16 17:07:53 +0800486
Kirill Korotaevfcfbd542006-10-02 02:18:23 -0700487 i = (int *) tbl_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 vleft = table->maxlen / sizeof(*i);
489 left = *lenp;
490
491 if (!conv)
492 conv = do_proc_dointvec_conv;
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (write) {
Luis R. Rodriguezd383d482017-07-12 14:33:33 -0700495 if (proc_first_pos_non_zero_ignore(ppos, table))
496 goto out;
Kees Cookf4aacea2014-06-06 14:37:19 -0700497
Amerigo Wang00b7c332010-05-05 00:26:45 +0000498 if (left > PAGE_SIZE - 1)
499 left = PAGE_SIZE - 1;
Christoph Hellwig32927392020-04-24 08:43:38 +0200500 p = buffer;
Amerigo Wang00b7c332010-05-05 00:26:45 +0000501 }
502
503 for (; left && vleft--; i++, first=0) {
504 unsigned long lval;
505 bool neg;
506
507 if (write) {
Linus Torvaldsbce93322022-12-05 12:09:06 -0800508 proc_skip_spaces(&p, &left);
Amerigo Wang00b7c332010-05-05 00:26:45 +0000509
J. R. Okajima563b0462010-05-25 16:10:14 -0700510 if (!left)
511 break;
Al Viro70f6cbb2015-12-24 00:13:10 -0500512 err = proc_get_long(&p, &left, &lval, &neg,
Amerigo Wang00b7c332010-05-05 00:26:45 +0000513 proc_wspace_sep,
514 sizeof(proc_wspace_sep), NULL);
515 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 break;
Amerigo Wang00b7c332010-05-05 00:26:45 +0000517 if (conv(&neg, &lval, i, 1, data)) {
518 err = -EINVAL;
519 break;
520 }
521 } else {
522 if (conv(&neg, &lval, i, 0, data)) {
523 err = -EINVAL;
524 break;
525 }
526 if (!first)
Christoph Hellwig32927392020-04-24 08:43:38 +0200527 proc_put_char(&buffer, &left, '\t');
528 proc_put_long(&buffer, &left, lval, neg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530 }
Amerigo Wang00b7c332010-05-05 00:26:45 +0000531
532 if (!write && !first && left && !err)
Christoph Hellwig32927392020-04-24 08:43:38 +0200533 proc_put_char(&buffer, &left, '\n');
J. R. Okajima563b0462010-05-25 16:10:14 -0700534 if (write && !err && left)
Linus Torvaldsbce93322022-12-05 12:09:06 -0800535 proc_skip_spaces(&p, &left);
Christoph Hellwig32927392020-04-24 08:43:38 +0200536 if (write && first)
537 return err ? : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 *lenp -= left;
Kees Cookf4aacea2014-06-06 14:37:19 -0700539out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 *ppos += *lenp;
Amerigo Wang00b7c332010-05-05 00:26:45 +0000541 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700544static int do_proc_dointvec(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200545 void *buffer, size_t *lenp, loff_t *ppos,
Amerigo Wang00b7c332010-05-05 00:26:45 +0000546 int (*conv)(bool *negp, unsigned long *lvalp, int *valp,
Kirill Korotaevfcfbd542006-10-02 02:18:23 -0700547 int write, void *data),
548 void *data)
549{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700550 return __do_proc_dointvec(table->data, table, write,
Kirill Korotaevfcfbd542006-10-02 02:18:23 -0700551 buffer, lenp, ppos, conv, data);
552}
553
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700554static int do_proc_douintvec_w(unsigned int *tbl_data,
555 struct ctl_table *table,
Christoph Hellwig32927392020-04-24 08:43:38 +0200556 void *buffer,
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700557 size_t *lenp, loff_t *ppos,
558 int (*conv)(unsigned long *lvalp,
559 unsigned int *valp,
560 int write, void *data),
561 void *data)
562{
563 unsigned long lval;
564 int err = 0;
565 size_t left;
566 bool neg;
Christoph Hellwig32927392020-04-24 08:43:38 +0200567 char *p = buffer;
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700568
569 left = *lenp;
570
571 if (proc_first_pos_non_zero_ignore(ppos, table))
572 goto bail_early;
573
574 if (left > PAGE_SIZE - 1)
575 left = PAGE_SIZE - 1;
576
Linus Torvaldsbce93322022-12-05 12:09:06 -0800577 proc_skip_spaces(&p, &left);
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700578 if (!left) {
579 err = -EINVAL;
580 goto out_free;
581 }
582
583 err = proc_get_long(&p, &left, &lval, &neg,
584 proc_wspace_sep,
585 sizeof(proc_wspace_sep), NULL);
586 if (err || neg) {
587 err = -EINVAL;
588 goto out_free;
589 }
590
591 if (conv(&lval, tbl_data, 1, data)) {
592 err = -EINVAL;
593 goto out_free;
594 }
595
596 if (!err && left)
Linus Torvaldsbce93322022-12-05 12:09:06 -0800597 proc_skip_spaces(&p, &left);
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700598
599out_free:
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700600 if (err)
601 return -EINVAL;
602
603 return 0;
604
605 /* This is in keeping with old __do_proc_dointvec() */
606bail_early:
607 *ppos += *lenp;
608 return err;
609}
610
Christoph Hellwig32927392020-04-24 08:43:38 +0200611static int do_proc_douintvec_r(unsigned int *tbl_data, void *buffer,
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700612 size_t *lenp, loff_t *ppos,
613 int (*conv)(unsigned long *lvalp,
614 unsigned int *valp,
615 int write, void *data),
616 void *data)
617{
618 unsigned long lval;
619 int err = 0;
620 size_t left;
621
622 left = *lenp;
623
624 if (conv(&lval, tbl_data, 0, data)) {
625 err = -EINVAL;
626 goto out;
627 }
628
Christoph Hellwig32927392020-04-24 08:43:38 +0200629 proc_put_long(&buffer, &left, lval, false);
630 if (!left)
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700631 goto out;
632
Christoph Hellwig32927392020-04-24 08:43:38 +0200633 proc_put_char(&buffer, &left, '\n');
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700634
635out:
636 *lenp -= left;
637 *ppos += *lenp;
638
639 return err;
640}
641
642static int __do_proc_douintvec(void *tbl_data, struct ctl_table *table,
Christoph Hellwig32927392020-04-24 08:43:38 +0200643 int write, void *buffer,
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700644 size_t *lenp, loff_t *ppos,
645 int (*conv)(unsigned long *lvalp,
646 unsigned int *valp,
647 int write, void *data),
648 void *data)
649{
650 unsigned int *i, vleft;
651
652 if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) {
653 *lenp = 0;
654 return 0;
655 }
656
657 i = (unsigned int *) tbl_data;
658 vleft = table->maxlen / sizeof(*i);
659
660 /*
661 * Arrays are not supported, keep this simple. *Do not* add
662 * support for them.
663 */
664 if (vleft != 1) {
665 *lenp = 0;
666 return -EINVAL;
667 }
668
669 if (!conv)
670 conv = do_proc_douintvec_conv;
671
672 if (write)
673 return do_proc_douintvec_w(i, table, buffer, lenp, ppos,
674 conv, data);
675 return do_proc_douintvec_r(i, buffer, lenp, ppos, conv, data);
676}
677
Luis Chamberlain1998f192022-01-21 22:13:20 -0800678int do_proc_douintvec(struct ctl_table *table, int write,
679 void *buffer, size_t *lenp, loff_t *ppos,
680 int (*conv)(unsigned long *lvalp,
681 unsigned int *valp,
682 int write, void *data),
683 void *data)
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700684{
685 return __do_proc_douintvec(table->data, table, write,
686 buffer, lenp, ppos, conv, data);
687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/**
Jia Hea2071572021-08-03 12:59:36 +0200690 * proc_dobool - read/write a bool
691 * @table: the sysctl table
692 * @write: %TRUE if this is a write to the sysctl file
693 * @buffer: the user buffer
694 * @lenp: the size of the user buffer
695 * @ppos: file position
696 *
Ondrej Mosnacekf1aa2eb2023-02-10 15:58:23 +0100697 * Reads/writes one integer value from/to the user buffer,
698 * treated as an ASCII string.
699 *
700 * table->data must point to a bool variable and table->maxlen must
701 * be sizeof(bool).
Jia Hea2071572021-08-03 12:59:36 +0200702 *
703 * Returns 0 on success.
704 */
705int proc_dobool(struct ctl_table *table, int write, void *buffer,
706 size_t *lenp, loff_t *ppos)
707{
Ondrej Mosnacekf1aa2eb2023-02-10 15:58:23 +0100708 struct ctl_table tmp;
709 bool *data = table->data;
710 int res, val;
711
712 /* Do not support arrays yet. */
713 if (table->maxlen != sizeof(bool))
714 return -EINVAL;
715
716 tmp = *table;
717 tmp.maxlen = sizeof(val);
718 tmp.data = &val;
719
720 val = READ_ONCE(*data);
721 res = proc_dointvec(&tmp, write, buffer, lenp, ppos);
722 if (res)
723 return res;
724 if (write)
725 WRITE_ONCE(*data, val);
726 return 0;
Jia Hea2071572021-08-03 12:59:36 +0200727}
728
729/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 * proc_dointvec - read a vector of integers
731 * @table: the sysctl table
732 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * @buffer: the user buffer
734 * @lenp: the size of the user buffer
735 * @ppos: file position
736 *
737 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
Fanjun Kong374a7232022-05-16 17:07:53 +0800738 * values from/to the user buffer, treated as an ASCII string.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 *
740 * Returns 0 on success.
741 */
Christoph Hellwig32927392020-04-24 08:43:38 +0200742int proc_dointvec(struct ctl_table *table, int write, void *buffer,
743 size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -0700745 return do_proc_dointvec(table, write, buffer, lenp, ppos, NULL, NULL);
746}
747
748/**
749 * proc_douintvec - read a vector of unsigned integers
750 * @table: the sysctl table
751 * @write: %TRUE if this is a write to the sysctl file
752 * @buffer: the user buffer
753 * @lenp: the size of the user buffer
754 * @ppos: file position
755 *
756 * Reads/writes up to table->maxlen/sizeof(unsigned int) unsigned integer
757 * values from/to the user buffer, treated as an ASCII string.
758 *
759 * Returns 0 on success.
760 */
Christoph Hellwig32927392020-04-24 08:43:38 +0200761int proc_douintvec(struct ctl_table *table, int write, void *buffer,
762 size_t *lenp, loff_t *ppos)
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -0700763{
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700764 return do_proc_douintvec(table, write, buffer, lenp, ppos,
765 do_proc_douintvec_conv, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800768/*
Andi Kleen25ddbb12008-10-15 22:01:41 -0700769 * Taint values can only be increased
770 * This means we can safely use a temporary.
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800771 */
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700772static int proc_taint(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200773 void *buffer, size_t *lenp, loff_t *ppos)
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800774{
Andi Kleen25ddbb12008-10-15 22:01:41 -0700775 struct ctl_table t;
776 unsigned long tmptaint = get_taint();
777 int err;
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800778
Bastian Blank91fcd412007-04-23 14:41:14 -0700779 if (write && !capable(CAP_SYS_ADMIN))
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800780 return -EPERM;
781
Andi Kleen25ddbb12008-10-15 22:01:41 -0700782 t = *table;
783 t.data = &tmptaint;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700784 err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700785 if (err < 0)
786 return err;
787
788 if (write) {
Rafael Aquinidb38d5c2020-06-07 21:40:17 -0700789 int i;
790
791 /*
792 * If we are relying on panic_on_taint not producing
793 * false positives due to userspace input, bail out
794 * before setting the requested taint flags.
795 */
796 if (panic_on_taint_nousertaint && (tmptaint & panic_on_taint))
797 return -EINVAL;
798
Andi Kleen25ddbb12008-10-15 22:01:41 -0700799 /*
800 * Poor man's atomic or. Not worth adding a primitive
801 * to everyone's atomic.h for this
802 */
Rafael Aquinie77132e2020-06-07 21:40:51 -0700803 for (i = 0; i < TAINT_FLAGS_COUNT; i++)
804 if ((1UL << i) & tmptaint)
Rusty Russell373d4d02013-01-21 17:17:39 +1030805 add_taint(i, LOCKDEP_STILL_OK);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700806 }
807
808 return err;
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800809}
810
Waiman Long24704f32018-04-10 16:35:38 -0700811/**
812 * struct do_proc_dointvec_minmax_conv_param - proc_dointvec_minmax() range checking structure
813 * @min: pointer to minimum allowable value
814 * @max: pointer to maximum allowable value
815 *
816 * The do_proc_dointvec_minmax_conv_param structure provides the
817 * minimum and maximum values for doing range checking for those sysctl
818 * parameters that use the proc_dointvec_minmax() handler.
819 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820struct do_proc_dointvec_minmax_conv_param {
821 int *min;
822 int *max;
823};
824
Amerigo Wang00b7c332010-05-05 00:26:45 +0000825static int do_proc_dointvec_minmax_conv(bool *negp, unsigned long *lvalp,
826 int *valp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 int write, void *data)
828{
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700829 int tmp, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 struct do_proc_dointvec_minmax_conv_param *param = data;
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700831 /*
832 * If writing, first do so via a temporary local int so we can
833 * bounds-check it before touching *valp.
834 */
835 int *ip = write ? &tmp : valp;
836
837 ret = do_proc_dointvec_conv(negp, lvalp, ip, write, data);
838 if (ret)
839 return ret;
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (write) {
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700842 if ((param->min && *param->min > tmp) ||
843 (param->max && *param->max < tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -EINVAL;
Kuniyuki Iwashimaf613d862022-07-06 16:39:54 -0700845 WRITE_ONCE(*valp, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return 0;
849}
850
851/**
852 * proc_dointvec_minmax - read a vector of integers with min/max values
853 * @table: the sysctl table
854 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 * @buffer: the user buffer
856 * @lenp: the size of the user buffer
857 * @ppos: file position
858 *
859 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
860 * values from/to the user buffer, treated as an ASCII string.
861 *
862 * This routine will ensure the values are within the range specified by
863 * table->extra1 (min) and table->extra2 (max).
864 *
Waiman Long24704f32018-04-10 16:35:38 -0700865 * Returns 0 on success or -EINVAL on write when the range check fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 */
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700867int proc_dointvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200868 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 struct do_proc_dointvec_minmax_conv_param param = {
871 .min = (int *) table->extra1,
872 .max = (int *) table->extra2,
873 };
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700874 return do_proc_dointvec(table, write, buffer, lenp, ppos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 do_proc_dointvec_minmax_conv, &param);
876}
877
Waiman Long24704f32018-04-10 16:35:38 -0700878/**
879 * struct do_proc_douintvec_minmax_conv_param - proc_douintvec_minmax() range checking structure
880 * @min: pointer to minimum allowable value
881 * @max: pointer to maximum allowable value
882 *
883 * The do_proc_douintvec_minmax_conv_param structure provides the
884 * minimum and maximum values for doing range checking for those sysctl
885 * parameters that use the proc_douintvec_minmax() handler.
886 */
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -0700887struct do_proc_douintvec_minmax_conv_param {
888 unsigned int *min;
889 unsigned int *max;
890};
891
892static int do_proc_douintvec_minmax_conv(unsigned long *lvalp,
893 unsigned int *valp,
894 int write, void *data)
895{
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700896 int ret;
897 unsigned int tmp;
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -0700898 struct do_proc_douintvec_minmax_conv_param *param = data;
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700899 /* write via temporary local uint for bounds-checking */
900 unsigned int *up = write ? &tmp : valp;
901
902 ret = do_proc_douintvec_conv(lvalp, up, write, data);
903 if (ret)
904 return ret;
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -0700905
906 if (write) {
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700907 if ((param->min && *param->min > tmp) ||
908 (param->max && *param->max < tmp))
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -0700909 return -ERANGE;
910
Kuniyuki Iwashima2d3b5592022-07-06 16:39:55 -0700911 WRITE_ONCE(*valp, tmp);
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -0700912 }
913
914 return 0;
915}
916
917/**
918 * proc_douintvec_minmax - read a vector of unsigned ints with min/max values
919 * @table: the sysctl table
920 * @write: %TRUE if this is a write to the sysctl file
921 * @buffer: the user buffer
922 * @lenp: the size of the user buffer
923 * @ppos: file position
924 *
925 * Reads/writes up to table->maxlen/sizeof(unsigned int) unsigned integer
926 * values from/to the user buffer, treated as an ASCII string. Negative
927 * strings are not allowed.
928 *
929 * This routine will ensure the values are within the range specified by
930 * table->extra1 (min) and table->extra2 (max). There is a final sanity
931 * check for UINT_MAX to avoid having to support wrap around uses from
932 * userspace.
933 *
Waiman Long24704f32018-04-10 16:35:38 -0700934 * Returns 0 on success or -ERANGE on write when the range check fails.
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -0700935 */
936int proc_douintvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200937 void *buffer, size_t *lenp, loff_t *ppos)
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -0700938{
939 struct do_proc_douintvec_minmax_conv_param param = {
940 .min = (unsigned int *) table->extra1,
941 .max = (unsigned int *) table->extra2,
942 };
943 return do_proc_douintvec(table, write, buffer, lenp, ppos,
944 do_proc_douintvec_minmax_conv, &param);
945}
946
Eric Dumazetcb944412021-03-25 11:08:13 -0700947/**
948 * proc_dou8vec_minmax - read a vector of unsigned chars with min/max values
949 * @table: the sysctl table
950 * @write: %TRUE if this is a write to the sysctl file
951 * @buffer: the user buffer
952 * @lenp: the size of the user buffer
953 * @ppos: file position
954 *
955 * Reads/writes up to table->maxlen/sizeof(u8) unsigned chars
956 * values from/to the user buffer, treated as an ASCII string. Negative
957 * strings are not allowed.
958 *
959 * This routine will ensure the values are within the range specified by
960 * table->extra1 (min) and table->extra2 (max).
961 *
962 * Returns 0 on success or an error on write when the range check fails.
963 */
964int proc_dou8vec_minmax(struct ctl_table *table, int write,
965 void *buffer, size_t *lenp, loff_t *ppos)
966{
967 struct ctl_table tmp;
968 unsigned int min = 0, max = 255U, val;
969 u8 *data = table->data;
970 struct do_proc_douintvec_minmax_conv_param param = {
971 .min = &min,
972 .max = &max,
973 };
974 int res;
975
976 /* Do not support arrays yet. */
977 if (table->maxlen != sizeof(u8))
978 return -EINVAL;
979
980 if (table->extra1) {
981 min = *(unsigned int *) table->extra1;
982 if (min > 255U)
983 return -EINVAL;
984 }
985 if (table->extra2) {
986 max = *(unsigned int *) table->extra2;
987 if (max > 255U)
988 return -EINVAL;
989 }
990
991 tmp = *table;
992
993 tmp.maxlen = sizeof(val);
994 tmp.data = &val;
Kuniyuki Iwashima7dee5d72022-07-11 17:15:19 -0700995 val = READ_ONCE(*data);
Eric Dumazetcb944412021-03-25 11:08:13 -0700996 res = do_proc_douintvec(&tmp, write, buffer, lenp, ppos,
997 do_proc_douintvec_minmax_conv, &param);
998 if (res)
999 return res;
1000 if (write)
Kuniyuki Iwashima7dee5d72022-07-11 17:15:19 -07001001 WRITE_ONCE(*data, val);
Eric Dumazetcb944412021-03-25 11:08:13 -07001002 return 0;
1003}
1004EXPORT_SYMBOL_GPL(proc_dou8vec_minmax);
1005
Dmitry Safonoveaee4172020-03-02 17:51:34 +00001006#ifdef CONFIG_MAGIC_SYSRQ
1007static int sysrq_sysctl_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001008 void *buffer, size_t *lenp, loff_t *ppos)
Dmitry Safonoveaee4172020-03-02 17:51:34 +00001009{
1010 int tmp, ret;
1011
1012 tmp = sysrq_mask();
1013
1014 ret = __do_proc_dointvec(&tmp, table, write, buffer,
1015 lenp, ppos, NULL, NULL);
1016 if (ret || !write)
1017 return ret;
1018
1019 if (write)
1020 sysrq_toggle_support(tmp);
1021
1022 return 0;
1023}
1024#endif
1025
Christoph Hellwig32927392020-04-24 08:43:38 +02001026static int __do_proc_doulongvec_minmax(void *data, struct ctl_table *table,
1027 int write, void *buffer, size_t *lenp, loff_t *ppos,
1028 unsigned long convmul, unsigned long convdiv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Amerigo Wang00b7c332010-05-05 00:26:45 +00001030 unsigned long *i, *min, *max;
1031 int vleft, first = 1, err = 0;
Amerigo Wang00b7c332010-05-05 00:26:45 +00001032 size_t left;
Christoph Hellwig32927392020-04-24 08:43:38 +02001033 char *p;
Amerigo Wang00b7c332010-05-05 00:26:45 +00001034
1035 if (!data || !table->maxlen || !*lenp || (*ppos && !write)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 *lenp = 0;
1037 return 0;
1038 }
Amerigo Wang00b7c332010-05-05 00:26:45 +00001039
Dong Chuanjian8ebc4122022-08-22 14:30:49 +08001040 i = data;
1041 min = table->extra1;
1042 max = table->extra2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 vleft = table->maxlen / sizeof(unsigned long);
1044 left = *lenp;
Amerigo Wang00b7c332010-05-05 00:26:45 +00001045
1046 if (write) {
Luis R. Rodriguezd383d482017-07-12 14:33:33 -07001047 if (proc_first_pos_non_zero_ignore(ppos, table))
1048 goto out;
Kees Cookf4aacea2014-06-06 14:37:19 -07001049
Amerigo Wang00b7c332010-05-05 00:26:45 +00001050 if (left > PAGE_SIZE - 1)
1051 left = PAGE_SIZE - 1;
Christoph Hellwig32927392020-04-24 08:43:38 +02001052 p = buffer;
Amerigo Wang00b7c332010-05-05 00:26:45 +00001053 }
1054
Eric Dumazet27b3d802010-10-07 12:59:29 -07001055 for (; left && vleft--; i++, first = 0) {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001056 unsigned long val;
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (write) {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001059 bool neg;
1060
Linus Torvaldsbce93322022-12-05 12:09:06 -08001061 proc_skip_spaces(&p, &left);
Cheng Lin09be1782019-01-03 15:26:13 -08001062 if (!left)
1063 break;
Amerigo Wang00b7c332010-05-05 00:26:45 +00001064
Al Viro70f6cbb2015-12-24 00:13:10 -05001065 err = proc_get_long(&p, &left, &val, &neg,
Amerigo Wang00b7c332010-05-05 00:26:45 +00001066 proc_wspace_sep,
1067 sizeof(proc_wspace_sep), NULL);
Baokun Li1622ed72022-01-21 22:13:48 -08001068 if (err || neg) {
1069 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 break;
Baokun Li1622ed72022-01-21 22:13:48 -08001071 }
1072
Eric Dumazetff9f8a72017-01-25 18:20:55 -08001073 val = convmul * val / convdiv;
Christian Braunere260ad02019-05-14 15:44:55 -07001074 if ((min && val < *min) || (max && val > *max)) {
1075 err = -EINVAL;
1076 break;
1077 }
Kuniyuki Iwashimac31bcc82022-07-06 16:39:56 -07001078 WRITE_ONCE(*i, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 } else {
Kuniyuki Iwashimac31bcc82022-07-06 16:39:56 -07001080 val = convdiv * READ_ONCE(*i) / convmul;
Christoph Hellwig32927392020-04-24 08:43:38 +02001081 if (!first)
1082 proc_put_char(&buffer, &left, '\t');
1083 proc_put_long(&buffer, &left, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085 }
1086
Amerigo Wang00b7c332010-05-05 00:26:45 +00001087 if (!write && !first && left && !err)
Christoph Hellwig32927392020-04-24 08:43:38 +02001088 proc_put_char(&buffer, &left, '\n');
Amerigo Wang00b7c332010-05-05 00:26:45 +00001089 if (write && !err)
Linus Torvaldsbce93322022-12-05 12:09:06 -08001090 proc_skip_spaces(&p, &left);
Christoph Hellwig32927392020-04-24 08:43:38 +02001091 if (write && first)
1092 return err ? : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 *lenp -= left;
Kees Cookf4aacea2014-06-06 14:37:19 -07001094out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 *ppos += *lenp;
Amerigo Wang00b7c332010-05-05 00:26:45 +00001096 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
Eric W. Biedermand8217f02007-10-18 03:05:22 -07001099static int do_proc_doulongvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001100 void *buffer, size_t *lenp, loff_t *ppos, unsigned long convmul,
1101 unsigned long convdiv)
Kirill Korotaevfcfbd542006-10-02 02:18:23 -07001102{
1103 return __do_proc_doulongvec_minmax(table->data, table, write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001104 buffer, lenp, ppos, convmul, convdiv);
Kirill Korotaevfcfbd542006-10-02 02:18:23 -07001105}
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107/**
1108 * proc_doulongvec_minmax - read a vector of long integers with min/max values
1109 * @table: the sysctl table
1110 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 * @buffer: the user buffer
1112 * @lenp: the size of the user buffer
1113 * @ppos: file position
1114 *
1115 * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
1116 * values from/to the user buffer, treated as an ASCII string.
1117 *
1118 * This routine will ensure the values are within the range specified by
1119 * table->extra1 (min) and table->extra2 (max).
1120 *
1121 * Returns 0 on success.
1122 */
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001123int proc_doulongvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001124 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001126 return do_proc_doulongvec_minmax(table, write, buffer, lenp, ppos, 1l, 1l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
1129/**
1130 * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
1131 * @table: the sysctl table
1132 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 * @buffer: the user buffer
1134 * @lenp: the size of the user buffer
1135 * @ppos: file position
1136 *
1137 * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
1138 * values from/to the user buffer, treated as an ASCII string. The values
1139 * are treated as milliseconds, and converted to jiffies when they are stored.
1140 *
1141 * This routine will ensure the values are within the range specified by
1142 * table->extra1 (min) and table->extra2 (max).
1143 *
1144 * Returns 0 on success.
1145 */
Eric W. Biedermand8217f02007-10-18 03:05:22 -07001146int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001147 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001149 return do_proc_doulongvec_minmax(table, write, buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 lenp, ppos, HZ, 1000l);
1151}
1152
1153
Amerigo Wang00b7c332010-05-05 00:26:45 +00001154static int do_proc_dointvec_jiffies_conv(bool *negp, unsigned long *lvalp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 int *valp,
1156 int write, void *data)
1157{
1158 if (write) {
Gao Feng63259452017-05-08 15:54:58 -07001159 if (*lvalp > INT_MAX / HZ)
Bart Samwelcba9f332006-03-24 03:15:50 -08001160 return 1;
Kuniyuki Iwashimae8778202022-07-06 16:39:57 -07001161 if (*negp)
1162 WRITE_ONCE(*valp, -*lvalp * HZ);
1163 else
1164 WRITE_ONCE(*valp, *lvalp * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 } else {
Kuniyuki Iwashimae8778202022-07-06 16:39:57 -07001166 int val = READ_ONCE(*valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 unsigned long lval;
1168 if (val < 0) {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001169 *negp = true;
Ilya Dryomov9a5bc722015-09-09 15:39:06 -07001170 lval = -(unsigned long)val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 } else {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001172 *negp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 lval = (unsigned long)val;
1174 }
1175 *lvalp = lval / HZ;
1176 }
1177 return 0;
1178}
1179
Amerigo Wang00b7c332010-05-05 00:26:45 +00001180static int do_proc_dointvec_userhz_jiffies_conv(bool *negp, unsigned long *lvalp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 int *valp,
1182 int write, void *data)
1183{
1184 if (write) {
Bart Samwelcba9f332006-03-24 03:15:50 -08001185 if (USER_HZ < HZ && *lvalp > (LONG_MAX / HZ) * USER_HZ)
1186 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 *valp = clock_t_to_jiffies(*negp ? -*lvalp : *lvalp);
1188 } else {
1189 int val = *valp;
1190 unsigned long lval;
1191 if (val < 0) {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001192 *negp = true;
Ilya Dryomov9a5bc722015-09-09 15:39:06 -07001193 lval = -(unsigned long)val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 } else {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001195 *negp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 lval = (unsigned long)val;
1197 }
1198 *lvalp = jiffies_to_clock_t(lval);
1199 }
1200 return 0;
1201}
1202
Amerigo Wang00b7c332010-05-05 00:26:45 +00001203static int do_proc_dointvec_ms_jiffies_conv(bool *negp, unsigned long *lvalp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 int *valp,
1205 int write, void *data)
1206{
1207 if (write) {
Francesco Fuscod738ce82013-07-24 10:39:07 +02001208 unsigned long jif = msecs_to_jiffies(*negp ? -*lvalp : *lvalp);
1209
1210 if (jif > INT_MAX)
1211 return 1;
Kuniyuki Iwashima7d1025e2022-07-11 17:15:20 -07001212 WRITE_ONCE(*valp, (int)jif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 } else {
Kuniyuki Iwashima7d1025e2022-07-11 17:15:20 -07001214 int val = READ_ONCE(*valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 unsigned long lval;
1216 if (val < 0) {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001217 *negp = true;
Ilya Dryomov9a5bc722015-09-09 15:39:06 -07001218 lval = -(unsigned long)val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 } else {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001220 *negp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 lval = (unsigned long)val;
1222 }
1223 *lvalp = jiffies_to_msecs(lval);
1224 }
1225 return 0;
1226}
1227
Yuwei Wangc381d022022-06-29 08:48:31 +00001228static int do_proc_dointvec_ms_jiffies_minmax_conv(bool *negp, unsigned long *lvalp,
1229 int *valp, int write, void *data)
1230{
1231 int tmp, ret;
1232 struct do_proc_dointvec_minmax_conv_param *param = data;
1233 /*
1234 * If writing, first do so via a temporary local int so we can
1235 * bounds-check it before touching *valp.
1236 */
1237 int *ip = write ? &tmp : valp;
1238
1239 ret = do_proc_dointvec_ms_jiffies_conv(negp, lvalp, ip, write, data);
1240 if (ret)
1241 return ret;
1242
1243 if (write) {
1244 if ((param->min && *param->min > tmp) ||
1245 (param->max && *param->max < tmp))
1246 return -EINVAL;
1247 *valp = tmp;
1248 }
1249 return 0;
1250}
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252/**
1253 * proc_dointvec_jiffies - read a vector of integers as seconds
1254 * @table: the sysctl table
1255 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 * @buffer: the user buffer
1257 * @lenp: the size of the user buffer
1258 * @ppos: file position
1259 *
1260 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
Fanjun Kong374a7232022-05-16 17:07:53 +08001261 * values from/to the user buffer, treated as an ASCII string.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 * The values read are assumed to be in seconds, and are converted into
1263 * jiffies.
1264 *
1265 * Returns 0 on success.
1266 */
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001267int proc_dointvec_jiffies(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001268 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001270 return do_proc_dointvec(table,write,buffer,lenp,ppos,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 do_proc_dointvec_jiffies_conv,NULL);
1272}
1273
Yuwei Wangc381d022022-06-29 08:48:31 +00001274int proc_dointvec_ms_jiffies_minmax(struct ctl_table *table, int write,
1275 void *buffer, size_t *lenp, loff_t *ppos)
1276{
1277 struct do_proc_dointvec_minmax_conv_param param = {
1278 .min = (int *) table->extra1,
1279 .max = (int *) table->extra2,
1280 };
1281 return do_proc_dointvec(table, write, buffer, lenp, ppos,
1282 do_proc_dointvec_ms_jiffies_minmax_conv, &param);
1283}
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285/**
1286 * proc_dointvec_userhz_jiffies - read a vector of integers as 1/USER_HZ seconds
1287 * @table: the sysctl table
1288 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 * @buffer: the user buffer
1290 * @lenp: the size of the user buffer
Randy Dunlap1e5d5332005-11-07 01:01:06 -08001291 * @ppos: pointer to the file position
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 *
1293 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
Fanjun Kong374a7232022-05-16 17:07:53 +08001294 * values from/to the user buffer, treated as an ASCII string.
1295 * The values read are assumed to be in 1/USER_HZ seconds, and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 * are converted into jiffies.
1297 *
1298 * Returns 0 on success.
1299 */
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001300int proc_dointvec_userhz_jiffies(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001301 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Fanjun Kong5bfd5d32022-05-22 13:29:33 +08001303 return do_proc_dointvec(table, write, buffer, lenp, ppos,
1304 do_proc_dointvec_userhz_jiffies_conv, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
1307/**
1308 * proc_dointvec_ms_jiffies - read a vector of integers as 1 milliseconds
1309 * @table: the sysctl table
1310 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 * @buffer: the user buffer
1312 * @lenp: the size of the user buffer
Martin Waitz67be2dd2005-05-01 08:59:26 -07001313 * @ppos: file position
1314 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 *
1316 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
Kuniyuki Iwashima7d1025e2022-07-11 17:15:20 -07001317 * values from/to the user buffer, treated as an ASCII string.
1318 * The values read are assumed to be in 1/1000 seconds, and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 * are converted into jiffies.
1320 *
1321 * Returns 0 on success.
1322 */
Christoph Hellwig32927392020-04-24 08:43:38 +02001323int proc_dointvec_ms_jiffies(struct ctl_table *table, int write, void *buffer,
1324 size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001326 return do_proc_dointvec(table, write, buffer, lenp, ppos,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 do_proc_dointvec_ms_jiffies_conv, NULL);
1328}
1329
Christoph Hellwig32927392020-04-24 08:43:38 +02001330static int proc_do_cad_pid(struct ctl_table *table, int write, void *buffer,
1331 size_t *lenp, loff_t *ppos)
Cedric Le Goater9ec52092006-10-02 02:19:00 -07001332{
1333 struct pid *new_pid;
1334 pid_t tmp;
1335 int r;
1336
Pavel Emelyanov6c5f3e72008-02-08 04:19:20 -08001337 tmp = pid_vnr(cad_pid);
Cedric Le Goater9ec52092006-10-02 02:19:00 -07001338
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001339 r = __do_proc_dointvec(&tmp, table, write, buffer,
Cedric Le Goater9ec52092006-10-02 02:19:00 -07001340 lenp, ppos, NULL, NULL);
1341 if (r || !write)
1342 return r;
1343
1344 new_pid = find_get_pid(tmp);
1345 if (!new_pid)
1346 return -ESRCH;
1347
1348 put_pid(xchg(&cad_pid, new_pid));
1349 return 0;
1350}
1351
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001352/**
1353 * proc_do_large_bitmap - read/write from/to a large bitmap
1354 * @table: the sysctl table
1355 * @write: %TRUE if this is a write to the sysctl file
1356 * @buffer: the user buffer
1357 * @lenp: the size of the user buffer
1358 * @ppos: file position
1359 *
1360 * The bitmap is stored at table->data and the bitmap length (in bits)
1361 * in table->maxlen.
1362 *
1363 * We use a range comma separated format (e.g. 1,3-4,10-10) so that
1364 * large bitmaps may be represented in a compact manner. Writing into
1365 * the file will clear the bitmap then update it with the given input.
1366 *
1367 * Returns 0 on success.
1368 */
1369int proc_do_large_bitmap(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001370 void *buffer, size_t *lenp, loff_t *ppos)
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001371{
1372 int err = 0;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001373 size_t left = *lenp;
1374 unsigned long bitmap_len = table->maxlen;
WANG Cong122ff242014-05-12 16:04:53 -07001375 unsigned long *bitmap = *(unsigned long **) table->data;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001376 unsigned long *tmp_bitmap = NULL;
1377 char tr_a[] = { '-', ',', '\n' }, tr_b[] = { ',', '\n', 0 }, c;
1378
WANG Cong122ff242014-05-12 16:04:53 -07001379 if (!bitmap || !bitmap_len || !left || (*ppos && !write)) {
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001380 *lenp = 0;
1381 return 0;
1382 }
1383
1384 if (write) {
Christoph Hellwig32927392020-04-24 08:43:38 +02001385 char *p = buffer;
Eric Sandeen3116ad32019-05-14 15:45:13 -07001386 size_t skipped = 0;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001387
Eric Sandeen3116ad32019-05-14 15:45:13 -07001388 if (left > PAGE_SIZE - 1) {
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001389 left = PAGE_SIZE - 1;
Eric Sandeen3116ad32019-05-14 15:45:13 -07001390 /* How much of the buffer we'll skip this pass */
1391 skipped = *lenp - left;
1392 }
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001393
Andy Shevchenko475dae32019-05-14 15:44:52 -07001394 tmp_bitmap = bitmap_zalloc(bitmap_len, GFP_KERNEL);
Christoph Hellwig32927392020-04-24 08:43:38 +02001395 if (!tmp_bitmap)
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001396 return -ENOMEM;
Al Viro70f6cbb2015-12-24 00:13:10 -05001397 proc_skip_char(&p, &left, '\n');
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001398 while (!err && left) {
1399 unsigned long val_a, val_b;
1400 bool neg;
Eric Sandeen3116ad32019-05-14 15:45:13 -07001401 size_t saved_left;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001402
Eric Sandeen3116ad32019-05-14 15:45:13 -07001403 /* In case we stop parsing mid-number, we can reset */
1404 saved_left = left;
Al Viro70f6cbb2015-12-24 00:13:10 -05001405 err = proc_get_long(&p, &left, &val_a, &neg, tr_a,
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001406 sizeof(tr_a), &c);
Eric Sandeen3116ad32019-05-14 15:45:13 -07001407 /*
1408 * If we consumed the entirety of a truncated buffer or
1409 * only one char is left (may be a "-"), then stop here,
1410 * reset, & come back for more.
1411 */
1412 if ((left <= 1) && skipped) {
1413 left = saved_left;
1414 break;
1415 }
1416
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001417 if (err)
1418 break;
1419 if (val_a >= bitmap_len || neg) {
1420 err = -EINVAL;
1421 break;
1422 }
1423
1424 val_b = val_a;
1425 if (left) {
Al Viro70f6cbb2015-12-24 00:13:10 -05001426 p++;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001427 left--;
1428 }
1429
1430 if (c == '-') {
Al Viro70f6cbb2015-12-24 00:13:10 -05001431 err = proc_get_long(&p, &left, &val_b,
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001432 &neg, tr_b, sizeof(tr_b),
1433 &c);
Eric Sandeen3116ad32019-05-14 15:45:13 -07001434 /*
1435 * If we consumed all of a truncated buffer or
1436 * then stop here, reset, & come back for more.
1437 */
1438 if (!left && skipped) {
1439 left = saved_left;
1440 break;
1441 }
1442
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001443 if (err)
1444 break;
1445 if (val_b >= bitmap_len || neg ||
1446 val_a > val_b) {
1447 err = -EINVAL;
1448 break;
1449 }
1450 if (left) {
Al Viro70f6cbb2015-12-24 00:13:10 -05001451 p++;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001452 left--;
1453 }
1454 }
1455
Akinobu Mita5a04cca2012-03-28 14:42:50 -07001456 bitmap_set(tmp_bitmap, val_a, val_b - val_a + 1);
Al Viro70f6cbb2015-12-24 00:13:10 -05001457 proc_skip_char(&p, &left, '\n');
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001458 }
Eric Sandeen3116ad32019-05-14 15:45:13 -07001459 left += skipped;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001460 } else {
1461 unsigned long bit_a, bit_b = 0;
Jiapeng Chong9a52c5f2021-06-30 18:54:53 -07001462 bool first = 1;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001463
1464 while (left) {
1465 bit_a = find_next_bit(bitmap, bitmap_len, bit_b);
1466 if (bit_a >= bitmap_len)
1467 break;
1468 bit_b = find_next_zero_bit(bitmap, bitmap_len,
1469 bit_a + 1) - 1;
1470
Christoph Hellwig32927392020-04-24 08:43:38 +02001471 if (!first)
1472 proc_put_char(&buffer, &left, ',');
1473 proc_put_long(&buffer, &left, bit_a, false);
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001474 if (bit_a != bit_b) {
Christoph Hellwig32927392020-04-24 08:43:38 +02001475 proc_put_char(&buffer, &left, '-');
1476 proc_put_long(&buffer, &left, bit_b, false);
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001477 }
1478
1479 first = 0; bit_b++;
1480 }
Christoph Hellwig32927392020-04-24 08:43:38 +02001481 proc_put_char(&buffer, &left, '\n');
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001482 }
1483
1484 if (!err) {
1485 if (write) {
1486 if (*ppos)
1487 bitmap_or(bitmap, bitmap, tmp_bitmap, bitmap_len);
1488 else
Akinobu Mita5a04cca2012-03-28 14:42:50 -07001489 bitmap_copy(bitmap, tmp_bitmap, bitmap_len);
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001490 }
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001491 *lenp -= left;
1492 *ppos += *lenp;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001493 }
Ola N. Kaldestadf9eb2fd2017-11-17 15:30:26 -08001494
Andy Shevchenko475dae32019-05-14 15:44:52 -07001495 bitmap_free(tmp_bitmap);
Ola N. Kaldestadf9eb2fd2017-11-17 15:30:26 -08001496 return err;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001497}
1498
Jovi Zhang55610502011-01-12 17:00:45 -08001499#else /* CONFIG_PROC_SYSCTL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001501int proc_dostring(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001502 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 return -ENOSYS;
1505}
1506
Jia Hea2071572021-08-03 12:59:36 +02001507int proc_dobool(struct ctl_table *table, int write,
1508 void *buffer, size_t *lenp, loff_t *ppos)
1509{
1510 return -ENOSYS;
1511}
1512
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001513int proc_dointvec(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001514 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 return -ENOSYS;
1517}
1518
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -07001519int proc_douintvec(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001520 void *buffer, size_t *lenp, loff_t *ppos)
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -07001521{
1522 return -ENOSYS;
1523}
1524
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001525int proc_dointvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001526 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 return -ENOSYS;
1529}
1530
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -07001531int proc_douintvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001532 void *buffer, size_t *lenp, loff_t *ppos)
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -07001533{
1534 return -ENOSYS;
1535}
1536
Eric Dumazetcb944412021-03-25 11:08:13 -07001537int proc_dou8vec_minmax(struct ctl_table *table, int write,
1538 void *buffer, size_t *lenp, loff_t *ppos)
1539{
1540 return -ENOSYS;
1541}
1542
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001543int proc_dointvec_jiffies(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001544 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 return -ENOSYS;
1547}
1548
Yuwei Wangc381d022022-06-29 08:48:31 +00001549int proc_dointvec_ms_jiffies_minmax(struct ctl_table *table, int write,
1550 void *buffer, size_t *lenp, loff_t *ppos)
1551{
1552 return -ENOSYS;
1553}
1554
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001555int proc_dointvec_userhz_jiffies(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001556 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 return -ENOSYS;
1559}
1560
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001561int proc_dointvec_ms_jiffies(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001562 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
1564 return -ENOSYS;
1565}
1566
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001567int proc_doulongvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001568 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 return -ENOSYS;
1571}
1572
Eric W. Biedermand8217f02007-10-18 03:05:22 -07001573int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001574 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Christoph Hellwig32927392020-04-24 08:43:38 +02001576 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Stephen Suryaputra0bc19982019-04-17 16:35:49 -04001579int proc_do_large_bitmap(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001580 void *buffer, size_t *lenp, loff_t *ppos)
Stephen Suryaputra0bc19982019-04-17 16:35:49 -04001581{
1582 return -ENOSYS;
1583}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Jovi Zhang55610502011-01-12 17:00:45 -08001585#endif /* CONFIG_PROC_SYSCTL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Eric Dumazeta8e11e52019-06-14 16:22:18 -07001587#if defined(CONFIG_SYSCTL)
1588int proc_do_static_key(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001589 void *buffer, size_t *lenp, loff_t *ppos)
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001590{
Eric Dumazeta8e11e52019-06-14 16:22:18 -07001591 struct static_key *key = (struct static_key *)table->data;
1592 static DEFINE_MUTEX(static_key_mutex);
1593 int val, ret;
1594 struct ctl_table tmp = {
1595 .data = &val,
1596 .maxlen = sizeof(val),
1597 .mode = table->mode,
Matteo Croceeec48442019-07-18 15:58:50 -07001598 .extra1 = SYSCTL_ZERO,
1599 .extra2 = SYSCTL_ONE,
Eric Dumazeta8e11e52019-06-14 16:22:18 -07001600 };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001601
1602 if (write && !capable(CAP_SYS_ADMIN))
1603 return -EPERM;
1604
Eric Dumazeta8e11e52019-06-14 16:22:18 -07001605 mutex_lock(&static_key_mutex);
1606 val = static_key_enabled(key);
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001607 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1608 if (write && !ret) {
Eric Dumazeta8e11e52019-06-14 16:22:18 -07001609 if (val)
1610 static_key_enable(key);
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001611 else
Eric Dumazeta8e11e52019-06-14 16:22:18 -07001612 static_key_disable(key);
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001613 }
Eric Dumazeta8e11e52019-06-14 16:22:18 -07001614 mutex_unlock(&static_key_mutex);
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001615 return ret;
1616}
Christoph Hellwigf461d2d2020-04-24 08:43:37 +02001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618static struct ctl_table kern_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 .procname = "panic",
1621 .data = &panic_timeout,
1622 .maxlen = sizeof(int),
1623 .mode = 0644,
1624 .proc_handler = proc_dointvec,
1625 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626#ifdef CONFIG_PROC_SYSCTL
1627 {
1628 .procname = "tainted",
1629 .maxlen = sizeof(long),
1630 .mode = 0644,
1631 .proc_handler = proc_taint,
1632 },
1633 {
1634 .procname = "sysctl_writes_strict",
1635 .data = &sysctl_writes_strict,
1636 .maxlen = sizeof(int),
1637 .mode = 0644,
1638 .proc_handler = proc_dointvec_minmax,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08001639 .extra1 = SYSCTL_NEG_ONE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 .extra2 = SYSCTL_ONE,
1641 },
1642#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 {
1644 .procname = "print-fatal-signals",
1645 .data = &print_fatal_signals,
1646 .maxlen = sizeof(int),
1647 .mode = 0644,
1648 .proc_handler = proc_dointvec,
1649 },
1650#ifdef CONFIG_SPARC
1651 {
1652 .procname = "reboot-cmd",
1653 .data = reboot_command,
1654 .maxlen = 256,
1655 .mode = 0644,
1656 .proc_handler = proc_dostring,
1657 },
1658 {
1659 .procname = "stop-a",
1660 .data = &stop_a_enabled,
1661 .maxlen = sizeof (int),
1662 .mode = 0644,
1663 .proc_handler = proc_dointvec,
1664 },
1665 {
1666 .procname = "scons-poweroff",
1667 .data = &scons_pwroff,
1668 .maxlen = sizeof (int),
1669 .mode = 0644,
1670 .proc_handler = proc_dointvec,
1671 },
1672#endif
1673#ifdef CONFIG_SPARC64
1674 {
1675 .procname = "tsb-ratio",
1676 .data = &sysctl_tsb_ratio,
1677 .maxlen = sizeof (int),
1678 .mode = 0644,
1679 .proc_handler = proc_dointvec,
1680 },
1681#endif
1682#ifdef CONFIG_PARISC
1683 {
1684 .procname = "soft-power",
1685 .data = &pwrsw_enabled,
1686 .maxlen = sizeof (int),
Christoph Hellwigf461d2d2020-04-24 08:43:37 +02001687 .mode = 0644,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 .proc_handler = proc_dointvec,
1689 },
1690#endif
1691#ifdef CONFIG_SYSCTL_ARCH_UNALIGN_ALLOW
1692 {
1693 .procname = "unaligned-trap",
1694 .data = &unaligned_enabled,
1695 .maxlen = sizeof (int),
1696 .mode = 0644,
1697 .proc_handler = proc_dointvec,
1698 },
1699#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700#ifdef CONFIG_STACK_TRACER
1701 {
1702 .procname = "stack_tracer_enabled",
1703 .data = &stack_tracer_enabled,
1704 .maxlen = sizeof(int),
1705 .mode = 0644,
1706 .proc_handler = stack_trace_sysctl,
1707 },
1708#endif
1709#ifdef CONFIG_TRACING
1710 {
1711 .procname = "ftrace_dump_on_oops",
1712 .data = &ftrace_dump_on_oops,
1713 .maxlen = sizeof(int),
1714 .mode = 0644,
1715 .proc_handler = proc_dointvec,
1716 },
1717 {
1718 .procname = "traceoff_on_warning",
1719 .data = &__disable_trace_on_warning,
1720 .maxlen = sizeof(__disable_trace_on_warning),
1721 .mode = 0644,
1722 .proc_handler = proc_dointvec,
1723 },
1724 {
1725 .procname = "tracepoint_printk",
1726 .data = &tracepoint_printk,
1727 .maxlen = sizeof(tracepoint_printk),
1728 .mode = 0644,
1729 .proc_handler = tracepoint_printk_sysctl,
1730 },
1731#endif
1732#ifdef CONFIG_MODULES
1733 {
1734 .procname = "modprobe",
1735 .data = &modprobe_path,
1736 .maxlen = KMOD_PATH_LEN,
1737 .mode = 0644,
1738 .proc_handler = proc_dostring,
1739 },
1740 {
1741 .procname = "modules_disabled",
1742 .data = &modules_disabled,
1743 .maxlen = sizeof(int),
1744 .mode = 0644,
1745 /* only handle a transition from default "0" to "1" */
1746 .proc_handler = proc_dointvec_minmax,
1747 .extra1 = SYSCTL_ONE,
1748 .extra2 = SYSCTL_ONE,
1749 },
1750#endif
1751#ifdef CONFIG_UEVENT_HELPER
1752 {
1753 .procname = "hotplug",
1754 .data = &uevent_helper,
1755 .maxlen = UEVENT_HELPER_PATH_LEN,
1756 .mode = 0644,
1757 .proc_handler = proc_dostring,
1758 },
1759#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760#ifdef CONFIG_MAGIC_SYSRQ
1761 {
1762 .procname = "sysrq",
1763 .data = NULL,
1764 .maxlen = sizeof (int),
1765 .mode = 0644,
1766 .proc_handler = sysrq_sysctl_handler,
1767 },
1768#endif
1769#ifdef CONFIG_PROC_SYSCTL
1770 {
1771 .procname = "cad_pid",
1772 .data = NULL,
1773 .maxlen = sizeof (int),
1774 .mode = 0600,
1775 .proc_handler = proc_do_cad_pid,
1776 },
1777#endif
1778 {
1779 .procname = "threads-max",
1780 .data = NULL,
1781 .maxlen = sizeof(int),
1782 .mode = 0644,
1783 .proc_handler = sysctl_max_threads,
1784 },
1785 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 .procname = "overflowuid",
1787 .data = &overflowuid,
1788 .maxlen = sizeof(int),
1789 .mode = 0644,
1790 .proc_handler = proc_dointvec_minmax,
Xiaoming Ni2452dcb2022-01-21 22:11:19 -08001791 .extra1 = SYSCTL_ZERO,
Luis Chamberlain54771612022-01-21 22:13:03 -08001792 .extra2 = SYSCTL_MAXOLDUID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 },
1794 {
1795 .procname = "overflowgid",
1796 .data = &overflowgid,
1797 .maxlen = sizeof(int),
1798 .mode = 0644,
1799 .proc_handler = proc_dointvec_minmax,
Xiaoming Ni2452dcb2022-01-21 22:11:19 -08001800 .extra1 = SYSCTL_ZERO,
Luis Chamberlain54771612022-01-21 22:13:03 -08001801 .extra2 = SYSCTL_MAXOLDUID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 },
1803#ifdef CONFIG_S390
1804 {
1805 .procname = "userprocess_debug",
1806 .data = &show_unhandled_signals,
1807 .maxlen = sizeof(int),
1808 .mode = 0644,
1809 .proc_handler = proc_dointvec,
1810 },
1811#endif
1812 {
1813 .procname = "pid_max",
1814 .data = &pid_max,
1815 .maxlen = sizeof (int),
1816 .mode = 0644,
1817 .proc_handler = proc_dointvec_minmax,
1818 .extra1 = &pid_max_min,
1819 .extra2 = &pid_max_max,
1820 },
1821 {
1822 .procname = "panic_on_oops",
1823 .data = &panic_on_oops,
1824 .maxlen = sizeof(int),
1825 .mode = 0644,
1826 .proc_handler = proc_dointvec,
1827 },
1828 {
1829 .procname = "panic_print",
1830 .data = &panic_print,
1831 .maxlen = sizeof(unsigned long),
1832 .mode = 0644,
1833 .proc_handler = proc_doulongvec_minmax,
1834 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 {
1836 .procname = "ngroups_max",
Stephen Kittf6288672022-01-21 22:11:09 -08001837 .data = (void *)&ngroups_max,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 .maxlen = sizeof (int),
1839 .mode = 0444,
1840 .proc_handler = proc_dointvec,
1841 },
1842 {
1843 .procname = "cap_last_cap",
1844 .data = (void *)&cap_last_cap,
1845 .maxlen = sizeof(int),
1846 .mode = 0444,
1847 .proc_handler = proc_dointvec,
1848 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849#if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86)
1850 {
1851 .procname = "unknown_nmi_panic",
1852 .data = &unknown_nmi_panic,
1853 .maxlen = sizeof (int),
1854 .mode = 0644,
1855 .proc_handler = proc_dointvec,
1856 },
1857#endif
Xiaoming Nib6522fa2020-04-11 21:06:19 +08001858
1859#if (defined(CONFIG_X86_32) || defined(CONFIG_PARISC)) && \
1860 defined(CONFIG_DEBUG_STACKOVERFLOW)
1861 {
1862 .procname = "panic_on_stackoverflow",
1863 .data = &sysctl_panic_on_stackoverflow,
1864 .maxlen = sizeof(int),
1865 .mode = 0644,
1866 .proc_handler = proc_dointvec,
1867 },
1868#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869#if defined(CONFIG_X86)
1870 {
1871 .procname = "panic_on_unrecovered_nmi",
1872 .data = &panic_on_unrecovered_nmi,
1873 .maxlen = sizeof(int),
1874 .mode = 0644,
1875 .proc_handler = proc_dointvec,
1876 },
1877 {
1878 .procname = "panic_on_io_nmi",
1879 .data = &panic_on_io_nmi,
1880 .maxlen = sizeof(int),
1881 .mode = 0644,
1882 .proc_handler = proc_dointvec,
1883 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 {
1885 .procname = "bootloader_type",
1886 .data = &bootloader_type,
1887 .maxlen = sizeof (int),
1888 .mode = 0444,
1889 .proc_handler = proc_dointvec,
1890 },
1891 {
1892 .procname = "bootloader_version",
1893 .data = &bootloader_version,
1894 .maxlen = sizeof (int),
1895 .mode = 0444,
1896 .proc_handler = proc_dointvec,
1897 },
1898 {
1899 .procname = "io_delay_type",
1900 .data = &io_delay_type,
1901 .maxlen = sizeof(int),
1902 .mode = 0644,
1903 .proc_handler = proc_dointvec,
1904 },
1905#endif
1906#if defined(CONFIG_MMU)
1907 {
1908 .procname = "randomize_va_space",
1909 .data = &randomize_va_space,
1910 .maxlen = sizeof(int),
1911 .mode = 0644,
1912 .proc_handler = proc_dointvec,
1913 },
1914#endif
1915#if defined(CONFIG_S390) && defined(CONFIG_SMP)
1916 {
1917 .procname = "spin_retry",
1918 .data = &spin_retry,
1919 .maxlen = sizeof (int),
1920 .mode = 0644,
1921 .proc_handler = proc_dointvec,
1922 },
1923#endif
1924#if defined(CONFIG_ACPI_SLEEP) && defined(CONFIG_X86)
1925 {
1926 .procname = "acpi_video_flags",
1927 .data = &acpi_realmode_flags,
1928 .maxlen = sizeof (unsigned long),
1929 .mode = 0644,
1930 .proc_handler = proc_doulongvec_minmax,
1931 },
1932#endif
1933#ifdef CONFIG_SYSCTL_ARCH_UNALIGN_NO_WARN
1934 {
1935 .procname = "ignore-unaligned-usertrap",
1936 .data = &no_unaligned_warning,
1937 .maxlen = sizeof (int),
Christoph Hellwigf461d2d2020-04-24 08:43:37 +02001938 .mode = 0644,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 .proc_handler = proc_dointvec,
1940 },
1941#endif
1942#ifdef CONFIG_IA64
1943 {
1944 .procname = "unaligned-dump-stack",
1945 .data = &unaligned_dump_stack,
1946 .maxlen = sizeof (int),
1947 .mode = 0644,
1948 .proc_handler = proc_dointvec,
1949 },
1950#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951#ifdef CONFIG_RT_MUTEXES
1952 {
1953 .procname = "max_lock_depth",
1954 .data = &max_lock_depth,
1955 .maxlen = sizeof(int),
1956 .mode = 0644,
1957 .proc_handler = proc_dointvec,
1958 },
1959#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960#ifdef CONFIG_PERF_EVENTS
1961 /*
1962 * User-space scripts rely on the existence of this file
1963 * as a feature check for perf_events being enabled.
1964 *
1965 * So it's an ABI, do not remove!
1966 */
1967 {
1968 .procname = "perf_event_paranoid",
1969 .data = &sysctl_perf_event_paranoid,
1970 .maxlen = sizeof(sysctl_perf_event_paranoid),
1971 .mode = 0644,
1972 .proc_handler = proc_dointvec,
1973 },
1974 {
1975 .procname = "perf_event_mlock_kb",
1976 .data = &sysctl_perf_event_mlock,
1977 .maxlen = sizeof(sysctl_perf_event_mlock),
1978 .mode = 0644,
1979 .proc_handler = proc_dointvec,
1980 },
1981 {
1982 .procname = "perf_event_max_sample_rate",
1983 .data = &sysctl_perf_event_sample_rate,
1984 .maxlen = sizeof(sysctl_perf_event_sample_rate),
1985 .mode = 0644,
1986 .proc_handler = perf_proc_update_handler,
1987 .extra1 = SYSCTL_ONE,
1988 },
1989 {
1990 .procname = "perf_cpu_time_max_percent",
1991 .data = &sysctl_perf_cpu_time_max_percent,
1992 .maxlen = sizeof(sysctl_perf_cpu_time_max_percent),
1993 .mode = 0644,
1994 .proc_handler = perf_cpu_time_max_percent_handler,
1995 .extra1 = SYSCTL_ZERO,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08001996 .extra2 = SYSCTL_ONE_HUNDRED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 },
1998 {
1999 .procname = "perf_event_max_stack",
2000 .data = &sysctl_perf_event_max_stack,
2001 .maxlen = sizeof(sysctl_perf_event_max_stack),
2002 .mode = 0644,
2003 .proc_handler = perf_event_max_stack_handler,
2004 .extra1 = SYSCTL_ZERO,
Xiaoming Nid73840e2022-01-21 22:11:14 -08002005 .extra2 = (void *)&six_hundred_forty_kb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 },
2007 {
2008 .procname = "perf_event_max_contexts_per_stack",
2009 .data = &sysctl_perf_event_max_contexts_per_stack,
2010 .maxlen = sizeof(sysctl_perf_event_max_contexts_per_stack),
2011 .mode = 0644,
2012 .proc_handler = perf_event_max_stack_handler,
2013 .extra1 = SYSCTL_ZERO,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002014 .extra2 = SYSCTL_ONE_THOUSAND,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 },
2016#endif
2017 {
2018 .procname = "panic_on_warn",
2019 .data = &panic_on_warn,
2020 .maxlen = sizeof(int),
2021 .mode = 0644,
2022 .proc_handler = proc_dointvec_minmax,
2023 .extra1 = SYSCTL_ZERO,
2024 .extra2 = SYSCTL_ONE,
2025 },
Geert Uytterhoeven7251ceb2022-05-17 17:07:31 +02002026#ifdef CONFIG_TREE_RCU
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 {
2028 .procname = "panic_on_rcu_stall",
2029 .data = &sysctl_panic_on_rcu_stall,
2030 .maxlen = sizeof(sysctl_panic_on_rcu_stall),
2031 .mode = 0644,
2032 .proc_handler = proc_dointvec_minmax,
2033 .extra1 = SYSCTL_ZERO,
2034 .extra2 = SYSCTL_ONE,
2035 },
chaodfe56402020-08-30 23:41:17 -07002036 {
2037 .procname = "max_rcu_stall_to_panic",
2038 .data = &sysctl_max_rcu_stall_to_panic,
2039 .maxlen = sizeof(sysctl_max_rcu_stall_to_panic),
2040 .mode = 0644,
2041 .proc_handler = proc_dointvec_minmax,
2042 .extra1 = SYSCTL_ONE,
2043 .extra2 = SYSCTL_INT_MAX,
2044 },
2045#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 { }
2047};
2048
2049static struct ctl_table vm_table[] = {
2050 {
2051 .procname = "overcommit_memory",
2052 .data = &sysctl_overcommit_memory,
2053 .maxlen = sizeof(sysctl_overcommit_memory),
2054 .mode = 0644,
Feng Tang56f35472020-08-06 23:23:15 -07002055 .proc_handler = overcommit_policy_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 .extra1 = SYSCTL_ZERO,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002057 .extra2 = SYSCTL_TWO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 },
2059 {
2060 .procname = "overcommit_ratio",
2061 .data = &sysctl_overcommit_ratio,
2062 .maxlen = sizeof(sysctl_overcommit_ratio),
2063 .mode = 0644,
2064 .proc_handler = overcommit_ratio_handler,
2065 },
2066 {
2067 .procname = "overcommit_kbytes",
2068 .data = &sysctl_overcommit_kbytes,
2069 .maxlen = sizeof(sysctl_overcommit_kbytes),
2070 .mode = 0644,
2071 .proc_handler = overcommit_kbytes_handler,
2072 },
2073 {
Christoph Hellwigf461d2d2020-04-24 08:43:37 +02002074 .procname = "page-cluster",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 .data = &page_cluster,
2076 .maxlen = sizeof(int),
2077 .mode = 0644,
2078 .proc_handler = proc_dointvec_minmax,
2079 .extra1 = SYSCTL_ZERO,
Kairui Songea0ffd02022-10-24 00:25:33 +08002080 .extra2 = (void *)&page_cluster_max,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 },
2082 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 .procname = "dirtytime_expire_seconds",
2084 .data = &dirtytime_expire_interval,
2085 .maxlen = sizeof(dirtytime_expire_interval),
2086 .mode = 0644,
2087 .proc_handler = dirtytime_interval_handler,
2088 .extra1 = SYSCTL_ZERO,
2089 },
2090 {
2091 .procname = "swappiness",
2092 .data = &vm_swappiness,
2093 .maxlen = sizeof(vm_swappiness),
2094 .mode = 0644,
2095 .proc_handler = proc_dointvec_minmax,
2096 .extra1 = SYSCTL_ZERO,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002097 .extra2 = SYSCTL_TWO_HUNDRED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 },
Muchun Song43b52402022-06-09 18:40:32 +08002099#ifdef CONFIG_NUMA
2100 {
2101 .procname = "numa_stat",
2102 .data = &sysctl_vm_numa_stat,
2103 .maxlen = sizeof(int),
2104 .mode = 0644,
2105 .proc_handler = sysctl_vm_numa_stat_handler,
2106 .extra1 = SYSCTL_ZERO,
2107 .extra2 = SYSCTL_ONE,
2108 },
2109#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 .procname = "drop_caches",
2112 .data = &sysctl_drop_caches,
2113 .maxlen = sizeof(int),
2114 .mode = 0200,
2115 .proc_handler = drop_caches_sysctl_handler,
2116 .extra1 = SYSCTL_ONE,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002117 .extra2 = SYSCTL_FOUR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 {
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07002120 .procname = "page_lock_unfairness",
2121 .data = &sysctl_page_lock_unfairness,
2122 .maxlen = sizeof(sysctl_page_lock_unfairness),
2123 .mode = 0644,
2124 .proc_handler = proc_dointvec_minmax,
2125 .extra1 = SYSCTL_ZERO,
2126 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127#ifdef CONFIG_MMU
2128 {
2129 .procname = "max_map_count",
2130 .data = &sysctl_max_map_count,
2131 .maxlen = sizeof(sysctl_max_map_count),
2132 .mode = 0644,
2133 .proc_handler = proc_dointvec_minmax,
2134 .extra1 = SYSCTL_ZERO,
2135 },
2136#else
2137 {
2138 .procname = "nr_trim_pages",
2139 .data = &sysctl_nr_trim_pages,
2140 .maxlen = sizeof(sysctl_nr_trim_pages),
2141 .mode = 0644,
2142 .proc_handler = proc_dointvec_minmax,
2143 .extra1 = SYSCTL_ZERO,
2144 },
2145#endif
2146 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 .procname = "vfs_cache_pressure",
2148 .data = &sysctl_vfs_cache_pressure,
2149 .maxlen = sizeof(sysctl_vfs_cache_pressure),
2150 .mode = 0644,
Lin Feng3b3376f2021-02-25 17:20:53 -08002151 .proc_handler = proc_dointvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 .extra1 = SYSCTL_ZERO,
2153 },
2154#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \
2155 defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT)
2156 {
2157 .procname = "legacy_va_layout",
2158 .data = &sysctl_legacy_va_layout,
2159 .maxlen = sizeof(sysctl_legacy_va_layout),
2160 .mode = 0644,
Lin Feng3b3376f2021-02-25 17:20:53 -08002161 .proc_handler = proc_dointvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 .extra1 = SYSCTL_ZERO,
2163 },
2164#endif
2165#ifdef CONFIG_NUMA
2166 {
2167 .procname = "zone_reclaim_mode",
2168 .data = &node_reclaim_mode,
2169 .maxlen = sizeof(node_reclaim_mode),
2170 .mode = 0644,
Lin Feng3b3376f2021-02-25 17:20:53 -08002171 .proc_handler = proc_dointvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 .extra1 = SYSCTL_ZERO,
2173 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174#endif
2175#ifdef CONFIG_SMP
2176 {
2177 .procname = "stat_interval",
2178 .data = &sysctl_stat_interval,
2179 .maxlen = sizeof(sysctl_stat_interval),
2180 .mode = 0644,
2181 .proc_handler = proc_dointvec_jiffies,
2182 },
2183 {
2184 .procname = "stat_refresh",
2185 .data = NULL,
2186 .maxlen = 0,
2187 .mode = 0600,
2188 .proc_handler = vmstat_refresh,
2189 },
2190#endif
2191#ifdef CONFIG_MMU
2192 {
2193 .procname = "mmap_min_addr",
2194 .data = &dac_mmap_min_addr,
2195 .maxlen = sizeof(unsigned long),
2196 .mode = 0644,
2197 .proc_handler = mmap_min_addr_handler,
2198 },
2199#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200#if (defined(CONFIG_X86_32) && !defined(CONFIG_UML))|| \
2201 (defined(CONFIG_SUPERH) && defined(CONFIG_VSYSCALL))
2202 {
2203 .procname = "vdso_enabled",
2204#ifdef CONFIG_X86_32
2205 .data = &vdso32_enabled,
2206 .maxlen = sizeof(vdso32_enabled),
2207#else
2208 .data = &vdso_enabled,
2209 .maxlen = sizeof(vdso_enabled),
2210#endif
2211 .mode = 0644,
2212 .proc_handler = proc_dointvec,
2213 .extra1 = SYSCTL_ZERO,
2214 },
2215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 {
2217 .procname = "user_reserve_kbytes",
2218 .data = &sysctl_user_reserve_kbytes,
2219 .maxlen = sizeof(sysctl_user_reserve_kbytes),
2220 .mode = 0644,
2221 .proc_handler = proc_doulongvec_minmax,
2222 },
2223 {
2224 .procname = "admin_reserve_kbytes",
2225 .data = &sysctl_admin_reserve_kbytes,
2226 .maxlen = sizeof(sysctl_admin_reserve_kbytes),
2227 .mode = 0644,
2228 .proc_handler = proc_doulongvec_minmax,
2229 },
2230#ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
2231 {
2232 .procname = "mmap_rnd_bits",
2233 .data = &mmap_rnd_bits,
2234 .maxlen = sizeof(mmap_rnd_bits),
2235 .mode = 0600,
2236 .proc_handler = proc_dointvec_minmax,
2237 .extra1 = (void *)&mmap_rnd_bits_min,
2238 .extra2 = (void *)&mmap_rnd_bits_max,
2239 },
2240#endif
2241#ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
2242 {
2243 .procname = "mmap_rnd_compat_bits",
2244 .data = &mmap_rnd_compat_bits,
2245 .maxlen = sizeof(mmap_rnd_compat_bits),
2246 .mode = 0600,
2247 .proc_handler = proc_dointvec_minmax,
2248 .extra1 = (void *)&mmap_rnd_compat_bits_min,
2249 .extra2 = (void *)&mmap_rnd_compat_bits_max,
2250 },
2251#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 { }
2253};
2254
Luis Chamberlaind8c04182022-01-21 22:13:31 -08002255int __init sysctl_init_bases(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Joel Granados2f5edd02023-05-23 14:22:19 +02002257 register_sysctl_init("kernel", kern_table);
Joel Granados2f5edd02023-05-23 14:22:19 +02002258 register_sysctl_init("vm", vm_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return 0;
2261}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262#endif /* CONFIG_SYSCTL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263/*
2264 * No sense putting this after each symbol definition, twice,
2265 * exception granted :-)
2266 */
Jia Hea2071572021-08-03 12:59:36 +02002267EXPORT_SYMBOL(proc_dobool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268EXPORT_SYMBOL(proc_dointvec);
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -07002269EXPORT_SYMBOL(proc_douintvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270EXPORT_SYMBOL(proc_dointvec_jiffies);
2271EXPORT_SYMBOL(proc_dointvec_minmax);
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -07002272EXPORT_SYMBOL_GPL(proc_douintvec_minmax);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
2274EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
2275EXPORT_SYMBOL(proc_dostring);
2276EXPORT_SYMBOL(proc_doulongvec_minmax);
2277EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);
Stephen Suryaputra0bc19982019-04-17 16:35:49 -04002278EXPORT_SYMBOL(proc_do_large_bitmap);