at master 13 kB view raw
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 */ 33struct completion; 34struct ctl_table; 35struct nsproxy; 36struct ctl_table_root; 37struct ctl_table_header; 38struct 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 56extern 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#define SYSCTL_CONV_IDENTITY(val) (val) 63/** 64 * 65 * "dir" originates from read_iter (dir = 0) or write_iter (dir = 1) 66 * in the file_operations struct at proc/proc_sysctl.c. Its value means 67 * one of two things for sysctl: 68 * 1. SYSCTL_USER_TO_KERN(dir) Writing to an internal kernel variable from user 69 * space (dir > 0) 70 * 2. SYSCTL_KERN_TO_USER(dir) Writing to a user space buffer from a kernel 71 * variable (dir == 0). 72 */ 73#define SYSCTL_USER_TO_KERN(dir) (!!(dir)) 74#define SYSCTL_KERN_TO_USER(dir) (!dir) 75 76#define SYSCTL_USER_TO_KERN_INT_CONV(name, u_ptr_op) \ 77int sysctl_user_to_kern_int_conv##name(const bool *negp, \ 78 const unsigned long *u_ptr,\ 79 int *k_ptr) \ 80{ \ 81 unsigned long u = u_ptr_op(*u_ptr); \ 82 if (*negp) { \ 83 if (u > (unsigned long) INT_MAX + 1) \ 84 return -EINVAL; \ 85 WRITE_ONCE(*k_ptr, -u); \ 86 } else { \ 87 if (u > (unsigned long) INT_MAX) \ 88 return -EINVAL; \ 89 WRITE_ONCE(*k_ptr, u); \ 90 } \ 91 return 0; \ 92} 93 94#define SYSCTL_KERN_TO_USER_INT_CONV(name, k_ptr_op) \ 95int sysctl_kern_to_user_int_conv##name(bool *negp, \ 96 unsigned long *u_ptr, \ 97 const int *k_ptr) \ 98{ \ 99 int val = READ_ONCE(*k_ptr); \ 100 if (val < 0) { \ 101 *negp = true; \ 102 *u_ptr = -k_ptr_op((unsigned long)val); \ 103 } else { \ 104 *negp = false; \ 105 *u_ptr = k_ptr_op((unsigned long)val); \ 106 } \ 107 return 0; \ 108} 109 110/** 111 * To range check on a converted value, use a temp k_ptr 112 * When checking range, value should be within (tbl->extra1, tbl->extra2) 113 */ 114#define SYSCTL_INT_CONV_CUSTOM(name, user_to_kern, kern_to_user, \ 115 k_ptr_range_check) \ 116int do_proc_int_conv##name(bool *negp, unsigned long *u_ptr, int *k_ptr,\ 117 int dir, const struct ctl_table *tbl) \ 118{ \ 119 if (SYSCTL_KERN_TO_USER(dir)) \ 120 return kern_to_user(negp, u_ptr, k_ptr); \ 121 \ 122 if (k_ptr_range_check) { \ 123 int tmp_k, ret; \ 124 if (!tbl) \ 125 return -EINVAL; \ 126 ret = user_to_kern(negp, u_ptr, &tmp_k); \ 127 if (ret) \ 128 return ret; \ 129 if ((tbl->extra1 && *(int *)tbl->extra1 > tmp_k) || \ 130 (tbl->extra2 && *(int *)tbl->extra2 < tmp_k)) \ 131 return -EINVAL; \ 132 WRITE_ONCE(*k_ptr, tmp_k); \ 133 } else \ 134 return user_to_kern(negp, u_ptr, k_ptr); \ 135 return 0; \ 136} 137 138#define SYSCTL_USER_TO_KERN_UINT_CONV(name, u_ptr_op) \ 139int sysctl_user_to_kern_uint_conv##name(const unsigned long *u_ptr,\ 140 unsigned int *k_ptr) \ 141{ \ 142 unsigned long u = u_ptr_op(*u_ptr); \ 143 if (u > UINT_MAX) \ 144 return -EINVAL; \ 145 WRITE_ONCE(*k_ptr, u); \ 146 return 0; \ 147} 148 149#define SYSCTL_UINT_CONV_CUSTOM(name, user_to_kern, kern_to_user, \ 150 k_ptr_range_check) \ 151int do_proc_uint_conv##name(unsigned long *u_ptr, unsigned int *k_ptr, \ 152 int dir, const struct ctl_table *tbl) \ 153{ \ 154 if (SYSCTL_KERN_TO_USER(dir)) \ 155 return kern_to_user(u_ptr, k_ptr); \ 156 \ 157 if (k_ptr_range_check) { \ 158 unsigned int tmp_k; \ 159 int ret; \ 160 if (!tbl) \ 161 return -EINVAL; \ 162 ret = user_to_kern(u_ptr, &tmp_k); \ 163 if (ret) \ 164 return ret; \ 165 if ((tbl->extra1 && \ 166 *(unsigned int *)tbl->extra1 > tmp_k) || \ 167 (tbl->extra2 && \ 168 *(unsigned int *)tbl->extra2 < tmp_k)) \ 169 return -ERANGE; \ 170 WRITE_ONCE(*k_ptr, tmp_k); \ 171 } else \ 172 return user_to_kern(u_ptr, k_ptr); \ 173 return 0; \ 174} 175 176 177extern const unsigned long sysctl_long_vals[]; 178 179typedef int proc_handler(const struct ctl_table *ctl, int write, void *buffer, 180 size_t *lenp, loff_t *ppos); 181 182int proc_dostring(const struct ctl_table *, int, void *, size_t *, loff_t *); 183int proc_dobool(const struct ctl_table *table, int write, void *buffer, 184 size_t *lenp, loff_t *ppos); 185int proc_dointvec(const struct ctl_table *, int, void *, size_t *, loff_t *); 186int proc_dointvec_minmax(const struct ctl_table *table, int dir, void *buffer, 187 size_t *lenp, loff_t *ppos); 188int proc_dointvec_conv(const struct ctl_table *table, int dir, void *buffer, 189 size_t *lenp, loff_t *ppos, 190 int (*conv)(bool *negp, unsigned long *u_ptr, int *k_ptr, 191 int dir, const struct ctl_table *table)); 192int proc_douintvec(const struct ctl_table *, int, void *, size_t *, loff_t *); 193int proc_douintvec_minmax(const struct ctl_table *table, int write, void *buffer, 194 size_t *lenp, loff_t *ppos); 195int proc_douintvec_conv(const struct ctl_table *table, int write, void *buffer, 196 size_t *lenp, loff_t *ppos, 197 int (*conv)(unsigned long *lvalp, unsigned int *valp, 198 int write, const struct ctl_table *table)); 199 200int proc_dou8vec_minmax(const struct ctl_table *table, int write, void *buffer, 201 size_t *lenp, loff_t *ppos); 202int proc_doulongvec_minmax(const struct ctl_table *, int, void *, size_t *, loff_t *); 203int proc_doulongvec_minmax_conv(const struct ctl_table *table, int dir, 204 void *buffer, size_t *lenp, loff_t *ppos, 205 unsigned long convmul, unsigned long convdiv); 206int proc_do_large_bitmap(const struct ctl_table *, int, void *, size_t *, loff_t *); 207int proc_do_static_key(const struct ctl_table *table, int write, void *buffer, 208 size_t *lenp, loff_t *ppos); 209int sysctl_kern_to_user_uint_conv(unsigned long *u_ptr, const unsigned int *k_ptr); 210 211/* 212 * Register a set of sysctl names by calling register_sysctl 213 * with an initialised array of struct ctl_table's. 214 * 215 * sysctl names can be mirrored automatically under /proc/sys. The 216 * procname supplied controls /proc naming. 217 * 218 * The table's mode will be honoured for proc-fs access. 219 * 220 * Leaf nodes in the sysctl tree will be represented by a single file 221 * under /proc; non-leaf nodes will be represented by directories. A 222 * null procname disables /proc mirroring at this node. 223 * 224 * The data and maxlen fields of the ctl_table 225 * struct enable minimal validation of the values being written to be 226 * performed, and the mode field allows minimal authentication. 227 * 228 * There must be a proc_handler routine for any terminal nodes 229 * mirrored under /proc/sys (non-terminals are handled by a built-in 230 * directory handler). Several default handlers are available to 231 * cover common cases. 232 */ 233 234/* Support for userspace poll() to watch for changes */ 235struct ctl_table_poll { 236 atomic_t event; 237 wait_queue_head_t wait; 238}; 239 240static inline void *proc_sys_poll_event(struct ctl_table_poll *poll) 241{ 242 return (void *)(unsigned long)atomic_read(&poll->event); 243} 244 245#define __CTL_TABLE_POLL_INITIALIZER(name) { \ 246 .event = ATOMIC_INIT(0), \ 247 .wait = __WAIT_QUEUE_HEAD_INITIALIZER(name.wait) } 248 249#define DEFINE_CTL_TABLE_POLL(name) \ 250 struct ctl_table_poll name = __CTL_TABLE_POLL_INITIALIZER(name) 251 252/* A sysctl table is an array of struct ctl_table: */ 253struct ctl_table { 254 const char *procname; /* Text ID for /proc/sys */ 255 void *data; 256 int maxlen; 257 umode_t mode; 258 proc_handler *proc_handler; /* Callback for text formatting */ 259 struct ctl_table_poll *poll; 260 void *extra1; 261 void *extra2; 262} __randomize_layout; 263 264struct ctl_node { 265 struct rb_node node; 266 struct ctl_table_header *header; 267}; 268 269/** 270 * struct ctl_table_header - maintains dynamic lists of struct ctl_table trees 271 * @ctl_table: pointer to the first element in ctl_table array 272 * @ctl_table_size: number of elements pointed by @ctl_table 273 * @used: The entry will never be touched when equal to 0. 274 * @count: Upped every time something is added to @inodes and downed every time 275 * something is removed from inodes 276 * @nreg: When nreg drops to 0 the ctl_table_header will be unregistered. 277 * @rcu: Delays the freeing of the inode. Introduced with "unfuck proc_sysctl ->d_compare()" 278 * 279 * @type: Enumeration to differentiate between ctl target types 280 * @type.SYSCTL_TABLE_TYPE_DEFAULT: ctl target with no special considerations 281 * @type.SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY: Identifies a permanently empty dir 282 * target to serve as a mount point 283 */ 284struct ctl_table_header { 285 union { 286 struct { 287 const struct ctl_table *ctl_table; 288 int ctl_table_size; 289 int used; 290 int count; 291 int nreg; 292 }; 293 struct rcu_head rcu; 294 }; 295 struct completion *unregistering; 296 const struct ctl_table *ctl_table_arg; 297 struct ctl_table_root *root; 298 struct ctl_table_set *set; 299 struct ctl_dir *parent; 300 struct ctl_node *node; 301 struct hlist_head inodes; /* head for proc_inode->sysctl_inodes */ 302 enum { 303 SYSCTL_TABLE_TYPE_DEFAULT, 304 SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY, 305 } type; 306}; 307 308struct ctl_dir { 309 /* Header must be at the start of ctl_dir */ 310 struct ctl_table_header header; 311 struct rb_root root; 312}; 313 314struct ctl_table_set { 315 int (*is_seen)(struct ctl_table_set *); 316 struct ctl_dir dir; 317}; 318 319struct ctl_table_root { 320 struct ctl_table_set default_set; 321 struct ctl_table_set *(*lookup)(struct ctl_table_root *root); 322 void (*set_ownership)(struct ctl_table_header *head, 323 kuid_t *uid, kgid_t *gid); 324 int (*permissions)(struct ctl_table_header *head, const struct ctl_table *table); 325}; 326 327#define register_sysctl(path, table) \ 328 register_sysctl_sz(path, table, ARRAY_SIZE(table)) 329 330#ifdef CONFIG_SYSCTL 331 332void proc_sys_poll_notify(struct ctl_table_poll *poll); 333 334extern void setup_sysctl_set(struct ctl_table_set *p, 335 struct ctl_table_root *root, 336 int (*is_seen)(struct ctl_table_set *)); 337extern void retire_sysctl_set(struct ctl_table_set *set); 338 339struct ctl_table_header *__register_sysctl_table( 340 struct ctl_table_set *set, 341 const char *path, const struct ctl_table *table, size_t table_size); 342struct ctl_table_header *register_sysctl_sz(const char *path, const struct ctl_table *table, 343 size_t table_size); 344void unregister_sysctl_table(struct ctl_table_header * table); 345 346extern int sysctl_init_bases(void); 347extern void __register_sysctl_init(const char *path, const struct ctl_table *table, 348 const char *table_name, size_t table_size); 349#define register_sysctl_init(path, table) \ 350 __register_sysctl_init(path, table, #table, ARRAY_SIZE(table)) 351extern struct ctl_table_header *register_sysctl_mount_point(const char *path); 352 353void do_sysctl_args(void); 354bool sysctl_is_alias(char *param); 355 356extern int unaligned_enabled; 357extern int no_unaligned_warning; 358 359#else /* CONFIG_SYSCTL */ 360 361static inline void register_sysctl_init(const char *path, const struct ctl_table *table) 362{ 363} 364 365static inline struct ctl_table_header *register_sysctl_mount_point(const char *path) 366{ 367 return NULL; 368} 369 370static inline struct ctl_table_header *register_sysctl_sz(const char *path, 371 const struct ctl_table *table, 372 size_t table_size) 373{ 374 return NULL; 375} 376 377static inline void unregister_sysctl_table(struct ctl_table_header * table) 378{ 379} 380 381static inline void setup_sysctl_set(struct ctl_table_set *p, 382 struct ctl_table_root *root, 383 int (*is_seen)(struct ctl_table_set *)) 384{ 385} 386 387static inline void do_sysctl_args(void) 388{ 389} 390 391static inline bool sysctl_is_alias(char *param) 392{ 393 return false; 394} 395#endif /* CONFIG_SYSCTL */ 396 397#endif /* _LINUX_SYSCTL_H */