1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 277b14db5SEric W. Biederman /* 377b14db5SEric W. Biederman * /proc/sys support 477b14db5SEric W. Biederman */ 51e0edd3fSAlexey Dobriyan #include <linux/init.h> 677b14db5SEric W. Biederman #include <linux/sysctl.h> 7f1ecf068SLucas De Marchi #include <linux/poll.h> 877b14db5SEric W. Biederman #include <linux/proc_fs.h> 987ebdc00SAndrew Morton #include <linux/printk.h> 1077b14db5SEric W. Biederman #include <linux/security.h> 1140401530SAl Viro #include <linux/sched.h> 125b825c3aSIngo Molnar #include <linux/cred.h> 1334286d66SNick Piggin #include <linux/namei.h> 1440401530SAl Viro #include <linux/mm.h> 151f87f0b5SEric W. Biederman #include <linux/module.h> 167b146cebSAndrey Ignatov #include <linux/bpf-cgroup.h> 173db978d4SVlastimil Babka #include <linux/mount.h> 1877b14db5SEric W. Biederman #include "internal.h" 1977b14db5SEric W. Biederman 20d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations; 2177b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations; 2203a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations; 239043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations; 249043476fSAl Viro static const struct inode_operations proc_sys_dir_operations; 2577b14db5SEric W. Biederman 26eec4844fSMatteo Croce /* shared constants to be used in various sysctls */ 27eec4844fSMatteo Croce const int sysctl_vals[] = { 0, 1, INT_MAX }; 28eec4844fSMatteo Croce EXPORT_SYMBOL(sysctl_vals); 29eec4844fSMatteo Croce 30f9bd6733SEric W. Biederman /* Support for permanently empty directories */ 31f9bd6733SEric W. Biederman 32f9bd6733SEric W. Biederman struct ctl_table sysctl_mount_point[] = { 33f9bd6733SEric W. Biederman { } 34f9bd6733SEric W. Biederman }; 35f9bd6733SEric W. Biederman 36f9bd6733SEric W. Biederman static bool is_empty_dir(struct ctl_table_header *head) 37f9bd6733SEric W. Biederman { 38f9bd6733SEric W. Biederman return head->ctl_table[0].child == sysctl_mount_point; 39f9bd6733SEric W. Biederman } 40f9bd6733SEric W. Biederman 41f9bd6733SEric W. Biederman static void set_empty_dir(struct ctl_dir *dir) 42f9bd6733SEric W. Biederman { 43f9bd6733SEric W. Biederman dir->header.ctl_table[0].child = sysctl_mount_point; 44f9bd6733SEric W. Biederman } 45f9bd6733SEric W. Biederman 46f9bd6733SEric W. Biederman static void clear_empty_dir(struct ctl_dir *dir) 47f9bd6733SEric W. Biederman 48f9bd6733SEric W. Biederman { 49f9bd6733SEric W. Biederman dir->header.ctl_table[0].child = NULL; 50f9bd6733SEric W. Biederman } 51f9bd6733SEric W. Biederman 52f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll) 53f1ecf068SLucas De Marchi { 54f1ecf068SLucas De Marchi if (!poll) 55f1ecf068SLucas De Marchi return; 56f1ecf068SLucas De Marchi 57f1ecf068SLucas De Marchi atomic_inc(&poll->event); 58f1ecf068SLucas De Marchi wake_up_interruptible(&poll->wait); 59f1ecf068SLucas De Marchi } 60f1ecf068SLucas De Marchi 61a194558eSEric W. Biederman static struct ctl_table root_table[] = { 62a194558eSEric W. Biederman { 63a194558eSEric W. Biederman .procname = "", 647ec66d06SEric W. Biederman .mode = S_IFDIR|S_IRUGO|S_IXUGO, 65a194558eSEric W. Biederman }, 66a194558eSEric W. Biederman { } 67a194558eSEric W. Biederman }; 680e47c99dSEric W. Biederman static struct ctl_table_root sysctl_table_root = { 690e47c99dSEric W. Biederman .default_set.dir.header = { 701f87f0b5SEric W. Biederman {{.count = 1, 71938aaa4fSEric W. Biederman .nreg = 1, 72ac13ac6fSEric W. Biederman .ctl_table = root_table }}, 730e47c99dSEric W. Biederman .ctl_table_arg = root_table, 741f87f0b5SEric W. Biederman .root = &sysctl_table_root, 751f87f0b5SEric W. Biederman .set = &sysctl_table_root.default_set, 767ec66d06SEric W. Biederman }, 771f87f0b5SEric W. Biederman }; 781f87f0b5SEric W. Biederman 791f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock); 801f87f0b5SEric W. Biederman 817ec66d06SEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header); 820e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead, 8313bcc6a2SEric W. Biederman struct ctl_table **pentry); 840e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head); 850e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header); 867ec66d06SEric W. Biederman 876980128fSEric W. Biederman static void sysctl_print_dir(struct ctl_dir *dir) 886980128fSEric W. Biederman { 896980128fSEric W. Biederman if (dir->header.parent) 906980128fSEric W. Biederman sysctl_print_dir(dir->header.parent); 9187ebdc00SAndrew Morton pr_cont("%s/", dir->header.ctl_table[0].procname); 926980128fSEric W. Biederman } 936980128fSEric W. Biederman 94076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2) 95076c3eedSEric W. Biederman { 96076c3eedSEric W. Biederman int minlen; 97076c3eedSEric W. Biederman int cmp; 98076c3eedSEric W. Biederman 99076c3eedSEric W. Biederman minlen = len1; 100076c3eedSEric W. Biederman if (minlen > len2) 101076c3eedSEric W. Biederman minlen = len2; 102076c3eedSEric W. Biederman 103076c3eedSEric W. Biederman cmp = memcmp(name1, name2, minlen); 104076c3eedSEric W. Biederman if (cmp == 0) 105076c3eedSEric W. Biederman cmp = len1 - len2; 106076c3eedSEric W. Biederman return cmp; 107076c3eedSEric W. Biederman } 108076c3eedSEric W. Biederman 10960f126d9SEric W. Biederman /* Called under sysctl_lock */ 110076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead, 1110e47c99dSEric W. Biederman struct ctl_dir *dir, const char *name, int namelen) 112076c3eedSEric W. Biederman { 113076c3eedSEric W. Biederman struct ctl_table_header *head; 114076c3eedSEric W. Biederman struct ctl_table *entry; 115ac13ac6fSEric W. Biederman struct rb_node *node = dir->root.rb_node; 116076c3eedSEric W. Biederman 117ac13ac6fSEric W. Biederman while (node) 118ac13ac6fSEric W. Biederman { 119ac13ac6fSEric W. Biederman struct ctl_node *ctl_node; 120ac13ac6fSEric W. Biederman const char *procname; 121ac13ac6fSEric W. Biederman int cmp; 122ac13ac6fSEric W. Biederman 123ac13ac6fSEric W. Biederman ctl_node = rb_entry(node, struct ctl_node, node); 124ac13ac6fSEric W. Biederman head = ctl_node->header; 125ac13ac6fSEric W. Biederman entry = &head->ctl_table[ctl_node - head->node]; 126ac13ac6fSEric W. Biederman procname = entry->procname; 127ac13ac6fSEric W. Biederman 128ac13ac6fSEric W. Biederman cmp = namecmp(name, namelen, procname, strlen(procname)); 129ac13ac6fSEric W. Biederman if (cmp < 0) 130ac13ac6fSEric W. Biederman node = node->rb_left; 131ac13ac6fSEric W. Biederman else if (cmp > 0) 132ac13ac6fSEric W. Biederman node = node->rb_right; 133ac13ac6fSEric W. Biederman else { 134076c3eedSEric W. Biederman *phead = head; 135076c3eedSEric W. Biederman return entry; 136076c3eedSEric W. Biederman } 137076c3eedSEric W. Biederman } 138076c3eedSEric W. Biederman return NULL; 139076c3eedSEric W. Biederman } 140076c3eedSEric W. Biederman 141ac13ac6fSEric W. Biederman static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry) 142ac13ac6fSEric W. Biederman { 143ac13ac6fSEric W. Biederman struct rb_node *node = &head->node[entry - head->ctl_table].node; 144ac13ac6fSEric W. Biederman struct rb_node **p = &head->parent->root.rb_node; 145ac13ac6fSEric W. Biederman struct rb_node *parent = NULL; 146ac13ac6fSEric W. Biederman const char *name = entry->procname; 147ac13ac6fSEric W. Biederman int namelen = strlen(name); 148ac13ac6fSEric W. Biederman 149ac13ac6fSEric W. Biederman while (*p) { 150ac13ac6fSEric W. Biederman struct ctl_table_header *parent_head; 151ac13ac6fSEric W. Biederman struct ctl_table *parent_entry; 152ac13ac6fSEric W. Biederman struct ctl_node *parent_node; 153ac13ac6fSEric W. Biederman const char *parent_name; 154ac13ac6fSEric W. Biederman int cmp; 155ac13ac6fSEric W. Biederman 156ac13ac6fSEric W. Biederman parent = *p; 157ac13ac6fSEric W. Biederman parent_node = rb_entry(parent, struct ctl_node, node); 158ac13ac6fSEric W. Biederman parent_head = parent_node->header; 159ac13ac6fSEric W. Biederman parent_entry = &parent_head->ctl_table[parent_node - parent_head->node]; 160ac13ac6fSEric W. Biederman parent_name = parent_entry->procname; 161ac13ac6fSEric W. Biederman 162ac13ac6fSEric W. Biederman cmp = namecmp(name, namelen, parent_name, strlen(parent_name)); 163ac13ac6fSEric W. Biederman if (cmp < 0) 164ac13ac6fSEric W. Biederman p = &(*p)->rb_left; 165ac13ac6fSEric W. Biederman else if (cmp > 0) 166ac13ac6fSEric W. Biederman p = &(*p)->rb_right; 167ac13ac6fSEric W. Biederman else { 16887ebdc00SAndrew Morton pr_err("sysctl duplicate entry: "); 169ac13ac6fSEric W. Biederman sysctl_print_dir(head->parent); 17087ebdc00SAndrew Morton pr_cont("/%s\n", entry->procname); 171ac13ac6fSEric W. Biederman return -EEXIST; 172ac13ac6fSEric W. Biederman } 173ac13ac6fSEric W. Biederman } 174ac13ac6fSEric W. Biederman 175ac13ac6fSEric W. Biederman rb_link_node(node, parent, p); 176ea5272f5SMichel Lespinasse rb_insert_color(node, &head->parent->root); 177ac13ac6fSEric W. Biederman return 0; 178ac13ac6fSEric W. Biederman } 179ac13ac6fSEric W. Biederman 180ac13ac6fSEric W. Biederman static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry) 181ac13ac6fSEric W. Biederman { 182ac13ac6fSEric W. Biederman struct rb_node *node = &head->node[entry - head->ctl_table].node; 183ac13ac6fSEric W. Biederman 184ac13ac6fSEric W. Biederman rb_erase(node, &head->parent->root); 185ac13ac6fSEric W. Biederman } 186ac13ac6fSEric W. Biederman 187e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head, 188e0d04529SEric W. Biederman struct ctl_table_root *root, struct ctl_table_set *set, 189ac13ac6fSEric W. Biederman struct ctl_node *node, struct ctl_table *table) 190e0d04529SEric W. Biederman { 1917ec66d06SEric W. Biederman head->ctl_table = table; 192e0d04529SEric W. Biederman head->ctl_table_arg = table; 193e0d04529SEric W. Biederman head->used = 0; 194e0d04529SEric W. Biederman head->count = 1; 195e0d04529SEric W. Biederman head->nreg = 1; 196e0d04529SEric W. Biederman head->unregistering = NULL; 197e0d04529SEric W. Biederman head->root = root; 198e0d04529SEric W. Biederman head->set = set; 199e0d04529SEric W. Biederman head->parent = NULL; 200ac13ac6fSEric W. Biederman head->node = node; 2012fd1d2c4SEric W. Biederman INIT_HLIST_HEAD(&head->inodes); 202ac13ac6fSEric W. Biederman if (node) { 203ac13ac6fSEric W. Biederman struct ctl_table *entry; 2044c199a93SMichel Lespinasse for (entry = table; entry->procname; entry++, node++) 205ac13ac6fSEric W. Biederman node->header = head; 206ac13ac6fSEric W. Biederman } 207ac13ac6fSEric W. Biederman } 208e0d04529SEric W. Biederman 2098425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head) 2108425d6aaSEric W. Biederman { 211ac13ac6fSEric W. Biederman struct ctl_table *entry; 212ac13ac6fSEric W. Biederman for (entry = head->ctl_table; entry->procname; entry++) 213ac13ac6fSEric W. Biederman erase_entry(head, entry); 2148425d6aaSEric W. Biederman } 2158425d6aaSEric W. Biederman 2160e47c99dSEric W. Biederman static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header) 2178425d6aaSEric W. Biederman { 218ac13ac6fSEric W. Biederman struct ctl_table *entry; 2190e47c99dSEric W. Biederman int err; 2200e47c99dSEric W. Biederman 221f9bd6733SEric W. Biederman /* Is this a permanently empty directory? */ 222f9bd6733SEric W. Biederman if (is_empty_dir(&dir->header)) 223f9bd6733SEric W. Biederman return -EROFS; 224f9bd6733SEric W. Biederman 225f9bd6733SEric W. Biederman /* Am I creating a permanently empty directory? */ 226f9bd6733SEric W. Biederman if (header->ctl_table == sysctl_mount_point) { 227f9bd6733SEric W. Biederman if (!RB_EMPTY_ROOT(&dir->root)) 228f9bd6733SEric W. Biederman return -EINVAL; 229f9bd6733SEric W. Biederman set_empty_dir(dir); 230f9bd6733SEric W. Biederman } 231f9bd6733SEric W. Biederman 2320e47c99dSEric W. Biederman dir->header.nreg++; 2337ec66d06SEric W. Biederman header->parent = dir; 2340e47c99dSEric W. Biederman err = insert_links(header); 2350e47c99dSEric W. Biederman if (err) 2360e47c99dSEric W. Biederman goto fail_links; 237ac13ac6fSEric W. Biederman for (entry = header->ctl_table; entry->procname; entry++) { 238ac13ac6fSEric W. Biederman err = insert_entry(header, entry); 239ac13ac6fSEric W. Biederman if (err) 240ac13ac6fSEric W. Biederman goto fail; 241ac13ac6fSEric W. Biederman } 2420e47c99dSEric W. Biederman return 0; 243ac13ac6fSEric W. Biederman fail: 244ac13ac6fSEric W. Biederman erase_header(header); 245ac13ac6fSEric W. Biederman put_links(header); 2460e47c99dSEric W. Biederman fail_links: 247f9bd6733SEric W. Biederman if (header->ctl_table == sysctl_mount_point) 248f9bd6733SEric W. Biederman clear_empty_dir(dir); 2490e47c99dSEric W. Biederman header->parent = NULL; 2500e47c99dSEric W. Biederman drop_sysctl_table(&dir->header); 2510e47c99dSEric W. Biederman return err; 2528425d6aaSEric W. Biederman } 2538425d6aaSEric W. Biederman 2541f87f0b5SEric W. Biederman /* called under sysctl_lock */ 2551f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p) 2561f87f0b5SEric W. Biederman { 2571f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 2581f87f0b5SEric W. Biederman return 0; 2591f87f0b5SEric W. Biederman p->used++; 2601f87f0b5SEric W. Biederman return 1; 2611f87f0b5SEric W. Biederman } 2621f87f0b5SEric W. Biederman 2631f87f0b5SEric W. Biederman /* called under sysctl_lock */ 2641f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p) 2651f87f0b5SEric W. Biederman { 2661f87f0b5SEric W. Biederman if (!--p->used) 2671f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 2681f87f0b5SEric W. Biederman complete(p->unregistering); 2691f87f0b5SEric W. Biederman } 2701f87f0b5SEric W. Biederman 271f90f3cafSEric W. Biederman static void proc_sys_invalidate_dcache(struct ctl_table_header *head) 272d6cffbbeSKonstantin Khlebnikov { 273f90f3cafSEric W. Biederman proc_invalidate_siblings_dcache(&head->inodes, &sysctl_lock); 274d6cffbbeSKonstantin Khlebnikov } 275d6cffbbeSKonstantin Khlebnikov 2761f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */ 2771f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p) 2781f87f0b5SEric W. Biederman { 2791f87f0b5SEric W. Biederman /* 2801f87f0b5SEric W. Biederman * if p->used is 0, nobody will ever touch that entry again; 2811f87f0b5SEric W. Biederman * we'll eliminate all paths to it before dropping sysctl_lock 2821f87f0b5SEric W. Biederman */ 2831f87f0b5SEric W. Biederman if (unlikely(p->used)) { 2841f87f0b5SEric W. Biederman struct completion wait; 2851f87f0b5SEric W. Biederman init_completion(&wait); 2861f87f0b5SEric W. Biederman p->unregistering = &wait; 2871f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 2881f87f0b5SEric W. Biederman wait_for_completion(&wait); 2891f87f0b5SEric W. Biederman } else { 2901f87f0b5SEric W. Biederman /* anything non-NULL; we'll never dereference it */ 2911f87f0b5SEric W. Biederman p->unregistering = ERR_PTR(-EINVAL); 292ace0c791SEric W. Biederman spin_unlock(&sysctl_lock); 2931f87f0b5SEric W. Biederman } 2941f87f0b5SEric W. Biederman /* 295f90f3cafSEric W. Biederman * Invalidate dentries for unregistered sysctls: namespaced sysctls 296d6cffbbeSKonstantin Khlebnikov * can have duplicate names and contaminate dcache very badly. 297d6cffbbeSKonstantin Khlebnikov */ 298f90f3cafSEric W. Biederman proc_sys_invalidate_dcache(p); 299d6cffbbeSKonstantin Khlebnikov /* 3001f87f0b5SEric W. Biederman * do not remove from the list until nobody holds it; walking the 3011f87f0b5SEric W. Biederman * list in do_sysctl() relies on that. 3021f87f0b5SEric W. Biederman */ 303ace0c791SEric W. Biederman spin_lock(&sysctl_lock); 3048425d6aaSEric W. Biederman erase_header(p); 3051f87f0b5SEric W. Biederman } 3061f87f0b5SEric W. Biederman 3071f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head) 3081f87f0b5SEric W. Biederman { 309ab4a1f24SPrasad Joshi BUG_ON(!head); 3101f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 3111f87f0b5SEric W. Biederman if (!use_table(head)) 3121f87f0b5SEric W. Biederman head = ERR_PTR(-ENOENT); 3131f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 3141f87f0b5SEric W. Biederman return head; 3151f87f0b5SEric W. Biederman } 3161f87f0b5SEric W. Biederman 3171f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head) 3181f87f0b5SEric W. Biederman { 3191f87f0b5SEric W. Biederman if (!head) 3201f87f0b5SEric W. Biederman return; 3211f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 3221f87f0b5SEric W. Biederman unuse_table(head); 3231f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 3241f87f0b5SEric W. Biederman } 3251f87f0b5SEric W. Biederman 3261f87f0b5SEric W. Biederman static struct ctl_table_set * 32713bcc6a2SEric W. Biederman lookup_header_set(struct ctl_table_root *root) 3281f87f0b5SEric W. Biederman { 3291f87f0b5SEric W. Biederman struct ctl_table_set *set = &root->default_set; 3301f87f0b5SEric W. Biederman if (root->lookup) 33113bcc6a2SEric W. Biederman set = root->lookup(root); 3321f87f0b5SEric W. Biederman return set; 3331f87f0b5SEric W. Biederman } 3341f87f0b5SEric W. Biederman 335076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead, 3367ec66d06SEric W. Biederman struct ctl_dir *dir, 337076c3eedSEric W. Biederman const char *name, int namelen) 338076c3eedSEric W. Biederman { 339076c3eedSEric W. Biederman struct ctl_table_header *head; 340076c3eedSEric W. Biederman struct ctl_table *entry; 341076c3eedSEric W. Biederman 342076c3eedSEric W. Biederman spin_lock(&sysctl_lock); 3430e47c99dSEric W. Biederman entry = find_entry(&head, dir, name, namelen); 344076c3eedSEric W. Biederman if (entry && use_table(head)) 345076c3eedSEric W. Biederman *phead = head; 346076c3eedSEric W. Biederman else 347076c3eedSEric W. Biederman entry = NULL; 348076c3eedSEric W. Biederman spin_unlock(&sysctl_lock); 349076c3eedSEric W. Biederman return entry; 350076c3eedSEric W. Biederman } 351076c3eedSEric W. Biederman 352ac13ac6fSEric W. Biederman static struct ctl_node *first_usable_entry(struct rb_node *node) 3531f87f0b5SEric W. Biederman { 354ac13ac6fSEric W. Biederman struct ctl_node *ctl_node; 3551f87f0b5SEric W. Biederman 356ac13ac6fSEric W. Biederman for (;node; node = rb_next(node)) { 357ac13ac6fSEric W. Biederman ctl_node = rb_entry(node, struct ctl_node, node); 358ac13ac6fSEric W. Biederman if (use_table(ctl_node->header)) 359ac13ac6fSEric W. Biederman return ctl_node; 3601f87f0b5SEric W. Biederman } 3611f87f0b5SEric W. Biederman return NULL; 3621f87f0b5SEric W. Biederman } 3631f87f0b5SEric W. Biederman 3647ec66d06SEric W. Biederman static void first_entry(struct ctl_dir *dir, 3656a75ce16SEric W. Biederman struct ctl_table_header **phead, struct ctl_table **pentry) 3661f87f0b5SEric W. Biederman { 367ac13ac6fSEric W. Biederman struct ctl_table_header *head = NULL; 3687ec66d06SEric W. Biederman struct ctl_table *entry = NULL; 369ac13ac6fSEric W. Biederman struct ctl_node *ctl_node; 3706a75ce16SEric W. Biederman 3716a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 372ac13ac6fSEric W. Biederman ctl_node = first_usable_entry(rb_first(&dir->root)); 3736a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 374ac13ac6fSEric W. Biederman if (ctl_node) { 375ac13ac6fSEric W. Biederman head = ctl_node->header; 376ac13ac6fSEric W. Biederman entry = &head->ctl_table[ctl_node - head->node]; 377ac13ac6fSEric W. Biederman } 3786a75ce16SEric W. Biederman *phead = head; 3796a75ce16SEric W. Biederman *pentry = entry; 3806a75ce16SEric W. Biederman } 3816a75ce16SEric W. Biederman 3827ec66d06SEric W. Biederman static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry) 3836a75ce16SEric W. Biederman { 3846a75ce16SEric W. Biederman struct ctl_table_header *head = *phead; 3856a75ce16SEric W. Biederman struct ctl_table *entry = *pentry; 386ac13ac6fSEric W. Biederman struct ctl_node *ctl_node = &head->node[entry - head->ctl_table]; 3876a75ce16SEric W. Biederman 3886a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 3896a75ce16SEric W. Biederman unuse_table(head); 390ac13ac6fSEric W. Biederman 391ac13ac6fSEric W. Biederman ctl_node = first_usable_entry(rb_next(&ctl_node->node)); 3926a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 393ac13ac6fSEric W. Biederman head = NULL; 394ac13ac6fSEric W. Biederman if (ctl_node) { 395ac13ac6fSEric W. Biederman head = ctl_node->header; 396ac13ac6fSEric W. Biederman entry = &head->ctl_table[ctl_node - head->node]; 3976a75ce16SEric W. Biederman } 3986a75ce16SEric W. Biederman *phead = head; 3996a75ce16SEric W. Biederman *pentry = entry; 4001f87f0b5SEric W. Biederman } 4011f87f0b5SEric W. Biederman 4021f87f0b5SEric W. Biederman /* 4031f87f0b5SEric W. Biederman * sysctl_perm does NOT grant the superuser all rights automatically, because 4041f87f0b5SEric W. Biederman * some sysctl variables are readonly even to root. 4051f87f0b5SEric W. Biederman */ 4061f87f0b5SEric W. Biederman 4071f87f0b5SEric W. Biederman static int test_perm(int mode, int op) 4081f87f0b5SEric W. Biederman { 409091bd3eaSEric W. Biederman if (uid_eq(current_euid(), GLOBAL_ROOT_UID)) 4101f87f0b5SEric W. Biederman mode >>= 6; 411091bd3eaSEric W. Biederman else if (in_egroup_p(GLOBAL_ROOT_GID)) 4121f87f0b5SEric W. Biederman mode >>= 3; 4131f87f0b5SEric W. Biederman if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0) 4141f87f0b5SEric W. Biederman return 0; 4151f87f0b5SEric W. Biederman return -EACCES; 4161f87f0b5SEric W. Biederman } 4171f87f0b5SEric W. Biederman 41873f7ef43SEric W. Biederman static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op) 4191f87f0b5SEric W. Biederman { 42073f7ef43SEric W. Biederman struct ctl_table_root *root = head->root; 4211f87f0b5SEric W. Biederman int mode; 4221f87f0b5SEric W. Biederman 4231f87f0b5SEric W. Biederman if (root->permissions) 42473f7ef43SEric W. Biederman mode = root->permissions(head, table); 4251f87f0b5SEric W. Biederman else 4261f87f0b5SEric W. Biederman mode = table->mode; 4271f87f0b5SEric W. Biederman 4281f87f0b5SEric W. Biederman return test_perm(mode, op); 4291f87f0b5SEric W. Biederman } 4301f87f0b5SEric W. Biederman 4319043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb, 4329043476fSAl Viro struct ctl_table_header *head, struct ctl_table *table) 43377b14db5SEric W. Biederman { 434e79c6a4fSDmitry Torokhov struct ctl_table_root *root = head->root; 43577b14db5SEric W. Biederman struct inode *inode; 4369043476fSAl Viro struct proc_inode *ei; 43777b14db5SEric W. Biederman 4389043476fSAl Viro inode = new_inode(sb); 43977b14db5SEric W. Biederman if (!inode) 440ea5751ccSIvan Delalande return ERR_PTR(-ENOMEM); 44177b14db5SEric W. Biederman 44285fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 44385fe4025SChristoph Hellwig 44477b14db5SEric W. Biederman ei = PROC_I(inode); 4459043476fSAl Viro 446d6cffbbeSKonstantin Khlebnikov spin_lock(&sysctl_lock); 447ace0c791SEric W. Biederman if (unlikely(head->unregistering)) { 448ace0c791SEric W. Biederman spin_unlock(&sysctl_lock); 449ace0c791SEric W. Biederman iput(inode); 450ea5751ccSIvan Delalande return ERR_PTR(-ENOENT); 451ace0c791SEric W. Biederman } 452ace0c791SEric W. Biederman ei->sysctl = head; 453ace0c791SEric W. Biederman ei->sysctl_entry = table; 4540afa5ca8SEric W. Biederman hlist_add_head_rcu(&ei->sibling_inodes, &head->inodes); 455d6cffbbeSKonstantin Khlebnikov head->count++; 456d6cffbbeSKonstantin Khlebnikov spin_unlock(&sysctl_lock); 457d6cffbbeSKonstantin Khlebnikov 458078cd827SDeepa Dinamani inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); 4599043476fSAl Viro inode->i_mode = table->mode; 4607ec66d06SEric W. Biederman if (!S_ISDIR(table->mode)) { 4619043476fSAl Viro inode->i_mode |= S_IFREG; 46277b14db5SEric W. Biederman inode->i_op = &proc_sys_inode_operations; 46377b14db5SEric W. Biederman inode->i_fop = &proc_sys_file_operations; 4649043476fSAl Viro } else { 4659043476fSAl Viro inode->i_mode |= S_IFDIR; 4669043476fSAl Viro inode->i_op = &proc_sys_dir_operations; 4679043476fSAl Viro inode->i_fop = &proc_sys_dir_file_operations; 468f9bd6733SEric W. Biederman if (is_empty_dir(head)) 469f9bd6733SEric W. Biederman make_empty_dir_inode(inode); 4709043476fSAl Viro } 471e79c6a4fSDmitry Torokhov 472e79c6a4fSDmitry Torokhov if (root->set_ownership) 473e79c6a4fSDmitry Torokhov root->set_ownership(head, table, &inode->i_uid, &inode->i_gid); 4745ec27ec7SRadoslaw Burny else { 4755ec27ec7SRadoslaw Burny inode->i_uid = GLOBAL_ROOT_UID; 4765ec27ec7SRadoslaw Burny inode->i_gid = GLOBAL_ROOT_GID; 4775ec27ec7SRadoslaw Burny } 478e79c6a4fSDmitry Torokhov 47977b14db5SEric W. Biederman return inode; 48077b14db5SEric W. Biederman } 48177b14db5SEric W. Biederman 482d6cffbbeSKonstantin Khlebnikov void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head) 483d6cffbbeSKonstantin Khlebnikov { 484d6cffbbeSKonstantin Khlebnikov spin_lock(&sysctl_lock); 4850afa5ca8SEric W. Biederman hlist_del_init_rcu(&PROC_I(inode)->sibling_inodes); 486d6cffbbeSKonstantin Khlebnikov if (!--head->count) 487d6cffbbeSKonstantin Khlebnikov kfree_rcu(head, rcu); 488d6cffbbeSKonstantin Khlebnikov spin_unlock(&sysctl_lock); 489d6cffbbeSKonstantin Khlebnikov } 490d6cffbbeSKonstantin Khlebnikov 49181324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode) 49277b14db5SEric W. Biederman { 4933cc3e046SEric W. Biederman struct ctl_table_header *head = PROC_I(inode)->sysctl; 4943cc3e046SEric W. Biederman if (!head) 4950e47c99dSEric W. Biederman head = &sysctl_table_root.default_set.dir.header; 4963cc3e046SEric W. Biederman return sysctl_head_grab(head); 49777b14db5SEric W. Biederman } 49877b14db5SEric W. Biederman 49977b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, 50000cd8dd3SAl Viro unsigned int flags) 50177b14db5SEric W. Biederman { 5029043476fSAl Viro struct ctl_table_header *head = grab_header(dir); 5039043476fSAl Viro struct ctl_table_header *h = NULL; 504dc12e909SAl Viro const struct qstr *name = &dentry->d_name; 5059043476fSAl Viro struct ctl_table *p; 50677b14db5SEric W. Biederman struct inode *inode; 5079043476fSAl Viro struct dentry *err = ERR_PTR(-ENOENT); 5087ec66d06SEric W. Biederman struct ctl_dir *ctl_dir; 5090e47c99dSEric W. Biederman int ret; 51077b14db5SEric W. Biederman 5119043476fSAl Viro if (IS_ERR(head)) 5129043476fSAl Viro return ERR_CAST(head); 5139043476fSAl Viro 5147ec66d06SEric W. Biederman ctl_dir = container_of(head, struct ctl_dir, header); 5159043476fSAl Viro 5167ec66d06SEric W. Biederman p = lookup_entry(&h, ctl_dir, name->name, name->len); 5179043476fSAl Viro if (!p) 51877b14db5SEric W. Biederman goto out; 51977b14db5SEric W. Biederman 5204e757320SEric W. Biederman if (S_ISLNK(p->mode)) { 52113bcc6a2SEric W. Biederman ret = sysctl_follow_link(&h, &p); 5220e47c99dSEric W. Biederman err = ERR_PTR(ret); 5230e47c99dSEric W. Biederman if (ret) 5240e47c99dSEric W. Biederman goto out; 5254e757320SEric W. Biederman } 5260e47c99dSEric W. Biederman 5279043476fSAl Viro inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p); 528ea5751ccSIvan Delalande if (IS_ERR(inode)) { 529ea5751ccSIvan Delalande err = ERR_CAST(inode); 53077b14db5SEric W. Biederman goto out; 531ea5751ccSIvan Delalande } 53277b14db5SEric W. Biederman 533fb045adbSNick Piggin d_set_d_op(dentry, &proc_sys_dentry_operations); 534888e2b03SAl Viro err = d_splice_alias(inode, dentry); 53577b14db5SEric W. Biederman 53677b14db5SEric W. Biederman out: 5376bf61045SFrancesco Ruggeri if (h) 5386bf61045SFrancesco Ruggeri sysctl_head_finish(h); 53977b14db5SEric W. Biederman sysctl_head_finish(head); 54077b14db5SEric W. Biederman return err; 54177b14db5SEric W. Biederman } 54277b14db5SEric W. Biederman 54332927393SChristoph Hellwig static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, 5447708bfb1SPavel Emelyanov size_t count, loff_t *ppos, int write) 54577b14db5SEric W. Biederman { 546496ad9aaSAl Viro struct inode *inode = file_inode(filp); 5479043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 5489043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 54932927393SChristoph Hellwig void *kbuf; 5502a2da53bSDavid Howells ssize_t error; 55177b14db5SEric W. Biederman 5529043476fSAl Viro if (IS_ERR(head)) 5539043476fSAl Viro return PTR_ERR(head); 55477b14db5SEric W. Biederman 55577b14db5SEric W. Biederman /* 55677b14db5SEric W. Biederman * At this point we know that the sysctl was not unregistered 55777b14db5SEric W. Biederman * and won't be until we finish. 55877b14db5SEric W. Biederman */ 55977b14db5SEric W. Biederman error = -EPERM; 56073f7ef43SEric W. Biederman if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ)) 56177b14db5SEric W. Biederman goto out; 56277b14db5SEric W. Biederman 5639043476fSAl Viro /* if that can happen at all, it should be -EINVAL, not -EISDIR */ 5649043476fSAl Viro error = -EINVAL; 5659043476fSAl Viro if (!table->proc_handler) 5669043476fSAl Viro goto out; 5679043476fSAl Viro 56832927393SChristoph Hellwig if (write) { 56932927393SChristoph Hellwig kbuf = memdup_user_nul(ubuf, count); 57032927393SChristoph Hellwig if (IS_ERR(kbuf)) { 57132927393SChristoph Hellwig error = PTR_ERR(kbuf); 5727b146cebSAndrey Ignatov goto out; 57332927393SChristoph Hellwig } 5744e63acdfSAndrey Ignatov } else { 57532927393SChristoph Hellwig error = -ENOMEM; 57632927393SChristoph Hellwig kbuf = kzalloc(count, GFP_KERNEL); 57732927393SChristoph Hellwig if (!kbuf) 57832927393SChristoph Hellwig goto out; 5794e63acdfSAndrey Ignatov } 5804e63acdfSAndrey Ignatov 58132927393SChristoph Hellwig error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, &kbuf, &count, 58232927393SChristoph Hellwig ppos); 58332927393SChristoph Hellwig if (error) 58432927393SChristoph Hellwig goto out_free_buf; 58532927393SChristoph Hellwig 58632927393SChristoph Hellwig /* careful: calling conventions are nasty here */ 58732927393SChristoph Hellwig error = table->proc_handler(table, write, kbuf, &count, ppos); 58832927393SChristoph Hellwig if (error) 58932927393SChristoph Hellwig goto out_free_buf; 59032927393SChristoph Hellwig 59132927393SChristoph Hellwig if (!write) { 59232927393SChristoph Hellwig error = -EFAULT; 59332927393SChristoph Hellwig if (copy_to_user(ubuf, kbuf, count)) 59432927393SChristoph Hellwig goto out_free_buf; 59532927393SChristoph Hellwig } 59632927393SChristoph Hellwig 5974e63acdfSAndrey Ignatov error = count; 59832927393SChristoph Hellwig out_free_buf: 59932927393SChristoph Hellwig kfree(kbuf); 60077b14db5SEric W. Biederman out: 60177b14db5SEric W. Biederman sysctl_head_finish(head); 60277b14db5SEric W. Biederman 60377b14db5SEric W. Biederman return error; 60477b14db5SEric W. Biederman } 60577b14db5SEric W. Biederman 6067708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf, 6077708bfb1SPavel Emelyanov size_t count, loff_t *ppos) 6087708bfb1SPavel Emelyanov { 6097708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0); 6107708bfb1SPavel Emelyanov } 6117708bfb1SPavel Emelyanov 61277b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf, 61377b14db5SEric W. Biederman size_t count, loff_t *ppos) 61477b14db5SEric W. Biederman { 6157708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1); 61677b14db5SEric W. Biederman } 61777b14db5SEric W. Biederman 618f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp) 619f1ecf068SLucas De Marchi { 6204e474a00SLucas De Marchi struct ctl_table_header *head = grab_header(inode); 621f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 622f1ecf068SLucas De Marchi 6234e474a00SLucas De Marchi /* sysctl was unregistered */ 6244e474a00SLucas De Marchi if (IS_ERR(head)) 6254e474a00SLucas De Marchi return PTR_ERR(head); 6264e474a00SLucas De Marchi 627f1ecf068SLucas De Marchi if (table->poll) 628f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 629f1ecf068SLucas De Marchi 6304e474a00SLucas De Marchi sysctl_head_finish(head); 6314e474a00SLucas De Marchi 632f1ecf068SLucas De Marchi return 0; 633f1ecf068SLucas De Marchi } 634f1ecf068SLucas De Marchi 635076ccb76SAl Viro static __poll_t proc_sys_poll(struct file *filp, poll_table *wait) 636f1ecf068SLucas De Marchi { 637496ad9aaSAl Viro struct inode *inode = file_inode(filp); 6384e474a00SLucas De Marchi struct ctl_table_header *head = grab_header(inode); 639f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 640076ccb76SAl Viro __poll_t ret = DEFAULT_POLLMASK; 6414e474a00SLucas De Marchi unsigned long event; 6424e474a00SLucas De Marchi 6434e474a00SLucas De Marchi /* sysctl was unregistered */ 6444e474a00SLucas De Marchi if (IS_ERR(head)) 645a9a08845SLinus Torvalds return EPOLLERR | EPOLLHUP; 646f1ecf068SLucas De Marchi 647f1ecf068SLucas De Marchi if (!table->proc_handler) 648f1ecf068SLucas De Marchi goto out; 649f1ecf068SLucas De Marchi 650f1ecf068SLucas De Marchi if (!table->poll) 651f1ecf068SLucas De Marchi goto out; 652f1ecf068SLucas De Marchi 6534e474a00SLucas De Marchi event = (unsigned long)filp->private_data; 654f1ecf068SLucas De Marchi poll_wait(filp, &table->poll->wait, wait); 655f1ecf068SLucas De Marchi 656f1ecf068SLucas De Marchi if (event != atomic_read(&table->poll->event)) { 657f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 658a9a08845SLinus Torvalds ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI; 659f1ecf068SLucas De Marchi } 660f1ecf068SLucas De Marchi 661f1ecf068SLucas De Marchi out: 6624e474a00SLucas De Marchi sysctl_head_finish(head); 6634e474a00SLucas De Marchi 664f1ecf068SLucas De Marchi return ret; 665f1ecf068SLucas De Marchi } 66677b14db5SEric W. Biederman 667f0c3b509SAl Viro static bool proc_sys_fill_cache(struct file *file, 668f0c3b509SAl Viro struct dir_context *ctx, 6699043476fSAl Viro struct ctl_table_header *head, 6709043476fSAl Viro struct ctl_table *table) 67177b14db5SEric W. Biederman { 672f0c3b509SAl Viro struct dentry *child, *dir = file->f_path.dentry; 67377b14db5SEric W. Biederman struct inode *inode; 67477b14db5SEric W. Biederman struct qstr qname; 67577b14db5SEric W. Biederman ino_t ino = 0; 67677b14db5SEric W. Biederman unsigned type = DT_UNKNOWN; 67777b14db5SEric W. Biederman 67877b14db5SEric W. Biederman qname.name = table->procname; 67977b14db5SEric W. Biederman qname.len = strlen(table->procname); 6808387ff25SLinus Torvalds qname.hash = full_name_hash(dir, qname.name, qname.len); 68177b14db5SEric W. Biederman 68277b14db5SEric W. Biederman child = d_lookup(dir, &qname); 68377b14db5SEric W. Biederman if (!child) { 68476aab3abSAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 68576aab3abSAl Viro child = d_alloc_parallel(dir, &qname, &wq); 68676aab3abSAl Viro if (IS_ERR(child)) 68776aab3abSAl Viro return false; 68876aab3abSAl Viro if (d_in_lookup(child)) { 689888e2b03SAl Viro struct dentry *res; 6909043476fSAl Viro inode = proc_sys_make_inode(dir->d_sb, head, table); 691ea5751ccSIvan Delalande if (IS_ERR(inode)) { 69276aab3abSAl Viro d_lookup_done(child); 6939043476fSAl Viro dput(child); 694f0c3b509SAl Viro return false; 69576aab3abSAl Viro } 696fb045adbSNick Piggin d_set_d_op(child, &proc_sys_dentry_operations); 697888e2b03SAl Viro res = d_splice_alias(inode, child); 698888e2b03SAl Viro d_lookup_done(child); 699888e2b03SAl Viro if (unlikely(res)) { 700888e2b03SAl Viro if (IS_ERR(res)) { 701888e2b03SAl Viro dput(child); 702888e2b03SAl Viro return false; 703888e2b03SAl Viro } 704888e2b03SAl Viro dput(child); 705888e2b03SAl Viro child = res; 706888e2b03SAl Viro } 70777b14db5SEric W. Biederman } 70877b14db5SEric W. Biederman } 7092b0143b5SDavid Howells inode = d_inode(child); 71077b14db5SEric W. Biederman ino = inode->i_ino; 71177b14db5SEric W. Biederman type = inode->i_mode >> 12; 71277b14db5SEric W. Biederman dput(child); 713f0c3b509SAl Viro return dir_emit(ctx, qname.name, qname.len, ino, type); 7149043476fSAl Viro } 7159043476fSAl Viro 716f0c3b509SAl Viro static bool proc_sys_link_fill_cache(struct file *file, 717f0c3b509SAl Viro struct dir_context *ctx, 7180e47c99dSEric W. Biederman struct ctl_table_header *head, 7190e47c99dSEric W. Biederman struct ctl_table *table) 7200e47c99dSEric W. Biederman { 721f0c3b509SAl Viro bool ret = true; 722a0b0d1c3SDanilo Krummrich 7230e47c99dSEric W. Biederman head = sysctl_head_grab(head); 724a0b0d1c3SDanilo Krummrich if (IS_ERR(head)) 725a0b0d1c3SDanilo Krummrich return false; 7260e47c99dSEric W. Biederman 7270e47c99dSEric W. Biederman /* It is not an error if we can not follow the link ignore it */ 728835b94e0SDanilo Krummrich if (sysctl_follow_link(&head, &table)) 7290e47c99dSEric W. Biederman goto out; 7300e47c99dSEric W. Biederman 731f0c3b509SAl Viro ret = proc_sys_fill_cache(file, ctx, head, table); 7320e47c99dSEric W. Biederman out: 7330e47c99dSEric W. Biederman sysctl_head_finish(head); 7340e47c99dSEric W. Biederman return ret; 7350e47c99dSEric W. Biederman } 7360e47c99dSEric W. Biederman 737e5eea098SJoe Perches static int scan(struct ctl_table_header *head, struct ctl_table *table, 7389043476fSAl Viro unsigned long *pos, struct file *file, 739f0c3b509SAl Viro struct dir_context *ctx) 7409043476fSAl Viro { 741f0c3b509SAl Viro bool res; 7429043476fSAl Viro 743f0c3b509SAl Viro if ((*pos)++ < ctx->pos) 744f0c3b509SAl Viro return true; 7459043476fSAl Viro 7460e47c99dSEric W. Biederman if (unlikely(S_ISLNK(table->mode))) 747f0c3b509SAl Viro res = proc_sys_link_fill_cache(file, ctx, head, table); 7480e47c99dSEric W. Biederman else 749f0c3b509SAl Viro res = proc_sys_fill_cache(file, ctx, head, table); 7509043476fSAl Viro 751f0c3b509SAl Viro if (res) 752f0c3b509SAl Viro ctx->pos = *pos; 7536a75ce16SEric W. Biederman 7546a75ce16SEric W. Biederman return res; 75577b14db5SEric W. Biederman } 75677b14db5SEric W. Biederman 757f0c3b509SAl Viro static int proc_sys_readdir(struct file *file, struct dir_context *ctx) 75877b14db5SEric W. Biederman { 759f0c3b509SAl Viro struct ctl_table_header *head = grab_header(file_inode(file)); 7609043476fSAl Viro struct ctl_table_header *h = NULL; 7616a75ce16SEric W. Biederman struct ctl_table *entry; 7627ec66d06SEric W. Biederman struct ctl_dir *ctl_dir; 76377b14db5SEric W. Biederman unsigned long pos; 76477b14db5SEric W. Biederman 7659043476fSAl Viro if (IS_ERR(head)) 7669043476fSAl Viro return PTR_ERR(head); 7679043476fSAl Viro 7687ec66d06SEric W. Biederman ctl_dir = container_of(head, struct ctl_dir, header); 76977b14db5SEric W. Biederman 770f0c3b509SAl Viro if (!dir_emit_dots(file, ctx)) 77193362fa4SZhou Chengming goto out; 772f0c3b509SAl Viro 77377b14db5SEric W. Biederman pos = 2; 77477b14db5SEric W. Biederman 7757ec66d06SEric W. Biederman for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) { 776f0c3b509SAl Viro if (!scan(h, entry, &pos, file, ctx)) { 7779043476fSAl Viro sysctl_head_finish(h); 7789043476fSAl Viro break; 77977b14db5SEric W. Biederman } 78077b14db5SEric W. Biederman } 78193362fa4SZhou Chengming out: 78277b14db5SEric W. Biederman sysctl_head_finish(head); 783f0c3b509SAl Viro return 0; 78477b14db5SEric W. Biederman } 78577b14db5SEric W. Biederman 78610556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask) 78777b14db5SEric W. Biederman { 78877b14db5SEric W. Biederman /* 78977b14db5SEric W. Biederman * sysctl entries that are not writeable, 79077b14db5SEric W. Biederman * are _NOT_ writeable, capabilities or not. 79177b14db5SEric W. Biederman */ 792f696a365SMiklos Szeredi struct ctl_table_header *head; 793f696a365SMiklos Szeredi struct ctl_table *table; 79477b14db5SEric W. Biederman int error; 79577b14db5SEric W. Biederman 796f696a365SMiklos Szeredi /* Executable files are not allowed under /proc/sys/ */ 797f696a365SMiklos Szeredi if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) 798f696a365SMiklos Szeredi return -EACCES; 799f696a365SMiklos Szeredi 800f696a365SMiklos Szeredi head = grab_header(inode); 8019043476fSAl Viro if (IS_ERR(head)) 8029043476fSAl Viro return PTR_ERR(head); 80377b14db5SEric W. Biederman 804f696a365SMiklos Szeredi table = PROC_I(inode)->sysctl_entry; 8059043476fSAl Viro if (!table) /* global root - r-xr-xr-x */ 8069043476fSAl Viro error = mask & MAY_WRITE ? -EACCES : 0; 8079043476fSAl Viro else /* Use the permissions on the sysctl table entry */ 80873f7ef43SEric W. Biederman error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK); 8099043476fSAl Viro 81077b14db5SEric W. Biederman sysctl_head_finish(head); 81177b14db5SEric W. Biederman return error; 81277b14db5SEric W. Biederman } 81377b14db5SEric W. Biederman 81477b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr) 81577b14db5SEric W. Biederman { 8162b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 81777b14db5SEric W. Biederman int error; 81877b14db5SEric W. Biederman 81977b14db5SEric W. Biederman if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) 82077b14db5SEric W. Biederman return -EPERM; 82177b14db5SEric W. Biederman 82231051c85SJan Kara error = setattr_prepare(dentry, attr); 8231025774cSChristoph Hellwig if (error) 82477b14db5SEric W. Biederman return error; 8251025774cSChristoph Hellwig 8261025774cSChristoph Hellwig setattr_copy(inode, attr); 8271025774cSChristoph Hellwig mark_inode_dirty(inode); 8281025774cSChristoph Hellwig return 0; 82977b14db5SEric W. Biederman } 83077b14db5SEric W. Biederman 831a528d35eSDavid Howells static int proc_sys_getattr(const struct path *path, struct kstat *stat, 832a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 8339043476fSAl Viro { 834a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry); 8359043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 8369043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 8379043476fSAl Viro 8389043476fSAl Viro if (IS_ERR(head)) 8399043476fSAl Viro return PTR_ERR(head); 8409043476fSAl Viro 8419043476fSAl Viro generic_fillattr(inode, stat); 8429043476fSAl Viro if (table) 8439043476fSAl Viro stat->mode = (stat->mode & S_IFMT) | table->mode; 8449043476fSAl Viro 8459043476fSAl Viro sysctl_head_finish(head); 8469043476fSAl Viro return 0; 8479043476fSAl Viro } 8489043476fSAl Viro 84977b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = { 850f1ecf068SLucas De Marchi .open = proc_sys_open, 851f1ecf068SLucas De Marchi .poll = proc_sys_poll, 85277b14db5SEric W. Biederman .read = proc_sys_read, 85377b14db5SEric W. Biederman .write = proc_sys_write, 8546038f373SArnd Bergmann .llseek = default_llseek, 8559043476fSAl Viro }; 8569043476fSAl Viro 8579043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = { 858887df078SPavel Emelyanov .read = generic_read_dir, 859f50752eaSAl Viro .iterate_shared = proc_sys_readdir, 8603222a3e5SChristoph Hellwig .llseek = generic_file_llseek, 86177b14db5SEric W. Biederman }; 86277b14db5SEric W. Biederman 86303a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = { 8649043476fSAl Viro .permission = proc_sys_permission, 8659043476fSAl Viro .setattr = proc_sys_setattr, 8669043476fSAl Viro .getattr = proc_sys_getattr, 8679043476fSAl Viro }; 8689043476fSAl Viro 8699043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = { 87077b14db5SEric W. Biederman .lookup = proc_sys_lookup, 87177b14db5SEric W. Biederman .permission = proc_sys_permission, 87277b14db5SEric W. Biederman .setattr = proc_sys_setattr, 8739043476fSAl Viro .getattr = proc_sys_getattr, 87477b14db5SEric W. Biederman }; 87577b14db5SEric W. Biederman 8760b728e19SAl Viro static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags) 87777b14db5SEric W. Biederman { 8780b728e19SAl Viro if (flags & LOOKUP_RCU) 87934286d66SNick Piggin return -ECHILD; 8802b0143b5SDavid Howells return !PROC_I(d_inode(dentry))->sysctl->unregistering; 8819043476fSAl Viro } 8829043476fSAl Viro 883fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry) 8849043476fSAl Viro { 8852b0143b5SDavid Howells return !!PROC_I(d_inode(dentry))->sysctl->unregistering; 8869043476fSAl Viro } 8879043476fSAl Viro 8881f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p) 8891f87f0b5SEric W. Biederman { 8901f87f0b5SEric W. Biederman struct ctl_table_set *set = p->set; 8911f87f0b5SEric W. Biederman int res; 8921f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 8931f87f0b5SEric W. Biederman if (p->unregistering) 8941f87f0b5SEric W. Biederman res = 0; 8951f87f0b5SEric W. Biederman else if (!set->is_seen) 8961f87f0b5SEric W. Biederman res = 1; 8971f87f0b5SEric W. Biederman else 8981f87f0b5SEric W. Biederman res = set->is_seen(set); 8991f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 9001f87f0b5SEric W. Biederman return res; 9011f87f0b5SEric W. Biederman } 9021f87f0b5SEric W. Biederman 9036fa67e70SAl Viro static int proc_sys_compare(const struct dentry *dentry, 904621e155aSNick Piggin unsigned int len, const char *str, const struct qstr *name) 9059043476fSAl Viro { 906dfef6dcdSAl Viro struct ctl_table_header *head; 907da53be12SLinus Torvalds struct inode *inode; 908da53be12SLinus Torvalds 90931e6b01fSNick Piggin /* Although proc doesn't have negative dentries, rcu-walk means 91031e6b01fSNick Piggin * that inode here can be NULL */ 911dfef6dcdSAl Viro /* AV: can it, indeed? */ 9122b0143b5SDavid Howells inode = d_inode_rcu(dentry); 91331e6b01fSNick Piggin if (!inode) 914dfef6dcdSAl Viro return 1; 915621e155aSNick Piggin if (name->len != len) 9169043476fSAl Viro return 1; 917621e155aSNick Piggin if (memcmp(name->name, str, len)) 9189043476fSAl Viro return 1; 919dfef6dcdSAl Viro head = rcu_dereference(PROC_I(inode)->sysctl); 920dfef6dcdSAl Viro return !head || !sysctl_is_seen(head); 92177b14db5SEric W. Biederman } 92277b14db5SEric W. Biederman 923d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = { 92477b14db5SEric W. Biederman .d_revalidate = proc_sys_revalidate, 9259043476fSAl Viro .d_delete = proc_sys_delete, 9269043476fSAl Viro .d_compare = proc_sys_compare, 92777b14db5SEric W. Biederman }; 92877b14db5SEric W. Biederman 9290e47c99dSEric W. Biederman static struct ctl_dir *find_subdir(struct ctl_dir *dir, 9307ec66d06SEric W. Biederman const char *name, int namelen) 9311f87f0b5SEric W. Biederman { 9327ec66d06SEric W. Biederman struct ctl_table_header *head; 9337ec66d06SEric W. Biederman struct ctl_table *entry; 9341f87f0b5SEric W. Biederman 9350e47c99dSEric W. Biederman entry = find_entry(&head, dir, name, namelen); 9367ec66d06SEric W. Biederman if (!entry) 9377ec66d06SEric W. Biederman return ERR_PTR(-ENOENT); 93851f72f4aSEric W. Biederman if (!S_ISDIR(entry->mode)) 9397ec66d06SEric W. Biederman return ERR_PTR(-ENOTDIR); 94051f72f4aSEric W. Biederman return container_of(head, struct ctl_dir, header); 9411f87f0b5SEric W. Biederman } 9421f87f0b5SEric W. Biederman 9437ec66d06SEric W. Biederman static struct ctl_dir *new_dir(struct ctl_table_set *set, 9447ec66d06SEric W. Biederman const char *name, int namelen) 9451f87f0b5SEric W. Biederman { 9467ec66d06SEric W. Biederman struct ctl_table *table; 9477ec66d06SEric W. Biederman struct ctl_dir *new; 948ac13ac6fSEric W. Biederman struct ctl_node *node; 9497ec66d06SEric W. Biederman char *new_name; 9501f87f0b5SEric W. Biederman 951ac13ac6fSEric W. Biederman new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) + 952ac13ac6fSEric W. Biederman sizeof(struct ctl_table)*2 + namelen + 1, 953ac13ac6fSEric W. Biederman GFP_KERNEL); 9547ec66d06SEric W. Biederman if (!new) 9557ec66d06SEric W. Biederman return NULL; 9567ec66d06SEric W. Biederman 957ac13ac6fSEric W. Biederman node = (struct ctl_node *)(new + 1); 958ac13ac6fSEric W. Biederman table = (struct ctl_table *)(node + 1); 9597ec66d06SEric W. Biederman new_name = (char *)(table + 2); 9607ec66d06SEric W. Biederman memcpy(new_name, name, namelen); 9617ec66d06SEric W. Biederman new_name[namelen] = '\0'; 9627ec66d06SEric W. Biederman table[0].procname = new_name; 9637ec66d06SEric W. Biederman table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO; 964ac13ac6fSEric W. Biederman init_header(&new->header, set->dir.header.root, set, node, table); 9657ec66d06SEric W. Biederman 9667ec66d06SEric W. Biederman return new; 9671f87f0b5SEric W. Biederman } 9681f87f0b5SEric W. Biederman 96960f126d9SEric W. Biederman /** 97060f126d9SEric W. Biederman * get_subdir - find or create a subdir with the specified name. 97160f126d9SEric W. Biederman * @dir: Directory to create the subdirectory in 97260f126d9SEric W. Biederman * @name: The name of the subdirectory to find or create 97360f126d9SEric W. Biederman * @namelen: The length of name 97460f126d9SEric W. Biederman * 97560f126d9SEric W. Biederman * Takes a directory with an elevated reference count so we know that 97660f126d9SEric W. Biederman * if we drop the lock the directory will not go away. Upon success 97760f126d9SEric W. Biederman * the reference is moved from @dir to the returned subdirectory. 97860f126d9SEric W. Biederman * Upon error an error code is returned and the reference on @dir is 97960f126d9SEric W. Biederman * simply dropped. 98060f126d9SEric W. Biederman */ 9810e47c99dSEric W. Biederman static struct ctl_dir *get_subdir(struct ctl_dir *dir, 9820e47c99dSEric W. Biederman const char *name, int namelen) 9837ec66d06SEric W. Biederman { 9840e47c99dSEric W. Biederman struct ctl_table_set *set = dir->header.set; 9857ec66d06SEric W. Biederman struct ctl_dir *subdir, *new = NULL; 9860eb97f38SEric W. Biederman int err; 9877ec66d06SEric W. Biederman 9887ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 9890e47c99dSEric W. Biederman subdir = find_subdir(dir, name, namelen); 9907ec66d06SEric W. Biederman if (!IS_ERR(subdir)) 9917ec66d06SEric W. Biederman goto found; 9927ec66d06SEric W. Biederman if (PTR_ERR(subdir) != -ENOENT) 9937ec66d06SEric W. Biederman goto failed; 9947ec66d06SEric W. Biederman 9957ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 9967ec66d06SEric W. Biederman new = new_dir(set, name, namelen); 9977ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 9987ec66d06SEric W. Biederman subdir = ERR_PTR(-ENOMEM); 9997ec66d06SEric W. Biederman if (!new) 10007ec66d06SEric W. Biederman goto failed; 10017ec66d06SEric W. Biederman 100260f126d9SEric W. Biederman /* Was the subdir added while we dropped the lock? */ 10030e47c99dSEric W. Biederman subdir = find_subdir(dir, name, namelen); 10047ec66d06SEric W. Biederman if (!IS_ERR(subdir)) 10057ec66d06SEric W. Biederman goto found; 10067ec66d06SEric W. Biederman if (PTR_ERR(subdir) != -ENOENT) 10077ec66d06SEric W. Biederman goto failed; 10087ec66d06SEric W. Biederman 100960f126d9SEric W. Biederman /* Nope. Use the our freshly made directory entry. */ 10100eb97f38SEric W. Biederman err = insert_header(dir, &new->header); 10110eb97f38SEric W. Biederman subdir = ERR_PTR(err); 10120eb97f38SEric W. Biederman if (err) 10130e47c99dSEric W. Biederman goto failed; 10147ec66d06SEric W. Biederman subdir = new; 10157ec66d06SEric W. Biederman found: 10167ec66d06SEric W. Biederman subdir->header.nreg++; 10177ec66d06SEric W. Biederman failed: 1018a1c83681SViresh Kumar if (IS_ERR(subdir)) { 101987ebdc00SAndrew Morton pr_err("sysctl could not get directory: "); 10206980128fSEric W. Biederman sysctl_print_dir(dir); 102187ebdc00SAndrew Morton pr_cont("/%*.*s %ld\n", 10227ec66d06SEric W. Biederman namelen, namelen, name, PTR_ERR(subdir)); 10231f87f0b5SEric W. Biederman } 10247ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 10257ec66d06SEric W. Biederman if (new) 10267ec66d06SEric W. Biederman drop_sysctl_table(&new->header); 10277ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 10287ec66d06SEric W. Biederman return subdir; 10291f87f0b5SEric W. Biederman } 10301f87f0b5SEric W. Biederman 10310e47c99dSEric W. Biederman static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir) 10320e47c99dSEric W. Biederman { 10330e47c99dSEric W. Biederman struct ctl_dir *parent; 10340e47c99dSEric W. Biederman const char *procname; 10350e47c99dSEric W. Biederman if (!dir->header.parent) 10360e47c99dSEric W. Biederman return &set->dir; 10370e47c99dSEric W. Biederman parent = xlate_dir(set, dir->header.parent); 10380e47c99dSEric W. Biederman if (IS_ERR(parent)) 10390e47c99dSEric W. Biederman return parent; 10400e47c99dSEric W. Biederman procname = dir->header.ctl_table[0].procname; 10410e47c99dSEric W. Biederman return find_subdir(parent, procname, strlen(procname)); 10420e47c99dSEric W. Biederman } 10430e47c99dSEric W. Biederman 10440e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead, 104513bcc6a2SEric W. Biederman struct ctl_table **pentry) 10460e47c99dSEric W. Biederman { 10470e47c99dSEric W. Biederman struct ctl_table_header *head; 10480e47c99dSEric W. Biederman struct ctl_table_root *root; 10490e47c99dSEric W. Biederman struct ctl_table_set *set; 10500e47c99dSEric W. Biederman struct ctl_table *entry; 10510e47c99dSEric W. Biederman struct ctl_dir *dir; 10520e47c99dSEric W. Biederman int ret; 10530e47c99dSEric W. Biederman 10540e47c99dSEric W. Biederman ret = 0; 10550e47c99dSEric W. Biederman spin_lock(&sysctl_lock); 10560e47c99dSEric W. Biederman root = (*pentry)->data; 105713bcc6a2SEric W. Biederman set = lookup_header_set(root); 10580e47c99dSEric W. Biederman dir = xlate_dir(set, (*phead)->parent); 10590e47c99dSEric W. Biederman if (IS_ERR(dir)) 10600e47c99dSEric W. Biederman ret = PTR_ERR(dir); 10610e47c99dSEric W. Biederman else { 10620e47c99dSEric W. Biederman const char *procname = (*pentry)->procname; 10630e47c99dSEric W. Biederman head = NULL; 10640e47c99dSEric W. Biederman entry = find_entry(&head, dir, procname, strlen(procname)); 10650e47c99dSEric W. Biederman ret = -ENOENT; 10660e47c99dSEric W. Biederman if (entry && use_table(head)) { 10670e47c99dSEric W. Biederman unuse_table(*phead); 10680e47c99dSEric W. Biederman *phead = head; 10690e47c99dSEric W. Biederman *pentry = entry; 10700e47c99dSEric W. Biederman ret = 0; 10710e47c99dSEric W. Biederman } 10720e47c99dSEric W. Biederman } 10730e47c99dSEric W. Biederman 10740e47c99dSEric W. Biederman spin_unlock(&sysctl_lock); 10750e47c99dSEric W. Biederman return ret; 10760e47c99dSEric W. Biederman } 10770e47c99dSEric W. Biederman 10787c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...) 10797c60c48fSEric W. Biederman { 10807c60c48fSEric W. Biederman struct va_format vaf; 10817c60c48fSEric W. Biederman va_list args; 10827c60c48fSEric W. Biederman 10837c60c48fSEric W. Biederman va_start(args, fmt); 10847c60c48fSEric W. Biederman vaf.fmt = fmt; 10857c60c48fSEric W. Biederman vaf.va = &args; 10867c60c48fSEric W. Biederman 108787ebdc00SAndrew Morton pr_err("sysctl table check failed: %s/%s %pV\n", 10887c60c48fSEric W. Biederman path, table->procname, &vaf); 10897c60c48fSEric W. Biederman 10907c60c48fSEric W. Biederman va_end(args); 10917c60c48fSEric W. Biederman return -EINVAL; 10927c60c48fSEric W. Biederman } 10937c60c48fSEric W. Biederman 10944f2fec00SLuis R. Rodriguez static int sysctl_check_table_array(const char *path, struct ctl_table *table) 10954f2fec00SLuis R. Rodriguez { 10964f2fec00SLuis R. Rodriguez int err = 0; 10974f2fec00SLuis R. Rodriguez 109861d9b56aSLuis R. Rodriguez if ((table->proc_handler == proc_douintvec) || 109961d9b56aSLuis R. Rodriguez (table->proc_handler == proc_douintvec_minmax)) { 11004f2fec00SLuis R. Rodriguez if (table->maxlen != sizeof(unsigned int)) 110164a11f3dSWaiman Long err |= sysctl_err(path, table, "array not allowed"); 11024f2fec00SLuis R. Rodriguez } 11034f2fec00SLuis R. Rodriguez 11044f2fec00SLuis R. Rodriguez return err; 11054f2fec00SLuis R. Rodriguez } 11064f2fec00SLuis R. Rodriguez 11077c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table) 11087c60c48fSEric W. Biederman { 11097c60c48fSEric W. Biederman int err = 0; 11107c60c48fSEric W. Biederman for (; table->procname; table++) { 11117c60c48fSEric W. Biederman if (table->child) 111289c5b53bSLuis R. Rodriguez err |= sysctl_err(path, table, "Not a file"); 11137c60c48fSEric W. Biederman 11141f87f0b5SEric W. Biederman if ((table->proc_handler == proc_dostring) || 11151f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec) || 11161680a386SLiping Zhang (table->proc_handler == proc_douintvec) || 111761d9b56aSLuis R. Rodriguez (table->proc_handler == proc_douintvec_minmax) || 11181f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_minmax) || 11191f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_jiffies) || 11201f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_userhz_jiffies) || 11211f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_ms_jiffies) || 11221f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_minmax) || 11231f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) { 11241f87f0b5SEric W. Biederman if (!table->data) 112589c5b53bSLuis R. Rodriguez err |= sysctl_err(path, table, "No data"); 11261f87f0b5SEric W. Biederman if (!table->maxlen) 112789c5b53bSLuis R. Rodriguez err |= sysctl_err(path, table, "No maxlen"); 11284f2fec00SLuis R. Rodriguez else 11294f2fec00SLuis R. Rodriguez err |= sysctl_check_table_array(path, table); 11301f87f0b5SEric W. Biederman } 11311f87f0b5SEric W. Biederman if (!table->proc_handler) 113289c5b53bSLuis R. Rodriguez err |= sysctl_err(path, table, "No proc_handler"); 11337c60c48fSEric W. Biederman 11347c60c48fSEric W. Biederman if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode) 113589c5b53bSLuis R. Rodriguez err |= sysctl_err(path, table, "bogus .mode 0%o", 11367c60c48fSEric W. Biederman table->mode); 11371f87f0b5SEric W. Biederman } 11387c60c48fSEric W. Biederman return err; 11391f87f0b5SEric W. Biederman } 11401f87f0b5SEric W. Biederman 11410e47c99dSEric W. Biederman static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table, 11420e47c99dSEric W. Biederman struct ctl_table_root *link_root) 11430e47c99dSEric W. Biederman { 11440e47c99dSEric W. Biederman struct ctl_table *link_table, *entry, *link; 11450e47c99dSEric W. Biederman struct ctl_table_header *links; 1146ac13ac6fSEric W. Biederman struct ctl_node *node; 11470e47c99dSEric W. Biederman char *link_name; 11480e47c99dSEric W. Biederman int nr_entries, name_bytes; 11490e47c99dSEric W. Biederman 11500e47c99dSEric W. Biederman name_bytes = 0; 11510e47c99dSEric W. Biederman nr_entries = 0; 11520e47c99dSEric W. Biederman for (entry = table; entry->procname; entry++) { 11530e47c99dSEric W. Biederman nr_entries++; 11540e47c99dSEric W. Biederman name_bytes += strlen(entry->procname) + 1; 11550e47c99dSEric W. Biederman } 11560e47c99dSEric W. Biederman 11570e47c99dSEric W. Biederman links = kzalloc(sizeof(struct ctl_table_header) + 1158ac13ac6fSEric W. Biederman sizeof(struct ctl_node)*nr_entries + 11590e47c99dSEric W. Biederman sizeof(struct ctl_table)*(nr_entries + 1) + 11600e47c99dSEric W. Biederman name_bytes, 11610e47c99dSEric W. Biederman GFP_KERNEL); 11620e47c99dSEric W. Biederman 11630e47c99dSEric W. Biederman if (!links) 11640e47c99dSEric W. Biederman return NULL; 11650e47c99dSEric W. Biederman 1166ac13ac6fSEric W. Biederman node = (struct ctl_node *)(links + 1); 1167ac13ac6fSEric W. Biederman link_table = (struct ctl_table *)(node + nr_entries); 11680e47c99dSEric W. Biederman link_name = (char *)&link_table[nr_entries + 1]; 11690e47c99dSEric W. Biederman 11700e47c99dSEric W. Biederman for (link = link_table, entry = table; entry->procname; link++, entry++) { 11710e47c99dSEric W. Biederman int len = strlen(entry->procname) + 1; 11720e47c99dSEric W. Biederman memcpy(link_name, entry->procname, len); 11730e47c99dSEric W. Biederman link->procname = link_name; 11740e47c99dSEric W. Biederman link->mode = S_IFLNK|S_IRWXUGO; 11750e47c99dSEric W. Biederman link->data = link_root; 11760e47c99dSEric W. Biederman link_name += len; 11770e47c99dSEric W. Biederman } 1178ac13ac6fSEric W. Biederman init_header(links, dir->header.root, dir->header.set, node, link_table); 11790e47c99dSEric W. Biederman links->nreg = nr_entries; 11800e47c99dSEric W. Biederman 11810e47c99dSEric W. Biederman return links; 11820e47c99dSEric W. Biederman } 11830e47c99dSEric W. Biederman 11840e47c99dSEric W. Biederman static bool get_links(struct ctl_dir *dir, 11850e47c99dSEric W. Biederman struct ctl_table *table, struct ctl_table_root *link_root) 11860e47c99dSEric W. Biederman { 11870e47c99dSEric W. Biederman struct ctl_table_header *head; 11880e47c99dSEric W. Biederman struct ctl_table *entry, *link; 11890e47c99dSEric W. Biederman 11900e47c99dSEric W. Biederman /* Are there links available for every entry in table? */ 11910e47c99dSEric W. Biederman for (entry = table; entry->procname; entry++) { 11920e47c99dSEric W. Biederman const char *procname = entry->procname; 11930e47c99dSEric W. Biederman link = find_entry(&head, dir, procname, strlen(procname)); 11940e47c99dSEric W. Biederman if (!link) 11950e47c99dSEric W. Biederman return false; 11960e47c99dSEric W. Biederman if (S_ISDIR(link->mode) && S_ISDIR(entry->mode)) 11970e47c99dSEric W. Biederman continue; 11980e47c99dSEric W. Biederman if (S_ISLNK(link->mode) && (link->data == link_root)) 11990e47c99dSEric W. Biederman continue; 12000e47c99dSEric W. Biederman return false; 12010e47c99dSEric W. Biederman } 12020e47c99dSEric W. Biederman 12030e47c99dSEric W. Biederman /* The checks passed. Increase the registration count on the links */ 12040e47c99dSEric W. Biederman for (entry = table; entry->procname; entry++) { 12050e47c99dSEric W. Biederman const char *procname = entry->procname; 12060e47c99dSEric W. Biederman link = find_entry(&head, dir, procname, strlen(procname)); 12070e47c99dSEric W. Biederman head->nreg++; 12080e47c99dSEric W. Biederman } 12090e47c99dSEric W. Biederman return true; 12100e47c99dSEric W. Biederman } 12110e47c99dSEric W. Biederman 12120e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head) 12130e47c99dSEric W. Biederman { 12140e47c99dSEric W. Biederman struct ctl_table_set *root_set = &sysctl_table_root.default_set; 12150e47c99dSEric W. Biederman struct ctl_dir *core_parent = NULL; 12160e47c99dSEric W. Biederman struct ctl_table_header *links; 12170e47c99dSEric W. Biederman int err; 12180e47c99dSEric W. Biederman 12190e47c99dSEric W. Biederman if (head->set == root_set) 12200e47c99dSEric W. Biederman return 0; 12210e47c99dSEric W. Biederman 12220e47c99dSEric W. Biederman core_parent = xlate_dir(root_set, head->parent); 12230e47c99dSEric W. Biederman if (IS_ERR(core_parent)) 12240e47c99dSEric W. Biederman return 0; 12250e47c99dSEric W. Biederman 12260e47c99dSEric W. Biederman if (get_links(core_parent, head->ctl_table, head->root)) 12270e47c99dSEric W. Biederman return 0; 12280e47c99dSEric W. Biederman 12290e47c99dSEric W. Biederman core_parent->header.nreg++; 12300e47c99dSEric W. Biederman spin_unlock(&sysctl_lock); 12310e47c99dSEric W. Biederman 12320e47c99dSEric W. Biederman links = new_links(core_parent, head->ctl_table, head->root); 12330e47c99dSEric W. Biederman 12340e47c99dSEric W. Biederman spin_lock(&sysctl_lock); 12350e47c99dSEric W. Biederman err = -ENOMEM; 12360e47c99dSEric W. Biederman if (!links) 12370e47c99dSEric W. Biederman goto out; 12380e47c99dSEric W. Biederman 12390e47c99dSEric W. Biederman err = 0; 12400e47c99dSEric W. Biederman if (get_links(core_parent, head->ctl_table, head->root)) { 12410e47c99dSEric W. Biederman kfree(links); 12420e47c99dSEric W. Biederman goto out; 12430e47c99dSEric W. Biederman } 12440e47c99dSEric W. Biederman 12450e47c99dSEric W. Biederman err = insert_header(core_parent, links); 12460e47c99dSEric W. Biederman if (err) 12470e47c99dSEric W. Biederman kfree(links); 12480e47c99dSEric W. Biederman out: 12490e47c99dSEric W. Biederman drop_sysctl_table(&core_parent->header); 12500e47c99dSEric W. Biederman return err; 12510e47c99dSEric W. Biederman } 12520e47c99dSEric W. Biederman 12531f87f0b5SEric W. Biederman /** 1254f728019bSEric W. Biederman * __register_sysctl_table - register a leaf sysctl table 125560a47a2eSEric W. Biederman * @set: Sysctl tree to register on 12561f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 12571f87f0b5SEric W. Biederman * @table: the top-level table structure 12581f87f0b5SEric W. Biederman * 12591f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 12601f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 12611f87f0b5SEric W. Biederman * 12621f87f0b5SEric W. Biederman * The members of the &struct ctl_table structure are used as follows: 12631f87f0b5SEric W. Biederman * 12641f87f0b5SEric W. Biederman * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not 12651f87f0b5SEric W. Biederman * enter a sysctl file 12661f87f0b5SEric W. Biederman * 12671f87f0b5SEric W. Biederman * data - a pointer to data for use by proc_handler 12681f87f0b5SEric W. Biederman * 12691f87f0b5SEric W. Biederman * maxlen - the maximum size in bytes of the data 12701f87f0b5SEric W. Biederman * 1271f728019bSEric W. Biederman * mode - the file permissions for the /proc/sys file 12721f87f0b5SEric W. Biederman * 1273f728019bSEric W. Biederman * child - must be %NULL. 12741f87f0b5SEric W. Biederman * 12751f87f0b5SEric W. Biederman * proc_handler - the text handler routine (described below) 12761f87f0b5SEric W. Biederman * 12771f87f0b5SEric W. Biederman * extra1, extra2 - extra pointers usable by the proc handler routines 12781f87f0b5SEric W. Biederman * 12791f87f0b5SEric W. Biederman * Leaf nodes in the sysctl tree will be represented by a single file 12801f87f0b5SEric W. Biederman * under /proc; non-leaf nodes will be represented by directories. 12811f87f0b5SEric W. Biederman * 1282f728019bSEric W. Biederman * There must be a proc_handler routine for any terminal nodes. 1283f728019bSEric W. Biederman * Several default handlers are available to cover common cases - 12841f87f0b5SEric W. Biederman * 12851f87f0b5SEric W. Biederman * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(), 12861f87f0b5SEric W. Biederman * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), 12871f87f0b5SEric W. Biederman * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax() 12881f87f0b5SEric W. Biederman * 12891f87f0b5SEric W. Biederman * It is the handler's job to read the input buffer from user memory 12901f87f0b5SEric W. Biederman * and process it. The handler should return 0 on success. 12911f87f0b5SEric W. Biederman * 12921f87f0b5SEric W. Biederman * This routine returns %NULL on a failure to register, and a pointer 12931f87f0b5SEric W. Biederman * to the table header on success. 12941f87f0b5SEric W. Biederman */ 12956e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table( 129660a47a2eSEric W. Biederman struct ctl_table_set *set, 12976e9d5164SEric W. Biederman const char *path, struct ctl_table *table) 12981f87f0b5SEric W. Biederman { 129960a47a2eSEric W. Biederman struct ctl_table_root *root = set->dir.header.root; 13001f87f0b5SEric W. Biederman struct ctl_table_header *header; 13016e9d5164SEric W. Biederman const char *name, *nextname; 13027ec66d06SEric W. Biederman struct ctl_dir *dir; 1303ac13ac6fSEric W. Biederman struct ctl_table *entry; 1304ac13ac6fSEric W. Biederman struct ctl_node *node; 1305ac13ac6fSEric W. Biederman int nr_entries = 0; 13061f87f0b5SEric W. Biederman 1307ac13ac6fSEric W. Biederman for (entry = table; entry->procname; entry++) 1308ac13ac6fSEric W. Biederman nr_entries++; 1309ac13ac6fSEric W. Biederman 1310ac13ac6fSEric W. Biederman header = kzalloc(sizeof(struct ctl_table_header) + 1311ac13ac6fSEric W. Biederman sizeof(struct ctl_node)*nr_entries, GFP_KERNEL); 13121f87f0b5SEric W. Biederman if (!header) 13131f87f0b5SEric W. Biederman return NULL; 13141f87f0b5SEric W. Biederman 1315ac13ac6fSEric W. Biederman node = (struct ctl_node *)(header + 1); 1316ac13ac6fSEric W. Biederman init_header(header, root, set, node, table); 13177c60c48fSEric W. Biederman if (sysctl_check_table(path, table)) 13187c60c48fSEric W. Biederman goto fail; 13198d6ecfccSEric W. Biederman 13201f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 13210e47c99dSEric W. Biederman dir = &set->dir; 132260f126d9SEric W. Biederman /* Reference moved down the diretory tree get_subdir */ 13237ec66d06SEric W. Biederman dir->header.nreg++; 13247ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 13257ec66d06SEric W. Biederman 13267ec66d06SEric W. Biederman /* Find the directory for the ctl_table */ 13277ec66d06SEric W. Biederman for (name = path; name; name = nextname) { 13287ec66d06SEric W. Biederman int namelen; 13297ec66d06SEric W. Biederman nextname = strchr(name, '/'); 13307ec66d06SEric W. Biederman if (nextname) { 13317ec66d06SEric W. Biederman namelen = nextname - name; 13327ec66d06SEric W. Biederman nextname++; 13337ec66d06SEric W. Biederman } else { 13347ec66d06SEric W. Biederman namelen = strlen(name); 13357ec66d06SEric W. Biederman } 13367ec66d06SEric W. Biederman if (namelen == 0) 13371f87f0b5SEric W. Biederman continue; 13387ec66d06SEric W. Biederman 13390e47c99dSEric W. Biederman dir = get_subdir(dir, name, namelen); 13407ec66d06SEric W. Biederman if (IS_ERR(dir)) 13417ec66d06SEric W. Biederman goto fail; 13421f87f0b5SEric W. Biederman } 13430e47c99dSEric W. Biederman 13447ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 13450e47c99dSEric W. Biederman if (insert_header(dir, header)) 13460e47c99dSEric W. Biederman goto fail_put_dir_locked; 13470e47c99dSEric W. Biederman 13487ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 13491f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 13501f87f0b5SEric W. Biederman 13511f87f0b5SEric W. Biederman return header; 13520e47c99dSEric W. Biederman 13537ec66d06SEric W. Biederman fail_put_dir_locked: 13547ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 13557c60c48fSEric W. Biederman spin_unlock(&sysctl_lock); 13567c60c48fSEric W. Biederman fail: 13577c60c48fSEric W. Biederman kfree(header); 13587c60c48fSEric W. Biederman dump_stack(); 13597c60c48fSEric W. Biederman return NULL; 13601f87f0b5SEric W. Biederman } 13611f87f0b5SEric W. Biederman 1362fea478d4SEric W. Biederman /** 1363fea478d4SEric W. Biederman * register_sysctl - register a sysctl table 1364fea478d4SEric W. Biederman * @path: The path to the directory the sysctl table is in. 1365fea478d4SEric W. Biederman * @table: the table structure 1366fea478d4SEric W. Biederman * 1367fea478d4SEric W. Biederman * Register a sysctl table. @table should be a filled in ctl_table 1368fea478d4SEric W. Biederman * array. A completely 0 filled entry terminates the table. 1369fea478d4SEric W. Biederman * 1370fea478d4SEric W. Biederman * See __register_sysctl_table for more details. 1371fea478d4SEric W. Biederman */ 1372fea478d4SEric W. Biederman struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table) 1373fea478d4SEric W. Biederman { 1374fea478d4SEric W. Biederman return __register_sysctl_table(&sysctl_table_root.default_set, 1375fea478d4SEric W. Biederman path, table); 1376fea478d4SEric W. Biederman } 1377fea478d4SEric W. Biederman EXPORT_SYMBOL(register_sysctl); 1378fea478d4SEric W. Biederman 13796e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name) 13806e9d5164SEric W. Biederman { 13816e9d5164SEric W. Biederman int namelen; 13826e9d5164SEric W. Biederman namelen = strlen(name); 13836e9d5164SEric W. Biederman if (((pos - path) + namelen + 2) >= PATH_MAX) 13846e9d5164SEric W. Biederman return NULL; 13856e9d5164SEric W. Biederman memcpy(pos, name, namelen); 13866e9d5164SEric W. Biederman pos[namelen] = '/'; 13876e9d5164SEric W. Biederman pos[namelen + 1] = '\0'; 13886e9d5164SEric W. Biederman pos += namelen + 1; 13896e9d5164SEric W. Biederman return pos; 13906e9d5164SEric W. Biederman } 13916e9d5164SEric W. Biederman 1392f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table) 1393f728019bSEric W. Biederman { 1394f728019bSEric W. Biederman int has_files = 0; 1395f728019bSEric W. Biederman int nr_subheaders = 0; 1396f728019bSEric W. Biederman struct ctl_table *entry; 1397f728019bSEric W. Biederman 1398f728019bSEric W. Biederman /* special case: no directory and empty directory */ 1399f728019bSEric W. Biederman if (!table || !table->procname) 1400f728019bSEric W. Biederman return 1; 1401f728019bSEric W. Biederman 1402f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1403f728019bSEric W. Biederman if (entry->child) 1404f728019bSEric W. Biederman nr_subheaders += count_subheaders(entry->child); 1405f728019bSEric W. Biederman else 1406f728019bSEric W. Biederman has_files = 1; 1407f728019bSEric W. Biederman } 1408f728019bSEric W. Biederman return nr_subheaders + has_files; 1409f728019bSEric W. Biederman } 1410f728019bSEric W. Biederman 1411f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos, 141260a47a2eSEric W. Biederman struct ctl_table_header ***subheader, struct ctl_table_set *set, 1413f728019bSEric W. Biederman struct ctl_table *table) 1414f728019bSEric W. Biederman { 1415f728019bSEric W. Biederman struct ctl_table *ctl_table_arg = NULL; 1416f728019bSEric W. Biederman struct ctl_table *entry, *files; 1417f728019bSEric W. Biederman int nr_files = 0; 1418f728019bSEric W. Biederman int nr_dirs = 0; 1419f728019bSEric W. Biederman int err = -ENOMEM; 1420f728019bSEric W. Biederman 1421f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1422f728019bSEric W. Biederman if (entry->child) 1423f728019bSEric W. Biederman nr_dirs++; 1424f728019bSEric W. Biederman else 1425f728019bSEric W. Biederman nr_files++; 1426f728019bSEric W. Biederman } 1427f728019bSEric W. Biederman 1428f728019bSEric W. Biederman files = table; 1429f728019bSEric W. Biederman /* If there are mixed files and directories we need a new table */ 1430f728019bSEric W. Biederman if (nr_dirs && nr_files) { 1431f728019bSEric W. Biederman struct ctl_table *new; 14326396bb22SKees Cook files = kcalloc(nr_files + 1, sizeof(struct ctl_table), 1433f728019bSEric W. Biederman GFP_KERNEL); 1434f728019bSEric W. Biederman if (!files) 1435f728019bSEric W. Biederman goto out; 1436f728019bSEric W. Biederman 1437f728019bSEric W. Biederman ctl_table_arg = files; 1438f728019bSEric W. Biederman for (new = files, entry = table; entry->procname; entry++) { 1439f728019bSEric W. Biederman if (entry->child) 1440f728019bSEric W. Biederman continue; 1441f728019bSEric W. Biederman *new = *entry; 1442f728019bSEric W. Biederman new++; 1443f728019bSEric W. Biederman } 1444f728019bSEric W. Biederman } 1445f728019bSEric W. Biederman 1446f728019bSEric W. Biederman /* Register everything except a directory full of subdirectories */ 1447f728019bSEric W. Biederman if (nr_files || !nr_dirs) { 1448f728019bSEric W. Biederman struct ctl_table_header *header; 144960a47a2eSEric W. Biederman header = __register_sysctl_table(set, path, files); 1450f728019bSEric W. Biederman if (!header) { 1451f728019bSEric W. Biederman kfree(ctl_table_arg); 1452f728019bSEric W. Biederman goto out; 1453f728019bSEric W. Biederman } 1454f728019bSEric W. Biederman 1455f728019bSEric W. Biederman /* Remember if we need to free the file table */ 1456f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1457f728019bSEric W. Biederman **subheader = header; 1458f728019bSEric W. Biederman (*subheader)++; 1459f728019bSEric W. Biederman } 1460f728019bSEric W. Biederman 1461f728019bSEric W. Biederman /* Recurse into the subdirectories. */ 1462f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1463f728019bSEric W. Biederman char *child_pos; 1464f728019bSEric W. Biederman 1465f728019bSEric W. Biederman if (!entry->child) 1466f728019bSEric W. Biederman continue; 1467f728019bSEric W. Biederman 1468f728019bSEric W. Biederman err = -ENAMETOOLONG; 1469f728019bSEric W. Biederman child_pos = append_path(path, pos, entry->procname); 1470f728019bSEric W. Biederman if (!child_pos) 1471f728019bSEric W. Biederman goto out; 1472f728019bSEric W. Biederman 1473f728019bSEric W. Biederman err = register_leaf_sysctl_tables(path, child_pos, subheader, 147460a47a2eSEric W. Biederman set, entry->child); 1475f728019bSEric W. Biederman pos[0] = '\0'; 1476f728019bSEric W. Biederman if (err) 1477f728019bSEric W. Biederman goto out; 1478f728019bSEric W. Biederman } 1479f728019bSEric W. Biederman err = 0; 1480f728019bSEric W. Biederman out: 1481f728019bSEric W. Biederman /* On failure our caller will unregister all registered subheaders */ 1482f728019bSEric W. Biederman return err; 1483f728019bSEric W. Biederman } 1484f728019bSEric W. Biederman 14856e9d5164SEric W. Biederman /** 14866e9d5164SEric W. Biederman * __register_sysctl_paths - register a sysctl table hierarchy 148760a47a2eSEric W. Biederman * @set: Sysctl tree to register on 14886e9d5164SEric W. Biederman * @path: The path to the directory the sysctl table is in. 14896e9d5164SEric W. Biederman * @table: the top-level table structure 14906e9d5164SEric W. Biederman * 14916e9d5164SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 14926e9d5164SEric W. Biederman * array. A completely 0 filled entry terminates the table. 14936e9d5164SEric W. Biederman * 14946e9d5164SEric W. Biederman * See __register_sysctl_table for more details. 14956e9d5164SEric W. Biederman */ 14966e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths( 149760a47a2eSEric W. Biederman struct ctl_table_set *set, 14986e9d5164SEric W. Biederman const struct ctl_path *path, struct ctl_table *table) 14996e9d5164SEric W. Biederman { 1500ec6a5266SEric W. Biederman struct ctl_table *ctl_table_arg = table; 1501f728019bSEric W. Biederman int nr_subheaders = count_subheaders(table); 1502f728019bSEric W. Biederman struct ctl_table_header *header = NULL, **subheaders, **subheader; 15036e9d5164SEric W. Biederman const struct ctl_path *component; 15046e9d5164SEric W. Biederman char *new_path, *pos; 15056e9d5164SEric W. Biederman 15066e9d5164SEric W. Biederman pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL); 15076e9d5164SEric W. Biederman if (!new_path) 15086e9d5164SEric W. Biederman return NULL; 15096e9d5164SEric W. Biederman 15106e9d5164SEric W. Biederman pos[0] = '\0'; 15116e9d5164SEric W. Biederman for (component = path; component->procname; component++) { 15126e9d5164SEric W. Biederman pos = append_path(new_path, pos, component->procname); 15136e9d5164SEric W. Biederman if (!pos) 15146e9d5164SEric W. Biederman goto out; 15156e9d5164SEric W. Biederman } 1516ec6a5266SEric W. Biederman while (table->procname && table->child && !table[1].procname) { 1517ec6a5266SEric W. Biederman pos = append_path(new_path, pos, table->procname); 1518ec6a5266SEric W. Biederman if (!pos) 1519ec6a5266SEric W. Biederman goto out; 1520ec6a5266SEric W. Biederman table = table->child; 1521ec6a5266SEric W. Biederman } 1522f728019bSEric W. Biederman if (nr_subheaders == 1) { 152360a47a2eSEric W. Biederman header = __register_sysctl_table(set, new_path, table); 1524ec6a5266SEric W. Biederman if (header) 1525ec6a5266SEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1526f728019bSEric W. Biederman } else { 1527f728019bSEric W. Biederman header = kzalloc(sizeof(*header) + 1528f728019bSEric W. Biederman sizeof(*subheaders)*nr_subheaders, GFP_KERNEL); 1529f728019bSEric W. Biederman if (!header) 1530f728019bSEric W. Biederman goto out; 1531f728019bSEric W. Biederman 1532f728019bSEric W. Biederman subheaders = (struct ctl_table_header **) (header + 1); 1533f728019bSEric W. Biederman subheader = subheaders; 1534f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1535f728019bSEric W. Biederman 1536f728019bSEric W. Biederman if (register_leaf_sysctl_tables(new_path, pos, &subheader, 153760a47a2eSEric W. Biederman set, table)) 1538f728019bSEric W. Biederman goto err_register_leaves; 1539f728019bSEric W. Biederman } 1540f728019bSEric W. Biederman 15416e9d5164SEric W. Biederman out: 15426e9d5164SEric W. Biederman kfree(new_path); 15436e9d5164SEric W. Biederman return header; 1544f728019bSEric W. Biederman 1545f728019bSEric W. Biederman err_register_leaves: 1546f728019bSEric W. Biederman while (subheader > subheaders) { 1547f728019bSEric W. Biederman struct ctl_table_header *subh = *(--subheader); 1548f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1549f728019bSEric W. Biederman unregister_sysctl_table(subh); 1550f728019bSEric W. Biederman kfree(table); 1551f728019bSEric W. Biederman } 1552f728019bSEric W. Biederman kfree(header); 1553f728019bSEric W. Biederman header = NULL; 1554f728019bSEric W. Biederman goto out; 15556e9d5164SEric W. Biederman } 15566e9d5164SEric W. Biederman 15571f87f0b5SEric W. Biederman /** 15581f87f0b5SEric W. Biederman * register_sysctl_table_path - register a sysctl table hierarchy 15591f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 15601f87f0b5SEric W. Biederman * @table: the top-level table structure 15611f87f0b5SEric W. Biederman * 15621f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 15631f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 15641f87f0b5SEric W. Biederman * 15651f87f0b5SEric W. Biederman * See __register_sysctl_paths for more details. 15661f87f0b5SEric W. Biederman */ 15671f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path, 15681f87f0b5SEric W. Biederman struct ctl_table *table) 15691f87f0b5SEric W. Biederman { 157060a47a2eSEric W. Biederman return __register_sysctl_paths(&sysctl_table_root.default_set, 15711f87f0b5SEric W. Biederman path, table); 15721f87f0b5SEric W. Biederman } 15731f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths); 15741f87f0b5SEric W. Biederman 15751f87f0b5SEric W. Biederman /** 15761f87f0b5SEric W. Biederman * register_sysctl_table - register a sysctl table hierarchy 15771f87f0b5SEric W. Biederman * @table: the top-level table structure 15781f87f0b5SEric W. Biederman * 15791f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 15801f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 15811f87f0b5SEric W. Biederman * 15821f87f0b5SEric W. Biederman * See register_sysctl_paths for more details. 15831f87f0b5SEric W. Biederman */ 15841f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table) 15851f87f0b5SEric W. Biederman { 15861f87f0b5SEric W. Biederman static const struct ctl_path null_path[] = { {} }; 15871f87f0b5SEric W. Biederman 15881f87f0b5SEric W. Biederman return register_sysctl_paths(null_path, table); 15891f87f0b5SEric W. Biederman } 15901f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table); 15911f87f0b5SEric W. Biederman 15920e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header) 15930e47c99dSEric W. Biederman { 15940e47c99dSEric W. Biederman struct ctl_table_set *root_set = &sysctl_table_root.default_set; 15950e47c99dSEric W. Biederman struct ctl_table_root *root = header->root; 15960e47c99dSEric W. Biederman struct ctl_dir *parent = header->parent; 15970e47c99dSEric W. Biederman struct ctl_dir *core_parent; 15980e47c99dSEric W. Biederman struct ctl_table *entry; 15990e47c99dSEric W. Biederman 16000e47c99dSEric W. Biederman if (header->set == root_set) 16010e47c99dSEric W. Biederman return; 16020e47c99dSEric W. Biederman 16030e47c99dSEric W. Biederman core_parent = xlate_dir(root_set, parent); 16040e47c99dSEric W. Biederman if (IS_ERR(core_parent)) 16050e47c99dSEric W. Biederman return; 16060e47c99dSEric W. Biederman 16070e47c99dSEric W. Biederman for (entry = header->ctl_table; entry->procname; entry++) { 16080e47c99dSEric W. Biederman struct ctl_table_header *link_head; 16090e47c99dSEric W. Biederman struct ctl_table *link; 16100e47c99dSEric W. Biederman const char *name = entry->procname; 16110e47c99dSEric W. Biederman 16120e47c99dSEric W. Biederman link = find_entry(&link_head, core_parent, name, strlen(name)); 16130e47c99dSEric W. Biederman if (link && 16140e47c99dSEric W. Biederman ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) || 16150e47c99dSEric W. Biederman (S_ISLNK(link->mode) && (link->data == root)))) { 16160e47c99dSEric W. Biederman drop_sysctl_table(link_head); 16170e47c99dSEric W. Biederman } 16180e47c99dSEric W. Biederman else { 161987ebdc00SAndrew Morton pr_err("sysctl link missing during unregister: "); 16200e47c99dSEric W. Biederman sysctl_print_dir(parent); 162187ebdc00SAndrew Morton pr_cont("/%s\n", name); 16220e47c99dSEric W. Biederman } 16230e47c99dSEric W. Biederman } 16240e47c99dSEric W. Biederman } 16250e47c99dSEric W. Biederman 1626938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header) 1627938aaa4fSEric W. Biederman { 16287ec66d06SEric W. Biederman struct ctl_dir *parent = header->parent; 16297ec66d06SEric W. Biederman 1630938aaa4fSEric W. Biederman if (--header->nreg) 1631938aaa4fSEric W. Biederman return; 1632938aaa4fSEric W. Biederman 163389189557SYueHaibing if (parent) { 16340e47c99dSEric W. Biederman put_links(header); 1635938aaa4fSEric W. Biederman start_unregistering(header); 163689189557SYueHaibing } 163789189557SYueHaibing 1638938aaa4fSEric W. Biederman if (!--header->count) 1639938aaa4fSEric W. Biederman kfree_rcu(header, rcu); 16407ec66d06SEric W. Biederman 16417ec66d06SEric W. Biederman if (parent) 16427ec66d06SEric W. Biederman drop_sysctl_table(&parent->header); 1643938aaa4fSEric W. Biederman } 1644938aaa4fSEric W. Biederman 16451f87f0b5SEric W. Biederman /** 16461f87f0b5SEric W. Biederman * unregister_sysctl_table - unregister a sysctl table hierarchy 16471f87f0b5SEric W. Biederman * @header: the header returned from register_sysctl_table 16481f87f0b5SEric W. Biederman * 16491f87f0b5SEric W. Biederman * Unregisters the sysctl table and all children. proc entries may not 16501f87f0b5SEric W. Biederman * actually be removed until they are no longer used by anyone. 16511f87f0b5SEric W. Biederman */ 16521f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header) 16531f87f0b5SEric W. Biederman { 1654f728019bSEric W. Biederman int nr_subheaders; 16551f87f0b5SEric W. Biederman might_sleep(); 16561f87f0b5SEric W. Biederman 16571f87f0b5SEric W. Biederman if (header == NULL) 16581f87f0b5SEric W. Biederman return; 16591f87f0b5SEric W. Biederman 1660f728019bSEric W. Biederman nr_subheaders = count_subheaders(header->ctl_table_arg); 1661f728019bSEric W. Biederman if (unlikely(nr_subheaders > 1)) { 1662f728019bSEric W. Biederman struct ctl_table_header **subheaders; 1663f728019bSEric W. Biederman int i; 1664f728019bSEric W. Biederman 1665f728019bSEric W. Biederman subheaders = (struct ctl_table_header **)(header + 1); 1666f728019bSEric W. Biederman for (i = nr_subheaders -1; i >= 0; i--) { 1667f728019bSEric W. Biederman struct ctl_table_header *subh = subheaders[i]; 1668f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1669f728019bSEric W. Biederman unregister_sysctl_table(subh); 1670f728019bSEric W. Biederman kfree(table); 1671f728019bSEric W. Biederman } 1672f728019bSEric W. Biederman kfree(header); 1673f728019bSEric W. Biederman return; 1674f728019bSEric W. Biederman } 1675f728019bSEric W. Biederman 16761f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1677938aaa4fSEric W. Biederman drop_sysctl_table(header); 16781f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 16791f87f0b5SEric W. Biederman } 16801f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table); 16811f87f0b5SEric W. Biederman 16820e47c99dSEric W. Biederman void setup_sysctl_set(struct ctl_table_set *set, 16839eb47c26SEric W. Biederman struct ctl_table_root *root, 16841f87f0b5SEric W. Biederman int (*is_seen)(struct ctl_table_set *)) 16851f87f0b5SEric W. Biederman { 16861347440dSDan Carpenter memset(set, 0, sizeof(*set)); 16870e47c99dSEric W. Biederman set->is_seen = is_seen; 1688ac13ac6fSEric W. Biederman init_header(&set->dir.header, root, set, NULL, root_table); 16891f87f0b5SEric W. Biederman } 16901f87f0b5SEric W. Biederman 169197324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set) 169297324cd8SEric W. Biederman { 1693ac13ac6fSEric W. Biederman WARN_ON(!RB_EMPTY_ROOT(&set->dir.root)); 169497324cd8SEric W. Biederman } 16951f87f0b5SEric W. Biederman 16961e0edd3fSAlexey Dobriyan int __init proc_sys_init(void) 169777b14db5SEric W. Biederman { 1698e1675231SAlexey Dobriyan struct proc_dir_entry *proc_sys_root; 1699e1675231SAlexey Dobriyan 170077b14db5SEric W. Biederman proc_sys_root = proc_mkdir("sys", NULL); 17019043476fSAl Viro proc_sys_root->proc_iops = &proc_sys_dir_operations; 1702d56c0d45SAlexey Dobriyan proc_sys_root->proc_dir_ops = &proc_sys_dir_file_operations; 170377b14db5SEric W. Biederman proc_sys_root->nlink = 0; 1704de4e83bdSEric W. Biederman 1705de4e83bdSEric W. Biederman return sysctl_init(); 170677b14db5SEric W. Biederman } 17073db978d4SVlastimil Babka 17080a477e1aSVlastimil Babka struct sysctl_alias { 17090a477e1aSVlastimil Babka const char *kernel_param; 17100a477e1aSVlastimil Babka const char *sysctl_param; 17110a477e1aSVlastimil Babka }; 17120a477e1aSVlastimil Babka 17130a477e1aSVlastimil Babka /* 17140a477e1aSVlastimil Babka * Historically some settings had both sysctl and a command line parameter. 17150a477e1aSVlastimil Babka * With the generic sysctl. parameter support, we can handle them at a single 17160a477e1aSVlastimil Babka * place and only keep the historical name for compatibility. This is not meant 17170a477e1aSVlastimil Babka * to add brand new aliases. When adding existing aliases, consider whether 17180a477e1aSVlastimil Babka * the possibly different moment of changing the value (e.g. from early_param 17190a477e1aSVlastimil Babka * to the moment do_sysctl_args() is called) is an issue for the specific 17200a477e1aSVlastimil Babka * parameter. 17210a477e1aSVlastimil Babka */ 17220a477e1aSVlastimil Babka static const struct sysctl_alias sysctl_aliases[] = { 1723f117955aSGuilherme G. Piccoli {"hardlockup_all_cpu_backtrace", "kernel.hardlockup_all_cpu_backtrace" }, 1724b467f3efSVlastimil Babka {"hung_task_panic", "kernel.hung_task_panic" }, 1725f117955aSGuilherme G. Piccoli {"numa_zonelist_order", "vm.numa_zonelist_order" }, 1726f117955aSGuilherme G. Piccoli {"softlockup_all_cpu_backtrace", "kernel.softlockup_all_cpu_backtrace" }, 1727f117955aSGuilherme G. Piccoli {"softlockup_panic", "kernel.softlockup_panic" }, 17280a477e1aSVlastimil Babka { } 17290a477e1aSVlastimil Babka }; 17300a477e1aSVlastimil Babka 17310a477e1aSVlastimil Babka static const char *sysctl_find_alias(char *param) 17320a477e1aSVlastimil Babka { 17330a477e1aSVlastimil Babka const struct sysctl_alias *alias; 17340a477e1aSVlastimil Babka 17350a477e1aSVlastimil Babka for (alias = &sysctl_aliases[0]; alias->kernel_param != NULL; alias++) { 17360a477e1aSVlastimil Babka if (strcmp(alias->kernel_param, param) == 0) 17370a477e1aSVlastimil Babka return alias->sysctl_param; 17380a477e1aSVlastimil Babka } 17390a477e1aSVlastimil Babka 17400a477e1aSVlastimil Babka return NULL; 17410a477e1aSVlastimil Babka } 17420a477e1aSVlastimil Babka 17433db978d4SVlastimil Babka /* Set sysctl value passed on kernel command line. */ 17443db978d4SVlastimil Babka static int process_sysctl_arg(char *param, char *val, 17453db978d4SVlastimil Babka const char *unused, void *arg) 17463db978d4SVlastimil Babka { 17473db978d4SVlastimil Babka char *path; 17483db978d4SVlastimil Babka struct vfsmount **proc_mnt = arg; 17493db978d4SVlastimil Babka struct file_system_type *proc_fs_type; 17503db978d4SVlastimil Babka struct file *file; 17513db978d4SVlastimil Babka int len; 17523db978d4SVlastimil Babka int err; 17533db978d4SVlastimil Babka loff_t pos = 0; 17543db978d4SVlastimil Babka ssize_t wret; 17553db978d4SVlastimil Babka 17560a477e1aSVlastimil Babka if (strncmp(param, "sysctl", sizeof("sysctl") - 1) == 0) { 17573db978d4SVlastimil Babka param += sizeof("sysctl") - 1; 17583db978d4SVlastimil Babka 17593db978d4SVlastimil Babka if (param[0] != '/' && param[0] != '.') 17603db978d4SVlastimil Babka return 0; 17613db978d4SVlastimil Babka 17623db978d4SVlastimil Babka param++; 17630a477e1aSVlastimil Babka } else { 17640a477e1aSVlastimil Babka param = (char *) sysctl_find_alias(param); 17650a477e1aSVlastimil Babka if (!param) 17660a477e1aSVlastimil Babka return 0; 17670a477e1aSVlastimil Babka } 17683db978d4SVlastimil Babka 17693db978d4SVlastimil Babka /* 17703db978d4SVlastimil Babka * To set sysctl options, we use a temporary mount of proc, look up the 17713db978d4SVlastimil Babka * respective sys/ file and write to it. To avoid mounting it when no 17723db978d4SVlastimil Babka * options were given, we mount it only when the first sysctl option is 17733db978d4SVlastimil Babka * found. Why not a persistent mount? There are problems with a 17743db978d4SVlastimil Babka * persistent mount of proc in that it forces userspace not to use any 17753db978d4SVlastimil Babka * proc mount options. 17763db978d4SVlastimil Babka */ 17773db978d4SVlastimil Babka if (!*proc_mnt) { 17783db978d4SVlastimil Babka proc_fs_type = get_fs_type("proc"); 17793db978d4SVlastimil Babka if (!proc_fs_type) { 17803db978d4SVlastimil Babka pr_err("Failed to find procfs to set sysctl from command line\n"); 17813db978d4SVlastimil Babka return 0; 17823db978d4SVlastimil Babka } 17833db978d4SVlastimil Babka *proc_mnt = kern_mount(proc_fs_type); 17843db978d4SVlastimil Babka put_filesystem(proc_fs_type); 17853db978d4SVlastimil Babka if (IS_ERR(*proc_mnt)) { 17863db978d4SVlastimil Babka pr_err("Failed to mount procfs to set sysctl from command line\n"); 17873db978d4SVlastimil Babka return 0; 17883db978d4SVlastimil Babka } 17893db978d4SVlastimil Babka } 17903db978d4SVlastimil Babka 17913db978d4SVlastimil Babka path = kasprintf(GFP_KERNEL, "sys/%s", param); 17923db978d4SVlastimil Babka if (!path) 17933db978d4SVlastimil Babka panic("%s: Failed to allocate path for %s\n", __func__, param); 17943db978d4SVlastimil Babka strreplace(path, '.', '/'); 17953db978d4SVlastimil Babka 17963db978d4SVlastimil Babka file = file_open_root((*proc_mnt)->mnt_root, *proc_mnt, path, O_WRONLY, 0); 17973db978d4SVlastimil Babka if (IS_ERR(file)) { 17983db978d4SVlastimil Babka err = PTR_ERR(file); 17993db978d4SVlastimil Babka if (err == -ENOENT) 18003db978d4SVlastimil Babka pr_err("Failed to set sysctl parameter '%s=%s': parameter not found\n", 18013db978d4SVlastimil Babka param, val); 18023db978d4SVlastimil Babka else if (err == -EACCES) 18033db978d4SVlastimil Babka pr_err("Failed to set sysctl parameter '%s=%s': permission denied (read-only?)\n", 18043db978d4SVlastimil Babka param, val); 18053db978d4SVlastimil Babka else 18063db978d4SVlastimil Babka pr_err("Error %pe opening proc file to set sysctl parameter '%s=%s'\n", 18073db978d4SVlastimil Babka file, param, val); 18083db978d4SVlastimil Babka goto out; 18093db978d4SVlastimil Babka } 18103db978d4SVlastimil Babka len = strlen(val); 18113db978d4SVlastimil Babka wret = kernel_write(file, val, len, &pos); 18123db978d4SVlastimil Babka if (wret < 0) { 18133db978d4SVlastimil Babka err = wret; 18143db978d4SVlastimil Babka if (err == -EINVAL) 18153db978d4SVlastimil Babka pr_err("Failed to set sysctl parameter '%s=%s': invalid value\n", 18163db978d4SVlastimil Babka param, val); 18173db978d4SVlastimil Babka else 18183db978d4SVlastimil Babka pr_err("Error %pe writing to proc file to set sysctl parameter '%s=%s'\n", 18193db978d4SVlastimil Babka ERR_PTR(err), param, val); 18203db978d4SVlastimil Babka } else if (wret != len) { 18213db978d4SVlastimil Babka pr_err("Wrote only %zd bytes of %d writing to proc file %s to set sysctl parameter '%s=%s\n", 18223db978d4SVlastimil Babka wret, len, path, param, val); 18233db978d4SVlastimil Babka } 18243db978d4SVlastimil Babka 18253db978d4SVlastimil Babka err = filp_close(file, NULL); 18263db978d4SVlastimil Babka if (err) 18273db978d4SVlastimil Babka pr_err("Error %pe closing proc file to set sysctl parameter '%s=%s\n", 18283db978d4SVlastimil Babka ERR_PTR(err), param, val); 18293db978d4SVlastimil Babka out: 18303db978d4SVlastimil Babka kfree(path); 18313db978d4SVlastimil Babka return 0; 18323db978d4SVlastimil Babka } 18333db978d4SVlastimil Babka 18343db978d4SVlastimil Babka void do_sysctl_args(void) 18353db978d4SVlastimil Babka { 18363db978d4SVlastimil Babka char *command_line; 18373db978d4SVlastimil Babka struct vfsmount *proc_mnt = NULL; 18383db978d4SVlastimil Babka 18393db978d4SVlastimil Babka command_line = kstrdup(saved_command_line, GFP_KERNEL); 18403db978d4SVlastimil Babka if (!command_line) 18413db978d4SVlastimil Babka panic("%s: Failed to allocate copy of command line\n", __func__); 18423db978d4SVlastimil Babka 18433db978d4SVlastimil Babka parse_args("Setting sysctl args", command_line, 18443db978d4SVlastimil Babka NULL, 0, -1, -1, &proc_mnt, process_sysctl_arg); 18453db978d4SVlastimil Babka 18463db978d4SVlastimil Babka if (proc_mnt) 18473db978d4SVlastimil Babka kern_unmount(proc_mnt); 18483db978d4SVlastimil Babka 18493db978d4SVlastimil Babka kfree(command_line); 18503db978d4SVlastimil Babka } 1851