177b14db5SEric W. Biederman /* 277b14db5SEric W. Biederman * /proc/sys support 377b14db5SEric W. Biederman */ 41e0edd3fSAlexey Dobriyan #include <linux/init.h> 577b14db5SEric W. Biederman #include <linux/sysctl.h> 6f1ecf068SLucas De Marchi #include <linux/poll.h> 777b14db5SEric W. Biederman #include <linux/proc_fs.h> 877b14db5SEric W. Biederman #include <linux/security.h> 934286d66SNick Piggin #include <linux/namei.h> 101f87f0b5SEric W. Biederman #include <linux/module.h> 1177b14db5SEric W. Biederman #include "internal.h" 1277b14db5SEric W. Biederman 13d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations; 1477b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations; 1503a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations; 169043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations; 179043476fSAl Viro static const struct inode_operations proc_sys_dir_operations; 1877b14db5SEric W. Biederman 19f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll) 20f1ecf068SLucas De Marchi { 21f1ecf068SLucas De Marchi if (!poll) 22f1ecf068SLucas De Marchi return; 23f1ecf068SLucas De Marchi 24f1ecf068SLucas De Marchi atomic_inc(&poll->event); 25f1ecf068SLucas De Marchi wake_up_interruptible(&poll->wait); 26f1ecf068SLucas De Marchi } 27f1ecf068SLucas De Marchi 28a194558eSEric W. Biederman static struct ctl_table root_table[] = { 29a194558eSEric W. Biederman { 30a194558eSEric W. Biederman .procname = "", 317ec66d06SEric W. Biederman .mode = S_IFDIR|S_IRUGO|S_IXUGO, 32a194558eSEric W. Biederman }, 33a194558eSEric W. Biederman { } 34a194558eSEric W. Biederman }; 350e47c99dSEric W. Biederman static struct ctl_table_root sysctl_table_root = { 360e47c99dSEric W. Biederman .default_set.list = LIST_HEAD_INIT(sysctl_table_root.default_set.dir.header.ctl_entry), 370e47c99dSEric W. Biederman .default_set.dir.header = { 381f87f0b5SEric W. Biederman {{.count = 1, 39938aaa4fSEric W. Biederman .nreg = 1, 401f87f0b5SEric W. Biederman .ctl_table = root_table, 411f87f0b5SEric W. Biederman .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}}, 420e47c99dSEric W. Biederman .ctl_table_arg = root_table, 431f87f0b5SEric W. Biederman .root = &sysctl_table_root, 441f87f0b5SEric W. Biederman .set = &sysctl_table_root.default_set, 457ec66d06SEric W. Biederman }, 461f87f0b5SEric W. Biederman }; 471f87f0b5SEric W. Biederman 481f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock); 491f87f0b5SEric W. Biederman 507ec66d06SEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header); 510e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead, 520e47c99dSEric W. Biederman struct ctl_table **pentry, struct nsproxy *namespaces); 530e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head); 540e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header); 557ec66d06SEric W. Biederman 566980128fSEric W. Biederman static void sysctl_print_dir(struct ctl_dir *dir) 576980128fSEric W. Biederman { 586980128fSEric W. Biederman if (dir->header.parent) 596980128fSEric W. Biederman sysctl_print_dir(dir->header.parent); 606980128fSEric W. Biederman printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname); 616980128fSEric W. Biederman } 626980128fSEric W. Biederman 63076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2) 64076c3eedSEric W. Biederman { 65076c3eedSEric W. Biederman int minlen; 66076c3eedSEric W. Biederman int cmp; 67076c3eedSEric W. Biederman 68076c3eedSEric W. Biederman minlen = len1; 69076c3eedSEric W. Biederman if (minlen > len2) 70076c3eedSEric W. Biederman minlen = len2; 71076c3eedSEric W. Biederman 72076c3eedSEric W. Biederman cmp = memcmp(name1, name2, minlen); 73076c3eedSEric W. Biederman if (cmp == 0) 74076c3eedSEric W. Biederman cmp = len1 - len2; 75076c3eedSEric W. Biederman return cmp; 76076c3eedSEric W. Biederman } 77076c3eedSEric W. Biederman 78076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead, 790e47c99dSEric W. Biederman struct ctl_dir *dir, const char *name, int namelen) 80076c3eedSEric W. Biederman { 810e47c99dSEric W. Biederman struct ctl_table_set *set = dir->header.set; 82076c3eedSEric W. Biederman struct ctl_table_header *head; 83076c3eedSEric W. Biederman struct ctl_table *entry; 84076c3eedSEric W. Biederman 85076c3eedSEric W. Biederman list_for_each_entry(head, &set->list, ctl_entry) { 86076c3eedSEric W. Biederman if (head->unregistering) 87076c3eedSEric W. Biederman continue; 887ec66d06SEric W. Biederman if (head->parent != dir) 89076c3eedSEric W. Biederman continue; 907ec66d06SEric W. Biederman for (entry = head->ctl_table; entry->procname; entry++) { 91076c3eedSEric W. Biederman const char *procname = entry->procname; 92076c3eedSEric W. Biederman if (namecmp(procname, strlen(procname), name, namelen) == 0) { 93076c3eedSEric W. Biederman *phead = head; 94076c3eedSEric W. Biederman return entry; 95076c3eedSEric W. Biederman } 96076c3eedSEric W. Biederman } 97076c3eedSEric W. Biederman } 98076c3eedSEric W. Biederman return NULL; 99076c3eedSEric W. Biederman } 100076c3eedSEric W. Biederman 101e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head, 102e0d04529SEric W. Biederman struct ctl_table_root *root, struct ctl_table_set *set, 103e0d04529SEric W. Biederman struct ctl_table *table) 104e0d04529SEric W. Biederman { 1057ec66d06SEric W. Biederman head->ctl_table = table; 106e0d04529SEric W. Biederman head->ctl_table_arg = table; 107e0d04529SEric W. Biederman INIT_LIST_HEAD(&head->ctl_entry); 108e0d04529SEric W. Biederman head->used = 0; 109e0d04529SEric W. Biederman head->count = 1; 110e0d04529SEric W. Biederman head->nreg = 1; 111e0d04529SEric W. Biederman head->unregistering = NULL; 112e0d04529SEric W. Biederman head->root = root; 113e0d04529SEric W. Biederman head->set = set; 114e0d04529SEric W. Biederman head->parent = NULL; 115e0d04529SEric W. Biederman } 116e0d04529SEric W. Biederman 1178425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head) 1188425d6aaSEric W. Biederman { 1198425d6aaSEric W. Biederman list_del_init(&head->ctl_entry); 1208425d6aaSEric W. Biederman } 1218425d6aaSEric W. Biederman 1220e47c99dSEric W. Biederman static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header) 1238425d6aaSEric W. Biederman { 1240e47c99dSEric W. Biederman int err; 1250e47c99dSEric W. Biederman 1260e47c99dSEric W. Biederman dir->header.nreg++; 1277ec66d06SEric W. Biederman header->parent = dir; 1280e47c99dSEric W. Biederman err = insert_links(header); 1290e47c99dSEric W. Biederman if (err) 1300e47c99dSEric W. Biederman goto fail_links; 1318425d6aaSEric W. Biederman list_add_tail(&header->ctl_entry, &header->set->list); 1320e47c99dSEric W. Biederman return 0; 1330e47c99dSEric W. Biederman fail_links: 1340e47c99dSEric W. Biederman header->parent = NULL; 1350e47c99dSEric W. Biederman drop_sysctl_table(&dir->header); 1360e47c99dSEric W. Biederman return err; 1378425d6aaSEric W. Biederman } 1388425d6aaSEric W. Biederman 1391f87f0b5SEric W. Biederman /* called under sysctl_lock */ 1401f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p) 1411f87f0b5SEric W. Biederman { 1421f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 1431f87f0b5SEric W. Biederman return 0; 1441f87f0b5SEric W. Biederman p->used++; 1451f87f0b5SEric W. Biederman return 1; 1461f87f0b5SEric W. Biederman } 1471f87f0b5SEric W. Biederman 1481f87f0b5SEric W. Biederman /* called under sysctl_lock */ 1491f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p) 1501f87f0b5SEric W. Biederman { 1511f87f0b5SEric W. Biederman if (!--p->used) 1521f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 1531f87f0b5SEric W. Biederman complete(p->unregistering); 1541f87f0b5SEric W. Biederman } 1551f87f0b5SEric W. Biederman 1561f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */ 1571f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p) 1581f87f0b5SEric W. Biederman { 1591f87f0b5SEric W. Biederman /* 1601f87f0b5SEric W. Biederman * if p->used is 0, nobody will ever touch that entry again; 1611f87f0b5SEric W. Biederman * we'll eliminate all paths to it before dropping sysctl_lock 1621f87f0b5SEric W. Biederman */ 1631f87f0b5SEric W. Biederman if (unlikely(p->used)) { 1641f87f0b5SEric W. Biederman struct completion wait; 1651f87f0b5SEric W. Biederman init_completion(&wait); 1661f87f0b5SEric W. Biederman p->unregistering = &wait; 1671f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1681f87f0b5SEric W. Biederman wait_for_completion(&wait); 1691f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1701f87f0b5SEric W. Biederman } else { 1711f87f0b5SEric W. Biederman /* anything non-NULL; we'll never dereference it */ 1721f87f0b5SEric W. Biederman p->unregistering = ERR_PTR(-EINVAL); 1731f87f0b5SEric W. Biederman } 1741f87f0b5SEric W. Biederman /* 1751f87f0b5SEric W. Biederman * do not remove from the list until nobody holds it; walking the 1761f87f0b5SEric W. Biederman * list in do_sysctl() relies on that. 1771f87f0b5SEric W. Biederman */ 1788425d6aaSEric W. Biederman erase_header(p); 1791f87f0b5SEric W. Biederman } 1801f87f0b5SEric W. Biederman 1811f87f0b5SEric W. Biederman static void sysctl_head_get(struct ctl_table_header *head) 1821f87f0b5SEric W. Biederman { 1831f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1841f87f0b5SEric W. Biederman head->count++; 1851f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1861f87f0b5SEric W. Biederman } 1871f87f0b5SEric W. Biederman 1881f87f0b5SEric W. Biederman void sysctl_head_put(struct ctl_table_header *head) 1891f87f0b5SEric W. Biederman { 1901f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1911f87f0b5SEric W. Biederman if (!--head->count) 1921f87f0b5SEric W. Biederman kfree_rcu(head, rcu); 1931f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1941f87f0b5SEric W. Biederman } 1951f87f0b5SEric W. Biederman 1961f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head) 1971f87f0b5SEric W. Biederman { 1981f87f0b5SEric W. Biederman if (!head) 1991f87f0b5SEric W. Biederman BUG(); 2001f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 2011f87f0b5SEric W. Biederman if (!use_table(head)) 2021f87f0b5SEric W. Biederman head = ERR_PTR(-ENOENT); 2031f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 2041f87f0b5SEric W. Biederman return head; 2051f87f0b5SEric W. Biederman } 2061f87f0b5SEric W. Biederman 2071f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head) 2081f87f0b5SEric W. Biederman { 2091f87f0b5SEric W. Biederman if (!head) 2101f87f0b5SEric W. Biederman return; 2111f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 2121f87f0b5SEric W. Biederman unuse_table(head); 2131f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 2141f87f0b5SEric W. Biederman } 2151f87f0b5SEric W. Biederman 2161f87f0b5SEric W. Biederman static struct ctl_table_set * 2171f87f0b5SEric W. Biederman lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces) 2181f87f0b5SEric W. Biederman { 2191f87f0b5SEric W. Biederman struct ctl_table_set *set = &root->default_set; 2201f87f0b5SEric W. Biederman if (root->lookup) 2211f87f0b5SEric W. Biederman set = root->lookup(root, namespaces); 2221f87f0b5SEric W. Biederman return set; 2231f87f0b5SEric W. Biederman } 2241f87f0b5SEric W. Biederman 225076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead, 2267ec66d06SEric W. Biederman struct ctl_dir *dir, 227076c3eedSEric W. Biederman const char *name, int namelen) 228076c3eedSEric W. Biederman { 229076c3eedSEric W. Biederman struct ctl_table_header *head; 230076c3eedSEric W. Biederman struct ctl_table *entry; 231076c3eedSEric W. Biederman 232076c3eedSEric W. Biederman spin_lock(&sysctl_lock); 2330e47c99dSEric W. Biederman entry = find_entry(&head, dir, name, namelen); 234076c3eedSEric W. Biederman if (entry && use_table(head)) 235076c3eedSEric W. Biederman *phead = head; 236076c3eedSEric W. Biederman else 237076c3eedSEric W. Biederman entry = NULL; 238076c3eedSEric W. Biederman spin_unlock(&sysctl_lock); 239076c3eedSEric W. Biederman return entry; 240076c3eedSEric W. Biederman } 241076c3eedSEric W. Biederman 2427ec66d06SEric W. Biederman static struct ctl_table_header *next_usable_entry(struct ctl_dir *dir, 2430e47c99dSEric W. Biederman struct list_head *tmp) 2441f87f0b5SEric W. Biederman { 2450e47c99dSEric W. Biederman struct ctl_table_set *set = dir->header.set; 2461f87f0b5SEric W. Biederman struct ctl_table_header *head; 2471f87f0b5SEric W. Biederman 2480e47c99dSEric W. Biederman for (tmp = tmp->next; tmp != &set->list; tmp = tmp->next) { 2491f87f0b5SEric W. Biederman head = list_entry(tmp, struct ctl_table_header, ctl_entry); 2501f87f0b5SEric W. Biederman 2517ec66d06SEric W. Biederman if (head->parent != dir || 2527ec66d06SEric W. Biederman !head->ctl_table->procname || 2536a75ce16SEric W. Biederman !use_table(head)) 2541f87f0b5SEric W. Biederman continue; 2551f87f0b5SEric W. Biederman 2560e47c99dSEric W. Biederman return head; 2571f87f0b5SEric W. Biederman } 2581f87f0b5SEric W. Biederman return NULL; 2591f87f0b5SEric W. Biederman } 2601f87f0b5SEric W. Biederman 2617ec66d06SEric W. Biederman static void first_entry(struct ctl_dir *dir, 2626a75ce16SEric W. Biederman struct ctl_table_header **phead, struct ctl_table **pentry) 2631f87f0b5SEric W. Biederman { 2647ec66d06SEric W. Biederman struct ctl_table_header *head; 2657ec66d06SEric W. Biederman struct ctl_table *entry = NULL; 2666a75ce16SEric W. Biederman 2676a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 2680e47c99dSEric W. Biederman head = next_usable_entry(dir, &dir->header.set->list); 2696a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 2707ec66d06SEric W. Biederman if (head) 2717ec66d06SEric W. Biederman entry = head->ctl_table; 2726a75ce16SEric W. Biederman *phead = head; 2736a75ce16SEric W. Biederman *pentry = entry; 2746a75ce16SEric W. Biederman } 2756a75ce16SEric W. Biederman 2767ec66d06SEric W. Biederman static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry) 2776a75ce16SEric W. Biederman { 2786a75ce16SEric W. Biederman struct ctl_table_header *head = *phead; 2796a75ce16SEric W. Biederman struct ctl_table *entry = *pentry; 2806a75ce16SEric W. Biederman 2816a75ce16SEric W. Biederman entry++; 2826a75ce16SEric W. Biederman if (!entry->procname) { 2836a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 2846a75ce16SEric W. Biederman unuse_table(head); 2850e47c99dSEric W. Biederman head = next_usable_entry(head->parent, &head->ctl_entry); 2866a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 2876a75ce16SEric W. Biederman if (head) 2887ec66d06SEric W. Biederman entry = head->ctl_table; 2896a75ce16SEric W. Biederman } 2906a75ce16SEric W. Biederman *phead = head; 2916a75ce16SEric W. Biederman *pentry = entry; 2921f87f0b5SEric W. Biederman } 2931f87f0b5SEric W. Biederman 2941f87f0b5SEric W. Biederman void register_sysctl_root(struct ctl_table_root *root) 2951f87f0b5SEric W. Biederman { 2961f87f0b5SEric W. Biederman } 2971f87f0b5SEric W. Biederman 2981f87f0b5SEric W. Biederman /* 2991f87f0b5SEric W. Biederman * sysctl_perm does NOT grant the superuser all rights automatically, because 3001f87f0b5SEric W. Biederman * some sysctl variables are readonly even to root. 3011f87f0b5SEric W. Biederman */ 3021f87f0b5SEric W. Biederman 3031f87f0b5SEric W. Biederman static int test_perm(int mode, int op) 3041f87f0b5SEric W. Biederman { 3051f87f0b5SEric W. Biederman if (!current_euid()) 3061f87f0b5SEric W. Biederman mode >>= 6; 3071f87f0b5SEric W. Biederman else if (in_egroup_p(0)) 3081f87f0b5SEric W. Biederman mode >>= 3; 3091f87f0b5SEric W. Biederman if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0) 3101f87f0b5SEric W. Biederman return 0; 3111f87f0b5SEric W. Biederman return -EACCES; 3121f87f0b5SEric W. Biederman } 3131f87f0b5SEric W. Biederman 3141f87f0b5SEric W. Biederman static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op) 3151f87f0b5SEric W. Biederman { 3161f87f0b5SEric W. Biederman int mode; 3171f87f0b5SEric W. Biederman 3181f87f0b5SEric W. Biederman if (root->permissions) 3191f87f0b5SEric W. Biederman mode = root->permissions(root, current->nsproxy, table); 3201f87f0b5SEric W. Biederman else 3211f87f0b5SEric W. Biederman mode = table->mode; 3221f87f0b5SEric W. Biederman 3231f87f0b5SEric W. Biederman return test_perm(mode, op); 3241f87f0b5SEric W. Biederman } 3251f87f0b5SEric W. Biederman 3269043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb, 3279043476fSAl Viro struct ctl_table_header *head, struct ctl_table *table) 32877b14db5SEric W. Biederman { 32977b14db5SEric W. Biederman struct inode *inode; 3309043476fSAl Viro struct proc_inode *ei; 33177b14db5SEric W. Biederman 3329043476fSAl Viro inode = new_inode(sb); 33377b14db5SEric W. Biederman if (!inode) 33477b14db5SEric W. Biederman goto out; 33577b14db5SEric W. Biederman 33685fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 33785fe4025SChristoph Hellwig 3389043476fSAl Viro sysctl_head_get(head); 33977b14db5SEric W. Biederman ei = PROC_I(inode); 3409043476fSAl Viro ei->sysctl = head; 3419043476fSAl Viro ei->sysctl_entry = table; 3429043476fSAl Viro 34377b14db5SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 3449043476fSAl Viro inode->i_mode = table->mode; 3457ec66d06SEric W. Biederman if (!S_ISDIR(table->mode)) { 3469043476fSAl Viro inode->i_mode |= S_IFREG; 34777b14db5SEric W. Biederman inode->i_op = &proc_sys_inode_operations; 34877b14db5SEric W. Biederman inode->i_fop = &proc_sys_file_operations; 3499043476fSAl Viro } else { 3509043476fSAl Viro inode->i_mode |= S_IFDIR; 3519043476fSAl Viro inode->i_op = &proc_sys_dir_operations; 3529043476fSAl Viro inode->i_fop = &proc_sys_dir_file_operations; 3539043476fSAl Viro } 35477b14db5SEric W. Biederman out: 35577b14db5SEric W. Biederman return inode; 35677b14db5SEric W. Biederman } 35777b14db5SEric W. Biederman 35881324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode) 35977b14db5SEric W. Biederman { 3603cc3e046SEric W. Biederman struct ctl_table_header *head = PROC_I(inode)->sysctl; 3613cc3e046SEric W. Biederman if (!head) 3620e47c99dSEric W. Biederman head = &sysctl_table_root.default_set.dir.header; 3633cc3e046SEric W. Biederman return sysctl_head_grab(head); 36477b14db5SEric W. Biederman } 36577b14db5SEric W. Biederman 36677b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, 36777b14db5SEric W. Biederman struct nameidata *nd) 36877b14db5SEric W. Biederman { 3699043476fSAl Viro struct ctl_table_header *head = grab_header(dir); 3709043476fSAl Viro struct ctl_table_header *h = NULL; 3719043476fSAl Viro struct qstr *name = &dentry->d_name; 3729043476fSAl Viro struct ctl_table *p; 37377b14db5SEric W. Biederman struct inode *inode; 3749043476fSAl Viro struct dentry *err = ERR_PTR(-ENOENT); 3757ec66d06SEric W. Biederman struct ctl_dir *ctl_dir; 3760e47c99dSEric W. Biederman int ret; 37777b14db5SEric W. Biederman 3789043476fSAl Viro if (IS_ERR(head)) 3799043476fSAl Viro return ERR_CAST(head); 3809043476fSAl Viro 3817ec66d06SEric W. Biederman ctl_dir = container_of(head, struct ctl_dir, header); 3829043476fSAl Viro 3837ec66d06SEric W. Biederman p = lookup_entry(&h, ctl_dir, name->name, name->len); 3849043476fSAl Viro if (!p) 38577b14db5SEric W. Biederman goto out; 38677b14db5SEric W. Biederman 3870e47c99dSEric W. Biederman ret = sysctl_follow_link(&h, &p, current->nsproxy); 3880e47c99dSEric W. Biederman err = ERR_PTR(ret); 3890e47c99dSEric W. Biederman if (ret) 3900e47c99dSEric W. Biederman goto out; 3910e47c99dSEric W. Biederman 39277b14db5SEric W. Biederman err = ERR_PTR(-ENOMEM); 3939043476fSAl Viro inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p); 3949043476fSAl Viro if (h) 3959043476fSAl Viro sysctl_head_finish(h); 3969043476fSAl Viro 39777b14db5SEric W. Biederman if (!inode) 39877b14db5SEric W. Biederman goto out; 39977b14db5SEric W. Biederman 40077b14db5SEric W. Biederman err = NULL; 401fb045adbSNick Piggin d_set_d_op(dentry, &proc_sys_dentry_operations); 40277b14db5SEric W. Biederman d_add(dentry, inode); 40377b14db5SEric W. Biederman 40477b14db5SEric W. Biederman out: 40577b14db5SEric W. Biederman sysctl_head_finish(head); 40677b14db5SEric W. Biederman return err; 40777b14db5SEric W. Biederman } 40877b14db5SEric W. Biederman 4097708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf, 4107708bfb1SPavel Emelyanov size_t count, loff_t *ppos, int write) 41177b14db5SEric W. Biederman { 4129043476fSAl Viro struct inode *inode = filp->f_path.dentry->d_inode; 4139043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 4149043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 4152a2da53bSDavid Howells ssize_t error; 4162a2da53bSDavid Howells size_t res; 41777b14db5SEric W. Biederman 4189043476fSAl Viro if (IS_ERR(head)) 4199043476fSAl Viro return PTR_ERR(head); 42077b14db5SEric W. Biederman 42177b14db5SEric W. Biederman /* 42277b14db5SEric W. Biederman * At this point we know that the sysctl was not unregistered 42377b14db5SEric W. Biederman * and won't be until we finish. 42477b14db5SEric W. Biederman */ 42577b14db5SEric W. Biederman error = -EPERM; 426d7321cd6SPavel Emelyanov if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ)) 42777b14db5SEric W. Biederman goto out; 42877b14db5SEric W. Biederman 4299043476fSAl Viro /* if that can happen at all, it should be -EINVAL, not -EISDIR */ 4309043476fSAl Viro error = -EINVAL; 4319043476fSAl Viro if (!table->proc_handler) 4329043476fSAl Viro goto out; 4339043476fSAl Viro 43477b14db5SEric W. Biederman /* careful: calling conventions are nasty here */ 43577b14db5SEric W. Biederman res = count; 4368d65af78SAlexey Dobriyan error = table->proc_handler(table, write, buf, &res, ppos); 43777b14db5SEric W. Biederman if (!error) 43877b14db5SEric W. Biederman error = res; 43977b14db5SEric W. Biederman out: 44077b14db5SEric W. Biederman sysctl_head_finish(head); 44177b14db5SEric W. Biederman 44277b14db5SEric W. Biederman return error; 44377b14db5SEric W. Biederman } 44477b14db5SEric W. Biederman 4457708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf, 4467708bfb1SPavel Emelyanov size_t count, loff_t *ppos) 4477708bfb1SPavel Emelyanov { 4487708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0); 4497708bfb1SPavel Emelyanov } 4507708bfb1SPavel Emelyanov 45177b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf, 45277b14db5SEric W. Biederman size_t count, loff_t *ppos) 45377b14db5SEric W. Biederman { 4547708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1); 45577b14db5SEric W. Biederman } 45677b14db5SEric W. Biederman 457f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp) 458f1ecf068SLucas De Marchi { 459f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 460f1ecf068SLucas De Marchi 461f1ecf068SLucas De Marchi if (table->poll) 462f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 463f1ecf068SLucas De Marchi 464f1ecf068SLucas De Marchi return 0; 465f1ecf068SLucas De Marchi } 466f1ecf068SLucas De Marchi 467f1ecf068SLucas De Marchi static unsigned int proc_sys_poll(struct file *filp, poll_table *wait) 468f1ecf068SLucas De Marchi { 469f1ecf068SLucas De Marchi struct inode *inode = filp->f_path.dentry->d_inode; 470f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 471f1ecf068SLucas De Marchi unsigned long event = (unsigned long)filp->private_data; 472f1ecf068SLucas De Marchi unsigned int ret = DEFAULT_POLLMASK; 473f1ecf068SLucas De Marchi 474f1ecf068SLucas De Marchi if (!table->proc_handler) 475f1ecf068SLucas De Marchi goto out; 476f1ecf068SLucas De Marchi 477f1ecf068SLucas De Marchi if (!table->poll) 478f1ecf068SLucas De Marchi goto out; 479f1ecf068SLucas De Marchi 480f1ecf068SLucas De Marchi poll_wait(filp, &table->poll->wait, wait); 481f1ecf068SLucas De Marchi 482f1ecf068SLucas De Marchi if (event != atomic_read(&table->poll->event)) { 483f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 484f1ecf068SLucas De Marchi ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI; 485f1ecf068SLucas De Marchi } 486f1ecf068SLucas De Marchi 487f1ecf068SLucas De Marchi out: 488f1ecf068SLucas De Marchi return ret; 489f1ecf068SLucas De Marchi } 49077b14db5SEric W. Biederman 49177b14db5SEric W. Biederman static int proc_sys_fill_cache(struct file *filp, void *dirent, 4929043476fSAl Viro filldir_t filldir, 4939043476fSAl Viro struct ctl_table_header *head, 4949043476fSAl Viro struct ctl_table *table) 49577b14db5SEric W. Biederman { 49677b14db5SEric W. Biederman struct dentry *child, *dir = filp->f_path.dentry; 49777b14db5SEric W. Biederman struct inode *inode; 49877b14db5SEric W. Biederman struct qstr qname; 49977b14db5SEric W. Biederman ino_t ino = 0; 50077b14db5SEric W. Biederman unsigned type = DT_UNKNOWN; 50177b14db5SEric W. Biederman 50277b14db5SEric W. Biederman qname.name = table->procname; 50377b14db5SEric W. Biederman qname.len = strlen(table->procname); 50477b14db5SEric W. Biederman qname.hash = full_name_hash(qname.name, qname.len); 50577b14db5SEric W. Biederman 50677b14db5SEric W. Biederman child = d_lookup(dir, &qname); 50777b14db5SEric W. Biederman if (!child) { 5089043476fSAl Viro child = d_alloc(dir, &qname); 5099043476fSAl Viro if (child) { 5109043476fSAl Viro inode = proc_sys_make_inode(dir->d_sb, head, table); 5119043476fSAl Viro if (!inode) { 5129043476fSAl Viro dput(child); 5139043476fSAl Viro return -ENOMEM; 5149043476fSAl Viro } else { 515fb045adbSNick Piggin d_set_d_op(child, &proc_sys_dentry_operations); 5169043476fSAl Viro d_add(child, inode); 51777b14db5SEric W. Biederman } 5189043476fSAl Viro } else { 5199043476fSAl Viro return -ENOMEM; 52077b14db5SEric W. Biederman } 52177b14db5SEric W. Biederman } 52277b14db5SEric W. Biederman inode = child->d_inode; 52377b14db5SEric W. Biederman ino = inode->i_ino; 52477b14db5SEric W. Biederman type = inode->i_mode >> 12; 52577b14db5SEric W. Biederman dput(child); 5269043476fSAl Viro return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type); 5279043476fSAl Viro } 5289043476fSAl Viro 5290e47c99dSEric W. Biederman static int proc_sys_link_fill_cache(struct file *filp, void *dirent, 5300e47c99dSEric W. Biederman filldir_t filldir, 5310e47c99dSEric W. Biederman struct ctl_table_header *head, 5320e47c99dSEric W. Biederman struct ctl_table *table) 5330e47c99dSEric W. Biederman { 5340e47c99dSEric W. Biederman int err, ret = 0; 5350e47c99dSEric W. Biederman head = sysctl_head_grab(head); 5360e47c99dSEric W. Biederman 5370e47c99dSEric W. Biederman /* It is not an error if we can not follow the link ignore it */ 5380e47c99dSEric W. Biederman err = sysctl_follow_link(&head, &table, current->nsproxy); 5390e47c99dSEric W. Biederman if (err) 5400e47c99dSEric W. Biederman goto out; 5410e47c99dSEric W. Biederman 5420e47c99dSEric W. Biederman ret = proc_sys_fill_cache(filp, dirent, filldir, head, table); 5430e47c99dSEric W. Biederman out: 5440e47c99dSEric W. Biederman sysctl_head_finish(head); 5450e47c99dSEric W. Biederman return ret; 5460e47c99dSEric W. Biederman } 5470e47c99dSEric W. Biederman 5489043476fSAl Viro static int scan(struct ctl_table_header *head, ctl_table *table, 5499043476fSAl Viro unsigned long *pos, struct file *file, 5509043476fSAl Viro void *dirent, filldir_t filldir) 5519043476fSAl Viro { 5529043476fSAl Viro int res; 5539043476fSAl Viro 5546a75ce16SEric W. Biederman if ((*pos)++ < file->f_pos) 5556a75ce16SEric W. Biederman return 0; 5569043476fSAl Viro 5570e47c99dSEric W. Biederman if (unlikely(S_ISLNK(table->mode))) 5580e47c99dSEric W. Biederman res = proc_sys_link_fill_cache(file, dirent, filldir, head, table); 5590e47c99dSEric W. Biederman else 5609043476fSAl Viro res = proc_sys_fill_cache(file, dirent, filldir, head, table); 5619043476fSAl Viro 5626a75ce16SEric W. Biederman if (res == 0) 5636a75ce16SEric W. Biederman file->f_pos = *pos; 5646a75ce16SEric W. Biederman 5656a75ce16SEric W. Biederman return res; 56677b14db5SEric W. Biederman } 56777b14db5SEric W. Biederman 56877b14db5SEric W. Biederman static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir) 56977b14db5SEric W. Biederman { 5709043476fSAl Viro struct dentry *dentry = filp->f_path.dentry; 57177b14db5SEric W. Biederman struct inode *inode = dentry->d_inode; 5729043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 5739043476fSAl Viro struct ctl_table_header *h = NULL; 5746a75ce16SEric W. Biederman struct ctl_table *entry; 5757ec66d06SEric W. Biederman struct ctl_dir *ctl_dir; 57677b14db5SEric W. Biederman unsigned long pos; 5779043476fSAl Viro int ret = -EINVAL; 57877b14db5SEric W. Biederman 5799043476fSAl Viro if (IS_ERR(head)) 5809043476fSAl Viro return PTR_ERR(head); 5819043476fSAl Viro 5827ec66d06SEric W. Biederman ctl_dir = container_of(head, struct ctl_dir, header); 58377b14db5SEric W. Biederman 58477b14db5SEric W. Biederman ret = 0; 58577b14db5SEric W. Biederman /* Avoid a switch here: arm builds fail with missing __cmpdi2 */ 58677b14db5SEric W. Biederman if (filp->f_pos == 0) { 58777b14db5SEric W. Biederman if (filldir(dirent, ".", 1, filp->f_pos, 58877b14db5SEric W. Biederman inode->i_ino, DT_DIR) < 0) 58977b14db5SEric W. Biederman goto out; 59077b14db5SEric W. Biederman filp->f_pos++; 59177b14db5SEric W. Biederman } 59277b14db5SEric W. Biederman if (filp->f_pos == 1) { 59377b14db5SEric W. Biederman if (filldir(dirent, "..", 2, filp->f_pos, 59477b14db5SEric W. Biederman parent_ino(dentry), DT_DIR) < 0) 59577b14db5SEric W. Biederman goto out; 59677b14db5SEric W. Biederman filp->f_pos++; 59777b14db5SEric W. Biederman } 59877b14db5SEric W. Biederman pos = 2; 59977b14db5SEric W. Biederman 6007ec66d06SEric W. Biederman for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) { 6016a75ce16SEric W. Biederman ret = scan(h, entry, &pos, filp, dirent, filldir); 6029043476fSAl Viro if (ret) { 6039043476fSAl Viro sysctl_head_finish(h); 6049043476fSAl Viro break; 60577b14db5SEric W. Biederman } 60677b14db5SEric W. Biederman } 60777b14db5SEric W. Biederman ret = 1; 60877b14db5SEric W. Biederman out: 60977b14db5SEric W. Biederman sysctl_head_finish(head); 61077b14db5SEric W. Biederman return ret; 61177b14db5SEric W. Biederman } 61277b14db5SEric W. Biederman 61310556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask) 61477b14db5SEric W. Biederman { 61577b14db5SEric W. Biederman /* 61677b14db5SEric W. Biederman * sysctl entries that are not writeable, 61777b14db5SEric W. Biederman * are _NOT_ writeable, capabilities or not. 61877b14db5SEric W. Biederman */ 619f696a365SMiklos Szeredi struct ctl_table_header *head; 620f696a365SMiklos Szeredi struct ctl_table *table; 62177b14db5SEric W. Biederman int error; 62277b14db5SEric W. Biederman 623f696a365SMiklos Szeredi /* Executable files are not allowed under /proc/sys/ */ 624f696a365SMiklos Szeredi if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) 625f696a365SMiklos Szeredi return -EACCES; 626f696a365SMiklos Szeredi 627f696a365SMiklos Szeredi head = grab_header(inode); 6289043476fSAl Viro if (IS_ERR(head)) 6299043476fSAl Viro return PTR_ERR(head); 63077b14db5SEric W. Biederman 631f696a365SMiklos Szeredi table = PROC_I(inode)->sysctl_entry; 6329043476fSAl Viro if (!table) /* global root - r-xr-xr-x */ 6339043476fSAl Viro error = mask & MAY_WRITE ? -EACCES : 0; 6349043476fSAl Viro else /* Use the permissions on the sysctl table entry */ 6351fc0f78cSAl Viro error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK); 6369043476fSAl Viro 63777b14db5SEric W. Biederman sysctl_head_finish(head); 63877b14db5SEric W. Biederman return error; 63977b14db5SEric W. Biederman } 64077b14db5SEric W. Biederman 64177b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr) 64277b14db5SEric W. Biederman { 64377b14db5SEric W. Biederman struct inode *inode = dentry->d_inode; 64477b14db5SEric W. Biederman int error; 64577b14db5SEric W. Biederman 64677b14db5SEric W. Biederman if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) 64777b14db5SEric W. Biederman return -EPERM; 64877b14db5SEric W. Biederman 64977b14db5SEric W. Biederman error = inode_change_ok(inode, attr); 6501025774cSChristoph Hellwig if (error) 65177b14db5SEric W. Biederman return error; 6521025774cSChristoph Hellwig 6531025774cSChristoph Hellwig if ((attr->ia_valid & ATTR_SIZE) && 6541025774cSChristoph Hellwig attr->ia_size != i_size_read(inode)) { 6551025774cSChristoph Hellwig error = vmtruncate(inode, attr->ia_size); 6561025774cSChristoph Hellwig if (error) 6571025774cSChristoph Hellwig return error; 6581025774cSChristoph Hellwig } 6591025774cSChristoph Hellwig 6601025774cSChristoph Hellwig setattr_copy(inode, attr); 6611025774cSChristoph Hellwig mark_inode_dirty(inode); 6621025774cSChristoph Hellwig return 0; 66377b14db5SEric W. Biederman } 66477b14db5SEric W. Biederman 6659043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 6669043476fSAl Viro { 6679043476fSAl Viro struct inode *inode = dentry->d_inode; 6689043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 6699043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 6709043476fSAl Viro 6719043476fSAl Viro if (IS_ERR(head)) 6729043476fSAl Viro return PTR_ERR(head); 6739043476fSAl Viro 6749043476fSAl Viro generic_fillattr(inode, stat); 6759043476fSAl Viro if (table) 6769043476fSAl Viro stat->mode = (stat->mode & S_IFMT) | table->mode; 6779043476fSAl Viro 6789043476fSAl Viro sysctl_head_finish(head); 6799043476fSAl Viro return 0; 6809043476fSAl Viro } 6819043476fSAl Viro 68277b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = { 683f1ecf068SLucas De Marchi .open = proc_sys_open, 684f1ecf068SLucas De Marchi .poll = proc_sys_poll, 68577b14db5SEric W. Biederman .read = proc_sys_read, 68677b14db5SEric W. Biederman .write = proc_sys_write, 6876038f373SArnd Bergmann .llseek = default_llseek, 6889043476fSAl Viro }; 6899043476fSAl Viro 6909043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = { 691887df078SPavel Emelyanov .read = generic_read_dir, 69277b14db5SEric W. Biederman .readdir = proc_sys_readdir, 6933222a3e5SChristoph Hellwig .llseek = generic_file_llseek, 69477b14db5SEric W. Biederman }; 69577b14db5SEric W. Biederman 69603a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = { 6979043476fSAl Viro .permission = proc_sys_permission, 6989043476fSAl Viro .setattr = proc_sys_setattr, 6999043476fSAl Viro .getattr = proc_sys_getattr, 7009043476fSAl Viro }; 7019043476fSAl Viro 7029043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = { 70377b14db5SEric W. Biederman .lookup = proc_sys_lookup, 70477b14db5SEric W. Biederman .permission = proc_sys_permission, 70577b14db5SEric W. Biederman .setattr = proc_sys_setattr, 7069043476fSAl Viro .getattr = proc_sys_getattr, 70777b14db5SEric W. Biederman }; 70877b14db5SEric W. Biederman 70977b14db5SEric W. Biederman static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd) 71077b14db5SEric W. Biederman { 71134286d66SNick Piggin if (nd->flags & LOOKUP_RCU) 71234286d66SNick Piggin return -ECHILD; 7139043476fSAl Viro return !PROC_I(dentry->d_inode)->sysctl->unregistering; 7149043476fSAl Viro } 7159043476fSAl Viro 716fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry) 7179043476fSAl Viro { 7189043476fSAl Viro return !!PROC_I(dentry->d_inode)->sysctl->unregistering; 7199043476fSAl Viro } 7209043476fSAl Viro 7211f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p) 7221f87f0b5SEric W. Biederman { 7231f87f0b5SEric W. Biederman struct ctl_table_set *set = p->set; 7241f87f0b5SEric W. Biederman int res; 7251f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 7261f87f0b5SEric W. Biederman if (p->unregistering) 7271f87f0b5SEric W. Biederman res = 0; 7281f87f0b5SEric W. Biederman else if (!set->is_seen) 7291f87f0b5SEric W. Biederman res = 1; 7301f87f0b5SEric W. Biederman else 7311f87f0b5SEric W. Biederman res = set->is_seen(set); 7321f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 7331f87f0b5SEric W. Biederman return res; 7341f87f0b5SEric W. Biederman } 7351f87f0b5SEric W. Biederman 736621e155aSNick Piggin static int proc_sys_compare(const struct dentry *parent, 737621e155aSNick Piggin const struct inode *pinode, 738621e155aSNick Piggin const struct dentry *dentry, const struct inode *inode, 739621e155aSNick Piggin unsigned int len, const char *str, const struct qstr *name) 7409043476fSAl Viro { 741dfef6dcdSAl Viro struct ctl_table_header *head; 74231e6b01fSNick Piggin /* Although proc doesn't have negative dentries, rcu-walk means 74331e6b01fSNick Piggin * that inode here can be NULL */ 744dfef6dcdSAl Viro /* AV: can it, indeed? */ 74531e6b01fSNick Piggin if (!inode) 746dfef6dcdSAl Viro return 1; 747621e155aSNick Piggin if (name->len != len) 7489043476fSAl Viro return 1; 749621e155aSNick Piggin if (memcmp(name->name, str, len)) 7509043476fSAl Viro return 1; 751dfef6dcdSAl Viro head = rcu_dereference(PROC_I(inode)->sysctl); 752dfef6dcdSAl Viro return !head || !sysctl_is_seen(head); 75377b14db5SEric W. Biederman } 75477b14db5SEric W. Biederman 755d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = { 75677b14db5SEric W. Biederman .d_revalidate = proc_sys_revalidate, 7579043476fSAl Viro .d_delete = proc_sys_delete, 7589043476fSAl Viro .d_compare = proc_sys_compare, 75977b14db5SEric W. Biederman }; 76077b14db5SEric W. Biederman 7610e47c99dSEric W. Biederman static struct ctl_dir *find_subdir(struct ctl_dir *dir, 7627ec66d06SEric W. Biederman const char *name, int namelen) 7631f87f0b5SEric W. Biederman { 7647ec66d06SEric W. Biederman struct ctl_table_header *head; 7657ec66d06SEric W. Biederman struct ctl_table *entry; 7661f87f0b5SEric W. Biederman 7670e47c99dSEric W. Biederman entry = find_entry(&head, dir, name, namelen); 7687ec66d06SEric W. Biederman if (!entry) 7697ec66d06SEric W. Biederman return ERR_PTR(-ENOENT); 7707ec66d06SEric W. Biederman if (S_ISDIR(entry->mode)) 7717ec66d06SEric W. Biederman return container_of(head, struct ctl_dir, header); 7727ec66d06SEric W. Biederman return ERR_PTR(-ENOTDIR); 7731f87f0b5SEric W. Biederman } 7741f87f0b5SEric W. Biederman 7757ec66d06SEric W. Biederman static struct ctl_dir *new_dir(struct ctl_table_set *set, 7767ec66d06SEric W. Biederman const char *name, int namelen) 7771f87f0b5SEric W. Biederman { 7787ec66d06SEric W. Biederman struct ctl_table *table; 7797ec66d06SEric W. Biederman struct ctl_dir *new; 7807ec66d06SEric W. Biederman char *new_name; 7811f87f0b5SEric W. Biederman 7827ec66d06SEric W. Biederman new = kzalloc(sizeof(*new) + sizeof(struct ctl_table)*2 + 7837ec66d06SEric W. Biederman namelen + 1, GFP_KERNEL); 7847ec66d06SEric W. Biederman if (!new) 7857ec66d06SEric W. Biederman return NULL; 7867ec66d06SEric W. Biederman 7877ec66d06SEric W. Biederman table = (struct ctl_table *)(new + 1); 7887ec66d06SEric W. Biederman new_name = (char *)(table + 2); 7897ec66d06SEric W. Biederman memcpy(new_name, name, namelen); 7907ec66d06SEric W. Biederman new_name[namelen] = '\0'; 7917ec66d06SEric W. Biederman table[0].procname = new_name; 7927ec66d06SEric W. Biederman table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO; 7930e47c99dSEric W. Biederman init_header(&new->header, set->dir.header.root, set, table); 7947ec66d06SEric W. Biederman 7957ec66d06SEric W. Biederman return new; 7961f87f0b5SEric W. Biederman } 7971f87f0b5SEric W. Biederman 7980e47c99dSEric W. Biederman static struct ctl_dir *get_subdir(struct ctl_dir *dir, 7990e47c99dSEric W. Biederman const char *name, int namelen) 8007ec66d06SEric W. Biederman { 8010e47c99dSEric W. Biederman struct ctl_table_set *set = dir->header.set; 8027ec66d06SEric W. Biederman struct ctl_dir *subdir, *new = NULL; 8037ec66d06SEric W. Biederman 8047ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 8050e47c99dSEric W. Biederman subdir = find_subdir(dir, name, namelen); 8067ec66d06SEric W. Biederman if (!IS_ERR(subdir)) 8077ec66d06SEric W. Biederman goto found; 8087ec66d06SEric W. Biederman if (PTR_ERR(subdir) != -ENOENT) 8097ec66d06SEric W. Biederman goto failed; 8107ec66d06SEric W. Biederman 8117ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 8127ec66d06SEric W. Biederman new = new_dir(set, name, namelen); 8137ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 8147ec66d06SEric W. Biederman subdir = ERR_PTR(-ENOMEM); 8157ec66d06SEric W. Biederman if (!new) 8167ec66d06SEric W. Biederman goto failed; 8177ec66d06SEric W. Biederman 8180e47c99dSEric W. Biederman subdir = find_subdir(dir, name, namelen); 8197ec66d06SEric W. Biederman if (!IS_ERR(subdir)) 8207ec66d06SEric W. Biederman goto found; 8217ec66d06SEric W. Biederman if (PTR_ERR(subdir) != -ENOENT) 8227ec66d06SEric W. Biederman goto failed; 8237ec66d06SEric W. Biederman 8240e47c99dSEric W. Biederman if (insert_header(dir, &new->header)) 8250e47c99dSEric W. Biederman goto failed; 8267ec66d06SEric W. Biederman subdir = new; 8277ec66d06SEric W. Biederman found: 8287ec66d06SEric W. Biederman subdir->header.nreg++; 8297ec66d06SEric W. Biederman failed: 8307ec66d06SEric W. Biederman if (unlikely(IS_ERR(subdir))) { 8316980128fSEric W. Biederman printk(KERN_ERR "sysctl could not get directory: "); 8326980128fSEric W. Biederman sysctl_print_dir(dir); 8336980128fSEric W. Biederman printk(KERN_CONT "/%*.*s %ld\n", 8347ec66d06SEric W. Biederman namelen, namelen, name, PTR_ERR(subdir)); 8351f87f0b5SEric W. Biederman } 8367ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 8377ec66d06SEric W. Biederman if (new) 8387ec66d06SEric W. Biederman drop_sysctl_table(&new->header); 8397ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 8407ec66d06SEric W. Biederman return subdir; 8411f87f0b5SEric W. Biederman } 8421f87f0b5SEric W. Biederman 8430e47c99dSEric W. Biederman static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir) 8440e47c99dSEric W. Biederman { 8450e47c99dSEric W. Biederman struct ctl_dir *parent; 8460e47c99dSEric W. Biederman const char *procname; 8470e47c99dSEric W. Biederman if (!dir->header.parent) 8480e47c99dSEric W. Biederman return &set->dir; 8490e47c99dSEric W. Biederman parent = xlate_dir(set, dir->header.parent); 8500e47c99dSEric W. Biederman if (IS_ERR(parent)) 8510e47c99dSEric W. Biederman return parent; 8520e47c99dSEric W. Biederman procname = dir->header.ctl_table[0].procname; 8530e47c99dSEric W. Biederman return find_subdir(parent, procname, strlen(procname)); 8540e47c99dSEric W. Biederman } 8550e47c99dSEric W. Biederman 8560e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead, 8570e47c99dSEric W. Biederman struct ctl_table **pentry, struct nsproxy *namespaces) 8580e47c99dSEric W. Biederman { 8590e47c99dSEric W. Biederman struct ctl_table_header *head; 8600e47c99dSEric W. Biederman struct ctl_table_root *root; 8610e47c99dSEric W. Biederman struct ctl_table_set *set; 8620e47c99dSEric W. Biederman struct ctl_table *entry; 8630e47c99dSEric W. Biederman struct ctl_dir *dir; 8640e47c99dSEric W. Biederman int ret; 8650e47c99dSEric W. Biederman 8660e47c99dSEric W. Biederman /* Get out quickly if not a link */ 8670e47c99dSEric W. Biederman if (!S_ISLNK((*pentry)->mode)) 8680e47c99dSEric W. Biederman return 0; 8690e47c99dSEric W. Biederman 8700e47c99dSEric W. Biederman ret = 0; 8710e47c99dSEric W. Biederman spin_lock(&sysctl_lock); 8720e47c99dSEric W. Biederman root = (*pentry)->data; 8730e47c99dSEric W. Biederman set = lookup_header_set(root, namespaces); 8740e47c99dSEric W. Biederman dir = xlate_dir(set, (*phead)->parent); 8750e47c99dSEric W. Biederman if (IS_ERR(dir)) 8760e47c99dSEric W. Biederman ret = PTR_ERR(dir); 8770e47c99dSEric W. Biederman else { 8780e47c99dSEric W. Biederman const char *procname = (*pentry)->procname; 8790e47c99dSEric W. Biederman head = NULL; 8800e47c99dSEric W. Biederman entry = find_entry(&head, dir, procname, strlen(procname)); 8810e47c99dSEric W. Biederman ret = -ENOENT; 8820e47c99dSEric W. Biederman if (entry && use_table(head)) { 8830e47c99dSEric W. Biederman unuse_table(*phead); 8840e47c99dSEric W. Biederman *phead = head; 8850e47c99dSEric W. Biederman *pentry = entry; 8860e47c99dSEric W. Biederman ret = 0; 8870e47c99dSEric W. Biederman } 8880e47c99dSEric W. Biederman } 8890e47c99dSEric W. Biederman 8900e47c99dSEric W. Biederman spin_unlock(&sysctl_lock); 8910e47c99dSEric W. Biederman return ret; 8920e47c99dSEric W. Biederman } 8930e47c99dSEric W. Biederman 8947c60c48fSEric W. Biederman static int sysctl_check_table_dups(const char *path, struct ctl_table *old, 8951f87f0b5SEric W. Biederman struct ctl_table *table) 8961f87f0b5SEric W. Biederman { 8977c60c48fSEric W. Biederman struct ctl_table *entry, *test; 8981f87f0b5SEric W. Biederman int error = 0; 8991f87f0b5SEric W. Biederman 9007c60c48fSEric W. Biederman for (entry = old; entry->procname; entry++) { 9017c60c48fSEric W. Biederman for (test = table; test->procname; test++) { 9027c60c48fSEric W. Biederman if (strcmp(entry->procname, test->procname) == 0) { 9037c60c48fSEric W. Biederman printk(KERN_ERR "sysctl duplicate entry: %s/%s\n", 9047c60c48fSEric W. Biederman path, test->procname); 9057c60c48fSEric W. Biederman error = -EEXIST; 9061f87f0b5SEric W. Biederman } 9077c60c48fSEric W. Biederman } 9087c60c48fSEric W. Biederman } 9097c60c48fSEric W. Biederman return error; 9107c60c48fSEric W. Biederman } 9117c60c48fSEric W. Biederman 9120e47c99dSEric W. Biederman static int sysctl_check_dups(struct ctl_dir *dir, 9137c60c48fSEric W. Biederman const char *path, struct ctl_table *table) 9147c60c48fSEric W. Biederman { 9157c60c48fSEric W. Biederman struct ctl_table_set *set; 9167ec66d06SEric W. Biederman struct ctl_table_header *head; 9177c60c48fSEric W. Biederman int error = 0; 9187c60c48fSEric W. Biederman 9190e47c99dSEric W. Biederman set = dir->header.set; 9207c60c48fSEric W. Biederman list_for_each_entry(head, &set->list, ctl_entry) { 9217c60c48fSEric W. Biederman if (head->unregistering) 9227c60c48fSEric W. Biederman continue; 9237ec66d06SEric W. Biederman if (head->parent != dir) 9247c60c48fSEric W. Biederman continue; 9250e47c99dSEric W. Biederman error = sysctl_check_table_dups(path, head->ctl_table, table); 9267c60c48fSEric W. Biederman } 9277c60c48fSEric W. Biederman return error; 9287c60c48fSEric W. Biederman } 9297c60c48fSEric W. Biederman 9307c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...) 9317c60c48fSEric W. Biederman { 9327c60c48fSEric W. Biederman struct va_format vaf; 9337c60c48fSEric W. Biederman va_list args; 9347c60c48fSEric W. Biederman 9357c60c48fSEric W. Biederman va_start(args, fmt); 9367c60c48fSEric W. Biederman vaf.fmt = fmt; 9377c60c48fSEric W. Biederman vaf.va = &args; 9387c60c48fSEric W. Biederman 9397c60c48fSEric W. Biederman printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n", 9407c60c48fSEric W. Biederman path, table->procname, &vaf); 9417c60c48fSEric W. Biederman 9427c60c48fSEric W. Biederman va_end(args); 9437c60c48fSEric W. Biederman return -EINVAL; 9447c60c48fSEric W. Biederman } 9457c60c48fSEric W. Biederman 9467c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table) 9477c60c48fSEric W. Biederman { 9487c60c48fSEric W. Biederman int err = 0; 9497c60c48fSEric W. Biederman for (; table->procname; table++) { 9507c60c48fSEric W. Biederman if (table->child) 9517c60c48fSEric W. Biederman err = sysctl_err(path, table, "Not a file"); 9527c60c48fSEric W. Biederman 9531f87f0b5SEric W. Biederman if ((table->proc_handler == proc_dostring) || 9541f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec) || 9551f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_minmax) || 9561f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_jiffies) || 9571f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_userhz_jiffies) || 9581f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_ms_jiffies) || 9591f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_minmax) || 9601f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) { 9611f87f0b5SEric W. Biederman if (!table->data) 9627c60c48fSEric W. Biederman err = sysctl_err(path, table, "No data"); 9631f87f0b5SEric W. Biederman if (!table->maxlen) 9647c60c48fSEric W. Biederman err = sysctl_err(path, table, "No maxlen"); 9651f87f0b5SEric W. Biederman } 9661f87f0b5SEric W. Biederman if (!table->proc_handler) 9677c60c48fSEric W. Biederman err = sysctl_err(path, table, "No proc_handler"); 9687c60c48fSEric W. Biederman 9697c60c48fSEric W. Biederman if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode) 9707c60c48fSEric W. Biederman err = sysctl_err(path, table, "bogus .mode 0%o", 9717c60c48fSEric W. Biederman table->mode); 9721f87f0b5SEric W. Biederman } 9737c60c48fSEric W. Biederman return err; 9741f87f0b5SEric W. Biederman } 9751f87f0b5SEric W. Biederman 9760e47c99dSEric W. Biederman static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table, 9770e47c99dSEric W. Biederman struct ctl_table_root *link_root) 9780e47c99dSEric W. Biederman { 9790e47c99dSEric W. Biederman struct ctl_table *link_table, *entry, *link; 9800e47c99dSEric W. Biederman struct ctl_table_header *links; 9810e47c99dSEric W. Biederman char *link_name; 9820e47c99dSEric W. Biederman int nr_entries, name_bytes; 9830e47c99dSEric W. Biederman 9840e47c99dSEric W. Biederman name_bytes = 0; 9850e47c99dSEric W. Biederman nr_entries = 0; 9860e47c99dSEric W. Biederman for (entry = table; entry->procname; entry++) { 9870e47c99dSEric W. Biederman nr_entries++; 9880e47c99dSEric W. Biederman name_bytes += strlen(entry->procname) + 1; 9890e47c99dSEric W. Biederman } 9900e47c99dSEric W. Biederman 9910e47c99dSEric W. Biederman links = kzalloc(sizeof(struct ctl_table_header) + 9920e47c99dSEric W. Biederman sizeof(struct ctl_table)*(nr_entries + 1) + 9930e47c99dSEric W. Biederman name_bytes, 9940e47c99dSEric W. Biederman GFP_KERNEL); 9950e47c99dSEric W. Biederman 9960e47c99dSEric W. Biederman if (!links) 9970e47c99dSEric W. Biederman return NULL; 9980e47c99dSEric W. Biederman 9990e47c99dSEric W. Biederman link_table = (struct ctl_table *)(links + 1); 10000e47c99dSEric W. Biederman link_name = (char *)&link_table[nr_entries + 1]; 10010e47c99dSEric W. Biederman 10020e47c99dSEric W. Biederman for (link = link_table, entry = table; entry->procname; link++, entry++) { 10030e47c99dSEric W. Biederman int len = strlen(entry->procname) + 1; 10040e47c99dSEric W. Biederman memcpy(link_name, entry->procname, len); 10050e47c99dSEric W. Biederman link->procname = link_name; 10060e47c99dSEric W. Biederman link->mode = S_IFLNK|S_IRWXUGO; 10070e47c99dSEric W. Biederman link->data = link_root; 10080e47c99dSEric W. Biederman link_name += len; 10090e47c99dSEric W. Biederman } 10100e47c99dSEric W. Biederman init_header(links, dir->header.root, dir->header.set, link_table); 10110e47c99dSEric W. Biederman links->nreg = nr_entries; 10120e47c99dSEric W. Biederman 10130e47c99dSEric W. Biederman return links; 10140e47c99dSEric W. Biederman } 10150e47c99dSEric W. Biederman 10160e47c99dSEric W. Biederman static bool get_links(struct ctl_dir *dir, 10170e47c99dSEric W. Biederman struct ctl_table *table, struct ctl_table_root *link_root) 10180e47c99dSEric W. Biederman { 10190e47c99dSEric W. Biederman struct ctl_table_header *head; 10200e47c99dSEric W. Biederman struct ctl_table *entry, *link; 10210e47c99dSEric W. Biederman 10220e47c99dSEric W. Biederman /* Are there links available for every entry in table? */ 10230e47c99dSEric W. Biederman for (entry = table; entry->procname; entry++) { 10240e47c99dSEric W. Biederman const char *procname = entry->procname; 10250e47c99dSEric W. Biederman link = find_entry(&head, dir, procname, strlen(procname)); 10260e47c99dSEric W. Biederman if (!link) 10270e47c99dSEric W. Biederman return false; 10280e47c99dSEric W. Biederman if (S_ISDIR(link->mode) && S_ISDIR(entry->mode)) 10290e47c99dSEric W. Biederman continue; 10300e47c99dSEric W. Biederman if (S_ISLNK(link->mode) && (link->data == link_root)) 10310e47c99dSEric W. Biederman continue; 10320e47c99dSEric W. Biederman return false; 10330e47c99dSEric W. Biederman } 10340e47c99dSEric W. Biederman 10350e47c99dSEric W. Biederman /* The checks passed. Increase the registration count on the links */ 10360e47c99dSEric W. Biederman for (entry = table; entry->procname; entry++) { 10370e47c99dSEric W. Biederman const char *procname = entry->procname; 10380e47c99dSEric W. Biederman link = find_entry(&head, dir, procname, strlen(procname)); 10390e47c99dSEric W. Biederman head->nreg++; 10400e47c99dSEric W. Biederman } 10410e47c99dSEric W. Biederman return true; 10420e47c99dSEric W. Biederman } 10430e47c99dSEric W. Biederman 10440e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head) 10450e47c99dSEric W. Biederman { 10460e47c99dSEric W. Biederman struct ctl_table_set *root_set = &sysctl_table_root.default_set; 10470e47c99dSEric W. Biederman struct ctl_dir *core_parent = NULL; 10480e47c99dSEric W. Biederman struct ctl_table_header *links; 10490e47c99dSEric W. Biederman int err; 10500e47c99dSEric W. Biederman 10510e47c99dSEric W. Biederman if (head->set == root_set) 10520e47c99dSEric W. Biederman return 0; 10530e47c99dSEric W. Biederman 10540e47c99dSEric W. Biederman core_parent = xlate_dir(root_set, head->parent); 10550e47c99dSEric W. Biederman if (IS_ERR(core_parent)) 10560e47c99dSEric W. Biederman return 0; 10570e47c99dSEric W. Biederman 10580e47c99dSEric W. Biederman if (get_links(core_parent, head->ctl_table, head->root)) 10590e47c99dSEric W. Biederman return 0; 10600e47c99dSEric W. Biederman 10610e47c99dSEric W. Biederman core_parent->header.nreg++; 10620e47c99dSEric W. Biederman spin_unlock(&sysctl_lock); 10630e47c99dSEric W. Biederman 10640e47c99dSEric W. Biederman links = new_links(core_parent, head->ctl_table, head->root); 10650e47c99dSEric W. Biederman 10660e47c99dSEric W. Biederman spin_lock(&sysctl_lock); 10670e47c99dSEric W. Biederman err = -ENOMEM; 10680e47c99dSEric W. Biederman if (!links) 10690e47c99dSEric W. Biederman goto out; 10700e47c99dSEric W. Biederman 10710e47c99dSEric W. Biederman err = 0; 10720e47c99dSEric W. Biederman if (get_links(core_parent, head->ctl_table, head->root)) { 10730e47c99dSEric W. Biederman kfree(links); 10740e47c99dSEric W. Biederman goto out; 10750e47c99dSEric W. Biederman } 10760e47c99dSEric W. Biederman 10770e47c99dSEric W. Biederman err = insert_header(core_parent, links); 10780e47c99dSEric W. Biederman if (err) 10790e47c99dSEric W. Biederman kfree(links); 10800e47c99dSEric W. Biederman out: 10810e47c99dSEric W. Biederman drop_sysctl_table(&core_parent->header); 10820e47c99dSEric W. Biederman return err; 10830e47c99dSEric W. Biederman } 10840e47c99dSEric W. Biederman 10851f87f0b5SEric W. Biederman /** 1086f728019bSEric W. Biederman * __register_sysctl_table - register a leaf sysctl table 10871f87f0b5SEric W. Biederman * @root: List of sysctl headers to register on 10881f87f0b5SEric W. Biederman * @namespaces: Data to compute which lists of sysctl entries are visible 10891f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 10901f87f0b5SEric W. Biederman * @table: the top-level table structure 10911f87f0b5SEric W. Biederman * 10921f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 10931f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 10941f87f0b5SEric W. Biederman * 10951f87f0b5SEric W. Biederman * The members of the &struct ctl_table structure are used as follows: 10961f87f0b5SEric W. Biederman * 10971f87f0b5SEric W. Biederman * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not 10981f87f0b5SEric W. Biederman * enter a sysctl file 10991f87f0b5SEric W. Biederman * 11001f87f0b5SEric W. Biederman * data - a pointer to data for use by proc_handler 11011f87f0b5SEric W. Biederman * 11021f87f0b5SEric W. Biederman * maxlen - the maximum size in bytes of the data 11031f87f0b5SEric W. Biederman * 1104f728019bSEric W. Biederman * mode - the file permissions for the /proc/sys file 11051f87f0b5SEric W. Biederman * 1106f728019bSEric W. Biederman * child - must be %NULL. 11071f87f0b5SEric W. Biederman * 11081f87f0b5SEric W. Biederman * proc_handler - the text handler routine (described below) 11091f87f0b5SEric W. Biederman * 11101f87f0b5SEric W. Biederman * extra1, extra2 - extra pointers usable by the proc handler routines 11111f87f0b5SEric W. Biederman * 11121f87f0b5SEric W. Biederman * Leaf nodes in the sysctl tree will be represented by a single file 11131f87f0b5SEric W. Biederman * under /proc; non-leaf nodes will be represented by directories. 11141f87f0b5SEric W. Biederman * 1115f728019bSEric W. Biederman * There must be a proc_handler routine for any terminal nodes. 1116f728019bSEric W. Biederman * Several default handlers are available to cover common cases - 11171f87f0b5SEric W. Biederman * 11181f87f0b5SEric W. Biederman * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(), 11191f87f0b5SEric W. Biederman * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), 11201f87f0b5SEric W. Biederman * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax() 11211f87f0b5SEric W. Biederman * 11221f87f0b5SEric W. Biederman * It is the handler's job to read the input buffer from user memory 11231f87f0b5SEric W. Biederman * and process it. The handler should return 0 on success. 11241f87f0b5SEric W. Biederman * 11251f87f0b5SEric W. Biederman * This routine returns %NULL on a failure to register, and a pointer 11261f87f0b5SEric W. Biederman * to the table header on success. 11271f87f0b5SEric W. Biederman */ 11286e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table( 11291f87f0b5SEric W. Biederman struct ctl_table_root *root, 11301f87f0b5SEric W. Biederman struct nsproxy *namespaces, 11316e9d5164SEric W. Biederman const char *path, struct ctl_table *table) 11321f87f0b5SEric W. Biederman { 11330e47c99dSEric W. Biederman struct ctl_table_header *links = NULL; 11341f87f0b5SEric W. Biederman struct ctl_table_header *header; 11356e9d5164SEric W. Biederman const char *name, *nextname; 11361f87f0b5SEric W. Biederman struct ctl_table_set *set; 11377ec66d06SEric W. Biederman struct ctl_dir *dir; 11381f87f0b5SEric W. Biederman 11397ec66d06SEric W. Biederman header = kzalloc(sizeof(struct ctl_table_header), GFP_KERNEL); 11401f87f0b5SEric W. Biederman if (!header) 11411f87f0b5SEric W. Biederman return NULL; 11421f87f0b5SEric W. Biederman 1143e0d04529SEric W. Biederman init_header(header, root, NULL, table); 11447c60c48fSEric W. Biederman if (sysctl_check_table(path, table)) 11457c60c48fSEric W. Biederman goto fail; 11468d6ecfccSEric W. Biederman 11471f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 11487ec66d06SEric W. Biederman header->set = set = lookup_header_set(root, namespaces); 11490e47c99dSEric W. Biederman dir = &set->dir; 11507ec66d06SEric W. Biederman dir->header.nreg++; 11517ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 11527ec66d06SEric W. Biederman 11537ec66d06SEric W. Biederman /* Find the directory for the ctl_table */ 11547ec66d06SEric W. Biederman for (name = path; name; name = nextname) { 11557ec66d06SEric W. Biederman int namelen; 11567ec66d06SEric W. Biederman nextname = strchr(name, '/'); 11577ec66d06SEric W. Biederman if (nextname) { 11587ec66d06SEric W. Biederman namelen = nextname - name; 11597ec66d06SEric W. Biederman nextname++; 11607ec66d06SEric W. Biederman } else { 11617ec66d06SEric W. Biederman namelen = strlen(name); 11627ec66d06SEric W. Biederman } 11637ec66d06SEric W. Biederman if (namelen == 0) 11641f87f0b5SEric W. Biederman continue; 11657ec66d06SEric W. Biederman 11660e47c99dSEric W. Biederman dir = get_subdir(dir, name, namelen); 11677ec66d06SEric W. Biederman if (IS_ERR(dir)) 11687ec66d06SEric W. Biederman goto fail; 11691f87f0b5SEric W. Biederman } 11700e47c99dSEric W. Biederman 11717ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 11720e47c99dSEric W. Biederman if (sysctl_check_dups(dir, path, table)) 11737ec66d06SEric W. Biederman goto fail_put_dir_locked; 11740e47c99dSEric W. Biederman 11750e47c99dSEric W. Biederman if (insert_header(dir, header)) 11760e47c99dSEric W. Biederman goto fail_put_dir_locked; 11770e47c99dSEric W. Biederman 11787ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 11791f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 11801f87f0b5SEric W. Biederman 11811f87f0b5SEric W. Biederman return header; 11820e47c99dSEric W. Biederman 11837ec66d06SEric W. Biederman fail_put_dir_locked: 11847ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 11857c60c48fSEric W. Biederman spin_unlock(&sysctl_lock); 11867c60c48fSEric W. Biederman fail: 11870e47c99dSEric W. Biederman kfree(links); 11887c60c48fSEric W. Biederman kfree(header); 11897c60c48fSEric W. Biederman dump_stack(); 11907c60c48fSEric W. Biederman return NULL; 11911f87f0b5SEric W. Biederman } 11921f87f0b5SEric W. Biederman 11936e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name) 11946e9d5164SEric W. Biederman { 11956e9d5164SEric W. Biederman int namelen; 11966e9d5164SEric W. Biederman namelen = strlen(name); 11976e9d5164SEric W. Biederman if (((pos - path) + namelen + 2) >= PATH_MAX) 11986e9d5164SEric W. Biederman return NULL; 11996e9d5164SEric W. Biederman memcpy(pos, name, namelen); 12006e9d5164SEric W. Biederman pos[namelen] = '/'; 12016e9d5164SEric W. Biederman pos[namelen + 1] = '\0'; 12026e9d5164SEric W. Biederman pos += namelen + 1; 12036e9d5164SEric W. Biederman return pos; 12046e9d5164SEric W. Biederman } 12056e9d5164SEric W. Biederman 1206f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table) 1207f728019bSEric W. Biederman { 1208f728019bSEric W. Biederman int has_files = 0; 1209f728019bSEric W. Biederman int nr_subheaders = 0; 1210f728019bSEric W. Biederman struct ctl_table *entry; 1211f728019bSEric W. Biederman 1212f728019bSEric W. Biederman /* special case: no directory and empty directory */ 1213f728019bSEric W. Biederman if (!table || !table->procname) 1214f728019bSEric W. Biederman return 1; 1215f728019bSEric W. Biederman 1216f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1217f728019bSEric W. Biederman if (entry->child) 1218f728019bSEric W. Biederman nr_subheaders += count_subheaders(entry->child); 1219f728019bSEric W. Biederman else 1220f728019bSEric W. Biederman has_files = 1; 1221f728019bSEric W. Biederman } 1222f728019bSEric W. Biederman return nr_subheaders + has_files; 1223f728019bSEric W. Biederman } 1224f728019bSEric W. Biederman 1225f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos, 1226f728019bSEric W. Biederman struct ctl_table_header ***subheader, 1227f728019bSEric W. Biederman struct ctl_table_root *root, struct nsproxy *namespaces, 1228f728019bSEric W. Biederman struct ctl_table *table) 1229f728019bSEric W. Biederman { 1230f728019bSEric W. Biederman struct ctl_table *ctl_table_arg = NULL; 1231f728019bSEric W. Biederman struct ctl_table *entry, *files; 1232f728019bSEric W. Biederman int nr_files = 0; 1233f728019bSEric W. Biederman int nr_dirs = 0; 1234f728019bSEric W. Biederman int err = -ENOMEM; 1235f728019bSEric W. Biederman 1236f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1237f728019bSEric W. Biederman if (entry->child) 1238f728019bSEric W. Biederman nr_dirs++; 1239f728019bSEric W. Biederman else 1240f728019bSEric W. Biederman nr_files++; 1241f728019bSEric W. Biederman } 1242f728019bSEric W. Biederman 1243f728019bSEric W. Biederman files = table; 1244f728019bSEric W. Biederman /* If there are mixed files and directories we need a new table */ 1245f728019bSEric W. Biederman if (nr_dirs && nr_files) { 1246f728019bSEric W. Biederman struct ctl_table *new; 1247f728019bSEric W. Biederman files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1), 1248f728019bSEric W. Biederman GFP_KERNEL); 1249f728019bSEric W. Biederman if (!files) 1250f728019bSEric W. Biederman goto out; 1251f728019bSEric W. Biederman 1252f728019bSEric W. Biederman ctl_table_arg = files; 1253f728019bSEric W. Biederman for (new = files, entry = table; entry->procname; entry++) { 1254f728019bSEric W. Biederman if (entry->child) 1255f728019bSEric W. Biederman continue; 1256f728019bSEric W. Biederman *new = *entry; 1257f728019bSEric W. Biederman new++; 1258f728019bSEric W. Biederman } 1259f728019bSEric W. Biederman } 1260f728019bSEric W. Biederman 1261f728019bSEric W. Biederman /* Register everything except a directory full of subdirectories */ 1262f728019bSEric W. Biederman if (nr_files || !nr_dirs) { 1263f728019bSEric W. Biederman struct ctl_table_header *header; 1264f728019bSEric W. Biederman header = __register_sysctl_table(root, namespaces, path, files); 1265f728019bSEric W. Biederman if (!header) { 1266f728019bSEric W. Biederman kfree(ctl_table_arg); 1267f728019bSEric W. Biederman goto out; 1268f728019bSEric W. Biederman } 1269f728019bSEric W. Biederman 1270f728019bSEric W. Biederman /* Remember if we need to free the file table */ 1271f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1272f728019bSEric W. Biederman **subheader = header; 1273f728019bSEric W. Biederman (*subheader)++; 1274f728019bSEric W. Biederman } 1275f728019bSEric W. Biederman 1276f728019bSEric W. Biederman /* Recurse into the subdirectories. */ 1277f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1278f728019bSEric W. Biederman char *child_pos; 1279f728019bSEric W. Biederman 1280f728019bSEric W. Biederman if (!entry->child) 1281f728019bSEric W. Biederman continue; 1282f728019bSEric W. Biederman 1283f728019bSEric W. Biederman err = -ENAMETOOLONG; 1284f728019bSEric W. Biederman child_pos = append_path(path, pos, entry->procname); 1285f728019bSEric W. Biederman if (!child_pos) 1286f728019bSEric W. Biederman goto out; 1287f728019bSEric W. Biederman 1288f728019bSEric W. Biederman err = register_leaf_sysctl_tables(path, child_pos, subheader, 1289f728019bSEric W. Biederman root, namespaces, entry->child); 1290f728019bSEric W. Biederman pos[0] = '\0'; 1291f728019bSEric W. Biederman if (err) 1292f728019bSEric W. Biederman goto out; 1293f728019bSEric W. Biederman } 1294f728019bSEric W. Biederman err = 0; 1295f728019bSEric W. Biederman out: 1296f728019bSEric W. Biederman /* On failure our caller will unregister all registered subheaders */ 1297f728019bSEric W. Biederman return err; 1298f728019bSEric W. Biederman } 1299f728019bSEric W. Biederman 13006e9d5164SEric W. Biederman /** 13016e9d5164SEric W. Biederman * __register_sysctl_paths - register a sysctl table hierarchy 13026e9d5164SEric W. Biederman * @root: List of sysctl headers to register on 13036e9d5164SEric W. Biederman * @namespaces: Data to compute which lists of sysctl entries are visible 13046e9d5164SEric W. Biederman * @path: The path to the directory the sysctl table is in. 13056e9d5164SEric W. Biederman * @table: the top-level table structure 13066e9d5164SEric W. Biederman * 13076e9d5164SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 13086e9d5164SEric W. Biederman * array. A completely 0 filled entry terminates the table. 13096e9d5164SEric W. Biederman * 13106e9d5164SEric W. Biederman * See __register_sysctl_table for more details. 13116e9d5164SEric W. Biederman */ 13126e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths( 13136e9d5164SEric W. Biederman struct ctl_table_root *root, 13146e9d5164SEric W. Biederman struct nsproxy *namespaces, 13156e9d5164SEric W. Biederman const struct ctl_path *path, struct ctl_table *table) 13166e9d5164SEric W. Biederman { 1317ec6a5266SEric W. Biederman struct ctl_table *ctl_table_arg = table; 1318f728019bSEric W. Biederman int nr_subheaders = count_subheaders(table); 1319f728019bSEric W. Biederman struct ctl_table_header *header = NULL, **subheaders, **subheader; 13206e9d5164SEric W. Biederman const struct ctl_path *component; 13216e9d5164SEric W. Biederman char *new_path, *pos; 13226e9d5164SEric W. Biederman 13236e9d5164SEric W. Biederman pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL); 13246e9d5164SEric W. Biederman if (!new_path) 13256e9d5164SEric W. Biederman return NULL; 13266e9d5164SEric W. Biederman 13276e9d5164SEric W. Biederman pos[0] = '\0'; 13286e9d5164SEric W. Biederman for (component = path; component->procname; component++) { 13296e9d5164SEric W. Biederman pos = append_path(new_path, pos, component->procname); 13306e9d5164SEric W. Biederman if (!pos) 13316e9d5164SEric W. Biederman goto out; 13326e9d5164SEric W. Biederman } 1333ec6a5266SEric W. Biederman while (table->procname && table->child && !table[1].procname) { 1334ec6a5266SEric W. Biederman pos = append_path(new_path, pos, table->procname); 1335ec6a5266SEric W. Biederman if (!pos) 1336ec6a5266SEric W. Biederman goto out; 1337ec6a5266SEric W. Biederman table = table->child; 1338ec6a5266SEric W. Biederman } 1339f728019bSEric W. Biederman if (nr_subheaders == 1) { 13406e9d5164SEric W. Biederman header = __register_sysctl_table(root, namespaces, new_path, table); 1341ec6a5266SEric W. Biederman if (header) 1342ec6a5266SEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1343f728019bSEric W. Biederman } else { 1344f728019bSEric W. Biederman header = kzalloc(sizeof(*header) + 1345f728019bSEric W. Biederman sizeof(*subheaders)*nr_subheaders, GFP_KERNEL); 1346f728019bSEric W. Biederman if (!header) 1347f728019bSEric W. Biederman goto out; 1348f728019bSEric W. Biederman 1349f728019bSEric W. Biederman subheaders = (struct ctl_table_header **) (header + 1); 1350f728019bSEric W. Biederman subheader = subheaders; 1351f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1352f728019bSEric W. Biederman 1353f728019bSEric W. Biederman if (register_leaf_sysctl_tables(new_path, pos, &subheader, 1354f728019bSEric W. Biederman root, namespaces, table)) 1355f728019bSEric W. Biederman goto err_register_leaves; 1356f728019bSEric W. Biederman } 1357f728019bSEric W. Biederman 13586e9d5164SEric W. Biederman out: 13596e9d5164SEric W. Biederman kfree(new_path); 13606e9d5164SEric W. Biederman return header; 1361f728019bSEric W. Biederman 1362f728019bSEric W. Biederman err_register_leaves: 1363f728019bSEric W. Biederman while (subheader > subheaders) { 1364f728019bSEric W. Biederman struct ctl_table_header *subh = *(--subheader); 1365f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1366f728019bSEric W. Biederman unregister_sysctl_table(subh); 1367f728019bSEric W. Biederman kfree(table); 1368f728019bSEric W. Biederman } 1369f728019bSEric W. Biederman kfree(header); 1370f728019bSEric W. Biederman header = NULL; 1371f728019bSEric W. Biederman goto out; 13726e9d5164SEric W. Biederman } 13736e9d5164SEric W. Biederman 13741f87f0b5SEric W. Biederman /** 13751f87f0b5SEric W. Biederman * register_sysctl_table_path - register a sysctl table hierarchy 13761f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 13771f87f0b5SEric W. Biederman * @table: the top-level table structure 13781f87f0b5SEric W. Biederman * 13791f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 13801f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 13811f87f0b5SEric W. Biederman * 13821f87f0b5SEric W. Biederman * See __register_sysctl_paths for more details. 13831f87f0b5SEric W. Biederman */ 13841f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path, 13851f87f0b5SEric W. Biederman struct ctl_table *table) 13861f87f0b5SEric W. Biederman { 13871f87f0b5SEric W. Biederman return __register_sysctl_paths(&sysctl_table_root, current->nsproxy, 13881f87f0b5SEric W. Biederman path, table); 13891f87f0b5SEric W. Biederman } 13901f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths); 13911f87f0b5SEric W. Biederman 13921f87f0b5SEric W. Biederman /** 13931f87f0b5SEric W. Biederman * register_sysctl_table - register a sysctl table hierarchy 13941f87f0b5SEric W. Biederman * @table: the top-level table structure 13951f87f0b5SEric W. Biederman * 13961f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 13971f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 13981f87f0b5SEric W. Biederman * 13991f87f0b5SEric W. Biederman * See register_sysctl_paths for more details. 14001f87f0b5SEric W. Biederman */ 14011f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table) 14021f87f0b5SEric W. Biederman { 14031f87f0b5SEric W. Biederman static const struct ctl_path null_path[] = { {} }; 14041f87f0b5SEric W. Biederman 14051f87f0b5SEric W. Biederman return register_sysctl_paths(null_path, table); 14061f87f0b5SEric W. Biederman } 14071f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table); 14081f87f0b5SEric W. Biederman 14090e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header) 14100e47c99dSEric W. Biederman { 14110e47c99dSEric W. Biederman struct ctl_table_set *root_set = &sysctl_table_root.default_set; 14120e47c99dSEric W. Biederman struct ctl_table_root *root = header->root; 14130e47c99dSEric W. Biederman struct ctl_dir *parent = header->parent; 14140e47c99dSEric W. Biederman struct ctl_dir *core_parent; 14150e47c99dSEric W. Biederman struct ctl_table *entry; 14160e47c99dSEric W. Biederman 14170e47c99dSEric W. Biederman if (header->set == root_set) 14180e47c99dSEric W. Biederman return; 14190e47c99dSEric W. Biederman 14200e47c99dSEric W. Biederman core_parent = xlate_dir(root_set, parent); 14210e47c99dSEric W. Biederman if (IS_ERR(core_parent)) 14220e47c99dSEric W. Biederman return; 14230e47c99dSEric W. Biederman 14240e47c99dSEric W. Biederman for (entry = header->ctl_table; entry->procname; entry++) { 14250e47c99dSEric W. Biederman struct ctl_table_header *link_head; 14260e47c99dSEric W. Biederman struct ctl_table *link; 14270e47c99dSEric W. Biederman const char *name = entry->procname; 14280e47c99dSEric W. Biederman 14290e47c99dSEric W. Biederman link = find_entry(&link_head, core_parent, name, strlen(name)); 14300e47c99dSEric W. Biederman if (link && 14310e47c99dSEric W. Biederman ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) || 14320e47c99dSEric W. Biederman (S_ISLNK(link->mode) && (link->data == root)))) { 14330e47c99dSEric W. Biederman drop_sysctl_table(link_head); 14340e47c99dSEric W. Biederman } 14350e47c99dSEric W. Biederman else { 14360e47c99dSEric W. Biederman printk(KERN_ERR "sysctl link missing during unregister: "); 14370e47c99dSEric W. Biederman sysctl_print_dir(parent); 14380e47c99dSEric W. Biederman printk(KERN_CONT "/%s\n", name); 14390e47c99dSEric W. Biederman } 14400e47c99dSEric W. Biederman } 14410e47c99dSEric W. Biederman } 14420e47c99dSEric W. Biederman 1443938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header) 1444938aaa4fSEric W. Biederman { 14457ec66d06SEric W. Biederman struct ctl_dir *parent = header->parent; 14467ec66d06SEric W. Biederman 1447938aaa4fSEric W. Biederman if (--header->nreg) 1448938aaa4fSEric W. Biederman return; 1449938aaa4fSEric W. Biederman 14500e47c99dSEric W. Biederman put_links(header); 1451938aaa4fSEric W. Biederman start_unregistering(header); 1452938aaa4fSEric W. Biederman if (!--header->count) 1453938aaa4fSEric W. Biederman kfree_rcu(header, rcu); 14547ec66d06SEric W. Biederman 14557ec66d06SEric W. Biederman if (parent) 14567ec66d06SEric W. Biederman drop_sysctl_table(&parent->header); 1457938aaa4fSEric W. Biederman } 1458938aaa4fSEric W. Biederman 14591f87f0b5SEric W. Biederman /** 14601f87f0b5SEric W. Biederman * unregister_sysctl_table - unregister a sysctl table hierarchy 14611f87f0b5SEric W. Biederman * @header: the header returned from register_sysctl_table 14621f87f0b5SEric W. Biederman * 14631f87f0b5SEric W. Biederman * Unregisters the sysctl table and all children. proc entries may not 14641f87f0b5SEric W. Biederman * actually be removed until they are no longer used by anyone. 14651f87f0b5SEric W. Biederman */ 14661f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header) 14671f87f0b5SEric W. Biederman { 1468f728019bSEric W. Biederman int nr_subheaders; 14691f87f0b5SEric W. Biederman might_sleep(); 14701f87f0b5SEric W. Biederman 14711f87f0b5SEric W. Biederman if (header == NULL) 14721f87f0b5SEric W. Biederman return; 14731f87f0b5SEric W. Biederman 1474f728019bSEric W. Biederman nr_subheaders = count_subheaders(header->ctl_table_arg); 1475f728019bSEric W. Biederman if (unlikely(nr_subheaders > 1)) { 1476f728019bSEric W. Biederman struct ctl_table_header **subheaders; 1477f728019bSEric W. Biederman int i; 1478f728019bSEric W. Biederman 1479f728019bSEric W. Biederman subheaders = (struct ctl_table_header **)(header + 1); 1480f728019bSEric W. Biederman for (i = nr_subheaders -1; i >= 0; i--) { 1481f728019bSEric W. Biederman struct ctl_table_header *subh = subheaders[i]; 1482f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1483f728019bSEric W. Biederman unregister_sysctl_table(subh); 1484f728019bSEric W. Biederman kfree(table); 1485f728019bSEric W. Biederman } 1486f728019bSEric W. Biederman kfree(header); 1487f728019bSEric W. Biederman return; 1488f728019bSEric W. Biederman } 1489f728019bSEric W. Biederman 14901f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1491938aaa4fSEric W. Biederman drop_sysctl_table(header); 14921f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 14931f87f0b5SEric W. Biederman } 14941f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table); 14951f87f0b5SEric W. Biederman 14960e47c99dSEric W. Biederman void setup_sysctl_set(struct ctl_table_set *set, 14979eb47c26SEric W. Biederman struct ctl_table_root *root, 14981f87f0b5SEric W. Biederman int (*is_seen)(struct ctl_table_set *)) 14991f87f0b5SEric W. Biederman { 15000e47c99dSEric W. Biederman memset(set, sizeof(*set), 0); 15010e47c99dSEric W. Biederman INIT_LIST_HEAD(&set->list); 15020e47c99dSEric W. Biederman set->is_seen = is_seen; 15030e47c99dSEric W. Biederman init_header(&set->dir.header, root, set, root_table); 15041f87f0b5SEric W. Biederman } 15051f87f0b5SEric W. Biederman 150697324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set) 150797324cd8SEric W. Biederman { 150897324cd8SEric W. Biederman WARN_ON(!list_empty(&set->list)); 150997324cd8SEric W. Biederman } 15101f87f0b5SEric W. Biederman 15111e0edd3fSAlexey Dobriyan int __init proc_sys_init(void) 151277b14db5SEric W. Biederman { 1513e1675231SAlexey Dobriyan struct proc_dir_entry *proc_sys_root; 1514e1675231SAlexey Dobriyan 151577b14db5SEric W. Biederman proc_sys_root = proc_mkdir("sys", NULL); 15169043476fSAl Viro proc_sys_root->proc_iops = &proc_sys_dir_operations; 15179043476fSAl Viro proc_sys_root->proc_fops = &proc_sys_dir_file_operations; 151877b14db5SEric W. Biederman proc_sys_root->nlink = 0; 1519de4e83bdSEric W. Biederman 1520de4e83bdSEric W. Biederman return sysctl_init(); 152177b14db5SEric W. Biederman } 1522