177b14db5SEric W. Biederman /* 277b14db5SEric W. Biederman * /proc/sys support 377b14db5SEric W. Biederman */ 41e0edd3fSAlexey Dobriyan #include <linux/init.h> 577b14db5SEric W. Biederman #include <linux/sysctl.h> 6f1ecf068SLucas De Marchi #include <linux/poll.h> 777b14db5SEric W. Biederman #include <linux/proc_fs.h> 877b14db5SEric W. Biederman #include <linux/security.h> 934286d66SNick Piggin #include <linux/namei.h> 101f87f0b5SEric W. Biederman #include <linux/module.h> 1177b14db5SEric W. Biederman #include "internal.h" 1277b14db5SEric W. Biederman 13d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations; 1477b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations; 1503a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations; 169043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations; 179043476fSAl Viro static const struct inode_operations proc_sys_dir_operations; 1877b14db5SEric W. Biederman 19f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll) 20f1ecf068SLucas De Marchi { 21f1ecf068SLucas De Marchi if (!poll) 22f1ecf068SLucas De Marchi return; 23f1ecf068SLucas De Marchi 24f1ecf068SLucas De Marchi atomic_inc(&poll->event); 25f1ecf068SLucas De Marchi wake_up_interruptible(&poll->wait); 26f1ecf068SLucas De Marchi } 27f1ecf068SLucas De Marchi 28a194558eSEric W. Biederman static struct ctl_table root_table[] = { 29a194558eSEric W. Biederman { 30a194558eSEric W. Biederman .procname = "", 317ec66d06SEric W. Biederman .mode = S_IFDIR|S_IRUGO|S_IXUGO, 32a194558eSEric W. Biederman }, 33a194558eSEric W. Biederman { } 34a194558eSEric W. Biederman }; 351f87f0b5SEric W. Biederman static struct ctl_table_root sysctl_table_root; 367ec66d06SEric W. Biederman static struct ctl_dir sysctl_root_dir = { 377ec66d06SEric W. Biederman .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, 447ec66d06SEric W. Biederman }, 451f87f0b5SEric W. Biederman }; 461f87f0b5SEric W. Biederman static struct ctl_table_root sysctl_table_root = { 471f87f0b5SEric W. Biederman .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list), 487ec66d06SEric W. Biederman .default_set.list = LIST_HEAD_INIT(sysctl_root_dir.header.ctl_entry), 499eb47c26SEric W. Biederman .default_set.root = &sysctl_table_root, 501f87f0b5SEric W. Biederman }; 511f87f0b5SEric W. Biederman 521f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock); 531f87f0b5SEric W. Biederman 547ec66d06SEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header); 557ec66d06SEric W. Biederman 56076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2) 57076c3eedSEric W. Biederman { 58076c3eedSEric W. Biederman int minlen; 59076c3eedSEric W. Biederman int cmp; 60076c3eedSEric W. Biederman 61076c3eedSEric W. Biederman minlen = len1; 62076c3eedSEric W. Biederman if (minlen > len2) 63076c3eedSEric W. Biederman minlen = len2; 64076c3eedSEric W. Biederman 65076c3eedSEric W. Biederman cmp = memcmp(name1, name2, minlen); 66076c3eedSEric W. Biederman if (cmp == 0) 67076c3eedSEric W. Biederman cmp = len1 - len2; 68076c3eedSEric W. Biederman return cmp; 69076c3eedSEric W. Biederman } 70076c3eedSEric W. Biederman 71076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead, 727ec66d06SEric W. Biederman struct ctl_table_set *set, struct ctl_dir *dir, 73076c3eedSEric W. Biederman const char *name, int namelen) 74076c3eedSEric W. Biederman { 75076c3eedSEric W. Biederman struct ctl_table_header *head; 76076c3eedSEric W. Biederman struct ctl_table *entry; 77076c3eedSEric W. Biederman 78076c3eedSEric W. Biederman list_for_each_entry(head, &set->list, ctl_entry) { 79076c3eedSEric W. Biederman if (head->unregistering) 80076c3eedSEric W. Biederman continue; 817ec66d06SEric W. Biederman if (head->parent != dir) 82076c3eedSEric W. Biederman continue; 837ec66d06SEric W. Biederman for (entry = head->ctl_table; entry->procname; entry++) { 84076c3eedSEric W. Biederman const char *procname = entry->procname; 85076c3eedSEric W. Biederman if (namecmp(procname, strlen(procname), name, namelen) == 0) { 86076c3eedSEric W. Biederman *phead = head; 87076c3eedSEric W. Biederman return entry; 88076c3eedSEric W. Biederman } 89076c3eedSEric W. Biederman } 90076c3eedSEric W. Biederman } 91076c3eedSEric W. Biederman return NULL; 92076c3eedSEric W. Biederman } 93076c3eedSEric W. Biederman 94e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head, 95e0d04529SEric W. Biederman struct ctl_table_root *root, struct ctl_table_set *set, 96e0d04529SEric W. Biederman struct ctl_table *table) 97e0d04529SEric W. Biederman { 987ec66d06SEric W. Biederman head->ctl_table = table; 99e0d04529SEric W. Biederman head->ctl_table_arg = table; 100e0d04529SEric W. Biederman INIT_LIST_HEAD(&head->ctl_entry); 101e0d04529SEric W. Biederman head->used = 0; 102e0d04529SEric W. Biederman head->count = 1; 103e0d04529SEric W. Biederman head->nreg = 1; 104e0d04529SEric W. Biederman head->unregistering = NULL; 105e0d04529SEric W. Biederman head->root = root; 106e0d04529SEric W. Biederman head->set = set; 107e0d04529SEric W. Biederman head->parent = NULL; 108e0d04529SEric W. Biederman } 109e0d04529SEric W. Biederman 1108425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head) 1118425d6aaSEric W. Biederman { 1128425d6aaSEric W. Biederman list_del_init(&head->ctl_entry); 1138425d6aaSEric W. Biederman } 1148425d6aaSEric W. Biederman 1157ec66d06SEric W. Biederman static void insert_header(struct ctl_dir *dir, struct ctl_table_header *header) 1168425d6aaSEric W. Biederman { 1177ec66d06SEric W. Biederman header->parent = dir; 1187ec66d06SEric W. Biederman header->parent->header.nreg++; 1198425d6aaSEric W. Biederman list_add_tail(&header->ctl_entry, &header->set->list); 1208425d6aaSEric W. Biederman } 1218425d6aaSEric W. Biederman 1221f87f0b5SEric W. Biederman /* called under sysctl_lock */ 1231f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p) 1241f87f0b5SEric W. Biederman { 1251f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 1261f87f0b5SEric W. Biederman return 0; 1271f87f0b5SEric W. Biederman p->used++; 1281f87f0b5SEric W. Biederman return 1; 1291f87f0b5SEric W. Biederman } 1301f87f0b5SEric W. Biederman 1311f87f0b5SEric W. Biederman /* called under sysctl_lock */ 1321f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p) 1331f87f0b5SEric W. Biederman { 1341f87f0b5SEric W. Biederman if (!--p->used) 1351f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 1361f87f0b5SEric W. Biederman complete(p->unregistering); 1371f87f0b5SEric W. Biederman } 1381f87f0b5SEric W. Biederman 1391f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */ 1401f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p) 1411f87f0b5SEric W. Biederman { 1421f87f0b5SEric W. Biederman /* 1431f87f0b5SEric W. Biederman * if p->used is 0, nobody will ever touch that entry again; 1441f87f0b5SEric W. Biederman * we'll eliminate all paths to it before dropping sysctl_lock 1451f87f0b5SEric W. Biederman */ 1461f87f0b5SEric W. Biederman if (unlikely(p->used)) { 1471f87f0b5SEric W. Biederman struct completion wait; 1481f87f0b5SEric W. Biederman init_completion(&wait); 1491f87f0b5SEric W. Biederman p->unregistering = &wait; 1501f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1511f87f0b5SEric W. Biederman wait_for_completion(&wait); 1521f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1531f87f0b5SEric W. Biederman } else { 1541f87f0b5SEric W. Biederman /* anything non-NULL; we'll never dereference it */ 1551f87f0b5SEric W. Biederman p->unregistering = ERR_PTR(-EINVAL); 1561f87f0b5SEric W. Biederman } 1571f87f0b5SEric W. Biederman /* 1581f87f0b5SEric W. Biederman * do not remove from the list until nobody holds it; walking the 1591f87f0b5SEric W. Biederman * list in do_sysctl() relies on that. 1601f87f0b5SEric W. Biederman */ 1618425d6aaSEric W. Biederman erase_header(p); 1621f87f0b5SEric W. Biederman } 1631f87f0b5SEric W. Biederman 1641f87f0b5SEric W. Biederman static void sysctl_head_get(struct ctl_table_header *head) 1651f87f0b5SEric W. Biederman { 1661f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1671f87f0b5SEric W. Biederman head->count++; 1681f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1691f87f0b5SEric W. Biederman } 1701f87f0b5SEric W. Biederman 1711f87f0b5SEric W. Biederman void sysctl_head_put(struct ctl_table_header *head) 1721f87f0b5SEric W. Biederman { 1731f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1741f87f0b5SEric W. Biederman if (!--head->count) 1751f87f0b5SEric W. Biederman kfree_rcu(head, rcu); 1761f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1771f87f0b5SEric W. Biederman } 1781f87f0b5SEric W. Biederman 1791f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head) 1801f87f0b5SEric W. Biederman { 1811f87f0b5SEric W. Biederman if (!head) 1821f87f0b5SEric W. Biederman BUG(); 1831f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1841f87f0b5SEric W. Biederman if (!use_table(head)) 1851f87f0b5SEric W. Biederman head = ERR_PTR(-ENOENT); 1861f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1871f87f0b5SEric W. Biederman return head; 1881f87f0b5SEric W. Biederman } 1891f87f0b5SEric W. Biederman 1901f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head) 1911f87f0b5SEric W. Biederman { 1921f87f0b5SEric W. Biederman if (!head) 1931f87f0b5SEric W. Biederman return; 1941f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1951f87f0b5SEric W. Biederman unuse_table(head); 1961f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 1971f87f0b5SEric W. Biederman } 1981f87f0b5SEric W. Biederman 1991f87f0b5SEric W. Biederman static struct ctl_table_set * 2001f87f0b5SEric W. Biederman lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces) 2011f87f0b5SEric W. Biederman { 2021f87f0b5SEric W. Biederman struct ctl_table_set *set = &root->default_set; 2031f87f0b5SEric W. Biederman if (root->lookup) 2041f87f0b5SEric W. Biederman set = root->lookup(root, namespaces); 2051f87f0b5SEric W. Biederman return set; 2061f87f0b5SEric W. Biederman } 2071f87f0b5SEric W. Biederman 2081f87f0b5SEric W. Biederman static struct list_head * 2091f87f0b5SEric W. Biederman lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces) 2101f87f0b5SEric W. Biederman { 2111f87f0b5SEric W. Biederman struct ctl_table_set *set = lookup_header_set(root, namespaces); 2121f87f0b5SEric W. Biederman return &set->list; 2131f87f0b5SEric W. Biederman } 2141f87f0b5SEric W. Biederman 215076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead, 2167ec66d06SEric W. Biederman struct ctl_dir *dir, 217076c3eedSEric W. Biederman const char *name, int namelen) 218076c3eedSEric W. Biederman { 219076c3eedSEric W. Biederman struct ctl_table_header *head; 220076c3eedSEric W. Biederman struct ctl_table *entry; 221076c3eedSEric W. Biederman struct ctl_table_root *root; 222076c3eedSEric W. Biederman struct ctl_table_set *set; 223076c3eedSEric W. Biederman 224076c3eedSEric W. Biederman spin_lock(&sysctl_lock); 225076c3eedSEric W. Biederman root = &sysctl_table_root; 226076c3eedSEric W. Biederman do { 227076c3eedSEric W. Biederman set = lookup_header_set(root, current->nsproxy); 2287ec66d06SEric W. Biederman entry = find_entry(&head, set, dir, name, namelen); 229076c3eedSEric W. Biederman if (entry && use_table(head)) 230076c3eedSEric W. Biederman *phead = head; 231076c3eedSEric W. Biederman else 232076c3eedSEric W. Biederman entry = NULL; 233076c3eedSEric W. Biederman root = list_entry(root->root_list.next, 234076c3eedSEric W. Biederman struct ctl_table_root, root_list); 235076c3eedSEric W. Biederman } while (!entry && root != &sysctl_table_root); 236076c3eedSEric W. Biederman spin_unlock(&sysctl_lock); 237076c3eedSEric W. Biederman return entry; 238076c3eedSEric W. Biederman } 239076c3eedSEric W. Biederman 2407ec66d06SEric W. Biederman static struct ctl_table_header *next_usable_entry(struct ctl_dir *dir, 2416a75ce16SEric W. Biederman struct ctl_table_root *root, struct list_head *tmp) 2421f87f0b5SEric W. Biederman { 2436a75ce16SEric W. Biederman struct nsproxy *namespaces = current->nsproxy; 2441f87f0b5SEric W. Biederman struct list_head *header_list; 2451f87f0b5SEric W. Biederman struct ctl_table_header *head; 2461f87f0b5SEric W. Biederman 2471f87f0b5SEric W. Biederman goto next; 2481f87f0b5SEric W. Biederman for (;;) { 2491f87f0b5SEric W. Biederman head = list_entry(tmp, struct ctl_table_header, ctl_entry); 2506a75ce16SEric W. Biederman root = head->root; 2511f87f0b5SEric W. Biederman 2527ec66d06SEric W. Biederman if (head->parent != dir || 2537ec66d06SEric W. Biederman !head->ctl_table->procname || 2546a75ce16SEric W. Biederman !use_table(head)) 2551f87f0b5SEric W. Biederman goto next; 2566a75ce16SEric W. Biederman 2571f87f0b5SEric W. Biederman return head; 2581f87f0b5SEric W. Biederman next: 2591f87f0b5SEric W. Biederman tmp = tmp->next; 2601f87f0b5SEric W. Biederman header_list = lookup_header_list(root, namespaces); 2611f87f0b5SEric W. Biederman if (tmp != header_list) 2621f87f0b5SEric W. Biederman continue; 2631f87f0b5SEric W. Biederman 2641f87f0b5SEric W. Biederman do { 2651f87f0b5SEric W. Biederman root = list_entry(root->root_list.next, 2661f87f0b5SEric W. Biederman struct ctl_table_root, root_list); 2671f87f0b5SEric W. Biederman if (root == &sysctl_table_root) 2681f87f0b5SEric W. Biederman goto out; 2691f87f0b5SEric W. Biederman header_list = lookup_header_list(root, namespaces); 2701f87f0b5SEric W. Biederman } while (list_empty(header_list)); 2711f87f0b5SEric W. Biederman tmp = header_list->next; 2721f87f0b5SEric W. Biederman } 2731f87f0b5SEric W. Biederman out: 2741f87f0b5SEric W. Biederman return NULL; 2751f87f0b5SEric W. Biederman } 2761f87f0b5SEric W. Biederman 2777ec66d06SEric W. Biederman static void first_entry(struct ctl_dir *dir, 2786a75ce16SEric W. Biederman struct ctl_table_header **phead, struct ctl_table **pentry) 2791f87f0b5SEric W. Biederman { 2807ec66d06SEric W. Biederman struct ctl_table_header *head; 2817ec66d06SEric W. Biederman struct ctl_table *entry = NULL; 2826a75ce16SEric W. Biederman 2836a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 2846a75ce16SEric W. Biederman head = next_usable_entry(dir, &sysctl_table_root, 2856a75ce16SEric W. Biederman &sysctl_table_root.default_set.list); 2866a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 2877ec66d06SEric W. Biederman if (head) 2887ec66d06SEric W. Biederman entry = head->ctl_table; 2896a75ce16SEric W. Biederman *phead = head; 2906a75ce16SEric W. Biederman *pentry = entry; 2916a75ce16SEric W. Biederman } 2926a75ce16SEric W. Biederman 2937ec66d06SEric W. Biederman static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry) 2946a75ce16SEric W. Biederman { 2956a75ce16SEric W. Biederman struct ctl_table_header *head = *phead; 2966a75ce16SEric W. Biederman struct ctl_table *entry = *pentry; 2976a75ce16SEric W. Biederman 2986a75ce16SEric W. Biederman entry++; 2996a75ce16SEric W. Biederman if (!entry->procname) { 3006a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 3016a75ce16SEric W. Biederman unuse_table(head); 3027ec66d06SEric W. Biederman head = next_usable_entry(head->parent, head->root, &head->ctl_entry); 3036a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 3046a75ce16SEric W. Biederman if (head) 3057ec66d06SEric W. Biederman entry = head->ctl_table; 3066a75ce16SEric W. Biederman } 3076a75ce16SEric W. Biederman *phead = head; 3086a75ce16SEric W. Biederman *pentry = entry; 3091f87f0b5SEric W. Biederman } 3101f87f0b5SEric W. Biederman 3111f87f0b5SEric W. Biederman void register_sysctl_root(struct ctl_table_root *root) 3121f87f0b5SEric W. Biederman { 3131f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 3141f87f0b5SEric W. Biederman list_add_tail(&root->root_list, &sysctl_table_root.root_list); 3151f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 3161f87f0b5SEric W. Biederman } 3171f87f0b5SEric W. Biederman 3181f87f0b5SEric W. Biederman /* 3191f87f0b5SEric W. Biederman * sysctl_perm does NOT grant the superuser all rights automatically, because 3201f87f0b5SEric W. Biederman * some sysctl variables are readonly even to root. 3211f87f0b5SEric W. Biederman */ 3221f87f0b5SEric W. Biederman 3231f87f0b5SEric W. Biederman static int test_perm(int mode, int op) 3241f87f0b5SEric W. Biederman { 3251f87f0b5SEric W. Biederman if (!current_euid()) 3261f87f0b5SEric W. Biederman mode >>= 6; 3271f87f0b5SEric W. Biederman else if (in_egroup_p(0)) 3281f87f0b5SEric W. Biederman mode >>= 3; 3291f87f0b5SEric W. Biederman if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0) 3301f87f0b5SEric W. Biederman return 0; 3311f87f0b5SEric W. Biederman return -EACCES; 3321f87f0b5SEric W. Biederman } 3331f87f0b5SEric W. Biederman 3341f87f0b5SEric W. Biederman static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op) 3351f87f0b5SEric W. Biederman { 3361f87f0b5SEric W. Biederman int mode; 3371f87f0b5SEric W. Biederman 3381f87f0b5SEric W. Biederman if (root->permissions) 3391f87f0b5SEric W. Biederman mode = root->permissions(root, current->nsproxy, table); 3401f87f0b5SEric W. Biederman else 3411f87f0b5SEric W. Biederman mode = table->mode; 3421f87f0b5SEric W. Biederman 3431f87f0b5SEric W. Biederman return test_perm(mode, op); 3441f87f0b5SEric W. Biederman } 3451f87f0b5SEric W. Biederman 3469043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb, 3479043476fSAl Viro struct ctl_table_header *head, struct ctl_table *table) 34877b14db5SEric W. Biederman { 34977b14db5SEric W. Biederman struct inode *inode; 3509043476fSAl Viro struct proc_inode *ei; 35177b14db5SEric W. Biederman 3529043476fSAl Viro inode = new_inode(sb); 35377b14db5SEric W. Biederman if (!inode) 35477b14db5SEric W. Biederman goto out; 35577b14db5SEric W. Biederman 35685fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 35785fe4025SChristoph Hellwig 3589043476fSAl Viro sysctl_head_get(head); 35977b14db5SEric W. Biederman ei = PROC_I(inode); 3609043476fSAl Viro ei->sysctl = head; 3619043476fSAl Viro ei->sysctl_entry = table; 3629043476fSAl Viro 36377b14db5SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 3649043476fSAl Viro inode->i_mode = table->mode; 3657ec66d06SEric W. Biederman if (!S_ISDIR(table->mode)) { 3669043476fSAl Viro inode->i_mode |= S_IFREG; 36777b14db5SEric W. Biederman inode->i_op = &proc_sys_inode_operations; 36877b14db5SEric W. Biederman inode->i_fop = &proc_sys_file_operations; 3699043476fSAl Viro } else { 3709043476fSAl Viro inode->i_mode |= S_IFDIR; 3719043476fSAl Viro inode->i_op = &proc_sys_dir_operations; 3729043476fSAl Viro inode->i_fop = &proc_sys_dir_file_operations; 3739043476fSAl Viro } 37477b14db5SEric W. Biederman out: 37577b14db5SEric W. Biederman return inode; 37677b14db5SEric W. Biederman } 37777b14db5SEric W. Biederman 37881324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode) 37977b14db5SEric W. Biederman { 3803cc3e046SEric W. Biederman struct ctl_table_header *head = PROC_I(inode)->sysctl; 3813cc3e046SEric W. Biederman if (!head) 3827ec66d06SEric W. Biederman head = &sysctl_root_dir.header; 3833cc3e046SEric W. Biederman return sysctl_head_grab(head); 38477b14db5SEric W. Biederman } 38577b14db5SEric W. Biederman 38677b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, 38777b14db5SEric W. Biederman struct nameidata *nd) 38877b14db5SEric W. Biederman { 3899043476fSAl Viro struct ctl_table_header *head = grab_header(dir); 3909043476fSAl Viro struct ctl_table_header *h = NULL; 3919043476fSAl Viro struct qstr *name = &dentry->d_name; 3929043476fSAl Viro struct ctl_table *p; 39377b14db5SEric W. Biederman struct inode *inode; 3949043476fSAl Viro struct dentry *err = ERR_PTR(-ENOENT); 3957ec66d06SEric W. Biederman struct ctl_dir *ctl_dir; 39677b14db5SEric W. Biederman 3979043476fSAl Viro if (IS_ERR(head)) 3989043476fSAl Viro return ERR_CAST(head); 3999043476fSAl Viro 4007ec66d06SEric W. Biederman ctl_dir = container_of(head, struct ctl_dir, header); 4019043476fSAl Viro 4027ec66d06SEric W. Biederman p = lookup_entry(&h, ctl_dir, name->name, name->len); 4039043476fSAl Viro if (!p) 40477b14db5SEric W. Biederman goto out; 40577b14db5SEric W. Biederman 40677b14db5SEric W. Biederman err = ERR_PTR(-ENOMEM); 4079043476fSAl Viro inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p); 4089043476fSAl Viro if (h) 4099043476fSAl Viro sysctl_head_finish(h); 4109043476fSAl Viro 41177b14db5SEric W. Biederman if (!inode) 41277b14db5SEric W. Biederman goto out; 41377b14db5SEric W. Biederman 41477b14db5SEric W. Biederman err = NULL; 415fb045adbSNick Piggin d_set_d_op(dentry, &proc_sys_dentry_operations); 41677b14db5SEric W. Biederman d_add(dentry, inode); 41777b14db5SEric W. Biederman 41877b14db5SEric W. Biederman out: 41977b14db5SEric W. Biederman sysctl_head_finish(head); 42077b14db5SEric W. Biederman return err; 42177b14db5SEric W. Biederman } 42277b14db5SEric W. Biederman 4237708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf, 4247708bfb1SPavel Emelyanov size_t count, loff_t *ppos, int write) 42577b14db5SEric W. Biederman { 4269043476fSAl Viro struct inode *inode = filp->f_path.dentry->d_inode; 4279043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 4289043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 4292a2da53bSDavid Howells ssize_t error; 4302a2da53bSDavid Howells size_t res; 43177b14db5SEric W. Biederman 4329043476fSAl Viro if (IS_ERR(head)) 4339043476fSAl Viro return PTR_ERR(head); 43477b14db5SEric W. Biederman 43577b14db5SEric W. Biederman /* 43677b14db5SEric W. Biederman * At this point we know that the sysctl was not unregistered 43777b14db5SEric W. Biederman * and won't be until we finish. 43877b14db5SEric W. Biederman */ 43977b14db5SEric W. Biederman error = -EPERM; 440d7321cd6SPavel Emelyanov if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ)) 44177b14db5SEric W. Biederman goto out; 44277b14db5SEric W. Biederman 4439043476fSAl Viro /* if that can happen at all, it should be -EINVAL, not -EISDIR */ 4449043476fSAl Viro error = -EINVAL; 4459043476fSAl Viro if (!table->proc_handler) 4469043476fSAl Viro goto out; 4479043476fSAl Viro 44877b14db5SEric W. Biederman /* careful: calling conventions are nasty here */ 44977b14db5SEric W. Biederman res = count; 4508d65af78SAlexey Dobriyan error = table->proc_handler(table, write, buf, &res, ppos); 45177b14db5SEric W. Biederman if (!error) 45277b14db5SEric W. Biederman error = res; 45377b14db5SEric W. Biederman out: 45477b14db5SEric W. Biederman sysctl_head_finish(head); 45577b14db5SEric W. Biederman 45677b14db5SEric W. Biederman return error; 45777b14db5SEric W. Biederman } 45877b14db5SEric W. Biederman 4597708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf, 4607708bfb1SPavel Emelyanov size_t count, loff_t *ppos) 4617708bfb1SPavel Emelyanov { 4627708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0); 4637708bfb1SPavel Emelyanov } 4647708bfb1SPavel Emelyanov 46577b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf, 46677b14db5SEric W. Biederman size_t count, loff_t *ppos) 46777b14db5SEric W. Biederman { 4687708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1); 46977b14db5SEric W. Biederman } 47077b14db5SEric W. Biederman 471f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp) 472f1ecf068SLucas De Marchi { 473f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 474f1ecf068SLucas De Marchi 475f1ecf068SLucas De Marchi if (table->poll) 476f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 477f1ecf068SLucas De Marchi 478f1ecf068SLucas De Marchi return 0; 479f1ecf068SLucas De Marchi } 480f1ecf068SLucas De Marchi 481f1ecf068SLucas De Marchi static unsigned int proc_sys_poll(struct file *filp, poll_table *wait) 482f1ecf068SLucas De Marchi { 483f1ecf068SLucas De Marchi struct inode *inode = filp->f_path.dentry->d_inode; 484f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 485f1ecf068SLucas De Marchi unsigned long event = (unsigned long)filp->private_data; 486f1ecf068SLucas De Marchi unsigned int ret = DEFAULT_POLLMASK; 487f1ecf068SLucas De Marchi 488f1ecf068SLucas De Marchi if (!table->proc_handler) 489f1ecf068SLucas De Marchi goto out; 490f1ecf068SLucas De Marchi 491f1ecf068SLucas De Marchi if (!table->poll) 492f1ecf068SLucas De Marchi goto out; 493f1ecf068SLucas De Marchi 494f1ecf068SLucas De Marchi poll_wait(filp, &table->poll->wait, wait); 495f1ecf068SLucas De Marchi 496f1ecf068SLucas De Marchi if (event != atomic_read(&table->poll->event)) { 497f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 498f1ecf068SLucas De Marchi ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI; 499f1ecf068SLucas De Marchi } 500f1ecf068SLucas De Marchi 501f1ecf068SLucas De Marchi out: 502f1ecf068SLucas De Marchi return ret; 503f1ecf068SLucas De Marchi } 50477b14db5SEric W. Biederman 50577b14db5SEric W. Biederman static int proc_sys_fill_cache(struct file *filp, void *dirent, 5069043476fSAl Viro filldir_t filldir, 5079043476fSAl Viro struct ctl_table_header *head, 5089043476fSAl Viro struct ctl_table *table) 50977b14db5SEric W. Biederman { 51077b14db5SEric W. Biederman struct dentry *child, *dir = filp->f_path.dentry; 51177b14db5SEric W. Biederman struct inode *inode; 51277b14db5SEric W. Biederman struct qstr qname; 51377b14db5SEric W. Biederman ino_t ino = 0; 51477b14db5SEric W. Biederman unsigned type = DT_UNKNOWN; 51577b14db5SEric W. Biederman 51677b14db5SEric W. Biederman qname.name = table->procname; 51777b14db5SEric W. Biederman qname.len = strlen(table->procname); 51877b14db5SEric W. Biederman qname.hash = full_name_hash(qname.name, qname.len); 51977b14db5SEric W. Biederman 52077b14db5SEric W. Biederman child = d_lookup(dir, &qname); 52177b14db5SEric W. Biederman if (!child) { 5229043476fSAl Viro child = d_alloc(dir, &qname); 5239043476fSAl Viro if (child) { 5249043476fSAl Viro inode = proc_sys_make_inode(dir->d_sb, head, table); 5259043476fSAl Viro if (!inode) { 5269043476fSAl Viro dput(child); 5279043476fSAl Viro return -ENOMEM; 5289043476fSAl Viro } else { 529fb045adbSNick Piggin d_set_d_op(child, &proc_sys_dentry_operations); 5309043476fSAl Viro d_add(child, inode); 53177b14db5SEric W. Biederman } 5329043476fSAl Viro } else { 5339043476fSAl Viro return -ENOMEM; 53477b14db5SEric W. Biederman } 53577b14db5SEric W. Biederman } 53677b14db5SEric W. Biederman inode = child->d_inode; 53777b14db5SEric W. Biederman ino = inode->i_ino; 53877b14db5SEric W. Biederman type = inode->i_mode >> 12; 53977b14db5SEric W. Biederman dput(child); 5409043476fSAl Viro return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type); 5419043476fSAl Viro } 5429043476fSAl Viro 5439043476fSAl Viro static int scan(struct ctl_table_header *head, ctl_table *table, 5449043476fSAl Viro unsigned long *pos, struct file *file, 5459043476fSAl Viro void *dirent, filldir_t filldir) 5469043476fSAl Viro { 5479043476fSAl Viro int res; 5489043476fSAl Viro 5496a75ce16SEric W. Biederman if ((*pos)++ < file->f_pos) 5506a75ce16SEric W. Biederman return 0; 5519043476fSAl Viro 5529043476fSAl Viro res = proc_sys_fill_cache(file, dirent, filldir, head, table); 5539043476fSAl Viro 5546a75ce16SEric W. Biederman if (res == 0) 5556a75ce16SEric W. Biederman file->f_pos = *pos; 5566a75ce16SEric W. Biederman 5576a75ce16SEric W. Biederman return res; 55877b14db5SEric W. Biederman } 55977b14db5SEric W. Biederman 56077b14db5SEric W. Biederman static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir) 56177b14db5SEric W. Biederman { 5629043476fSAl Viro struct dentry *dentry = filp->f_path.dentry; 56377b14db5SEric W. Biederman struct inode *inode = dentry->d_inode; 5649043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 5659043476fSAl Viro struct ctl_table_header *h = NULL; 5666a75ce16SEric W. Biederman struct ctl_table *entry; 5677ec66d06SEric W. Biederman struct ctl_dir *ctl_dir; 56877b14db5SEric W. Biederman unsigned long pos; 5699043476fSAl Viro int ret = -EINVAL; 57077b14db5SEric W. Biederman 5719043476fSAl Viro if (IS_ERR(head)) 5729043476fSAl Viro return PTR_ERR(head); 5739043476fSAl Viro 5747ec66d06SEric W. Biederman ctl_dir = container_of(head, struct ctl_dir, header); 57577b14db5SEric W. Biederman 57677b14db5SEric W. Biederman ret = 0; 57777b14db5SEric W. Biederman /* Avoid a switch here: arm builds fail with missing __cmpdi2 */ 57877b14db5SEric W. Biederman if (filp->f_pos == 0) { 57977b14db5SEric W. Biederman if (filldir(dirent, ".", 1, filp->f_pos, 58077b14db5SEric W. Biederman inode->i_ino, DT_DIR) < 0) 58177b14db5SEric W. Biederman goto out; 58277b14db5SEric W. Biederman filp->f_pos++; 58377b14db5SEric W. Biederman } 58477b14db5SEric W. Biederman if (filp->f_pos == 1) { 58577b14db5SEric W. Biederman if (filldir(dirent, "..", 2, filp->f_pos, 58677b14db5SEric W. Biederman parent_ino(dentry), DT_DIR) < 0) 58777b14db5SEric W. Biederman goto out; 58877b14db5SEric W. Biederman filp->f_pos++; 58977b14db5SEric W. Biederman } 59077b14db5SEric W. Biederman pos = 2; 59177b14db5SEric W. Biederman 5927ec66d06SEric W. Biederman for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) { 5936a75ce16SEric W. Biederman ret = scan(h, entry, &pos, filp, dirent, filldir); 5949043476fSAl Viro if (ret) { 5959043476fSAl Viro sysctl_head_finish(h); 5969043476fSAl Viro break; 59777b14db5SEric W. Biederman } 59877b14db5SEric W. Biederman } 59977b14db5SEric W. Biederman ret = 1; 60077b14db5SEric W. Biederman out: 60177b14db5SEric W. Biederman sysctl_head_finish(head); 60277b14db5SEric W. Biederman return ret; 60377b14db5SEric W. Biederman } 60477b14db5SEric W. Biederman 60510556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask) 60677b14db5SEric W. Biederman { 60777b14db5SEric W. Biederman /* 60877b14db5SEric W. Biederman * sysctl entries that are not writeable, 60977b14db5SEric W. Biederman * are _NOT_ writeable, capabilities or not. 61077b14db5SEric W. Biederman */ 611f696a365SMiklos Szeredi struct ctl_table_header *head; 612f696a365SMiklos Szeredi struct ctl_table *table; 61377b14db5SEric W. Biederman int error; 61477b14db5SEric W. Biederman 615f696a365SMiklos Szeredi /* Executable files are not allowed under /proc/sys/ */ 616f696a365SMiklos Szeredi if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) 617f696a365SMiklos Szeredi return -EACCES; 618f696a365SMiklos Szeredi 619f696a365SMiklos Szeredi head = grab_header(inode); 6209043476fSAl Viro if (IS_ERR(head)) 6219043476fSAl Viro return PTR_ERR(head); 62277b14db5SEric W. Biederman 623f696a365SMiklos Szeredi table = PROC_I(inode)->sysctl_entry; 6249043476fSAl Viro if (!table) /* global root - r-xr-xr-x */ 6259043476fSAl Viro error = mask & MAY_WRITE ? -EACCES : 0; 6269043476fSAl Viro else /* Use the permissions on the sysctl table entry */ 6271fc0f78cSAl Viro error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK); 6289043476fSAl Viro 62977b14db5SEric W. Biederman sysctl_head_finish(head); 63077b14db5SEric W. Biederman return error; 63177b14db5SEric W. Biederman } 63277b14db5SEric W. Biederman 63377b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr) 63477b14db5SEric W. Biederman { 63577b14db5SEric W. Biederman struct inode *inode = dentry->d_inode; 63677b14db5SEric W. Biederman int error; 63777b14db5SEric W. Biederman 63877b14db5SEric W. Biederman if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) 63977b14db5SEric W. Biederman return -EPERM; 64077b14db5SEric W. Biederman 64177b14db5SEric W. Biederman error = inode_change_ok(inode, attr); 6421025774cSChristoph Hellwig if (error) 64377b14db5SEric W. Biederman return error; 6441025774cSChristoph Hellwig 6451025774cSChristoph Hellwig if ((attr->ia_valid & ATTR_SIZE) && 6461025774cSChristoph Hellwig attr->ia_size != i_size_read(inode)) { 6471025774cSChristoph Hellwig error = vmtruncate(inode, attr->ia_size); 6481025774cSChristoph Hellwig if (error) 6491025774cSChristoph Hellwig return error; 6501025774cSChristoph Hellwig } 6511025774cSChristoph Hellwig 6521025774cSChristoph Hellwig setattr_copy(inode, attr); 6531025774cSChristoph Hellwig mark_inode_dirty(inode); 6541025774cSChristoph Hellwig return 0; 65577b14db5SEric W. Biederman } 65677b14db5SEric W. Biederman 6579043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 6589043476fSAl Viro { 6599043476fSAl Viro struct inode *inode = dentry->d_inode; 6609043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 6619043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 6629043476fSAl Viro 6639043476fSAl Viro if (IS_ERR(head)) 6649043476fSAl Viro return PTR_ERR(head); 6659043476fSAl Viro 6669043476fSAl Viro generic_fillattr(inode, stat); 6679043476fSAl Viro if (table) 6689043476fSAl Viro stat->mode = (stat->mode & S_IFMT) | table->mode; 6699043476fSAl Viro 6709043476fSAl Viro sysctl_head_finish(head); 6719043476fSAl Viro return 0; 6729043476fSAl Viro } 6739043476fSAl Viro 67477b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = { 675f1ecf068SLucas De Marchi .open = proc_sys_open, 676f1ecf068SLucas De Marchi .poll = proc_sys_poll, 67777b14db5SEric W. Biederman .read = proc_sys_read, 67877b14db5SEric W. Biederman .write = proc_sys_write, 6796038f373SArnd Bergmann .llseek = default_llseek, 6809043476fSAl Viro }; 6819043476fSAl Viro 6829043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = { 683887df078SPavel Emelyanov .read = generic_read_dir, 68477b14db5SEric W. Biederman .readdir = proc_sys_readdir, 6853222a3e5SChristoph Hellwig .llseek = generic_file_llseek, 68677b14db5SEric W. Biederman }; 68777b14db5SEric W. Biederman 68803a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = { 6899043476fSAl Viro .permission = proc_sys_permission, 6909043476fSAl Viro .setattr = proc_sys_setattr, 6919043476fSAl Viro .getattr = proc_sys_getattr, 6929043476fSAl Viro }; 6939043476fSAl Viro 6949043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = { 69577b14db5SEric W. Biederman .lookup = proc_sys_lookup, 69677b14db5SEric W. Biederman .permission = proc_sys_permission, 69777b14db5SEric W. Biederman .setattr = proc_sys_setattr, 6989043476fSAl Viro .getattr = proc_sys_getattr, 69977b14db5SEric W. Biederman }; 70077b14db5SEric W. Biederman 70177b14db5SEric W. Biederman static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd) 70277b14db5SEric W. Biederman { 70334286d66SNick Piggin if (nd->flags & LOOKUP_RCU) 70434286d66SNick Piggin return -ECHILD; 7059043476fSAl Viro return !PROC_I(dentry->d_inode)->sysctl->unregistering; 7069043476fSAl Viro } 7079043476fSAl Viro 708fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry) 7099043476fSAl Viro { 7109043476fSAl Viro return !!PROC_I(dentry->d_inode)->sysctl->unregistering; 7119043476fSAl Viro } 7129043476fSAl Viro 7131f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p) 7141f87f0b5SEric W. Biederman { 7151f87f0b5SEric W. Biederman struct ctl_table_set *set = p->set; 7161f87f0b5SEric W. Biederman int res; 7171f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 7181f87f0b5SEric W. Biederman if (p->unregistering) 7191f87f0b5SEric W. Biederman res = 0; 7201f87f0b5SEric W. Biederman else if (!set->is_seen) 7211f87f0b5SEric W. Biederman res = 1; 7221f87f0b5SEric W. Biederman else 7231f87f0b5SEric W. Biederman res = set->is_seen(set); 7241f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 7251f87f0b5SEric W. Biederman return res; 7261f87f0b5SEric W. Biederman } 7271f87f0b5SEric W. Biederman 728621e155aSNick Piggin static int proc_sys_compare(const struct dentry *parent, 729621e155aSNick Piggin const struct inode *pinode, 730621e155aSNick Piggin const struct dentry *dentry, const struct inode *inode, 731621e155aSNick Piggin unsigned int len, const char *str, const struct qstr *name) 7329043476fSAl Viro { 733dfef6dcdSAl Viro struct ctl_table_header *head; 73431e6b01fSNick Piggin /* Although proc doesn't have negative dentries, rcu-walk means 73531e6b01fSNick Piggin * that inode here can be NULL */ 736dfef6dcdSAl Viro /* AV: can it, indeed? */ 73731e6b01fSNick Piggin if (!inode) 738dfef6dcdSAl Viro return 1; 739621e155aSNick Piggin if (name->len != len) 7409043476fSAl Viro return 1; 741621e155aSNick Piggin if (memcmp(name->name, str, len)) 7429043476fSAl Viro return 1; 743dfef6dcdSAl Viro head = rcu_dereference(PROC_I(inode)->sysctl); 744dfef6dcdSAl Viro return !head || !sysctl_is_seen(head); 74577b14db5SEric W. Biederman } 74677b14db5SEric W. Biederman 747d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = { 74877b14db5SEric W. Biederman .d_revalidate = proc_sys_revalidate, 7499043476fSAl Viro .d_delete = proc_sys_delete, 7509043476fSAl Viro .d_compare = proc_sys_compare, 75177b14db5SEric W. Biederman }; 75277b14db5SEric W. Biederman 7537ec66d06SEric W. Biederman static struct ctl_dir *find_subdir(struct ctl_table_set *set, struct ctl_dir *dir, 7547ec66d06SEric W. Biederman const char *name, int namelen) 7551f87f0b5SEric W. Biederman { 7567ec66d06SEric W. Biederman struct ctl_table_header *head; 7577ec66d06SEric W. Biederman struct ctl_table *entry; 7581f87f0b5SEric W. Biederman 7597ec66d06SEric W. Biederman entry = find_entry(&head, set, dir, name, namelen); 7607ec66d06SEric W. Biederman if (!entry) 7617ec66d06SEric W. Biederman return ERR_PTR(-ENOENT); 7627ec66d06SEric W. Biederman if (S_ISDIR(entry->mode)) 7637ec66d06SEric W. Biederman return container_of(head, struct ctl_dir, header); 7647ec66d06SEric W. Biederman return ERR_PTR(-ENOTDIR); 7651f87f0b5SEric W. Biederman } 7661f87f0b5SEric W. Biederman 7677ec66d06SEric W. Biederman static struct ctl_dir *new_dir(struct ctl_table_set *set, 7687ec66d06SEric W. Biederman const char *name, int namelen) 7691f87f0b5SEric W. Biederman { 7707ec66d06SEric W. Biederman struct ctl_table *table; 7717ec66d06SEric W. Biederman struct ctl_dir *new; 7727ec66d06SEric W. Biederman char *new_name; 7731f87f0b5SEric W. Biederman 7747ec66d06SEric W. Biederman new = kzalloc(sizeof(*new) + sizeof(struct ctl_table)*2 + 7757ec66d06SEric W. Biederman namelen + 1, GFP_KERNEL); 7767ec66d06SEric W. Biederman if (!new) 7777ec66d06SEric W. Biederman return NULL; 7787ec66d06SEric W. Biederman 7797ec66d06SEric W. Biederman table = (struct ctl_table *)(new + 1); 7807ec66d06SEric W. Biederman new_name = (char *)(table + 2); 7817ec66d06SEric W. Biederman memcpy(new_name, name, namelen); 7827ec66d06SEric W. Biederman new_name[namelen] = '\0'; 7837ec66d06SEric W. Biederman table[0].procname = new_name; 7847ec66d06SEric W. Biederman table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO; 7857ec66d06SEric W. Biederman init_header(&new->header, set->root, set, table); 7867ec66d06SEric W. Biederman 7877ec66d06SEric W. Biederman return new; 7881f87f0b5SEric W. Biederman } 7891f87f0b5SEric W. Biederman 7907ec66d06SEric W. Biederman static struct ctl_dir *get_subdir(struct ctl_table_set *set, 7917ec66d06SEric W. Biederman struct ctl_dir *dir, const char *name, int namelen) 7927ec66d06SEric W. Biederman { 7937ec66d06SEric W. Biederman struct ctl_dir *subdir, *new = NULL; 7947ec66d06SEric W. Biederman 7957ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 7967ec66d06SEric W. Biederman subdir = find_subdir(dir->header.set, dir, name, namelen); 7977ec66d06SEric W. Biederman if (!IS_ERR(subdir)) 7987ec66d06SEric W. Biederman goto found; 7997ec66d06SEric W. Biederman if ((PTR_ERR(subdir) == -ENOENT) && set != dir->header.set) 8007ec66d06SEric W. Biederman subdir = find_subdir(set, dir, name, namelen); 8017ec66d06SEric W. Biederman if (!IS_ERR(subdir)) 8027ec66d06SEric W. Biederman goto found; 8037ec66d06SEric W. Biederman if (PTR_ERR(subdir) != -ENOENT) 8047ec66d06SEric W. Biederman goto failed; 8057ec66d06SEric W. Biederman 8067ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 8077ec66d06SEric W. Biederman new = new_dir(set, name, namelen); 8087ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 8097ec66d06SEric W. Biederman subdir = ERR_PTR(-ENOMEM); 8107ec66d06SEric W. Biederman if (!new) 8117ec66d06SEric W. Biederman goto failed; 8127ec66d06SEric W. Biederman 8137ec66d06SEric W. Biederman subdir = find_subdir(set, dir, name, namelen); 8147ec66d06SEric W. Biederman if (!IS_ERR(subdir)) 8157ec66d06SEric W. Biederman goto found; 8167ec66d06SEric W. Biederman if (PTR_ERR(subdir) != -ENOENT) 8177ec66d06SEric W. Biederman goto failed; 8187ec66d06SEric W. Biederman 8197ec66d06SEric W. Biederman insert_header(dir, &new->header); 8207ec66d06SEric W. Biederman subdir = new; 8217ec66d06SEric W. Biederman found: 8227ec66d06SEric W. Biederman subdir->header.nreg++; 8237ec66d06SEric W. Biederman failed: 8247ec66d06SEric W. Biederman if (unlikely(IS_ERR(subdir))) { 8257ec66d06SEric W. Biederman printk(KERN_ERR "sysctl could not get directory: %*.*s %ld\n", 8267ec66d06SEric W. Biederman namelen, namelen, name, PTR_ERR(subdir)); 8271f87f0b5SEric W. Biederman } 8287ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 8297ec66d06SEric W. Biederman if (new) 8307ec66d06SEric W. Biederman drop_sysctl_table(&new->header); 8317ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 8327ec66d06SEric W. Biederman return subdir; 8331f87f0b5SEric W. Biederman } 8341f87f0b5SEric W. Biederman 8357c60c48fSEric W. Biederman static int sysctl_check_table_dups(const char *path, struct ctl_table *old, 8361f87f0b5SEric W. Biederman struct ctl_table *table) 8371f87f0b5SEric W. Biederman { 8387c60c48fSEric W. Biederman struct ctl_table *entry, *test; 8391f87f0b5SEric W. Biederman int error = 0; 8401f87f0b5SEric W. Biederman 8417c60c48fSEric W. Biederman for (entry = old; entry->procname; entry++) { 8427c60c48fSEric W. Biederman for (test = table; test->procname; test++) { 8437c60c48fSEric W. Biederman if (strcmp(entry->procname, test->procname) == 0) { 8447c60c48fSEric W. Biederman printk(KERN_ERR "sysctl duplicate entry: %s/%s\n", 8457c60c48fSEric W. Biederman path, test->procname); 8467c60c48fSEric W. Biederman error = -EEXIST; 8471f87f0b5SEric W. Biederman } 8487c60c48fSEric W. Biederman } 8497c60c48fSEric W. Biederman } 8507c60c48fSEric W. Biederman return error; 8517c60c48fSEric W. Biederman } 8527c60c48fSEric W. Biederman 8537c60c48fSEric W. Biederman static int sysctl_check_dups(struct nsproxy *namespaces, 8547ec66d06SEric W. Biederman struct ctl_dir *dir, 8557c60c48fSEric W. Biederman const char *path, struct ctl_table *table) 8567c60c48fSEric W. Biederman { 8577c60c48fSEric W. Biederman struct ctl_table_root *root; 8587c60c48fSEric W. Biederman struct ctl_table_set *set; 8597ec66d06SEric W. Biederman struct ctl_table_header *head; 8607c60c48fSEric W. Biederman int error = 0; 8617c60c48fSEric W. Biederman 8627c60c48fSEric W. Biederman root = &sysctl_table_root; 8637c60c48fSEric W. Biederman do { 8647c60c48fSEric W. Biederman set = lookup_header_set(root, namespaces); 8657c60c48fSEric W. Biederman 8667c60c48fSEric W. Biederman list_for_each_entry(head, &set->list, ctl_entry) { 8677c60c48fSEric W. Biederman if (head->unregistering) 8687c60c48fSEric W. Biederman continue; 8697ec66d06SEric W. Biederman if (head->parent != dir) 8707c60c48fSEric W. Biederman continue; 8717ec66d06SEric W. Biederman error = sysctl_check_table_dups(path, head->ctl_table, 8727c60c48fSEric W. Biederman table); 8737c60c48fSEric W. Biederman } 8747c60c48fSEric W. Biederman root = list_entry(root->root_list.next, 8757c60c48fSEric W. Biederman struct ctl_table_root, root_list); 8767c60c48fSEric W. Biederman } while (root != &sysctl_table_root); 8777c60c48fSEric W. Biederman return error; 8787c60c48fSEric W. Biederman } 8797c60c48fSEric W. Biederman 8807c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...) 8817c60c48fSEric W. Biederman { 8827c60c48fSEric W. Biederman struct va_format vaf; 8837c60c48fSEric W. Biederman va_list args; 8847c60c48fSEric W. Biederman 8857c60c48fSEric W. Biederman va_start(args, fmt); 8867c60c48fSEric W. Biederman vaf.fmt = fmt; 8877c60c48fSEric W. Biederman vaf.va = &args; 8887c60c48fSEric W. Biederman 8897c60c48fSEric W. Biederman printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n", 8907c60c48fSEric W. Biederman path, table->procname, &vaf); 8917c60c48fSEric W. Biederman 8927c60c48fSEric W. Biederman va_end(args); 8937c60c48fSEric W. Biederman return -EINVAL; 8947c60c48fSEric W. Biederman } 8957c60c48fSEric W. Biederman 8967c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table) 8977c60c48fSEric W. Biederman { 8987c60c48fSEric W. Biederman int err = 0; 8997c60c48fSEric W. Biederman for (; table->procname; table++) { 9007c60c48fSEric W. Biederman if (table->child) 9017c60c48fSEric W. Biederman err = sysctl_err(path, table, "Not a file"); 9027c60c48fSEric W. Biederman 9031f87f0b5SEric W. Biederman if ((table->proc_handler == proc_dostring) || 9041f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec) || 9051f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_minmax) || 9061f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_jiffies) || 9071f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_userhz_jiffies) || 9081f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_ms_jiffies) || 9091f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_minmax) || 9101f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) { 9111f87f0b5SEric W. Biederman if (!table->data) 9127c60c48fSEric W. Biederman err = sysctl_err(path, table, "No data"); 9131f87f0b5SEric W. Biederman if (!table->maxlen) 9147c60c48fSEric W. Biederman err = sysctl_err(path, table, "No maxlen"); 9151f87f0b5SEric W. Biederman } 9161f87f0b5SEric W. Biederman if (!table->proc_handler) 9177c60c48fSEric W. Biederman err = sysctl_err(path, table, "No proc_handler"); 9187c60c48fSEric W. Biederman 9197c60c48fSEric W. Biederman if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode) 9207c60c48fSEric W. Biederman err = sysctl_err(path, table, "bogus .mode 0%o", 9217c60c48fSEric W. Biederman table->mode); 9221f87f0b5SEric W. Biederman } 9237c60c48fSEric W. Biederman return err; 9241f87f0b5SEric W. Biederman } 9251f87f0b5SEric W. Biederman 9261f87f0b5SEric W. Biederman /** 927f728019bSEric W. Biederman * __register_sysctl_table - register a leaf sysctl table 9281f87f0b5SEric W. Biederman * @root: List of sysctl headers to register on 9291f87f0b5SEric W. Biederman * @namespaces: Data to compute which lists of sysctl entries are visible 9301f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 9311f87f0b5SEric W. Biederman * @table: the top-level table structure 9321f87f0b5SEric W. Biederman * 9331f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 9341f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 9351f87f0b5SEric W. Biederman * 9361f87f0b5SEric W. Biederman * The members of the &struct ctl_table structure are used as follows: 9371f87f0b5SEric W. Biederman * 9381f87f0b5SEric W. Biederman * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not 9391f87f0b5SEric W. Biederman * enter a sysctl file 9401f87f0b5SEric W. Biederman * 9411f87f0b5SEric W. Biederman * data - a pointer to data for use by proc_handler 9421f87f0b5SEric W. Biederman * 9431f87f0b5SEric W. Biederman * maxlen - the maximum size in bytes of the data 9441f87f0b5SEric W. Biederman * 945f728019bSEric W. Biederman * mode - the file permissions for the /proc/sys file 9461f87f0b5SEric W. Biederman * 947f728019bSEric W. Biederman * child - must be %NULL. 9481f87f0b5SEric W. Biederman * 9491f87f0b5SEric W. Biederman * proc_handler - the text handler routine (described below) 9501f87f0b5SEric W. Biederman * 9511f87f0b5SEric W. Biederman * extra1, extra2 - extra pointers usable by the proc handler routines 9521f87f0b5SEric W. Biederman * 9531f87f0b5SEric W. Biederman * Leaf nodes in the sysctl tree will be represented by a single file 9541f87f0b5SEric W. Biederman * under /proc; non-leaf nodes will be represented by directories. 9551f87f0b5SEric W. Biederman * 956f728019bSEric W. Biederman * There must be a proc_handler routine for any terminal nodes. 957f728019bSEric W. Biederman * Several default handlers are available to cover common cases - 9581f87f0b5SEric W. Biederman * 9591f87f0b5SEric W. Biederman * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(), 9601f87f0b5SEric W. Biederman * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), 9611f87f0b5SEric W. Biederman * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax() 9621f87f0b5SEric W. Biederman * 9631f87f0b5SEric W. Biederman * It is the handler's job to read the input buffer from user memory 9641f87f0b5SEric W. Biederman * and process it. The handler should return 0 on success. 9651f87f0b5SEric W. Biederman * 9661f87f0b5SEric W. Biederman * This routine returns %NULL on a failure to register, and a pointer 9671f87f0b5SEric W. Biederman * to the table header on success. 9681f87f0b5SEric W. Biederman */ 9696e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table( 9701f87f0b5SEric W. Biederman struct ctl_table_root *root, 9711f87f0b5SEric W. Biederman struct nsproxy *namespaces, 9726e9d5164SEric W. Biederman const char *path, struct ctl_table *table) 9731f87f0b5SEric W. Biederman { 9741f87f0b5SEric W. Biederman struct ctl_table_header *header; 9756e9d5164SEric W. Biederman const char *name, *nextname; 9761f87f0b5SEric W. Biederman struct ctl_table_set *set; 9777ec66d06SEric W. Biederman struct ctl_dir *dir; 9781f87f0b5SEric W. Biederman 9797ec66d06SEric W. Biederman header = kzalloc(sizeof(struct ctl_table_header), GFP_KERNEL); 9801f87f0b5SEric W. Biederman if (!header) 9811f87f0b5SEric W. Biederman return NULL; 9821f87f0b5SEric W. Biederman 983e0d04529SEric W. Biederman init_header(header, root, NULL, table); 9847c60c48fSEric W. Biederman if (sysctl_check_table(path, table)) 9857c60c48fSEric W. Biederman goto fail; 9868d6ecfccSEric W. Biederman 9871f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 9887ec66d06SEric W. Biederman header->set = set = lookup_header_set(root, namespaces); 9897ec66d06SEric W. Biederman dir = &sysctl_root_dir; 9907ec66d06SEric W. Biederman dir->header.nreg++; 9917ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 9927ec66d06SEric W. Biederman 9937ec66d06SEric W. Biederman /* Find the directory for the ctl_table */ 9947ec66d06SEric W. Biederman for (name = path; name; name = nextname) { 9957ec66d06SEric W. Biederman int namelen; 9967ec66d06SEric W. Biederman nextname = strchr(name, '/'); 9977ec66d06SEric W. Biederman if (nextname) { 9987ec66d06SEric W. Biederman namelen = nextname - name; 9997ec66d06SEric W. Biederman nextname++; 10007ec66d06SEric W. Biederman } else { 10017ec66d06SEric W. Biederman namelen = strlen(name); 10027ec66d06SEric W. Biederman } 10037ec66d06SEric W. Biederman if (namelen == 0) 10041f87f0b5SEric W. Biederman continue; 10057ec66d06SEric W. Biederman 10067ec66d06SEric W. Biederman dir = get_subdir(set, dir, name, namelen); 10077ec66d06SEric W. Biederman if (IS_ERR(dir)) 10087ec66d06SEric W. Biederman goto fail; 10091f87f0b5SEric W. Biederman } 10107ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 10117ec66d06SEric W. Biederman if (sysctl_check_dups(namespaces, dir, path, table)) 10127ec66d06SEric W. Biederman goto fail_put_dir_locked; 10137ec66d06SEric W. Biederman insert_header(dir, header); 10147ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 10151f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 10161f87f0b5SEric W. Biederman 10171f87f0b5SEric W. Biederman return header; 10187ec66d06SEric W. Biederman fail_put_dir_locked: 10197ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 10207c60c48fSEric W. Biederman spin_unlock(&sysctl_lock); 10217c60c48fSEric W. Biederman fail: 10227c60c48fSEric W. Biederman kfree(header); 10237c60c48fSEric W. Biederman dump_stack(); 10247c60c48fSEric W. Biederman return NULL; 10251f87f0b5SEric W. Biederman } 10261f87f0b5SEric W. Biederman 10276e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name) 10286e9d5164SEric W. Biederman { 10296e9d5164SEric W. Biederman int namelen; 10306e9d5164SEric W. Biederman namelen = strlen(name); 10316e9d5164SEric W. Biederman if (((pos - path) + namelen + 2) >= PATH_MAX) 10326e9d5164SEric W. Biederman return NULL; 10336e9d5164SEric W. Biederman memcpy(pos, name, namelen); 10346e9d5164SEric W. Biederman pos[namelen] = '/'; 10356e9d5164SEric W. Biederman pos[namelen + 1] = '\0'; 10366e9d5164SEric W. Biederman pos += namelen + 1; 10376e9d5164SEric W. Biederman return pos; 10386e9d5164SEric W. Biederman } 10396e9d5164SEric W. Biederman 1040f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table) 1041f728019bSEric W. Biederman { 1042f728019bSEric W. Biederman int has_files = 0; 1043f728019bSEric W. Biederman int nr_subheaders = 0; 1044f728019bSEric W. Biederman struct ctl_table *entry; 1045f728019bSEric W. Biederman 1046f728019bSEric W. Biederman /* special case: no directory and empty directory */ 1047f728019bSEric W. Biederman if (!table || !table->procname) 1048f728019bSEric W. Biederman return 1; 1049f728019bSEric W. Biederman 1050f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1051f728019bSEric W. Biederman if (entry->child) 1052f728019bSEric W. Biederman nr_subheaders += count_subheaders(entry->child); 1053f728019bSEric W. Biederman else 1054f728019bSEric W. Biederman has_files = 1; 1055f728019bSEric W. Biederman } 1056f728019bSEric W. Biederman return nr_subheaders + has_files; 1057f728019bSEric W. Biederman } 1058f728019bSEric W. Biederman 1059f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos, 1060f728019bSEric W. Biederman struct ctl_table_header ***subheader, 1061f728019bSEric W. Biederman struct ctl_table_root *root, struct nsproxy *namespaces, 1062f728019bSEric W. Biederman struct ctl_table *table) 1063f728019bSEric W. Biederman { 1064f728019bSEric W. Biederman struct ctl_table *ctl_table_arg = NULL; 1065f728019bSEric W. Biederman struct ctl_table *entry, *files; 1066f728019bSEric W. Biederman int nr_files = 0; 1067f728019bSEric W. Biederman int nr_dirs = 0; 1068f728019bSEric W. Biederman int err = -ENOMEM; 1069f728019bSEric W. Biederman 1070f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1071f728019bSEric W. Biederman if (entry->child) 1072f728019bSEric W. Biederman nr_dirs++; 1073f728019bSEric W. Biederman else 1074f728019bSEric W. Biederman nr_files++; 1075f728019bSEric W. Biederman } 1076f728019bSEric W. Biederman 1077f728019bSEric W. Biederman files = table; 1078f728019bSEric W. Biederman /* If there are mixed files and directories we need a new table */ 1079f728019bSEric W. Biederman if (nr_dirs && nr_files) { 1080f728019bSEric W. Biederman struct ctl_table *new; 1081f728019bSEric W. Biederman files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1), 1082f728019bSEric W. Biederman GFP_KERNEL); 1083f728019bSEric W. Biederman if (!files) 1084f728019bSEric W. Biederman goto out; 1085f728019bSEric W. Biederman 1086f728019bSEric W. Biederman ctl_table_arg = files; 1087f728019bSEric W. Biederman for (new = files, entry = table; entry->procname; entry++) { 1088f728019bSEric W. Biederman if (entry->child) 1089f728019bSEric W. Biederman continue; 1090f728019bSEric W. Biederman *new = *entry; 1091f728019bSEric W. Biederman new++; 1092f728019bSEric W. Biederman } 1093f728019bSEric W. Biederman } 1094f728019bSEric W. Biederman 1095f728019bSEric W. Biederman /* Register everything except a directory full of subdirectories */ 1096f728019bSEric W. Biederman if (nr_files || !nr_dirs) { 1097f728019bSEric W. Biederman struct ctl_table_header *header; 1098f728019bSEric W. Biederman header = __register_sysctl_table(root, namespaces, path, files); 1099f728019bSEric W. Biederman if (!header) { 1100f728019bSEric W. Biederman kfree(ctl_table_arg); 1101f728019bSEric W. Biederman goto out; 1102f728019bSEric W. Biederman } 1103f728019bSEric W. Biederman 1104f728019bSEric W. Biederman /* Remember if we need to free the file table */ 1105f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1106f728019bSEric W. Biederman **subheader = header; 1107f728019bSEric W. Biederman (*subheader)++; 1108f728019bSEric W. Biederman } 1109f728019bSEric W. Biederman 1110f728019bSEric W. Biederman /* Recurse into the subdirectories. */ 1111f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1112f728019bSEric W. Biederman char *child_pos; 1113f728019bSEric W. Biederman 1114f728019bSEric W. Biederman if (!entry->child) 1115f728019bSEric W. Biederman continue; 1116f728019bSEric W. Biederman 1117f728019bSEric W. Biederman err = -ENAMETOOLONG; 1118f728019bSEric W. Biederman child_pos = append_path(path, pos, entry->procname); 1119f728019bSEric W. Biederman if (!child_pos) 1120f728019bSEric W. Biederman goto out; 1121f728019bSEric W. Biederman 1122f728019bSEric W. Biederman err = register_leaf_sysctl_tables(path, child_pos, subheader, 1123f728019bSEric W. Biederman root, namespaces, entry->child); 1124f728019bSEric W. Biederman pos[0] = '\0'; 1125f728019bSEric W. Biederman if (err) 1126f728019bSEric W. Biederman goto out; 1127f728019bSEric W. Biederman } 1128f728019bSEric W. Biederman err = 0; 1129f728019bSEric W. Biederman out: 1130f728019bSEric W. Biederman /* On failure our caller will unregister all registered subheaders */ 1131f728019bSEric W. Biederman return err; 1132f728019bSEric W. Biederman } 1133f728019bSEric W. Biederman 11346e9d5164SEric W. Biederman /** 11356e9d5164SEric W. Biederman * __register_sysctl_paths - register a sysctl table hierarchy 11366e9d5164SEric W. Biederman * @root: List of sysctl headers to register on 11376e9d5164SEric W. Biederman * @namespaces: Data to compute which lists of sysctl entries are visible 11386e9d5164SEric W. Biederman * @path: The path to the directory the sysctl table is in. 11396e9d5164SEric W. Biederman * @table: the top-level table structure 11406e9d5164SEric W. Biederman * 11416e9d5164SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 11426e9d5164SEric W. Biederman * array. A completely 0 filled entry terminates the table. 11436e9d5164SEric W. Biederman * 11446e9d5164SEric W. Biederman * See __register_sysctl_table for more details. 11456e9d5164SEric W. Biederman */ 11466e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths( 11476e9d5164SEric W. Biederman struct ctl_table_root *root, 11486e9d5164SEric W. Biederman struct nsproxy *namespaces, 11496e9d5164SEric W. Biederman const struct ctl_path *path, struct ctl_table *table) 11506e9d5164SEric W. Biederman { 1151ec6a5266SEric W. Biederman struct ctl_table *ctl_table_arg = table; 1152f728019bSEric W. Biederman int nr_subheaders = count_subheaders(table); 1153f728019bSEric W. Biederman struct ctl_table_header *header = NULL, **subheaders, **subheader; 11546e9d5164SEric W. Biederman const struct ctl_path *component; 11556e9d5164SEric W. Biederman char *new_path, *pos; 11566e9d5164SEric W. Biederman 11576e9d5164SEric W. Biederman pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL); 11586e9d5164SEric W. Biederman if (!new_path) 11596e9d5164SEric W. Biederman return NULL; 11606e9d5164SEric W. Biederman 11616e9d5164SEric W. Biederman pos[0] = '\0'; 11626e9d5164SEric W. Biederman for (component = path; component->procname; component++) { 11636e9d5164SEric W. Biederman pos = append_path(new_path, pos, component->procname); 11646e9d5164SEric W. Biederman if (!pos) 11656e9d5164SEric W. Biederman goto out; 11666e9d5164SEric W. Biederman } 1167ec6a5266SEric W. Biederman while (table->procname && table->child && !table[1].procname) { 1168ec6a5266SEric W. Biederman pos = append_path(new_path, pos, table->procname); 1169ec6a5266SEric W. Biederman if (!pos) 1170ec6a5266SEric W. Biederman goto out; 1171ec6a5266SEric W. Biederman table = table->child; 1172ec6a5266SEric W. Biederman } 1173f728019bSEric W. Biederman if (nr_subheaders == 1) { 11746e9d5164SEric W. Biederman header = __register_sysctl_table(root, namespaces, new_path, table); 1175ec6a5266SEric W. Biederman if (header) 1176ec6a5266SEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1177f728019bSEric W. Biederman } else { 1178f728019bSEric W. Biederman header = kzalloc(sizeof(*header) + 1179f728019bSEric W. Biederman sizeof(*subheaders)*nr_subheaders, GFP_KERNEL); 1180f728019bSEric W. Biederman if (!header) 1181f728019bSEric W. Biederman goto out; 1182f728019bSEric W. Biederman 1183f728019bSEric W. Biederman subheaders = (struct ctl_table_header **) (header + 1); 1184f728019bSEric W. Biederman subheader = subheaders; 1185f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1186f728019bSEric W. Biederman 1187f728019bSEric W. Biederman if (register_leaf_sysctl_tables(new_path, pos, &subheader, 1188f728019bSEric W. Biederman root, namespaces, table)) 1189f728019bSEric W. Biederman goto err_register_leaves; 1190f728019bSEric W. Biederman } 1191f728019bSEric W. Biederman 11926e9d5164SEric W. Biederman out: 11936e9d5164SEric W. Biederman kfree(new_path); 11946e9d5164SEric W. Biederman return header; 1195f728019bSEric W. Biederman 1196f728019bSEric W. Biederman err_register_leaves: 1197f728019bSEric W. Biederman while (subheader > subheaders) { 1198f728019bSEric W. Biederman struct ctl_table_header *subh = *(--subheader); 1199f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1200f728019bSEric W. Biederman unregister_sysctl_table(subh); 1201f728019bSEric W. Biederman kfree(table); 1202f728019bSEric W. Biederman } 1203f728019bSEric W. Biederman kfree(header); 1204f728019bSEric W. Biederman header = NULL; 1205f728019bSEric W. Biederman goto out; 12066e9d5164SEric W. Biederman } 12076e9d5164SEric W. Biederman 12081f87f0b5SEric W. Biederman /** 12091f87f0b5SEric W. Biederman * register_sysctl_table_path - register a sysctl table hierarchy 12101f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 12111f87f0b5SEric W. Biederman * @table: the top-level table structure 12121f87f0b5SEric W. Biederman * 12131f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 12141f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 12151f87f0b5SEric W. Biederman * 12161f87f0b5SEric W. Biederman * See __register_sysctl_paths for more details. 12171f87f0b5SEric W. Biederman */ 12181f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path, 12191f87f0b5SEric W. Biederman struct ctl_table *table) 12201f87f0b5SEric W. Biederman { 12211f87f0b5SEric W. Biederman return __register_sysctl_paths(&sysctl_table_root, current->nsproxy, 12221f87f0b5SEric W. Biederman path, table); 12231f87f0b5SEric W. Biederman } 12241f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths); 12251f87f0b5SEric W. Biederman 12261f87f0b5SEric W. Biederman /** 12271f87f0b5SEric W. Biederman * register_sysctl_table - register a sysctl table hierarchy 12281f87f0b5SEric W. Biederman * @table: the top-level table structure 12291f87f0b5SEric W. Biederman * 12301f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 12311f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 12321f87f0b5SEric W. Biederman * 12331f87f0b5SEric W. Biederman * See register_sysctl_paths for more details. 12341f87f0b5SEric W. Biederman */ 12351f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table) 12361f87f0b5SEric W. Biederman { 12371f87f0b5SEric W. Biederman static const struct ctl_path null_path[] = { {} }; 12381f87f0b5SEric W. Biederman 12391f87f0b5SEric W. Biederman return register_sysctl_paths(null_path, table); 12401f87f0b5SEric W. Biederman } 12411f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table); 12421f87f0b5SEric W. Biederman 1243938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header) 1244938aaa4fSEric W. Biederman { 12457ec66d06SEric W. Biederman struct ctl_dir *parent = header->parent; 12467ec66d06SEric W. Biederman 1247938aaa4fSEric W. Biederman if (--header->nreg) 1248938aaa4fSEric W. Biederman return; 1249938aaa4fSEric W. Biederman 1250938aaa4fSEric W. Biederman start_unregistering(header); 1251938aaa4fSEric W. Biederman if (!--header->count) 1252938aaa4fSEric W. Biederman kfree_rcu(header, rcu); 12537ec66d06SEric W. Biederman 12547ec66d06SEric W. Biederman if (parent) 12557ec66d06SEric W. Biederman drop_sysctl_table(&parent->header); 1256938aaa4fSEric W. Biederman } 1257938aaa4fSEric W. Biederman 12581f87f0b5SEric W. Biederman /** 12591f87f0b5SEric W. Biederman * unregister_sysctl_table - unregister a sysctl table hierarchy 12601f87f0b5SEric W. Biederman * @header: the header returned from register_sysctl_table 12611f87f0b5SEric W. Biederman * 12621f87f0b5SEric W. Biederman * Unregisters the sysctl table and all children. proc entries may not 12631f87f0b5SEric W. Biederman * actually be removed until they are no longer used by anyone. 12641f87f0b5SEric W. Biederman */ 12651f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header) 12661f87f0b5SEric W. Biederman { 1267f728019bSEric W. Biederman int nr_subheaders; 12681f87f0b5SEric W. Biederman might_sleep(); 12691f87f0b5SEric W. Biederman 12701f87f0b5SEric W. Biederman if (header == NULL) 12711f87f0b5SEric W. Biederman return; 12721f87f0b5SEric W. Biederman 1273f728019bSEric W. Biederman nr_subheaders = count_subheaders(header->ctl_table_arg); 1274f728019bSEric W. Biederman if (unlikely(nr_subheaders > 1)) { 1275f728019bSEric W. Biederman struct ctl_table_header **subheaders; 1276f728019bSEric W. Biederman int i; 1277f728019bSEric W. Biederman 1278f728019bSEric W. Biederman subheaders = (struct ctl_table_header **)(header + 1); 1279f728019bSEric W. Biederman for (i = nr_subheaders -1; i >= 0; i--) { 1280f728019bSEric W. Biederman struct ctl_table_header *subh = subheaders[i]; 1281f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1282f728019bSEric W. Biederman unregister_sysctl_table(subh); 1283f728019bSEric W. Biederman kfree(table); 1284f728019bSEric W. Biederman } 1285f728019bSEric W. Biederman kfree(header); 1286f728019bSEric W. Biederman return; 1287f728019bSEric W. Biederman } 1288f728019bSEric W. Biederman 12891f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1290938aaa4fSEric W. Biederman drop_sysctl_table(header); 12911f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 12921f87f0b5SEric W. Biederman } 12931f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table); 12941f87f0b5SEric W. Biederman 12951f87f0b5SEric W. Biederman void setup_sysctl_set(struct ctl_table_set *p, 12969eb47c26SEric W. Biederman struct ctl_table_root *root, 12971f87f0b5SEric W. Biederman int (*is_seen)(struct ctl_table_set *)) 12981f87f0b5SEric W. Biederman { 12991f87f0b5SEric W. Biederman INIT_LIST_HEAD(&p->list); 13009eb47c26SEric W. Biederman p->root = root; 13011f87f0b5SEric W. Biederman p->is_seen = is_seen; 13021f87f0b5SEric W. Biederman } 13031f87f0b5SEric W. Biederman 130497324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set) 130597324cd8SEric W. Biederman { 130697324cd8SEric W. Biederman WARN_ON(!list_empty(&set->list)); 130797324cd8SEric W. Biederman } 13081f87f0b5SEric W. Biederman 13091e0edd3fSAlexey Dobriyan int __init proc_sys_init(void) 131077b14db5SEric W. Biederman { 1311e1675231SAlexey Dobriyan struct proc_dir_entry *proc_sys_root; 1312e1675231SAlexey Dobriyan 131377b14db5SEric W. Biederman proc_sys_root = proc_mkdir("sys", NULL); 13149043476fSAl Viro proc_sys_root->proc_iops = &proc_sys_dir_operations; 13159043476fSAl Viro proc_sys_root->proc_fops = &proc_sys_dir_file_operations; 131677b14db5SEric W. Biederman proc_sys_root->nlink = 0; 1317de4e83bdSEric W. Biederman 1318de4e83bdSEric W. Biederman return sysctl_init(); 131977b14db5SEric W. Biederman } 1320