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 dir, void *buf,
78 size_t *lenp, loff_t *ppos);
79
80 /* proc_handler functions */
81 int proc_dostring(const struct ctl_table *ctl, int dir, void *buf, size_t *lenp,
82 loff_t *ppos);
83 int proc_dobool(const struct ctl_table *ctl, int dir, void *buf, size_t *lenp,
84 loff_t *ppos);
85 int proc_dointvec(const struct ctl_table *ctl, int dir, void *buf, size_t *lenp,
86 loff_t *ppos);
87 int proc_dointvec_minmax(const struct ctl_table *ctl, int dir, void *buf,
88 size_t *lenp, loff_t *ppos);
89 int proc_douintvec(const struct ctl_table *ctl, int dir, void *buf, size_t *lenp,
90 loff_t *ppos);
91 int proc_douintvec_minmax(const struct ctl_table *ctl, int dir, void *buf,
92 size_t *lenp, loff_t *ppos);
93 int proc_dou8vec_minmax(const struct ctl_table *ctl, int dir, void *buf,
94 size_t *lenp, loff_t *ppos);
95 int proc_doulongvec_minmax(const struct ctl_table *ctl, int dir, void *buf,
96 size_t *lenp, loff_t *ppos);
97 int proc_do_large_bitmap(const struct ctl_table *ctl, int dir, void *buf,
98 size_t *lenp, loff_t *ppos);
99 int proc_do_static_key(const struct ctl_table *ctl, int dir, void *buf,
100 size_t *lenp, loff_t *ppos);
101
102 /*
103 * proc_handler aggregators
104 *
105 * Create a proc_handler with a custom converter. Use when the user space
106 * value is a transformation of the kernel value. Cannot be passed as
107 * proc_handlers.
108 *
109 * Example of creating your custom proc handler:
110 * int custom_converter(bool *negp, ulong *u_ptr, uint *k_ptr,
111 * int dir, const struct ctl_table *ctl) {...}
112 * int custom_proc_handler(const struct ctl_table *ctl, int dir,
113 * void *buf, size_t *lenp, loff_t *ppos
114 * { return proc_dointvec_conv(ctl, dir, buf, lenp, ppos, custom_converter); }
115 */
116 int proc_dointvec_conv(const struct ctl_table *ctl, int dir, void *buf,
117 size_t *lenp, loff_t *ppos,
118 int (*conv)(bool *negp, unsigned long *u_ptr, int *k_ptr,
119 int dir, const struct ctl_table *ctl));
120 int proc_douintvec_conv(const struct ctl_table *ctl, int dir, void *buf,
121 size_t *lenp, loff_t *ppos,
122 int (*conv)(bool *negp, ulong *u_ptr, uint *k_ptr,
123 int dir, const struct ctl_table *ctl));
124 int proc_doulongvec_conv(const struct ctl_table *ctl, int dir, void *buf,
125 size_t *lenp, loff_t *ppos,
126 int (*conv)(bool *negp, ulong *u_ptr, ulong *k_ptr,
127 int dir, const struct ctl_table *ctl));
128
129 /*
130 * bi-directional converter functions
131 *
132 * Specify the converter function for both directions (user to kernel & kernel
133 * to user). Use when you want to change the value of the variable before
134 * assignment. Used to create custom proc_handler aggregators.
135 *
136 * Example of creating your custom bi-directional converter:
137 * int custom_u2k(ulong *u_ptr, const uint *k_ptr) { ... }
138 * int custom_converter(bool *negp, ulong *u_ptr, uint *k_ptr,
139 * int dir, const struct ctl_table *ctl)
140 * { return proc_uint_conv(u_ptr, k_ptr, dir, ctl, true,
141 * custom_u2k, proc_uint_k2u_conv}
142 */
143 int proc_int_conv(bool *negp, ulong *u_ptr, int *k_ptr, int dir,
144 const struct ctl_table *tbl, bool k_ptr_range_check,
145 int (*user_to_kern)(const bool *negp, const ulong *u_ptr, int *k_ptr),
146 int (*kern_to_user)(bool *negp, ulong *u_ptr, const int *k_ptr));
147 int proc_uint_conv(ulong *u_ptr, uint *k_ptr, int dir,
148 const struct ctl_table *tbl, bool k_ptr_range_check,
149 int (*user_to_kern)(const ulong *u_ptr, uint *k_ptr),
150 int (*kern_to_user)(ulong *u_ptr, const uint *k_ptr));
151 int proc_ulong_conv(ulong *u_ptr, ulong *k_ptr, int dir,
152 const struct ctl_table *tbl, bool k_ptr_range_check,
153 int (*user_to_kern)(const ulong *u_ptr, ulong *k_ptr),
154 int (*kern_to_user)(ulong *u_ptr, const ulong *k_ptr));
155
156 /*
157 * uni-directional converter functions
158 *
159 * Specify the converter function for one directions (user to kernel or
160 * kernel to user). Use to call the actual value conversion. Used to Create
161 * bi-directional converters.
162 *
163 * Example of creating a uni-directional converter:
164 * ulong op(const ulong val) { ... }
165 * int custom_unidir_conv(ulong *u_ptr, const uint *k_ptr)
166 * { return proc_uint_k2u_conv_kop(u_ptr, k_ptr, op); }
167 */
168 int proc_int_k2u_conv_kop(ulong *u_ptr, const int *k_ptr, bool *negp,
169 ulong (*k_ptr_op)(const ulong));
170 int proc_int_u2k_conv_uop(const ulong *u_ptr, int *k_ptr, const bool *negp,
171 ulong (*u_ptr_op)(const ulong));
172 int proc_uint_k2u_conv(ulong *u_ptr, const uint *k_ptr);
173 int proc_uint_u2k_conv_uop(const ulong *u_ptr, uint *k_ptr,
174 ulong (*u_ptr_op)(const ulong));
175 int proc_ulong_u2k_conv_uop(const ulong *u_ptr, ulong *k_ptr,
176 ulong (*u_ptr_op)(const ulong));
177 int proc_ulong_k2u_conv_kop(ulong *u_ptr, const ulong *k_ptr,
178 ulong (*k_ptr_op)(const ulong));
179
180 /*
181 * Register a set of sysctl names by calling register_sysctl
182 * with an initialised array of struct ctl_table's.
183 *
184 * sysctl names can be mirrored automatically under /proc/sys. The
185 * procname supplied controls /proc naming.
186 *
187 * The table's mode will be honoured for proc-fs access.
188 *
189 * Leaf nodes in the sysctl tree will be represented by a single file
190 * under /proc; non-leaf nodes will be represented by directories. A
191 * null procname disables /proc mirroring at this node.
192 *
193 * The data and maxlen fields of the ctl_table
194 * struct enable minimal validation of the values being written to be
195 * performed, and the mode field allows minimal authentication.
196 *
197 * There must be a proc_handler routine for any terminal nodes
198 * mirrored under /proc/sys (non-terminals are handled by a built-in
199 * directory handler). Several default handlers are available to
200 * cover common cases.
201 */
202
203 /* Support for userspace poll() to watch for changes */
204 struct ctl_table_poll {
205 atomic_t event;
206 wait_queue_head_t wait;
207 };
208
proc_sys_poll_event(struct ctl_table_poll * poll)209 static inline void *proc_sys_poll_event(struct ctl_table_poll *poll)
210 {
211 return (void *)(unsigned long)atomic_read(&poll->event);
212 }
213
214 #define __CTL_TABLE_POLL_INITIALIZER(name) { \
215 .event = ATOMIC_INIT(0), \
216 .wait = __WAIT_QUEUE_HEAD_INITIALIZER(name.wait) }
217
218 #define DEFINE_CTL_TABLE_POLL(name) \
219 struct ctl_table_poll name = __CTL_TABLE_POLL_INITIALIZER(name)
220
221 /* A sysctl table is an array of struct ctl_table: */
222 struct ctl_table {
223 const char *procname; /* Text ID for /proc/sys */
224 void *data;
225 int maxlen;
226 umode_t mode;
227 proc_handler *proc_handler; /* Callback for text formatting */
228 struct ctl_table_poll *poll;
229 void *extra1;
230 void *extra2;
231 } __randomize_layout;
232
233 struct ctl_node {
234 struct rb_node node;
235 struct ctl_table_header *header;
236 };
237
238 /**
239 * struct ctl_table_header - maintains dynamic lists of struct ctl_table trees
240 * @ctl_table: pointer to the first element in ctl_table array
241 * @ctl_table_size: number of elements pointed by @ctl_table
242 * @used: The entry will never be touched when equal to 0.
243 * @count: Upped every time something is added to @inodes and downed every time
244 * something is removed from inodes
245 * @nreg: When nreg drops to 0 the ctl_table_header will be unregistered.
246 * @rcu: Delays the freeing of the inode. Introduced with "unfuck proc_sysctl ->d_compare()"
247 *
248 * @type: Enumeration to differentiate between ctl target types:
249 * type.SYSCTL_TABLE_TYPE_DEFAULT: ctl target with no special considerations
250 * type.SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY: Identifies a permanently empty dir
251 * target to serve as a mount point
252 */
253 struct ctl_table_header {
254 union {
255 struct {
256 const struct ctl_table *ctl_table;
257 int ctl_table_size;
258 int used;
259 int count;
260 int nreg;
261 };
262 struct rcu_head rcu;
263 };
264 struct completion *unregistering;
265 const struct ctl_table *ctl_table_arg;
266 struct ctl_table_root *root;
267 struct ctl_table_set *set;
268 struct ctl_dir *parent;
269 struct ctl_node *node;
270 struct hlist_head inodes; /* head for proc_inode->sysctl_inodes */
271 enum {
272 SYSCTL_TABLE_TYPE_DEFAULT,
273 SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY,
274 } type;
275 };
276
277 struct ctl_dir {
278 /* Header must be at the start of ctl_dir */
279 struct ctl_table_header header;
280 struct rb_root root;
281 };
282
283 struct ctl_table_set {
284 int (*is_seen)(struct ctl_table_set *);
285 struct ctl_dir dir;
286 };
287
288 struct ctl_table_root {
289 struct ctl_table_set default_set;
290 struct ctl_table_set *(*lookup)(struct ctl_table_root *root);
291 void (*set_ownership)(struct ctl_table_header *head,
292 kuid_t *uid, kgid_t *gid);
293 int (*permissions)(struct ctl_table_header *head, const struct ctl_table *table);
294 };
295
296 #define register_sysctl(path, table) \
297 register_sysctl_sz(path, table, ARRAY_SIZE(table))
298
299 #ifdef CONFIG_SYSCTL
300
301 void proc_sys_poll_notify(struct ctl_table_poll *poll);
302
303 extern void setup_sysctl_set(struct ctl_table_set *p,
304 struct ctl_table_root *root,
305 int (*is_seen)(struct ctl_table_set *));
306 extern void retire_sysctl_set(struct ctl_table_set *set);
307
308 struct ctl_table_header *__register_sysctl_table(
309 struct ctl_table_set *set,
310 const char *path, const struct ctl_table *table, size_t table_size);
311 struct ctl_table_header *register_sysctl_sz(const char *path, const struct ctl_table *table,
312 size_t table_size);
313 void unregister_sysctl_table(struct ctl_table_header * table);
314
315 extern int sysctl_init_bases(void);
316 extern void __register_sysctl_init(const char *path, const struct ctl_table *table,
317 const char *table_name, size_t table_size);
318 #define register_sysctl_init(path, table) \
319 __register_sysctl_init(path, table, #table, ARRAY_SIZE(table))
320 extern struct ctl_table_header *register_sysctl_mount_point(const char *path);
321
322 void do_sysctl_args(void);
323 bool sysctl_is_alias(char *param);
324
325 extern int unaligned_enabled;
326 extern int no_unaligned_warning;
327
328 #else /* CONFIG_SYSCTL */
329
register_sysctl_init(const char * path,const struct ctl_table * table)330 static inline void register_sysctl_init(const char *path, const struct ctl_table *table)
331 {
332 }
333
register_sysctl_mount_point(const char * path)334 static inline struct ctl_table_header *register_sysctl_mount_point(const char *path)
335 {
336 return NULL;
337 }
338
register_sysctl_sz(const char * path,const struct ctl_table * table,size_t table_size)339 static inline struct ctl_table_header *register_sysctl_sz(const char *path,
340 const struct ctl_table *table,
341 size_t table_size)
342 {
343 return NULL;
344 }
345
unregister_sysctl_table(struct ctl_table_header * table)346 static inline void unregister_sysctl_table(struct ctl_table_header * table)
347 {
348 }
349
setup_sysctl_set(struct ctl_table_set * p,struct ctl_table_root * root,int (* is_seen)(struct ctl_table_set *))350 static inline void setup_sysctl_set(struct ctl_table_set *p,
351 struct ctl_table_root *root,
352 int (*is_seen)(struct ctl_table_set *))
353 {
354 }
355
do_sysctl_args(void)356 static inline void do_sysctl_args(void)
357 {
358 }
359
sysctl_is_alias(char * param)360 static inline bool sysctl_is_alias(char *param)
361 {
362 return false;
363 }
364 #endif /* CONFIG_SYSCTL */
365
366 #endif /* _LINUX_SYSCTL_H */
367