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), 481f87f0b5SEric W. Biederman }; 491f87f0b5SEric W. Biederman 501f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock); 511f87f0b5SEric W. Biederman 52076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2) 53076c3eedSEric W. Biederman { 54076c3eedSEric W. Biederman int minlen; 55076c3eedSEric W. Biederman int cmp; 56076c3eedSEric W. Biederman 57076c3eedSEric W. Biederman minlen = len1; 58076c3eedSEric W. Biederman if (minlen > len2) 59076c3eedSEric W. Biederman minlen = len2; 60076c3eedSEric W. Biederman 61076c3eedSEric W. Biederman cmp = memcmp(name1, name2, minlen); 62076c3eedSEric W. Biederman if (cmp == 0) 63076c3eedSEric W. Biederman cmp = len1 - len2; 64076c3eedSEric W. Biederman return cmp; 65076c3eedSEric W. Biederman } 66076c3eedSEric W. Biederman 67076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead, 68076c3eedSEric W. Biederman struct ctl_table_set *set, 69076c3eedSEric W. Biederman struct ctl_table_header *dir_head, struct ctl_table *dir, 70076c3eedSEric W. Biederman const char *name, int namelen) 71076c3eedSEric W. Biederman { 72076c3eedSEric W. Biederman struct ctl_table_header *head; 73076c3eedSEric W. Biederman struct ctl_table *entry; 74076c3eedSEric W. Biederman 75076c3eedSEric W. Biederman if (dir_head->set == set) { 76076c3eedSEric W. Biederman for (entry = dir; entry->procname; entry++) { 77076c3eedSEric W. Biederman const char *procname = entry->procname; 78076c3eedSEric W. Biederman if (namecmp(procname, strlen(procname), name, namelen) == 0) { 79076c3eedSEric W. Biederman *phead = dir_head; 80076c3eedSEric W. Biederman return entry; 81076c3eedSEric W. Biederman } 82076c3eedSEric W. Biederman } 83076c3eedSEric W. Biederman } 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; 88076c3eedSEric W. Biederman if (head->attached_to != dir) 89076c3eedSEric W. Biederman continue; 90076c3eedSEric W. Biederman for (entry = head->attached_by; 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 { 105e0d04529SEric W. Biederman head->ctl_table_arg = table; 106e0d04529SEric W. Biederman INIT_LIST_HEAD(&head->ctl_entry); 107e0d04529SEric W. Biederman head->used = 0; 108e0d04529SEric W. Biederman head->count = 1; 109e0d04529SEric W. Biederman head->nreg = 1; 110e0d04529SEric W. Biederman head->unregistering = NULL; 111e0d04529SEric W. Biederman head->root = root; 112e0d04529SEric W. Biederman head->set = set; 113e0d04529SEric W. Biederman head->parent = NULL; 114e0d04529SEric W. Biederman } 115e0d04529SEric W. Biederman 1168425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head) 1178425d6aaSEric W. Biederman { 1188425d6aaSEric W. Biederman list_del_init(&head->ctl_entry); 1198425d6aaSEric W. Biederman } 1208425d6aaSEric W. Biederman 1218425d6aaSEric W. Biederman static void insert_header(struct ctl_table_header *header) 1228425d6aaSEric W. Biederman { 1238425d6aaSEric W. Biederman header->parent->count++; 1248425d6aaSEric W. Biederman list_add_tail(&header->ctl_entry, &header->set->list); 1258425d6aaSEric W. Biederman } 1268425d6aaSEric W. Biederman 1271f87f0b5SEric W. Biederman /* called under sysctl_lock */ 1281f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p) 1291f87f0b5SEric W. Biederman { 1301f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 1311f87f0b5SEric W. Biederman return 0; 1321f87f0b5SEric W. Biederman p->used++; 1331f87f0b5SEric W. Biederman return 1; 1341f87f0b5SEric W. Biederman } 1351f87f0b5SEric W. Biederman 1361f87f0b5SEric W. Biederman /* called under sysctl_lock */ 1371f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p) 1381f87f0b5SEric W. Biederman { 1391f87f0b5SEric W. Biederman if (!--p->used) 1401f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 1411f87f0b5SEric W. Biederman complete(p->unregistering); 1421f87f0b5SEric W. Biederman } 1431f87f0b5SEric W. Biederman 1441f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */ 1451f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p) 1461f87f0b5SEric W. Biederman { 1471f87f0b5SEric W. Biederman /* 1481f87f0b5SEric W. Biederman * if p->used is 0, nobody will ever touch that entry again; 1491f87f0b5SEric W. Biederman * we'll eliminate all paths to it before dropping sysctl_lock 1501f87f0b5SEric W. Biederman */ 1511f87f0b5SEric W. Biederman if (unlikely(p->used)) { 1521f87f0b5SEric W. Biederman struct completion wait; 1531f87f0b5SEric W. Biederman init_completion(&wait); 1541f87f0b5SEric W. Biederman p->unregistering = &wait; 1551f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1561f87f0b5SEric W. Biederman wait_for_completion(&wait); 1571f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1581f87f0b5SEric W. Biederman } else { 1591f87f0b5SEric W. Biederman /* anything non-NULL; we'll never dereference it */ 1601f87f0b5SEric W. Biederman p->unregistering = ERR_PTR(-EINVAL); 1611f87f0b5SEric W. Biederman } 1621f87f0b5SEric W. Biederman /* 1631f87f0b5SEric W. Biederman * do not remove from the list until nobody holds it; walking the 1641f87f0b5SEric W. Biederman * list in do_sysctl() relies on that. 1651f87f0b5SEric W. Biederman */ 1668425d6aaSEric W. Biederman erase_header(p); 1671f87f0b5SEric W. Biederman } 1681f87f0b5SEric W. Biederman 1691f87f0b5SEric W. Biederman static void sysctl_head_get(struct ctl_table_header *head) 1701f87f0b5SEric W. Biederman { 1711f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1721f87f0b5SEric W. Biederman head->count++; 1731f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1741f87f0b5SEric W. Biederman } 1751f87f0b5SEric W. Biederman 1761f87f0b5SEric W. Biederman void sysctl_head_put(struct ctl_table_header *head) 1771f87f0b5SEric W. Biederman { 1781f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1791f87f0b5SEric W. Biederman if (!--head->count) 1801f87f0b5SEric W. Biederman kfree_rcu(head, rcu); 1811f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1821f87f0b5SEric W. Biederman } 1831f87f0b5SEric W. Biederman 1841f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head) 1851f87f0b5SEric W. Biederman { 1861f87f0b5SEric W. Biederman if (!head) 1871f87f0b5SEric W. Biederman BUG(); 1881f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1891f87f0b5SEric W. Biederman if (!use_table(head)) 1901f87f0b5SEric W. Biederman head = ERR_PTR(-ENOENT); 1911f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1921f87f0b5SEric W. Biederman return head; 1931f87f0b5SEric W. Biederman } 1941f87f0b5SEric W. Biederman 1951f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head) 1961f87f0b5SEric W. Biederman { 1971f87f0b5SEric W. Biederman if (!head) 1981f87f0b5SEric W. Biederman return; 1991f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 2001f87f0b5SEric W. Biederman unuse_table(head); 2011f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 2021f87f0b5SEric W. Biederman } 2031f87f0b5SEric W. Biederman 2041f87f0b5SEric W. Biederman static struct ctl_table_set * 2051f87f0b5SEric W. Biederman lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces) 2061f87f0b5SEric W. Biederman { 2071f87f0b5SEric W. Biederman struct ctl_table_set *set = &root->default_set; 2081f87f0b5SEric W. Biederman if (root->lookup) 2091f87f0b5SEric W. Biederman set = root->lookup(root, namespaces); 2101f87f0b5SEric W. Biederman return set; 2111f87f0b5SEric W. Biederman } 2121f87f0b5SEric W. Biederman 2131f87f0b5SEric W. Biederman static struct list_head * 2141f87f0b5SEric W. Biederman lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces) 2151f87f0b5SEric W. Biederman { 2161f87f0b5SEric W. Biederman struct ctl_table_set *set = lookup_header_set(root, namespaces); 2171f87f0b5SEric W. Biederman return &set->list; 2181f87f0b5SEric W. Biederman } 2191f87f0b5SEric W. Biederman 220076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead, 221076c3eedSEric W. Biederman struct ctl_table_header *dir_head, 222076c3eedSEric W. Biederman struct ctl_table *dir, 223076c3eedSEric W. Biederman const char *name, int namelen) 224076c3eedSEric W. Biederman { 225076c3eedSEric W. Biederman struct ctl_table_header *head; 226076c3eedSEric W. Biederman struct ctl_table *entry; 227076c3eedSEric W. Biederman struct ctl_table_root *root; 228076c3eedSEric W. Biederman struct ctl_table_set *set; 229076c3eedSEric W. Biederman 230076c3eedSEric W. Biederman spin_lock(&sysctl_lock); 231076c3eedSEric W. Biederman root = &sysctl_table_root; 232076c3eedSEric W. Biederman do { 233076c3eedSEric W. Biederman set = lookup_header_set(root, current->nsproxy); 234076c3eedSEric W. Biederman entry = find_entry(&head, set, dir_head, dir, name, namelen); 235076c3eedSEric W. Biederman if (entry && use_table(head)) 236076c3eedSEric W. Biederman *phead = head; 237076c3eedSEric W. Biederman else 238076c3eedSEric W. Biederman entry = NULL; 239076c3eedSEric W. Biederman root = list_entry(root->root_list.next, 240076c3eedSEric W. Biederman struct ctl_table_root, root_list); 241076c3eedSEric W. Biederman } while (!entry && root != &sysctl_table_root); 242076c3eedSEric W. Biederman spin_unlock(&sysctl_lock); 243076c3eedSEric W. Biederman return entry; 244076c3eedSEric W. Biederman } 245076c3eedSEric W. Biederman 2466a75ce16SEric W. Biederman static struct ctl_table_header *next_usable_entry(struct ctl_table *dir, 2476a75ce16SEric W. Biederman struct ctl_table_root *root, struct list_head *tmp) 2481f87f0b5SEric W. Biederman { 2496a75ce16SEric W. Biederman struct nsproxy *namespaces = current->nsproxy; 2501f87f0b5SEric W. Biederman struct list_head *header_list; 2511f87f0b5SEric W. Biederman struct ctl_table_header *head; 2521f87f0b5SEric W. Biederman 2531f87f0b5SEric W. Biederman goto next; 2541f87f0b5SEric W. Biederman for (;;) { 2551f87f0b5SEric W. Biederman head = list_entry(tmp, struct ctl_table_header, ctl_entry); 2566a75ce16SEric W. Biederman root = head->root; 2571f87f0b5SEric W. Biederman 2586a75ce16SEric W. Biederman if (head->attached_to != dir || 2596a75ce16SEric W. Biederman !head->attached_by->procname || 2606a75ce16SEric W. Biederman !use_table(head)) 2611f87f0b5SEric W. Biederman goto next; 2626a75ce16SEric W. Biederman 2631f87f0b5SEric W. Biederman return head; 2641f87f0b5SEric W. Biederman next: 2651f87f0b5SEric W. Biederman tmp = tmp->next; 2661f87f0b5SEric W. Biederman header_list = lookup_header_list(root, namespaces); 2671f87f0b5SEric W. Biederman if (tmp != header_list) 2681f87f0b5SEric W. Biederman continue; 2691f87f0b5SEric W. Biederman 2701f87f0b5SEric W. Biederman do { 2711f87f0b5SEric W. Biederman root = list_entry(root->root_list.next, 2721f87f0b5SEric W. Biederman struct ctl_table_root, root_list); 2731f87f0b5SEric W. Biederman if (root == &sysctl_table_root) 2741f87f0b5SEric W. Biederman goto out; 2751f87f0b5SEric W. Biederman header_list = lookup_header_list(root, namespaces); 2761f87f0b5SEric W. Biederman } while (list_empty(header_list)); 2771f87f0b5SEric W. Biederman tmp = header_list->next; 2781f87f0b5SEric W. Biederman } 2791f87f0b5SEric W. Biederman out: 2801f87f0b5SEric W. Biederman return NULL; 2811f87f0b5SEric W. Biederman } 2821f87f0b5SEric W. Biederman 2836a75ce16SEric W. Biederman static void first_entry( 2846a75ce16SEric W. Biederman struct ctl_table_header *dir_head, struct ctl_table *dir, 2856a75ce16SEric W. Biederman struct ctl_table_header **phead, struct ctl_table **pentry) 2861f87f0b5SEric W. Biederman { 2876a75ce16SEric W. Biederman struct ctl_table_header *head = dir_head; 2886a75ce16SEric W. Biederman struct ctl_table *entry = dir; 2896a75ce16SEric W. Biederman 2906a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 2916a75ce16SEric W. Biederman if (entry->procname) { 2926a75ce16SEric W. Biederman use_table(head); 2936a75ce16SEric W. Biederman } else { 2946a75ce16SEric W. Biederman head = next_usable_entry(dir, &sysctl_table_root, 2956a75ce16SEric W. Biederman &sysctl_table_root.default_set.list); 2966a75ce16SEric W. Biederman if (head) 2976a75ce16SEric W. Biederman entry = head->attached_by; 2986a75ce16SEric W. Biederman } 2996a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 3006a75ce16SEric W. Biederman *phead = head; 3016a75ce16SEric W. Biederman *pentry = entry; 3026a75ce16SEric W. Biederman } 3036a75ce16SEric W. Biederman 3046a75ce16SEric W. Biederman static void next_entry(struct ctl_table *dir, 3056a75ce16SEric W. Biederman struct ctl_table_header **phead, struct ctl_table **pentry) 3066a75ce16SEric W. Biederman { 3076a75ce16SEric W. Biederman struct ctl_table_header *head = *phead; 3086a75ce16SEric W. Biederman struct ctl_table *entry = *pentry; 3096a75ce16SEric W. Biederman 3106a75ce16SEric W. Biederman entry++; 3116a75ce16SEric W. Biederman if (!entry->procname) { 3126a75ce16SEric W. Biederman struct ctl_table_root *root = head->root; 3136a75ce16SEric W. Biederman struct list_head *tmp = &head->ctl_entry; 3146a75ce16SEric W. Biederman if (head->attached_to != dir) { 3156a75ce16SEric W. Biederman root = &sysctl_table_root; 3166a75ce16SEric W. Biederman tmp = &sysctl_table_root.default_set.list; 3176a75ce16SEric W. Biederman } 3186a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 3196a75ce16SEric W. Biederman unuse_table(head); 3206a75ce16SEric W. Biederman head = next_usable_entry(dir, root, tmp); 3216a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 3226a75ce16SEric W. Biederman if (head) 3236a75ce16SEric W. Biederman entry = head->attached_by; 3246a75ce16SEric W. Biederman } 3256a75ce16SEric W. Biederman *phead = head; 3266a75ce16SEric W. Biederman *pentry = entry; 3271f87f0b5SEric W. Biederman } 3281f87f0b5SEric W. Biederman 3291f87f0b5SEric W. Biederman void register_sysctl_root(struct ctl_table_root *root) 3301f87f0b5SEric W. Biederman { 3311f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 3321f87f0b5SEric W. Biederman list_add_tail(&root->root_list, &sysctl_table_root.root_list); 3331f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 3341f87f0b5SEric W. Biederman } 3351f87f0b5SEric W. Biederman 3361f87f0b5SEric W. Biederman /* 3371f87f0b5SEric W. Biederman * sysctl_perm does NOT grant the superuser all rights automatically, because 3381f87f0b5SEric W. Biederman * some sysctl variables are readonly even to root. 3391f87f0b5SEric W. Biederman */ 3401f87f0b5SEric W. Biederman 3411f87f0b5SEric W. Biederman static int test_perm(int mode, int op) 3421f87f0b5SEric W. Biederman { 3431f87f0b5SEric W. Biederman if (!current_euid()) 3441f87f0b5SEric W. Biederman mode >>= 6; 3451f87f0b5SEric W. Biederman else if (in_egroup_p(0)) 3461f87f0b5SEric W. Biederman mode >>= 3; 3471f87f0b5SEric W. Biederman if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0) 3481f87f0b5SEric W. Biederman return 0; 3491f87f0b5SEric W. Biederman return -EACCES; 3501f87f0b5SEric W. Biederman } 3511f87f0b5SEric W. Biederman 3521f87f0b5SEric W. Biederman static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op) 3531f87f0b5SEric W. Biederman { 3541f87f0b5SEric W. Biederman int mode; 3551f87f0b5SEric W. Biederman 3561f87f0b5SEric W. Biederman if (root->permissions) 3571f87f0b5SEric W. Biederman mode = root->permissions(root, current->nsproxy, table); 3581f87f0b5SEric W. Biederman else 3591f87f0b5SEric W. Biederman mode = table->mode; 3601f87f0b5SEric W. Biederman 3611f87f0b5SEric W. Biederman return test_perm(mode, op); 3621f87f0b5SEric W. Biederman } 3631f87f0b5SEric W. Biederman 3649043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb, 3659043476fSAl Viro struct ctl_table_header *head, struct ctl_table *table) 36677b14db5SEric W. Biederman { 36777b14db5SEric W. Biederman struct inode *inode; 3689043476fSAl Viro struct proc_inode *ei; 36977b14db5SEric W. Biederman 3709043476fSAl Viro inode = new_inode(sb); 37177b14db5SEric W. Biederman if (!inode) 37277b14db5SEric W. Biederman goto out; 37377b14db5SEric W. Biederman 37485fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 37585fe4025SChristoph Hellwig 3769043476fSAl Viro sysctl_head_get(head); 37777b14db5SEric W. Biederman ei = PROC_I(inode); 3789043476fSAl Viro ei->sysctl = head; 3799043476fSAl Viro ei->sysctl_entry = table; 3809043476fSAl Viro 38177b14db5SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 3829043476fSAl Viro inode->i_mode = table->mode; 3839043476fSAl Viro if (!table->child) { 3849043476fSAl Viro inode->i_mode |= S_IFREG; 38577b14db5SEric W. Biederman inode->i_op = &proc_sys_inode_operations; 38677b14db5SEric W. Biederman inode->i_fop = &proc_sys_file_operations; 3879043476fSAl Viro } else { 3889043476fSAl Viro inode->i_mode |= S_IFDIR; 3899043476fSAl Viro inode->i_op = &proc_sys_dir_operations; 3909043476fSAl Viro inode->i_fop = &proc_sys_dir_file_operations; 3919043476fSAl Viro } 39277b14db5SEric W. Biederman out: 39377b14db5SEric W. Biederman return inode; 39477b14db5SEric W. Biederman } 39577b14db5SEric W. Biederman 39681324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode) 39777b14db5SEric W. Biederman { 3983cc3e046SEric W. Biederman struct ctl_table_header *head = PROC_I(inode)->sysctl; 3993cc3e046SEric W. Biederman if (!head) 4003cc3e046SEric W. Biederman head = &root_table_header; 4013cc3e046SEric W. Biederman return sysctl_head_grab(head); 40277b14db5SEric W. Biederman } 40377b14db5SEric W. Biederman 40477b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, 40577b14db5SEric W. Biederman struct nameidata *nd) 40677b14db5SEric W. Biederman { 4079043476fSAl Viro struct ctl_table_header *head = grab_header(dir); 4089043476fSAl Viro struct ctl_table *table = PROC_I(dir)->sysctl_entry; 4099043476fSAl Viro struct ctl_table_header *h = NULL; 4109043476fSAl Viro struct qstr *name = &dentry->d_name; 4119043476fSAl Viro struct ctl_table *p; 41277b14db5SEric W. Biederman struct inode *inode; 4139043476fSAl Viro struct dentry *err = ERR_PTR(-ENOENT); 41477b14db5SEric W. Biederman 4159043476fSAl Viro if (IS_ERR(head)) 4169043476fSAl Viro return ERR_CAST(head); 4179043476fSAl Viro 4189043476fSAl Viro if (table && !table->child) { 4199043476fSAl Viro WARN_ON(1); 4209043476fSAl Viro goto out; 4219043476fSAl Viro } 4229043476fSAl Viro 423a194558eSEric W. Biederman table = table ? table->child : &head->ctl_table[1]; 4249043476fSAl Viro 425076c3eedSEric W. Biederman p = lookup_entry(&h, head, table, name->name, name->len); 4269043476fSAl Viro if (!p) 42777b14db5SEric W. Biederman goto out; 42877b14db5SEric W. Biederman 42977b14db5SEric W. Biederman err = ERR_PTR(-ENOMEM); 4309043476fSAl Viro inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p); 4319043476fSAl Viro if (h) 4329043476fSAl Viro sysctl_head_finish(h); 4339043476fSAl Viro 43477b14db5SEric W. Biederman if (!inode) 43577b14db5SEric W. Biederman goto out; 43677b14db5SEric W. Biederman 43777b14db5SEric W. Biederman err = NULL; 438fb045adbSNick Piggin d_set_d_op(dentry, &proc_sys_dentry_operations); 43977b14db5SEric W. Biederman d_add(dentry, inode); 44077b14db5SEric W. Biederman 44177b14db5SEric W. Biederman out: 44277b14db5SEric W. Biederman sysctl_head_finish(head); 44377b14db5SEric W. Biederman return err; 44477b14db5SEric W. Biederman } 44577b14db5SEric W. Biederman 4467708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf, 4477708bfb1SPavel Emelyanov size_t count, loff_t *ppos, int write) 44877b14db5SEric W. Biederman { 4499043476fSAl Viro struct inode *inode = filp->f_path.dentry->d_inode; 4509043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 4519043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 4522a2da53bSDavid Howells ssize_t error; 4532a2da53bSDavid Howells size_t res; 45477b14db5SEric W. Biederman 4559043476fSAl Viro if (IS_ERR(head)) 4569043476fSAl Viro return PTR_ERR(head); 45777b14db5SEric W. Biederman 45877b14db5SEric W. Biederman /* 45977b14db5SEric W. Biederman * At this point we know that the sysctl was not unregistered 46077b14db5SEric W. Biederman * and won't be until we finish. 46177b14db5SEric W. Biederman */ 46277b14db5SEric W. Biederman error = -EPERM; 463d7321cd6SPavel Emelyanov if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ)) 46477b14db5SEric W. Biederman goto out; 46577b14db5SEric W. Biederman 4669043476fSAl Viro /* if that can happen at all, it should be -EINVAL, not -EISDIR */ 4679043476fSAl Viro error = -EINVAL; 4689043476fSAl Viro if (!table->proc_handler) 4699043476fSAl Viro goto out; 4709043476fSAl Viro 47177b14db5SEric W. Biederman /* careful: calling conventions are nasty here */ 47277b14db5SEric W. Biederman res = count; 4738d65af78SAlexey Dobriyan error = table->proc_handler(table, write, buf, &res, ppos); 47477b14db5SEric W. Biederman if (!error) 47577b14db5SEric W. Biederman error = res; 47677b14db5SEric W. Biederman out: 47777b14db5SEric W. Biederman sysctl_head_finish(head); 47877b14db5SEric W. Biederman 47977b14db5SEric W. Biederman return error; 48077b14db5SEric W. Biederman } 48177b14db5SEric W. Biederman 4827708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf, 4837708bfb1SPavel Emelyanov size_t count, loff_t *ppos) 4847708bfb1SPavel Emelyanov { 4857708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0); 4867708bfb1SPavel Emelyanov } 4877708bfb1SPavel Emelyanov 48877b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf, 48977b14db5SEric W. Biederman size_t count, loff_t *ppos) 49077b14db5SEric W. Biederman { 4917708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1); 49277b14db5SEric W. Biederman } 49377b14db5SEric W. Biederman 494f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp) 495f1ecf068SLucas De Marchi { 496f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 497f1ecf068SLucas De Marchi 498f1ecf068SLucas De Marchi if (table->poll) 499f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 500f1ecf068SLucas De Marchi 501f1ecf068SLucas De Marchi return 0; 502f1ecf068SLucas De Marchi } 503f1ecf068SLucas De Marchi 504f1ecf068SLucas De Marchi static unsigned int proc_sys_poll(struct file *filp, poll_table *wait) 505f1ecf068SLucas De Marchi { 506f1ecf068SLucas De Marchi struct inode *inode = filp->f_path.dentry->d_inode; 507f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 508f1ecf068SLucas De Marchi unsigned long event = (unsigned long)filp->private_data; 509f1ecf068SLucas De Marchi unsigned int ret = DEFAULT_POLLMASK; 510f1ecf068SLucas De Marchi 511f1ecf068SLucas De Marchi if (!table->proc_handler) 512f1ecf068SLucas De Marchi goto out; 513f1ecf068SLucas De Marchi 514f1ecf068SLucas De Marchi if (!table->poll) 515f1ecf068SLucas De Marchi goto out; 516f1ecf068SLucas De Marchi 517f1ecf068SLucas De Marchi poll_wait(filp, &table->poll->wait, wait); 518f1ecf068SLucas De Marchi 519f1ecf068SLucas De Marchi if (event != atomic_read(&table->poll->event)) { 520f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 521f1ecf068SLucas De Marchi ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI; 522f1ecf068SLucas De Marchi } 523f1ecf068SLucas De Marchi 524f1ecf068SLucas De Marchi out: 525f1ecf068SLucas De Marchi return ret; 526f1ecf068SLucas De Marchi } 52777b14db5SEric W. Biederman 52877b14db5SEric W. Biederman static int proc_sys_fill_cache(struct file *filp, void *dirent, 5299043476fSAl Viro filldir_t filldir, 5309043476fSAl Viro struct ctl_table_header *head, 5319043476fSAl Viro struct ctl_table *table) 53277b14db5SEric W. Biederman { 53377b14db5SEric W. Biederman struct dentry *child, *dir = filp->f_path.dentry; 53477b14db5SEric W. Biederman struct inode *inode; 53577b14db5SEric W. Biederman struct qstr qname; 53677b14db5SEric W. Biederman ino_t ino = 0; 53777b14db5SEric W. Biederman unsigned type = DT_UNKNOWN; 53877b14db5SEric W. Biederman 53977b14db5SEric W. Biederman qname.name = table->procname; 54077b14db5SEric W. Biederman qname.len = strlen(table->procname); 54177b14db5SEric W. Biederman qname.hash = full_name_hash(qname.name, qname.len); 54277b14db5SEric W. Biederman 54377b14db5SEric W. Biederman child = d_lookup(dir, &qname); 54477b14db5SEric W. Biederman if (!child) { 5459043476fSAl Viro child = d_alloc(dir, &qname); 5469043476fSAl Viro if (child) { 5479043476fSAl Viro inode = proc_sys_make_inode(dir->d_sb, head, table); 5489043476fSAl Viro if (!inode) { 5499043476fSAl Viro dput(child); 5509043476fSAl Viro return -ENOMEM; 5519043476fSAl Viro } else { 552fb045adbSNick Piggin d_set_d_op(child, &proc_sys_dentry_operations); 5539043476fSAl Viro d_add(child, inode); 55477b14db5SEric W. Biederman } 5559043476fSAl Viro } else { 5569043476fSAl Viro return -ENOMEM; 55777b14db5SEric W. Biederman } 55877b14db5SEric W. Biederman } 55977b14db5SEric W. Biederman inode = child->d_inode; 56077b14db5SEric W. Biederman ino = inode->i_ino; 56177b14db5SEric W. Biederman type = inode->i_mode >> 12; 56277b14db5SEric W. Biederman dput(child); 5639043476fSAl Viro return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type); 5649043476fSAl Viro } 5659043476fSAl Viro 5669043476fSAl Viro static int scan(struct ctl_table_header *head, ctl_table *table, 5679043476fSAl Viro unsigned long *pos, struct file *file, 5689043476fSAl Viro void *dirent, filldir_t filldir) 5699043476fSAl Viro { 5709043476fSAl Viro int res; 5719043476fSAl Viro 5726a75ce16SEric W. Biederman if ((*pos)++ < file->f_pos) 5736a75ce16SEric W. Biederman return 0; 5749043476fSAl Viro 5759043476fSAl Viro res = proc_sys_fill_cache(file, dirent, filldir, head, table); 5769043476fSAl Viro 5776a75ce16SEric W. Biederman if (res == 0) 5786a75ce16SEric W. Biederman file->f_pos = *pos; 5796a75ce16SEric W. Biederman 5806a75ce16SEric W. Biederman return res; 58177b14db5SEric W. Biederman } 58277b14db5SEric W. Biederman 58377b14db5SEric W. Biederman static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir) 58477b14db5SEric W. Biederman { 5859043476fSAl Viro struct dentry *dentry = filp->f_path.dentry; 58677b14db5SEric W. Biederman struct inode *inode = dentry->d_inode; 5879043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 5889043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 5899043476fSAl Viro struct ctl_table_header *h = NULL; 5906a75ce16SEric W. Biederman struct ctl_table *entry; 59177b14db5SEric W. Biederman unsigned long pos; 5929043476fSAl Viro int ret = -EINVAL; 59377b14db5SEric W. Biederman 5949043476fSAl Viro if (IS_ERR(head)) 5959043476fSAl Viro return PTR_ERR(head); 5969043476fSAl Viro 5979043476fSAl Viro if (table && !table->child) { 5989043476fSAl Viro WARN_ON(1); 59977b14db5SEric W. Biederman goto out; 6009043476fSAl Viro } 6019043476fSAl Viro 602a194558eSEric W. Biederman table = table ? table->child : &head->ctl_table[1]; 60377b14db5SEric W. Biederman 60477b14db5SEric W. Biederman ret = 0; 60577b14db5SEric W. Biederman /* Avoid a switch here: arm builds fail with missing __cmpdi2 */ 60677b14db5SEric W. Biederman if (filp->f_pos == 0) { 60777b14db5SEric W. Biederman if (filldir(dirent, ".", 1, filp->f_pos, 60877b14db5SEric W. Biederman inode->i_ino, DT_DIR) < 0) 60977b14db5SEric W. Biederman goto out; 61077b14db5SEric W. Biederman filp->f_pos++; 61177b14db5SEric W. Biederman } 61277b14db5SEric W. Biederman if (filp->f_pos == 1) { 61377b14db5SEric W. Biederman if (filldir(dirent, "..", 2, filp->f_pos, 61477b14db5SEric W. Biederman parent_ino(dentry), DT_DIR) < 0) 61577b14db5SEric W. Biederman goto out; 61677b14db5SEric W. Biederman filp->f_pos++; 61777b14db5SEric W. Biederman } 61877b14db5SEric W. Biederman pos = 2; 61977b14db5SEric W. Biederman 6206a75ce16SEric W. Biederman for (first_entry(head, table, &h, &entry); h; next_entry(table, &h, &entry)) { 6216a75ce16SEric W. Biederman ret = scan(h, entry, &pos, filp, dirent, filldir); 6229043476fSAl Viro if (ret) { 6239043476fSAl Viro sysctl_head_finish(h); 6249043476fSAl Viro break; 62577b14db5SEric W. Biederman } 62677b14db5SEric W. Biederman } 62777b14db5SEric W. Biederman ret = 1; 62877b14db5SEric W. Biederman out: 62977b14db5SEric W. Biederman sysctl_head_finish(head); 63077b14db5SEric W. Biederman return ret; 63177b14db5SEric W. Biederman } 63277b14db5SEric W. Biederman 63310556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask) 63477b14db5SEric W. Biederman { 63577b14db5SEric W. Biederman /* 63677b14db5SEric W. Biederman * sysctl entries that are not writeable, 63777b14db5SEric W. Biederman * are _NOT_ writeable, capabilities or not. 63877b14db5SEric W. Biederman */ 639f696a365SMiklos Szeredi struct ctl_table_header *head; 640f696a365SMiklos Szeredi struct ctl_table *table; 64177b14db5SEric W. Biederman int error; 64277b14db5SEric W. Biederman 643f696a365SMiklos Szeredi /* Executable files are not allowed under /proc/sys/ */ 644f696a365SMiklos Szeredi if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) 645f696a365SMiklos Szeredi return -EACCES; 646f696a365SMiklos Szeredi 647f696a365SMiklos Szeredi head = grab_header(inode); 6489043476fSAl Viro if (IS_ERR(head)) 6499043476fSAl Viro return PTR_ERR(head); 65077b14db5SEric W. Biederman 651f696a365SMiklos Szeredi table = PROC_I(inode)->sysctl_entry; 6529043476fSAl Viro if (!table) /* global root - r-xr-xr-x */ 6539043476fSAl Viro error = mask & MAY_WRITE ? -EACCES : 0; 6549043476fSAl Viro else /* Use the permissions on the sysctl table entry */ 6551fc0f78cSAl Viro error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK); 6569043476fSAl Viro 65777b14db5SEric W. Biederman sysctl_head_finish(head); 65877b14db5SEric W. Biederman return error; 65977b14db5SEric W. Biederman } 66077b14db5SEric W. Biederman 66177b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr) 66277b14db5SEric W. Biederman { 66377b14db5SEric W. Biederman struct inode *inode = dentry->d_inode; 66477b14db5SEric W. Biederman int error; 66577b14db5SEric W. Biederman 66677b14db5SEric W. Biederman if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) 66777b14db5SEric W. Biederman return -EPERM; 66877b14db5SEric W. Biederman 66977b14db5SEric W. Biederman error = inode_change_ok(inode, attr); 6701025774cSChristoph Hellwig if (error) 67177b14db5SEric W. Biederman return error; 6721025774cSChristoph Hellwig 6731025774cSChristoph Hellwig if ((attr->ia_valid & ATTR_SIZE) && 6741025774cSChristoph Hellwig attr->ia_size != i_size_read(inode)) { 6751025774cSChristoph Hellwig error = vmtruncate(inode, attr->ia_size); 6761025774cSChristoph Hellwig if (error) 6771025774cSChristoph Hellwig return error; 6781025774cSChristoph Hellwig } 6791025774cSChristoph Hellwig 6801025774cSChristoph Hellwig setattr_copy(inode, attr); 6811025774cSChristoph Hellwig mark_inode_dirty(inode); 6821025774cSChristoph Hellwig return 0; 68377b14db5SEric W. Biederman } 68477b14db5SEric W. Biederman 6859043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 6869043476fSAl Viro { 6879043476fSAl Viro struct inode *inode = dentry->d_inode; 6889043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 6899043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 6909043476fSAl Viro 6919043476fSAl Viro if (IS_ERR(head)) 6929043476fSAl Viro return PTR_ERR(head); 6939043476fSAl Viro 6949043476fSAl Viro generic_fillattr(inode, stat); 6959043476fSAl Viro if (table) 6969043476fSAl Viro stat->mode = (stat->mode & S_IFMT) | table->mode; 6979043476fSAl Viro 6989043476fSAl Viro sysctl_head_finish(head); 6999043476fSAl Viro return 0; 7009043476fSAl Viro } 7019043476fSAl Viro 70277b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = { 703f1ecf068SLucas De Marchi .open = proc_sys_open, 704f1ecf068SLucas De Marchi .poll = proc_sys_poll, 70577b14db5SEric W. Biederman .read = proc_sys_read, 70677b14db5SEric W. Biederman .write = proc_sys_write, 7076038f373SArnd Bergmann .llseek = default_llseek, 7089043476fSAl Viro }; 7099043476fSAl Viro 7109043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = { 711887df078SPavel Emelyanov .read = generic_read_dir, 71277b14db5SEric W. Biederman .readdir = proc_sys_readdir, 7133222a3e5SChristoph Hellwig .llseek = generic_file_llseek, 71477b14db5SEric W. Biederman }; 71577b14db5SEric W. Biederman 71603a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = { 7179043476fSAl Viro .permission = proc_sys_permission, 7189043476fSAl Viro .setattr = proc_sys_setattr, 7199043476fSAl Viro .getattr = proc_sys_getattr, 7209043476fSAl Viro }; 7219043476fSAl Viro 7229043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = { 72377b14db5SEric W. Biederman .lookup = proc_sys_lookup, 72477b14db5SEric W. Biederman .permission = proc_sys_permission, 72577b14db5SEric W. Biederman .setattr = proc_sys_setattr, 7269043476fSAl Viro .getattr = proc_sys_getattr, 72777b14db5SEric W. Biederman }; 72877b14db5SEric W. Biederman 72977b14db5SEric W. Biederman static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd) 73077b14db5SEric W. Biederman { 73134286d66SNick Piggin if (nd->flags & LOOKUP_RCU) 73234286d66SNick Piggin return -ECHILD; 7339043476fSAl Viro return !PROC_I(dentry->d_inode)->sysctl->unregistering; 7349043476fSAl Viro } 7359043476fSAl Viro 736fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry) 7379043476fSAl Viro { 7389043476fSAl Viro return !!PROC_I(dentry->d_inode)->sysctl->unregistering; 7399043476fSAl Viro } 7409043476fSAl Viro 7411f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p) 7421f87f0b5SEric W. Biederman { 7431f87f0b5SEric W. Biederman struct ctl_table_set *set = p->set; 7441f87f0b5SEric W. Biederman int res; 7451f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 7461f87f0b5SEric W. Biederman if (p->unregistering) 7471f87f0b5SEric W. Biederman res = 0; 7481f87f0b5SEric W. Biederman else if (!set->is_seen) 7491f87f0b5SEric W. Biederman res = 1; 7501f87f0b5SEric W. Biederman else 7511f87f0b5SEric W. Biederman res = set->is_seen(set); 7521f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 7531f87f0b5SEric W. Biederman return res; 7541f87f0b5SEric W. Biederman } 7551f87f0b5SEric W. Biederman 756621e155aSNick Piggin static int proc_sys_compare(const struct dentry *parent, 757621e155aSNick Piggin const struct inode *pinode, 758621e155aSNick Piggin const struct dentry *dentry, const struct inode *inode, 759621e155aSNick Piggin unsigned int len, const char *str, const struct qstr *name) 7609043476fSAl Viro { 761dfef6dcdSAl Viro struct ctl_table_header *head; 76231e6b01fSNick Piggin /* Although proc doesn't have negative dentries, rcu-walk means 76331e6b01fSNick Piggin * that inode here can be NULL */ 764dfef6dcdSAl Viro /* AV: can it, indeed? */ 76531e6b01fSNick Piggin if (!inode) 766dfef6dcdSAl Viro return 1; 767621e155aSNick Piggin if (name->len != len) 7689043476fSAl Viro return 1; 769621e155aSNick Piggin if (memcmp(name->name, str, len)) 7709043476fSAl Viro return 1; 771dfef6dcdSAl Viro head = rcu_dereference(PROC_I(inode)->sysctl); 772dfef6dcdSAl Viro return !head || !sysctl_is_seen(head); 77377b14db5SEric W. Biederman } 77477b14db5SEric W. Biederman 775d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = { 77677b14db5SEric W. Biederman .d_revalidate = proc_sys_revalidate, 7779043476fSAl Viro .d_delete = proc_sys_delete, 7789043476fSAl Viro .d_compare = proc_sys_compare, 77977b14db5SEric W. Biederman }; 78077b14db5SEric W. Biederman 7811f87f0b5SEric W. Biederman static struct ctl_table *is_branch_in(struct ctl_table *branch, 7821f87f0b5SEric W. Biederman struct ctl_table *table) 7831f87f0b5SEric W. Biederman { 7841f87f0b5SEric W. Biederman struct ctl_table *p; 7851f87f0b5SEric W. Biederman const char *s = branch->procname; 7861f87f0b5SEric W. Biederman 7871f87f0b5SEric W. Biederman /* branch should have named subdirectory as its first element */ 7881f87f0b5SEric W. Biederman if (!s || !branch->child) 7891f87f0b5SEric W. Biederman return NULL; 7901f87f0b5SEric W. Biederman 7911f87f0b5SEric W. Biederman /* ... and nothing else */ 7921f87f0b5SEric W. Biederman if (branch[1].procname) 7931f87f0b5SEric W. Biederman return NULL; 7941f87f0b5SEric W. Biederman 7951f87f0b5SEric W. Biederman /* table should contain subdirectory with the same name */ 7961f87f0b5SEric W. Biederman for (p = table; p->procname; p++) { 7971f87f0b5SEric W. Biederman if (!p->child) 7981f87f0b5SEric W. Biederman continue; 7991f87f0b5SEric W. Biederman if (p->procname && strcmp(p->procname, s) == 0) 8001f87f0b5SEric W. Biederman return p; 8011f87f0b5SEric W. Biederman } 8021f87f0b5SEric W. Biederman return NULL; 8031f87f0b5SEric W. Biederman } 8041f87f0b5SEric W. Biederman 8051f87f0b5SEric W. Biederman /* see if attaching q to p would be an improvement */ 8061f87f0b5SEric W. Biederman static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q) 8071f87f0b5SEric W. Biederman { 8081f87f0b5SEric W. Biederman struct ctl_table *to = p->ctl_table, *by = q->ctl_table; 8091f87f0b5SEric W. Biederman struct ctl_table *next; 8101f87f0b5SEric W. Biederman int is_better = 0; 8111f87f0b5SEric W. Biederman int not_in_parent = !p->attached_by; 8121f87f0b5SEric W. Biederman 8131f87f0b5SEric W. Biederman while ((next = is_branch_in(by, to)) != NULL) { 8141f87f0b5SEric W. Biederman if (by == q->attached_by) 8151f87f0b5SEric W. Biederman is_better = 1; 8161f87f0b5SEric W. Biederman if (to == p->attached_by) 8171f87f0b5SEric W. Biederman not_in_parent = 1; 8181f87f0b5SEric W. Biederman by = by->child; 8191f87f0b5SEric W. Biederman to = next->child; 8201f87f0b5SEric W. Biederman } 8211f87f0b5SEric W. Biederman 8221f87f0b5SEric W. Biederman if (is_better && not_in_parent) { 8231f87f0b5SEric W. Biederman q->attached_by = by; 8241f87f0b5SEric W. Biederman q->attached_to = to; 8251f87f0b5SEric W. Biederman q->parent = p; 8261f87f0b5SEric W. Biederman } 8271f87f0b5SEric W. Biederman } 8281f87f0b5SEric W. Biederman 8297c60c48fSEric W. Biederman static int sysctl_check_table_dups(const char *path, struct ctl_table *old, 8301f87f0b5SEric W. Biederman struct ctl_table *table) 8311f87f0b5SEric W. Biederman { 8327c60c48fSEric W. Biederman struct ctl_table *entry, *test; 8331f87f0b5SEric W. Biederman int error = 0; 8341f87f0b5SEric W. Biederman 8357c60c48fSEric W. Biederman for (entry = old; entry->procname; entry++) { 8367c60c48fSEric W. Biederman for (test = table; test->procname; test++) { 8377c60c48fSEric W. Biederman if (strcmp(entry->procname, test->procname) == 0) { 8387c60c48fSEric W. Biederman printk(KERN_ERR "sysctl duplicate entry: %s/%s\n", 8397c60c48fSEric W. Biederman path, test->procname); 8407c60c48fSEric W. Biederman error = -EEXIST; 8411f87f0b5SEric W. Biederman } 8427c60c48fSEric W. Biederman } 8437c60c48fSEric W. Biederman } 8447c60c48fSEric W. Biederman return error; 8457c60c48fSEric W. Biederman } 8467c60c48fSEric W. Biederman 8477c60c48fSEric W. Biederman static int sysctl_check_dups(struct nsproxy *namespaces, 8487c60c48fSEric W. Biederman struct ctl_table_header *header, 8497c60c48fSEric W. Biederman const char *path, struct ctl_table *table) 8507c60c48fSEric W. Biederman { 8517c60c48fSEric W. Biederman struct ctl_table_root *root; 8527c60c48fSEric W. Biederman struct ctl_table_set *set; 8537c60c48fSEric W. Biederman struct ctl_table_header *dir_head, *head; 8547c60c48fSEric W. Biederman struct ctl_table *dir_table; 8557c60c48fSEric W. Biederman int error = 0; 8567c60c48fSEric W. Biederman 8577c60c48fSEric W. Biederman /* No dups if we are the only member of our directory */ 8587c60c48fSEric W. Biederman if (header->attached_by != table) 8597c60c48fSEric W. Biederman return 0; 8607c60c48fSEric W. Biederman 8617c60c48fSEric W. Biederman dir_head = header->parent; 8627c60c48fSEric W. Biederman dir_table = header->attached_to; 8637c60c48fSEric W. Biederman 8647c60c48fSEric W. Biederman error = sysctl_check_table_dups(path, dir_table, table); 8657c60c48fSEric W. Biederman 8667c60c48fSEric W. Biederman root = &sysctl_table_root; 8677c60c48fSEric W. Biederman do { 8687c60c48fSEric W. Biederman set = lookup_header_set(root, namespaces); 8697c60c48fSEric W. Biederman 8707c60c48fSEric W. Biederman list_for_each_entry(head, &set->list, ctl_entry) { 8717c60c48fSEric W. Biederman if (head->unregistering) 8727c60c48fSEric W. Biederman continue; 8737c60c48fSEric W. Biederman if (head->attached_to != dir_table) 8747c60c48fSEric W. Biederman continue; 8757c60c48fSEric W. Biederman error = sysctl_check_table_dups(path, head->attached_by, 8767c60c48fSEric W. Biederman table); 8777c60c48fSEric W. Biederman } 8787c60c48fSEric W. Biederman root = list_entry(root->root_list.next, 8797c60c48fSEric W. Biederman struct ctl_table_root, root_list); 8807c60c48fSEric W. Biederman } while (root != &sysctl_table_root); 8817c60c48fSEric W. Biederman return error; 8827c60c48fSEric W. Biederman } 8837c60c48fSEric W. Biederman 8847c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...) 8857c60c48fSEric W. Biederman { 8867c60c48fSEric W. Biederman struct va_format vaf; 8877c60c48fSEric W. Biederman va_list args; 8887c60c48fSEric W. Biederman 8897c60c48fSEric W. Biederman va_start(args, fmt); 8907c60c48fSEric W. Biederman vaf.fmt = fmt; 8917c60c48fSEric W. Biederman vaf.va = &args; 8927c60c48fSEric W. Biederman 8937c60c48fSEric W. Biederman printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n", 8947c60c48fSEric W. Biederman path, table->procname, &vaf); 8957c60c48fSEric W. Biederman 8967c60c48fSEric W. Biederman va_end(args); 8977c60c48fSEric W. Biederman return -EINVAL; 8987c60c48fSEric W. Biederman } 8997c60c48fSEric W. Biederman 9007c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table) 9017c60c48fSEric W. Biederman { 9027c60c48fSEric W. Biederman int err = 0; 9037c60c48fSEric W. Biederman for (; table->procname; table++) { 9047c60c48fSEric W. Biederman if (table->child) 9057c60c48fSEric W. Biederman err = sysctl_err(path, table, "Not a file"); 9067c60c48fSEric W. Biederman 9071f87f0b5SEric W. Biederman if ((table->proc_handler == proc_dostring) || 9081f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec) || 9091f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_minmax) || 9101f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_jiffies) || 9111f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_userhz_jiffies) || 9121f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_ms_jiffies) || 9131f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_minmax) || 9141f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) { 9151f87f0b5SEric W. Biederman if (!table->data) 9167c60c48fSEric W. Biederman err = sysctl_err(path, table, "No data"); 9171f87f0b5SEric W. Biederman if (!table->maxlen) 9187c60c48fSEric W. Biederman err = sysctl_err(path, table, "No maxlen"); 9191f87f0b5SEric W. Biederman } 9201f87f0b5SEric W. Biederman if (!table->proc_handler) 9217c60c48fSEric W. Biederman err = sysctl_err(path, table, "No proc_handler"); 9227c60c48fSEric W. Biederman 9237c60c48fSEric W. Biederman if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode) 9247c60c48fSEric W. Biederman err = sysctl_err(path, table, "bogus .mode 0%o", 9257c60c48fSEric W. Biederman table->mode); 9261f87f0b5SEric W. Biederman } 9277c60c48fSEric W. Biederman return err; 9281f87f0b5SEric W. Biederman } 9291f87f0b5SEric W. Biederman 9301f87f0b5SEric W. Biederman /** 931f728019bSEric W. Biederman * __register_sysctl_table - register a leaf sysctl table 9321f87f0b5SEric W. Biederman * @root: List of sysctl headers to register on 9331f87f0b5SEric W. Biederman * @namespaces: Data to compute which lists of sysctl entries are visible 9341f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 9351f87f0b5SEric W. Biederman * @table: the top-level table structure 9361f87f0b5SEric W. Biederman * 9371f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 9381f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 9391f87f0b5SEric W. Biederman * 9401f87f0b5SEric W. Biederman * The members of the &struct ctl_table structure are used as follows: 9411f87f0b5SEric W. Biederman * 9421f87f0b5SEric W. Biederman * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not 9431f87f0b5SEric W. Biederman * enter a sysctl file 9441f87f0b5SEric W. Biederman * 9451f87f0b5SEric W. Biederman * data - a pointer to data for use by proc_handler 9461f87f0b5SEric W. Biederman * 9471f87f0b5SEric W. Biederman * maxlen - the maximum size in bytes of the data 9481f87f0b5SEric W. Biederman * 949f728019bSEric W. Biederman * mode - the file permissions for the /proc/sys file 9501f87f0b5SEric W. Biederman * 951f728019bSEric W. Biederman * child - must be %NULL. 9521f87f0b5SEric W. Biederman * 9531f87f0b5SEric W. Biederman * proc_handler - the text handler routine (described below) 9541f87f0b5SEric W. Biederman * 9551f87f0b5SEric W. Biederman * extra1, extra2 - extra pointers usable by the proc handler routines 9561f87f0b5SEric W. Biederman * 9571f87f0b5SEric W. Biederman * Leaf nodes in the sysctl tree will be represented by a single file 9581f87f0b5SEric W. Biederman * under /proc; non-leaf nodes will be represented by directories. 9591f87f0b5SEric W. Biederman * 960f728019bSEric W. Biederman * There must be a proc_handler routine for any terminal nodes. 961f728019bSEric W. Biederman * Several default handlers are available to cover common cases - 9621f87f0b5SEric W. Biederman * 9631f87f0b5SEric W. Biederman * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(), 9641f87f0b5SEric W. Biederman * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), 9651f87f0b5SEric W. Biederman * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax() 9661f87f0b5SEric W. Biederman * 9671f87f0b5SEric W. Biederman * It is the handler's job to read the input buffer from user memory 9681f87f0b5SEric W. Biederman * and process it. The handler should return 0 on success. 9691f87f0b5SEric W. Biederman * 9701f87f0b5SEric W. Biederman * This routine returns %NULL on a failure to register, and a pointer 9711f87f0b5SEric W. Biederman * to the table header on success. 9721f87f0b5SEric W. Biederman */ 9736e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table( 9741f87f0b5SEric W. Biederman struct ctl_table_root *root, 9751f87f0b5SEric W. Biederman struct nsproxy *namespaces, 9766e9d5164SEric W. Biederman const char *path, struct ctl_table *table) 9771f87f0b5SEric W. Biederman { 9781f87f0b5SEric W. Biederman struct ctl_table_header *header; 9791f87f0b5SEric W. Biederman struct ctl_table *new, **prevp; 9806e9d5164SEric W. Biederman const char *name, *nextname; 9816e9d5164SEric W. Biederman unsigned int npath = 0; 9821f87f0b5SEric W. Biederman struct ctl_table_set *set; 983f05e53a7SEric W. Biederman size_t path_bytes = 0; 984f05e53a7SEric W. Biederman char *new_name; 9851f87f0b5SEric W. Biederman 9861f87f0b5SEric W. Biederman /* Count the path components */ 9876e9d5164SEric W. Biederman for (name = path; name; name = nextname) { 9886e9d5164SEric W. Biederman int namelen; 9896e9d5164SEric W. Biederman nextname = strchr(name, '/'); 9906e9d5164SEric W. Biederman if (nextname) { 9916e9d5164SEric W. Biederman namelen = nextname - name; 9926e9d5164SEric W. Biederman nextname++; 9936e9d5164SEric W. Biederman } else { 9946e9d5164SEric W. Biederman namelen = strlen(name); 9956e9d5164SEric W. Biederman } 9966e9d5164SEric W. Biederman if (namelen == 0) 9976e9d5164SEric W. Biederman continue; 9986e9d5164SEric W. Biederman path_bytes += namelen + 1; 9996e9d5164SEric W. Biederman npath++; 10006e9d5164SEric W. Biederman } 10011f87f0b5SEric W. Biederman 10021f87f0b5SEric W. Biederman /* 10031f87f0b5SEric W. Biederman * For each path component, allocate a 2-element ctl_table array. 10041f87f0b5SEric W. Biederman * The first array element will be filled with the sysctl entry 10051f87f0b5SEric W. Biederman * for this, the second will be the sentinel (procname == 0). 10061f87f0b5SEric W. Biederman * 10071f87f0b5SEric W. Biederman * We allocate everything in one go so that we don't have to 10081f87f0b5SEric W. Biederman * worry about freeing additional memory in unregister_sysctl_table. 10091f87f0b5SEric W. Biederman */ 1010f05e53a7SEric W. Biederman header = kzalloc(sizeof(struct ctl_table_header) + path_bytes + 10111f87f0b5SEric W. Biederman (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL); 10121f87f0b5SEric W. Biederman if (!header) 10131f87f0b5SEric W. Biederman return NULL; 10141f87f0b5SEric W. Biederman 10151f87f0b5SEric W. Biederman new = (struct ctl_table *) (header + 1); 1016f05e53a7SEric W. Biederman new_name = (char *)(new + (2 * npath)); 10171f87f0b5SEric W. Biederman 10181f87f0b5SEric W. Biederman /* Now connect the dots */ 10191f87f0b5SEric W. Biederman prevp = &header->ctl_table; 10206e9d5164SEric W. Biederman for (name = path; name; name = nextname) { 10216e9d5164SEric W. Biederman int namelen; 10226e9d5164SEric W. Biederman nextname = strchr(name, '/'); 10236e9d5164SEric W. Biederman if (nextname) { 10246e9d5164SEric W. Biederman namelen = nextname - name; 10256e9d5164SEric W. Biederman nextname++; 10266e9d5164SEric W. Biederman } else { 10276e9d5164SEric W. Biederman namelen = strlen(name); 10286e9d5164SEric W. Biederman } 10296e9d5164SEric W. Biederman if (namelen == 0) 10306e9d5164SEric W. Biederman continue; 10316e9d5164SEric W. Biederman memcpy(new_name, name, namelen); 10326e9d5164SEric W. Biederman new_name[namelen] = '\0'; 10336e9d5164SEric W. Biederman 1034f05e53a7SEric W. Biederman new->procname = new_name; 10351f87f0b5SEric W. Biederman new->mode = 0555; 10361f87f0b5SEric W. Biederman 10371f87f0b5SEric W. Biederman *prevp = new; 10381f87f0b5SEric W. Biederman prevp = &new->child; 10391f87f0b5SEric W. Biederman 10401f87f0b5SEric W. Biederman new += 2; 10416e9d5164SEric W. Biederman new_name += namelen + 1; 10421f87f0b5SEric W. Biederman } 10431f87f0b5SEric W. Biederman *prevp = table; 10441f87f0b5SEric W. Biederman 1045e0d04529SEric W. Biederman init_header(header, root, NULL, table); 10467c60c48fSEric W. Biederman if (sysctl_check_table(path, table)) 10477c60c48fSEric W. Biederman goto fail; 10488d6ecfccSEric W. Biederman 10491f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 10501f87f0b5SEric W. Biederman header->set = lookup_header_set(root, namespaces); 10511f87f0b5SEric W. Biederman header->attached_by = header->ctl_table; 1052a194558eSEric W. Biederman header->attached_to = &root_table[1]; 10531f87f0b5SEric W. Biederman header->parent = &root_table_header; 1054bd295b56SEric W. Biederman set = header->set; 1055bd295b56SEric W. Biederman root = header->root; 1056bd295b56SEric W. Biederman for (;;) { 10571f87f0b5SEric W. Biederman struct ctl_table_header *p; 10581f87f0b5SEric W. Biederman list_for_each_entry(p, &set->list, ctl_entry) { 10591f87f0b5SEric W. Biederman if (p->unregistering) 10601f87f0b5SEric W. Biederman continue; 10611f87f0b5SEric W. Biederman try_attach(p, header); 10621f87f0b5SEric W. Biederman } 1063bd295b56SEric W. Biederman if (root == &sysctl_table_root) 1064bd295b56SEric W. Biederman break; 1065bd295b56SEric W. Biederman root = list_entry(root->root_list.prev, 1066bd295b56SEric W. Biederman struct ctl_table_root, root_list); 1067bd295b56SEric W. Biederman set = lookup_header_set(root, namespaces); 10681f87f0b5SEric W. Biederman } 10697c60c48fSEric W. Biederman if (sysctl_check_dups(namespaces, header, path, table)) 10707c60c48fSEric W. Biederman goto fail_locked; 10718425d6aaSEric W. Biederman insert_header(header); 10721f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 10731f87f0b5SEric W. Biederman 10741f87f0b5SEric W. Biederman return header; 10757c60c48fSEric W. Biederman fail_locked: 10767c60c48fSEric W. Biederman spin_unlock(&sysctl_lock); 10777c60c48fSEric W. Biederman fail: 10787c60c48fSEric W. Biederman kfree(header); 10797c60c48fSEric W. Biederman dump_stack(); 10807c60c48fSEric W. Biederman return NULL; 10811f87f0b5SEric W. Biederman } 10821f87f0b5SEric W. Biederman 10836e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name) 10846e9d5164SEric W. Biederman { 10856e9d5164SEric W. Biederman int namelen; 10866e9d5164SEric W. Biederman namelen = strlen(name); 10876e9d5164SEric W. Biederman if (((pos - path) + namelen + 2) >= PATH_MAX) 10886e9d5164SEric W. Biederman return NULL; 10896e9d5164SEric W. Biederman memcpy(pos, name, namelen); 10906e9d5164SEric W. Biederman pos[namelen] = '/'; 10916e9d5164SEric W. Biederman pos[namelen + 1] = '\0'; 10926e9d5164SEric W. Biederman pos += namelen + 1; 10936e9d5164SEric W. Biederman return pos; 10946e9d5164SEric W. Biederman } 10956e9d5164SEric W. Biederman 1096f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table) 1097f728019bSEric W. Biederman { 1098f728019bSEric W. Biederman int has_files = 0; 1099f728019bSEric W. Biederman int nr_subheaders = 0; 1100f728019bSEric W. Biederman struct ctl_table *entry; 1101f728019bSEric W. Biederman 1102f728019bSEric W. Biederman /* special case: no directory and empty directory */ 1103f728019bSEric W. Biederman if (!table || !table->procname) 1104f728019bSEric W. Biederman return 1; 1105f728019bSEric W. Biederman 1106f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1107f728019bSEric W. Biederman if (entry->child) 1108f728019bSEric W. Biederman nr_subheaders += count_subheaders(entry->child); 1109f728019bSEric W. Biederman else 1110f728019bSEric W. Biederman has_files = 1; 1111f728019bSEric W. Biederman } 1112f728019bSEric W. Biederman return nr_subheaders + has_files; 1113f728019bSEric W. Biederman } 1114f728019bSEric W. Biederman 1115f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos, 1116f728019bSEric W. Biederman struct ctl_table_header ***subheader, 1117f728019bSEric W. Biederman struct ctl_table_root *root, struct nsproxy *namespaces, 1118f728019bSEric W. Biederman struct ctl_table *table) 1119f728019bSEric W. Biederman { 1120f728019bSEric W. Biederman struct ctl_table *ctl_table_arg = NULL; 1121f728019bSEric W. Biederman struct ctl_table *entry, *files; 1122f728019bSEric W. Biederman int nr_files = 0; 1123f728019bSEric W. Biederman int nr_dirs = 0; 1124f728019bSEric W. Biederman int err = -ENOMEM; 1125f728019bSEric W. Biederman 1126f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1127f728019bSEric W. Biederman if (entry->child) 1128f728019bSEric W. Biederman nr_dirs++; 1129f728019bSEric W. Biederman else 1130f728019bSEric W. Biederman nr_files++; 1131f728019bSEric W. Biederman } 1132f728019bSEric W. Biederman 1133f728019bSEric W. Biederman files = table; 1134f728019bSEric W. Biederman /* If there are mixed files and directories we need a new table */ 1135f728019bSEric W. Biederman if (nr_dirs && nr_files) { 1136f728019bSEric W. Biederman struct ctl_table *new; 1137f728019bSEric W. Biederman files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1), 1138f728019bSEric W. Biederman GFP_KERNEL); 1139f728019bSEric W. Biederman if (!files) 1140f728019bSEric W. Biederman goto out; 1141f728019bSEric W. Biederman 1142f728019bSEric W. Biederman ctl_table_arg = files; 1143f728019bSEric W. Biederman for (new = files, entry = table; entry->procname; entry++) { 1144f728019bSEric W. Biederman if (entry->child) 1145f728019bSEric W. Biederman continue; 1146f728019bSEric W. Biederman *new = *entry; 1147f728019bSEric W. Biederman new++; 1148f728019bSEric W. Biederman } 1149f728019bSEric W. Biederman } 1150f728019bSEric W. Biederman 1151f728019bSEric W. Biederman /* Register everything except a directory full of subdirectories */ 1152f728019bSEric W. Biederman if (nr_files || !nr_dirs) { 1153f728019bSEric W. Biederman struct ctl_table_header *header; 1154f728019bSEric W. Biederman header = __register_sysctl_table(root, namespaces, path, files); 1155f728019bSEric W. Biederman if (!header) { 1156f728019bSEric W. Biederman kfree(ctl_table_arg); 1157f728019bSEric W. Biederman goto out; 1158f728019bSEric W. Biederman } 1159f728019bSEric W. Biederman 1160f728019bSEric W. Biederman /* Remember if we need to free the file table */ 1161f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1162f728019bSEric W. Biederman **subheader = header; 1163f728019bSEric W. Biederman (*subheader)++; 1164f728019bSEric W. Biederman } 1165f728019bSEric W. Biederman 1166f728019bSEric W. Biederman /* Recurse into the subdirectories. */ 1167f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1168f728019bSEric W. Biederman char *child_pos; 1169f728019bSEric W. Biederman 1170f728019bSEric W. Biederman if (!entry->child) 1171f728019bSEric W. Biederman continue; 1172f728019bSEric W. Biederman 1173f728019bSEric W. Biederman err = -ENAMETOOLONG; 1174f728019bSEric W. Biederman child_pos = append_path(path, pos, entry->procname); 1175f728019bSEric W. Biederman if (!child_pos) 1176f728019bSEric W. Biederman goto out; 1177f728019bSEric W. Biederman 1178f728019bSEric W. Biederman err = register_leaf_sysctl_tables(path, child_pos, subheader, 1179f728019bSEric W. Biederman root, namespaces, entry->child); 1180f728019bSEric W. Biederman pos[0] = '\0'; 1181f728019bSEric W. Biederman if (err) 1182f728019bSEric W. Biederman goto out; 1183f728019bSEric W. Biederman } 1184f728019bSEric W. Biederman err = 0; 1185f728019bSEric W. Biederman out: 1186f728019bSEric W. Biederman /* On failure our caller will unregister all registered subheaders */ 1187f728019bSEric W. Biederman return err; 1188f728019bSEric W. Biederman } 1189f728019bSEric W. Biederman 11906e9d5164SEric W. Biederman /** 11916e9d5164SEric W. Biederman * __register_sysctl_paths - register a sysctl table hierarchy 11926e9d5164SEric W. Biederman * @root: List of sysctl headers to register on 11936e9d5164SEric W. Biederman * @namespaces: Data to compute which lists of sysctl entries are visible 11946e9d5164SEric W. Biederman * @path: The path to the directory the sysctl table is in. 11956e9d5164SEric W. Biederman * @table: the top-level table structure 11966e9d5164SEric W. Biederman * 11976e9d5164SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 11986e9d5164SEric W. Biederman * array. A completely 0 filled entry terminates the table. 11996e9d5164SEric W. Biederman * 12006e9d5164SEric W. Biederman * See __register_sysctl_table for more details. 12016e9d5164SEric W. Biederman */ 12026e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths( 12036e9d5164SEric W. Biederman struct ctl_table_root *root, 12046e9d5164SEric W. Biederman struct nsproxy *namespaces, 12056e9d5164SEric W. Biederman const struct ctl_path *path, struct ctl_table *table) 12066e9d5164SEric W. Biederman { 1207ec6a5266SEric W. Biederman struct ctl_table *ctl_table_arg = table; 1208f728019bSEric W. Biederman int nr_subheaders = count_subheaders(table); 1209f728019bSEric W. Biederman struct ctl_table_header *header = NULL, **subheaders, **subheader; 12106e9d5164SEric W. Biederman const struct ctl_path *component; 12116e9d5164SEric W. Biederman char *new_path, *pos; 12126e9d5164SEric W. Biederman 12136e9d5164SEric W. Biederman pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL); 12146e9d5164SEric W. Biederman if (!new_path) 12156e9d5164SEric W. Biederman return NULL; 12166e9d5164SEric W. Biederman 12176e9d5164SEric W. Biederman pos[0] = '\0'; 12186e9d5164SEric W. Biederman for (component = path; component->procname; component++) { 12196e9d5164SEric W. Biederman pos = append_path(new_path, pos, component->procname); 12206e9d5164SEric W. Biederman if (!pos) 12216e9d5164SEric W. Biederman goto out; 12226e9d5164SEric W. Biederman } 1223ec6a5266SEric W. Biederman while (table->procname && table->child && !table[1].procname) { 1224ec6a5266SEric W. Biederman pos = append_path(new_path, pos, table->procname); 1225ec6a5266SEric W. Biederman if (!pos) 1226ec6a5266SEric W. Biederman goto out; 1227ec6a5266SEric W. Biederman table = table->child; 1228ec6a5266SEric W. Biederman } 1229f728019bSEric W. Biederman if (nr_subheaders == 1) { 12306e9d5164SEric W. Biederman header = __register_sysctl_table(root, namespaces, new_path, table); 1231ec6a5266SEric W. Biederman if (header) 1232ec6a5266SEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1233f728019bSEric W. Biederman } else { 1234f728019bSEric W. Biederman header = kzalloc(sizeof(*header) + 1235f728019bSEric W. Biederman sizeof(*subheaders)*nr_subheaders, GFP_KERNEL); 1236f728019bSEric W. Biederman if (!header) 1237f728019bSEric W. Biederman goto out; 1238f728019bSEric W. Biederman 1239f728019bSEric W. Biederman subheaders = (struct ctl_table_header **) (header + 1); 1240f728019bSEric W. Biederman subheader = subheaders; 1241f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1242f728019bSEric W. Biederman 1243f728019bSEric W. Biederman if (register_leaf_sysctl_tables(new_path, pos, &subheader, 1244f728019bSEric W. Biederman root, namespaces, table)) 1245f728019bSEric W. Biederman goto err_register_leaves; 1246f728019bSEric W. Biederman } 1247f728019bSEric W. Biederman 12486e9d5164SEric W. Biederman out: 12496e9d5164SEric W. Biederman kfree(new_path); 12506e9d5164SEric W. Biederman return header; 1251f728019bSEric W. Biederman 1252f728019bSEric W. Biederman err_register_leaves: 1253f728019bSEric W. Biederman while (subheader > subheaders) { 1254f728019bSEric W. Biederman struct ctl_table_header *subh = *(--subheader); 1255f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1256f728019bSEric W. Biederman unregister_sysctl_table(subh); 1257f728019bSEric W. Biederman kfree(table); 1258f728019bSEric W. Biederman } 1259f728019bSEric W. Biederman kfree(header); 1260f728019bSEric W. Biederman header = NULL; 1261f728019bSEric W. Biederman goto out; 12626e9d5164SEric W. Biederman } 12636e9d5164SEric W. Biederman 12641f87f0b5SEric W. Biederman /** 12651f87f0b5SEric W. Biederman * register_sysctl_table_path - register a sysctl table hierarchy 12661f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 12671f87f0b5SEric W. Biederman * @table: the top-level table structure 12681f87f0b5SEric W. Biederman * 12691f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 12701f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 12711f87f0b5SEric W. Biederman * 12721f87f0b5SEric W. Biederman * See __register_sysctl_paths for more details. 12731f87f0b5SEric W. Biederman */ 12741f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path, 12751f87f0b5SEric W. Biederman struct ctl_table *table) 12761f87f0b5SEric W. Biederman { 12771f87f0b5SEric W. Biederman return __register_sysctl_paths(&sysctl_table_root, current->nsproxy, 12781f87f0b5SEric W. Biederman path, table); 12791f87f0b5SEric W. Biederman } 12801f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths); 12811f87f0b5SEric W. Biederman 12821f87f0b5SEric W. Biederman /** 12831f87f0b5SEric W. Biederman * register_sysctl_table - register a sysctl table hierarchy 12841f87f0b5SEric W. Biederman * @table: the top-level table structure 12851f87f0b5SEric W. Biederman * 12861f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 12871f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 12881f87f0b5SEric W. Biederman * 12891f87f0b5SEric W. Biederman * See register_sysctl_paths for more details. 12901f87f0b5SEric W. Biederman */ 12911f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table) 12921f87f0b5SEric W. Biederman { 12931f87f0b5SEric W. Biederman static const struct ctl_path null_path[] = { {} }; 12941f87f0b5SEric W. Biederman 12951f87f0b5SEric W. Biederman return register_sysctl_paths(null_path, table); 12961f87f0b5SEric W. Biederman } 12971f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table); 12981f87f0b5SEric W. Biederman 1299938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header) 1300938aaa4fSEric W. Biederman { 1301938aaa4fSEric W. Biederman if (--header->nreg) 1302938aaa4fSEric W. Biederman return; 1303938aaa4fSEric W. Biederman 1304938aaa4fSEric W. Biederman start_unregistering(header); 1305938aaa4fSEric W. Biederman if (!--header->parent->count) { 1306938aaa4fSEric W. Biederman WARN_ON(1); 1307938aaa4fSEric W. Biederman kfree_rcu(header->parent, rcu); 1308938aaa4fSEric W. Biederman } 1309938aaa4fSEric W. Biederman if (!--header->count) 1310938aaa4fSEric W. Biederman kfree_rcu(header, rcu); 1311938aaa4fSEric W. Biederman } 1312938aaa4fSEric W. Biederman 13131f87f0b5SEric W. Biederman /** 13141f87f0b5SEric W. Biederman * unregister_sysctl_table - unregister a sysctl table hierarchy 13151f87f0b5SEric W. Biederman * @header: the header returned from register_sysctl_table 13161f87f0b5SEric W. Biederman * 13171f87f0b5SEric W. Biederman * Unregisters the sysctl table and all children. proc entries may not 13181f87f0b5SEric W. Biederman * actually be removed until they are no longer used by anyone. 13191f87f0b5SEric W. Biederman */ 13201f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header) 13211f87f0b5SEric W. Biederman { 1322f728019bSEric W. Biederman int nr_subheaders; 13231f87f0b5SEric W. Biederman might_sleep(); 13241f87f0b5SEric W. Biederman 13251f87f0b5SEric W. Biederman if (header == NULL) 13261f87f0b5SEric W. Biederman return; 13271f87f0b5SEric W. Biederman 1328f728019bSEric W. Biederman nr_subheaders = count_subheaders(header->ctl_table_arg); 1329f728019bSEric W. Biederman if (unlikely(nr_subheaders > 1)) { 1330f728019bSEric W. Biederman struct ctl_table_header **subheaders; 1331f728019bSEric W. Biederman int i; 1332f728019bSEric W. Biederman 1333f728019bSEric W. Biederman subheaders = (struct ctl_table_header **)(header + 1); 1334f728019bSEric W. Biederman for (i = nr_subheaders -1; i >= 0; i--) { 1335f728019bSEric W. Biederman struct ctl_table_header *subh = subheaders[i]; 1336f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1337f728019bSEric W. Biederman unregister_sysctl_table(subh); 1338f728019bSEric W. Biederman kfree(table); 1339f728019bSEric W. Biederman } 1340f728019bSEric W. Biederman kfree(header); 1341f728019bSEric W. Biederman return; 1342f728019bSEric W. Biederman } 1343f728019bSEric W. Biederman 13441f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1345938aaa4fSEric W. Biederman drop_sysctl_table(header); 13461f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 13471f87f0b5SEric W. Biederman } 13481f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table); 13491f87f0b5SEric W. Biederman 13501f87f0b5SEric W. Biederman void setup_sysctl_set(struct ctl_table_set *p, 13511f87f0b5SEric W. Biederman int (*is_seen)(struct ctl_table_set *)) 13521f87f0b5SEric W. Biederman { 13531f87f0b5SEric W. Biederman INIT_LIST_HEAD(&p->list); 13541f87f0b5SEric W. Biederman p->is_seen = is_seen; 13551f87f0b5SEric W. Biederman } 13561f87f0b5SEric W. Biederman 135797324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set) 135897324cd8SEric W. Biederman { 135997324cd8SEric W. Biederman WARN_ON(!list_empty(&set->list)); 136097324cd8SEric W. Biederman } 13611f87f0b5SEric W. Biederman 13621e0edd3fSAlexey Dobriyan int __init proc_sys_init(void) 136377b14db5SEric W. Biederman { 1364e1675231SAlexey Dobriyan struct proc_dir_entry *proc_sys_root; 1365e1675231SAlexey Dobriyan 136677b14db5SEric W. Biederman proc_sys_root = proc_mkdir("sys", NULL); 13679043476fSAl Viro proc_sys_root->proc_iops = &proc_sys_dir_operations; 13689043476fSAl Viro proc_sys_root->proc_fops = &proc_sys_dir_file_operations; 136977b14db5SEric W. Biederman proc_sys_root->nlink = 0; 1370de4e83bdSEric W. Biederman 1371de4e83bdSEric W. Biederman return sysctl_init(); 137277b14db5SEric W. Biederman } 1373