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 = "", 31a194558eSEric W. Biederman .mode = S_IRUGO|S_IXUGO, 32a194558eSEric W. Biederman .child = &root_table[1], 33a194558eSEric W. Biederman }, 34a194558eSEric W. Biederman { } 35a194558eSEric W. Biederman }; 361f87f0b5SEric W. Biederman static struct ctl_table_root sysctl_table_root; 371f87f0b5SEric W. Biederman static struct ctl_table_header root_table_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),}}, 421f87f0b5SEric W. Biederman .root = &sysctl_table_root, 431f87f0b5SEric W. Biederman .set = &sysctl_table_root.default_set, 441f87f0b5SEric W. Biederman }; 451f87f0b5SEric W. Biederman static struct ctl_table_root sysctl_table_root = { 461f87f0b5SEric W. Biederman .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list), 471f87f0b5SEric W. Biederman .default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry), 489eb47c26SEric W. Biederman .default_set.root = &sysctl_table_root, 491f87f0b5SEric W. Biederman }; 501f87f0b5SEric W. Biederman 511f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock); 521f87f0b5SEric W. Biederman 53076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2) 54076c3eedSEric W. Biederman { 55076c3eedSEric W. Biederman int minlen; 56076c3eedSEric W. Biederman int cmp; 57076c3eedSEric W. Biederman 58076c3eedSEric W. Biederman minlen = len1; 59076c3eedSEric W. Biederman if (minlen > len2) 60076c3eedSEric W. Biederman minlen = len2; 61076c3eedSEric W. Biederman 62076c3eedSEric W. Biederman cmp = memcmp(name1, name2, minlen); 63076c3eedSEric W. Biederman if (cmp == 0) 64076c3eedSEric W. Biederman cmp = len1 - len2; 65076c3eedSEric W. Biederman return cmp; 66076c3eedSEric W. Biederman } 67076c3eedSEric W. Biederman 68076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead, 69076c3eedSEric W. Biederman struct ctl_table_set *set, 70076c3eedSEric W. Biederman struct ctl_table_header *dir_head, struct ctl_table *dir, 71076c3eedSEric W. Biederman const char *name, int namelen) 72076c3eedSEric W. Biederman { 73076c3eedSEric W. Biederman struct ctl_table_header *head; 74076c3eedSEric W. Biederman struct ctl_table *entry; 75076c3eedSEric W. Biederman 76076c3eedSEric W. Biederman if (dir_head->set == set) { 77076c3eedSEric W. Biederman for (entry = dir; entry->procname; entry++) { 78076c3eedSEric W. Biederman const char *procname = entry->procname; 79076c3eedSEric W. Biederman if (namecmp(procname, strlen(procname), name, namelen) == 0) { 80076c3eedSEric W. Biederman *phead = dir_head; 81076c3eedSEric W. Biederman return entry; 82076c3eedSEric W. Biederman } 83076c3eedSEric W. Biederman } 84076c3eedSEric W. Biederman } 85076c3eedSEric W. Biederman 86076c3eedSEric W. Biederman list_for_each_entry(head, &set->list, ctl_entry) { 87076c3eedSEric W. Biederman if (head->unregistering) 88076c3eedSEric W. Biederman continue; 89076c3eedSEric W. Biederman if (head->attached_to != dir) 90076c3eedSEric W. Biederman continue; 91076c3eedSEric W. Biederman for (entry = head->attached_by; entry->procname; entry++) { 92076c3eedSEric W. Biederman const char *procname = entry->procname; 93076c3eedSEric W. Biederman if (namecmp(procname, strlen(procname), name, namelen) == 0) { 94076c3eedSEric W. Biederman *phead = head; 95076c3eedSEric W. Biederman return entry; 96076c3eedSEric W. Biederman } 97076c3eedSEric W. Biederman } 98076c3eedSEric W. Biederman } 99076c3eedSEric W. Biederman return NULL; 100076c3eedSEric W. Biederman } 101076c3eedSEric W. Biederman 102e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head, 103e0d04529SEric W. Biederman struct ctl_table_root *root, struct ctl_table_set *set, 104e0d04529SEric W. Biederman struct ctl_table *table) 105e0d04529SEric W. Biederman { 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 1228425d6aaSEric W. Biederman static void insert_header(struct ctl_table_header *header) 1238425d6aaSEric W. Biederman { 1248425d6aaSEric W. Biederman header->parent->count++; 1258425d6aaSEric W. Biederman list_add_tail(&header->ctl_entry, &header->set->list); 1268425d6aaSEric W. Biederman } 1278425d6aaSEric W. Biederman 1281f87f0b5SEric W. Biederman /* called under sysctl_lock */ 1291f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p) 1301f87f0b5SEric W. Biederman { 1311f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 1321f87f0b5SEric W. Biederman return 0; 1331f87f0b5SEric W. Biederman p->used++; 1341f87f0b5SEric W. Biederman return 1; 1351f87f0b5SEric W. Biederman } 1361f87f0b5SEric W. Biederman 1371f87f0b5SEric W. Biederman /* called under sysctl_lock */ 1381f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p) 1391f87f0b5SEric W. Biederman { 1401f87f0b5SEric W. Biederman if (!--p->used) 1411f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 1421f87f0b5SEric W. Biederman complete(p->unregistering); 1431f87f0b5SEric W. Biederman } 1441f87f0b5SEric W. Biederman 1451f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */ 1461f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p) 1471f87f0b5SEric W. Biederman { 1481f87f0b5SEric W. Biederman /* 1491f87f0b5SEric W. Biederman * if p->used is 0, nobody will ever touch that entry again; 1501f87f0b5SEric W. Biederman * we'll eliminate all paths to it before dropping sysctl_lock 1511f87f0b5SEric W. Biederman */ 1521f87f0b5SEric W. Biederman if (unlikely(p->used)) { 1531f87f0b5SEric W. Biederman struct completion wait; 1541f87f0b5SEric W. Biederman init_completion(&wait); 1551f87f0b5SEric W. Biederman p->unregistering = &wait; 1561f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1571f87f0b5SEric W. Biederman wait_for_completion(&wait); 1581f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1591f87f0b5SEric W. Biederman } else { 1601f87f0b5SEric W. Biederman /* anything non-NULL; we'll never dereference it */ 1611f87f0b5SEric W. Biederman p->unregistering = ERR_PTR(-EINVAL); 1621f87f0b5SEric W. Biederman } 1631f87f0b5SEric W. Biederman /* 1641f87f0b5SEric W. Biederman * do not remove from the list until nobody holds it; walking the 1651f87f0b5SEric W. Biederman * list in do_sysctl() relies on that. 1661f87f0b5SEric W. Biederman */ 1678425d6aaSEric W. Biederman erase_header(p); 1681f87f0b5SEric W. Biederman } 1691f87f0b5SEric W. Biederman 1701f87f0b5SEric W. Biederman static void sysctl_head_get(struct ctl_table_header *head) 1711f87f0b5SEric W. Biederman { 1721f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1731f87f0b5SEric W. Biederman head->count++; 1741f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1751f87f0b5SEric W. Biederman } 1761f87f0b5SEric W. Biederman 1771f87f0b5SEric W. Biederman void sysctl_head_put(struct ctl_table_header *head) 1781f87f0b5SEric W. Biederman { 1791f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1801f87f0b5SEric W. Biederman if (!--head->count) 1811f87f0b5SEric W. Biederman kfree_rcu(head, rcu); 1821f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1831f87f0b5SEric W. Biederman } 1841f87f0b5SEric W. Biederman 1851f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head) 1861f87f0b5SEric W. Biederman { 1871f87f0b5SEric W. Biederman if (!head) 1881f87f0b5SEric W. Biederman BUG(); 1891f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1901f87f0b5SEric W. Biederman if (!use_table(head)) 1911f87f0b5SEric W. Biederman head = ERR_PTR(-ENOENT); 1921f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1931f87f0b5SEric W. Biederman return head; 1941f87f0b5SEric W. Biederman } 1951f87f0b5SEric W. Biederman 1961f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head) 1971f87f0b5SEric W. Biederman { 1981f87f0b5SEric W. Biederman if (!head) 1991f87f0b5SEric W. Biederman return; 2001f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 2011f87f0b5SEric W. Biederman unuse_table(head); 2021f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 2031f87f0b5SEric W. Biederman } 2041f87f0b5SEric W. Biederman 2051f87f0b5SEric W. Biederman static struct ctl_table_set * 2061f87f0b5SEric W. Biederman lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces) 2071f87f0b5SEric W. Biederman { 2081f87f0b5SEric W. Biederman struct ctl_table_set *set = &root->default_set; 2091f87f0b5SEric W. Biederman if (root->lookup) 2101f87f0b5SEric W. Biederman set = root->lookup(root, namespaces); 2111f87f0b5SEric W. Biederman return set; 2121f87f0b5SEric W. Biederman } 2131f87f0b5SEric W. Biederman 2141f87f0b5SEric W. Biederman static struct list_head * 2151f87f0b5SEric W. Biederman lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces) 2161f87f0b5SEric W. Biederman { 2171f87f0b5SEric W. Biederman struct ctl_table_set *set = lookup_header_set(root, namespaces); 2181f87f0b5SEric W. Biederman return &set->list; 2191f87f0b5SEric W. Biederman } 2201f87f0b5SEric W. Biederman 221076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead, 222076c3eedSEric W. Biederman struct ctl_table_header *dir_head, 223076c3eedSEric W. Biederman struct ctl_table *dir, 224076c3eedSEric W. Biederman const char *name, int namelen) 225076c3eedSEric W. Biederman { 226076c3eedSEric W. Biederman struct ctl_table_header *head; 227076c3eedSEric W. Biederman struct ctl_table *entry; 228076c3eedSEric W. Biederman struct ctl_table_root *root; 229076c3eedSEric W. Biederman struct ctl_table_set *set; 230076c3eedSEric W. Biederman 231076c3eedSEric W. Biederman spin_lock(&sysctl_lock); 232076c3eedSEric W. Biederman root = &sysctl_table_root; 233076c3eedSEric W. Biederman do { 234076c3eedSEric W. Biederman set = lookup_header_set(root, current->nsproxy); 235076c3eedSEric W. Biederman entry = find_entry(&head, set, dir_head, dir, name, namelen); 236076c3eedSEric W. Biederman if (entry && use_table(head)) 237076c3eedSEric W. Biederman *phead = head; 238076c3eedSEric W. Biederman else 239076c3eedSEric W. Biederman entry = NULL; 240076c3eedSEric W. Biederman root = list_entry(root->root_list.next, 241076c3eedSEric W. Biederman struct ctl_table_root, root_list); 242076c3eedSEric W. Biederman } while (!entry && root != &sysctl_table_root); 243076c3eedSEric W. Biederman spin_unlock(&sysctl_lock); 244076c3eedSEric W. Biederman return entry; 245076c3eedSEric W. Biederman } 246076c3eedSEric W. Biederman 2476a75ce16SEric W. Biederman static struct ctl_table_header *next_usable_entry(struct ctl_table *dir, 2486a75ce16SEric W. Biederman struct ctl_table_root *root, struct list_head *tmp) 2491f87f0b5SEric W. Biederman { 2506a75ce16SEric W. Biederman struct nsproxy *namespaces = current->nsproxy; 2511f87f0b5SEric W. Biederman struct list_head *header_list; 2521f87f0b5SEric W. Biederman struct ctl_table_header *head; 2531f87f0b5SEric W. Biederman 2541f87f0b5SEric W. Biederman goto next; 2551f87f0b5SEric W. Biederman for (;;) { 2561f87f0b5SEric W. Biederman head = list_entry(tmp, struct ctl_table_header, ctl_entry); 2576a75ce16SEric W. Biederman root = head->root; 2581f87f0b5SEric W. Biederman 2596a75ce16SEric W. Biederman if (head->attached_to != dir || 2606a75ce16SEric W. Biederman !head->attached_by->procname || 2616a75ce16SEric W. Biederman !use_table(head)) 2621f87f0b5SEric W. Biederman goto next; 2636a75ce16SEric W. Biederman 2641f87f0b5SEric W. Biederman return head; 2651f87f0b5SEric W. Biederman next: 2661f87f0b5SEric W. Biederman tmp = tmp->next; 2671f87f0b5SEric W. Biederman header_list = lookup_header_list(root, namespaces); 2681f87f0b5SEric W. Biederman if (tmp != header_list) 2691f87f0b5SEric W. Biederman continue; 2701f87f0b5SEric W. Biederman 2711f87f0b5SEric W. Biederman do { 2721f87f0b5SEric W. Biederman root = list_entry(root->root_list.next, 2731f87f0b5SEric W. Biederman struct ctl_table_root, root_list); 2741f87f0b5SEric W. Biederman if (root == &sysctl_table_root) 2751f87f0b5SEric W. Biederman goto out; 2761f87f0b5SEric W. Biederman header_list = lookup_header_list(root, namespaces); 2771f87f0b5SEric W. Biederman } while (list_empty(header_list)); 2781f87f0b5SEric W. Biederman tmp = header_list->next; 2791f87f0b5SEric W. Biederman } 2801f87f0b5SEric W. Biederman out: 2811f87f0b5SEric W. Biederman return NULL; 2821f87f0b5SEric W. Biederman } 2831f87f0b5SEric W. Biederman 2846a75ce16SEric W. Biederman static void first_entry( 2856a75ce16SEric W. Biederman struct ctl_table_header *dir_head, struct ctl_table *dir, 2866a75ce16SEric W. Biederman struct ctl_table_header **phead, struct ctl_table **pentry) 2871f87f0b5SEric W. Biederman { 2886a75ce16SEric W. Biederman struct ctl_table_header *head = dir_head; 2896a75ce16SEric W. Biederman struct ctl_table *entry = dir; 2906a75ce16SEric W. Biederman 2916a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 2926a75ce16SEric W. Biederman if (entry->procname) { 2936a75ce16SEric W. Biederman use_table(head); 2946a75ce16SEric W. Biederman } else { 2956a75ce16SEric W. Biederman head = next_usable_entry(dir, &sysctl_table_root, 2966a75ce16SEric W. Biederman &sysctl_table_root.default_set.list); 2976a75ce16SEric W. Biederman if (head) 2986a75ce16SEric W. Biederman entry = head->attached_by; 2996a75ce16SEric W. Biederman } 3006a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 3016a75ce16SEric W. Biederman *phead = head; 3026a75ce16SEric W. Biederman *pentry = entry; 3036a75ce16SEric W. Biederman } 3046a75ce16SEric W. Biederman 3056a75ce16SEric W. Biederman static void next_entry(struct ctl_table *dir, 3066a75ce16SEric W. Biederman struct ctl_table_header **phead, struct ctl_table **pentry) 3076a75ce16SEric W. Biederman { 3086a75ce16SEric W. Biederman struct ctl_table_header *head = *phead; 3096a75ce16SEric W. Biederman struct ctl_table *entry = *pentry; 3106a75ce16SEric W. Biederman 3116a75ce16SEric W. Biederman entry++; 3126a75ce16SEric W. Biederman if (!entry->procname) { 3136a75ce16SEric W. Biederman struct ctl_table_root *root = head->root; 3146a75ce16SEric W. Biederman struct list_head *tmp = &head->ctl_entry; 3156a75ce16SEric W. Biederman if (head->attached_to != dir) { 3166a75ce16SEric W. Biederman root = &sysctl_table_root; 3176a75ce16SEric W. Biederman tmp = &sysctl_table_root.default_set.list; 3186a75ce16SEric W. Biederman } 3196a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 3206a75ce16SEric W. Biederman unuse_table(head); 3216a75ce16SEric W. Biederman head = next_usable_entry(dir, root, tmp); 3226a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 3236a75ce16SEric W. Biederman if (head) 3246a75ce16SEric W. Biederman entry = head->attached_by; 3256a75ce16SEric W. Biederman } 3266a75ce16SEric W. Biederman *phead = head; 3276a75ce16SEric W. Biederman *pentry = entry; 3281f87f0b5SEric W. Biederman } 3291f87f0b5SEric W. Biederman 3301f87f0b5SEric W. Biederman void register_sysctl_root(struct ctl_table_root *root) 3311f87f0b5SEric W. Biederman { 3321f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 3331f87f0b5SEric W. Biederman list_add_tail(&root->root_list, &sysctl_table_root.root_list); 3341f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 3351f87f0b5SEric W. Biederman } 3361f87f0b5SEric W. Biederman 3371f87f0b5SEric W. Biederman /* 3381f87f0b5SEric W. Biederman * sysctl_perm does NOT grant the superuser all rights automatically, because 3391f87f0b5SEric W. Biederman * some sysctl variables are readonly even to root. 3401f87f0b5SEric W. Biederman */ 3411f87f0b5SEric W. Biederman 3421f87f0b5SEric W. Biederman static int test_perm(int mode, int op) 3431f87f0b5SEric W. Biederman { 3441f87f0b5SEric W. Biederman if (!current_euid()) 3451f87f0b5SEric W. Biederman mode >>= 6; 3461f87f0b5SEric W. Biederman else if (in_egroup_p(0)) 3471f87f0b5SEric W. Biederman mode >>= 3; 3481f87f0b5SEric W. Biederman if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0) 3491f87f0b5SEric W. Biederman return 0; 3501f87f0b5SEric W. Biederman return -EACCES; 3511f87f0b5SEric W. Biederman } 3521f87f0b5SEric W. Biederman 3531f87f0b5SEric W. Biederman static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op) 3541f87f0b5SEric W. Biederman { 3551f87f0b5SEric W. Biederman int mode; 3561f87f0b5SEric W. Biederman 3571f87f0b5SEric W. Biederman if (root->permissions) 3581f87f0b5SEric W. Biederman mode = root->permissions(root, current->nsproxy, table); 3591f87f0b5SEric W. Biederman else 3601f87f0b5SEric W. Biederman mode = table->mode; 3611f87f0b5SEric W. Biederman 3621f87f0b5SEric W. Biederman return test_perm(mode, op); 3631f87f0b5SEric W. Biederman } 3641f87f0b5SEric W. Biederman 3659043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb, 3669043476fSAl Viro struct ctl_table_header *head, struct ctl_table *table) 36777b14db5SEric W. Biederman { 36877b14db5SEric W. Biederman struct inode *inode; 3699043476fSAl Viro struct proc_inode *ei; 37077b14db5SEric W. Biederman 3719043476fSAl Viro inode = new_inode(sb); 37277b14db5SEric W. Biederman if (!inode) 37377b14db5SEric W. Biederman goto out; 37477b14db5SEric W. Biederman 37585fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 37685fe4025SChristoph Hellwig 3779043476fSAl Viro sysctl_head_get(head); 37877b14db5SEric W. Biederman ei = PROC_I(inode); 3799043476fSAl Viro ei->sysctl = head; 3809043476fSAl Viro ei->sysctl_entry = table; 3819043476fSAl Viro 38277b14db5SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 3839043476fSAl Viro inode->i_mode = table->mode; 3849043476fSAl Viro if (!table->child) { 3859043476fSAl Viro inode->i_mode |= S_IFREG; 38677b14db5SEric W. Biederman inode->i_op = &proc_sys_inode_operations; 38777b14db5SEric W. Biederman inode->i_fop = &proc_sys_file_operations; 3889043476fSAl Viro } else { 3899043476fSAl Viro inode->i_mode |= S_IFDIR; 3909043476fSAl Viro inode->i_op = &proc_sys_dir_operations; 3919043476fSAl Viro inode->i_fop = &proc_sys_dir_file_operations; 3929043476fSAl Viro } 39377b14db5SEric W. Biederman out: 39477b14db5SEric W. Biederman return inode; 39577b14db5SEric W. Biederman } 39677b14db5SEric W. Biederman 39781324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode) 39877b14db5SEric W. Biederman { 3993cc3e046SEric W. Biederman struct ctl_table_header *head = PROC_I(inode)->sysctl; 4003cc3e046SEric W. Biederman if (!head) 4013cc3e046SEric W. Biederman head = &root_table_header; 4023cc3e046SEric W. Biederman return sysctl_head_grab(head); 40377b14db5SEric W. Biederman } 40477b14db5SEric W. Biederman 40577b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, 40677b14db5SEric W. Biederman struct nameidata *nd) 40777b14db5SEric W. Biederman { 4089043476fSAl Viro struct ctl_table_header *head = grab_header(dir); 4099043476fSAl Viro struct ctl_table *table = PROC_I(dir)->sysctl_entry; 4109043476fSAl Viro struct ctl_table_header *h = NULL; 4119043476fSAl Viro struct qstr *name = &dentry->d_name; 4129043476fSAl Viro struct ctl_table *p; 41377b14db5SEric W. Biederman struct inode *inode; 4149043476fSAl Viro struct dentry *err = ERR_PTR(-ENOENT); 41577b14db5SEric W. Biederman 4169043476fSAl Viro if (IS_ERR(head)) 4179043476fSAl Viro return ERR_CAST(head); 4189043476fSAl Viro 4199043476fSAl Viro if (table && !table->child) { 4209043476fSAl Viro WARN_ON(1); 4219043476fSAl Viro goto out; 4229043476fSAl Viro } 4239043476fSAl Viro 424a194558eSEric W. Biederman table = table ? table->child : &head->ctl_table[1]; 4259043476fSAl Viro 426076c3eedSEric W. Biederman p = lookup_entry(&h, head, table, name->name, name->len); 4279043476fSAl Viro if (!p) 42877b14db5SEric W. Biederman goto out; 42977b14db5SEric W. Biederman 43077b14db5SEric W. Biederman err = ERR_PTR(-ENOMEM); 4319043476fSAl Viro inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p); 4329043476fSAl Viro if (h) 4339043476fSAl Viro sysctl_head_finish(h); 4349043476fSAl Viro 43577b14db5SEric W. Biederman if (!inode) 43677b14db5SEric W. Biederman goto out; 43777b14db5SEric W. Biederman 43877b14db5SEric W. Biederman err = NULL; 439fb045adbSNick Piggin d_set_d_op(dentry, &proc_sys_dentry_operations); 44077b14db5SEric W. Biederman d_add(dentry, inode); 44177b14db5SEric W. Biederman 44277b14db5SEric W. Biederman out: 44377b14db5SEric W. Biederman sysctl_head_finish(head); 44477b14db5SEric W. Biederman return err; 44577b14db5SEric W. Biederman } 44677b14db5SEric W. Biederman 4477708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf, 4487708bfb1SPavel Emelyanov size_t count, loff_t *ppos, int write) 44977b14db5SEric W. Biederman { 4509043476fSAl Viro struct inode *inode = filp->f_path.dentry->d_inode; 4519043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 4529043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 4532a2da53bSDavid Howells ssize_t error; 4542a2da53bSDavid Howells size_t res; 45577b14db5SEric W. Biederman 4569043476fSAl Viro if (IS_ERR(head)) 4579043476fSAl Viro return PTR_ERR(head); 45877b14db5SEric W. Biederman 45977b14db5SEric W. Biederman /* 46077b14db5SEric W. Biederman * At this point we know that the sysctl was not unregistered 46177b14db5SEric W. Biederman * and won't be until we finish. 46277b14db5SEric W. Biederman */ 46377b14db5SEric W. Biederman error = -EPERM; 464d7321cd6SPavel Emelyanov if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ)) 46577b14db5SEric W. Biederman goto out; 46677b14db5SEric W. Biederman 4679043476fSAl Viro /* if that can happen at all, it should be -EINVAL, not -EISDIR */ 4689043476fSAl Viro error = -EINVAL; 4699043476fSAl Viro if (!table->proc_handler) 4709043476fSAl Viro goto out; 4719043476fSAl Viro 47277b14db5SEric W. Biederman /* careful: calling conventions are nasty here */ 47377b14db5SEric W. Biederman res = count; 4748d65af78SAlexey Dobriyan error = table->proc_handler(table, write, buf, &res, ppos); 47577b14db5SEric W. Biederman if (!error) 47677b14db5SEric W. Biederman error = res; 47777b14db5SEric W. Biederman out: 47877b14db5SEric W. Biederman sysctl_head_finish(head); 47977b14db5SEric W. Biederman 48077b14db5SEric W. Biederman return error; 48177b14db5SEric W. Biederman } 48277b14db5SEric W. Biederman 4837708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf, 4847708bfb1SPavel Emelyanov size_t count, loff_t *ppos) 4857708bfb1SPavel Emelyanov { 4867708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0); 4877708bfb1SPavel Emelyanov } 4887708bfb1SPavel Emelyanov 48977b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf, 49077b14db5SEric W. Biederman size_t count, loff_t *ppos) 49177b14db5SEric W. Biederman { 4927708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1); 49377b14db5SEric W. Biederman } 49477b14db5SEric W. Biederman 495f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp) 496f1ecf068SLucas De Marchi { 497f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 498f1ecf068SLucas De Marchi 499f1ecf068SLucas De Marchi if (table->poll) 500f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 501f1ecf068SLucas De Marchi 502f1ecf068SLucas De Marchi return 0; 503f1ecf068SLucas De Marchi } 504f1ecf068SLucas De Marchi 505f1ecf068SLucas De Marchi static unsigned int proc_sys_poll(struct file *filp, poll_table *wait) 506f1ecf068SLucas De Marchi { 507f1ecf068SLucas De Marchi struct inode *inode = filp->f_path.dentry->d_inode; 508f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 509f1ecf068SLucas De Marchi unsigned long event = (unsigned long)filp->private_data; 510f1ecf068SLucas De Marchi unsigned int ret = DEFAULT_POLLMASK; 511f1ecf068SLucas De Marchi 512f1ecf068SLucas De Marchi if (!table->proc_handler) 513f1ecf068SLucas De Marchi goto out; 514f1ecf068SLucas De Marchi 515f1ecf068SLucas De Marchi if (!table->poll) 516f1ecf068SLucas De Marchi goto out; 517f1ecf068SLucas De Marchi 518f1ecf068SLucas De Marchi poll_wait(filp, &table->poll->wait, wait); 519f1ecf068SLucas De Marchi 520f1ecf068SLucas De Marchi if (event != atomic_read(&table->poll->event)) { 521f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 522f1ecf068SLucas De Marchi ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI; 523f1ecf068SLucas De Marchi } 524f1ecf068SLucas De Marchi 525f1ecf068SLucas De Marchi out: 526f1ecf068SLucas De Marchi return ret; 527f1ecf068SLucas De Marchi } 52877b14db5SEric W. Biederman 52977b14db5SEric W. Biederman static int proc_sys_fill_cache(struct file *filp, void *dirent, 5309043476fSAl Viro filldir_t filldir, 5319043476fSAl Viro struct ctl_table_header *head, 5329043476fSAl Viro struct ctl_table *table) 53377b14db5SEric W. Biederman { 53477b14db5SEric W. Biederman struct dentry *child, *dir = filp->f_path.dentry; 53577b14db5SEric W. Biederman struct inode *inode; 53677b14db5SEric W. Biederman struct qstr qname; 53777b14db5SEric W. Biederman ino_t ino = 0; 53877b14db5SEric W. Biederman unsigned type = DT_UNKNOWN; 53977b14db5SEric W. Biederman 54077b14db5SEric W. Biederman qname.name = table->procname; 54177b14db5SEric W. Biederman qname.len = strlen(table->procname); 54277b14db5SEric W. Biederman qname.hash = full_name_hash(qname.name, qname.len); 54377b14db5SEric W. Biederman 54477b14db5SEric W. Biederman child = d_lookup(dir, &qname); 54577b14db5SEric W. Biederman if (!child) { 5469043476fSAl Viro child = d_alloc(dir, &qname); 5479043476fSAl Viro if (child) { 5489043476fSAl Viro inode = proc_sys_make_inode(dir->d_sb, head, table); 5499043476fSAl Viro if (!inode) { 5509043476fSAl Viro dput(child); 5519043476fSAl Viro return -ENOMEM; 5529043476fSAl Viro } else { 553fb045adbSNick Piggin d_set_d_op(child, &proc_sys_dentry_operations); 5549043476fSAl Viro d_add(child, inode); 55577b14db5SEric W. Biederman } 5569043476fSAl Viro } else { 5579043476fSAl Viro return -ENOMEM; 55877b14db5SEric W. Biederman } 55977b14db5SEric W. Biederman } 56077b14db5SEric W. Biederman inode = child->d_inode; 56177b14db5SEric W. Biederman ino = inode->i_ino; 56277b14db5SEric W. Biederman type = inode->i_mode >> 12; 56377b14db5SEric W. Biederman dput(child); 5649043476fSAl Viro return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type); 5659043476fSAl Viro } 5669043476fSAl Viro 5679043476fSAl Viro static int scan(struct ctl_table_header *head, ctl_table *table, 5689043476fSAl Viro unsigned long *pos, struct file *file, 5699043476fSAl Viro void *dirent, filldir_t filldir) 5709043476fSAl Viro { 5719043476fSAl Viro int res; 5729043476fSAl Viro 5736a75ce16SEric W. Biederman if ((*pos)++ < file->f_pos) 5746a75ce16SEric W. Biederman return 0; 5759043476fSAl Viro 5769043476fSAl Viro res = proc_sys_fill_cache(file, dirent, filldir, head, table); 5779043476fSAl Viro 5786a75ce16SEric W. Biederman if (res == 0) 5796a75ce16SEric W. Biederman file->f_pos = *pos; 5806a75ce16SEric W. Biederman 5816a75ce16SEric W. Biederman return res; 58277b14db5SEric W. Biederman } 58377b14db5SEric W. Biederman 58477b14db5SEric W. Biederman static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir) 58577b14db5SEric W. Biederman { 5869043476fSAl Viro struct dentry *dentry = filp->f_path.dentry; 58777b14db5SEric W. Biederman struct inode *inode = dentry->d_inode; 5889043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 5899043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 5909043476fSAl Viro struct ctl_table_header *h = NULL; 5916a75ce16SEric W. Biederman struct ctl_table *entry; 59277b14db5SEric W. Biederman unsigned long pos; 5939043476fSAl Viro int ret = -EINVAL; 59477b14db5SEric W. Biederman 5959043476fSAl Viro if (IS_ERR(head)) 5969043476fSAl Viro return PTR_ERR(head); 5979043476fSAl Viro 5989043476fSAl Viro if (table && !table->child) { 5999043476fSAl Viro WARN_ON(1); 60077b14db5SEric W. Biederman goto out; 6019043476fSAl Viro } 6029043476fSAl Viro 603a194558eSEric W. Biederman table = table ? table->child : &head->ctl_table[1]; 60477b14db5SEric W. Biederman 60577b14db5SEric W. Biederman ret = 0; 60677b14db5SEric W. Biederman /* Avoid a switch here: arm builds fail with missing __cmpdi2 */ 60777b14db5SEric W. Biederman if (filp->f_pos == 0) { 60877b14db5SEric W. Biederman if (filldir(dirent, ".", 1, filp->f_pos, 60977b14db5SEric W. Biederman inode->i_ino, DT_DIR) < 0) 61077b14db5SEric W. Biederman goto out; 61177b14db5SEric W. Biederman filp->f_pos++; 61277b14db5SEric W. Biederman } 61377b14db5SEric W. Biederman if (filp->f_pos == 1) { 61477b14db5SEric W. Biederman if (filldir(dirent, "..", 2, filp->f_pos, 61577b14db5SEric W. Biederman parent_ino(dentry), DT_DIR) < 0) 61677b14db5SEric W. Biederman goto out; 61777b14db5SEric W. Biederman filp->f_pos++; 61877b14db5SEric W. Biederman } 61977b14db5SEric W. Biederman pos = 2; 62077b14db5SEric W. Biederman 6216a75ce16SEric W. Biederman for (first_entry(head, table, &h, &entry); h; next_entry(table, &h, &entry)) { 6226a75ce16SEric W. Biederman ret = scan(h, entry, &pos, filp, dirent, filldir); 6239043476fSAl Viro if (ret) { 6249043476fSAl Viro sysctl_head_finish(h); 6259043476fSAl Viro break; 62677b14db5SEric W. Biederman } 62777b14db5SEric W. Biederman } 62877b14db5SEric W. Biederman ret = 1; 62977b14db5SEric W. Biederman out: 63077b14db5SEric W. Biederman sysctl_head_finish(head); 63177b14db5SEric W. Biederman return ret; 63277b14db5SEric W. Biederman } 63377b14db5SEric W. Biederman 63410556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask) 63577b14db5SEric W. Biederman { 63677b14db5SEric W. Biederman /* 63777b14db5SEric W. Biederman * sysctl entries that are not writeable, 63877b14db5SEric W. Biederman * are _NOT_ writeable, capabilities or not. 63977b14db5SEric W. Biederman */ 640f696a365SMiklos Szeredi struct ctl_table_header *head; 641f696a365SMiklos Szeredi struct ctl_table *table; 64277b14db5SEric W. Biederman int error; 64377b14db5SEric W. Biederman 644f696a365SMiklos Szeredi /* Executable files are not allowed under /proc/sys/ */ 645f696a365SMiklos Szeredi if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) 646f696a365SMiklos Szeredi return -EACCES; 647f696a365SMiklos Szeredi 648f696a365SMiklos Szeredi head = grab_header(inode); 6499043476fSAl Viro if (IS_ERR(head)) 6509043476fSAl Viro return PTR_ERR(head); 65177b14db5SEric W. Biederman 652f696a365SMiklos Szeredi table = PROC_I(inode)->sysctl_entry; 6539043476fSAl Viro if (!table) /* global root - r-xr-xr-x */ 6549043476fSAl Viro error = mask & MAY_WRITE ? -EACCES : 0; 6559043476fSAl Viro else /* Use the permissions on the sysctl table entry */ 6561fc0f78cSAl Viro error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK); 6579043476fSAl Viro 65877b14db5SEric W. Biederman sysctl_head_finish(head); 65977b14db5SEric W. Biederman return error; 66077b14db5SEric W. Biederman } 66177b14db5SEric W. Biederman 66277b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr) 66377b14db5SEric W. Biederman { 66477b14db5SEric W. Biederman struct inode *inode = dentry->d_inode; 66577b14db5SEric W. Biederman int error; 66677b14db5SEric W. Biederman 66777b14db5SEric W. Biederman if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) 66877b14db5SEric W. Biederman return -EPERM; 66977b14db5SEric W. Biederman 67077b14db5SEric W. Biederman error = inode_change_ok(inode, attr); 6711025774cSChristoph Hellwig if (error) 67277b14db5SEric W. Biederman return error; 6731025774cSChristoph Hellwig 6741025774cSChristoph Hellwig if ((attr->ia_valid & ATTR_SIZE) && 6751025774cSChristoph Hellwig attr->ia_size != i_size_read(inode)) { 6761025774cSChristoph Hellwig error = vmtruncate(inode, attr->ia_size); 6771025774cSChristoph Hellwig if (error) 6781025774cSChristoph Hellwig return error; 6791025774cSChristoph Hellwig } 6801025774cSChristoph Hellwig 6811025774cSChristoph Hellwig setattr_copy(inode, attr); 6821025774cSChristoph Hellwig mark_inode_dirty(inode); 6831025774cSChristoph Hellwig return 0; 68477b14db5SEric W. Biederman } 68577b14db5SEric W. Biederman 6869043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 6879043476fSAl Viro { 6889043476fSAl Viro struct inode *inode = dentry->d_inode; 6899043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 6909043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 6919043476fSAl Viro 6929043476fSAl Viro if (IS_ERR(head)) 6939043476fSAl Viro return PTR_ERR(head); 6949043476fSAl Viro 6959043476fSAl Viro generic_fillattr(inode, stat); 6969043476fSAl Viro if (table) 6979043476fSAl Viro stat->mode = (stat->mode & S_IFMT) | table->mode; 6989043476fSAl Viro 6999043476fSAl Viro sysctl_head_finish(head); 7009043476fSAl Viro return 0; 7019043476fSAl Viro } 7029043476fSAl Viro 70377b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = { 704f1ecf068SLucas De Marchi .open = proc_sys_open, 705f1ecf068SLucas De Marchi .poll = proc_sys_poll, 70677b14db5SEric W. Biederman .read = proc_sys_read, 70777b14db5SEric W. Biederman .write = proc_sys_write, 7086038f373SArnd Bergmann .llseek = default_llseek, 7099043476fSAl Viro }; 7109043476fSAl Viro 7119043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = { 712887df078SPavel Emelyanov .read = generic_read_dir, 71377b14db5SEric W. Biederman .readdir = proc_sys_readdir, 7143222a3e5SChristoph Hellwig .llseek = generic_file_llseek, 71577b14db5SEric W. Biederman }; 71677b14db5SEric W. Biederman 71703a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = { 7189043476fSAl Viro .permission = proc_sys_permission, 7199043476fSAl Viro .setattr = proc_sys_setattr, 7209043476fSAl Viro .getattr = proc_sys_getattr, 7219043476fSAl Viro }; 7229043476fSAl Viro 7239043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = { 72477b14db5SEric W. Biederman .lookup = proc_sys_lookup, 72577b14db5SEric W. Biederman .permission = proc_sys_permission, 72677b14db5SEric W. Biederman .setattr = proc_sys_setattr, 7279043476fSAl Viro .getattr = proc_sys_getattr, 72877b14db5SEric W. Biederman }; 72977b14db5SEric W. Biederman 73077b14db5SEric W. Biederman static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd) 73177b14db5SEric W. Biederman { 73234286d66SNick Piggin if (nd->flags & LOOKUP_RCU) 73334286d66SNick Piggin return -ECHILD; 7349043476fSAl Viro return !PROC_I(dentry->d_inode)->sysctl->unregistering; 7359043476fSAl Viro } 7369043476fSAl Viro 737fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry) 7389043476fSAl Viro { 7399043476fSAl Viro return !!PROC_I(dentry->d_inode)->sysctl->unregistering; 7409043476fSAl Viro } 7419043476fSAl Viro 7421f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p) 7431f87f0b5SEric W. Biederman { 7441f87f0b5SEric W. Biederman struct ctl_table_set *set = p->set; 7451f87f0b5SEric W. Biederman int res; 7461f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 7471f87f0b5SEric W. Biederman if (p->unregistering) 7481f87f0b5SEric W. Biederman res = 0; 7491f87f0b5SEric W. Biederman else if (!set->is_seen) 7501f87f0b5SEric W. Biederman res = 1; 7511f87f0b5SEric W. Biederman else 7521f87f0b5SEric W. Biederman res = set->is_seen(set); 7531f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 7541f87f0b5SEric W. Biederman return res; 7551f87f0b5SEric W. Biederman } 7561f87f0b5SEric W. Biederman 757621e155aSNick Piggin static int proc_sys_compare(const struct dentry *parent, 758621e155aSNick Piggin const struct inode *pinode, 759621e155aSNick Piggin const struct dentry *dentry, const struct inode *inode, 760621e155aSNick Piggin unsigned int len, const char *str, const struct qstr *name) 7619043476fSAl Viro { 762dfef6dcdSAl Viro struct ctl_table_header *head; 76331e6b01fSNick Piggin /* Although proc doesn't have negative dentries, rcu-walk means 76431e6b01fSNick Piggin * that inode here can be NULL */ 765dfef6dcdSAl Viro /* AV: can it, indeed? */ 76631e6b01fSNick Piggin if (!inode) 767dfef6dcdSAl Viro return 1; 768621e155aSNick Piggin if (name->len != len) 7699043476fSAl Viro return 1; 770621e155aSNick Piggin if (memcmp(name->name, str, len)) 7719043476fSAl Viro return 1; 772dfef6dcdSAl Viro head = rcu_dereference(PROC_I(inode)->sysctl); 773dfef6dcdSAl Viro return !head || !sysctl_is_seen(head); 77477b14db5SEric W. Biederman } 77577b14db5SEric W. Biederman 776d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = { 77777b14db5SEric W. Biederman .d_revalidate = proc_sys_revalidate, 7789043476fSAl Viro .d_delete = proc_sys_delete, 7799043476fSAl Viro .d_compare = proc_sys_compare, 78077b14db5SEric W. Biederman }; 78177b14db5SEric W. Biederman 7821f87f0b5SEric W. Biederman static struct ctl_table *is_branch_in(struct ctl_table *branch, 7831f87f0b5SEric W. Biederman struct ctl_table *table) 7841f87f0b5SEric W. Biederman { 7851f87f0b5SEric W. Biederman struct ctl_table *p; 7861f87f0b5SEric W. Biederman const char *s = branch->procname; 7871f87f0b5SEric W. Biederman 7881f87f0b5SEric W. Biederman /* branch should have named subdirectory as its first element */ 7891f87f0b5SEric W. Biederman if (!s || !branch->child) 7901f87f0b5SEric W. Biederman return NULL; 7911f87f0b5SEric W. Biederman 7921f87f0b5SEric W. Biederman /* ... and nothing else */ 7931f87f0b5SEric W. Biederman if (branch[1].procname) 7941f87f0b5SEric W. Biederman return NULL; 7951f87f0b5SEric W. Biederman 7961f87f0b5SEric W. Biederman /* table should contain subdirectory with the same name */ 7971f87f0b5SEric W. Biederman for (p = table; p->procname; p++) { 7981f87f0b5SEric W. Biederman if (!p->child) 7991f87f0b5SEric W. Biederman continue; 8001f87f0b5SEric W. Biederman if (p->procname && strcmp(p->procname, s) == 0) 8011f87f0b5SEric W. Biederman return p; 8021f87f0b5SEric W. Biederman } 8031f87f0b5SEric W. Biederman return NULL; 8041f87f0b5SEric W. Biederman } 8051f87f0b5SEric W. Biederman 8061f87f0b5SEric W. Biederman /* see if attaching q to p would be an improvement */ 8071f87f0b5SEric W. Biederman static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q) 8081f87f0b5SEric W. Biederman { 8091f87f0b5SEric W. Biederman struct ctl_table *to = p->ctl_table, *by = q->ctl_table; 8101f87f0b5SEric W. Biederman struct ctl_table *next; 8111f87f0b5SEric W. Biederman int is_better = 0; 8121f87f0b5SEric W. Biederman int not_in_parent = !p->attached_by; 8131f87f0b5SEric W. Biederman 8141f87f0b5SEric W. Biederman while ((next = is_branch_in(by, to)) != NULL) { 8151f87f0b5SEric W. Biederman if (by == q->attached_by) 8161f87f0b5SEric W. Biederman is_better = 1; 8171f87f0b5SEric W. Biederman if (to == p->attached_by) 8181f87f0b5SEric W. Biederman not_in_parent = 1; 8191f87f0b5SEric W. Biederman by = by->child; 8201f87f0b5SEric W. Biederman to = next->child; 8211f87f0b5SEric W. Biederman } 8221f87f0b5SEric W. Biederman 8231f87f0b5SEric W. Biederman if (is_better && not_in_parent) { 8241f87f0b5SEric W. Biederman q->attached_by = by; 8251f87f0b5SEric W. Biederman q->attached_to = to; 8261f87f0b5SEric W. Biederman q->parent = p; 8271f87f0b5SEric W. Biederman } 8281f87f0b5SEric W. Biederman } 8291f87f0b5SEric W. Biederman 8307c60c48fSEric W. Biederman static int sysctl_check_table_dups(const char *path, struct ctl_table *old, 8311f87f0b5SEric W. Biederman struct ctl_table *table) 8321f87f0b5SEric W. Biederman { 8337c60c48fSEric W. Biederman struct ctl_table *entry, *test; 8341f87f0b5SEric W. Biederman int error = 0; 8351f87f0b5SEric W. Biederman 8367c60c48fSEric W. Biederman for (entry = old; entry->procname; entry++) { 8377c60c48fSEric W. Biederman for (test = table; test->procname; test++) { 8387c60c48fSEric W. Biederman if (strcmp(entry->procname, test->procname) == 0) { 8397c60c48fSEric W. Biederman printk(KERN_ERR "sysctl duplicate entry: %s/%s\n", 8407c60c48fSEric W. Biederman path, test->procname); 8417c60c48fSEric W. Biederman error = -EEXIST; 8421f87f0b5SEric W. Biederman } 8437c60c48fSEric W. Biederman } 8447c60c48fSEric W. Biederman } 8457c60c48fSEric W. Biederman return error; 8467c60c48fSEric W. Biederman } 8477c60c48fSEric W. Biederman 8487c60c48fSEric W. Biederman static int sysctl_check_dups(struct nsproxy *namespaces, 8497c60c48fSEric W. Biederman struct ctl_table_header *header, 8507c60c48fSEric W. Biederman const char *path, struct ctl_table *table) 8517c60c48fSEric W. Biederman { 8527c60c48fSEric W. Biederman struct ctl_table_root *root; 8537c60c48fSEric W. Biederman struct ctl_table_set *set; 8547c60c48fSEric W. Biederman struct ctl_table_header *dir_head, *head; 8557c60c48fSEric W. Biederman struct ctl_table *dir_table; 8567c60c48fSEric W. Biederman int error = 0; 8577c60c48fSEric W. Biederman 8587c60c48fSEric W. Biederman /* No dups if we are the only member of our directory */ 8597c60c48fSEric W. Biederman if (header->attached_by != table) 8607c60c48fSEric W. Biederman return 0; 8617c60c48fSEric W. Biederman 8627c60c48fSEric W. Biederman dir_head = header->parent; 8637c60c48fSEric W. Biederman dir_table = header->attached_to; 8647c60c48fSEric W. Biederman 8657c60c48fSEric W. Biederman error = sysctl_check_table_dups(path, dir_table, table); 8667c60c48fSEric W. Biederman 8677c60c48fSEric W. Biederman root = &sysctl_table_root; 8687c60c48fSEric W. Biederman do { 8697c60c48fSEric W. Biederman set = lookup_header_set(root, namespaces); 8707c60c48fSEric W. Biederman 8717c60c48fSEric W. Biederman list_for_each_entry(head, &set->list, ctl_entry) { 8727c60c48fSEric W. Biederman if (head->unregistering) 8737c60c48fSEric W. Biederman continue; 8747c60c48fSEric W. Biederman if (head->attached_to != dir_table) 8757c60c48fSEric W. Biederman continue; 8767c60c48fSEric W. Biederman error = sysctl_check_table_dups(path, head->attached_by, 8777c60c48fSEric W. Biederman table); 8787c60c48fSEric W. Biederman } 8797c60c48fSEric W. Biederman root = list_entry(root->root_list.next, 8807c60c48fSEric W. Biederman struct ctl_table_root, root_list); 8817c60c48fSEric W. Biederman } while (root != &sysctl_table_root); 8827c60c48fSEric W. Biederman return error; 8837c60c48fSEric W. Biederman } 8847c60c48fSEric W. Biederman 8857c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...) 8867c60c48fSEric W. Biederman { 8877c60c48fSEric W. Biederman struct va_format vaf; 8887c60c48fSEric W. Biederman va_list args; 8897c60c48fSEric W. Biederman 8907c60c48fSEric W. Biederman va_start(args, fmt); 8917c60c48fSEric W. Biederman vaf.fmt = fmt; 8927c60c48fSEric W. Biederman vaf.va = &args; 8937c60c48fSEric W. Biederman 8947c60c48fSEric W. Biederman printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n", 8957c60c48fSEric W. Biederman path, table->procname, &vaf); 8967c60c48fSEric W. Biederman 8977c60c48fSEric W. Biederman va_end(args); 8987c60c48fSEric W. Biederman return -EINVAL; 8997c60c48fSEric W. Biederman } 9007c60c48fSEric W. Biederman 9017c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table) 9027c60c48fSEric W. Biederman { 9037c60c48fSEric W. Biederman int err = 0; 9047c60c48fSEric W. Biederman for (; table->procname; table++) { 9057c60c48fSEric W. Biederman if (table->child) 9067c60c48fSEric W. Biederman err = sysctl_err(path, table, "Not a file"); 9077c60c48fSEric W. Biederman 9081f87f0b5SEric W. Biederman if ((table->proc_handler == proc_dostring) || 9091f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec) || 9101f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_minmax) || 9111f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_jiffies) || 9121f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_userhz_jiffies) || 9131f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_ms_jiffies) || 9141f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_minmax) || 9151f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) { 9161f87f0b5SEric W. Biederman if (!table->data) 9177c60c48fSEric W. Biederman err = sysctl_err(path, table, "No data"); 9181f87f0b5SEric W. Biederman if (!table->maxlen) 9197c60c48fSEric W. Biederman err = sysctl_err(path, table, "No maxlen"); 9201f87f0b5SEric W. Biederman } 9211f87f0b5SEric W. Biederman if (!table->proc_handler) 9227c60c48fSEric W. Biederman err = sysctl_err(path, table, "No proc_handler"); 9237c60c48fSEric W. Biederman 9247c60c48fSEric W. Biederman if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode) 9257c60c48fSEric W. Biederman err = sysctl_err(path, table, "bogus .mode 0%o", 9267c60c48fSEric W. Biederman table->mode); 9271f87f0b5SEric W. Biederman } 9287c60c48fSEric W. Biederman return err; 9291f87f0b5SEric W. Biederman } 9301f87f0b5SEric W. Biederman 9311f87f0b5SEric W. Biederman /** 932f728019bSEric W. Biederman * __register_sysctl_table - register a leaf sysctl table 9331f87f0b5SEric W. Biederman * @root: List of sysctl headers to register on 9341f87f0b5SEric W. Biederman * @namespaces: Data to compute which lists of sysctl entries are visible 9351f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 9361f87f0b5SEric W. Biederman * @table: the top-level table structure 9371f87f0b5SEric W. Biederman * 9381f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 9391f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 9401f87f0b5SEric W. Biederman * 9411f87f0b5SEric W. Biederman * The members of the &struct ctl_table structure are used as follows: 9421f87f0b5SEric W. Biederman * 9431f87f0b5SEric W. Biederman * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not 9441f87f0b5SEric W. Biederman * enter a sysctl file 9451f87f0b5SEric W. Biederman * 9461f87f0b5SEric W. Biederman * data - a pointer to data for use by proc_handler 9471f87f0b5SEric W. Biederman * 9481f87f0b5SEric W. Biederman * maxlen - the maximum size in bytes of the data 9491f87f0b5SEric W. Biederman * 950f728019bSEric W. Biederman * mode - the file permissions for the /proc/sys file 9511f87f0b5SEric W. Biederman * 952f728019bSEric W. Biederman * child - must be %NULL. 9531f87f0b5SEric W. Biederman * 9541f87f0b5SEric W. Biederman * proc_handler - the text handler routine (described below) 9551f87f0b5SEric W. Biederman * 9561f87f0b5SEric W. Biederman * extra1, extra2 - extra pointers usable by the proc handler routines 9571f87f0b5SEric W. Biederman * 9581f87f0b5SEric W. Biederman * Leaf nodes in the sysctl tree will be represented by a single file 9591f87f0b5SEric W. Biederman * under /proc; non-leaf nodes will be represented by directories. 9601f87f0b5SEric W. Biederman * 961f728019bSEric W. Biederman * There must be a proc_handler routine for any terminal nodes. 962f728019bSEric W. Biederman * Several default handlers are available to cover common cases - 9631f87f0b5SEric W. Biederman * 9641f87f0b5SEric W. Biederman * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(), 9651f87f0b5SEric W. Biederman * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), 9661f87f0b5SEric W. Biederman * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax() 9671f87f0b5SEric W. Biederman * 9681f87f0b5SEric W. Biederman * It is the handler's job to read the input buffer from user memory 9691f87f0b5SEric W. Biederman * and process it. The handler should return 0 on success. 9701f87f0b5SEric W. Biederman * 9711f87f0b5SEric W. Biederman * This routine returns %NULL on a failure to register, and a pointer 9721f87f0b5SEric W. Biederman * to the table header on success. 9731f87f0b5SEric W. Biederman */ 9746e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table( 9751f87f0b5SEric W. Biederman struct ctl_table_root *root, 9761f87f0b5SEric W. Biederman struct nsproxy *namespaces, 9776e9d5164SEric W. Biederman const char *path, struct ctl_table *table) 9781f87f0b5SEric W. Biederman { 9791f87f0b5SEric W. Biederman struct ctl_table_header *header; 9801f87f0b5SEric W. Biederman struct ctl_table *new, **prevp; 9816e9d5164SEric W. Biederman const char *name, *nextname; 9826e9d5164SEric W. Biederman unsigned int npath = 0; 9831f87f0b5SEric W. Biederman struct ctl_table_set *set; 984f05e53a7SEric W. Biederman size_t path_bytes = 0; 985f05e53a7SEric W. Biederman char *new_name; 9861f87f0b5SEric W. Biederman 9871f87f0b5SEric W. Biederman /* Count the path components */ 9886e9d5164SEric W. Biederman for (name = path; name; name = nextname) { 9896e9d5164SEric W. Biederman int namelen; 9906e9d5164SEric W. Biederman nextname = strchr(name, '/'); 9916e9d5164SEric W. Biederman if (nextname) { 9926e9d5164SEric W. Biederman namelen = nextname - name; 9936e9d5164SEric W. Biederman nextname++; 9946e9d5164SEric W. Biederman } else { 9956e9d5164SEric W. Biederman namelen = strlen(name); 9966e9d5164SEric W. Biederman } 9976e9d5164SEric W. Biederman if (namelen == 0) 9986e9d5164SEric W. Biederman continue; 9996e9d5164SEric W. Biederman path_bytes += namelen + 1; 10006e9d5164SEric W. Biederman npath++; 10016e9d5164SEric W. Biederman } 10021f87f0b5SEric W. Biederman 10031f87f0b5SEric W. Biederman /* 10041f87f0b5SEric W. Biederman * For each path component, allocate a 2-element ctl_table array. 10051f87f0b5SEric W. Biederman * The first array element will be filled with the sysctl entry 10061f87f0b5SEric W. Biederman * for this, the second will be the sentinel (procname == 0). 10071f87f0b5SEric W. Biederman * 10081f87f0b5SEric W. Biederman * We allocate everything in one go so that we don't have to 10091f87f0b5SEric W. Biederman * worry about freeing additional memory in unregister_sysctl_table. 10101f87f0b5SEric W. Biederman */ 1011f05e53a7SEric W. Biederman header = kzalloc(sizeof(struct ctl_table_header) + path_bytes + 10121f87f0b5SEric W. Biederman (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL); 10131f87f0b5SEric W. Biederman if (!header) 10141f87f0b5SEric W. Biederman return NULL; 10151f87f0b5SEric W. Biederman 10161f87f0b5SEric W. Biederman new = (struct ctl_table *) (header + 1); 1017f05e53a7SEric W. Biederman new_name = (char *)(new + (2 * npath)); 10181f87f0b5SEric W. Biederman 10191f87f0b5SEric W. Biederman /* Now connect the dots */ 10201f87f0b5SEric W. Biederman prevp = &header->ctl_table; 10216e9d5164SEric W. Biederman for (name = path; name; name = nextname) { 10226e9d5164SEric W. Biederman int namelen; 10236e9d5164SEric W. Biederman nextname = strchr(name, '/'); 10246e9d5164SEric W. Biederman if (nextname) { 10256e9d5164SEric W. Biederman namelen = nextname - name; 10266e9d5164SEric W. Biederman nextname++; 10276e9d5164SEric W. Biederman } else { 10286e9d5164SEric W. Biederman namelen = strlen(name); 10296e9d5164SEric W. Biederman } 10306e9d5164SEric W. Biederman if (namelen == 0) 10316e9d5164SEric W. Biederman continue; 10326e9d5164SEric W. Biederman memcpy(new_name, name, namelen); 10336e9d5164SEric W. Biederman new_name[namelen] = '\0'; 10346e9d5164SEric W. Biederman 1035f05e53a7SEric W. Biederman new->procname = new_name; 10361f87f0b5SEric W. Biederman new->mode = 0555; 10371f87f0b5SEric W. Biederman 10381f87f0b5SEric W. Biederman *prevp = new; 10391f87f0b5SEric W. Biederman prevp = &new->child; 10401f87f0b5SEric W. Biederman 10411f87f0b5SEric W. Biederman new += 2; 10426e9d5164SEric W. Biederman new_name += namelen + 1; 10431f87f0b5SEric W. Biederman } 10441f87f0b5SEric W. Biederman *prevp = table; 10451f87f0b5SEric W. Biederman 1046e0d04529SEric W. Biederman init_header(header, root, NULL, table); 10477c60c48fSEric W. Biederman if (sysctl_check_table(path, table)) 10487c60c48fSEric W. Biederman goto fail; 10498d6ecfccSEric W. Biederman 10501f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 10511f87f0b5SEric W. Biederman header->set = lookup_header_set(root, namespaces); 10521f87f0b5SEric W. Biederman header->attached_by = header->ctl_table; 1053a194558eSEric W. Biederman header->attached_to = &root_table[1]; 10541f87f0b5SEric W. Biederman header->parent = &root_table_header; 1055bd295b56SEric W. Biederman set = header->set; 1056bd295b56SEric W. Biederman root = header->root; 1057bd295b56SEric W. Biederman for (;;) { 10581f87f0b5SEric W. Biederman struct ctl_table_header *p; 10591f87f0b5SEric W. Biederman list_for_each_entry(p, &set->list, ctl_entry) { 10601f87f0b5SEric W. Biederman if (p->unregistering) 10611f87f0b5SEric W. Biederman continue; 10621f87f0b5SEric W. Biederman try_attach(p, header); 10631f87f0b5SEric W. Biederman } 1064bd295b56SEric W. Biederman if (root == &sysctl_table_root) 1065bd295b56SEric W. Biederman break; 1066bd295b56SEric W. Biederman root = list_entry(root->root_list.prev, 1067bd295b56SEric W. Biederman struct ctl_table_root, root_list); 1068bd295b56SEric W. Biederman set = lookup_header_set(root, namespaces); 10691f87f0b5SEric W. Biederman } 10707c60c48fSEric W. Biederman if (sysctl_check_dups(namespaces, header, path, table)) 10717c60c48fSEric W. Biederman goto fail_locked; 10728425d6aaSEric W. Biederman insert_header(header); 10731f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 10741f87f0b5SEric W. Biederman 10751f87f0b5SEric W. Biederman return header; 10767c60c48fSEric W. Biederman fail_locked: 10777c60c48fSEric W. Biederman spin_unlock(&sysctl_lock); 10787c60c48fSEric W. Biederman fail: 10797c60c48fSEric W. Biederman kfree(header); 10807c60c48fSEric W. Biederman dump_stack(); 10817c60c48fSEric W. Biederman return NULL; 10821f87f0b5SEric W. Biederman } 10831f87f0b5SEric W. Biederman 10846e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name) 10856e9d5164SEric W. Biederman { 10866e9d5164SEric W. Biederman int namelen; 10876e9d5164SEric W. Biederman namelen = strlen(name); 10886e9d5164SEric W. Biederman if (((pos - path) + namelen + 2) >= PATH_MAX) 10896e9d5164SEric W. Biederman return NULL; 10906e9d5164SEric W. Biederman memcpy(pos, name, namelen); 10916e9d5164SEric W. Biederman pos[namelen] = '/'; 10926e9d5164SEric W. Biederman pos[namelen + 1] = '\0'; 10936e9d5164SEric W. Biederman pos += namelen + 1; 10946e9d5164SEric W. Biederman return pos; 10956e9d5164SEric W. Biederman } 10966e9d5164SEric W. Biederman 1097f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table) 1098f728019bSEric W. Biederman { 1099f728019bSEric W. Biederman int has_files = 0; 1100f728019bSEric W. Biederman int nr_subheaders = 0; 1101f728019bSEric W. Biederman struct ctl_table *entry; 1102f728019bSEric W. Biederman 1103f728019bSEric W. Biederman /* special case: no directory and empty directory */ 1104f728019bSEric W. Biederman if (!table || !table->procname) 1105f728019bSEric W. Biederman return 1; 1106f728019bSEric W. Biederman 1107f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1108f728019bSEric W. Biederman if (entry->child) 1109f728019bSEric W. Biederman nr_subheaders += count_subheaders(entry->child); 1110f728019bSEric W. Biederman else 1111f728019bSEric W. Biederman has_files = 1; 1112f728019bSEric W. Biederman } 1113f728019bSEric W. Biederman return nr_subheaders + has_files; 1114f728019bSEric W. Biederman } 1115f728019bSEric W. Biederman 1116f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos, 1117f728019bSEric W. Biederman struct ctl_table_header ***subheader, 1118f728019bSEric W. Biederman struct ctl_table_root *root, struct nsproxy *namespaces, 1119f728019bSEric W. Biederman struct ctl_table *table) 1120f728019bSEric W. Biederman { 1121f728019bSEric W. Biederman struct ctl_table *ctl_table_arg = NULL; 1122f728019bSEric W. Biederman struct ctl_table *entry, *files; 1123f728019bSEric W. Biederman int nr_files = 0; 1124f728019bSEric W. Biederman int nr_dirs = 0; 1125f728019bSEric W. Biederman int err = -ENOMEM; 1126f728019bSEric W. Biederman 1127f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1128f728019bSEric W. Biederman if (entry->child) 1129f728019bSEric W. Biederman nr_dirs++; 1130f728019bSEric W. Biederman else 1131f728019bSEric W. Biederman nr_files++; 1132f728019bSEric W. Biederman } 1133f728019bSEric W. Biederman 1134f728019bSEric W. Biederman files = table; 1135f728019bSEric W. Biederman /* If there are mixed files and directories we need a new table */ 1136f728019bSEric W. Biederman if (nr_dirs && nr_files) { 1137f728019bSEric W. Biederman struct ctl_table *new; 1138f728019bSEric W. Biederman files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1), 1139f728019bSEric W. Biederman GFP_KERNEL); 1140f728019bSEric W. Biederman if (!files) 1141f728019bSEric W. Biederman goto out; 1142f728019bSEric W. Biederman 1143f728019bSEric W. Biederman ctl_table_arg = files; 1144f728019bSEric W. Biederman for (new = files, entry = table; entry->procname; entry++) { 1145f728019bSEric W. Biederman if (entry->child) 1146f728019bSEric W. Biederman continue; 1147f728019bSEric W. Biederman *new = *entry; 1148f728019bSEric W. Biederman new++; 1149f728019bSEric W. Biederman } 1150f728019bSEric W. Biederman } 1151f728019bSEric W. Biederman 1152f728019bSEric W. Biederman /* Register everything except a directory full of subdirectories */ 1153f728019bSEric W. Biederman if (nr_files || !nr_dirs) { 1154f728019bSEric W. Biederman struct ctl_table_header *header; 1155f728019bSEric W. Biederman header = __register_sysctl_table(root, namespaces, path, files); 1156f728019bSEric W. Biederman if (!header) { 1157f728019bSEric W. Biederman kfree(ctl_table_arg); 1158f728019bSEric W. Biederman goto out; 1159f728019bSEric W. Biederman } 1160f728019bSEric W. Biederman 1161f728019bSEric W. Biederman /* Remember if we need to free the file table */ 1162f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1163f728019bSEric W. Biederman **subheader = header; 1164f728019bSEric W. Biederman (*subheader)++; 1165f728019bSEric W. Biederman } 1166f728019bSEric W. Biederman 1167f728019bSEric W. Biederman /* Recurse into the subdirectories. */ 1168f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1169f728019bSEric W. Biederman char *child_pos; 1170f728019bSEric W. Biederman 1171f728019bSEric W. Biederman if (!entry->child) 1172f728019bSEric W. Biederman continue; 1173f728019bSEric W. Biederman 1174f728019bSEric W. Biederman err = -ENAMETOOLONG; 1175f728019bSEric W. Biederman child_pos = append_path(path, pos, entry->procname); 1176f728019bSEric W. Biederman if (!child_pos) 1177f728019bSEric W. Biederman goto out; 1178f728019bSEric W. Biederman 1179f728019bSEric W. Biederman err = register_leaf_sysctl_tables(path, child_pos, subheader, 1180f728019bSEric W. Biederman root, namespaces, entry->child); 1181f728019bSEric W. Biederman pos[0] = '\0'; 1182f728019bSEric W. Biederman if (err) 1183f728019bSEric W. Biederman goto out; 1184f728019bSEric W. Biederman } 1185f728019bSEric W. Biederman err = 0; 1186f728019bSEric W. Biederman out: 1187f728019bSEric W. Biederman /* On failure our caller will unregister all registered subheaders */ 1188f728019bSEric W. Biederman return err; 1189f728019bSEric W. Biederman } 1190f728019bSEric W. Biederman 11916e9d5164SEric W. Biederman /** 11926e9d5164SEric W. Biederman * __register_sysctl_paths - register a sysctl table hierarchy 11936e9d5164SEric W. Biederman * @root: List of sysctl headers to register on 11946e9d5164SEric W. Biederman * @namespaces: Data to compute which lists of sysctl entries are visible 11956e9d5164SEric W. Biederman * @path: The path to the directory the sysctl table is in. 11966e9d5164SEric W. Biederman * @table: the top-level table structure 11976e9d5164SEric W. Biederman * 11986e9d5164SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 11996e9d5164SEric W. Biederman * array. A completely 0 filled entry terminates the table. 12006e9d5164SEric W. Biederman * 12016e9d5164SEric W. Biederman * See __register_sysctl_table for more details. 12026e9d5164SEric W. Biederman */ 12036e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths( 12046e9d5164SEric W. Biederman struct ctl_table_root *root, 12056e9d5164SEric W. Biederman struct nsproxy *namespaces, 12066e9d5164SEric W. Biederman const struct ctl_path *path, struct ctl_table *table) 12076e9d5164SEric W. Biederman { 1208ec6a5266SEric W. Biederman struct ctl_table *ctl_table_arg = table; 1209f728019bSEric W. Biederman int nr_subheaders = count_subheaders(table); 1210f728019bSEric W. Biederman struct ctl_table_header *header = NULL, **subheaders, **subheader; 12116e9d5164SEric W. Biederman const struct ctl_path *component; 12126e9d5164SEric W. Biederman char *new_path, *pos; 12136e9d5164SEric W. Biederman 12146e9d5164SEric W. Biederman pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL); 12156e9d5164SEric W. Biederman if (!new_path) 12166e9d5164SEric W. Biederman return NULL; 12176e9d5164SEric W. Biederman 12186e9d5164SEric W. Biederman pos[0] = '\0'; 12196e9d5164SEric W. Biederman for (component = path; component->procname; component++) { 12206e9d5164SEric W. Biederman pos = append_path(new_path, pos, component->procname); 12216e9d5164SEric W. Biederman if (!pos) 12226e9d5164SEric W. Biederman goto out; 12236e9d5164SEric W. Biederman } 1224ec6a5266SEric W. Biederman while (table->procname && table->child && !table[1].procname) { 1225ec6a5266SEric W. Biederman pos = append_path(new_path, pos, table->procname); 1226ec6a5266SEric W. Biederman if (!pos) 1227ec6a5266SEric W. Biederman goto out; 1228ec6a5266SEric W. Biederman table = table->child; 1229ec6a5266SEric W. Biederman } 1230f728019bSEric W. Biederman if (nr_subheaders == 1) { 12316e9d5164SEric W. Biederman header = __register_sysctl_table(root, namespaces, new_path, table); 1232ec6a5266SEric W. Biederman if (header) 1233ec6a5266SEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1234f728019bSEric W. Biederman } else { 1235f728019bSEric W. Biederman header = kzalloc(sizeof(*header) + 1236f728019bSEric W. Biederman sizeof(*subheaders)*nr_subheaders, GFP_KERNEL); 1237f728019bSEric W. Biederman if (!header) 1238f728019bSEric W. Biederman goto out; 1239f728019bSEric W. Biederman 1240f728019bSEric W. Biederman subheaders = (struct ctl_table_header **) (header + 1); 1241f728019bSEric W. Biederman subheader = subheaders; 1242f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1243f728019bSEric W. Biederman 1244f728019bSEric W. Biederman if (register_leaf_sysctl_tables(new_path, pos, &subheader, 1245f728019bSEric W. Biederman root, namespaces, table)) 1246f728019bSEric W. Biederman goto err_register_leaves; 1247f728019bSEric W. Biederman } 1248f728019bSEric W. Biederman 12496e9d5164SEric W. Biederman out: 12506e9d5164SEric W. Biederman kfree(new_path); 12516e9d5164SEric W. Biederman return header; 1252f728019bSEric W. Biederman 1253f728019bSEric W. Biederman err_register_leaves: 1254f728019bSEric W. Biederman while (subheader > subheaders) { 1255f728019bSEric W. Biederman struct ctl_table_header *subh = *(--subheader); 1256f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1257f728019bSEric W. Biederman unregister_sysctl_table(subh); 1258f728019bSEric W. Biederman kfree(table); 1259f728019bSEric W. Biederman } 1260f728019bSEric W. Biederman kfree(header); 1261f728019bSEric W. Biederman header = NULL; 1262f728019bSEric W. Biederman goto out; 12636e9d5164SEric W. Biederman } 12646e9d5164SEric W. Biederman 12651f87f0b5SEric W. Biederman /** 12661f87f0b5SEric W. Biederman * register_sysctl_table_path - register a sysctl table hierarchy 12671f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 12681f87f0b5SEric W. Biederman * @table: the top-level table structure 12691f87f0b5SEric W. Biederman * 12701f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 12711f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 12721f87f0b5SEric W. Biederman * 12731f87f0b5SEric W. Biederman * See __register_sysctl_paths for more details. 12741f87f0b5SEric W. Biederman */ 12751f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path, 12761f87f0b5SEric W. Biederman struct ctl_table *table) 12771f87f0b5SEric W. Biederman { 12781f87f0b5SEric W. Biederman return __register_sysctl_paths(&sysctl_table_root, current->nsproxy, 12791f87f0b5SEric W. Biederman path, table); 12801f87f0b5SEric W. Biederman } 12811f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths); 12821f87f0b5SEric W. Biederman 12831f87f0b5SEric W. Biederman /** 12841f87f0b5SEric W. Biederman * register_sysctl_table - register a sysctl table hierarchy 12851f87f0b5SEric W. Biederman * @table: the top-level table structure 12861f87f0b5SEric W. Biederman * 12871f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 12881f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 12891f87f0b5SEric W. Biederman * 12901f87f0b5SEric W. Biederman * See register_sysctl_paths for more details. 12911f87f0b5SEric W. Biederman */ 12921f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table) 12931f87f0b5SEric W. Biederman { 12941f87f0b5SEric W. Biederman static const struct ctl_path null_path[] = { {} }; 12951f87f0b5SEric W. Biederman 12961f87f0b5SEric W. Biederman return register_sysctl_paths(null_path, table); 12971f87f0b5SEric W. Biederman } 12981f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table); 12991f87f0b5SEric W. Biederman 1300938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header) 1301938aaa4fSEric W. Biederman { 1302938aaa4fSEric W. Biederman if (--header->nreg) 1303938aaa4fSEric W. Biederman return; 1304938aaa4fSEric W. Biederman 1305938aaa4fSEric W. Biederman start_unregistering(header); 1306938aaa4fSEric W. Biederman if (!--header->parent->count) { 1307938aaa4fSEric W. Biederman WARN_ON(1); 1308938aaa4fSEric W. Biederman kfree_rcu(header->parent, rcu); 1309938aaa4fSEric W. Biederman } 1310938aaa4fSEric W. Biederman if (!--header->count) 1311938aaa4fSEric W. Biederman kfree_rcu(header, rcu); 1312938aaa4fSEric W. Biederman } 1313938aaa4fSEric W. Biederman 13141f87f0b5SEric W. Biederman /** 13151f87f0b5SEric W. Biederman * unregister_sysctl_table - unregister a sysctl table hierarchy 13161f87f0b5SEric W. Biederman * @header: the header returned from register_sysctl_table 13171f87f0b5SEric W. Biederman * 13181f87f0b5SEric W. Biederman * Unregisters the sysctl table and all children. proc entries may not 13191f87f0b5SEric W. Biederman * actually be removed until they are no longer used by anyone. 13201f87f0b5SEric W. Biederman */ 13211f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header) 13221f87f0b5SEric W. Biederman { 1323f728019bSEric W. Biederman int nr_subheaders; 13241f87f0b5SEric W. Biederman might_sleep(); 13251f87f0b5SEric W. Biederman 13261f87f0b5SEric W. Biederman if (header == NULL) 13271f87f0b5SEric W. Biederman return; 13281f87f0b5SEric W. Biederman 1329f728019bSEric W. Biederman nr_subheaders = count_subheaders(header->ctl_table_arg); 1330f728019bSEric W. Biederman if (unlikely(nr_subheaders > 1)) { 1331f728019bSEric W. Biederman struct ctl_table_header **subheaders; 1332f728019bSEric W. Biederman int i; 1333f728019bSEric W. Biederman 1334f728019bSEric W. Biederman subheaders = (struct ctl_table_header **)(header + 1); 1335f728019bSEric W. Biederman for (i = nr_subheaders -1; i >= 0; i--) { 1336f728019bSEric W. Biederman struct ctl_table_header *subh = subheaders[i]; 1337f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1338f728019bSEric W. Biederman unregister_sysctl_table(subh); 1339f728019bSEric W. Biederman kfree(table); 1340f728019bSEric W. Biederman } 1341f728019bSEric W. Biederman kfree(header); 1342f728019bSEric W. Biederman return; 1343f728019bSEric W. Biederman } 1344f728019bSEric W. Biederman 13451f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1346938aaa4fSEric W. Biederman drop_sysctl_table(header); 13471f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 13481f87f0b5SEric W. Biederman } 13491f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table); 13501f87f0b5SEric W. Biederman 13511f87f0b5SEric W. Biederman void setup_sysctl_set(struct ctl_table_set *p, 13529eb47c26SEric W. Biederman struct ctl_table_root *root, 13531f87f0b5SEric W. Biederman int (*is_seen)(struct ctl_table_set *)) 13541f87f0b5SEric W. Biederman { 13551f87f0b5SEric W. Biederman INIT_LIST_HEAD(&p->list); 13569eb47c26SEric W. Biederman p->root = root; 13571f87f0b5SEric W. Biederman p->is_seen = is_seen; 13581f87f0b5SEric W. Biederman } 13591f87f0b5SEric W. Biederman 136097324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set) 136197324cd8SEric W. Biederman { 136297324cd8SEric W. Biederman WARN_ON(!list_empty(&set->list)); 136397324cd8SEric W. Biederman } 13641f87f0b5SEric W. Biederman 13651e0edd3fSAlexey Dobriyan int __init proc_sys_init(void) 136677b14db5SEric W. Biederman { 1367e1675231SAlexey Dobriyan struct proc_dir_entry *proc_sys_root; 1368e1675231SAlexey Dobriyan 136977b14db5SEric W. Biederman proc_sys_root = proc_mkdir("sys", NULL); 13709043476fSAl Viro proc_sys_root->proc_iops = &proc_sys_dir_operations; 13719043476fSAl Viro proc_sys_root->proc_fops = &proc_sys_dir_file_operations; 137277b14db5SEric W. Biederman proc_sys_root->nlink = 0; 1373de4e83bdSEric W. Biederman 1374de4e83bdSEric W. Biederman return sysctl_init(); 137577b14db5SEric W. Biederman } 1376