• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * sysctl.c: General linux system control interface
3  *
4  * Begun 24 March 1995, Stephen Tweedie
5  * Added /proc support, Dec 1995
6  * Added bdflush entry and intvec min/max checking, 2/23/96, Tom Dyas.
7  * Added hooks for /proc/sys/net (minor, minor patch), 96/4/1, Mike Shaver.
8  * Added kernel/java-{interpreter,appletviewer}, 96/5/10, Mike Shaver.
9  * Dynamic registration fixes, Stephen Tweedie.
10  * Added kswapd-interval, ctrl-alt-del, printk stuff, 1/8/97, Chris Horn.
11  * Made sysctl support optional via CONFIG_SYSCTL, 1/10/97, Chris
12  *  Horn.
13  * Added proc_doulongvec_ms_jiffies_minmax, 09/08/99, Carlos H. Bauer.
14  * Added proc_doulongvec_minmax, 09/08/99, Carlos H. Bauer.
15  * Changed linked lists to use list.h instead of lists.h, 02/24/00, Bill
16  *  Wendling.
17  * The list_for_each() macro wasn't appropriate for the sysctl loop.
18  *  Removed it and replaced it with older style, 03/23/00, Bill Wendling
19  */
20 
21 #include <linux/module.h>
22 #include <linux/mm.h>
23 #include <linux/swap.h>
24 #include <linux/slab.h>
25 #include <linux/sysctl.h>
26 #include <linux/proc_fs.h>
27 #include <linux/security.h>
28 #include <linux/ctype.h>
29 #include <linux/utsname.h>
30 #include <linux/smp_lock.h>
31 #include <linux/fs.h>
32 #include <linux/init.h>
33 #include <linux/kernel.h>
34 #include <linux/kobject.h>
35 #include <linux/net.h>
36 #include <linux/sysrq.h>
37 #include <linux/highuid.h>
38 #include <linux/writeback.h>
39 #include <linux/hugetlb.h>
40 #include <linux/initrd.h>
41 #include <linux/key.h>
42 #include <linux/times.h>
43 #include <linux/limits.h>
44 #include <linux/dcache.h>
45 #include <linux/syscalls.h>
46 #include <linux/vmstat.h>
47 #include <linux/nfs_fs.h>
48 #include <linux/acpi.h>
49 #include <linux/reboot.h>
50 #include <linux/ftrace.h>
51 
52 #include <asm/uaccess.h>
53 #include <asm/processor.h>
54 
55 #ifdef CONFIG_X86
56 #include <asm/nmi.h>
57 #include <asm/stacktrace.h>
58 #include <asm/io.h>
59 #endif
60 
61 static int deprecated_sysctl_warning(struct __sysctl_args *args);
62 
63 #if defined(CONFIG_SYSCTL)
64 
65 /* External variables not in a header file. */
66 extern int C_A_D;
67 extern int print_fatal_signals;
68 extern int sysctl_overcommit_memory;
69 extern int sysctl_overcommit_ratio;
70 extern int sysctl_panic_on_oom;
71 extern int sysctl_oom_kill_allocating_task;
72 extern int sysctl_oom_dump_tasks;
73 extern int max_threads;
74 extern int core_uses_pid;
75 extern int suid_dumpable;
76 extern char core_pattern[];
77 extern int pid_max;
78 extern int min_free_kbytes;
79 extern int min_free_order_shift;
80 extern int pid_max_min, pid_max_max;
81 extern int sysctl_drop_caches;
82 extern int percpu_pagelist_fraction;
83 extern int compat_log;
84 extern int latencytop_enabled;
85 extern int sysctl_nr_open_min, sysctl_nr_open_max;
86 #ifndef CONFIG_MMU
87 extern int sysctl_nr_trim_pages;
88 #endif
89 #ifdef CONFIG_RCU_TORTURE_TEST
90 extern int rcutorture_runnable;
91 #endif /* #ifdef CONFIG_RCU_TORTURE_TEST */
92 
93 /* Constants used for minimum and  maximum */
94 #ifdef CONFIG_DETECT_SOFTLOCKUP
95 static int sixty = 60;
96 static int neg_one = -1;
97 #endif
98 
99 #if defined(CONFIG_MMU) && defined(CONFIG_FILE_LOCKING)
100 static int two = 2;
101 #endif
102 
103 static int zero;
104 static int one = 1;
105 static unsigned long one_ul = 1;
106 static int one_hundred = 100;
107 
108 /* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
109 static int maxolduid = 65535;
110 static int minolduid;
111 static int min_percpu_pagelist_fract = 8;
112 
113 static int ngroups_max = NGROUPS_MAX;
114 
115 #ifdef CONFIG_MODULES
116 extern char modprobe_path[];
117 #endif
118 #ifdef CONFIG_CHR_DEV_SG
119 extern int sg_big_buff;
120 #endif
121 
122 #ifdef CONFIG_SPARC
123 #include <asm/system.h>
124 #endif
125 
126 #ifdef CONFIG_SPARC64
127 extern int sysctl_tsb_ratio;
128 #endif
129 
130 #ifdef __hppa__
131 extern int pwrsw_enabled;
132 extern int unaligned_enabled;
133 #endif
134 
135 #ifdef CONFIG_S390
136 #ifdef CONFIG_MATHEMU
137 extern int sysctl_ieee_emulation_warnings;
138 #endif
139 extern int sysctl_userprocess_debug;
140 extern int spin_retry;
141 #endif
142 
143 #ifdef CONFIG_BSD_PROCESS_ACCT
144 extern int acct_parm[];
145 #endif
146 
147 #ifdef CONFIG_IA64
148 extern int no_unaligned_warning;
149 extern int unaligned_dump_stack;
150 #endif
151 
152 #ifdef CONFIG_RT_MUTEXES
153 extern int max_lock_depth;
154 #endif
155 
156 #ifdef CONFIG_PROC_SYSCTL
157 static int proc_do_cad_pid(struct ctl_table *table, int write, struct file *filp,
158 		  void __user *buffer, size_t *lenp, loff_t *ppos);
159 static int proc_taint(struct ctl_table *table, int write, struct file *filp,
160 			       void __user *buffer, size_t *lenp, loff_t *ppos);
161 #endif
162 
163 static struct ctl_table root_table[];
164 static struct ctl_table_root sysctl_table_root;
165 static struct ctl_table_header root_table_header = {
166 	.count = 1,
167 	.ctl_table = root_table,
168 	.ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),
169 	.root = &sysctl_table_root,
170 	.set = &sysctl_table_root.default_set,
171 };
172 static struct ctl_table_root sysctl_table_root = {
173 	.root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
174 	.default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
175 };
176 
177 static struct ctl_table kern_table[];
178 static struct ctl_table vm_table[];
179 static struct ctl_table fs_table[];
180 static struct ctl_table debug_table[];
181 static struct ctl_table dev_table[];
182 extern struct ctl_table random_table[];
183 #ifdef CONFIG_INOTIFY_USER
184 extern struct ctl_table inotify_table[];
185 #endif
186 #ifdef CONFIG_EPOLL
187 extern struct ctl_table epoll_table[];
188 #endif
189 
190 #ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
191 int sysctl_legacy_va_layout;
192 #endif
193 
194 extern int prove_locking;
195 extern int lock_stat;
196 
197 /* The default sysctl tables: */
198 
199 static struct ctl_table root_table[] = {
200 	{
201 		.ctl_name	= CTL_KERN,
202 		.procname	= "kernel",
203 		.mode		= 0555,
204 		.child		= kern_table,
205 	},
206 	{
207 		.ctl_name	= CTL_VM,
208 		.procname	= "vm",
209 		.mode		= 0555,
210 		.child		= vm_table,
211 	},
212 	{
213 		.ctl_name	= CTL_FS,
214 		.procname	= "fs",
215 		.mode		= 0555,
216 		.child		= fs_table,
217 	},
218 	{
219 		.ctl_name	= CTL_DEBUG,
220 		.procname	= "debug",
221 		.mode		= 0555,
222 		.child		= debug_table,
223 	},
224 	{
225 		.ctl_name	= CTL_DEV,
226 		.procname	= "dev",
227 		.mode		= 0555,
228 		.child		= dev_table,
229 	},
230 /*
231  * NOTE: do not add new entries to this table unless you have read
232  * Documentation/sysctl/ctl_unnumbered.txt
233  */
234 	{ .ctl_name = 0 }
235 };
236 
237 #ifdef CONFIG_SCHED_DEBUG
238 static int min_sched_granularity_ns = 100000;		/* 100 usecs */
239 static int max_sched_granularity_ns = NSEC_PER_SEC;	/* 1 second */
240 static int min_wakeup_granularity_ns;			/* 0 usecs */
241 static int max_wakeup_granularity_ns = NSEC_PER_SEC;	/* 1 second */
242 #endif
243 
244 static struct ctl_table kern_table[] = {
245 #ifdef CONFIG_SCHED_DEBUG
246 	{
247 		.ctl_name	= CTL_UNNUMBERED,
248 		.procname	= "sched_min_granularity_ns",
249 		.data		= &sysctl_sched_min_granularity,
250 		.maxlen		= sizeof(unsigned int),
251 		.mode		= 0644,
252 		.proc_handler	= &sched_nr_latency_handler,
253 		.strategy	= &sysctl_intvec,
254 		.extra1		= &min_sched_granularity_ns,
255 		.extra2		= &max_sched_granularity_ns,
256 	},
257 	{
258 		.ctl_name	= CTL_UNNUMBERED,
259 		.procname	= "sched_latency_ns",
260 		.data		= &sysctl_sched_latency,
261 		.maxlen		= sizeof(unsigned int),
262 		.mode		= 0644,
263 		.proc_handler	= &sched_nr_latency_handler,
264 		.strategy	= &sysctl_intvec,
265 		.extra1		= &min_sched_granularity_ns,
266 		.extra2		= &max_sched_granularity_ns,
267 	},
268 	{
269 		.ctl_name	= CTL_UNNUMBERED,
270 		.procname	= "sched_wakeup_granularity_ns",
271 		.data		= &sysctl_sched_wakeup_granularity,
272 		.maxlen		= sizeof(unsigned int),
273 		.mode		= 0644,
274 		.proc_handler	= &proc_dointvec_minmax,
275 		.strategy	= &sysctl_intvec,
276 		.extra1		= &min_wakeup_granularity_ns,
277 		.extra2		= &max_wakeup_granularity_ns,
278 	},
279 	{
280 		.ctl_name	= CTL_UNNUMBERED,
281 		.procname	= "sched_shares_ratelimit",
282 		.data		= &sysctl_sched_shares_ratelimit,
283 		.maxlen		= sizeof(unsigned int),
284 		.mode		= 0644,
285 		.proc_handler	= &proc_dointvec,
286 	},
287 	{
288 		.ctl_name	= CTL_UNNUMBERED,
289 		.procname	= "sched_shares_thresh",
290 		.data		= &sysctl_sched_shares_thresh,
291 		.maxlen		= sizeof(unsigned int),
292 		.mode		= 0644,
293 		.proc_handler	= &proc_dointvec_minmax,
294 		.strategy	= &sysctl_intvec,
295 		.extra1		= &zero,
296 	},
297 	{
298 		.ctl_name	= CTL_UNNUMBERED,
299 		.procname	= "sched_child_runs_first",
300 		.data		= &sysctl_sched_child_runs_first,
301 		.maxlen		= sizeof(unsigned int),
302 		.mode		= 0644,
303 		.proc_handler	= &proc_dointvec,
304 	},
305 	{
306 		.ctl_name	= CTL_UNNUMBERED,
307 		.procname	= "sched_features",
308 		.data		= &sysctl_sched_features,
309 		.maxlen		= sizeof(unsigned int),
310 		.mode		= 0644,
311 		.proc_handler	= &proc_dointvec,
312 	},
313 	{
314 		.ctl_name	= CTL_UNNUMBERED,
315 		.procname	= "sched_migration_cost",
316 		.data		= &sysctl_sched_migration_cost,
317 		.maxlen		= sizeof(unsigned int),
318 		.mode		= 0644,
319 		.proc_handler	= &proc_dointvec,
320 	},
321 	{
322 		.ctl_name	= CTL_UNNUMBERED,
323 		.procname	= "sched_nr_migrate",
324 		.data		= &sysctl_sched_nr_migrate,
325 		.maxlen		= sizeof(unsigned int),
326 		.mode		= 0644,
327 		.proc_handler	= &proc_dointvec,
328 	},
329 #endif
330 	{
331 		.ctl_name	= CTL_UNNUMBERED,
332 		.procname	= "sched_rt_period_us",
333 		.data		= &sysctl_sched_rt_period,
334 		.maxlen		= sizeof(unsigned int),
335 		.mode		= 0644,
336 		.proc_handler	= &sched_rt_handler,
337 	},
338 	{
339 		.ctl_name	= CTL_UNNUMBERED,
340 		.procname	= "sched_rt_runtime_us",
341 		.data		= &sysctl_sched_rt_runtime,
342 		.maxlen		= sizeof(int),
343 		.mode		= 0644,
344 		.proc_handler	= &sched_rt_handler,
345 	},
346 	{
347 		.ctl_name	= CTL_UNNUMBERED,
348 		.procname	= "sched_compat_yield",
349 		.data		= &sysctl_sched_compat_yield,
350 		.maxlen		= sizeof(unsigned int),
351 		.mode		= 0644,
352 		.proc_handler	= &proc_dointvec,
353 	},
354 #ifdef CONFIG_PROVE_LOCKING
355 	{
356 		.ctl_name	= CTL_UNNUMBERED,
357 		.procname	= "prove_locking",
358 		.data		= &prove_locking,
359 		.maxlen		= sizeof(int),
360 		.mode		= 0644,
361 		.proc_handler	= &proc_dointvec,
362 	},
363 #endif
364 #ifdef CONFIG_LOCK_STAT
365 	{
366 		.ctl_name	= CTL_UNNUMBERED,
367 		.procname	= "lock_stat",
368 		.data		= &lock_stat,
369 		.maxlen		= sizeof(int),
370 		.mode		= 0644,
371 		.proc_handler	= &proc_dointvec,
372 	},
373 #endif
374 	{
375 		.ctl_name	= KERN_PANIC,
376 		.procname	= "panic",
377 		.data		= &panic_timeout,
378 		.maxlen		= sizeof(int),
379 		.mode		= 0644,
380 		.proc_handler	= &proc_dointvec,
381 	},
382 	{
383 		.ctl_name	= KERN_CORE_USES_PID,
384 		.procname	= "core_uses_pid",
385 		.data		= &core_uses_pid,
386 		.maxlen		= sizeof(int),
387 		.mode		= 0644,
388 		.proc_handler	= &proc_dointvec,
389 	},
390 	{
391 		.ctl_name	= KERN_CORE_PATTERN,
392 		.procname	= "core_pattern",
393 		.data		= core_pattern,
394 		.maxlen		= CORENAME_MAX_SIZE,
395 		.mode		= 0644,
396 		.proc_handler	= &proc_dostring,
397 		.strategy	= &sysctl_string,
398 	},
399 #ifdef CONFIG_PROC_SYSCTL
400 	{
401 		.procname	= "tainted",
402 		.maxlen 	= sizeof(long),
403 		.mode		= 0644,
404 		.proc_handler	= &proc_taint,
405 	},
406 #endif
407 #ifdef CONFIG_LATENCYTOP
408 	{
409 		.procname	= "latencytop",
410 		.data		= &latencytop_enabled,
411 		.maxlen		= sizeof(int),
412 		.mode		= 0644,
413 		.proc_handler	= &proc_dointvec,
414 	},
415 #endif
416 #ifdef CONFIG_BLK_DEV_INITRD
417 	{
418 		.ctl_name	= KERN_REALROOTDEV,
419 		.procname	= "real-root-dev",
420 		.data		= &real_root_dev,
421 		.maxlen		= sizeof(int),
422 		.mode		= 0644,
423 		.proc_handler	= &proc_dointvec,
424 	},
425 #endif
426 	{
427 		.ctl_name	= CTL_UNNUMBERED,
428 		.procname	= "print-fatal-signals",
429 		.data		= &print_fatal_signals,
430 		.maxlen		= sizeof(int),
431 		.mode		= 0644,
432 		.proc_handler	= &proc_dointvec,
433 	},
434 #ifdef CONFIG_SPARC
435 	{
436 		.ctl_name	= KERN_SPARC_REBOOT,
437 		.procname	= "reboot-cmd",
438 		.data		= reboot_command,
439 		.maxlen		= 256,
440 		.mode		= 0644,
441 		.proc_handler	= &proc_dostring,
442 		.strategy	= &sysctl_string,
443 	},
444 	{
445 		.ctl_name	= KERN_SPARC_STOP_A,
446 		.procname	= "stop-a",
447 		.data		= &stop_a_enabled,
448 		.maxlen		= sizeof (int),
449 		.mode		= 0644,
450 		.proc_handler	= &proc_dointvec,
451 	},
452 	{
453 		.ctl_name	= KERN_SPARC_SCONS_PWROFF,
454 		.procname	= "scons-poweroff",
455 		.data		= &scons_pwroff,
456 		.maxlen		= sizeof (int),
457 		.mode		= 0644,
458 		.proc_handler	= &proc_dointvec,
459 	},
460 #endif
461 #ifdef CONFIG_SPARC64
462 	{
463 		.ctl_name	= CTL_UNNUMBERED,
464 		.procname	= "tsb-ratio",
465 		.data		= &sysctl_tsb_ratio,
466 		.maxlen		= sizeof (int),
467 		.mode		= 0644,
468 		.proc_handler	= &proc_dointvec,
469 	},
470 #endif
471 #ifdef __hppa__
472 	{
473 		.ctl_name	= KERN_HPPA_PWRSW,
474 		.procname	= "soft-power",
475 		.data		= &pwrsw_enabled,
476 		.maxlen		= sizeof (int),
477 	 	.mode		= 0644,
478 		.proc_handler	= &proc_dointvec,
479 	},
480 	{
481 		.ctl_name	= KERN_HPPA_UNALIGNED,
482 		.procname	= "unaligned-trap",
483 		.data		= &unaligned_enabled,
484 		.maxlen		= sizeof (int),
485 		.mode		= 0644,
486 		.proc_handler	= &proc_dointvec,
487 	},
488 #endif
489 	{
490 		.ctl_name	= KERN_CTLALTDEL,
491 		.procname	= "ctrl-alt-del",
492 		.data		= &C_A_D,
493 		.maxlen		= sizeof(int),
494 		.mode		= 0644,
495 		.proc_handler	= &proc_dointvec,
496 	},
497 #ifdef CONFIG_FUNCTION_TRACER
498 	{
499 		.ctl_name	= CTL_UNNUMBERED,
500 		.procname	= "ftrace_enabled",
501 		.data		= &ftrace_enabled,
502 		.maxlen		= sizeof(int),
503 		.mode		= 0644,
504 		.proc_handler	= &ftrace_enable_sysctl,
505 	},
506 #endif
507 #ifdef CONFIG_STACK_TRACER
508 	{
509 		.ctl_name	= CTL_UNNUMBERED,
510 		.procname	= "stack_tracer_enabled",
511 		.data		= &stack_tracer_enabled,
512 		.maxlen		= sizeof(int),
513 		.mode		= 0644,
514 		.proc_handler	= &stack_trace_sysctl,
515 	},
516 #endif
517 #ifdef CONFIG_TRACING
518 	{
519 		.ctl_name	= CTL_UNNUMBERED,
520 		.procname	= "ftrace_dump_on_oops",
521 		.data		= &ftrace_dump_on_oops,
522 		.maxlen		= sizeof(int),
523 		.mode		= 0644,
524 		.proc_handler	= &proc_dointvec,
525 	},
526 #endif
527 #ifdef CONFIG_MODULES
528 	{
529 		.ctl_name	= KERN_MODPROBE,
530 		.procname	= "modprobe",
531 		.data		= &modprobe_path,
532 		.maxlen		= KMOD_PATH_LEN,
533 		.mode		= 0644,
534 		.proc_handler	= &proc_dostring,
535 		.strategy	= &sysctl_string,
536 	},
537 #endif
538 #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
539 	{
540 		.ctl_name	= KERN_HOTPLUG,
541 		.procname	= "hotplug",
542 		.data		= &uevent_helper,
543 		.maxlen		= UEVENT_HELPER_PATH_LEN,
544 		.mode		= 0644,
545 		.proc_handler	= &proc_dostring,
546 		.strategy	= &sysctl_string,
547 	},
548 #endif
549 #ifdef CONFIG_CHR_DEV_SG
550 	{
551 		.ctl_name	= KERN_SG_BIG_BUFF,
552 		.procname	= "sg-big-buff",
553 		.data		= &sg_big_buff,
554 		.maxlen		= sizeof (int),
555 		.mode		= 0444,
556 		.proc_handler	= &proc_dointvec,
557 	},
558 #endif
559 #ifdef CONFIG_BSD_PROCESS_ACCT
560 	{
561 		.ctl_name	= KERN_ACCT,
562 		.procname	= "acct",
563 		.data		= &acct_parm,
564 		.maxlen		= 3*sizeof(int),
565 		.mode		= 0644,
566 		.proc_handler	= &proc_dointvec,
567 	},
568 #endif
569 #ifdef CONFIG_MAGIC_SYSRQ
570 	{
571 		.ctl_name	= KERN_SYSRQ,
572 		.procname	= "sysrq",
573 		.data		= &__sysrq_enabled,
574 		.maxlen		= sizeof (int),
575 		.mode		= 0644,
576 		.proc_handler	= &proc_dointvec,
577 	},
578 #endif
579 #ifdef CONFIG_PROC_SYSCTL
580 	{
581 		.procname	= "cad_pid",
582 		.data		= NULL,
583 		.maxlen		= sizeof (int),
584 		.mode		= 0600,
585 		.proc_handler	= &proc_do_cad_pid,
586 	},
587 #endif
588 	{
589 		.ctl_name	= KERN_MAX_THREADS,
590 		.procname	= "threads-max",
591 		.data		= &max_threads,
592 		.maxlen		= sizeof(int),
593 		.mode		= 0644,
594 		.proc_handler	= &proc_dointvec,
595 	},
596 	{
597 		.ctl_name	= KERN_RANDOM,
598 		.procname	= "random",
599 		.mode		= 0555,
600 		.child		= random_table,
601 	},
602 	{
603 		.ctl_name	= KERN_OVERFLOWUID,
604 		.procname	= "overflowuid",
605 		.data		= &overflowuid,
606 		.maxlen		= sizeof(int),
607 		.mode		= 0644,
608 		.proc_handler	= &proc_dointvec_minmax,
609 		.strategy	= &sysctl_intvec,
610 		.extra1		= &minolduid,
611 		.extra2		= &maxolduid,
612 	},
613 	{
614 		.ctl_name	= KERN_OVERFLOWGID,
615 		.procname	= "overflowgid",
616 		.data		= &overflowgid,
617 		.maxlen		= sizeof(int),
618 		.mode		= 0644,
619 		.proc_handler	= &proc_dointvec_minmax,
620 		.strategy	= &sysctl_intvec,
621 		.extra1		= &minolduid,
622 		.extra2		= &maxolduid,
623 	},
624 #ifdef CONFIG_S390
625 #ifdef CONFIG_MATHEMU
626 	{
627 		.ctl_name	= KERN_IEEE_EMULATION_WARNINGS,
628 		.procname	= "ieee_emulation_warnings",
629 		.data		= &sysctl_ieee_emulation_warnings,
630 		.maxlen		= sizeof(int),
631 		.mode		= 0644,
632 		.proc_handler	= &proc_dointvec,
633 	},
634 #endif
635 	{
636 		.ctl_name	= KERN_S390_USER_DEBUG_LOGGING,
637 		.procname	= "userprocess_debug",
638 		.data		= &sysctl_userprocess_debug,
639 		.maxlen		= sizeof(int),
640 		.mode		= 0644,
641 		.proc_handler	= &proc_dointvec,
642 	},
643 #endif
644 	{
645 		.ctl_name	= KERN_PIDMAX,
646 		.procname	= "pid_max",
647 		.data		= &pid_max,
648 		.maxlen		= sizeof (int),
649 		.mode		= 0644,
650 		.proc_handler	= &proc_dointvec_minmax,
651 		.strategy	= sysctl_intvec,
652 		.extra1		= &pid_max_min,
653 		.extra2		= &pid_max_max,
654 	},
655 	{
656 		.ctl_name	= KERN_PANIC_ON_OOPS,
657 		.procname	= "panic_on_oops",
658 		.data		= &panic_on_oops,
659 		.maxlen		= sizeof(int),
660 		.mode		= 0644,
661 		.proc_handler	= &proc_dointvec,
662 	},
663 #if defined CONFIG_PRINTK
664 	{
665 		.ctl_name	= KERN_PRINTK,
666 		.procname	= "printk",
667 		.data		= &console_loglevel,
668 		.maxlen		= 4*sizeof(int),
669 		.mode		= 0644,
670 		.proc_handler	= &proc_dointvec,
671 	},
672 	{
673 		.ctl_name	= KERN_PRINTK_RATELIMIT,
674 		.procname	= "printk_ratelimit",
675 		.data		= &printk_ratelimit_state.interval,
676 		.maxlen		= sizeof(int),
677 		.mode		= 0644,
678 		.proc_handler	= &proc_dointvec_jiffies,
679 		.strategy	= &sysctl_jiffies,
680 	},
681 	{
682 		.ctl_name	= KERN_PRINTK_RATELIMIT_BURST,
683 		.procname	= "printk_ratelimit_burst",
684 		.data		= &printk_ratelimit_state.burst,
685 		.maxlen		= sizeof(int),
686 		.mode		= 0644,
687 		.proc_handler	= &proc_dointvec,
688 	},
689 #endif
690 	{
691 		.ctl_name	= KERN_NGROUPS_MAX,
692 		.procname	= "ngroups_max",
693 		.data		= &ngroups_max,
694 		.maxlen		= sizeof (int),
695 		.mode		= 0444,
696 		.proc_handler	= &proc_dointvec,
697 	},
698 #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86)
699 	{
700 		.ctl_name       = KERN_UNKNOWN_NMI_PANIC,
701 		.procname       = "unknown_nmi_panic",
702 		.data           = &unknown_nmi_panic,
703 		.maxlen         = sizeof (int),
704 		.mode           = 0644,
705 		.proc_handler   = &proc_dointvec,
706 	},
707 	{
708 		.procname       = "nmi_watchdog",
709 		.data           = &nmi_watchdog_enabled,
710 		.maxlen         = sizeof (int),
711 		.mode           = 0644,
712 		.proc_handler   = &proc_nmi_enabled,
713 	},
714 #endif
715 #if defined(CONFIG_X86)
716 	{
717 		.ctl_name	= KERN_PANIC_ON_NMI,
718 		.procname	= "panic_on_unrecovered_nmi",
719 		.data		= &panic_on_unrecovered_nmi,
720 		.maxlen		= sizeof(int),
721 		.mode		= 0644,
722 		.proc_handler	= &proc_dointvec,
723 	},
724 	{
725 		.ctl_name	= KERN_BOOTLOADER_TYPE,
726 		.procname	= "bootloader_type",
727 		.data		= &bootloader_type,
728 		.maxlen		= sizeof (int),
729 		.mode		= 0444,
730 		.proc_handler	= &proc_dointvec,
731 	},
732 	{
733 		.ctl_name	= CTL_UNNUMBERED,
734 		.procname	= "kstack_depth_to_print",
735 		.data		= &kstack_depth_to_print,
736 		.maxlen		= sizeof(int),
737 		.mode		= 0644,
738 		.proc_handler	= &proc_dointvec,
739 	},
740 	{
741 		.ctl_name	= CTL_UNNUMBERED,
742 		.procname	= "io_delay_type",
743 		.data		= &io_delay_type,
744 		.maxlen		= sizeof(int),
745 		.mode		= 0644,
746 		.proc_handler	= &proc_dointvec,
747 	},
748 #endif
749 #if defined(CONFIG_MMU)
750 	{
751 		.ctl_name	= KERN_RANDOMIZE,
752 		.procname	= "randomize_va_space",
753 		.data		= &randomize_va_space,
754 		.maxlen		= sizeof(int),
755 		.mode		= 0644,
756 		.proc_handler	= &proc_dointvec,
757 	},
758 #endif
759 #if defined(CONFIG_S390) && defined(CONFIG_SMP)
760 	{
761 		.ctl_name	= KERN_SPIN_RETRY,
762 		.procname	= "spin_retry",
763 		.data		= &spin_retry,
764 		.maxlen		= sizeof (int),
765 		.mode		= 0644,
766 		.proc_handler	= &proc_dointvec,
767 	},
768 #endif
769 #if	defined(CONFIG_ACPI_SLEEP) && defined(CONFIG_X86)
770 	{
771 		.procname	= "acpi_video_flags",
772 		.data		= &acpi_realmode_flags,
773 		.maxlen		= sizeof (unsigned long),
774 		.mode		= 0644,
775 		.proc_handler	= &proc_doulongvec_minmax,
776 	},
777 #endif
778 #ifdef CONFIG_IA64
779 	{
780 		.ctl_name	= KERN_IA64_UNALIGNED,
781 		.procname	= "ignore-unaligned-usertrap",
782 		.data		= &no_unaligned_warning,
783 		.maxlen		= sizeof (int),
784 	 	.mode		= 0644,
785 		.proc_handler	= &proc_dointvec,
786 	},
787 	{
788 		.ctl_name	= CTL_UNNUMBERED,
789 		.procname	= "unaligned-dump-stack",
790 		.data		= &unaligned_dump_stack,
791 		.maxlen		= sizeof (int),
792 		.mode		= 0644,
793 		.proc_handler	= &proc_dointvec,
794 	},
795 #endif
796 #ifdef CONFIG_DETECT_SOFTLOCKUP
797 	{
798 		.ctl_name	= CTL_UNNUMBERED,
799 		.procname	= "softlockup_panic",
800 		.data		= &softlockup_panic,
801 		.maxlen		= sizeof(int),
802 		.mode		= 0644,
803 		.proc_handler	= &proc_dointvec_minmax,
804 		.strategy	= &sysctl_intvec,
805 		.extra1		= &zero,
806 		.extra2		= &one,
807 	},
808 	{
809 		.ctl_name	= CTL_UNNUMBERED,
810 		.procname	= "softlockup_thresh",
811 		.data		= &softlockup_thresh,
812 		.maxlen		= sizeof(int),
813 		.mode		= 0644,
814 		.proc_handler	= &proc_dosoftlockup_thresh,
815 		.strategy	= &sysctl_intvec,
816 		.extra1		= &neg_one,
817 		.extra2		= &sixty,
818 	},
819 	{
820 		.ctl_name	= CTL_UNNUMBERED,
821 		.procname	= "hung_task_check_count",
822 		.data		= &sysctl_hung_task_check_count,
823 		.maxlen		= sizeof(unsigned long),
824 		.mode		= 0644,
825 		.proc_handler	= &proc_doulongvec_minmax,
826 		.strategy	= &sysctl_intvec,
827 	},
828 	{
829 		.ctl_name	= CTL_UNNUMBERED,
830 		.procname	= "hung_task_timeout_secs",
831 		.data		= &sysctl_hung_task_timeout_secs,
832 		.maxlen		= sizeof(unsigned long),
833 		.mode		= 0644,
834 		.proc_handler	= &proc_doulongvec_minmax,
835 		.strategy	= &sysctl_intvec,
836 	},
837 	{
838 		.ctl_name	= CTL_UNNUMBERED,
839 		.procname	= "hung_task_warnings",
840 		.data		= &sysctl_hung_task_warnings,
841 		.maxlen		= sizeof(unsigned long),
842 		.mode		= 0644,
843 		.proc_handler	= &proc_doulongvec_minmax,
844 		.strategy	= &sysctl_intvec,
845 	},
846 #endif
847 #ifdef CONFIG_COMPAT
848 	{
849 		.ctl_name	= KERN_COMPAT_LOG,
850 		.procname	= "compat-log",
851 		.data		= &compat_log,
852 		.maxlen		= sizeof (int),
853 	 	.mode		= 0644,
854 		.proc_handler	= &proc_dointvec,
855 	},
856 #endif
857 #ifdef CONFIG_RT_MUTEXES
858 	{
859 		.ctl_name	= KERN_MAX_LOCK_DEPTH,
860 		.procname	= "max_lock_depth",
861 		.data		= &max_lock_depth,
862 		.maxlen		= sizeof(int),
863 		.mode		= 0644,
864 		.proc_handler	= &proc_dointvec,
865 	},
866 #endif
867 	{
868 		.ctl_name	= CTL_UNNUMBERED,
869 		.procname	= "poweroff_cmd",
870 		.data		= &poweroff_cmd,
871 		.maxlen		= POWEROFF_CMD_PATH_LEN,
872 		.mode		= 0644,
873 		.proc_handler	= &proc_dostring,
874 		.strategy	= &sysctl_string,
875 	},
876 #ifdef CONFIG_KEYS
877 	{
878 		.ctl_name	= CTL_UNNUMBERED,
879 		.procname	= "keys",
880 		.mode		= 0555,
881 		.child		= key_sysctls,
882 	},
883 #endif
884 #ifdef CONFIG_RCU_TORTURE_TEST
885 	{
886 		.ctl_name       = CTL_UNNUMBERED,
887 		.procname       = "rcutorture_runnable",
888 		.data           = &rcutorture_runnable,
889 		.maxlen         = sizeof(int),
890 		.mode           = 0644,
891 		.proc_handler   = &proc_dointvec,
892 	},
893 #endif
894 #ifdef CONFIG_UNEVICTABLE_LRU
895 	{
896 		.ctl_name	= CTL_UNNUMBERED,
897 		.procname	= "scan_unevictable_pages",
898 		.data		= &scan_unevictable_pages,
899 		.maxlen		= sizeof(scan_unevictable_pages),
900 		.mode		= 0644,
901 		.proc_handler	= &scan_unevictable_handler,
902 	},
903 #endif
904 /*
905  * NOTE: do not add new entries to this table unless you have read
906  * Documentation/sysctl/ctl_unnumbered.txt
907  */
908 	{ .ctl_name = 0 }
909 };
910 
911 static struct ctl_table vm_table[] = {
912 	{
913 		.ctl_name	= VM_OVERCOMMIT_MEMORY,
914 		.procname	= "overcommit_memory",
915 		.data		= &sysctl_overcommit_memory,
916 		.maxlen		= sizeof(sysctl_overcommit_memory),
917 		.mode		= 0644,
918 		.proc_handler	= &proc_dointvec,
919 	},
920 	{
921 		.ctl_name	= VM_PANIC_ON_OOM,
922 		.procname	= "panic_on_oom",
923 		.data		= &sysctl_panic_on_oom,
924 		.maxlen		= sizeof(sysctl_panic_on_oom),
925 		.mode		= 0644,
926 		.proc_handler	= &proc_dointvec,
927 	},
928 	{
929 		.ctl_name	= CTL_UNNUMBERED,
930 		.procname	= "oom_kill_allocating_task",
931 		.data		= &sysctl_oom_kill_allocating_task,
932 		.maxlen		= sizeof(sysctl_oom_kill_allocating_task),
933 		.mode		= 0644,
934 		.proc_handler	= &proc_dointvec,
935 	},
936 	{
937 		.ctl_name	= CTL_UNNUMBERED,
938 		.procname	= "oom_dump_tasks",
939 		.data		= &sysctl_oom_dump_tasks,
940 		.maxlen		= sizeof(sysctl_oom_dump_tasks),
941 		.mode		= 0644,
942 		.proc_handler	= &proc_dointvec,
943 	},
944 	{
945 		.ctl_name	= VM_OVERCOMMIT_RATIO,
946 		.procname	= "overcommit_ratio",
947 		.data		= &sysctl_overcommit_ratio,
948 		.maxlen		= sizeof(sysctl_overcommit_ratio),
949 		.mode		= 0644,
950 		.proc_handler	= &proc_dointvec,
951 	},
952 	{
953 		.ctl_name	= VM_PAGE_CLUSTER,
954 		.procname	= "page-cluster",
955 		.data		= &page_cluster,
956 		.maxlen		= sizeof(int),
957 		.mode		= 0644,
958 		.proc_handler	= &proc_dointvec,
959 	},
960 	{
961 		.ctl_name	= VM_DIRTY_BACKGROUND,
962 		.procname	= "dirty_background_ratio",
963 		.data		= &dirty_background_ratio,
964 		.maxlen		= sizeof(dirty_background_ratio),
965 		.mode		= 0644,
966 		.proc_handler	= &dirty_background_ratio_handler,
967 		.strategy	= &sysctl_intvec,
968 		.extra1		= &zero,
969 		.extra2		= &one_hundred,
970 	},
971 	{
972 		.ctl_name	= CTL_UNNUMBERED,
973 		.procname	= "dirty_background_bytes",
974 		.data		= &dirty_background_bytes,
975 		.maxlen		= sizeof(dirty_background_bytes),
976 		.mode		= 0644,
977 		.proc_handler	= &dirty_background_bytes_handler,
978 		.strategy	= &sysctl_intvec,
979 		.extra1		= &one_ul,
980 	},
981 	{
982 		.ctl_name	= VM_DIRTY_RATIO,
983 		.procname	= "dirty_ratio",
984 		.data		= &vm_dirty_ratio,
985 		.maxlen		= sizeof(vm_dirty_ratio),
986 		.mode		= 0644,
987 		.proc_handler	= &dirty_ratio_handler,
988 		.strategy	= &sysctl_intvec,
989 		.extra1		= &zero,
990 		.extra2		= &one_hundred,
991 	},
992 	{
993 		.ctl_name	= CTL_UNNUMBERED,
994 		.procname	= "dirty_bytes",
995 		.data		= &vm_dirty_bytes,
996 		.maxlen		= sizeof(vm_dirty_bytes),
997 		.mode		= 0644,
998 		.proc_handler	= &dirty_bytes_handler,
999 		.strategy	= &sysctl_intvec,
1000 		.extra1		= &one_ul,
1001 	},
1002 	{
1003 		.procname	= "dirty_writeback_centisecs",
1004 		.data		= &dirty_writeback_interval,
1005 		.maxlen		= sizeof(dirty_writeback_interval),
1006 		.mode		= 0644,
1007 		.proc_handler	= &dirty_writeback_centisecs_handler,
1008 	},
1009 	{
1010 		.procname	= "dirty_expire_centisecs",
1011 		.data		= &dirty_expire_interval,
1012 		.maxlen		= sizeof(dirty_expire_interval),
1013 		.mode		= 0644,
1014 		.proc_handler	= &proc_dointvec_userhz_jiffies,
1015 	},
1016 	{
1017 		.ctl_name	= VM_NR_PDFLUSH_THREADS,
1018 		.procname	= "nr_pdflush_threads",
1019 		.data		= &nr_pdflush_threads,
1020 		.maxlen		= sizeof nr_pdflush_threads,
1021 		.mode		= 0444 /* read-only*/,
1022 		.proc_handler	= &proc_dointvec,
1023 	},
1024 	{
1025 		.ctl_name	= VM_SWAPPINESS,
1026 		.procname	= "swappiness",
1027 		.data		= &vm_swappiness,
1028 		.maxlen		= sizeof(vm_swappiness),
1029 		.mode		= 0644,
1030 		.proc_handler	= &proc_dointvec_minmax,
1031 		.strategy	= &sysctl_intvec,
1032 		.extra1		= &zero,
1033 		.extra2		= &one_hundred,
1034 	},
1035 #ifdef CONFIG_HUGETLB_PAGE
1036 	 {
1037 		.procname	= "nr_hugepages",
1038 		.data		= NULL,
1039 		.maxlen		= sizeof(unsigned long),
1040 		.mode		= 0644,
1041 		.proc_handler	= &hugetlb_sysctl_handler,
1042 		.extra1		= (void *)&hugetlb_zero,
1043 		.extra2		= (void *)&hugetlb_infinity,
1044 	 },
1045 	 {
1046 		.ctl_name	= VM_HUGETLB_GROUP,
1047 		.procname	= "hugetlb_shm_group",
1048 		.data		= &sysctl_hugetlb_shm_group,
1049 		.maxlen		= sizeof(gid_t),
1050 		.mode		= 0644,
1051 		.proc_handler	= &proc_dointvec,
1052 	 },
1053 	 {
1054 		.ctl_name	= CTL_UNNUMBERED,
1055 		.procname	= "hugepages_treat_as_movable",
1056 		.data		= &hugepages_treat_as_movable,
1057 		.maxlen		= sizeof(int),
1058 		.mode		= 0644,
1059 		.proc_handler	= &hugetlb_treat_movable_handler,
1060 	},
1061 	{
1062 		.ctl_name	= CTL_UNNUMBERED,
1063 		.procname	= "nr_overcommit_hugepages",
1064 		.data		= NULL,
1065 		.maxlen		= sizeof(unsigned long),
1066 		.mode		= 0644,
1067 		.proc_handler	= &hugetlb_overcommit_handler,
1068 		.extra1		= (void *)&hugetlb_zero,
1069 		.extra2		= (void *)&hugetlb_infinity,
1070 	},
1071 #endif
1072 	{
1073 		.ctl_name	= VM_LOWMEM_RESERVE_RATIO,
1074 		.procname	= "lowmem_reserve_ratio",
1075 		.data		= &sysctl_lowmem_reserve_ratio,
1076 		.maxlen		= sizeof(sysctl_lowmem_reserve_ratio),
1077 		.mode		= 0644,
1078 		.proc_handler	= &lowmem_reserve_ratio_sysctl_handler,
1079 		.strategy	= &sysctl_intvec,
1080 	},
1081 	{
1082 		.ctl_name	= VM_DROP_PAGECACHE,
1083 		.procname	= "drop_caches",
1084 		.data		= &sysctl_drop_caches,
1085 		.maxlen		= sizeof(int),
1086 		.mode		= 0644,
1087 		.proc_handler	= drop_caches_sysctl_handler,
1088 		.strategy	= &sysctl_intvec,
1089 	},
1090 	{
1091 		.ctl_name	= VM_MIN_FREE_KBYTES,
1092 		.procname	= "min_free_kbytes",
1093 		.data		= &min_free_kbytes,
1094 		.maxlen		= sizeof(min_free_kbytes),
1095 		.mode		= 0644,
1096 		.proc_handler	= &min_free_kbytes_sysctl_handler,
1097 		.strategy	= &sysctl_intvec,
1098 		.extra1		= &zero,
1099 	},
1100 	{
1101 		.ctl_name	= CTL_UNNUMBERED,
1102 		.procname	= "min_free_order_shift",
1103 		.data		= &min_free_order_shift,
1104 		.maxlen		= sizeof(min_free_order_shift),
1105 		.mode		= 0644,
1106 		.proc_handler	= &proc_dointvec
1107 	},
1108 	{
1109 		.ctl_name	= VM_PERCPU_PAGELIST_FRACTION,
1110 		.procname	= "percpu_pagelist_fraction",
1111 		.data		= &percpu_pagelist_fraction,
1112 		.maxlen		= sizeof(percpu_pagelist_fraction),
1113 		.mode		= 0644,
1114 		.proc_handler	= &percpu_pagelist_fraction_sysctl_handler,
1115 		.strategy	= &sysctl_intvec,
1116 		.extra1		= &min_percpu_pagelist_fract,
1117 	},
1118 #ifdef CONFIG_MMU
1119 	{
1120 		.ctl_name	= VM_MAX_MAP_COUNT,
1121 		.procname	= "max_map_count",
1122 		.data		= &sysctl_max_map_count,
1123 		.maxlen		= sizeof(sysctl_max_map_count),
1124 		.mode		= 0644,
1125 		.proc_handler	= &proc_dointvec
1126 	},
1127 #else
1128 	{
1129 		.ctl_name	= CTL_UNNUMBERED,
1130 		.procname	= "nr_trim_pages",
1131 		.data		= &sysctl_nr_trim_pages,
1132 		.maxlen		= sizeof(sysctl_nr_trim_pages),
1133 		.mode		= 0644,
1134 		.proc_handler	= &proc_dointvec_minmax,
1135 		.strategy	= &sysctl_intvec,
1136 		.extra1		= &zero,
1137 	},
1138 #endif
1139 	{
1140 		.ctl_name	= VM_LAPTOP_MODE,
1141 		.procname	= "laptop_mode",
1142 		.data		= &laptop_mode,
1143 		.maxlen		= sizeof(laptop_mode),
1144 		.mode		= 0644,
1145 		.proc_handler	= &proc_dointvec_jiffies,
1146 		.strategy	= &sysctl_jiffies,
1147 	},
1148 	{
1149 		.ctl_name	= VM_BLOCK_DUMP,
1150 		.procname	= "block_dump",
1151 		.data		= &block_dump,
1152 		.maxlen		= sizeof(block_dump),
1153 		.mode		= 0644,
1154 		.proc_handler	= &proc_dointvec,
1155 		.strategy	= &sysctl_intvec,
1156 		.extra1		= &zero,
1157 	},
1158 	{
1159 		.ctl_name	= VM_VFS_CACHE_PRESSURE,
1160 		.procname	= "vfs_cache_pressure",
1161 		.data		= &sysctl_vfs_cache_pressure,
1162 		.maxlen		= sizeof(sysctl_vfs_cache_pressure),
1163 		.mode		= 0644,
1164 		.proc_handler	= &proc_dointvec,
1165 		.strategy	= &sysctl_intvec,
1166 		.extra1		= &zero,
1167 	},
1168 #ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
1169 	{
1170 		.ctl_name	= VM_LEGACY_VA_LAYOUT,
1171 		.procname	= "legacy_va_layout",
1172 		.data		= &sysctl_legacy_va_layout,
1173 		.maxlen		= sizeof(sysctl_legacy_va_layout),
1174 		.mode		= 0644,
1175 		.proc_handler	= &proc_dointvec,
1176 		.strategy	= &sysctl_intvec,
1177 		.extra1		= &zero,
1178 	},
1179 #endif
1180 #ifdef CONFIG_NUMA
1181 	{
1182 		.ctl_name	= VM_ZONE_RECLAIM_MODE,
1183 		.procname	= "zone_reclaim_mode",
1184 		.data		= &zone_reclaim_mode,
1185 		.maxlen		= sizeof(zone_reclaim_mode),
1186 		.mode		= 0644,
1187 		.proc_handler	= &proc_dointvec,
1188 		.strategy	= &sysctl_intvec,
1189 		.extra1		= &zero,
1190 	},
1191 	{
1192 		.ctl_name	= VM_MIN_UNMAPPED,
1193 		.procname	= "min_unmapped_ratio",
1194 		.data		= &sysctl_min_unmapped_ratio,
1195 		.maxlen		= sizeof(sysctl_min_unmapped_ratio),
1196 		.mode		= 0644,
1197 		.proc_handler	= &sysctl_min_unmapped_ratio_sysctl_handler,
1198 		.strategy	= &sysctl_intvec,
1199 		.extra1		= &zero,
1200 		.extra2		= &one_hundred,
1201 	},
1202 	{
1203 		.ctl_name	= VM_MIN_SLAB,
1204 		.procname	= "min_slab_ratio",
1205 		.data		= &sysctl_min_slab_ratio,
1206 		.maxlen		= sizeof(sysctl_min_slab_ratio),
1207 		.mode		= 0644,
1208 		.proc_handler	= &sysctl_min_slab_ratio_sysctl_handler,
1209 		.strategy	= &sysctl_intvec,
1210 		.extra1		= &zero,
1211 		.extra2		= &one_hundred,
1212 	},
1213 #endif
1214 #ifdef CONFIG_SMP
1215 	{
1216 		.ctl_name	= CTL_UNNUMBERED,
1217 		.procname	= "stat_interval",
1218 		.data		= &sysctl_stat_interval,
1219 		.maxlen		= sizeof(sysctl_stat_interval),
1220 		.mode		= 0644,
1221 		.proc_handler	= &proc_dointvec_jiffies,
1222 		.strategy	= &sysctl_jiffies,
1223 	},
1224 #endif
1225 	{
1226 		.ctl_name	= CTL_UNNUMBERED,
1227 		.procname	= "mmap_min_addr",
1228 		.data		= &mmap_min_addr,
1229 		.maxlen         = sizeof(unsigned long),
1230 		.mode		= 0644,
1231 		.proc_handler	= &proc_doulongvec_minmax,
1232 	},
1233 #ifdef CONFIG_NUMA
1234 	{
1235 		.ctl_name	= CTL_UNNUMBERED,
1236 		.procname	= "numa_zonelist_order",
1237 		.data		= &numa_zonelist_order,
1238 		.maxlen		= NUMA_ZONELIST_ORDER_LEN,
1239 		.mode		= 0644,
1240 		.proc_handler	= &numa_zonelist_order_handler,
1241 		.strategy	= &sysctl_string,
1242 	},
1243 #endif
1244 #if (defined(CONFIG_X86_32) && !defined(CONFIG_UML))|| \
1245    (defined(CONFIG_SUPERH) && defined(CONFIG_VSYSCALL))
1246 	{
1247 		.ctl_name	= VM_VDSO_ENABLED,
1248 		.procname	= "vdso_enabled",
1249 		.data		= &vdso_enabled,
1250 		.maxlen		= sizeof(vdso_enabled),
1251 		.mode		= 0644,
1252 		.proc_handler	= &proc_dointvec,
1253 		.strategy	= &sysctl_intvec,
1254 		.extra1		= &zero,
1255 	},
1256 #endif
1257 #ifdef CONFIG_HIGHMEM
1258 	{
1259 		.ctl_name	= CTL_UNNUMBERED,
1260 		.procname	= "highmem_is_dirtyable",
1261 		.data		= &vm_highmem_is_dirtyable,
1262 		.maxlen		= sizeof(vm_highmem_is_dirtyable),
1263 		.mode		= 0644,
1264 		.proc_handler	= &proc_dointvec_minmax,
1265 		.strategy	= &sysctl_intvec,
1266 		.extra1		= &zero,
1267 		.extra2		= &one,
1268 	},
1269 #endif
1270 /*
1271  * NOTE: do not add new entries to this table unless you have read
1272  * Documentation/sysctl/ctl_unnumbered.txt
1273  */
1274 	{ .ctl_name = 0 }
1275 };
1276 
1277 #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
1278 static struct ctl_table binfmt_misc_table[] = {
1279 	{ .ctl_name = 0 }
1280 };
1281 #endif
1282 
1283 static struct ctl_table fs_table[] = {
1284 	{
1285 		.ctl_name	= FS_NRINODE,
1286 		.procname	= "inode-nr",
1287 		.data		= &inodes_stat,
1288 		.maxlen		= 2*sizeof(int),
1289 		.mode		= 0444,
1290 		.proc_handler	= &proc_dointvec,
1291 	},
1292 	{
1293 		.ctl_name	= FS_STATINODE,
1294 		.procname	= "inode-state",
1295 		.data		= &inodes_stat,
1296 		.maxlen		= 7*sizeof(int),
1297 		.mode		= 0444,
1298 		.proc_handler	= &proc_dointvec,
1299 	},
1300 	{
1301 		.procname	= "file-nr",
1302 		.data		= &files_stat,
1303 		.maxlen		= 3*sizeof(int),
1304 		.mode		= 0444,
1305 		.proc_handler	= &proc_nr_files,
1306 	},
1307 	{
1308 		.ctl_name	= FS_MAXFILE,
1309 		.procname	= "file-max",
1310 		.data		= &files_stat.max_files,
1311 		.maxlen		= sizeof(int),
1312 		.mode		= 0644,
1313 		.proc_handler	= &proc_dointvec,
1314 	},
1315 	{
1316 		.ctl_name	= CTL_UNNUMBERED,
1317 		.procname	= "nr_open",
1318 		.data		= &sysctl_nr_open,
1319 		.maxlen		= sizeof(int),
1320 		.mode		= 0644,
1321 		.proc_handler	= &proc_dointvec_minmax,
1322 		.extra1		= &sysctl_nr_open_min,
1323 		.extra2		= &sysctl_nr_open_max,
1324 	},
1325 	{
1326 		.ctl_name	= FS_DENTRY,
1327 		.procname	= "dentry-state",
1328 		.data		= &dentry_stat,
1329 		.maxlen		= 6*sizeof(int),
1330 		.mode		= 0444,
1331 		.proc_handler	= &proc_dointvec,
1332 	},
1333 	{
1334 		.ctl_name	= FS_OVERFLOWUID,
1335 		.procname	= "overflowuid",
1336 		.data		= &fs_overflowuid,
1337 		.maxlen		= sizeof(int),
1338 		.mode		= 0644,
1339 		.proc_handler	= &proc_dointvec_minmax,
1340 		.strategy	= &sysctl_intvec,
1341 		.extra1		= &minolduid,
1342 		.extra2		= &maxolduid,
1343 	},
1344 	{
1345 		.ctl_name	= FS_OVERFLOWGID,
1346 		.procname	= "overflowgid",
1347 		.data		= &fs_overflowgid,
1348 		.maxlen		= sizeof(int),
1349 		.mode		= 0644,
1350 		.proc_handler	= &proc_dointvec_minmax,
1351 		.strategy	= &sysctl_intvec,
1352 		.extra1		= &minolduid,
1353 		.extra2		= &maxolduid,
1354 	},
1355 #ifdef CONFIG_FILE_LOCKING
1356 	{
1357 		.ctl_name	= FS_LEASES,
1358 		.procname	= "leases-enable",
1359 		.data		= &leases_enable,
1360 		.maxlen		= sizeof(int),
1361 		.mode		= 0644,
1362 		.proc_handler	= &proc_dointvec,
1363 	},
1364 #endif
1365 #ifdef CONFIG_DNOTIFY
1366 	{
1367 		.ctl_name	= FS_DIR_NOTIFY,
1368 		.procname	= "dir-notify-enable",
1369 		.data		= &dir_notify_enable,
1370 		.maxlen		= sizeof(int),
1371 		.mode		= 0644,
1372 		.proc_handler	= &proc_dointvec,
1373 	},
1374 #endif
1375 #ifdef CONFIG_MMU
1376 #ifdef CONFIG_FILE_LOCKING
1377 	{
1378 		.ctl_name	= FS_LEASE_TIME,
1379 		.procname	= "lease-break-time",
1380 		.data		= &lease_break_time,
1381 		.maxlen		= sizeof(int),
1382 		.mode		= 0644,
1383 		.proc_handler	= &proc_dointvec_minmax,
1384 		.strategy	= &sysctl_intvec,
1385 		.extra1		= &zero,
1386 		.extra2		= &two,
1387 	},
1388 #endif
1389 #ifdef CONFIG_AIO
1390 	{
1391 		.procname	= "aio-nr",
1392 		.data		= &aio_nr,
1393 		.maxlen		= sizeof(aio_nr),
1394 		.mode		= 0444,
1395 		.proc_handler	= &proc_doulongvec_minmax,
1396 	},
1397 	{
1398 		.procname	= "aio-max-nr",
1399 		.data		= &aio_max_nr,
1400 		.maxlen		= sizeof(aio_max_nr),
1401 		.mode		= 0644,
1402 		.proc_handler	= &proc_doulongvec_minmax,
1403 	},
1404 #endif /* CONFIG_AIO */
1405 #ifdef CONFIG_INOTIFY_USER
1406 	{
1407 		.ctl_name	= FS_INOTIFY,
1408 		.procname	= "inotify",
1409 		.mode		= 0555,
1410 		.child		= inotify_table,
1411 	},
1412 #endif
1413 #ifdef CONFIG_EPOLL
1414 	{
1415 		.procname	= "epoll",
1416 		.mode		= 0555,
1417 		.child		= epoll_table,
1418 	},
1419 #endif
1420 #endif
1421 	{
1422 		.ctl_name	= KERN_SETUID_DUMPABLE,
1423 		.procname	= "suid_dumpable",
1424 		.data		= &suid_dumpable,
1425 		.maxlen		= sizeof(int),
1426 		.mode		= 0644,
1427 		.proc_handler	= &proc_dointvec,
1428 	},
1429 #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
1430 	{
1431 		.ctl_name	= CTL_UNNUMBERED,
1432 		.procname	= "binfmt_misc",
1433 		.mode		= 0555,
1434 		.child		= binfmt_misc_table,
1435 	},
1436 #endif
1437 /*
1438  * NOTE: do not add new entries to this table unless you have read
1439  * Documentation/sysctl/ctl_unnumbered.txt
1440  */
1441 	{ .ctl_name = 0 }
1442 };
1443 
1444 static struct ctl_table debug_table[] = {
1445 #if defined(CONFIG_X86) || defined(CONFIG_PPC)
1446 	{
1447 		.ctl_name	= CTL_UNNUMBERED,
1448 		.procname	= "exception-trace",
1449 		.data		= &show_unhandled_signals,
1450 		.maxlen		= sizeof(int),
1451 		.mode		= 0644,
1452 		.proc_handler	= proc_dointvec
1453 	},
1454 #endif
1455 	{ .ctl_name = 0 }
1456 };
1457 
1458 static struct ctl_table dev_table[] = {
1459 	{ .ctl_name = 0 }
1460 };
1461 
1462 static DEFINE_SPINLOCK(sysctl_lock);
1463 
1464 /* called under sysctl_lock */
use_table(struct ctl_table_header * p)1465 static int use_table(struct ctl_table_header *p)
1466 {
1467 	if (unlikely(p->unregistering))
1468 		return 0;
1469 	p->used++;
1470 	return 1;
1471 }
1472 
1473 /* called under sysctl_lock */
unuse_table(struct ctl_table_header * p)1474 static void unuse_table(struct ctl_table_header *p)
1475 {
1476 	if (!--p->used)
1477 		if (unlikely(p->unregistering))
1478 			complete(p->unregistering);
1479 }
1480 
1481 /* called under sysctl_lock, will reacquire if has to wait */
start_unregistering(struct ctl_table_header * p)1482 static void start_unregistering(struct ctl_table_header *p)
1483 {
1484 	/*
1485 	 * if p->used is 0, nobody will ever touch that entry again;
1486 	 * we'll eliminate all paths to it before dropping sysctl_lock
1487 	 */
1488 	if (unlikely(p->used)) {
1489 		struct completion wait;
1490 		init_completion(&wait);
1491 		p->unregistering = &wait;
1492 		spin_unlock(&sysctl_lock);
1493 		wait_for_completion(&wait);
1494 		spin_lock(&sysctl_lock);
1495 	} else {
1496 		/* anything non-NULL; we'll never dereference it */
1497 		p->unregistering = ERR_PTR(-EINVAL);
1498 	}
1499 	/*
1500 	 * do not remove from the list until nobody holds it; walking the
1501 	 * list in do_sysctl() relies on that.
1502 	 */
1503 	list_del_init(&p->ctl_entry);
1504 }
1505 
sysctl_head_get(struct ctl_table_header * head)1506 void sysctl_head_get(struct ctl_table_header *head)
1507 {
1508 	spin_lock(&sysctl_lock);
1509 	head->count++;
1510 	spin_unlock(&sysctl_lock);
1511 }
1512 
sysctl_head_put(struct ctl_table_header * head)1513 void sysctl_head_put(struct ctl_table_header *head)
1514 {
1515 	spin_lock(&sysctl_lock);
1516 	if (!--head->count)
1517 		kfree(head);
1518 	spin_unlock(&sysctl_lock);
1519 }
1520 
sysctl_head_grab(struct ctl_table_header * head)1521 struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
1522 {
1523 	if (!head)
1524 		BUG();
1525 	spin_lock(&sysctl_lock);
1526 	if (!use_table(head))
1527 		head = ERR_PTR(-ENOENT);
1528 	spin_unlock(&sysctl_lock);
1529 	return head;
1530 }
1531 
sysctl_head_finish(struct ctl_table_header * head)1532 void sysctl_head_finish(struct ctl_table_header *head)
1533 {
1534 	if (!head)
1535 		return;
1536 	spin_lock(&sysctl_lock);
1537 	unuse_table(head);
1538 	spin_unlock(&sysctl_lock);
1539 }
1540 
1541 static struct ctl_table_set *
lookup_header_set(struct ctl_table_root * root,struct nsproxy * namespaces)1542 lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
1543 {
1544 	struct ctl_table_set *set = &root->default_set;
1545 	if (root->lookup)
1546 		set = root->lookup(root, namespaces);
1547 	return set;
1548 }
1549 
1550 static struct list_head *
lookup_header_list(struct ctl_table_root * root,struct nsproxy * namespaces)1551 lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
1552 {
1553 	struct ctl_table_set *set = lookup_header_set(root, namespaces);
1554 	return &set->list;
1555 }
1556 
__sysctl_head_next(struct nsproxy * namespaces,struct ctl_table_header * prev)1557 struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
1558 					    struct ctl_table_header *prev)
1559 {
1560 	struct ctl_table_root *root;
1561 	struct list_head *header_list;
1562 	struct ctl_table_header *head;
1563 	struct list_head *tmp;
1564 
1565 	spin_lock(&sysctl_lock);
1566 	if (prev) {
1567 		head = prev;
1568 		tmp = &prev->ctl_entry;
1569 		unuse_table(prev);
1570 		goto next;
1571 	}
1572 	tmp = &root_table_header.ctl_entry;
1573 	for (;;) {
1574 		head = list_entry(tmp, struct ctl_table_header, ctl_entry);
1575 
1576 		if (!use_table(head))
1577 			goto next;
1578 		spin_unlock(&sysctl_lock);
1579 		return head;
1580 	next:
1581 		root = head->root;
1582 		tmp = tmp->next;
1583 		header_list = lookup_header_list(root, namespaces);
1584 		if (tmp != header_list)
1585 			continue;
1586 
1587 		do {
1588 			root = list_entry(root->root_list.next,
1589 					struct ctl_table_root, root_list);
1590 			if (root == &sysctl_table_root)
1591 				goto out;
1592 			header_list = lookup_header_list(root, namespaces);
1593 		} while (list_empty(header_list));
1594 		tmp = header_list->next;
1595 	}
1596 out:
1597 	spin_unlock(&sysctl_lock);
1598 	return NULL;
1599 }
1600 
sysctl_head_next(struct ctl_table_header * prev)1601 struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
1602 {
1603 	return __sysctl_head_next(current->nsproxy, prev);
1604 }
1605 
register_sysctl_root(struct ctl_table_root * root)1606 void register_sysctl_root(struct ctl_table_root *root)
1607 {
1608 	spin_lock(&sysctl_lock);
1609 	list_add_tail(&root->root_list, &sysctl_table_root.root_list);
1610 	spin_unlock(&sysctl_lock);
1611 }
1612 
1613 #ifdef CONFIG_SYSCTL_SYSCALL
1614 /* Perform the actual read/write of a sysctl table entry. */
do_sysctl_strategy(struct ctl_table_root * root,struct ctl_table * table,void __user * oldval,size_t __user * oldlenp,void __user * newval,size_t newlen)1615 static int do_sysctl_strategy(struct ctl_table_root *root,
1616 			struct ctl_table *table,
1617 			void __user *oldval, size_t __user *oldlenp,
1618 			void __user *newval, size_t newlen)
1619 {
1620 	int op = 0, rc;
1621 
1622 	if (oldval)
1623 		op |= MAY_READ;
1624 	if (newval)
1625 		op |= MAY_WRITE;
1626 	if (sysctl_perm(root, table, op))
1627 		return -EPERM;
1628 
1629 	if (table->strategy) {
1630 		rc = table->strategy(table, oldval, oldlenp, newval, newlen);
1631 		if (rc < 0)
1632 			return rc;
1633 		if (rc > 0)
1634 			return 0;
1635 	}
1636 
1637 	/* If there is no strategy routine, or if the strategy returns
1638 	 * zero, proceed with automatic r/w */
1639 	if (table->data && table->maxlen) {
1640 		rc = sysctl_data(table, oldval, oldlenp, newval, newlen);
1641 		if (rc < 0)
1642 			return rc;
1643 	}
1644 	return 0;
1645 }
1646 
parse_table(int __user * name,int nlen,void __user * oldval,size_t __user * oldlenp,void __user * newval,size_t newlen,struct ctl_table_root * root,struct ctl_table * table)1647 static int parse_table(int __user *name, int nlen,
1648 		       void __user *oldval, size_t __user *oldlenp,
1649 		       void __user *newval, size_t newlen,
1650 		       struct ctl_table_root *root,
1651 		       struct ctl_table *table)
1652 {
1653 	int n;
1654 repeat:
1655 	if (!nlen)
1656 		return -ENOTDIR;
1657 	if (get_user(n, name))
1658 		return -EFAULT;
1659 	for ( ; table->ctl_name || table->procname; table++) {
1660 		if (!table->ctl_name)
1661 			continue;
1662 		if (n == table->ctl_name) {
1663 			int error;
1664 			if (table->child) {
1665 				if (sysctl_perm(root, table, MAY_EXEC))
1666 					return -EPERM;
1667 				name++;
1668 				nlen--;
1669 				table = table->child;
1670 				goto repeat;
1671 			}
1672 			error = do_sysctl_strategy(root, table,
1673 						   oldval, oldlenp,
1674 						   newval, newlen);
1675 			return error;
1676 		}
1677 	}
1678 	return -ENOTDIR;
1679 }
1680 
do_sysctl(int __user * name,int nlen,void __user * oldval,size_t __user * oldlenp,void __user * newval,size_t newlen)1681 int do_sysctl(int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp,
1682 	       void __user *newval, size_t newlen)
1683 {
1684 	struct ctl_table_header *head;
1685 	int error = -ENOTDIR;
1686 
1687 	if (nlen <= 0 || nlen >= CTL_MAXNAME)
1688 		return -ENOTDIR;
1689 	if (oldval) {
1690 		int old_len;
1691 		if (!oldlenp || get_user(old_len, oldlenp))
1692 			return -EFAULT;
1693 	}
1694 
1695 	for (head = sysctl_head_next(NULL); head;
1696 			head = sysctl_head_next(head)) {
1697 		error = parse_table(name, nlen, oldval, oldlenp,
1698 					newval, newlen,
1699 					head->root, head->ctl_table);
1700 		if (error != -ENOTDIR) {
1701 			sysctl_head_finish(head);
1702 			break;
1703 		}
1704 	}
1705 	return error;
1706 }
1707 
SYSCALL_DEFINE1(sysctl,struct __sysctl_args __user *,args)1708 SYSCALL_DEFINE1(sysctl, struct __sysctl_args __user *, args)
1709 {
1710 	struct __sysctl_args tmp;
1711 	int error;
1712 
1713 	if (copy_from_user(&tmp, args, sizeof(tmp)))
1714 		return -EFAULT;
1715 
1716 	error = deprecated_sysctl_warning(&tmp);
1717 	if (error)
1718 		goto out;
1719 
1720 	lock_kernel();
1721 	error = do_sysctl(tmp.name, tmp.nlen, tmp.oldval, tmp.oldlenp,
1722 			  tmp.newval, tmp.newlen);
1723 	unlock_kernel();
1724 out:
1725 	return error;
1726 }
1727 #endif /* CONFIG_SYSCTL_SYSCALL */
1728 
1729 /*
1730  * sysctl_perm does NOT grant the superuser all rights automatically, because
1731  * some sysctl variables are readonly even to root.
1732  */
1733 
test_perm(int mode,int op)1734 static int test_perm(int mode, int op)
1735 {
1736 	if (!current_euid())
1737 		mode >>= 6;
1738 	else if (in_egroup_p(0))
1739 		mode >>= 3;
1740 	if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
1741 		return 0;
1742 	return -EACCES;
1743 }
1744 
sysctl_perm(struct ctl_table_root * root,struct ctl_table * table,int op)1745 int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
1746 {
1747 	int error;
1748 	int mode;
1749 
1750 	error = security_sysctl(table, op & (MAY_READ | MAY_WRITE | MAY_EXEC));
1751 	if (error)
1752 		return error;
1753 
1754 	if (root->permissions)
1755 		mode = root->permissions(root, current->nsproxy, table);
1756 	else
1757 		mode = table->mode;
1758 
1759 	return test_perm(mode, op);
1760 }
1761 
sysctl_set_parent(struct ctl_table * parent,struct ctl_table * table)1762 static void sysctl_set_parent(struct ctl_table *parent, struct ctl_table *table)
1763 {
1764 	for (; table->ctl_name || table->procname; table++) {
1765 		table->parent = parent;
1766 		if (table->child)
1767 			sysctl_set_parent(table, table->child);
1768 	}
1769 }
1770 
sysctl_init(void)1771 static __init int sysctl_init(void)
1772 {
1773 	sysctl_set_parent(NULL, root_table);
1774 #ifdef CONFIG_SYSCTL_SYSCALL_CHECK
1775 	{
1776 		int err;
1777 		err = sysctl_check_table(current->nsproxy, root_table);
1778 	}
1779 #endif
1780 	return 0;
1781 }
1782 
1783 core_initcall(sysctl_init);
1784 
is_branch_in(struct ctl_table * branch,struct ctl_table * table)1785 static struct ctl_table *is_branch_in(struct ctl_table *branch,
1786 				      struct ctl_table *table)
1787 {
1788 	struct ctl_table *p;
1789 	const char *s = branch->procname;
1790 
1791 	/* branch should have named subdirectory as its first element */
1792 	if (!s || !branch->child)
1793 		return NULL;
1794 
1795 	/* ... and nothing else */
1796 	if (branch[1].procname || branch[1].ctl_name)
1797 		return NULL;
1798 
1799 	/* table should contain subdirectory with the same name */
1800 	for (p = table; p->procname || p->ctl_name; p++) {
1801 		if (!p->child)
1802 			continue;
1803 		if (p->procname && strcmp(p->procname, s) == 0)
1804 			return p;
1805 	}
1806 	return NULL;
1807 }
1808 
1809 /* see if attaching q to p would be an improvement */
try_attach(struct ctl_table_header * p,struct ctl_table_header * q)1810 static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
1811 {
1812 	struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
1813 	struct ctl_table *next;
1814 	int is_better = 0;
1815 	int not_in_parent = !p->attached_by;
1816 
1817 	while ((next = is_branch_in(by, to)) != NULL) {
1818 		if (by == q->attached_by)
1819 			is_better = 1;
1820 		if (to == p->attached_by)
1821 			not_in_parent = 1;
1822 		by = by->child;
1823 		to = next->child;
1824 	}
1825 
1826 	if (is_better && not_in_parent) {
1827 		q->attached_by = by;
1828 		q->attached_to = to;
1829 		q->parent = p;
1830 	}
1831 }
1832 
1833 /**
1834  * __register_sysctl_paths - register a sysctl hierarchy
1835  * @root: List of sysctl headers to register on
1836  * @namespaces: Data to compute which lists of sysctl entries are visible
1837  * @path: The path to the directory the sysctl table is in.
1838  * @table: the top-level table structure
1839  *
1840  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1841  * array. A completely 0 filled entry terminates the table.
1842  *
1843  * The members of the &struct ctl_table structure are used as follows:
1844  *
1845  * ctl_name - This is the numeric sysctl value used by sysctl(2). The number
1846  *            must be unique within that level of sysctl
1847  *
1848  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1849  *            enter a sysctl file
1850  *
1851  * data - a pointer to data for use by proc_handler
1852  *
1853  * maxlen - the maximum size in bytes of the data
1854  *
1855  * mode - the file permissions for the /proc/sys file, and for sysctl(2)
1856  *
1857  * child - a pointer to the child sysctl table if this entry is a directory, or
1858  *         %NULL.
1859  *
1860  * proc_handler - the text handler routine (described below)
1861  *
1862  * strategy - the strategy routine (described below)
1863  *
1864  * de - for internal use by the sysctl routines
1865  *
1866  * extra1, extra2 - extra pointers usable by the proc handler routines
1867  *
1868  * Leaf nodes in the sysctl tree will be represented by a single file
1869  * under /proc; non-leaf nodes will be represented by directories.
1870  *
1871  * sysctl(2) can automatically manage read and write requests through
1872  * the sysctl table.  The data and maxlen fields of the ctl_table
1873  * struct enable minimal validation of the values being written to be
1874  * performed, and the mode field allows minimal authentication.
1875  *
1876  * More sophisticated management can be enabled by the provision of a
1877  * strategy routine with the table entry.  This will be called before
1878  * any automatic read or write of the data is performed.
1879  *
1880  * The strategy routine may return
1881  *
1882  * < 0 - Error occurred (error is passed to user process)
1883  *
1884  * 0   - OK - proceed with automatic read or write.
1885  *
1886  * > 0 - OK - read or write has been done by the strategy routine, so
1887  *       return immediately.
1888  *
1889  * There must be a proc_handler routine for any terminal nodes
1890  * mirrored under /proc/sys (non-terminals are handled by a built-in
1891  * directory handler).  Several default handlers are available to
1892  * cover common cases -
1893  *
1894  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1895  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1896  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1897  *
1898  * It is the handler's job to read the input buffer from user memory
1899  * and process it. The handler should return 0 on success.
1900  *
1901  * This routine returns %NULL on a failure to register, and a pointer
1902  * to the table header on success.
1903  */
__register_sysctl_paths(struct ctl_table_root * root,struct nsproxy * namespaces,const struct ctl_path * path,struct ctl_table * table)1904 struct ctl_table_header *__register_sysctl_paths(
1905 	struct ctl_table_root *root,
1906 	struct nsproxy *namespaces,
1907 	const struct ctl_path *path, struct ctl_table *table)
1908 {
1909 	struct ctl_table_header *header;
1910 	struct ctl_table *new, **prevp;
1911 	unsigned int n, npath;
1912 	struct ctl_table_set *set;
1913 
1914 	/* Count the path components */
1915 	for (npath = 0; path[npath].ctl_name || path[npath].procname; ++npath)
1916 		;
1917 
1918 	/*
1919 	 * For each path component, allocate a 2-element ctl_table array.
1920 	 * The first array element will be filled with the sysctl entry
1921 	 * for this, the second will be the sentinel (ctl_name == 0).
1922 	 *
1923 	 * We allocate everything in one go so that we don't have to
1924 	 * worry about freeing additional memory in unregister_sysctl_table.
1925 	 */
1926 	header = kzalloc(sizeof(struct ctl_table_header) +
1927 			 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
1928 	if (!header)
1929 		return NULL;
1930 
1931 	new = (struct ctl_table *) (header + 1);
1932 
1933 	/* Now connect the dots */
1934 	prevp = &header->ctl_table;
1935 	for (n = 0; n < npath; ++n, ++path) {
1936 		/* Copy the procname */
1937 		new->procname = path->procname;
1938 		new->ctl_name = path->ctl_name;
1939 		new->mode     = 0555;
1940 
1941 		*prevp = new;
1942 		prevp = &new->child;
1943 
1944 		new += 2;
1945 	}
1946 	*prevp = table;
1947 	header->ctl_table_arg = table;
1948 
1949 	INIT_LIST_HEAD(&header->ctl_entry);
1950 	header->used = 0;
1951 	header->unregistering = NULL;
1952 	header->root = root;
1953 	sysctl_set_parent(NULL, header->ctl_table);
1954 	header->count = 1;
1955 #ifdef CONFIG_SYSCTL_SYSCALL_CHECK
1956 	if (sysctl_check_table(namespaces, header->ctl_table)) {
1957 		kfree(header);
1958 		return NULL;
1959 	}
1960 #endif
1961 	spin_lock(&sysctl_lock);
1962 	header->set = lookup_header_set(root, namespaces);
1963 	header->attached_by = header->ctl_table;
1964 	header->attached_to = root_table;
1965 	header->parent = &root_table_header;
1966 	for (set = header->set; set; set = set->parent) {
1967 		struct ctl_table_header *p;
1968 		list_for_each_entry(p, &set->list, ctl_entry) {
1969 			if (p->unregistering)
1970 				continue;
1971 			try_attach(p, header);
1972 		}
1973 	}
1974 	header->parent->count++;
1975 	list_add_tail(&header->ctl_entry, &header->set->list);
1976 	spin_unlock(&sysctl_lock);
1977 
1978 	return header;
1979 }
1980 
1981 /**
1982  * register_sysctl_table_path - register a sysctl table hierarchy
1983  * @path: The path to the directory the sysctl table is in.
1984  * @table: the top-level table structure
1985  *
1986  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1987  * array. A completely 0 filled entry terminates the table.
1988  *
1989  * See __register_sysctl_paths for more details.
1990  */
register_sysctl_paths(const struct ctl_path * path,struct ctl_table * table)1991 struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1992 						struct ctl_table *table)
1993 {
1994 	return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1995 					path, table);
1996 }
1997 
1998 /**
1999  * register_sysctl_table - register a sysctl table hierarchy
2000  * @table: the top-level table structure
2001  *
2002  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
2003  * array. A completely 0 filled entry terminates the table.
2004  *
2005  * See register_sysctl_paths for more details.
2006  */
register_sysctl_table(struct ctl_table * table)2007 struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
2008 {
2009 	static const struct ctl_path null_path[] = { {} };
2010 
2011 	return register_sysctl_paths(null_path, table);
2012 }
2013 
2014 /**
2015  * unregister_sysctl_table - unregister a sysctl table hierarchy
2016  * @header: the header returned from register_sysctl_table
2017  *
2018  * Unregisters the sysctl table and all children. proc entries may not
2019  * actually be removed until they are no longer used by anyone.
2020  */
unregister_sysctl_table(struct ctl_table_header * header)2021 void unregister_sysctl_table(struct ctl_table_header * header)
2022 {
2023 	might_sleep();
2024 
2025 	if (header == NULL)
2026 		return;
2027 
2028 	spin_lock(&sysctl_lock);
2029 	start_unregistering(header);
2030 	if (!--header->parent->count) {
2031 		WARN_ON(1);
2032 		kfree(header->parent);
2033 	}
2034 	if (!--header->count)
2035 		kfree(header);
2036 	spin_unlock(&sysctl_lock);
2037 }
2038 
sysctl_is_seen(struct ctl_table_header * p)2039 int sysctl_is_seen(struct ctl_table_header *p)
2040 {
2041 	struct ctl_table_set *set = p->set;
2042 	int res;
2043 	spin_lock(&sysctl_lock);
2044 	if (p->unregistering)
2045 		res = 0;
2046 	else if (!set->is_seen)
2047 		res = 1;
2048 	else
2049 		res = set->is_seen(set);
2050 	spin_unlock(&sysctl_lock);
2051 	return res;
2052 }
2053 
setup_sysctl_set(struct ctl_table_set * p,struct ctl_table_set * parent,int (* is_seen)(struct ctl_table_set *))2054 void setup_sysctl_set(struct ctl_table_set *p,
2055 	struct ctl_table_set *parent,
2056 	int (*is_seen)(struct ctl_table_set *))
2057 {
2058 	INIT_LIST_HEAD(&p->list);
2059 	p->parent = parent ? parent : &sysctl_table_root.default_set;
2060 	p->is_seen = is_seen;
2061 }
2062 
2063 #else /* !CONFIG_SYSCTL */
register_sysctl_table(struct ctl_table * table)2064 struct ctl_table_header *register_sysctl_table(struct ctl_table * table)
2065 {
2066 	return NULL;
2067 }
2068 
register_sysctl_paths(const struct ctl_path * path,struct ctl_table * table)2069 struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
2070 						    struct ctl_table *table)
2071 {
2072 	return NULL;
2073 }
2074 
unregister_sysctl_table(struct ctl_table_header * table)2075 void unregister_sysctl_table(struct ctl_table_header * table)
2076 {
2077 }
2078 
setup_sysctl_set(struct ctl_table_set * p,struct ctl_table_set * parent,int (* is_seen)(struct ctl_table_set *))2079 void setup_sysctl_set(struct ctl_table_set *p,
2080 	struct ctl_table_set *parent,
2081 	int (*is_seen)(struct ctl_table_set *))
2082 {
2083 }
2084 
sysctl_head_put(struct ctl_table_header * head)2085 void sysctl_head_put(struct ctl_table_header *head)
2086 {
2087 }
2088 
2089 #endif /* CONFIG_SYSCTL */
2090 
2091 /*
2092  * /proc/sys support
2093  */
2094 
2095 #ifdef CONFIG_PROC_SYSCTL
2096 
_proc_do_string(void * data,int maxlen,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2097 static int _proc_do_string(void* data, int maxlen, int write,
2098 			   struct file *filp, void __user *buffer,
2099 			   size_t *lenp, loff_t *ppos)
2100 {
2101 	size_t len;
2102 	char __user *p;
2103 	char c;
2104 
2105 	if (!data || !maxlen || !*lenp) {
2106 		*lenp = 0;
2107 		return 0;
2108 	}
2109 
2110 	if (write) {
2111 		len = 0;
2112 		p = buffer;
2113 		while (len < *lenp) {
2114 			if (get_user(c, p++))
2115 				return -EFAULT;
2116 			if (c == 0 || c == '\n')
2117 				break;
2118 			len++;
2119 		}
2120 		if (len >= maxlen)
2121 			len = maxlen-1;
2122 		if(copy_from_user(data, buffer, len))
2123 			return -EFAULT;
2124 		((char *) data)[len] = 0;
2125 		*ppos += *lenp;
2126 	} else {
2127 		len = strlen(data);
2128 		if (len > maxlen)
2129 			len = maxlen;
2130 
2131 		if (*ppos > len) {
2132 			*lenp = 0;
2133 			return 0;
2134 		}
2135 
2136 		data += *ppos;
2137 		len  -= *ppos;
2138 
2139 		if (len > *lenp)
2140 			len = *lenp;
2141 		if (len)
2142 			if(copy_to_user(buffer, data, len))
2143 				return -EFAULT;
2144 		if (len < *lenp) {
2145 			if(put_user('\n', ((char __user *) buffer) + len))
2146 				return -EFAULT;
2147 			len++;
2148 		}
2149 		*lenp = len;
2150 		*ppos += len;
2151 	}
2152 	return 0;
2153 }
2154 
2155 /**
2156  * proc_dostring - read a string sysctl
2157  * @table: the sysctl table
2158  * @write: %TRUE if this is a write to the sysctl file
2159  * @filp: the file structure
2160  * @buffer: the user buffer
2161  * @lenp: the size of the user buffer
2162  * @ppos: file position
2163  *
2164  * Reads/writes a string from/to the user buffer. If the kernel
2165  * buffer provided is not large enough to hold the string, the
2166  * string is truncated. The copied string is %NULL-terminated.
2167  * If the string is being read by the user process, it is copied
2168  * and a newline '\n' is added. It is truncated if the buffer is
2169  * not large enough.
2170  *
2171  * Returns 0 on success.
2172  */
proc_dostring(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2173 int proc_dostring(struct ctl_table *table, int write, struct file *filp,
2174 		  void __user *buffer, size_t *lenp, loff_t *ppos)
2175 {
2176 	return _proc_do_string(table->data, table->maxlen, write, filp,
2177 			       buffer, lenp, ppos);
2178 }
2179 
2180 
do_proc_dointvec_conv(int * negp,unsigned long * lvalp,int * valp,int write,void * data)2181 static int do_proc_dointvec_conv(int *negp, unsigned long *lvalp,
2182 				 int *valp,
2183 				 int write, void *data)
2184 {
2185 	if (write) {
2186 		*valp = *negp ? -*lvalp : *lvalp;
2187 	} else {
2188 		int val = *valp;
2189 		if (val < 0) {
2190 			*negp = -1;
2191 			*lvalp = (unsigned long)-val;
2192 		} else {
2193 			*negp = 0;
2194 			*lvalp = (unsigned long)val;
2195 		}
2196 	}
2197 	return 0;
2198 }
2199 
__do_proc_dointvec(void * tbl_data,struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos,int (* conv)(int * negp,unsigned long * lvalp,int * valp,int write,void * data),void * data)2200 static int __do_proc_dointvec(void *tbl_data, struct ctl_table *table,
2201 		  int write, struct file *filp, void __user *buffer,
2202 		  size_t *lenp, loff_t *ppos,
2203 		  int (*conv)(int *negp, unsigned long *lvalp, int *valp,
2204 			      int write, void *data),
2205 		  void *data)
2206 {
2207 #define TMPBUFLEN 21
2208 	int *i, vleft, first=1, neg, val;
2209 	unsigned long lval;
2210 	size_t left, len;
2211 
2212 	char buf[TMPBUFLEN], *p;
2213 	char __user *s = buffer;
2214 
2215 	if (!tbl_data || !table->maxlen || !*lenp ||
2216 	    (*ppos && !write)) {
2217 		*lenp = 0;
2218 		return 0;
2219 	}
2220 
2221 	i = (int *) tbl_data;
2222 	vleft = table->maxlen / sizeof(*i);
2223 	left = *lenp;
2224 
2225 	if (!conv)
2226 		conv = do_proc_dointvec_conv;
2227 
2228 	for (; left && vleft--; i++, first=0) {
2229 		if (write) {
2230 			while (left) {
2231 				char c;
2232 				if (get_user(c, s))
2233 					return -EFAULT;
2234 				if (!isspace(c))
2235 					break;
2236 				left--;
2237 				s++;
2238 			}
2239 			if (!left)
2240 				break;
2241 			neg = 0;
2242 			len = left;
2243 			if (len > sizeof(buf) - 1)
2244 				len = sizeof(buf) - 1;
2245 			if (copy_from_user(buf, s, len))
2246 				return -EFAULT;
2247 			buf[len] = 0;
2248 			p = buf;
2249 			if (*p == '-' && left > 1) {
2250 				neg = 1;
2251 				p++;
2252 			}
2253 			if (*p < '0' || *p > '9')
2254 				break;
2255 
2256 			lval = simple_strtoul(p, &p, 0);
2257 
2258 			len = p-buf;
2259 			if ((len < left) && *p && !isspace(*p))
2260 				break;
2261 			if (neg)
2262 				val = -val;
2263 			s += len;
2264 			left -= len;
2265 
2266 			if (conv(&neg, &lval, i, 1, data))
2267 				break;
2268 		} else {
2269 			p = buf;
2270 			if (!first)
2271 				*p++ = '\t';
2272 
2273 			if (conv(&neg, &lval, i, 0, data))
2274 				break;
2275 
2276 			sprintf(p, "%s%lu", neg ? "-" : "", lval);
2277 			len = strlen(buf);
2278 			if (len > left)
2279 				len = left;
2280 			if(copy_to_user(s, buf, len))
2281 				return -EFAULT;
2282 			left -= len;
2283 			s += len;
2284 		}
2285 	}
2286 
2287 	if (!write && !first && left) {
2288 		if(put_user('\n', s))
2289 			return -EFAULT;
2290 		left--, s++;
2291 	}
2292 	if (write) {
2293 		while (left) {
2294 			char c;
2295 			if (get_user(c, s++))
2296 				return -EFAULT;
2297 			if (!isspace(c))
2298 				break;
2299 			left--;
2300 		}
2301 	}
2302 	if (write && first)
2303 		return -EINVAL;
2304 	*lenp -= left;
2305 	*ppos += *lenp;
2306 	return 0;
2307 #undef TMPBUFLEN
2308 }
2309 
do_proc_dointvec(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos,int (* conv)(int * negp,unsigned long * lvalp,int * valp,int write,void * data),void * data)2310 static int do_proc_dointvec(struct ctl_table *table, int write, struct file *filp,
2311 		  void __user *buffer, size_t *lenp, loff_t *ppos,
2312 		  int (*conv)(int *negp, unsigned long *lvalp, int *valp,
2313 			      int write, void *data),
2314 		  void *data)
2315 {
2316 	return __do_proc_dointvec(table->data, table, write, filp,
2317 			buffer, lenp, ppos, conv, data);
2318 }
2319 
2320 /**
2321  * proc_dointvec - read a vector of integers
2322  * @table: the sysctl table
2323  * @write: %TRUE if this is a write to the sysctl file
2324  * @filp: the file structure
2325  * @buffer: the user buffer
2326  * @lenp: the size of the user buffer
2327  * @ppos: file position
2328  *
2329  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2330  * values from/to the user buffer, treated as an ASCII string.
2331  *
2332  * Returns 0 on success.
2333  */
proc_dointvec(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2334 int proc_dointvec(struct ctl_table *table, int write, struct file *filp,
2335 		     void __user *buffer, size_t *lenp, loff_t *ppos)
2336 {
2337     return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
2338 		    	    NULL,NULL);
2339 }
2340 
2341 /*
2342  * Taint values can only be increased
2343  * This means we can safely use a temporary.
2344  */
proc_taint(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2345 static int proc_taint(struct ctl_table *table, int write, struct file *filp,
2346 			       void __user *buffer, size_t *lenp, loff_t *ppos)
2347 {
2348 	struct ctl_table t;
2349 	unsigned long tmptaint = get_taint();
2350 	int err;
2351 
2352 	if (write && !capable(CAP_SYS_ADMIN))
2353 		return -EPERM;
2354 
2355 	t = *table;
2356 	t.data = &tmptaint;
2357 	err = proc_doulongvec_minmax(&t, write, filp, buffer, lenp, ppos);
2358 	if (err < 0)
2359 		return err;
2360 
2361 	if (write) {
2362 		/*
2363 		 * Poor man's atomic or. Not worth adding a primitive
2364 		 * to everyone's atomic.h for this
2365 		 */
2366 		int i;
2367 		for (i = 0; i < BITS_PER_LONG && tmptaint >> i; i++) {
2368 			if ((tmptaint >> i) & 1)
2369 				add_taint(i);
2370 		}
2371 	}
2372 
2373 	return err;
2374 }
2375 
2376 struct do_proc_dointvec_minmax_conv_param {
2377 	int *min;
2378 	int *max;
2379 };
2380 
do_proc_dointvec_minmax_conv(int * negp,unsigned long * lvalp,int * valp,int write,void * data)2381 static int do_proc_dointvec_minmax_conv(int *negp, unsigned long *lvalp,
2382 					int *valp,
2383 					int write, void *data)
2384 {
2385 	struct do_proc_dointvec_minmax_conv_param *param = data;
2386 	if (write) {
2387 		int val = *negp ? -*lvalp : *lvalp;
2388 		if ((param->min && *param->min > val) ||
2389 		    (param->max && *param->max < val))
2390 			return -EINVAL;
2391 		*valp = val;
2392 	} else {
2393 		int val = *valp;
2394 		if (val < 0) {
2395 			*negp = -1;
2396 			*lvalp = (unsigned long)-val;
2397 		} else {
2398 			*negp = 0;
2399 			*lvalp = (unsigned long)val;
2400 		}
2401 	}
2402 	return 0;
2403 }
2404 
2405 /**
2406  * proc_dointvec_minmax - read a vector of integers with min/max values
2407  * @table: the sysctl table
2408  * @write: %TRUE if this is a write to the sysctl file
2409  * @filp: the file structure
2410  * @buffer: the user buffer
2411  * @lenp: the size of the user buffer
2412  * @ppos: file position
2413  *
2414  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2415  * values from/to the user buffer, treated as an ASCII string.
2416  *
2417  * This routine will ensure the values are within the range specified by
2418  * table->extra1 (min) and table->extra2 (max).
2419  *
2420  * Returns 0 on success.
2421  */
proc_dointvec_minmax(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2422 int proc_dointvec_minmax(struct ctl_table *table, int write, struct file *filp,
2423 		  void __user *buffer, size_t *lenp, loff_t *ppos)
2424 {
2425 	struct do_proc_dointvec_minmax_conv_param param = {
2426 		.min = (int *) table->extra1,
2427 		.max = (int *) table->extra2,
2428 	};
2429 	return do_proc_dointvec(table, write, filp, buffer, lenp, ppos,
2430 				do_proc_dointvec_minmax_conv, &param);
2431 }
2432 
__do_proc_doulongvec_minmax(void * data,struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos,unsigned long convmul,unsigned long convdiv)2433 static int __do_proc_doulongvec_minmax(void *data, struct ctl_table *table, int write,
2434 				     struct file *filp,
2435 				     void __user *buffer,
2436 				     size_t *lenp, loff_t *ppos,
2437 				     unsigned long convmul,
2438 				     unsigned long convdiv)
2439 {
2440 #define TMPBUFLEN 21
2441 	unsigned long *i, *min, *max, val;
2442 	int vleft, first=1, neg;
2443 	size_t len, left;
2444 	char buf[TMPBUFLEN], *p;
2445 	char __user *s = buffer;
2446 
2447 	if (!data || !table->maxlen || !*lenp ||
2448 	    (*ppos && !write)) {
2449 		*lenp = 0;
2450 		return 0;
2451 	}
2452 
2453 	i = (unsigned long *) data;
2454 	min = (unsigned long *) table->extra1;
2455 	max = (unsigned long *) table->extra2;
2456 	vleft = table->maxlen / sizeof(unsigned long);
2457 	left = *lenp;
2458 
2459 	for (; left && vleft--; i++, min++, max++, first=0) {
2460 		if (write) {
2461 			while (left) {
2462 				char c;
2463 				if (get_user(c, s))
2464 					return -EFAULT;
2465 				if (!isspace(c))
2466 					break;
2467 				left--;
2468 				s++;
2469 			}
2470 			if (!left)
2471 				break;
2472 			neg = 0;
2473 			len = left;
2474 			if (len > TMPBUFLEN-1)
2475 				len = TMPBUFLEN-1;
2476 			if (copy_from_user(buf, s, len))
2477 				return -EFAULT;
2478 			buf[len] = 0;
2479 			p = buf;
2480 			if (*p == '-' && left > 1) {
2481 				neg = 1;
2482 				p++;
2483 			}
2484 			if (*p < '0' || *p > '9')
2485 				break;
2486 			val = simple_strtoul(p, &p, 0) * convmul / convdiv ;
2487 			len = p-buf;
2488 			if ((len < left) && *p && !isspace(*p))
2489 				break;
2490 			if (neg)
2491 				val = -val;
2492 			s += len;
2493 			left -= len;
2494 
2495 			if(neg)
2496 				continue;
2497 			if ((min && val < *min) || (max && val > *max))
2498 				continue;
2499 			*i = val;
2500 		} else {
2501 			p = buf;
2502 			if (!first)
2503 				*p++ = '\t';
2504 			sprintf(p, "%lu", convdiv * (*i) / convmul);
2505 			len = strlen(buf);
2506 			if (len > left)
2507 				len = left;
2508 			if(copy_to_user(s, buf, len))
2509 				return -EFAULT;
2510 			left -= len;
2511 			s += len;
2512 		}
2513 	}
2514 
2515 	if (!write && !first && left) {
2516 		if(put_user('\n', s))
2517 			return -EFAULT;
2518 		left--, s++;
2519 	}
2520 	if (write) {
2521 		while (left) {
2522 			char c;
2523 			if (get_user(c, s++))
2524 				return -EFAULT;
2525 			if (!isspace(c))
2526 				break;
2527 			left--;
2528 		}
2529 	}
2530 	if (write && first)
2531 		return -EINVAL;
2532 	*lenp -= left;
2533 	*ppos += *lenp;
2534 	return 0;
2535 #undef TMPBUFLEN
2536 }
2537 
do_proc_doulongvec_minmax(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos,unsigned long convmul,unsigned long convdiv)2538 static int do_proc_doulongvec_minmax(struct ctl_table *table, int write,
2539 				     struct file *filp,
2540 				     void __user *buffer,
2541 				     size_t *lenp, loff_t *ppos,
2542 				     unsigned long convmul,
2543 				     unsigned long convdiv)
2544 {
2545 	return __do_proc_doulongvec_minmax(table->data, table, write,
2546 			filp, buffer, lenp, ppos, convmul, convdiv);
2547 }
2548 
2549 /**
2550  * proc_doulongvec_minmax - read a vector of long integers with min/max values
2551  * @table: the sysctl table
2552  * @write: %TRUE if this is a write to the sysctl file
2553  * @filp: the file structure
2554  * @buffer: the user buffer
2555  * @lenp: the size of the user buffer
2556  * @ppos: file position
2557  *
2558  * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
2559  * values from/to the user buffer, treated as an ASCII string.
2560  *
2561  * This routine will ensure the values are within the range specified by
2562  * table->extra1 (min) and table->extra2 (max).
2563  *
2564  * Returns 0 on success.
2565  */
proc_doulongvec_minmax(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2566 int proc_doulongvec_minmax(struct ctl_table *table, int write, struct file *filp,
2567 			   void __user *buffer, size_t *lenp, loff_t *ppos)
2568 {
2569     return do_proc_doulongvec_minmax(table, write, filp, buffer, lenp, ppos, 1l, 1l);
2570 }
2571 
2572 /**
2573  * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
2574  * @table: the sysctl table
2575  * @write: %TRUE if this is a write to the sysctl file
2576  * @filp: the file structure
2577  * @buffer: the user buffer
2578  * @lenp: the size of the user buffer
2579  * @ppos: file position
2580  *
2581  * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
2582  * values from/to the user buffer, treated as an ASCII string. The values
2583  * are treated as milliseconds, and converted to jiffies when they are stored.
2584  *
2585  * This routine will ensure the values are within the range specified by
2586  * table->extra1 (min) and table->extra2 (max).
2587  *
2588  * Returns 0 on success.
2589  */
proc_doulongvec_ms_jiffies_minmax(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2590 int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int write,
2591 				      struct file *filp,
2592 				      void __user *buffer,
2593 				      size_t *lenp, loff_t *ppos)
2594 {
2595     return do_proc_doulongvec_minmax(table, write, filp, buffer,
2596 				     lenp, ppos, HZ, 1000l);
2597 }
2598 
2599 
do_proc_dointvec_jiffies_conv(int * negp,unsigned long * lvalp,int * valp,int write,void * data)2600 static int do_proc_dointvec_jiffies_conv(int *negp, unsigned long *lvalp,
2601 					 int *valp,
2602 					 int write, void *data)
2603 {
2604 	if (write) {
2605 		if (*lvalp > LONG_MAX / HZ)
2606 			return 1;
2607 		*valp = *negp ? -(*lvalp*HZ) : (*lvalp*HZ);
2608 	} else {
2609 		int val = *valp;
2610 		unsigned long lval;
2611 		if (val < 0) {
2612 			*negp = -1;
2613 			lval = (unsigned long)-val;
2614 		} else {
2615 			*negp = 0;
2616 			lval = (unsigned long)val;
2617 		}
2618 		*lvalp = lval / HZ;
2619 	}
2620 	return 0;
2621 }
2622 
do_proc_dointvec_userhz_jiffies_conv(int * negp,unsigned long * lvalp,int * valp,int write,void * data)2623 static int do_proc_dointvec_userhz_jiffies_conv(int *negp, unsigned long *lvalp,
2624 						int *valp,
2625 						int write, void *data)
2626 {
2627 	if (write) {
2628 		if (USER_HZ < HZ && *lvalp > (LONG_MAX / HZ) * USER_HZ)
2629 			return 1;
2630 		*valp = clock_t_to_jiffies(*negp ? -*lvalp : *lvalp);
2631 	} else {
2632 		int val = *valp;
2633 		unsigned long lval;
2634 		if (val < 0) {
2635 			*negp = -1;
2636 			lval = (unsigned long)-val;
2637 		} else {
2638 			*negp = 0;
2639 			lval = (unsigned long)val;
2640 		}
2641 		*lvalp = jiffies_to_clock_t(lval);
2642 	}
2643 	return 0;
2644 }
2645 
do_proc_dointvec_ms_jiffies_conv(int * negp,unsigned long * lvalp,int * valp,int write,void * data)2646 static int do_proc_dointvec_ms_jiffies_conv(int *negp, unsigned long *lvalp,
2647 					    int *valp,
2648 					    int write, void *data)
2649 {
2650 	if (write) {
2651 		*valp = msecs_to_jiffies(*negp ? -*lvalp : *lvalp);
2652 	} else {
2653 		int val = *valp;
2654 		unsigned long lval;
2655 		if (val < 0) {
2656 			*negp = -1;
2657 			lval = (unsigned long)-val;
2658 		} else {
2659 			*negp = 0;
2660 			lval = (unsigned long)val;
2661 		}
2662 		*lvalp = jiffies_to_msecs(lval);
2663 	}
2664 	return 0;
2665 }
2666 
2667 /**
2668  * proc_dointvec_jiffies - read a vector of integers as seconds
2669  * @table: the sysctl table
2670  * @write: %TRUE if this is a write to the sysctl file
2671  * @filp: the file structure
2672  * @buffer: the user buffer
2673  * @lenp: the size of the user buffer
2674  * @ppos: file position
2675  *
2676  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2677  * values from/to the user buffer, treated as an ASCII string.
2678  * The values read are assumed to be in seconds, and are converted into
2679  * jiffies.
2680  *
2681  * Returns 0 on success.
2682  */
proc_dointvec_jiffies(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2683 int proc_dointvec_jiffies(struct ctl_table *table, int write, struct file *filp,
2684 			  void __user *buffer, size_t *lenp, loff_t *ppos)
2685 {
2686     return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
2687 		    	    do_proc_dointvec_jiffies_conv,NULL);
2688 }
2689 
2690 /**
2691  * proc_dointvec_userhz_jiffies - read a vector of integers as 1/USER_HZ seconds
2692  * @table: the sysctl table
2693  * @write: %TRUE if this is a write to the sysctl file
2694  * @filp: the file structure
2695  * @buffer: the user buffer
2696  * @lenp: the size of the user buffer
2697  * @ppos: pointer to the file position
2698  *
2699  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2700  * values from/to the user buffer, treated as an ASCII string.
2701  * The values read are assumed to be in 1/USER_HZ seconds, and
2702  * are converted into jiffies.
2703  *
2704  * Returns 0 on success.
2705  */
proc_dointvec_userhz_jiffies(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2706 int proc_dointvec_userhz_jiffies(struct ctl_table *table, int write, struct file *filp,
2707 				 void __user *buffer, size_t *lenp, loff_t *ppos)
2708 {
2709     return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
2710 		    	    do_proc_dointvec_userhz_jiffies_conv,NULL);
2711 }
2712 
2713 /**
2714  * proc_dointvec_ms_jiffies - read a vector of integers as 1 milliseconds
2715  * @table: the sysctl table
2716  * @write: %TRUE if this is a write to the sysctl file
2717  * @filp: the file structure
2718  * @buffer: the user buffer
2719  * @lenp: the size of the user buffer
2720  * @ppos: file position
2721  * @ppos: the current position in the file
2722  *
2723  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2724  * values from/to the user buffer, treated as an ASCII string.
2725  * The values read are assumed to be in 1/1000 seconds, and
2726  * are converted into jiffies.
2727  *
2728  * Returns 0 on success.
2729  */
proc_dointvec_ms_jiffies(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2730 int proc_dointvec_ms_jiffies(struct ctl_table *table, int write, struct file *filp,
2731 			     void __user *buffer, size_t *lenp, loff_t *ppos)
2732 {
2733 	return do_proc_dointvec(table, write, filp, buffer, lenp, ppos,
2734 				do_proc_dointvec_ms_jiffies_conv, NULL);
2735 }
2736 
proc_do_cad_pid(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2737 static int proc_do_cad_pid(struct ctl_table *table, int write, struct file *filp,
2738 			   void __user *buffer, size_t *lenp, loff_t *ppos)
2739 {
2740 	struct pid *new_pid;
2741 	pid_t tmp;
2742 	int r;
2743 
2744 	tmp = pid_vnr(cad_pid);
2745 
2746 	r = __do_proc_dointvec(&tmp, table, write, filp, buffer,
2747 			       lenp, ppos, NULL, NULL);
2748 	if (r || !write)
2749 		return r;
2750 
2751 	new_pid = find_get_pid(tmp);
2752 	if (!new_pid)
2753 		return -ESRCH;
2754 
2755 	put_pid(xchg(&cad_pid, new_pid));
2756 	return 0;
2757 }
2758 
2759 #else /* CONFIG_PROC_FS */
2760 
proc_dostring(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2761 int proc_dostring(struct ctl_table *table, int write, struct file *filp,
2762 		  void __user *buffer, size_t *lenp, loff_t *ppos)
2763 {
2764 	return -ENOSYS;
2765 }
2766 
proc_dointvec(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2767 int proc_dointvec(struct ctl_table *table, int write, struct file *filp,
2768 		  void __user *buffer, size_t *lenp, loff_t *ppos)
2769 {
2770 	return -ENOSYS;
2771 }
2772 
proc_dointvec_minmax(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2773 int proc_dointvec_minmax(struct ctl_table *table, int write, struct file *filp,
2774 		    void __user *buffer, size_t *lenp, loff_t *ppos)
2775 {
2776 	return -ENOSYS;
2777 }
2778 
proc_dointvec_jiffies(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2779 int proc_dointvec_jiffies(struct ctl_table *table, int write, struct file *filp,
2780 		    void __user *buffer, size_t *lenp, loff_t *ppos)
2781 {
2782 	return -ENOSYS;
2783 }
2784 
proc_dointvec_userhz_jiffies(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2785 int proc_dointvec_userhz_jiffies(struct ctl_table *table, int write, struct file *filp,
2786 		    void __user *buffer, size_t *lenp, loff_t *ppos)
2787 {
2788 	return -ENOSYS;
2789 }
2790 
proc_dointvec_ms_jiffies(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2791 int proc_dointvec_ms_jiffies(struct ctl_table *table, int write, struct file *filp,
2792 			     void __user *buffer, size_t *lenp, loff_t *ppos)
2793 {
2794 	return -ENOSYS;
2795 }
2796 
proc_doulongvec_minmax(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2797 int proc_doulongvec_minmax(struct ctl_table *table, int write, struct file *filp,
2798 		    void __user *buffer, size_t *lenp, loff_t *ppos)
2799 {
2800 	return -ENOSYS;
2801 }
2802 
proc_doulongvec_ms_jiffies_minmax(struct ctl_table * table,int write,struct file * filp,void __user * buffer,size_t * lenp,loff_t * ppos)2803 int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int write,
2804 				      struct file *filp,
2805 				      void __user *buffer,
2806 				      size_t *lenp, loff_t *ppos)
2807 {
2808     return -ENOSYS;
2809 }
2810 
2811 
2812 #endif /* CONFIG_PROC_FS */
2813 
2814 
2815 #ifdef CONFIG_SYSCTL_SYSCALL
2816 /*
2817  * General sysctl support routines
2818  */
2819 
2820 /* The generic sysctl data routine (used if no strategy routine supplied) */
sysctl_data(struct ctl_table * table,void __user * oldval,size_t __user * oldlenp,void __user * newval,size_t newlen)2821 int sysctl_data(struct ctl_table *table,
2822 		void __user *oldval, size_t __user *oldlenp,
2823 		void __user *newval, size_t newlen)
2824 {
2825 	size_t len;
2826 
2827 	/* Get out of I don't have a variable */
2828 	if (!table->data || !table->maxlen)
2829 		return -ENOTDIR;
2830 
2831 	if (oldval && oldlenp) {
2832 		if (get_user(len, oldlenp))
2833 			return -EFAULT;
2834 		if (len) {
2835 			if (len > table->maxlen)
2836 				len = table->maxlen;
2837 			if (copy_to_user(oldval, table->data, len))
2838 				return -EFAULT;
2839 			if (put_user(len, oldlenp))
2840 				return -EFAULT;
2841 		}
2842 	}
2843 
2844 	if (newval && newlen) {
2845 		if (newlen > table->maxlen)
2846 			newlen = table->maxlen;
2847 
2848 		if (copy_from_user(table->data, newval, newlen))
2849 			return -EFAULT;
2850 	}
2851 	return 1;
2852 }
2853 
2854 /* The generic string strategy routine: */
sysctl_string(struct ctl_table * table,void __user * oldval,size_t __user * oldlenp,void __user * newval,size_t newlen)2855 int sysctl_string(struct ctl_table *table,
2856 		  void __user *oldval, size_t __user *oldlenp,
2857 		  void __user *newval, size_t newlen)
2858 {
2859 	if (!table->data || !table->maxlen)
2860 		return -ENOTDIR;
2861 
2862 	if (oldval && oldlenp) {
2863 		size_t bufsize;
2864 		if (get_user(bufsize, oldlenp))
2865 			return -EFAULT;
2866 		if (bufsize) {
2867 			size_t len = strlen(table->data), copied;
2868 
2869 			/* This shouldn't trigger for a well-formed sysctl */
2870 			if (len > table->maxlen)
2871 				len = table->maxlen;
2872 
2873 			/* Copy up to a max of bufsize-1 bytes of the string */
2874 			copied = (len >= bufsize) ? bufsize - 1 : len;
2875 
2876 			if (copy_to_user(oldval, table->data, copied) ||
2877 			    put_user(0, (char __user *)(oldval + copied)))
2878 				return -EFAULT;
2879 			if (put_user(len, oldlenp))
2880 				return -EFAULT;
2881 		}
2882 	}
2883 	if (newval && newlen) {
2884 		size_t len = newlen;
2885 		if (len > table->maxlen)
2886 			len = table->maxlen;
2887 		if(copy_from_user(table->data, newval, len))
2888 			return -EFAULT;
2889 		if (len == table->maxlen)
2890 			len--;
2891 		((char *) table->data)[len] = 0;
2892 	}
2893 	return 1;
2894 }
2895 
2896 /*
2897  * This function makes sure that all of the integers in the vector
2898  * are between the minimum and maximum values given in the arrays
2899  * table->extra1 and table->extra2, respectively.
2900  */
sysctl_intvec(struct ctl_table * table,void __user * oldval,size_t __user * oldlenp,void __user * newval,size_t newlen)2901 int sysctl_intvec(struct ctl_table *table,
2902 		void __user *oldval, size_t __user *oldlenp,
2903 		void __user *newval, size_t newlen)
2904 {
2905 
2906 	if (newval && newlen) {
2907 		int __user *vec = (int __user *) newval;
2908 		int *min = (int *) table->extra1;
2909 		int *max = (int *) table->extra2;
2910 		size_t length;
2911 		int i;
2912 
2913 		if (newlen % sizeof(int) != 0)
2914 			return -EINVAL;
2915 
2916 		if (!table->extra1 && !table->extra2)
2917 			return 0;
2918 
2919 		if (newlen > table->maxlen)
2920 			newlen = table->maxlen;
2921 		length = newlen / sizeof(int);
2922 
2923 		for (i = 0; i < length; i++) {
2924 			int value;
2925 			if (get_user(value, vec + i))
2926 				return -EFAULT;
2927 			if (min && value < min[i])
2928 				return -EINVAL;
2929 			if (max && value > max[i])
2930 				return -EINVAL;
2931 		}
2932 	}
2933 	return 0;
2934 }
2935 
2936 /* Strategy function to convert jiffies to seconds */
sysctl_jiffies(struct ctl_table * table,void __user * oldval,size_t __user * oldlenp,void __user * newval,size_t newlen)2937 int sysctl_jiffies(struct ctl_table *table,
2938 		void __user *oldval, size_t __user *oldlenp,
2939 		void __user *newval, size_t newlen)
2940 {
2941 	if (oldval && oldlenp) {
2942 		size_t olen;
2943 
2944 		if (get_user(olen, oldlenp))
2945 			return -EFAULT;
2946 		if (olen) {
2947 			int val;
2948 
2949 			if (olen < sizeof(int))
2950 				return -EINVAL;
2951 
2952 			val = *(int *)(table->data) / HZ;
2953 			if (put_user(val, (int __user *)oldval))
2954 				return -EFAULT;
2955 			if (put_user(sizeof(int), oldlenp))
2956 				return -EFAULT;
2957 		}
2958 	}
2959 	if (newval && newlen) {
2960 		int new;
2961 		if (newlen != sizeof(int))
2962 			return -EINVAL;
2963 		if (get_user(new, (int __user *)newval))
2964 			return -EFAULT;
2965 		*(int *)(table->data) = new*HZ;
2966 	}
2967 	return 1;
2968 }
2969 
2970 /* Strategy function to convert jiffies to seconds */
sysctl_ms_jiffies(struct ctl_table * table,void __user * oldval,size_t __user * oldlenp,void __user * newval,size_t newlen)2971 int sysctl_ms_jiffies(struct ctl_table *table,
2972 		void __user *oldval, size_t __user *oldlenp,
2973 		void __user *newval, size_t newlen)
2974 {
2975 	if (oldval && oldlenp) {
2976 		size_t olen;
2977 
2978 		if (get_user(olen, oldlenp))
2979 			return -EFAULT;
2980 		if (olen) {
2981 			int val;
2982 
2983 			if (olen < sizeof(int))
2984 				return -EINVAL;
2985 
2986 			val = jiffies_to_msecs(*(int *)(table->data));
2987 			if (put_user(val, (int __user *)oldval))
2988 				return -EFAULT;
2989 			if (put_user(sizeof(int), oldlenp))
2990 				return -EFAULT;
2991 		}
2992 	}
2993 	if (newval && newlen) {
2994 		int new;
2995 		if (newlen != sizeof(int))
2996 			return -EINVAL;
2997 		if (get_user(new, (int __user *)newval))
2998 			return -EFAULT;
2999 		*(int *)(table->data) = msecs_to_jiffies(new);
3000 	}
3001 	return 1;
3002 }
3003 
3004 
3005 
3006 #else /* CONFIG_SYSCTL_SYSCALL */
3007 
3008 
SYSCALL_DEFINE1(sysctl,struct __sysctl_args __user *,args)3009 SYSCALL_DEFINE1(sysctl, struct __sysctl_args __user *, args)
3010 {
3011 	struct __sysctl_args tmp;
3012 	int error;
3013 
3014 	if (copy_from_user(&tmp, args, sizeof(tmp)))
3015 		return -EFAULT;
3016 
3017 	error = deprecated_sysctl_warning(&tmp);
3018 
3019 	/* If no error reading the parameters then just -ENOSYS ... */
3020 	if (!error)
3021 		error = -ENOSYS;
3022 
3023 	return error;
3024 }
3025 
sysctl_data(struct ctl_table * table,void __user * oldval,size_t __user * oldlenp,void __user * newval,size_t newlen)3026 int sysctl_data(struct ctl_table *table,
3027 		  void __user *oldval, size_t __user *oldlenp,
3028 		  void __user *newval, size_t newlen)
3029 {
3030 	return -ENOSYS;
3031 }
3032 
sysctl_string(struct ctl_table * table,void __user * oldval,size_t __user * oldlenp,void __user * newval,size_t newlen)3033 int sysctl_string(struct ctl_table *table,
3034 		  void __user *oldval, size_t __user *oldlenp,
3035 		  void __user *newval, size_t newlen)
3036 {
3037 	return -ENOSYS;
3038 }
3039 
sysctl_intvec(struct ctl_table * table,void __user * oldval,size_t __user * oldlenp,void __user * newval,size_t newlen)3040 int sysctl_intvec(struct ctl_table *table,
3041 		void __user *oldval, size_t __user *oldlenp,
3042 		void __user *newval, size_t newlen)
3043 {
3044 	return -ENOSYS;
3045 }
3046 
sysctl_jiffies(struct ctl_table * table,void __user * oldval,size_t __user * oldlenp,void __user * newval,size_t newlen)3047 int sysctl_jiffies(struct ctl_table *table,
3048 		void __user *oldval, size_t __user *oldlenp,
3049 		void __user *newval, size_t newlen)
3050 {
3051 	return -ENOSYS;
3052 }
3053 
sysctl_ms_jiffies(struct ctl_table * table,void __user * oldval,size_t __user * oldlenp,void __user * newval,size_t newlen)3054 int sysctl_ms_jiffies(struct ctl_table *table,
3055 		void __user *oldval, size_t __user *oldlenp,
3056 		void __user *newval, size_t newlen)
3057 {
3058 	return -ENOSYS;
3059 }
3060 
3061 #endif /* CONFIG_SYSCTL_SYSCALL */
3062 
deprecated_sysctl_warning(struct __sysctl_args * args)3063 static int deprecated_sysctl_warning(struct __sysctl_args *args)
3064 {
3065 	static int msg_count;
3066 	int name[CTL_MAXNAME];
3067 	int i;
3068 
3069 	/* Check args->nlen. */
3070 	if (args->nlen < 0 || args->nlen > CTL_MAXNAME)
3071 		return -ENOTDIR;
3072 
3073 	/* Read in the sysctl name for better debug message logging */
3074 	for (i = 0; i < args->nlen; i++)
3075 		if (get_user(name[i], args->name + i))
3076 			return -EFAULT;
3077 
3078 	/* Ignore accesses to kernel.version */
3079 	if ((args->nlen == 2) && (name[0] == CTL_KERN) && (name[1] == KERN_VERSION))
3080 		return 0;
3081 
3082 	if (msg_count < 5) {
3083 		msg_count++;
3084 		printk(KERN_INFO
3085 			"warning: process `%s' used the deprecated sysctl "
3086 			"system call with ", current->comm);
3087 		for (i = 0; i < args->nlen; i++)
3088 			printk("%d.", name[i]);
3089 		printk("\n");
3090 	}
3091 	return 0;
3092 }
3093 
3094 /*
3095  * No sense putting this after each symbol definition, twice,
3096  * exception granted :-)
3097  */
3098 EXPORT_SYMBOL(proc_dointvec);
3099 EXPORT_SYMBOL(proc_dointvec_jiffies);
3100 EXPORT_SYMBOL(proc_dointvec_minmax);
3101 EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
3102 EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
3103 EXPORT_SYMBOL(proc_dostring);
3104 EXPORT_SYMBOL(proc_doulongvec_minmax);
3105 EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);
3106 EXPORT_SYMBOL(register_sysctl_table);
3107 EXPORT_SYMBOL(register_sysctl_paths);
3108 EXPORT_SYMBOL(sysctl_intvec);
3109 EXPORT_SYMBOL(sysctl_jiffies);
3110 EXPORT_SYMBOL(sysctl_ms_jiffies);
3111 EXPORT_SYMBOL(sysctl_string);
3112 EXPORT_SYMBOL(sysctl_data);
3113 EXPORT_SYMBOL(unregister_sysctl_table);
3114