xref: /linux/include/linux/sysctl.h (revision 23b0f90ba871f096474e1c27c3d14f455189d2d9)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * sysctl.h: General linux system control interface
4  *
5  * Begun 24 March 1995, Stephen Tweedie
6  *
7  ****************************************************************
8  ****************************************************************
9  **
10  **  WARNING:
11  **  The values in this file are exported to user space via
12  **  the sysctl() binary interface.  Do *NOT* change the
13  **  numbering of any existing values here, and do not change
14  **  any numbers within any one set of values.  If you have to
15  **  redefine an existing interface, use a new number for it.
16  **  The kernel will then return -ENOTDIR to any application using
17  **  the old binary interface.
18  **
19  ****************************************************************
20  ****************************************************************
21  */
22 #ifndef _LINUX_SYSCTL_H
23 #define _LINUX_SYSCTL_H
24 
25 #include <linux/list.h>
26 #include <linux/rcupdate.h>
27 #include <linux/wait.h>
28 #include <linux/rbtree.h>
29 #include <linux/uidgid.h>
30 #include <uapi/linux/sysctl.h>
31 
32 /* For the /proc/sys support */
33 struct completion;
34 struct ctl_table;
35 struct nsproxy;
36 struct ctl_table_root;
37 struct ctl_table_header;
38 struct ctl_dir;
39 
40 /* Keep the same order as in fs/proc/proc_sysctl.c */
41 #define SYSCTL_ZERO			((void *)&sysctl_vals[0])
42 #define SYSCTL_ONE			((void *)&sysctl_vals[1])
43 #define SYSCTL_TWO			((void *)&sysctl_vals[2])
44 #define SYSCTL_THREE			((void *)&sysctl_vals[3])
45 #define SYSCTL_FOUR			((void *)&sysctl_vals[4])
46 #define SYSCTL_ONE_HUNDRED		((void *)&sysctl_vals[5])
47 #define SYSCTL_TWO_HUNDRED		((void *)&sysctl_vals[6])
48 #define SYSCTL_ONE_THOUSAND		((void *)&sysctl_vals[7])
49 #define SYSCTL_THREE_THOUSAND		((void *)&sysctl_vals[8])
50 #define SYSCTL_INT_MAX			((void *)&sysctl_vals[9])
51 
52 /* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
53 #define SYSCTL_MAXOLDUID		((void *)&sysctl_vals[10])
54 #define SYSCTL_NEG_ONE			((void *)&sysctl_vals[11])
55 
56 extern const int sysctl_vals[];
57 
58 #define SYSCTL_LONG_ZERO	((void *)&sysctl_long_vals[0])
59 #define SYSCTL_LONG_ONE		((void *)&sysctl_long_vals[1])
60 #define SYSCTL_LONG_MAX		((void *)&sysctl_long_vals[2])
61 
62 /**
63  *
64  * "dir" originates from read_iter (dir = 0) or write_iter (dir = 1)
65  * in the file_operations struct at proc/proc_sysctl.c. Its value means
66  * one of two things for sysctl:
67  * 1. SYSCTL_USER_TO_KERN(dir) Writing to an internal kernel variable from user
68  *                             space (dir > 0)
69  * 2. SYSCTL_KERN_TO_USER(dir) Writing to a user space buffer from a kernel
70  *                             variable (dir == 0).
71  */
72 #define SYSCTL_USER_TO_KERN(dir) (!!(dir))
73 #define SYSCTL_KERN_TO_USER(dir) (!dir)
74 
75 extern const unsigned long sysctl_long_vals[];
76 
77 typedef int proc_handler(const struct ctl_table *ctl, int write, void *buffer,
78 		size_t *lenp, loff_t *ppos);
79 
80 int proc_dostring(const struct ctl_table *, int, void *, size_t *, loff_t *);
81 int proc_dobool(const struct ctl_table *table, int write, void *buffer,
82 		size_t *lenp, loff_t *ppos);
83 
84 int proc_dointvec(const struct ctl_table *, int, void *, size_t *, loff_t *);
85 int proc_dointvec_minmax(const struct ctl_table *table, int dir, void *buffer,
86 			 size_t *lenp, loff_t *ppos);
87 int proc_dointvec_conv(const struct ctl_table *table, int dir, void *buffer,
88 		       size_t *lenp, loff_t *ppos,
89 		       int (*conv)(bool *negp, unsigned long *u_ptr, int *k_ptr,
90 				   int dir, const struct ctl_table *table));
91 int proc_int_k2u_conv_kop(ulong *u_ptr, const int *k_ptr, bool *negp,
92 			  ulong (*k_ptr_op)(const ulong));
93 int proc_int_u2k_conv_uop(const ulong *u_ptr, int *k_ptr, const bool *negp,
94 			  ulong (*u_ptr_op)(const ulong));
95 int proc_int_conv(bool *negp, ulong *u_ptr, int *k_ptr, int dir,
96 		  const struct ctl_table *tbl, bool k_ptr_range_check,
97 		  int (*user_to_kern)(const bool *negp, const ulong *u_ptr, int *k_ptr),
98 		  int (*kern_to_user)(bool *negp, ulong *u_ptr, const int *k_ptr));
99 
100 int proc_douintvec(const struct ctl_table *, int, void *, size_t *, loff_t *);
101 int proc_douintvec_minmax(const struct ctl_table *table, int write, void *buffer,
102 		size_t *lenp, loff_t *ppos);
103 int proc_douintvec_conv(const struct ctl_table *table, int write, void *buffer,
104 			size_t *lenp, loff_t *ppos,
105 			int (*conv)(unsigned long *lvalp, unsigned int *valp,
106 				    int write, const struct ctl_table *table));
107 int proc_uint_k2u_conv(ulong *u_ptr, const uint *k_ptr);
108 int proc_uint_u2k_conv_uop(const ulong *u_ptr, uint *k_ptr,
109 			   ulong (*u_ptr_op)(const ulong));
110 int proc_uint_conv(ulong *u_ptr, uint *k_ptr, int dir,
111 		   const struct ctl_table *tbl, bool k_ptr_range_check,
112 		   int (*user_to_kern)(const ulong *u_ptr, uint *k_ptr),
113 		   int (*kern_to_user)(ulong *u_ptr, const uint *k_ptr));
114 
115 int proc_dou8vec_minmax(const struct ctl_table *table, int write, void *buffer,
116 			size_t *lenp, loff_t *ppos);
117 int proc_doulongvec_minmax(const struct ctl_table *, int, void *, size_t *, loff_t *);
118 int proc_doulongvec_minmax_conv(const struct ctl_table *table, int dir,
119 				void *buffer, size_t *lenp, loff_t *ppos,
120 				unsigned long convmul, unsigned long convdiv);
121 int proc_do_large_bitmap(const struct ctl_table *, int, void *, size_t *, loff_t *);
122 int proc_do_static_key(const struct ctl_table *table, int write, void *buffer,
123 		size_t *lenp, loff_t *ppos);
124 
125 /*
126  * Register a set of sysctl names by calling register_sysctl
127  * with an initialised array of struct ctl_table's.
128  *
129  * sysctl names can be mirrored automatically under /proc/sys.  The
130  * procname supplied controls /proc naming.
131  *
132  * The table's mode will be honoured for proc-fs access.
133  *
134  * Leaf nodes in the sysctl tree will be represented by a single file
135  * under /proc; non-leaf nodes will be represented by directories.  A
136  * null procname disables /proc mirroring at this node.
137  *
138  * The data and maxlen fields of the ctl_table
139  * struct enable minimal validation of the values being written to be
140  * performed, and the mode field allows minimal authentication.
141  *
142  * There must be a proc_handler routine for any terminal nodes
143  * mirrored under /proc/sys (non-terminals are handled by a built-in
144  * directory handler).  Several default handlers are available to
145  * cover common cases.
146  */
147 
148 /* Support for userspace poll() to watch for changes */
149 struct ctl_table_poll {
150 	atomic_t event;
151 	wait_queue_head_t wait;
152 };
153 
proc_sys_poll_event(struct ctl_table_poll * poll)154 static inline void *proc_sys_poll_event(struct ctl_table_poll *poll)
155 {
156 	return (void *)(unsigned long)atomic_read(&poll->event);
157 }
158 
159 #define __CTL_TABLE_POLL_INITIALIZER(name) {				\
160 	.event = ATOMIC_INIT(0),					\
161 	.wait = __WAIT_QUEUE_HEAD_INITIALIZER(name.wait) }
162 
163 #define DEFINE_CTL_TABLE_POLL(name)					\
164 	struct ctl_table_poll name = __CTL_TABLE_POLL_INITIALIZER(name)
165 
166 /* A sysctl table is an array of struct ctl_table: */
167 struct ctl_table {
168 	const char *procname;		/* Text ID for /proc/sys */
169 	void *data;
170 	int maxlen;
171 	umode_t mode;
172 	proc_handler *proc_handler;	/* Callback for text formatting */
173 	struct ctl_table_poll *poll;
174 	void *extra1;
175 	void *extra2;
176 } __randomize_layout;
177 
178 struct ctl_node {
179 	struct rb_node node;
180 	struct ctl_table_header *header;
181 };
182 
183 /**
184  * struct ctl_table_header - maintains dynamic lists of struct ctl_table trees
185  * @ctl_table: pointer to the first element in ctl_table array
186  * @ctl_table_size: number of elements pointed by @ctl_table
187  * @used: The entry will never be touched when equal to 0.
188  * @count: Upped every time something is added to @inodes and downed every time
189  *         something is removed from inodes
190  * @nreg: When nreg drops to 0 the ctl_table_header will be unregistered.
191  * @rcu: Delays the freeing of the inode. Introduced with "unfuck proc_sysctl ->d_compare()"
192  *
193  * @type: Enumeration to differentiate between ctl target types
194  * @type.SYSCTL_TABLE_TYPE_DEFAULT: ctl target with no special considerations
195  * @type.SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY: Identifies a permanently empty dir
196  *                                            target to serve as a mount point
197  */
198 struct ctl_table_header {
199 	union {
200 		struct {
201 			const struct ctl_table *ctl_table;
202 			int ctl_table_size;
203 			int used;
204 			int count;
205 			int nreg;
206 		};
207 		struct rcu_head rcu;
208 	};
209 	struct completion *unregistering;
210 	const struct ctl_table *ctl_table_arg;
211 	struct ctl_table_root *root;
212 	struct ctl_table_set *set;
213 	struct ctl_dir *parent;
214 	struct ctl_node *node;
215 	struct hlist_head inodes; /* head for proc_inode->sysctl_inodes */
216 	enum {
217 		SYSCTL_TABLE_TYPE_DEFAULT,
218 		SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY,
219 	} type;
220 };
221 
222 struct ctl_dir {
223 	/* Header must be at the start of ctl_dir */
224 	struct ctl_table_header header;
225 	struct rb_root root;
226 };
227 
228 struct ctl_table_set {
229 	int (*is_seen)(struct ctl_table_set *);
230 	struct ctl_dir dir;
231 };
232 
233 struct ctl_table_root {
234 	struct ctl_table_set default_set;
235 	struct ctl_table_set *(*lookup)(struct ctl_table_root *root);
236 	void (*set_ownership)(struct ctl_table_header *head,
237 			      kuid_t *uid, kgid_t *gid);
238 	int (*permissions)(struct ctl_table_header *head, const struct ctl_table *table);
239 };
240 
241 #define register_sysctl(path, table)	\
242 	register_sysctl_sz(path, table, ARRAY_SIZE(table))
243 
244 #ifdef CONFIG_SYSCTL
245 
246 void proc_sys_poll_notify(struct ctl_table_poll *poll);
247 
248 extern void setup_sysctl_set(struct ctl_table_set *p,
249 	struct ctl_table_root *root,
250 	int (*is_seen)(struct ctl_table_set *));
251 extern void retire_sysctl_set(struct ctl_table_set *set);
252 
253 struct ctl_table_header *__register_sysctl_table(
254 	struct ctl_table_set *set,
255 	const char *path, const struct ctl_table *table, size_t table_size);
256 struct ctl_table_header *register_sysctl_sz(const char *path, const struct ctl_table *table,
257 					    size_t table_size);
258 void unregister_sysctl_table(struct ctl_table_header * table);
259 
260 extern int sysctl_init_bases(void);
261 extern void __register_sysctl_init(const char *path, const struct ctl_table *table,
262 				 const char *table_name, size_t table_size);
263 #define register_sysctl_init(path, table)	\
264 	__register_sysctl_init(path, table, #table, ARRAY_SIZE(table))
265 extern struct ctl_table_header *register_sysctl_mount_point(const char *path);
266 
267 void do_sysctl_args(void);
268 bool sysctl_is_alias(char *param);
269 
270 extern int unaligned_enabled;
271 extern int no_unaligned_warning;
272 
273 #else /* CONFIG_SYSCTL */
274 
register_sysctl_init(const char * path,const struct ctl_table * table)275 static inline void register_sysctl_init(const char *path, const struct ctl_table *table)
276 {
277 }
278 
register_sysctl_mount_point(const char * path)279 static inline struct ctl_table_header *register_sysctl_mount_point(const char *path)
280 {
281 	return NULL;
282 }
283 
register_sysctl_sz(const char * path,const struct ctl_table * table,size_t table_size)284 static inline struct ctl_table_header *register_sysctl_sz(const char *path,
285 							  const struct ctl_table *table,
286 							  size_t table_size)
287 {
288 	return NULL;
289 }
290 
unregister_sysctl_table(struct ctl_table_header * table)291 static inline void unregister_sysctl_table(struct ctl_table_header * table)
292 {
293 }
294 
setup_sysctl_set(struct ctl_table_set * p,struct ctl_table_root * root,int (* is_seen)(struct ctl_table_set *))295 static inline void setup_sysctl_set(struct ctl_table_set *p,
296 	struct ctl_table_root *root,
297 	int (*is_seen)(struct ctl_table_set *))
298 {
299 }
300 
do_sysctl_args(void)301 static inline void do_sysctl_args(void)
302 {
303 }
304 
sysctl_is_alias(char * param)305 static inline bool sysctl_is_alias(char *param)
306 {
307 	return false;
308 }
309 #endif /* CONFIG_SYSCTL */
310 
311 #endif /* _LINUX_SYSCTL_H */
312