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> 1777b14db5SEric W. Biederman #include "internal.h" 1877b14db5SEric W. Biederman 19d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations; 2077b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations; 2103a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations; 229043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations; 239043476fSAl Viro static const struct inode_operations proc_sys_dir_operations; 2477b14db5SEric W. Biederman 25eec4844fSMatteo Croce /* shared constants to be used in various sysctls */ 26eec4844fSMatteo Croce const int sysctl_vals[] = { 0, 1, INT_MAX }; 27eec4844fSMatteo Croce EXPORT_SYMBOL(sysctl_vals); 28eec4844fSMatteo Croce 29f9bd6733SEric W. Biederman /* Support for permanently empty directories */ 30f9bd6733SEric W. Biederman 31f9bd6733SEric W. Biederman struct ctl_table sysctl_mount_point[] = { 32f9bd6733SEric W. Biederman { } 33f9bd6733SEric W. Biederman }; 34f9bd6733SEric W. Biederman 35f9bd6733SEric W. Biederman static bool is_empty_dir(struct ctl_table_header *head) 36f9bd6733SEric W. Biederman { 37f9bd6733SEric W. Biederman return head->ctl_table[0].child == sysctl_mount_point; 38f9bd6733SEric W. Biederman } 39f9bd6733SEric W. Biederman 40f9bd6733SEric W. Biederman static void set_empty_dir(struct ctl_dir *dir) 41f9bd6733SEric W. Biederman { 42f9bd6733SEric W. Biederman dir->header.ctl_table[0].child = sysctl_mount_point; 43f9bd6733SEric W. Biederman } 44f9bd6733SEric W. Biederman 45f9bd6733SEric W. Biederman static void clear_empty_dir(struct ctl_dir *dir) 46f9bd6733SEric W. Biederman 47f9bd6733SEric W. Biederman { 48f9bd6733SEric W. Biederman dir->header.ctl_table[0].child = NULL; 49f9bd6733SEric W. Biederman } 50f9bd6733SEric W. Biederman 51f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll) 52f1ecf068SLucas De Marchi { 53f1ecf068SLucas De Marchi if (!poll) 54f1ecf068SLucas De Marchi return; 55f1ecf068SLucas De Marchi 56f1ecf068SLucas De Marchi atomic_inc(&poll->event); 57f1ecf068SLucas De Marchi wake_up_interruptible(&poll->wait); 58f1ecf068SLucas De Marchi } 59f1ecf068SLucas De Marchi 60a194558eSEric W. Biederman static struct ctl_table root_table[] = { 61a194558eSEric W. Biederman { 62a194558eSEric W. Biederman .procname = "", 637ec66d06SEric W. Biederman .mode = S_IFDIR|S_IRUGO|S_IXUGO, 64a194558eSEric W. Biederman }, 65a194558eSEric W. Biederman { } 66a194558eSEric W. Biederman }; 670e47c99dSEric W. Biederman static struct ctl_table_root sysctl_table_root = { 680e47c99dSEric W. Biederman .default_set.dir.header = { 691f87f0b5SEric W. Biederman {{.count = 1, 70938aaa4fSEric W. Biederman .nreg = 1, 71ac13ac6fSEric W. Biederman .ctl_table = root_table }}, 720e47c99dSEric W. Biederman .ctl_table_arg = root_table, 731f87f0b5SEric W. Biederman .root = &sysctl_table_root, 741f87f0b5SEric W. Biederman .set = &sysctl_table_root.default_set, 757ec66d06SEric W. Biederman }, 761f87f0b5SEric W. Biederman }; 771f87f0b5SEric W. Biederman 781f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock); 791f87f0b5SEric W. Biederman 807ec66d06SEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header); 810e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead, 8213bcc6a2SEric W. Biederman struct ctl_table **pentry); 830e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head); 840e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header); 857ec66d06SEric W. Biederman 866980128fSEric W. Biederman static void sysctl_print_dir(struct ctl_dir *dir) 876980128fSEric W. Biederman { 886980128fSEric W. Biederman if (dir->header.parent) 896980128fSEric W. Biederman sysctl_print_dir(dir->header.parent); 9087ebdc00SAndrew Morton pr_cont("%s/", dir->header.ctl_table[0].procname); 916980128fSEric W. Biederman } 926980128fSEric W. Biederman 93076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2) 94076c3eedSEric W. Biederman { 95076c3eedSEric W. Biederman int minlen; 96076c3eedSEric W. Biederman int cmp; 97076c3eedSEric W. Biederman 98076c3eedSEric W. Biederman minlen = len1; 99076c3eedSEric W. Biederman if (minlen > len2) 100076c3eedSEric W. Biederman minlen = len2; 101076c3eedSEric W. Biederman 102076c3eedSEric W. Biederman cmp = memcmp(name1, name2, minlen); 103076c3eedSEric W. Biederman if (cmp == 0) 104076c3eedSEric W. Biederman cmp = len1 - len2; 105076c3eedSEric W. Biederman return cmp; 106076c3eedSEric W. Biederman } 107076c3eedSEric W. Biederman 10860f126d9SEric W. Biederman /* Called under sysctl_lock */ 109076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead, 1100e47c99dSEric W. Biederman struct ctl_dir *dir, const char *name, int namelen) 111076c3eedSEric W. Biederman { 112076c3eedSEric W. Biederman struct ctl_table_header *head; 113076c3eedSEric W. Biederman struct ctl_table *entry; 114ac13ac6fSEric W. Biederman struct rb_node *node = dir->root.rb_node; 115076c3eedSEric W. Biederman 116ac13ac6fSEric W. Biederman while (node) 117ac13ac6fSEric W. Biederman { 118ac13ac6fSEric W. Biederman struct ctl_node *ctl_node; 119ac13ac6fSEric W. Biederman const char *procname; 120ac13ac6fSEric W. Biederman int cmp; 121ac13ac6fSEric W. Biederman 122ac13ac6fSEric W. Biederman ctl_node = rb_entry(node, struct ctl_node, node); 123ac13ac6fSEric W. Biederman head = ctl_node->header; 124ac13ac6fSEric W. Biederman entry = &head->ctl_table[ctl_node - head->node]; 125ac13ac6fSEric W. Biederman procname = entry->procname; 126ac13ac6fSEric W. Biederman 127ac13ac6fSEric W. Biederman cmp = namecmp(name, namelen, procname, strlen(procname)); 128ac13ac6fSEric W. Biederman if (cmp < 0) 129ac13ac6fSEric W. Biederman node = node->rb_left; 130ac13ac6fSEric W. Biederman else if (cmp > 0) 131ac13ac6fSEric W. Biederman node = node->rb_right; 132ac13ac6fSEric W. Biederman else { 133076c3eedSEric W. Biederman *phead = head; 134076c3eedSEric W. Biederman return entry; 135076c3eedSEric W. Biederman } 136076c3eedSEric W. Biederman } 137076c3eedSEric W. Biederman return NULL; 138076c3eedSEric W. Biederman } 139076c3eedSEric W. Biederman 140ac13ac6fSEric W. Biederman static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry) 141ac13ac6fSEric W. Biederman { 142ac13ac6fSEric W. Biederman struct rb_node *node = &head->node[entry - head->ctl_table].node; 143ac13ac6fSEric W. Biederman struct rb_node **p = &head->parent->root.rb_node; 144ac13ac6fSEric W. Biederman struct rb_node *parent = NULL; 145ac13ac6fSEric W. Biederman const char *name = entry->procname; 146ac13ac6fSEric W. Biederman int namelen = strlen(name); 147ac13ac6fSEric W. Biederman 148ac13ac6fSEric W. Biederman while (*p) { 149ac13ac6fSEric W. Biederman struct ctl_table_header *parent_head; 150ac13ac6fSEric W. Biederman struct ctl_table *parent_entry; 151ac13ac6fSEric W. Biederman struct ctl_node *parent_node; 152ac13ac6fSEric W. Biederman const char *parent_name; 153ac13ac6fSEric W. Biederman int cmp; 154ac13ac6fSEric W. Biederman 155ac13ac6fSEric W. Biederman parent = *p; 156ac13ac6fSEric W. Biederman parent_node = rb_entry(parent, struct ctl_node, node); 157ac13ac6fSEric W. Biederman parent_head = parent_node->header; 158ac13ac6fSEric W. Biederman parent_entry = &parent_head->ctl_table[parent_node - parent_head->node]; 159ac13ac6fSEric W. Biederman parent_name = parent_entry->procname; 160ac13ac6fSEric W. Biederman 161ac13ac6fSEric W. Biederman cmp = namecmp(name, namelen, parent_name, strlen(parent_name)); 162ac13ac6fSEric W. Biederman if (cmp < 0) 163ac13ac6fSEric W. Biederman p = &(*p)->rb_left; 164ac13ac6fSEric W. Biederman else if (cmp > 0) 165ac13ac6fSEric W. Biederman p = &(*p)->rb_right; 166ac13ac6fSEric W. Biederman else { 16787ebdc00SAndrew Morton pr_err("sysctl duplicate entry: "); 168ac13ac6fSEric W. Biederman sysctl_print_dir(head->parent); 16987ebdc00SAndrew Morton pr_cont("/%s\n", entry->procname); 170ac13ac6fSEric W. Biederman return -EEXIST; 171ac13ac6fSEric W. Biederman } 172ac13ac6fSEric W. Biederman } 173ac13ac6fSEric W. Biederman 174ac13ac6fSEric W. Biederman rb_link_node(node, parent, p); 175ea5272f5SMichel Lespinasse rb_insert_color(node, &head->parent->root); 176ac13ac6fSEric W. Biederman return 0; 177ac13ac6fSEric W. Biederman } 178ac13ac6fSEric W. Biederman 179ac13ac6fSEric W. Biederman static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry) 180ac13ac6fSEric W. Biederman { 181ac13ac6fSEric W. Biederman struct rb_node *node = &head->node[entry - head->ctl_table].node; 182ac13ac6fSEric W. Biederman 183ac13ac6fSEric W. Biederman rb_erase(node, &head->parent->root); 184ac13ac6fSEric W. Biederman } 185ac13ac6fSEric W. Biederman 186e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head, 187e0d04529SEric W. Biederman struct ctl_table_root *root, struct ctl_table_set *set, 188ac13ac6fSEric W. Biederman struct ctl_node *node, struct ctl_table *table) 189e0d04529SEric W. Biederman { 1907ec66d06SEric W. Biederman head->ctl_table = table; 191e0d04529SEric W. Biederman head->ctl_table_arg = table; 192e0d04529SEric W. Biederman head->used = 0; 193e0d04529SEric W. Biederman head->count = 1; 194e0d04529SEric W. Biederman head->nreg = 1; 195e0d04529SEric W. Biederman head->unregistering = NULL; 196e0d04529SEric W. Biederman head->root = root; 197e0d04529SEric W. Biederman head->set = set; 198e0d04529SEric W. Biederman head->parent = NULL; 199ac13ac6fSEric W. Biederman head->node = node; 2002fd1d2c4SEric W. Biederman INIT_HLIST_HEAD(&head->inodes); 201ac13ac6fSEric W. Biederman if (node) { 202ac13ac6fSEric W. Biederman struct ctl_table *entry; 2034c199a93SMichel Lespinasse for (entry = table; entry->procname; entry++, node++) 204ac13ac6fSEric W. Biederman node->header = head; 205ac13ac6fSEric W. Biederman } 206ac13ac6fSEric W. Biederman } 207e0d04529SEric W. Biederman 2088425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head) 2098425d6aaSEric W. Biederman { 210ac13ac6fSEric W. Biederman struct ctl_table *entry; 211ac13ac6fSEric W. Biederman for (entry = head->ctl_table; entry->procname; entry++) 212ac13ac6fSEric W. Biederman erase_entry(head, entry); 2138425d6aaSEric W. Biederman } 2148425d6aaSEric W. Biederman 2150e47c99dSEric W. Biederman static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header) 2168425d6aaSEric W. Biederman { 217ac13ac6fSEric W. Biederman struct ctl_table *entry; 2180e47c99dSEric W. Biederman int err; 2190e47c99dSEric W. Biederman 220f9bd6733SEric W. Biederman /* Is this a permanently empty directory? */ 221f9bd6733SEric W. Biederman if (is_empty_dir(&dir->header)) 222f9bd6733SEric W. Biederman return -EROFS; 223f9bd6733SEric W. Biederman 224f9bd6733SEric W. Biederman /* Am I creating a permanently empty directory? */ 225f9bd6733SEric W. Biederman if (header->ctl_table == sysctl_mount_point) { 226f9bd6733SEric W. Biederman if (!RB_EMPTY_ROOT(&dir->root)) 227f9bd6733SEric W. Biederman return -EINVAL; 228f9bd6733SEric W. Biederman set_empty_dir(dir); 229f9bd6733SEric W. Biederman } 230f9bd6733SEric W. Biederman 2310e47c99dSEric W. Biederman dir->header.nreg++; 2327ec66d06SEric W. Biederman header->parent = dir; 2330e47c99dSEric W. Biederman err = insert_links(header); 2340e47c99dSEric W. Biederman if (err) 2350e47c99dSEric W. Biederman goto fail_links; 236ac13ac6fSEric W. Biederman for (entry = header->ctl_table; entry->procname; entry++) { 237ac13ac6fSEric W. Biederman err = insert_entry(header, entry); 238ac13ac6fSEric W. Biederman if (err) 239ac13ac6fSEric W. Biederman goto fail; 240ac13ac6fSEric W. Biederman } 2410e47c99dSEric W. Biederman return 0; 242ac13ac6fSEric W. Biederman fail: 243ac13ac6fSEric W. Biederman erase_header(header); 244ac13ac6fSEric W. Biederman put_links(header); 2450e47c99dSEric W. Biederman fail_links: 246f9bd6733SEric W. Biederman if (header->ctl_table == sysctl_mount_point) 247f9bd6733SEric W. Biederman clear_empty_dir(dir); 2480e47c99dSEric W. Biederman header->parent = NULL; 2490e47c99dSEric W. Biederman drop_sysctl_table(&dir->header); 2500e47c99dSEric W. Biederman return err; 2518425d6aaSEric W. Biederman } 2528425d6aaSEric W. Biederman 2531f87f0b5SEric W. Biederman /* called under sysctl_lock */ 2541f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p) 2551f87f0b5SEric W. Biederman { 2561f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 2571f87f0b5SEric W. Biederman return 0; 2581f87f0b5SEric W. Biederman p->used++; 2591f87f0b5SEric W. Biederman return 1; 2601f87f0b5SEric W. Biederman } 2611f87f0b5SEric W. Biederman 2621f87f0b5SEric W. Biederman /* called under sysctl_lock */ 2631f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p) 2641f87f0b5SEric W. Biederman { 2651f87f0b5SEric W. Biederman if (!--p->used) 2661f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 2671f87f0b5SEric W. Biederman complete(p->unregistering); 2681f87f0b5SEric W. Biederman } 2691f87f0b5SEric W. Biederman 270d6cffbbeSKonstantin Khlebnikov static void proc_sys_prune_dcache(struct ctl_table_header *head) 271d6cffbbeSKonstantin Khlebnikov { 2722fd1d2c4SEric W. Biederman struct inode *inode; 273d6cffbbeSKonstantin Khlebnikov struct proc_inode *ei; 2742fd1d2c4SEric W. Biederman struct hlist_node *node; 2752fd1d2c4SEric W. Biederman struct super_block *sb; 276d6cffbbeSKonstantin Khlebnikov 277ace0c791SEric W. Biederman rcu_read_lock(); 2782fd1d2c4SEric W. Biederman for (;;) { 2792fd1d2c4SEric W. Biederman node = hlist_first_rcu(&head->inodes); 2802fd1d2c4SEric W. Biederman if (!node) 2812fd1d2c4SEric W. Biederman break; 2822fd1d2c4SEric W. Biederman ei = hlist_entry(node, struct proc_inode, sysctl_inodes); 2832fd1d2c4SEric W. Biederman spin_lock(&sysctl_lock); 2842fd1d2c4SEric W. Biederman hlist_del_init_rcu(&ei->sysctl_inodes); 2852fd1d2c4SEric W. Biederman spin_unlock(&sysctl_lock); 2862fd1d2c4SEric W. Biederman 2872fd1d2c4SEric W. Biederman inode = &ei->vfs_inode; 2882fd1d2c4SEric W. Biederman sb = inode->i_sb; 2892fd1d2c4SEric W. Biederman if (!atomic_inc_not_zero(&sb->s_active)) 2902fd1d2c4SEric W. Biederman continue; 2912fd1d2c4SEric W. Biederman inode = igrab(inode); 292ace0c791SEric W. Biederman rcu_read_unlock(); 2932fd1d2c4SEric W. Biederman if (unlikely(!inode)) { 2942fd1d2c4SEric W. Biederman deactivate_super(sb); 2952fd1d2c4SEric W. Biederman rcu_read_lock(); 2962fd1d2c4SEric W. Biederman continue; 2972fd1d2c4SEric W. Biederman } 2982fd1d2c4SEric W. Biederman 299d6cffbbeSKonstantin Khlebnikov d_prune_aliases(inode); 3002fd1d2c4SEric W. Biederman iput(inode); 3012fd1d2c4SEric W. Biederman deactivate_super(sb); 3022fd1d2c4SEric W. Biederman 303ace0c791SEric W. Biederman rcu_read_lock(); 304d6cffbbeSKonstantin Khlebnikov } 305ace0c791SEric W. Biederman rcu_read_unlock(); 306d6cffbbeSKonstantin Khlebnikov } 307d6cffbbeSKonstantin Khlebnikov 3081f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */ 3091f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p) 3101f87f0b5SEric W. Biederman { 3111f87f0b5SEric W. Biederman /* 3121f87f0b5SEric W. Biederman * if p->used is 0, nobody will ever touch that entry again; 3131f87f0b5SEric W. Biederman * we'll eliminate all paths to it before dropping sysctl_lock 3141f87f0b5SEric W. Biederman */ 3151f87f0b5SEric W. Biederman if (unlikely(p->used)) { 3161f87f0b5SEric W. Biederman struct completion wait; 3171f87f0b5SEric W. Biederman init_completion(&wait); 3181f87f0b5SEric W. Biederman p->unregistering = &wait; 3191f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 3201f87f0b5SEric W. Biederman wait_for_completion(&wait); 3211f87f0b5SEric W. Biederman } else { 3221f87f0b5SEric W. Biederman /* anything non-NULL; we'll never dereference it */ 3231f87f0b5SEric W. Biederman p->unregistering = ERR_PTR(-EINVAL); 324ace0c791SEric W. Biederman spin_unlock(&sysctl_lock); 3251f87f0b5SEric W. Biederman } 3261f87f0b5SEric W. Biederman /* 327d6cffbbeSKonstantin Khlebnikov * Prune dentries for unregistered sysctls: namespaced sysctls 328d6cffbbeSKonstantin Khlebnikov * can have duplicate names and contaminate dcache very badly. 329d6cffbbeSKonstantin Khlebnikov */ 330d6cffbbeSKonstantin Khlebnikov proc_sys_prune_dcache(p); 331d6cffbbeSKonstantin Khlebnikov /* 3321f87f0b5SEric W. Biederman * do not remove from the list until nobody holds it; walking the 3331f87f0b5SEric W. Biederman * list in do_sysctl() relies on that. 3341f87f0b5SEric W. Biederman */ 335ace0c791SEric W. Biederman spin_lock(&sysctl_lock); 3368425d6aaSEric W. Biederman erase_header(p); 3371f87f0b5SEric W. Biederman } 3381f87f0b5SEric W. Biederman 3391f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head) 3401f87f0b5SEric W. Biederman { 341ab4a1f24SPrasad Joshi BUG_ON(!head); 3421f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 3431f87f0b5SEric W. Biederman if (!use_table(head)) 3441f87f0b5SEric W. Biederman head = ERR_PTR(-ENOENT); 3451f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 3461f87f0b5SEric W. Biederman return head; 3471f87f0b5SEric W. Biederman } 3481f87f0b5SEric W. Biederman 3491f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head) 3501f87f0b5SEric W. Biederman { 3511f87f0b5SEric W. Biederman if (!head) 3521f87f0b5SEric W. Biederman return; 3531f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 3541f87f0b5SEric W. Biederman unuse_table(head); 3551f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 3561f87f0b5SEric W. Biederman } 3571f87f0b5SEric W. Biederman 3581f87f0b5SEric W. Biederman static struct ctl_table_set * 35913bcc6a2SEric W. Biederman lookup_header_set(struct ctl_table_root *root) 3601f87f0b5SEric W. Biederman { 3611f87f0b5SEric W. Biederman struct ctl_table_set *set = &root->default_set; 3621f87f0b5SEric W. Biederman if (root->lookup) 36313bcc6a2SEric W. Biederman set = root->lookup(root); 3641f87f0b5SEric W. Biederman return set; 3651f87f0b5SEric W. Biederman } 3661f87f0b5SEric W. Biederman 367076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead, 3687ec66d06SEric W. Biederman struct ctl_dir *dir, 369076c3eedSEric W. Biederman const char *name, int namelen) 370076c3eedSEric W. Biederman { 371076c3eedSEric W. Biederman struct ctl_table_header *head; 372076c3eedSEric W. Biederman struct ctl_table *entry; 373076c3eedSEric W. Biederman 374076c3eedSEric W. Biederman spin_lock(&sysctl_lock); 3750e47c99dSEric W. Biederman entry = find_entry(&head, dir, name, namelen); 376076c3eedSEric W. Biederman if (entry && use_table(head)) 377076c3eedSEric W. Biederman *phead = head; 378076c3eedSEric W. Biederman else 379076c3eedSEric W. Biederman entry = NULL; 380076c3eedSEric W. Biederman spin_unlock(&sysctl_lock); 381076c3eedSEric W. Biederman return entry; 382076c3eedSEric W. Biederman } 383076c3eedSEric W. Biederman 384ac13ac6fSEric W. Biederman static struct ctl_node *first_usable_entry(struct rb_node *node) 3851f87f0b5SEric W. Biederman { 386ac13ac6fSEric W. Biederman struct ctl_node *ctl_node; 3871f87f0b5SEric W. Biederman 388ac13ac6fSEric W. Biederman for (;node; node = rb_next(node)) { 389ac13ac6fSEric W. Biederman ctl_node = rb_entry(node, struct ctl_node, node); 390ac13ac6fSEric W. Biederman if (use_table(ctl_node->header)) 391ac13ac6fSEric W. Biederman return ctl_node; 3921f87f0b5SEric W. Biederman } 3931f87f0b5SEric W. Biederman return NULL; 3941f87f0b5SEric W. Biederman } 3951f87f0b5SEric W. Biederman 3967ec66d06SEric W. Biederman static void first_entry(struct ctl_dir *dir, 3976a75ce16SEric W. Biederman struct ctl_table_header **phead, struct ctl_table **pentry) 3981f87f0b5SEric W. Biederman { 399ac13ac6fSEric W. Biederman struct ctl_table_header *head = NULL; 4007ec66d06SEric W. Biederman struct ctl_table *entry = NULL; 401ac13ac6fSEric W. Biederman struct ctl_node *ctl_node; 4026a75ce16SEric W. Biederman 4036a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 404ac13ac6fSEric W. Biederman ctl_node = first_usable_entry(rb_first(&dir->root)); 4056a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 406ac13ac6fSEric W. Biederman if (ctl_node) { 407ac13ac6fSEric W. Biederman head = ctl_node->header; 408ac13ac6fSEric W. Biederman entry = &head->ctl_table[ctl_node - head->node]; 409ac13ac6fSEric W. Biederman } 4106a75ce16SEric W. Biederman *phead = head; 4116a75ce16SEric W. Biederman *pentry = entry; 4126a75ce16SEric W. Biederman } 4136a75ce16SEric W. Biederman 4147ec66d06SEric W. Biederman static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry) 4156a75ce16SEric W. Biederman { 4166a75ce16SEric W. Biederman struct ctl_table_header *head = *phead; 4176a75ce16SEric W. Biederman struct ctl_table *entry = *pentry; 418ac13ac6fSEric W. Biederman struct ctl_node *ctl_node = &head->node[entry - head->ctl_table]; 4196a75ce16SEric W. Biederman 4206a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 4216a75ce16SEric W. Biederman unuse_table(head); 422ac13ac6fSEric W. Biederman 423ac13ac6fSEric W. Biederman ctl_node = first_usable_entry(rb_next(&ctl_node->node)); 4246a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 425ac13ac6fSEric W. Biederman head = NULL; 426ac13ac6fSEric W. Biederman if (ctl_node) { 427ac13ac6fSEric W. Biederman head = ctl_node->header; 428ac13ac6fSEric W. Biederman entry = &head->ctl_table[ctl_node - head->node]; 4296a75ce16SEric W. Biederman } 4306a75ce16SEric W. Biederman *phead = head; 4316a75ce16SEric W. Biederman *pentry = entry; 4321f87f0b5SEric W. Biederman } 4331f87f0b5SEric W. Biederman 4341f87f0b5SEric W. Biederman /* 4351f87f0b5SEric W. Biederman * sysctl_perm does NOT grant the superuser all rights automatically, because 4361f87f0b5SEric W. Biederman * some sysctl variables are readonly even to root. 4371f87f0b5SEric W. Biederman */ 4381f87f0b5SEric W. Biederman 4391f87f0b5SEric W. Biederman static int test_perm(int mode, int op) 4401f87f0b5SEric W. Biederman { 441091bd3eaSEric W. Biederman if (uid_eq(current_euid(), GLOBAL_ROOT_UID)) 4421f87f0b5SEric W. Biederman mode >>= 6; 443091bd3eaSEric W. Biederman else if (in_egroup_p(GLOBAL_ROOT_GID)) 4441f87f0b5SEric W. Biederman mode >>= 3; 4451f87f0b5SEric W. Biederman if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0) 4461f87f0b5SEric W. Biederman return 0; 4471f87f0b5SEric W. Biederman return -EACCES; 4481f87f0b5SEric W. Biederman } 4491f87f0b5SEric W. Biederman 45073f7ef43SEric W. Biederman static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op) 4511f87f0b5SEric W. Biederman { 45273f7ef43SEric W. Biederman struct ctl_table_root *root = head->root; 4531f87f0b5SEric W. Biederman int mode; 4541f87f0b5SEric W. Biederman 4551f87f0b5SEric W. Biederman if (root->permissions) 45673f7ef43SEric W. Biederman mode = root->permissions(head, table); 4571f87f0b5SEric W. Biederman else 4581f87f0b5SEric W. Biederman mode = table->mode; 4591f87f0b5SEric W. Biederman 4601f87f0b5SEric W. Biederman return test_perm(mode, op); 4611f87f0b5SEric W. Biederman } 4621f87f0b5SEric W. Biederman 4639043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb, 4649043476fSAl Viro struct ctl_table_header *head, struct ctl_table *table) 46577b14db5SEric W. Biederman { 466e79c6a4fSDmitry Torokhov struct ctl_table_root *root = head->root; 46777b14db5SEric W. Biederman struct inode *inode; 4689043476fSAl Viro struct proc_inode *ei; 46977b14db5SEric W. Biederman 4709043476fSAl Viro inode = new_inode(sb); 47177b14db5SEric W. Biederman if (!inode) 472ea5751ccSIvan Delalande return ERR_PTR(-ENOMEM); 47377b14db5SEric W. Biederman 47485fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 47585fe4025SChristoph Hellwig 47677b14db5SEric W. Biederman ei = PROC_I(inode); 4779043476fSAl Viro 478d6cffbbeSKonstantin Khlebnikov spin_lock(&sysctl_lock); 479ace0c791SEric W. Biederman if (unlikely(head->unregistering)) { 480ace0c791SEric W. Biederman spin_unlock(&sysctl_lock); 481ace0c791SEric W. Biederman iput(inode); 482ea5751ccSIvan Delalande return ERR_PTR(-ENOENT); 483ace0c791SEric W. Biederman } 484ace0c791SEric W. Biederman ei->sysctl = head; 485ace0c791SEric W. Biederman ei->sysctl_entry = table; 4862fd1d2c4SEric W. Biederman hlist_add_head_rcu(&ei->sysctl_inodes, &head->inodes); 487d6cffbbeSKonstantin Khlebnikov head->count++; 488d6cffbbeSKonstantin Khlebnikov spin_unlock(&sysctl_lock); 489d6cffbbeSKonstantin Khlebnikov 490078cd827SDeepa Dinamani inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); 4919043476fSAl Viro inode->i_mode = table->mode; 4927ec66d06SEric W. Biederman if (!S_ISDIR(table->mode)) { 4939043476fSAl Viro inode->i_mode |= S_IFREG; 49477b14db5SEric W. Biederman inode->i_op = &proc_sys_inode_operations; 49577b14db5SEric W. Biederman inode->i_fop = &proc_sys_file_operations; 4969043476fSAl Viro } else { 4979043476fSAl Viro inode->i_mode |= S_IFDIR; 4989043476fSAl Viro inode->i_op = &proc_sys_dir_operations; 4999043476fSAl Viro inode->i_fop = &proc_sys_dir_file_operations; 500f9bd6733SEric W. Biederman if (is_empty_dir(head)) 501f9bd6733SEric W. Biederman make_empty_dir_inode(inode); 5029043476fSAl Viro } 503e79c6a4fSDmitry Torokhov 504e79c6a4fSDmitry Torokhov if (root->set_ownership) 505e79c6a4fSDmitry Torokhov root->set_ownership(head, table, &inode->i_uid, &inode->i_gid); 5065ec27ec7SRadoslaw Burny else { 5075ec27ec7SRadoslaw Burny inode->i_uid = GLOBAL_ROOT_UID; 5085ec27ec7SRadoslaw Burny inode->i_gid = GLOBAL_ROOT_GID; 5095ec27ec7SRadoslaw Burny } 510e79c6a4fSDmitry Torokhov 51177b14db5SEric W. Biederman return inode; 51277b14db5SEric W. Biederman } 51377b14db5SEric W. Biederman 514d6cffbbeSKonstantin Khlebnikov void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head) 515d6cffbbeSKonstantin Khlebnikov { 516d6cffbbeSKonstantin Khlebnikov spin_lock(&sysctl_lock); 5172fd1d2c4SEric W. Biederman hlist_del_init_rcu(&PROC_I(inode)->sysctl_inodes); 518d6cffbbeSKonstantin Khlebnikov if (!--head->count) 519d6cffbbeSKonstantin Khlebnikov kfree_rcu(head, rcu); 520d6cffbbeSKonstantin Khlebnikov spin_unlock(&sysctl_lock); 521d6cffbbeSKonstantin Khlebnikov } 522d6cffbbeSKonstantin Khlebnikov 52381324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode) 52477b14db5SEric W. Biederman { 5253cc3e046SEric W. Biederman struct ctl_table_header *head = PROC_I(inode)->sysctl; 5263cc3e046SEric W. Biederman if (!head) 5270e47c99dSEric W. Biederman head = &sysctl_table_root.default_set.dir.header; 5283cc3e046SEric W. Biederman return sysctl_head_grab(head); 52977b14db5SEric W. Biederman } 53077b14db5SEric W. Biederman 53177b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, 53200cd8dd3SAl Viro unsigned int flags) 53377b14db5SEric W. Biederman { 5349043476fSAl Viro struct ctl_table_header *head = grab_header(dir); 5359043476fSAl Viro struct ctl_table_header *h = NULL; 536dc12e909SAl Viro const struct qstr *name = &dentry->d_name; 5379043476fSAl Viro struct ctl_table *p; 53877b14db5SEric W. Biederman struct inode *inode; 5399043476fSAl Viro struct dentry *err = ERR_PTR(-ENOENT); 5407ec66d06SEric W. Biederman struct ctl_dir *ctl_dir; 5410e47c99dSEric W. Biederman int ret; 54277b14db5SEric W. Biederman 5439043476fSAl Viro if (IS_ERR(head)) 5449043476fSAl Viro return ERR_CAST(head); 5459043476fSAl Viro 5467ec66d06SEric W. Biederman ctl_dir = container_of(head, struct ctl_dir, header); 5479043476fSAl Viro 5487ec66d06SEric W. Biederman p = lookup_entry(&h, ctl_dir, name->name, name->len); 5499043476fSAl Viro if (!p) 55077b14db5SEric W. Biederman goto out; 55177b14db5SEric W. Biederman 5524e757320SEric W. Biederman if (S_ISLNK(p->mode)) { 55313bcc6a2SEric W. Biederman ret = sysctl_follow_link(&h, &p); 5540e47c99dSEric W. Biederman err = ERR_PTR(ret); 5550e47c99dSEric W. Biederman if (ret) 5560e47c99dSEric W. Biederman goto out; 5574e757320SEric W. Biederman } 5580e47c99dSEric W. Biederman 5599043476fSAl Viro inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p); 560ea5751ccSIvan Delalande if (IS_ERR(inode)) { 561ea5751ccSIvan Delalande err = ERR_CAST(inode); 56277b14db5SEric W. Biederman goto out; 563ea5751ccSIvan Delalande } 56477b14db5SEric W. Biederman 565fb045adbSNick Piggin d_set_d_op(dentry, &proc_sys_dentry_operations); 566888e2b03SAl Viro err = d_splice_alias(inode, dentry); 56777b14db5SEric W. Biederman 56877b14db5SEric W. Biederman out: 5696bf61045SFrancesco Ruggeri if (h) 5706bf61045SFrancesco Ruggeri sysctl_head_finish(h); 57177b14db5SEric W. Biederman sysctl_head_finish(head); 57277b14db5SEric W. Biederman return err; 57377b14db5SEric W. Biederman } 57477b14db5SEric W. Biederman 5757708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf, 5767708bfb1SPavel Emelyanov size_t count, loff_t *ppos, int write) 57777b14db5SEric W. Biederman { 578496ad9aaSAl Viro struct inode *inode = file_inode(filp); 5799043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 5809043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 5814e63acdfSAndrey Ignatov void *new_buf = NULL; 5822a2da53bSDavid Howells ssize_t error; 58377b14db5SEric W. Biederman 5849043476fSAl Viro if (IS_ERR(head)) 5859043476fSAl Viro return PTR_ERR(head); 58677b14db5SEric W. Biederman 58777b14db5SEric W. Biederman /* 58877b14db5SEric W. Biederman * At this point we know that the sysctl was not unregistered 58977b14db5SEric W. Biederman * and won't be until we finish. 59077b14db5SEric W. Biederman */ 59177b14db5SEric W. Biederman error = -EPERM; 59273f7ef43SEric W. Biederman if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ)) 59377b14db5SEric W. Biederman goto out; 59477b14db5SEric W. Biederman 5959043476fSAl Viro /* if that can happen at all, it should be -EINVAL, not -EISDIR */ 5969043476fSAl Viro error = -EINVAL; 5979043476fSAl Viro if (!table->proc_handler) 5989043476fSAl Viro goto out; 5999043476fSAl Viro 6004e63acdfSAndrey Ignatov error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, buf, &count, 601e1550bfeSAndrey Ignatov ppos, &new_buf); 6027b146cebSAndrey Ignatov if (error) 6037b146cebSAndrey Ignatov goto out; 6047b146cebSAndrey Ignatov 60577b14db5SEric W. Biederman /* careful: calling conventions are nasty here */ 6064e63acdfSAndrey Ignatov if (new_buf) { 6074e63acdfSAndrey Ignatov mm_segment_t old_fs; 6084e63acdfSAndrey Ignatov 6094e63acdfSAndrey Ignatov old_fs = get_fs(); 6104e63acdfSAndrey Ignatov set_fs(KERNEL_DS); 6114e63acdfSAndrey Ignatov error = table->proc_handler(table, write, (void __user *)new_buf, 6124e63acdfSAndrey Ignatov &count, ppos); 6134e63acdfSAndrey Ignatov set_fs(old_fs); 6144e63acdfSAndrey Ignatov kfree(new_buf); 6154e63acdfSAndrey Ignatov } else { 6164e63acdfSAndrey Ignatov error = table->proc_handler(table, write, buf, &count, ppos); 6174e63acdfSAndrey Ignatov } 6184e63acdfSAndrey Ignatov 61977b14db5SEric W. Biederman if (!error) 6204e63acdfSAndrey Ignatov error = count; 62177b14db5SEric W. Biederman out: 62277b14db5SEric W. Biederman sysctl_head_finish(head); 62377b14db5SEric W. Biederman 62477b14db5SEric W. Biederman return error; 62577b14db5SEric W. Biederman } 62677b14db5SEric W. Biederman 6277708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf, 6287708bfb1SPavel Emelyanov size_t count, loff_t *ppos) 6297708bfb1SPavel Emelyanov { 6307708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0); 6317708bfb1SPavel Emelyanov } 6327708bfb1SPavel Emelyanov 63377b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf, 63477b14db5SEric W. Biederman size_t count, loff_t *ppos) 63577b14db5SEric W. Biederman { 6367708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1); 63777b14db5SEric W. Biederman } 63877b14db5SEric W. Biederman 639f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp) 640f1ecf068SLucas De Marchi { 6414e474a00SLucas De Marchi struct ctl_table_header *head = grab_header(inode); 642f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 643f1ecf068SLucas De Marchi 6444e474a00SLucas De Marchi /* sysctl was unregistered */ 6454e474a00SLucas De Marchi if (IS_ERR(head)) 6464e474a00SLucas De Marchi return PTR_ERR(head); 6474e474a00SLucas De Marchi 648f1ecf068SLucas De Marchi if (table->poll) 649f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 650f1ecf068SLucas De Marchi 6514e474a00SLucas De Marchi sysctl_head_finish(head); 6524e474a00SLucas De Marchi 653f1ecf068SLucas De Marchi return 0; 654f1ecf068SLucas De Marchi } 655f1ecf068SLucas De Marchi 656076ccb76SAl Viro static __poll_t proc_sys_poll(struct file *filp, poll_table *wait) 657f1ecf068SLucas De Marchi { 658496ad9aaSAl Viro struct inode *inode = file_inode(filp); 6594e474a00SLucas De Marchi struct ctl_table_header *head = grab_header(inode); 660f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 661076ccb76SAl Viro __poll_t ret = DEFAULT_POLLMASK; 6624e474a00SLucas De Marchi unsigned long event; 6634e474a00SLucas De Marchi 6644e474a00SLucas De Marchi /* sysctl was unregistered */ 6654e474a00SLucas De Marchi if (IS_ERR(head)) 666a9a08845SLinus Torvalds return EPOLLERR | EPOLLHUP; 667f1ecf068SLucas De Marchi 668f1ecf068SLucas De Marchi if (!table->proc_handler) 669f1ecf068SLucas De Marchi goto out; 670f1ecf068SLucas De Marchi 671f1ecf068SLucas De Marchi if (!table->poll) 672f1ecf068SLucas De Marchi goto out; 673f1ecf068SLucas De Marchi 6744e474a00SLucas De Marchi event = (unsigned long)filp->private_data; 675f1ecf068SLucas De Marchi poll_wait(filp, &table->poll->wait, wait); 676f1ecf068SLucas De Marchi 677f1ecf068SLucas De Marchi if (event != atomic_read(&table->poll->event)) { 678f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 679a9a08845SLinus Torvalds ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI; 680f1ecf068SLucas De Marchi } 681f1ecf068SLucas De Marchi 682f1ecf068SLucas De Marchi out: 6834e474a00SLucas De Marchi sysctl_head_finish(head); 6844e474a00SLucas De Marchi 685f1ecf068SLucas De Marchi return ret; 686f1ecf068SLucas De Marchi } 68777b14db5SEric W. Biederman 688f0c3b509SAl Viro static bool proc_sys_fill_cache(struct file *file, 689f0c3b509SAl Viro struct dir_context *ctx, 6909043476fSAl Viro struct ctl_table_header *head, 6919043476fSAl Viro struct ctl_table *table) 69277b14db5SEric W. Biederman { 693f0c3b509SAl Viro struct dentry *child, *dir = file->f_path.dentry; 69477b14db5SEric W. Biederman struct inode *inode; 69577b14db5SEric W. Biederman struct qstr qname; 69677b14db5SEric W. Biederman ino_t ino = 0; 69777b14db5SEric W. Biederman unsigned type = DT_UNKNOWN; 69877b14db5SEric W. Biederman 69977b14db5SEric W. Biederman qname.name = table->procname; 70077b14db5SEric W. Biederman qname.len = strlen(table->procname); 7018387ff25SLinus Torvalds qname.hash = full_name_hash(dir, qname.name, qname.len); 70277b14db5SEric W. Biederman 70377b14db5SEric W. Biederman child = d_lookup(dir, &qname); 70477b14db5SEric W. Biederman if (!child) { 70576aab3abSAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 70676aab3abSAl Viro child = d_alloc_parallel(dir, &qname, &wq); 70776aab3abSAl Viro if (IS_ERR(child)) 70876aab3abSAl Viro return false; 70976aab3abSAl Viro if (d_in_lookup(child)) { 710888e2b03SAl Viro struct dentry *res; 7119043476fSAl Viro inode = proc_sys_make_inode(dir->d_sb, head, table); 712ea5751ccSIvan Delalande if (IS_ERR(inode)) { 71376aab3abSAl Viro d_lookup_done(child); 7149043476fSAl Viro dput(child); 715f0c3b509SAl Viro return false; 71676aab3abSAl Viro } 717fb045adbSNick Piggin d_set_d_op(child, &proc_sys_dentry_operations); 718888e2b03SAl Viro res = d_splice_alias(inode, child); 719888e2b03SAl Viro d_lookup_done(child); 720888e2b03SAl Viro if (unlikely(res)) { 721888e2b03SAl Viro if (IS_ERR(res)) { 722888e2b03SAl Viro dput(child); 723888e2b03SAl Viro return false; 724888e2b03SAl Viro } 725888e2b03SAl Viro dput(child); 726888e2b03SAl Viro child = res; 727888e2b03SAl Viro } 72877b14db5SEric W. Biederman } 72977b14db5SEric W. Biederman } 7302b0143b5SDavid Howells inode = d_inode(child); 73177b14db5SEric W. Biederman ino = inode->i_ino; 73277b14db5SEric W. Biederman type = inode->i_mode >> 12; 73377b14db5SEric W. Biederman dput(child); 734f0c3b509SAl Viro return dir_emit(ctx, qname.name, qname.len, ino, type); 7359043476fSAl Viro } 7369043476fSAl Viro 737f0c3b509SAl Viro static bool proc_sys_link_fill_cache(struct file *file, 738f0c3b509SAl Viro struct dir_context *ctx, 7390e47c99dSEric W. Biederman struct ctl_table_header *head, 7400e47c99dSEric W. Biederman struct ctl_table *table) 7410e47c99dSEric W. Biederman { 742f0c3b509SAl Viro bool ret = true; 743a0b0d1c3SDanilo Krummrich 7440e47c99dSEric W. Biederman head = sysctl_head_grab(head); 745a0b0d1c3SDanilo Krummrich if (IS_ERR(head)) 746a0b0d1c3SDanilo Krummrich return false; 7470e47c99dSEric W. Biederman 7480e47c99dSEric W. Biederman /* It is not an error if we can not follow the link ignore it */ 749835b94e0SDanilo Krummrich if (sysctl_follow_link(&head, &table)) 7500e47c99dSEric W. Biederman goto out; 7510e47c99dSEric W. Biederman 752f0c3b509SAl Viro ret = proc_sys_fill_cache(file, ctx, head, table); 7530e47c99dSEric W. Biederman out: 7540e47c99dSEric W. Biederman sysctl_head_finish(head); 7550e47c99dSEric W. Biederman return ret; 7560e47c99dSEric W. Biederman } 7570e47c99dSEric W. Biederman 758e5eea098SJoe Perches static int scan(struct ctl_table_header *head, struct ctl_table *table, 7599043476fSAl Viro unsigned long *pos, struct file *file, 760f0c3b509SAl Viro struct dir_context *ctx) 7619043476fSAl Viro { 762f0c3b509SAl Viro bool res; 7639043476fSAl Viro 764f0c3b509SAl Viro if ((*pos)++ < ctx->pos) 765f0c3b509SAl Viro return true; 7669043476fSAl Viro 7670e47c99dSEric W. Biederman if (unlikely(S_ISLNK(table->mode))) 768f0c3b509SAl Viro res = proc_sys_link_fill_cache(file, ctx, head, table); 7690e47c99dSEric W. Biederman else 770f0c3b509SAl Viro res = proc_sys_fill_cache(file, ctx, head, table); 7719043476fSAl Viro 772f0c3b509SAl Viro if (res) 773f0c3b509SAl Viro ctx->pos = *pos; 7746a75ce16SEric W. Biederman 7756a75ce16SEric W. Biederman return res; 77677b14db5SEric W. Biederman } 77777b14db5SEric W. Biederman 778f0c3b509SAl Viro static int proc_sys_readdir(struct file *file, struct dir_context *ctx) 77977b14db5SEric W. Biederman { 780f0c3b509SAl Viro struct ctl_table_header *head = grab_header(file_inode(file)); 7819043476fSAl Viro struct ctl_table_header *h = NULL; 7826a75ce16SEric W. Biederman struct ctl_table *entry; 7837ec66d06SEric W. Biederman struct ctl_dir *ctl_dir; 78477b14db5SEric W. Biederman unsigned long pos; 78577b14db5SEric W. Biederman 7869043476fSAl Viro if (IS_ERR(head)) 7879043476fSAl Viro return PTR_ERR(head); 7889043476fSAl Viro 7897ec66d06SEric W. Biederman ctl_dir = container_of(head, struct ctl_dir, header); 79077b14db5SEric W. Biederman 791f0c3b509SAl Viro if (!dir_emit_dots(file, ctx)) 79293362fa4SZhou Chengming goto out; 793f0c3b509SAl Viro 79477b14db5SEric W. Biederman pos = 2; 79577b14db5SEric W. Biederman 7967ec66d06SEric W. Biederman for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) { 797f0c3b509SAl Viro if (!scan(h, entry, &pos, file, ctx)) { 7989043476fSAl Viro sysctl_head_finish(h); 7999043476fSAl Viro break; 80077b14db5SEric W. Biederman } 80177b14db5SEric W. Biederman } 80293362fa4SZhou Chengming out: 80377b14db5SEric W. Biederman sysctl_head_finish(head); 804f0c3b509SAl Viro return 0; 80577b14db5SEric W. Biederman } 80677b14db5SEric W. Biederman 80710556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask) 80877b14db5SEric W. Biederman { 80977b14db5SEric W. Biederman /* 81077b14db5SEric W. Biederman * sysctl entries that are not writeable, 81177b14db5SEric W. Biederman * are _NOT_ writeable, capabilities or not. 81277b14db5SEric W. Biederman */ 813f696a365SMiklos Szeredi struct ctl_table_header *head; 814f696a365SMiklos Szeredi struct ctl_table *table; 81577b14db5SEric W. Biederman int error; 81677b14db5SEric W. Biederman 817f696a365SMiklos Szeredi /* Executable files are not allowed under /proc/sys/ */ 818f696a365SMiklos Szeredi if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) 819f696a365SMiklos Szeredi return -EACCES; 820f696a365SMiklos Szeredi 821f696a365SMiklos Szeredi head = grab_header(inode); 8229043476fSAl Viro if (IS_ERR(head)) 8239043476fSAl Viro return PTR_ERR(head); 82477b14db5SEric W. Biederman 825f696a365SMiklos Szeredi table = PROC_I(inode)->sysctl_entry; 8269043476fSAl Viro if (!table) /* global root - r-xr-xr-x */ 8279043476fSAl Viro error = mask & MAY_WRITE ? -EACCES : 0; 8289043476fSAl Viro else /* Use the permissions on the sysctl table entry */ 82973f7ef43SEric W. Biederman error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK); 8309043476fSAl Viro 83177b14db5SEric W. Biederman sysctl_head_finish(head); 83277b14db5SEric W. Biederman return error; 83377b14db5SEric W. Biederman } 83477b14db5SEric W. Biederman 83577b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr) 83677b14db5SEric W. Biederman { 8372b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 83877b14db5SEric W. Biederman int error; 83977b14db5SEric W. Biederman 84077b14db5SEric W. Biederman if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) 84177b14db5SEric W. Biederman return -EPERM; 84277b14db5SEric W. Biederman 84331051c85SJan Kara error = setattr_prepare(dentry, attr); 8441025774cSChristoph Hellwig if (error) 84577b14db5SEric W. Biederman return error; 8461025774cSChristoph Hellwig 8471025774cSChristoph Hellwig setattr_copy(inode, attr); 8481025774cSChristoph Hellwig mark_inode_dirty(inode); 8491025774cSChristoph Hellwig return 0; 85077b14db5SEric W. Biederman } 85177b14db5SEric W. Biederman 852a528d35eSDavid Howells static int proc_sys_getattr(const struct path *path, struct kstat *stat, 853a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 8549043476fSAl Viro { 855a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry); 8569043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 8579043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 8589043476fSAl Viro 8599043476fSAl Viro if (IS_ERR(head)) 8609043476fSAl Viro return PTR_ERR(head); 8619043476fSAl Viro 8629043476fSAl Viro generic_fillattr(inode, stat); 8639043476fSAl Viro if (table) 8649043476fSAl Viro stat->mode = (stat->mode & S_IFMT) | table->mode; 8659043476fSAl Viro 8669043476fSAl Viro sysctl_head_finish(head); 8679043476fSAl Viro return 0; 8689043476fSAl Viro } 8699043476fSAl Viro 87077b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = { 871f1ecf068SLucas De Marchi .open = proc_sys_open, 872f1ecf068SLucas De Marchi .poll = proc_sys_poll, 87377b14db5SEric W. Biederman .read = proc_sys_read, 87477b14db5SEric W. Biederman .write = proc_sys_write, 8756038f373SArnd Bergmann .llseek = default_llseek, 8769043476fSAl Viro }; 8779043476fSAl Viro 8789043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = { 879887df078SPavel Emelyanov .read = generic_read_dir, 880f50752eaSAl Viro .iterate_shared = proc_sys_readdir, 8813222a3e5SChristoph Hellwig .llseek = generic_file_llseek, 88277b14db5SEric W. Biederman }; 88377b14db5SEric W. Biederman 88403a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = { 8859043476fSAl Viro .permission = proc_sys_permission, 8869043476fSAl Viro .setattr = proc_sys_setattr, 8879043476fSAl Viro .getattr = proc_sys_getattr, 8889043476fSAl Viro }; 8899043476fSAl Viro 8909043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = { 89177b14db5SEric W. Biederman .lookup = proc_sys_lookup, 89277b14db5SEric W. Biederman .permission = proc_sys_permission, 89377b14db5SEric W. Biederman .setattr = proc_sys_setattr, 8949043476fSAl Viro .getattr = proc_sys_getattr, 89577b14db5SEric W. Biederman }; 89677b14db5SEric W. Biederman 8970b728e19SAl Viro static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags) 89877b14db5SEric W. Biederman { 8990b728e19SAl Viro if (flags & LOOKUP_RCU) 90034286d66SNick Piggin return -ECHILD; 9012b0143b5SDavid Howells return !PROC_I(d_inode(dentry))->sysctl->unregistering; 9029043476fSAl Viro } 9039043476fSAl Viro 904fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry) 9059043476fSAl Viro { 9062b0143b5SDavid Howells return !!PROC_I(d_inode(dentry))->sysctl->unregistering; 9079043476fSAl Viro } 9089043476fSAl Viro 9091f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p) 9101f87f0b5SEric W. Biederman { 9111f87f0b5SEric W. Biederman struct ctl_table_set *set = p->set; 9121f87f0b5SEric W. Biederman int res; 9131f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 9141f87f0b5SEric W. Biederman if (p->unregistering) 9151f87f0b5SEric W. Biederman res = 0; 9161f87f0b5SEric W. Biederman else if (!set->is_seen) 9171f87f0b5SEric W. Biederman res = 1; 9181f87f0b5SEric W. Biederman else 9191f87f0b5SEric W. Biederman res = set->is_seen(set); 9201f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 9211f87f0b5SEric W. Biederman return res; 9221f87f0b5SEric W. Biederman } 9231f87f0b5SEric W. Biederman 9246fa67e70SAl Viro static int proc_sys_compare(const struct dentry *dentry, 925621e155aSNick Piggin unsigned int len, const char *str, const struct qstr *name) 9269043476fSAl Viro { 927dfef6dcdSAl Viro struct ctl_table_header *head; 928da53be12SLinus Torvalds struct inode *inode; 929da53be12SLinus Torvalds 93031e6b01fSNick Piggin /* Although proc doesn't have negative dentries, rcu-walk means 93131e6b01fSNick Piggin * that inode here can be NULL */ 932dfef6dcdSAl Viro /* AV: can it, indeed? */ 9332b0143b5SDavid Howells inode = d_inode_rcu(dentry); 93431e6b01fSNick Piggin if (!inode) 935dfef6dcdSAl Viro return 1; 936621e155aSNick Piggin if (name->len != len) 9379043476fSAl Viro return 1; 938621e155aSNick Piggin if (memcmp(name->name, str, len)) 9399043476fSAl Viro return 1; 940dfef6dcdSAl Viro head = rcu_dereference(PROC_I(inode)->sysctl); 941dfef6dcdSAl Viro return !head || !sysctl_is_seen(head); 94277b14db5SEric W. Biederman } 94377b14db5SEric W. Biederman 944d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = { 94577b14db5SEric W. Biederman .d_revalidate = proc_sys_revalidate, 9469043476fSAl Viro .d_delete = proc_sys_delete, 9479043476fSAl Viro .d_compare = proc_sys_compare, 94877b14db5SEric W. Biederman }; 94977b14db5SEric W. Biederman 9500e47c99dSEric W. Biederman static struct ctl_dir *find_subdir(struct ctl_dir *dir, 9517ec66d06SEric W. Biederman const char *name, int namelen) 9521f87f0b5SEric W. Biederman { 9537ec66d06SEric W. Biederman struct ctl_table_header *head; 9547ec66d06SEric W. Biederman struct ctl_table *entry; 9551f87f0b5SEric W. Biederman 9560e47c99dSEric W. Biederman entry = find_entry(&head, dir, name, namelen); 9577ec66d06SEric W. Biederman if (!entry) 9587ec66d06SEric W. Biederman return ERR_PTR(-ENOENT); 95951f72f4aSEric W. Biederman if (!S_ISDIR(entry->mode)) 9607ec66d06SEric W. Biederman return ERR_PTR(-ENOTDIR); 96151f72f4aSEric W. Biederman return container_of(head, struct ctl_dir, header); 9621f87f0b5SEric W. Biederman } 9631f87f0b5SEric W. Biederman 9647ec66d06SEric W. Biederman static struct ctl_dir *new_dir(struct ctl_table_set *set, 9657ec66d06SEric W. Biederman const char *name, int namelen) 9661f87f0b5SEric W. Biederman { 9677ec66d06SEric W. Biederman struct ctl_table *table; 9687ec66d06SEric W. Biederman struct ctl_dir *new; 969ac13ac6fSEric W. Biederman struct ctl_node *node; 9707ec66d06SEric W. Biederman char *new_name; 9711f87f0b5SEric W. Biederman 972ac13ac6fSEric W. Biederman new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) + 973ac13ac6fSEric W. Biederman sizeof(struct ctl_table)*2 + namelen + 1, 974ac13ac6fSEric W. Biederman GFP_KERNEL); 9757ec66d06SEric W. Biederman if (!new) 9767ec66d06SEric W. Biederman return NULL; 9777ec66d06SEric W. Biederman 978ac13ac6fSEric W. Biederman node = (struct ctl_node *)(new + 1); 979ac13ac6fSEric W. Biederman table = (struct ctl_table *)(node + 1); 9807ec66d06SEric W. Biederman new_name = (char *)(table + 2); 9817ec66d06SEric W. Biederman memcpy(new_name, name, namelen); 9827ec66d06SEric W. Biederman new_name[namelen] = '\0'; 9837ec66d06SEric W. Biederman table[0].procname = new_name; 9847ec66d06SEric W. Biederman table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO; 985ac13ac6fSEric W. Biederman init_header(&new->header, set->dir.header.root, set, node, table); 9867ec66d06SEric W. Biederman 9877ec66d06SEric W. Biederman return new; 9881f87f0b5SEric W. Biederman } 9891f87f0b5SEric W. Biederman 99060f126d9SEric W. Biederman /** 99160f126d9SEric W. Biederman * get_subdir - find or create a subdir with the specified name. 99260f126d9SEric W. Biederman * @dir: Directory to create the subdirectory in 99360f126d9SEric W. Biederman * @name: The name of the subdirectory to find or create 99460f126d9SEric W. Biederman * @namelen: The length of name 99560f126d9SEric W. Biederman * 99660f126d9SEric W. Biederman * Takes a directory with an elevated reference count so we know that 99760f126d9SEric W. Biederman * if we drop the lock the directory will not go away. Upon success 99860f126d9SEric W. Biederman * the reference is moved from @dir to the returned subdirectory. 99960f126d9SEric W. Biederman * Upon error an error code is returned and the reference on @dir is 100060f126d9SEric W. Biederman * simply dropped. 100160f126d9SEric W. Biederman */ 10020e47c99dSEric W. Biederman static struct ctl_dir *get_subdir(struct ctl_dir *dir, 10030e47c99dSEric W. Biederman const char *name, int namelen) 10047ec66d06SEric W. Biederman { 10050e47c99dSEric W. Biederman struct ctl_table_set *set = dir->header.set; 10067ec66d06SEric W. Biederman struct ctl_dir *subdir, *new = NULL; 10070eb97f38SEric W. Biederman int err; 10087ec66d06SEric W. Biederman 10097ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 10100e47c99dSEric W. Biederman subdir = find_subdir(dir, name, namelen); 10117ec66d06SEric W. Biederman if (!IS_ERR(subdir)) 10127ec66d06SEric W. Biederman goto found; 10137ec66d06SEric W. Biederman if (PTR_ERR(subdir) != -ENOENT) 10147ec66d06SEric W. Biederman goto failed; 10157ec66d06SEric W. Biederman 10167ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 10177ec66d06SEric W. Biederman new = new_dir(set, name, namelen); 10187ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 10197ec66d06SEric W. Biederman subdir = ERR_PTR(-ENOMEM); 10207ec66d06SEric W. Biederman if (!new) 10217ec66d06SEric W. Biederman goto failed; 10227ec66d06SEric W. Biederman 102360f126d9SEric W. Biederman /* Was the subdir added while we dropped the lock? */ 10240e47c99dSEric W. Biederman subdir = find_subdir(dir, name, namelen); 10257ec66d06SEric W. Biederman if (!IS_ERR(subdir)) 10267ec66d06SEric W. Biederman goto found; 10277ec66d06SEric W. Biederman if (PTR_ERR(subdir) != -ENOENT) 10287ec66d06SEric W. Biederman goto failed; 10297ec66d06SEric W. Biederman 103060f126d9SEric W. Biederman /* Nope. Use the our freshly made directory entry. */ 10310eb97f38SEric W. Biederman err = insert_header(dir, &new->header); 10320eb97f38SEric W. Biederman subdir = ERR_PTR(err); 10330eb97f38SEric W. Biederman if (err) 10340e47c99dSEric W. Biederman goto failed; 10357ec66d06SEric W. Biederman subdir = new; 10367ec66d06SEric W. Biederman found: 10377ec66d06SEric W. Biederman subdir->header.nreg++; 10387ec66d06SEric W. Biederman failed: 1039a1c83681SViresh Kumar if (IS_ERR(subdir)) { 104087ebdc00SAndrew Morton pr_err("sysctl could not get directory: "); 10416980128fSEric W. Biederman sysctl_print_dir(dir); 104287ebdc00SAndrew Morton pr_cont("/%*.*s %ld\n", 10437ec66d06SEric W. Biederman namelen, namelen, name, PTR_ERR(subdir)); 10441f87f0b5SEric W. Biederman } 10457ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 10467ec66d06SEric W. Biederman if (new) 10477ec66d06SEric W. Biederman drop_sysctl_table(&new->header); 10487ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 10497ec66d06SEric W. Biederman return subdir; 10501f87f0b5SEric W. Biederman } 10511f87f0b5SEric W. Biederman 10520e47c99dSEric W. Biederman static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir) 10530e47c99dSEric W. Biederman { 10540e47c99dSEric W. Biederman struct ctl_dir *parent; 10550e47c99dSEric W. Biederman const char *procname; 10560e47c99dSEric W. Biederman if (!dir->header.parent) 10570e47c99dSEric W. Biederman return &set->dir; 10580e47c99dSEric W. Biederman parent = xlate_dir(set, dir->header.parent); 10590e47c99dSEric W. Biederman if (IS_ERR(parent)) 10600e47c99dSEric W. Biederman return parent; 10610e47c99dSEric W. Biederman procname = dir->header.ctl_table[0].procname; 10620e47c99dSEric W. Biederman return find_subdir(parent, procname, strlen(procname)); 10630e47c99dSEric W. Biederman } 10640e47c99dSEric W. Biederman 10650e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead, 106613bcc6a2SEric W. Biederman struct ctl_table **pentry) 10670e47c99dSEric W. Biederman { 10680e47c99dSEric W. Biederman struct ctl_table_header *head; 10690e47c99dSEric W. Biederman struct ctl_table_root *root; 10700e47c99dSEric W. Biederman struct ctl_table_set *set; 10710e47c99dSEric W. Biederman struct ctl_table *entry; 10720e47c99dSEric W. Biederman struct ctl_dir *dir; 10730e47c99dSEric W. Biederman int ret; 10740e47c99dSEric W. Biederman 10750e47c99dSEric W. Biederman ret = 0; 10760e47c99dSEric W. Biederman spin_lock(&sysctl_lock); 10770e47c99dSEric W. Biederman root = (*pentry)->data; 107813bcc6a2SEric W. Biederman set = lookup_header_set(root); 10790e47c99dSEric W. Biederman dir = xlate_dir(set, (*phead)->parent); 10800e47c99dSEric W. Biederman if (IS_ERR(dir)) 10810e47c99dSEric W. Biederman ret = PTR_ERR(dir); 10820e47c99dSEric W. Biederman else { 10830e47c99dSEric W. Biederman const char *procname = (*pentry)->procname; 10840e47c99dSEric W. Biederman head = NULL; 10850e47c99dSEric W. Biederman entry = find_entry(&head, dir, procname, strlen(procname)); 10860e47c99dSEric W. Biederman ret = -ENOENT; 10870e47c99dSEric W. Biederman if (entry && use_table(head)) { 10880e47c99dSEric W. Biederman unuse_table(*phead); 10890e47c99dSEric W. Biederman *phead = head; 10900e47c99dSEric W. Biederman *pentry = entry; 10910e47c99dSEric W. Biederman ret = 0; 10920e47c99dSEric W. Biederman } 10930e47c99dSEric W. Biederman } 10940e47c99dSEric W. Biederman 10950e47c99dSEric W. Biederman spin_unlock(&sysctl_lock); 10960e47c99dSEric W. Biederman return ret; 10970e47c99dSEric W. Biederman } 10980e47c99dSEric W. Biederman 10997c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...) 11007c60c48fSEric W. Biederman { 11017c60c48fSEric W. Biederman struct va_format vaf; 11027c60c48fSEric W. Biederman va_list args; 11037c60c48fSEric W. Biederman 11047c60c48fSEric W. Biederman va_start(args, fmt); 11057c60c48fSEric W. Biederman vaf.fmt = fmt; 11067c60c48fSEric W. Biederman vaf.va = &args; 11077c60c48fSEric W. Biederman 110887ebdc00SAndrew Morton pr_err("sysctl table check failed: %s/%s %pV\n", 11097c60c48fSEric W. Biederman path, table->procname, &vaf); 11107c60c48fSEric W. Biederman 11117c60c48fSEric W. Biederman va_end(args); 11127c60c48fSEric W. Biederman return -EINVAL; 11137c60c48fSEric W. Biederman } 11147c60c48fSEric W. Biederman 11154f2fec00SLuis R. Rodriguez static int sysctl_check_table_array(const char *path, struct ctl_table *table) 11164f2fec00SLuis R. Rodriguez { 11174f2fec00SLuis R. Rodriguez int err = 0; 11184f2fec00SLuis R. Rodriguez 111961d9b56aSLuis R. Rodriguez if ((table->proc_handler == proc_douintvec) || 112061d9b56aSLuis R. Rodriguez (table->proc_handler == proc_douintvec_minmax)) { 11214f2fec00SLuis R. Rodriguez if (table->maxlen != sizeof(unsigned int)) 112264a11f3dSWaiman Long err |= sysctl_err(path, table, "array not allowed"); 11234f2fec00SLuis R. Rodriguez } 11244f2fec00SLuis R. Rodriguez 11254f2fec00SLuis R. Rodriguez return err; 11264f2fec00SLuis R. Rodriguez } 11274f2fec00SLuis R. Rodriguez 11287c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table) 11297c60c48fSEric W. Biederman { 11307c60c48fSEric W. Biederman int err = 0; 11317c60c48fSEric W. Biederman for (; table->procname; table++) { 11327c60c48fSEric W. Biederman if (table->child) 113389c5b53bSLuis R. Rodriguez err |= sysctl_err(path, table, "Not a file"); 11347c60c48fSEric W. Biederman 11351f87f0b5SEric W. Biederman if ((table->proc_handler == proc_dostring) || 11361f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec) || 11371680a386SLiping Zhang (table->proc_handler == proc_douintvec) || 113861d9b56aSLuis R. Rodriguez (table->proc_handler == proc_douintvec_minmax) || 11391f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_minmax) || 11401f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_jiffies) || 11411f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_userhz_jiffies) || 11421f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_ms_jiffies) || 11431f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_minmax) || 11441f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) { 11451f87f0b5SEric W. Biederman if (!table->data) 114689c5b53bSLuis R. Rodriguez err |= sysctl_err(path, table, "No data"); 11471f87f0b5SEric W. Biederman if (!table->maxlen) 114889c5b53bSLuis R. Rodriguez err |= sysctl_err(path, table, "No maxlen"); 11494f2fec00SLuis R. Rodriguez else 11504f2fec00SLuis R. Rodriguez err |= sysctl_check_table_array(path, table); 11511f87f0b5SEric W. Biederman } 11521f87f0b5SEric W. Biederman if (!table->proc_handler) 115389c5b53bSLuis R. Rodriguez err |= sysctl_err(path, table, "No proc_handler"); 11547c60c48fSEric W. Biederman 11557c60c48fSEric W. Biederman if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode) 115689c5b53bSLuis R. Rodriguez err |= sysctl_err(path, table, "bogus .mode 0%o", 11577c60c48fSEric W. Biederman table->mode); 11581f87f0b5SEric W. Biederman } 11597c60c48fSEric W. Biederman return err; 11601f87f0b5SEric W. Biederman } 11611f87f0b5SEric W. Biederman 11620e47c99dSEric W. Biederman static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table, 11630e47c99dSEric W. Biederman struct ctl_table_root *link_root) 11640e47c99dSEric W. Biederman { 11650e47c99dSEric W. Biederman struct ctl_table *link_table, *entry, *link; 11660e47c99dSEric W. Biederman struct ctl_table_header *links; 1167ac13ac6fSEric W. Biederman struct ctl_node *node; 11680e47c99dSEric W. Biederman char *link_name; 11690e47c99dSEric W. Biederman int nr_entries, name_bytes; 11700e47c99dSEric W. Biederman 11710e47c99dSEric W. Biederman name_bytes = 0; 11720e47c99dSEric W. Biederman nr_entries = 0; 11730e47c99dSEric W. Biederman for (entry = table; entry->procname; entry++) { 11740e47c99dSEric W. Biederman nr_entries++; 11750e47c99dSEric W. Biederman name_bytes += strlen(entry->procname) + 1; 11760e47c99dSEric W. Biederman } 11770e47c99dSEric W. Biederman 11780e47c99dSEric W. Biederman links = kzalloc(sizeof(struct ctl_table_header) + 1179ac13ac6fSEric W. Biederman sizeof(struct ctl_node)*nr_entries + 11800e47c99dSEric W. Biederman sizeof(struct ctl_table)*(nr_entries + 1) + 11810e47c99dSEric W. Biederman name_bytes, 11820e47c99dSEric W. Biederman GFP_KERNEL); 11830e47c99dSEric W. Biederman 11840e47c99dSEric W. Biederman if (!links) 11850e47c99dSEric W. Biederman return NULL; 11860e47c99dSEric W. Biederman 1187ac13ac6fSEric W. Biederman node = (struct ctl_node *)(links + 1); 1188ac13ac6fSEric W. Biederman link_table = (struct ctl_table *)(node + nr_entries); 11890e47c99dSEric W. Biederman link_name = (char *)&link_table[nr_entries + 1]; 11900e47c99dSEric W. Biederman 11910e47c99dSEric W. Biederman for (link = link_table, entry = table; entry->procname; link++, entry++) { 11920e47c99dSEric W. Biederman int len = strlen(entry->procname) + 1; 11930e47c99dSEric W. Biederman memcpy(link_name, entry->procname, len); 11940e47c99dSEric W. Biederman link->procname = link_name; 11950e47c99dSEric W. Biederman link->mode = S_IFLNK|S_IRWXUGO; 11960e47c99dSEric W. Biederman link->data = link_root; 11970e47c99dSEric W. Biederman link_name += len; 11980e47c99dSEric W. Biederman } 1199ac13ac6fSEric W. Biederman init_header(links, dir->header.root, dir->header.set, node, link_table); 12000e47c99dSEric W. Biederman links->nreg = nr_entries; 12010e47c99dSEric W. Biederman 12020e47c99dSEric W. Biederman return links; 12030e47c99dSEric W. Biederman } 12040e47c99dSEric W. Biederman 12050e47c99dSEric W. Biederman static bool get_links(struct ctl_dir *dir, 12060e47c99dSEric W. Biederman struct ctl_table *table, struct ctl_table_root *link_root) 12070e47c99dSEric W. Biederman { 12080e47c99dSEric W. Biederman struct ctl_table_header *head; 12090e47c99dSEric W. Biederman struct ctl_table *entry, *link; 12100e47c99dSEric W. Biederman 12110e47c99dSEric W. Biederman /* Are there links available for every entry in table? */ 12120e47c99dSEric W. Biederman for (entry = table; entry->procname; entry++) { 12130e47c99dSEric W. Biederman const char *procname = entry->procname; 12140e47c99dSEric W. Biederman link = find_entry(&head, dir, procname, strlen(procname)); 12150e47c99dSEric W. Biederman if (!link) 12160e47c99dSEric W. Biederman return false; 12170e47c99dSEric W. Biederman if (S_ISDIR(link->mode) && S_ISDIR(entry->mode)) 12180e47c99dSEric W. Biederman continue; 12190e47c99dSEric W. Biederman if (S_ISLNK(link->mode) && (link->data == link_root)) 12200e47c99dSEric W. Biederman continue; 12210e47c99dSEric W. Biederman return false; 12220e47c99dSEric W. Biederman } 12230e47c99dSEric W. Biederman 12240e47c99dSEric W. Biederman /* The checks passed. Increase the registration count on the links */ 12250e47c99dSEric W. Biederman for (entry = table; entry->procname; entry++) { 12260e47c99dSEric W. Biederman const char *procname = entry->procname; 12270e47c99dSEric W. Biederman link = find_entry(&head, dir, procname, strlen(procname)); 12280e47c99dSEric W. Biederman head->nreg++; 12290e47c99dSEric W. Biederman } 12300e47c99dSEric W. Biederman return true; 12310e47c99dSEric W. Biederman } 12320e47c99dSEric W. Biederman 12330e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head) 12340e47c99dSEric W. Biederman { 12350e47c99dSEric W. Biederman struct ctl_table_set *root_set = &sysctl_table_root.default_set; 12360e47c99dSEric W. Biederman struct ctl_dir *core_parent = NULL; 12370e47c99dSEric W. Biederman struct ctl_table_header *links; 12380e47c99dSEric W. Biederman int err; 12390e47c99dSEric W. Biederman 12400e47c99dSEric W. Biederman if (head->set == root_set) 12410e47c99dSEric W. Biederman return 0; 12420e47c99dSEric W. Biederman 12430e47c99dSEric W. Biederman core_parent = xlate_dir(root_set, head->parent); 12440e47c99dSEric W. Biederman if (IS_ERR(core_parent)) 12450e47c99dSEric W. Biederman return 0; 12460e47c99dSEric W. Biederman 12470e47c99dSEric W. Biederman if (get_links(core_parent, head->ctl_table, head->root)) 12480e47c99dSEric W. Biederman return 0; 12490e47c99dSEric W. Biederman 12500e47c99dSEric W. Biederman core_parent->header.nreg++; 12510e47c99dSEric W. Biederman spin_unlock(&sysctl_lock); 12520e47c99dSEric W. Biederman 12530e47c99dSEric W. Biederman links = new_links(core_parent, head->ctl_table, head->root); 12540e47c99dSEric W. Biederman 12550e47c99dSEric W. Biederman spin_lock(&sysctl_lock); 12560e47c99dSEric W. Biederman err = -ENOMEM; 12570e47c99dSEric W. Biederman if (!links) 12580e47c99dSEric W. Biederman goto out; 12590e47c99dSEric W. Biederman 12600e47c99dSEric W. Biederman err = 0; 12610e47c99dSEric W. Biederman if (get_links(core_parent, head->ctl_table, head->root)) { 12620e47c99dSEric W. Biederman kfree(links); 12630e47c99dSEric W. Biederman goto out; 12640e47c99dSEric W. Biederman } 12650e47c99dSEric W. Biederman 12660e47c99dSEric W. Biederman err = insert_header(core_parent, links); 12670e47c99dSEric W. Biederman if (err) 12680e47c99dSEric W. Biederman kfree(links); 12690e47c99dSEric W. Biederman out: 12700e47c99dSEric W. Biederman drop_sysctl_table(&core_parent->header); 12710e47c99dSEric W. Biederman return err; 12720e47c99dSEric W. Biederman } 12730e47c99dSEric W. Biederman 12741f87f0b5SEric W. Biederman /** 1275f728019bSEric W. Biederman * __register_sysctl_table - register a leaf sysctl table 127660a47a2eSEric W. Biederman * @set: Sysctl tree to register on 12771f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 12781f87f0b5SEric W. Biederman * @table: the top-level table structure 12791f87f0b5SEric W. Biederman * 12801f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 12811f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 12821f87f0b5SEric W. Biederman * 12831f87f0b5SEric W. Biederman * The members of the &struct ctl_table structure are used as follows: 12841f87f0b5SEric W. Biederman * 12851f87f0b5SEric W. Biederman * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not 12861f87f0b5SEric W. Biederman * enter a sysctl file 12871f87f0b5SEric W. Biederman * 12881f87f0b5SEric W. Biederman * data - a pointer to data for use by proc_handler 12891f87f0b5SEric W. Biederman * 12901f87f0b5SEric W. Biederman * maxlen - the maximum size in bytes of the data 12911f87f0b5SEric W. Biederman * 1292f728019bSEric W. Biederman * mode - the file permissions for the /proc/sys file 12931f87f0b5SEric W. Biederman * 1294f728019bSEric W. Biederman * child - must be %NULL. 12951f87f0b5SEric W. Biederman * 12961f87f0b5SEric W. Biederman * proc_handler - the text handler routine (described below) 12971f87f0b5SEric W. Biederman * 12981f87f0b5SEric W. Biederman * extra1, extra2 - extra pointers usable by the proc handler routines 12991f87f0b5SEric W. Biederman * 13001f87f0b5SEric W. Biederman * Leaf nodes in the sysctl tree will be represented by a single file 13011f87f0b5SEric W. Biederman * under /proc; non-leaf nodes will be represented by directories. 13021f87f0b5SEric W. Biederman * 1303f728019bSEric W. Biederman * There must be a proc_handler routine for any terminal nodes. 1304f728019bSEric W. Biederman * Several default handlers are available to cover common cases - 13051f87f0b5SEric W. Biederman * 13061f87f0b5SEric W. Biederman * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(), 13071f87f0b5SEric W. Biederman * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), 13081f87f0b5SEric W. Biederman * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax() 13091f87f0b5SEric W. Biederman * 13101f87f0b5SEric W. Biederman * It is the handler's job to read the input buffer from user memory 13111f87f0b5SEric W. Biederman * and process it. The handler should return 0 on success. 13121f87f0b5SEric W. Biederman * 13131f87f0b5SEric W. Biederman * This routine returns %NULL on a failure to register, and a pointer 13141f87f0b5SEric W. Biederman * to the table header on success. 13151f87f0b5SEric W. Biederman */ 13166e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table( 131760a47a2eSEric W. Biederman struct ctl_table_set *set, 13186e9d5164SEric W. Biederman const char *path, struct ctl_table *table) 13191f87f0b5SEric W. Biederman { 132060a47a2eSEric W. Biederman struct ctl_table_root *root = set->dir.header.root; 13211f87f0b5SEric W. Biederman struct ctl_table_header *header; 13226e9d5164SEric W. Biederman const char *name, *nextname; 13237ec66d06SEric W. Biederman struct ctl_dir *dir; 1324ac13ac6fSEric W. Biederman struct ctl_table *entry; 1325ac13ac6fSEric W. Biederman struct ctl_node *node; 1326ac13ac6fSEric W. Biederman int nr_entries = 0; 13271f87f0b5SEric W. Biederman 1328ac13ac6fSEric W. Biederman for (entry = table; entry->procname; entry++) 1329ac13ac6fSEric W. Biederman nr_entries++; 1330ac13ac6fSEric W. Biederman 1331ac13ac6fSEric W. Biederman header = kzalloc(sizeof(struct ctl_table_header) + 1332ac13ac6fSEric W. Biederman sizeof(struct ctl_node)*nr_entries, GFP_KERNEL); 13331f87f0b5SEric W. Biederman if (!header) 13341f87f0b5SEric W. Biederman return NULL; 13351f87f0b5SEric W. Biederman 1336ac13ac6fSEric W. Biederman node = (struct ctl_node *)(header + 1); 1337ac13ac6fSEric W. Biederman init_header(header, root, set, node, table); 13387c60c48fSEric W. Biederman if (sysctl_check_table(path, table)) 13397c60c48fSEric W. Biederman goto fail; 13408d6ecfccSEric W. Biederman 13411f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 13420e47c99dSEric W. Biederman dir = &set->dir; 134360f126d9SEric W. Biederman /* Reference moved down the diretory tree get_subdir */ 13447ec66d06SEric W. Biederman dir->header.nreg++; 13457ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 13467ec66d06SEric W. Biederman 13477ec66d06SEric W. Biederman /* Find the directory for the ctl_table */ 13487ec66d06SEric W. Biederman for (name = path; name; name = nextname) { 13497ec66d06SEric W. Biederman int namelen; 13507ec66d06SEric W. Biederman nextname = strchr(name, '/'); 13517ec66d06SEric W. Biederman if (nextname) { 13527ec66d06SEric W. Biederman namelen = nextname - name; 13537ec66d06SEric W. Biederman nextname++; 13547ec66d06SEric W. Biederman } else { 13557ec66d06SEric W. Biederman namelen = strlen(name); 13567ec66d06SEric W. Biederman } 13577ec66d06SEric W. Biederman if (namelen == 0) 13581f87f0b5SEric W. Biederman continue; 13597ec66d06SEric W. Biederman 13600e47c99dSEric W. Biederman dir = get_subdir(dir, name, namelen); 13617ec66d06SEric W. Biederman if (IS_ERR(dir)) 13627ec66d06SEric W. Biederman goto fail; 13631f87f0b5SEric W. Biederman } 13640e47c99dSEric W. Biederman 13657ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 13660e47c99dSEric W. Biederman if (insert_header(dir, header)) 13670e47c99dSEric W. Biederman goto fail_put_dir_locked; 13680e47c99dSEric W. Biederman 13697ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 13701f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 13711f87f0b5SEric W. Biederman 13721f87f0b5SEric W. Biederman return header; 13730e47c99dSEric W. Biederman 13747ec66d06SEric W. Biederman fail_put_dir_locked: 13757ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 13767c60c48fSEric W. Biederman spin_unlock(&sysctl_lock); 13777c60c48fSEric W. Biederman fail: 13787c60c48fSEric W. Biederman kfree(header); 13797c60c48fSEric W. Biederman dump_stack(); 13807c60c48fSEric W. Biederman return NULL; 13811f87f0b5SEric W. Biederman } 13821f87f0b5SEric W. Biederman 1383fea478d4SEric W. Biederman /** 1384fea478d4SEric W. Biederman * register_sysctl - register a sysctl table 1385fea478d4SEric W. Biederman * @path: The path to the directory the sysctl table is in. 1386fea478d4SEric W. Biederman * @table: the table structure 1387fea478d4SEric W. Biederman * 1388fea478d4SEric W. Biederman * Register a sysctl table. @table should be a filled in ctl_table 1389fea478d4SEric W. Biederman * array. A completely 0 filled entry terminates the table. 1390fea478d4SEric W. Biederman * 1391fea478d4SEric W. Biederman * See __register_sysctl_table for more details. 1392fea478d4SEric W. Biederman */ 1393fea478d4SEric W. Biederman struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table) 1394fea478d4SEric W. Biederman { 1395fea478d4SEric W. Biederman return __register_sysctl_table(&sysctl_table_root.default_set, 1396fea478d4SEric W. Biederman path, table); 1397fea478d4SEric W. Biederman } 1398fea478d4SEric W. Biederman EXPORT_SYMBOL(register_sysctl); 1399fea478d4SEric W. Biederman 14006e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name) 14016e9d5164SEric W. Biederman { 14026e9d5164SEric W. Biederman int namelen; 14036e9d5164SEric W. Biederman namelen = strlen(name); 14046e9d5164SEric W. Biederman if (((pos - path) + namelen + 2) >= PATH_MAX) 14056e9d5164SEric W. Biederman return NULL; 14066e9d5164SEric W. Biederman memcpy(pos, name, namelen); 14076e9d5164SEric W. Biederman pos[namelen] = '/'; 14086e9d5164SEric W. Biederman pos[namelen + 1] = '\0'; 14096e9d5164SEric W. Biederman pos += namelen + 1; 14106e9d5164SEric W. Biederman return pos; 14116e9d5164SEric W. Biederman } 14126e9d5164SEric W. Biederman 1413f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table) 1414f728019bSEric W. Biederman { 1415f728019bSEric W. Biederman int has_files = 0; 1416f728019bSEric W. Biederman int nr_subheaders = 0; 1417f728019bSEric W. Biederman struct ctl_table *entry; 1418f728019bSEric W. Biederman 1419f728019bSEric W. Biederman /* special case: no directory and empty directory */ 1420f728019bSEric W. Biederman if (!table || !table->procname) 1421f728019bSEric W. Biederman return 1; 1422f728019bSEric W. Biederman 1423f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1424f728019bSEric W. Biederman if (entry->child) 1425f728019bSEric W. Biederman nr_subheaders += count_subheaders(entry->child); 1426f728019bSEric W. Biederman else 1427f728019bSEric W. Biederman has_files = 1; 1428f728019bSEric W. Biederman } 1429f728019bSEric W. Biederman return nr_subheaders + has_files; 1430f728019bSEric W. Biederman } 1431f728019bSEric W. Biederman 1432f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos, 143360a47a2eSEric W. Biederman struct ctl_table_header ***subheader, struct ctl_table_set *set, 1434f728019bSEric W. Biederman struct ctl_table *table) 1435f728019bSEric W. Biederman { 1436f728019bSEric W. Biederman struct ctl_table *ctl_table_arg = NULL; 1437f728019bSEric W. Biederman struct ctl_table *entry, *files; 1438f728019bSEric W. Biederman int nr_files = 0; 1439f728019bSEric W. Biederman int nr_dirs = 0; 1440f728019bSEric W. Biederman int err = -ENOMEM; 1441f728019bSEric W. Biederman 1442f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1443f728019bSEric W. Biederman if (entry->child) 1444f728019bSEric W. Biederman nr_dirs++; 1445f728019bSEric W. Biederman else 1446f728019bSEric W. Biederman nr_files++; 1447f728019bSEric W. Biederman } 1448f728019bSEric W. Biederman 1449f728019bSEric W. Biederman files = table; 1450f728019bSEric W. Biederman /* If there are mixed files and directories we need a new table */ 1451f728019bSEric W. Biederman if (nr_dirs && nr_files) { 1452f728019bSEric W. Biederman struct ctl_table *new; 14536396bb22SKees Cook files = kcalloc(nr_files + 1, sizeof(struct ctl_table), 1454f728019bSEric W. Biederman GFP_KERNEL); 1455f728019bSEric W. Biederman if (!files) 1456f728019bSEric W. Biederman goto out; 1457f728019bSEric W. Biederman 1458f728019bSEric W. Biederman ctl_table_arg = files; 1459f728019bSEric W. Biederman for (new = files, entry = table; entry->procname; entry++) { 1460f728019bSEric W. Biederman if (entry->child) 1461f728019bSEric W. Biederman continue; 1462f728019bSEric W. Biederman *new = *entry; 1463f728019bSEric W. Biederman new++; 1464f728019bSEric W. Biederman } 1465f728019bSEric W. Biederman } 1466f728019bSEric W. Biederman 1467f728019bSEric W. Biederman /* Register everything except a directory full of subdirectories */ 1468f728019bSEric W. Biederman if (nr_files || !nr_dirs) { 1469f728019bSEric W. Biederman struct ctl_table_header *header; 147060a47a2eSEric W. Biederman header = __register_sysctl_table(set, path, files); 1471f728019bSEric W. Biederman if (!header) { 1472f728019bSEric W. Biederman kfree(ctl_table_arg); 1473f728019bSEric W. Biederman goto out; 1474f728019bSEric W. Biederman } 1475f728019bSEric W. Biederman 1476f728019bSEric W. Biederman /* Remember if we need to free the file table */ 1477f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1478f728019bSEric W. Biederman **subheader = header; 1479f728019bSEric W. Biederman (*subheader)++; 1480f728019bSEric W. Biederman } 1481f728019bSEric W. Biederman 1482f728019bSEric W. Biederman /* Recurse into the subdirectories. */ 1483f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1484f728019bSEric W. Biederman char *child_pos; 1485f728019bSEric W. Biederman 1486f728019bSEric W. Biederman if (!entry->child) 1487f728019bSEric W. Biederman continue; 1488f728019bSEric W. Biederman 1489f728019bSEric W. Biederman err = -ENAMETOOLONG; 1490f728019bSEric W. Biederman child_pos = append_path(path, pos, entry->procname); 1491f728019bSEric W. Biederman if (!child_pos) 1492f728019bSEric W. Biederman goto out; 1493f728019bSEric W. Biederman 1494f728019bSEric W. Biederman err = register_leaf_sysctl_tables(path, child_pos, subheader, 149560a47a2eSEric W. Biederman set, entry->child); 1496f728019bSEric W. Biederman pos[0] = '\0'; 1497f728019bSEric W. Biederman if (err) 1498f728019bSEric W. Biederman goto out; 1499f728019bSEric W. Biederman } 1500f728019bSEric W. Biederman err = 0; 1501f728019bSEric W. Biederman out: 1502f728019bSEric W. Biederman /* On failure our caller will unregister all registered subheaders */ 1503f728019bSEric W. Biederman return err; 1504f728019bSEric W. Biederman } 1505f728019bSEric W. Biederman 15066e9d5164SEric W. Biederman /** 15076e9d5164SEric W. Biederman * __register_sysctl_paths - register a sysctl table hierarchy 150860a47a2eSEric W. Biederman * @set: Sysctl tree to register on 15096e9d5164SEric W. Biederman * @path: The path to the directory the sysctl table is in. 15106e9d5164SEric W. Biederman * @table: the top-level table structure 15116e9d5164SEric W. Biederman * 15126e9d5164SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 15136e9d5164SEric W. Biederman * array. A completely 0 filled entry terminates the table. 15146e9d5164SEric W. Biederman * 15156e9d5164SEric W. Biederman * See __register_sysctl_table for more details. 15166e9d5164SEric W. Biederman */ 15176e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths( 151860a47a2eSEric W. Biederman struct ctl_table_set *set, 15196e9d5164SEric W. Biederman const struct ctl_path *path, struct ctl_table *table) 15206e9d5164SEric W. Biederman { 1521ec6a5266SEric W. Biederman struct ctl_table *ctl_table_arg = table; 1522f728019bSEric W. Biederman int nr_subheaders = count_subheaders(table); 1523f728019bSEric W. Biederman struct ctl_table_header *header = NULL, **subheaders, **subheader; 15246e9d5164SEric W. Biederman const struct ctl_path *component; 15256e9d5164SEric W. Biederman char *new_path, *pos; 15266e9d5164SEric W. Biederman 15276e9d5164SEric W. Biederman pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL); 15286e9d5164SEric W. Biederman if (!new_path) 15296e9d5164SEric W. Biederman return NULL; 15306e9d5164SEric W. Biederman 15316e9d5164SEric W. Biederman pos[0] = '\0'; 15326e9d5164SEric W. Biederman for (component = path; component->procname; component++) { 15336e9d5164SEric W. Biederman pos = append_path(new_path, pos, component->procname); 15346e9d5164SEric W. Biederman if (!pos) 15356e9d5164SEric W. Biederman goto out; 15366e9d5164SEric W. Biederman } 1537ec6a5266SEric W. Biederman while (table->procname && table->child && !table[1].procname) { 1538ec6a5266SEric W. Biederman pos = append_path(new_path, pos, table->procname); 1539ec6a5266SEric W. Biederman if (!pos) 1540ec6a5266SEric W. Biederman goto out; 1541ec6a5266SEric W. Biederman table = table->child; 1542ec6a5266SEric W. Biederman } 1543f728019bSEric W. Biederman if (nr_subheaders == 1) { 154460a47a2eSEric W. Biederman header = __register_sysctl_table(set, new_path, table); 1545ec6a5266SEric W. Biederman if (header) 1546ec6a5266SEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1547f728019bSEric W. Biederman } else { 1548f728019bSEric W. Biederman header = kzalloc(sizeof(*header) + 1549f728019bSEric W. Biederman sizeof(*subheaders)*nr_subheaders, GFP_KERNEL); 1550f728019bSEric W. Biederman if (!header) 1551f728019bSEric W. Biederman goto out; 1552f728019bSEric W. Biederman 1553f728019bSEric W. Biederman subheaders = (struct ctl_table_header **) (header + 1); 1554f728019bSEric W. Biederman subheader = subheaders; 1555f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1556f728019bSEric W. Biederman 1557f728019bSEric W. Biederman if (register_leaf_sysctl_tables(new_path, pos, &subheader, 155860a47a2eSEric W. Biederman set, table)) 1559f728019bSEric W. Biederman goto err_register_leaves; 1560f728019bSEric W. Biederman } 1561f728019bSEric W. Biederman 15626e9d5164SEric W. Biederman out: 15636e9d5164SEric W. Biederman kfree(new_path); 15646e9d5164SEric W. Biederman return header; 1565f728019bSEric W. Biederman 1566f728019bSEric W. Biederman err_register_leaves: 1567f728019bSEric W. Biederman while (subheader > subheaders) { 1568f728019bSEric W. Biederman struct ctl_table_header *subh = *(--subheader); 1569f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1570f728019bSEric W. Biederman unregister_sysctl_table(subh); 1571f728019bSEric W. Biederman kfree(table); 1572f728019bSEric W. Biederman } 1573f728019bSEric W. Biederman kfree(header); 1574f728019bSEric W. Biederman header = NULL; 1575f728019bSEric W. Biederman goto out; 15766e9d5164SEric W. Biederman } 15776e9d5164SEric W. Biederman 15781f87f0b5SEric W. Biederman /** 15791f87f0b5SEric W. Biederman * register_sysctl_table_path - register a sysctl table hierarchy 15801f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 15811f87f0b5SEric W. Biederman * @table: the top-level table structure 15821f87f0b5SEric W. Biederman * 15831f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 15841f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 15851f87f0b5SEric W. Biederman * 15861f87f0b5SEric W. Biederman * See __register_sysctl_paths for more details. 15871f87f0b5SEric W. Biederman */ 15881f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path, 15891f87f0b5SEric W. Biederman struct ctl_table *table) 15901f87f0b5SEric W. Biederman { 159160a47a2eSEric W. Biederman return __register_sysctl_paths(&sysctl_table_root.default_set, 15921f87f0b5SEric W. Biederman path, table); 15931f87f0b5SEric W. Biederman } 15941f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths); 15951f87f0b5SEric W. Biederman 15961f87f0b5SEric W. Biederman /** 15971f87f0b5SEric W. Biederman * register_sysctl_table - register a sysctl table hierarchy 15981f87f0b5SEric W. Biederman * @table: the top-level table structure 15991f87f0b5SEric W. Biederman * 16001f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 16011f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 16021f87f0b5SEric W. Biederman * 16031f87f0b5SEric W. Biederman * See register_sysctl_paths for more details. 16041f87f0b5SEric W. Biederman */ 16051f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table) 16061f87f0b5SEric W. Biederman { 16071f87f0b5SEric W. Biederman static const struct ctl_path null_path[] = { {} }; 16081f87f0b5SEric W. Biederman 16091f87f0b5SEric W. Biederman return register_sysctl_paths(null_path, table); 16101f87f0b5SEric W. Biederman } 16111f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table); 16121f87f0b5SEric W. Biederman 16130e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header) 16140e47c99dSEric W. Biederman { 16150e47c99dSEric W. Biederman struct ctl_table_set *root_set = &sysctl_table_root.default_set; 16160e47c99dSEric W. Biederman struct ctl_table_root *root = header->root; 16170e47c99dSEric W. Biederman struct ctl_dir *parent = header->parent; 16180e47c99dSEric W. Biederman struct ctl_dir *core_parent; 16190e47c99dSEric W. Biederman struct ctl_table *entry; 16200e47c99dSEric W. Biederman 16210e47c99dSEric W. Biederman if (header->set == root_set) 16220e47c99dSEric W. Biederman return; 16230e47c99dSEric W. Biederman 16240e47c99dSEric W. Biederman core_parent = xlate_dir(root_set, parent); 16250e47c99dSEric W. Biederman if (IS_ERR(core_parent)) 16260e47c99dSEric W. Biederman return; 16270e47c99dSEric W. Biederman 16280e47c99dSEric W. Biederman for (entry = header->ctl_table; entry->procname; entry++) { 16290e47c99dSEric W. Biederman struct ctl_table_header *link_head; 16300e47c99dSEric W. Biederman struct ctl_table *link; 16310e47c99dSEric W. Biederman const char *name = entry->procname; 16320e47c99dSEric W. Biederman 16330e47c99dSEric W. Biederman link = find_entry(&link_head, core_parent, name, strlen(name)); 16340e47c99dSEric W. Biederman if (link && 16350e47c99dSEric W. Biederman ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) || 16360e47c99dSEric W. Biederman (S_ISLNK(link->mode) && (link->data == root)))) { 16370e47c99dSEric W. Biederman drop_sysctl_table(link_head); 16380e47c99dSEric W. Biederman } 16390e47c99dSEric W. Biederman else { 164087ebdc00SAndrew Morton pr_err("sysctl link missing during unregister: "); 16410e47c99dSEric W. Biederman sysctl_print_dir(parent); 164287ebdc00SAndrew Morton pr_cont("/%s\n", name); 16430e47c99dSEric W. Biederman } 16440e47c99dSEric W. Biederman } 16450e47c99dSEric W. Biederman } 16460e47c99dSEric W. Biederman 1647938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header) 1648938aaa4fSEric W. Biederman { 16497ec66d06SEric W. Biederman struct ctl_dir *parent = header->parent; 16507ec66d06SEric W. Biederman 1651938aaa4fSEric W. Biederman if (--header->nreg) 1652938aaa4fSEric W. Biederman return; 1653938aaa4fSEric W. Biederman 165489189557SYueHaibing if (parent) { 16550e47c99dSEric W. Biederman put_links(header); 1656938aaa4fSEric W. Biederman start_unregistering(header); 165789189557SYueHaibing } 165889189557SYueHaibing 1659938aaa4fSEric W. Biederman if (!--header->count) 1660938aaa4fSEric W. Biederman kfree_rcu(header, rcu); 16617ec66d06SEric W. Biederman 16627ec66d06SEric W. Biederman if (parent) 16637ec66d06SEric W. Biederman drop_sysctl_table(&parent->header); 1664938aaa4fSEric W. Biederman } 1665938aaa4fSEric W. Biederman 16661f87f0b5SEric W. Biederman /** 16671f87f0b5SEric W. Biederman * unregister_sysctl_table - unregister a sysctl table hierarchy 16681f87f0b5SEric W. Biederman * @header: the header returned from register_sysctl_table 16691f87f0b5SEric W. Biederman * 16701f87f0b5SEric W. Biederman * Unregisters the sysctl table and all children. proc entries may not 16711f87f0b5SEric W. Biederman * actually be removed until they are no longer used by anyone. 16721f87f0b5SEric W. Biederman */ 16731f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header) 16741f87f0b5SEric W. Biederman { 1675f728019bSEric W. Biederman int nr_subheaders; 16761f87f0b5SEric W. Biederman might_sleep(); 16771f87f0b5SEric W. Biederman 16781f87f0b5SEric W. Biederman if (header == NULL) 16791f87f0b5SEric W. Biederman return; 16801f87f0b5SEric W. Biederman 1681f728019bSEric W. Biederman nr_subheaders = count_subheaders(header->ctl_table_arg); 1682f728019bSEric W. Biederman if (unlikely(nr_subheaders > 1)) { 1683f728019bSEric W. Biederman struct ctl_table_header **subheaders; 1684f728019bSEric W. Biederman int i; 1685f728019bSEric W. Biederman 1686f728019bSEric W. Biederman subheaders = (struct ctl_table_header **)(header + 1); 1687f728019bSEric W. Biederman for (i = nr_subheaders -1; i >= 0; i--) { 1688f728019bSEric W. Biederman struct ctl_table_header *subh = subheaders[i]; 1689f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1690f728019bSEric W. Biederman unregister_sysctl_table(subh); 1691f728019bSEric W. Biederman kfree(table); 1692f728019bSEric W. Biederman } 1693f728019bSEric W. Biederman kfree(header); 1694f728019bSEric W. Biederman return; 1695f728019bSEric W. Biederman } 1696f728019bSEric W. Biederman 16971f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1698938aaa4fSEric W. Biederman drop_sysctl_table(header); 16991f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 17001f87f0b5SEric W. Biederman } 17011f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table); 17021f87f0b5SEric W. Biederman 17030e47c99dSEric W. Biederman void setup_sysctl_set(struct ctl_table_set *set, 17049eb47c26SEric W. Biederman struct ctl_table_root *root, 17051f87f0b5SEric W. Biederman int (*is_seen)(struct ctl_table_set *)) 17061f87f0b5SEric W. Biederman { 17071347440dSDan Carpenter memset(set, 0, sizeof(*set)); 17080e47c99dSEric W. Biederman set->is_seen = is_seen; 1709ac13ac6fSEric W. Biederman init_header(&set->dir.header, root, set, NULL, root_table); 17101f87f0b5SEric W. Biederman } 17111f87f0b5SEric W. Biederman 171297324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set) 171397324cd8SEric W. Biederman { 1714ac13ac6fSEric W. Biederman WARN_ON(!RB_EMPTY_ROOT(&set->dir.root)); 171597324cd8SEric W. Biederman } 17161f87f0b5SEric W. Biederman 17171e0edd3fSAlexey Dobriyan int __init proc_sys_init(void) 171877b14db5SEric W. Biederman { 1719e1675231SAlexey Dobriyan struct proc_dir_entry *proc_sys_root; 1720e1675231SAlexey Dobriyan 172177b14db5SEric W. Biederman proc_sys_root = proc_mkdir("sys", NULL); 17229043476fSAl Viro proc_sys_root->proc_iops = &proc_sys_dir_operations; 1723d56c0d45SAlexey Dobriyan proc_sys_root->proc_dir_ops = &proc_sys_dir_file_operations; 172477b14db5SEric W. Biederman proc_sys_root->nlink = 0; 1725de4e83bdSEric W. Biederman 1726de4e83bdSEric W. Biederman return sysctl_init(); 172777b14db5SEric W. Biederman } 1728