Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * /proc/sys support |
| 3 | */ |
Alexey Dobriyan | 1e0edd3 | 2008-10-17 05:07:44 +0400 | [diff] [blame] | 4 | #include <linux/init.h> |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 5 | #include <linux/sysctl.h> |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 6 | #include <linux/poll.h> |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 7 | #include <linux/proc_fs.h> |
| 8 | #include <linux/security.h> |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 9 | #include <linux/namei.h> |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 10 | #include <linux/module.h> |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 11 | #include "internal.h" |
| 12 | |
Al Viro | d72f71e | 2009-02-20 05:58:47 +0000 | [diff] [blame] | 13 | static const struct dentry_operations proc_sys_dentry_operations; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 14 | static const struct file_operations proc_sys_file_operations; |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 15 | static const struct inode_operations proc_sys_inode_operations; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 16 | static const struct file_operations proc_sys_dir_file_operations; |
| 17 | static const struct inode_operations proc_sys_dir_operations; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 18 | |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 19 | void proc_sys_poll_notify(struct ctl_table_poll *poll) |
| 20 | { |
| 21 | if (!poll) |
| 22 | return; |
| 23 | |
| 24 | atomic_inc(&poll->event); |
| 25 | wake_up_interruptible(&poll->wait); |
| 26 | } |
| 27 | |
Eric W. Biederman | a194558 | 2012-01-21 17:51:48 -0800 | [diff] [blame] | 28 | static struct ctl_table root_table[] = { |
| 29 | { |
| 30 | .procname = "", |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 31 | .mode = S_IFDIR|S_IRUGO|S_IXUGO, |
Eric W. Biederman | a194558 | 2012-01-21 17:51:48 -0800 | [diff] [blame] | 32 | }, |
| 33 | { } |
| 34 | }; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 35 | static struct ctl_table_root sysctl_table_root; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 36 | static struct ctl_dir sysctl_root_dir = { |
| 37 | .header = { |
| 38 | {{.count = 1, |
| 39 | .nreg = 1, |
| 40 | .ctl_table = root_table, |
| 41 | .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}}, |
| 42 | .root = &sysctl_table_root, |
| 43 | .set = &sysctl_table_root.default_set, |
| 44 | }, |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 45 | }; |
| 46 | static struct ctl_table_root sysctl_table_root = { |
| 47 | .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list), |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 48 | .default_set.list = LIST_HEAD_INIT(sysctl_root_dir.header.ctl_entry), |
Eric W. Biederman | 9eb47c2 | 2012-01-22 21:26:00 -0800 | [diff] [blame] | 49 | .default_set.root = &sysctl_table_root, |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static DEFINE_SPINLOCK(sysctl_lock); |
| 53 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 54 | static void drop_sysctl_table(struct ctl_table_header *header); |
| 55 | |
Eric W. Biederman | 6980128 | 2012-01-21 20:09:45 -0800 | [diff] [blame^] | 56 | static void sysctl_print_dir(struct ctl_dir *dir) |
| 57 | { |
| 58 | if (dir->header.parent) |
| 59 | sysctl_print_dir(dir->header.parent); |
| 60 | printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname); |
| 61 | } |
| 62 | |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 63 | static int namecmp(const char *name1, int len1, const char *name2, int len2) |
| 64 | { |
| 65 | int minlen; |
| 66 | int cmp; |
| 67 | |
| 68 | minlen = len1; |
| 69 | if (minlen > len2) |
| 70 | minlen = len2; |
| 71 | |
| 72 | cmp = memcmp(name1, name2, minlen); |
| 73 | if (cmp == 0) |
| 74 | cmp = len1 - len2; |
| 75 | return cmp; |
| 76 | } |
| 77 | |
| 78 | static struct ctl_table *find_entry(struct ctl_table_header **phead, |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 79 | struct ctl_table_set *set, struct ctl_dir *dir, |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 80 | const char *name, int namelen) |
| 81 | { |
| 82 | struct ctl_table_header *head; |
| 83 | struct ctl_table *entry; |
| 84 | |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 85 | list_for_each_entry(head, &set->list, ctl_entry) { |
| 86 | if (head->unregistering) |
| 87 | continue; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 88 | if (head->parent != dir) |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 89 | continue; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 90 | for (entry = head->ctl_table; entry->procname; entry++) { |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 91 | const char *procname = entry->procname; |
| 92 | if (namecmp(procname, strlen(procname), name, namelen) == 0) { |
| 93 | *phead = head; |
| 94 | return entry; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | return NULL; |
| 99 | } |
| 100 | |
Eric W. Biederman | e0d0452 | 2012-01-09 22:36:41 -0800 | [diff] [blame] | 101 | static void init_header(struct ctl_table_header *head, |
| 102 | struct ctl_table_root *root, struct ctl_table_set *set, |
| 103 | struct ctl_table *table) |
| 104 | { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 105 | head->ctl_table = table; |
Eric W. Biederman | e0d0452 | 2012-01-09 22:36:41 -0800 | [diff] [blame] | 106 | head->ctl_table_arg = table; |
| 107 | INIT_LIST_HEAD(&head->ctl_entry); |
| 108 | head->used = 0; |
| 109 | head->count = 1; |
| 110 | head->nreg = 1; |
| 111 | head->unregistering = NULL; |
| 112 | head->root = root; |
| 113 | head->set = set; |
| 114 | head->parent = NULL; |
| 115 | } |
| 116 | |
Eric W. Biederman | 8425d6a | 2012-01-09 17:35:01 -0800 | [diff] [blame] | 117 | static void erase_header(struct ctl_table_header *head) |
| 118 | { |
| 119 | list_del_init(&head->ctl_entry); |
| 120 | } |
| 121 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 122 | static void insert_header(struct ctl_dir *dir, struct ctl_table_header *header) |
Eric W. Biederman | 8425d6a | 2012-01-09 17:35:01 -0800 | [diff] [blame] | 123 | { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 124 | header->parent = dir; |
| 125 | header->parent->header.nreg++; |
Eric W. Biederman | 8425d6a | 2012-01-09 17:35:01 -0800 | [diff] [blame] | 126 | list_add_tail(&header->ctl_entry, &header->set->list); |
| 127 | } |
| 128 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 129 | /* called under sysctl_lock */ |
| 130 | static int use_table(struct ctl_table_header *p) |
| 131 | { |
| 132 | if (unlikely(p->unregistering)) |
| 133 | return 0; |
| 134 | p->used++; |
| 135 | return 1; |
| 136 | } |
| 137 | |
| 138 | /* called under sysctl_lock */ |
| 139 | static void unuse_table(struct ctl_table_header *p) |
| 140 | { |
| 141 | if (!--p->used) |
| 142 | if (unlikely(p->unregistering)) |
| 143 | complete(p->unregistering); |
| 144 | } |
| 145 | |
| 146 | /* called under sysctl_lock, will reacquire if has to wait */ |
| 147 | static void start_unregistering(struct ctl_table_header *p) |
| 148 | { |
| 149 | /* |
| 150 | * if p->used is 0, nobody will ever touch that entry again; |
| 151 | * we'll eliminate all paths to it before dropping sysctl_lock |
| 152 | */ |
| 153 | if (unlikely(p->used)) { |
| 154 | struct completion wait; |
| 155 | init_completion(&wait); |
| 156 | p->unregistering = &wait; |
| 157 | spin_unlock(&sysctl_lock); |
| 158 | wait_for_completion(&wait); |
| 159 | spin_lock(&sysctl_lock); |
| 160 | } else { |
| 161 | /* anything non-NULL; we'll never dereference it */ |
| 162 | p->unregistering = ERR_PTR(-EINVAL); |
| 163 | } |
| 164 | /* |
| 165 | * do not remove from the list until nobody holds it; walking the |
| 166 | * list in do_sysctl() relies on that. |
| 167 | */ |
Eric W. Biederman | 8425d6a | 2012-01-09 17:35:01 -0800 | [diff] [blame] | 168 | erase_header(p); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static void sysctl_head_get(struct ctl_table_header *head) |
| 172 | { |
| 173 | spin_lock(&sysctl_lock); |
| 174 | head->count++; |
| 175 | spin_unlock(&sysctl_lock); |
| 176 | } |
| 177 | |
| 178 | void sysctl_head_put(struct ctl_table_header *head) |
| 179 | { |
| 180 | spin_lock(&sysctl_lock); |
| 181 | if (!--head->count) |
| 182 | kfree_rcu(head, rcu); |
| 183 | spin_unlock(&sysctl_lock); |
| 184 | } |
| 185 | |
| 186 | static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head) |
| 187 | { |
| 188 | if (!head) |
| 189 | BUG(); |
| 190 | spin_lock(&sysctl_lock); |
| 191 | if (!use_table(head)) |
| 192 | head = ERR_PTR(-ENOENT); |
| 193 | spin_unlock(&sysctl_lock); |
| 194 | return head; |
| 195 | } |
| 196 | |
| 197 | static void sysctl_head_finish(struct ctl_table_header *head) |
| 198 | { |
| 199 | if (!head) |
| 200 | return; |
| 201 | spin_lock(&sysctl_lock); |
| 202 | unuse_table(head); |
| 203 | spin_unlock(&sysctl_lock); |
| 204 | } |
| 205 | |
| 206 | static struct ctl_table_set * |
| 207 | lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces) |
| 208 | { |
| 209 | struct ctl_table_set *set = &root->default_set; |
| 210 | if (root->lookup) |
| 211 | set = root->lookup(root, namespaces); |
| 212 | return set; |
| 213 | } |
| 214 | |
| 215 | static struct list_head * |
| 216 | lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces) |
| 217 | { |
| 218 | struct ctl_table_set *set = lookup_header_set(root, namespaces); |
| 219 | return &set->list; |
| 220 | } |
| 221 | |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 222 | static struct ctl_table *lookup_entry(struct ctl_table_header **phead, |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 223 | struct ctl_dir *dir, |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 224 | const char *name, int namelen) |
| 225 | { |
| 226 | struct ctl_table_header *head; |
| 227 | struct ctl_table *entry; |
| 228 | struct ctl_table_root *root; |
| 229 | struct ctl_table_set *set; |
| 230 | |
| 231 | spin_lock(&sysctl_lock); |
| 232 | root = &sysctl_table_root; |
| 233 | do { |
| 234 | set = lookup_header_set(root, current->nsproxy); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 235 | entry = find_entry(&head, set, dir, name, namelen); |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 236 | if (entry && use_table(head)) |
| 237 | *phead = head; |
| 238 | else |
| 239 | entry = NULL; |
| 240 | root = list_entry(root->root_list.next, |
| 241 | struct ctl_table_root, root_list); |
| 242 | } while (!entry && root != &sysctl_table_root); |
| 243 | spin_unlock(&sysctl_lock); |
| 244 | return entry; |
| 245 | } |
| 246 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 247 | static struct ctl_table_header *next_usable_entry(struct ctl_dir *dir, |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 248 | struct ctl_table_root *root, struct list_head *tmp) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 249 | { |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 250 | struct nsproxy *namespaces = current->nsproxy; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 251 | struct list_head *header_list; |
| 252 | struct ctl_table_header *head; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 253 | |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 254 | goto next; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 255 | for (;;) { |
| 256 | head = list_entry(tmp, struct ctl_table_header, ctl_entry); |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 257 | root = head->root; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 258 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 259 | if (head->parent != dir || |
| 260 | !head->ctl_table->procname || |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 261 | !use_table(head)) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 262 | goto next; |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 263 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 264 | return head; |
| 265 | next: |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 266 | tmp = tmp->next; |
| 267 | header_list = lookup_header_list(root, namespaces); |
| 268 | if (tmp != header_list) |
| 269 | continue; |
| 270 | |
| 271 | do { |
| 272 | root = list_entry(root->root_list.next, |
| 273 | struct ctl_table_root, root_list); |
| 274 | if (root == &sysctl_table_root) |
| 275 | goto out; |
| 276 | header_list = lookup_header_list(root, namespaces); |
| 277 | } while (list_empty(header_list)); |
| 278 | tmp = header_list->next; |
| 279 | } |
| 280 | out: |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 281 | return NULL; |
| 282 | } |
| 283 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 284 | static void first_entry(struct ctl_dir *dir, |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 285 | struct ctl_table_header **phead, struct ctl_table **pentry) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 286 | { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 287 | struct ctl_table_header *head; |
| 288 | struct ctl_table *entry = NULL; |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 289 | |
| 290 | spin_lock(&sysctl_lock); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 291 | head = next_usable_entry(dir, &sysctl_table_root, |
| 292 | &sysctl_table_root.default_set.list); |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 293 | spin_unlock(&sysctl_lock); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 294 | if (head) |
| 295 | entry = head->ctl_table; |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 296 | *phead = head; |
| 297 | *pentry = entry; |
| 298 | } |
| 299 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 300 | static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry) |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 301 | { |
| 302 | struct ctl_table_header *head = *phead; |
| 303 | struct ctl_table *entry = *pentry; |
| 304 | |
| 305 | entry++; |
| 306 | if (!entry->procname) { |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 307 | spin_lock(&sysctl_lock); |
| 308 | unuse_table(head); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 309 | head = next_usable_entry(head->parent, head->root, &head->ctl_entry); |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 310 | spin_unlock(&sysctl_lock); |
| 311 | if (head) |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 312 | entry = head->ctl_table; |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 313 | } |
| 314 | *phead = head; |
| 315 | *pentry = entry; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void register_sysctl_root(struct ctl_table_root *root) |
| 319 | { |
| 320 | spin_lock(&sysctl_lock); |
| 321 | list_add_tail(&root->root_list, &sysctl_table_root.root_list); |
| 322 | spin_unlock(&sysctl_lock); |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * sysctl_perm does NOT grant the superuser all rights automatically, because |
| 327 | * some sysctl variables are readonly even to root. |
| 328 | */ |
| 329 | |
| 330 | static int test_perm(int mode, int op) |
| 331 | { |
| 332 | if (!current_euid()) |
| 333 | mode >>= 6; |
| 334 | else if (in_egroup_p(0)) |
| 335 | mode >>= 3; |
| 336 | if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0) |
| 337 | return 0; |
| 338 | return -EACCES; |
| 339 | } |
| 340 | |
| 341 | static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op) |
| 342 | { |
| 343 | int mode; |
| 344 | |
| 345 | if (root->permissions) |
| 346 | mode = root->permissions(root, current->nsproxy, table); |
| 347 | else |
| 348 | mode = table->mode; |
| 349 | |
| 350 | return test_perm(mode, op); |
| 351 | } |
| 352 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 353 | static struct inode *proc_sys_make_inode(struct super_block *sb, |
| 354 | struct ctl_table_header *head, struct ctl_table *table) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 355 | { |
| 356 | struct inode *inode; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 357 | struct proc_inode *ei; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 358 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 359 | inode = new_inode(sb); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 360 | if (!inode) |
| 361 | goto out; |
| 362 | |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 363 | inode->i_ino = get_next_ino(); |
| 364 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 365 | sysctl_head_get(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 366 | ei = PROC_I(inode); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 367 | ei->sysctl = head; |
| 368 | ei->sysctl_entry = table; |
| 369 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 370 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 371 | inode->i_mode = table->mode; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 372 | if (!S_ISDIR(table->mode)) { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 373 | inode->i_mode |= S_IFREG; |
| 374 | inode->i_op = &proc_sys_inode_operations; |
| 375 | inode->i_fop = &proc_sys_file_operations; |
| 376 | } else { |
| 377 | inode->i_mode |= S_IFDIR; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 378 | inode->i_op = &proc_sys_dir_operations; |
| 379 | inode->i_fop = &proc_sys_dir_file_operations; |
| 380 | } |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 381 | out: |
| 382 | return inode; |
| 383 | } |
| 384 | |
Adrian Bunk | 8132436 | 2008-10-03 00:33:54 +0400 | [diff] [blame] | 385 | static struct ctl_table_header *grab_header(struct inode *inode) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 386 | { |
Eric W. Biederman | 3cc3e04 | 2012-01-07 06:57:47 -0800 | [diff] [blame] | 387 | struct ctl_table_header *head = PROC_I(inode)->sysctl; |
| 388 | if (!head) |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 389 | head = &sysctl_root_dir.header; |
Eric W. Biederman | 3cc3e04 | 2012-01-07 06:57:47 -0800 | [diff] [blame] | 390 | return sysctl_head_grab(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, |
| 394 | struct nameidata *nd) |
| 395 | { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 396 | struct ctl_table_header *head = grab_header(dir); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 397 | struct ctl_table_header *h = NULL; |
| 398 | struct qstr *name = &dentry->d_name; |
| 399 | struct ctl_table *p; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 400 | struct inode *inode; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 401 | struct dentry *err = ERR_PTR(-ENOENT); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 402 | struct ctl_dir *ctl_dir; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 403 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 404 | if (IS_ERR(head)) |
| 405 | return ERR_CAST(head); |
| 406 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 407 | ctl_dir = container_of(head, struct ctl_dir, header); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 408 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 409 | p = lookup_entry(&h, ctl_dir, name->name, name->len); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 410 | if (!p) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 411 | goto out; |
| 412 | |
| 413 | err = ERR_PTR(-ENOMEM); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 414 | inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p); |
| 415 | if (h) |
| 416 | sysctl_head_finish(h); |
| 417 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 418 | if (!inode) |
| 419 | goto out; |
| 420 | |
| 421 | err = NULL; |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 422 | d_set_d_op(dentry, &proc_sys_dentry_operations); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 423 | d_add(dentry, inode); |
| 424 | |
| 425 | out: |
| 426 | sysctl_head_finish(head); |
| 427 | return err; |
| 428 | } |
| 429 | |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 430 | static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf, |
| 431 | size_t count, loff_t *ppos, int write) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 432 | { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 433 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 434 | struct ctl_table_header *head = grab_header(inode); |
| 435 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
David Howells | 2a2da53 | 2007-10-25 15:27:40 +0100 | [diff] [blame] | 436 | ssize_t error; |
| 437 | size_t res; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 438 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 439 | if (IS_ERR(head)) |
| 440 | return PTR_ERR(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 441 | |
| 442 | /* |
| 443 | * At this point we know that the sysctl was not unregistered |
| 444 | * and won't be until we finish. |
| 445 | */ |
| 446 | error = -EPERM; |
Pavel Emelyanov | d7321cd | 2008-04-29 01:02:44 -0700 | [diff] [blame] | 447 | if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ)) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 448 | goto out; |
| 449 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 450 | /* if that can happen at all, it should be -EINVAL, not -EISDIR */ |
| 451 | error = -EINVAL; |
| 452 | if (!table->proc_handler) |
| 453 | goto out; |
| 454 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 455 | /* careful: calling conventions are nasty here */ |
| 456 | res = count; |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 457 | error = table->proc_handler(table, write, buf, &res, ppos); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 458 | if (!error) |
| 459 | error = res; |
| 460 | out: |
| 461 | sysctl_head_finish(head); |
| 462 | |
| 463 | return error; |
| 464 | } |
| 465 | |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 466 | static ssize_t proc_sys_read(struct file *filp, char __user *buf, |
| 467 | size_t count, loff_t *ppos) |
| 468 | { |
| 469 | return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0); |
| 470 | } |
| 471 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 472 | static ssize_t proc_sys_write(struct file *filp, const char __user *buf, |
| 473 | size_t count, loff_t *ppos) |
| 474 | { |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 475 | return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 478 | static int proc_sys_open(struct inode *inode, struct file *filp) |
| 479 | { |
| 480 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
| 481 | |
| 482 | if (table->poll) |
| 483 | filp->private_data = proc_sys_poll_event(table->poll); |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static unsigned int proc_sys_poll(struct file *filp, poll_table *wait) |
| 489 | { |
| 490 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 491 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
| 492 | unsigned long event = (unsigned long)filp->private_data; |
| 493 | unsigned int ret = DEFAULT_POLLMASK; |
| 494 | |
| 495 | if (!table->proc_handler) |
| 496 | goto out; |
| 497 | |
| 498 | if (!table->poll) |
| 499 | goto out; |
| 500 | |
| 501 | poll_wait(filp, &table->poll->wait, wait); |
| 502 | |
| 503 | if (event != atomic_read(&table->poll->event)) { |
| 504 | filp->private_data = proc_sys_poll_event(table->poll); |
| 505 | ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI; |
| 506 | } |
| 507 | |
| 508 | out: |
| 509 | return ret; |
| 510 | } |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 511 | |
| 512 | static int proc_sys_fill_cache(struct file *filp, void *dirent, |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 513 | filldir_t filldir, |
| 514 | struct ctl_table_header *head, |
| 515 | struct ctl_table *table) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 516 | { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 517 | struct dentry *child, *dir = filp->f_path.dentry; |
| 518 | struct inode *inode; |
| 519 | struct qstr qname; |
| 520 | ino_t ino = 0; |
| 521 | unsigned type = DT_UNKNOWN; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 522 | |
| 523 | qname.name = table->procname; |
| 524 | qname.len = strlen(table->procname); |
| 525 | qname.hash = full_name_hash(qname.name, qname.len); |
| 526 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 527 | child = d_lookup(dir, &qname); |
| 528 | if (!child) { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 529 | child = d_alloc(dir, &qname); |
| 530 | if (child) { |
| 531 | inode = proc_sys_make_inode(dir->d_sb, head, table); |
| 532 | if (!inode) { |
| 533 | dput(child); |
| 534 | return -ENOMEM; |
| 535 | } else { |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 536 | d_set_d_op(child, &proc_sys_dentry_operations); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 537 | d_add(child, inode); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 538 | } |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 539 | } else { |
| 540 | return -ENOMEM; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 541 | } |
| 542 | } |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 543 | inode = child->d_inode; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 544 | ino = inode->i_ino; |
| 545 | type = inode->i_mode >> 12; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 546 | dput(child); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 547 | return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type); |
| 548 | } |
| 549 | |
| 550 | static int scan(struct ctl_table_header *head, ctl_table *table, |
| 551 | unsigned long *pos, struct file *file, |
| 552 | void *dirent, filldir_t filldir) |
| 553 | { |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 554 | int res; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 555 | |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 556 | if ((*pos)++ < file->f_pos) |
| 557 | return 0; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 558 | |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 559 | res = proc_sys_fill_cache(file, dirent, filldir, head, table); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 560 | |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 561 | if (res == 0) |
| 562 | file->f_pos = *pos; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 563 | |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 564 | return res; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 568 | { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 569 | struct dentry *dentry = filp->f_path.dentry; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 570 | struct inode *inode = dentry->d_inode; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 571 | struct ctl_table_header *head = grab_header(inode); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 572 | struct ctl_table_header *h = NULL; |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 573 | struct ctl_table *entry; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 574 | struct ctl_dir *ctl_dir; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 575 | unsigned long pos; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 576 | int ret = -EINVAL; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 577 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 578 | if (IS_ERR(head)) |
| 579 | return PTR_ERR(head); |
| 580 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 581 | ctl_dir = container_of(head, struct ctl_dir, header); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 582 | |
| 583 | ret = 0; |
| 584 | /* Avoid a switch here: arm builds fail with missing __cmpdi2 */ |
| 585 | if (filp->f_pos == 0) { |
| 586 | if (filldir(dirent, ".", 1, filp->f_pos, |
| 587 | inode->i_ino, DT_DIR) < 0) |
| 588 | goto out; |
| 589 | filp->f_pos++; |
| 590 | } |
| 591 | if (filp->f_pos == 1) { |
| 592 | if (filldir(dirent, "..", 2, filp->f_pos, |
| 593 | parent_ino(dentry), DT_DIR) < 0) |
| 594 | goto out; |
| 595 | filp->f_pos++; |
| 596 | } |
| 597 | pos = 2; |
| 598 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 599 | for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) { |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 600 | ret = scan(h, entry, &pos, filp, dirent, filldir); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 601 | if (ret) { |
| 602 | sysctl_head_finish(h); |
| 603 | break; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | ret = 1; |
| 607 | out: |
| 608 | sysctl_head_finish(head); |
| 609 | return ret; |
| 610 | } |
| 611 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 612 | static int proc_sys_permission(struct inode *inode, int mask) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 613 | { |
| 614 | /* |
| 615 | * sysctl entries that are not writeable, |
| 616 | * are _NOT_ writeable, capabilities or not. |
| 617 | */ |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 618 | struct ctl_table_header *head; |
| 619 | struct ctl_table *table; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 620 | int error; |
| 621 | |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 622 | /* Executable files are not allowed under /proc/sys/ */ |
| 623 | if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) |
| 624 | return -EACCES; |
| 625 | |
| 626 | head = grab_header(inode); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 627 | if (IS_ERR(head)) |
| 628 | return PTR_ERR(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 629 | |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 630 | table = PROC_I(inode)->sysctl_entry; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 631 | if (!table) /* global root - r-xr-xr-x */ |
| 632 | error = mask & MAY_WRITE ? -EACCES : 0; |
| 633 | else /* Use the permissions on the sysctl table entry */ |
Al Viro | 1fc0f78 | 2011-06-20 18:59:02 -0400 | [diff] [blame] | 634 | error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 635 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 636 | sysctl_head_finish(head); |
| 637 | return error; |
| 638 | } |
| 639 | |
| 640 | static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr) |
| 641 | { |
| 642 | struct inode *inode = dentry->d_inode; |
| 643 | int error; |
| 644 | |
| 645 | if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) |
| 646 | return -EPERM; |
| 647 | |
| 648 | error = inode_change_ok(inode, attr); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 649 | if (error) |
| 650 | return error; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 651 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 652 | if ((attr->ia_valid & ATTR_SIZE) && |
| 653 | attr->ia_size != i_size_read(inode)) { |
| 654 | error = vmtruncate(inode, attr->ia_size); |
| 655 | if (error) |
| 656 | return error; |
| 657 | } |
| 658 | |
| 659 | setattr_copy(inode, attr); |
| 660 | mark_inode_dirty(inode); |
| 661 | return 0; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 662 | } |
| 663 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 664 | static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
| 665 | { |
| 666 | struct inode *inode = dentry->d_inode; |
| 667 | struct ctl_table_header *head = grab_header(inode); |
| 668 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
| 669 | |
| 670 | if (IS_ERR(head)) |
| 671 | return PTR_ERR(head); |
| 672 | |
| 673 | generic_fillattr(inode, stat); |
| 674 | if (table) |
| 675 | stat->mode = (stat->mode & S_IFMT) | table->mode; |
| 676 | |
| 677 | sysctl_head_finish(head); |
| 678 | return 0; |
| 679 | } |
| 680 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 681 | static const struct file_operations proc_sys_file_operations = { |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 682 | .open = proc_sys_open, |
| 683 | .poll = proc_sys_poll, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 684 | .read = proc_sys_read, |
| 685 | .write = proc_sys_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 686 | .llseek = default_llseek, |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 687 | }; |
| 688 | |
| 689 | static const struct file_operations proc_sys_dir_file_operations = { |
Pavel Emelyanov | 887df07 | 2011-11-02 13:38:42 -0700 | [diff] [blame] | 690 | .read = generic_read_dir, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 691 | .readdir = proc_sys_readdir, |
Christoph Hellwig | 3222a3e | 2008-09-03 21:53:01 +0200 | [diff] [blame] | 692 | .llseek = generic_file_llseek, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 693 | }; |
| 694 | |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 695 | static const struct inode_operations proc_sys_inode_operations = { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 696 | .permission = proc_sys_permission, |
| 697 | .setattr = proc_sys_setattr, |
| 698 | .getattr = proc_sys_getattr, |
| 699 | }; |
| 700 | |
| 701 | static const struct inode_operations proc_sys_dir_operations = { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 702 | .lookup = proc_sys_lookup, |
| 703 | .permission = proc_sys_permission, |
| 704 | .setattr = proc_sys_setattr, |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 705 | .getattr = proc_sys_getattr, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 706 | }; |
| 707 | |
| 708 | static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd) |
| 709 | { |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 710 | if (nd->flags & LOOKUP_RCU) |
| 711 | return -ECHILD; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 712 | return !PROC_I(dentry->d_inode)->sysctl->unregistering; |
| 713 | } |
| 714 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 715 | static int proc_sys_delete(const struct dentry *dentry) |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 716 | { |
| 717 | return !!PROC_I(dentry->d_inode)->sysctl->unregistering; |
| 718 | } |
| 719 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 720 | static int sysctl_is_seen(struct ctl_table_header *p) |
| 721 | { |
| 722 | struct ctl_table_set *set = p->set; |
| 723 | int res; |
| 724 | spin_lock(&sysctl_lock); |
| 725 | if (p->unregistering) |
| 726 | res = 0; |
| 727 | else if (!set->is_seen) |
| 728 | res = 1; |
| 729 | else |
| 730 | res = set->is_seen(set); |
| 731 | spin_unlock(&sysctl_lock); |
| 732 | return res; |
| 733 | } |
| 734 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 735 | static int proc_sys_compare(const struct dentry *parent, |
| 736 | const struct inode *pinode, |
| 737 | const struct dentry *dentry, const struct inode *inode, |
| 738 | unsigned int len, const char *str, const struct qstr *name) |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 739 | { |
Al Viro | dfef6dc | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 740 | struct ctl_table_header *head; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 741 | /* Although proc doesn't have negative dentries, rcu-walk means |
| 742 | * that inode here can be NULL */ |
Al Viro | dfef6dc | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 743 | /* AV: can it, indeed? */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 744 | if (!inode) |
Al Viro | dfef6dc | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 745 | return 1; |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 746 | if (name->len != len) |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 747 | return 1; |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 748 | if (memcmp(name->name, str, len)) |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 749 | return 1; |
Al Viro | dfef6dc | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 750 | head = rcu_dereference(PROC_I(inode)->sysctl); |
| 751 | return !head || !sysctl_is_seen(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 752 | } |
| 753 | |
Al Viro | d72f71e | 2009-02-20 05:58:47 +0000 | [diff] [blame] | 754 | static const struct dentry_operations proc_sys_dentry_operations = { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 755 | .d_revalidate = proc_sys_revalidate, |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 756 | .d_delete = proc_sys_delete, |
| 757 | .d_compare = proc_sys_compare, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 758 | }; |
| 759 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 760 | static struct ctl_dir *find_subdir(struct ctl_table_set *set, struct ctl_dir *dir, |
| 761 | const char *name, int namelen) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 762 | { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 763 | struct ctl_table_header *head; |
| 764 | struct ctl_table *entry; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 765 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 766 | entry = find_entry(&head, set, dir, name, namelen); |
| 767 | if (!entry) |
| 768 | return ERR_PTR(-ENOENT); |
| 769 | if (S_ISDIR(entry->mode)) |
| 770 | return container_of(head, struct ctl_dir, header); |
| 771 | return ERR_PTR(-ENOTDIR); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 772 | } |
| 773 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 774 | static struct ctl_dir *new_dir(struct ctl_table_set *set, |
| 775 | const char *name, int namelen) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 776 | { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 777 | struct ctl_table *table; |
| 778 | struct ctl_dir *new; |
| 779 | char *new_name; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 780 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 781 | new = kzalloc(sizeof(*new) + sizeof(struct ctl_table)*2 + |
| 782 | namelen + 1, GFP_KERNEL); |
| 783 | if (!new) |
| 784 | return NULL; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 785 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 786 | table = (struct ctl_table *)(new + 1); |
| 787 | new_name = (char *)(table + 2); |
| 788 | memcpy(new_name, name, namelen); |
| 789 | new_name[namelen] = '\0'; |
| 790 | table[0].procname = new_name; |
| 791 | table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO; |
| 792 | init_header(&new->header, set->root, set, table); |
| 793 | |
| 794 | return new; |
| 795 | } |
| 796 | |
| 797 | static struct ctl_dir *get_subdir(struct ctl_table_set *set, |
| 798 | struct ctl_dir *dir, const char *name, int namelen) |
| 799 | { |
| 800 | struct ctl_dir *subdir, *new = NULL; |
| 801 | |
| 802 | spin_lock(&sysctl_lock); |
| 803 | subdir = find_subdir(dir->header.set, dir, name, namelen); |
| 804 | if (!IS_ERR(subdir)) |
| 805 | goto found; |
| 806 | if ((PTR_ERR(subdir) == -ENOENT) && set != dir->header.set) |
| 807 | subdir = find_subdir(set, dir, name, namelen); |
| 808 | if (!IS_ERR(subdir)) |
| 809 | goto found; |
| 810 | if (PTR_ERR(subdir) != -ENOENT) |
| 811 | goto failed; |
| 812 | |
| 813 | spin_unlock(&sysctl_lock); |
| 814 | new = new_dir(set, name, namelen); |
| 815 | spin_lock(&sysctl_lock); |
| 816 | subdir = ERR_PTR(-ENOMEM); |
| 817 | if (!new) |
| 818 | goto failed; |
| 819 | |
| 820 | subdir = find_subdir(set, dir, name, namelen); |
| 821 | if (!IS_ERR(subdir)) |
| 822 | goto found; |
| 823 | if (PTR_ERR(subdir) != -ENOENT) |
| 824 | goto failed; |
| 825 | |
| 826 | insert_header(dir, &new->header); |
| 827 | subdir = new; |
| 828 | found: |
| 829 | subdir->header.nreg++; |
| 830 | failed: |
| 831 | if (unlikely(IS_ERR(subdir))) { |
Eric W. Biederman | 6980128 | 2012-01-21 20:09:45 -0800 | [diff] [blame^] | 832 | printk(KERN_ERR "sysctl could not get directory: "); |
| 833 | sysctl_print_dir(dir); |
| 834 | printk(KERN_CONT "/%*.*s %ld\n", |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 835 | namelen, namelen, name, PTR_ERR(subdir)); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 836 | } |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 837 | drop_sysctl_table(&dir->header); |
| 838 | if (new) |
| 839 | drop_sysctl_table(&new->header); |
| 840 | spin_unlock(&sysctl_lock); |
| 841 | return subdir; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 842 | } |
| 843 | |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 844 | static int sysctl_check_table_dups(const char *path, struct ctl_table *old, |
| 845 | struct ctl_table *table) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 846 | { |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 847 | struct ctl_table *entry, *test; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 848 | int error = 0; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 849 | |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 850 | for (entry = old; entry->procname; entry++) { |
| 851 | for (test = table; test->procname; test++) { |
| 852 | if (strcmp(entry->procname, test->procname) == 0) { |
| 853 | printk(KERN_ERR "sysctl duplicate entry: %s/%s\n", |
| 854 | path, test->procname); |
| 855 | error = -EEXIST; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 856 | } |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 857 | } |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 858 | } |
| 859 | return error; |
| 860 | } |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 861 | |
| 862 | static int sysctl_check_dups(struct nsproxy *namespaces, |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 863 | struct ctl_dir *dir, |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 864 | const char *path, struct ctl_table *table) |
| 865 | { |
| 866 | struct ctl_table_root *root; |
| 867 | struct ctl_table_set *set; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 868 | struct ctl_table_header *head; |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 869 | int error = 0; |
| 870 | |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 871 | root = &sysctl_table_root; |
| 872 | do { |
| 873 | set = lookup_header_set(root, namespaces); |
| 874 | |
| 875 | list_for_each_entry(head, &set->list, ctl_entry) { |
| 876 | if (head->unregistering) |
| 877 | continue; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 878 | if (head->parent != dir) |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 879 | continue; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 880 | error = sysctl_check_table_dups(path, head->ctl_table, |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 881 | table); |
| 882 | } |
| 883 | root = list_entry(root->root_list.next, |
| 884 | struct ctl_table_root, root_list); |
| 885 | } while (root != &sysctl_table_root); |
| 886 | return error; |
| 887 | } |
| 888 | |
| 889 | static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...) |
| 890 | { |
| 891 | struct va_format vaf; |
| 892 | va_list args; |
| 893 | |
| 894 | va_start(args, fmt); |
| 895 | vaf.fmt = fmt; |
| 896 | vaf.va = &args; |
| 897 | |
| 898 | printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n", |
| 899 | path, table->procname, &vaf); |
| 900 | |
| 901 | va_end(args); |
| 902 | return -EINVAL; |
| 903 | } |
| 904 | |
| 905 | static int sysctl_check_table(const char *path, struct ctl_table *table) |
| 906 | { |
| 907 | int err = 0; |
| 908 | for (; table->procname; table++) { |
| 909 | if (table->child) |
| 910 | err = sysctl_err(path, table, "Not a file"); |
| 911 | |
| 912 | if ((table->proc_handler == proc_dostring) || |
| 913 | (table->proc_handler == proc_dointvec) || |
| 914 | (table->proc_handler == proc_dointvec_minmax) || |
| 915 | (table->proc_handler == proc_dointvec_jiffies) || |
| 916 | (table->proc_handler == proc_dointvec_userhz_jiffies) || |
| 917 | (table->proc_handler == proc_dointvec_ms_jiffies) || |
| 918 | (table->proc_handler == proc_doulongvec_minmax) || |
| 919 | (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) { |
| 920 | if (!table->data) |
| 921 | err = sysctl_err(path, table, "No data"); |
| 922 | if (!table->maxlen) |
| 923 | err = sysctl_err(path, table, "No maxlen"); |
| 924 | } |
| 925 | if (!table->proc_handler) |
| 926 | err = sysctl_err(path, table, "No proc_handler"); |
| 927 | |
| 928 | if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode) |
| 929 | err = sysctl_err(path, table, "bogus .mode 0%o", |
| 930 | table->mode); |
| 931 | } |
| 932 | return err; |
| 933 | } |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 934 | |
| 935 | /** |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 936 | * __register_sysctl_table - register a leaf sysctl table |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 937 | * @root: List of sysctl headers to register on |
| 938 | * @namespaces: Data to compute which lists of sysctl entries are visible |
| 939 | * @path: The path to the directory the sysctl table is in. |
| 940 | * @table: the top-level table structure |
| 941 | * |
| 942 | * Register a sysctl table hierarchy. @table should be a filled in ctl_table |
| 943 | * array. A completely 0 filled entry terminates the table. |
| 944 | * |
| 945 | * The members of the &struct ctl_table structure are used as follows: |
| 946 | * |
| 947 | * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not |
| 948 | * enter a sysctl file |
| 949 | * |
| 950 | * data - a pointer to data for use by proc_handler |
| 951 | * |
| 952 | * maxlen - the maximum size in bytes of the data |
| 953 | * |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 954 | * mode - the file permissions for the /proc/sys file |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 955 | * |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 956 | * child - must be %NULL. |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 957 | * |
| 958 | * proc_handler - the text handler routine (described below) |
| 959 | * |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 960 | * extra1, extra2 - extra pointers usable by the proc handler routines |
| 961 | * |
| 962 | * Leaf nodes in the sysctl tree will be represented by a single file |
| 963 | * under /proc; non-leaf nodes will be represented by directories. |
| 964 | * |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 965 | * There must be a proc_handler routine for any terminal nodes. |
| 966 | * Several default handlers are available to cover common cases - |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 967 | * |
| 968 | * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(), |
| 969 | * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), |
| 970 | * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax() |
| 971 | * |
| 972 | * It is the handler's job to read the input buffer from user memory |
| 973 | * and process it. The handler should return 0 on success. |
| 974 | * |
| 975 | * This routine returns %NULL on a failure to register, and a pointer |
| 976 | * to the table header on success. |
| 977 | */ |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 978 | struct ctl_table_header *__register_sysctl_table( |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 979 | struct ctl_table_root *root, |
| 980 | struct nsproxy *namespaces, |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 981 | const char *path, struct ctl_table *table) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 982 | { |
| 983 | struct ctl_table_header *header; |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 984 | const char *name, *nextname; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 985 | struct ctl_table_set *set; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 986 | struct ctl_dir *dir; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 987 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 988 | header = kzalloc(sizeof(struct ctl_table_header), GFP_KERNEL); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 989 | if (!header) |
| 990 | return NULL; |
| 991 | |
Eric W. Biederman | e0d0452 | 2012-01-09 22:36:41 -0800 | [diff] [blame] | 992 | init_header(header, root, NULL, table); |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 993 | if (sysctl_check_table(path, table)) |
| 994 | goto fail; |
Eric W. Biederman | 8d6ecfc | 2012-01-06 11:55:30 -0800 | [diff] [blame] | 995 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 996 | spin_lock(&sysctl_lock); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 997 | header->set = set = lookup_header_set(root, namespaces); |
| 998 | dir = &sysctl_root_dir; |
| 999 | dir->header.nreg++; |
| 1000 | spin_unlock(&sysctl_lock); |
| 1001 | |
| 1002 | /* Find the directory for the ctl_table */ |
| 1003 | for (name = path; name; name = nextname) { |
| 1004 | int namelen; |
| 1005 | nextname = strchr(name, '/'); |
| 1006 | if (nextname) { |
| 1007 | namelen = nextname - name; |
| 1008 | nextname++; |
| 1009 | } else { |
| 1010 | namelen = strlen(name); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1011 | } |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1012 | if (namelen == 0) |
| 1013 | continue; |
| 1014 | |
| 1015 | dir = get_subdir(set, dir, name, namelen); |
| 1016 | if (IS_ERR(dir)) |
| 1017 | goto fail; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1018 | } |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1019 | spin_lock(&sysctl_lock); |
| 1020 | if (sysctl_check_dups(namespaces, dir, path, table)) |
| 1021 | goto fail_put_dir_locked; |
| 1022 | insert_header(dir, header); |
| 1023 | drop_sysctl_table(&dir->header); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1024 | spin_unlock(&sysctl_lock); |
| 1025 | |
| 1026 | return header; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1027 | fail_put_dir_locked: |
| 1028 | drop_sysctl_table(&dir->header); |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 1029 | spin_unlock(&sysctl_lock); |
| 1030 | fail: |
| 1031 | kfree(header); |
| 1032 | dump_stack(); |
| 1033 | return NULL; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1036 | static char *append_path(const char *path, char *pos, const char *name) |
| 1037 | { |
| 1038 | int namelen; |
| 1039 | namelen = strlen(name); |
| 1040 | if (((pos - path) + namelen + 2) >= PATH_MAX) |
| 1041 | return NULL; |
| 1042 | memcpy(pos, name, namelen); |
| 1043 | pos[namelen] = '/'; |
| 1044 | pos[namelen + 1] = '\0'; |
| 1045 | pos += namelen + 1; |
| 1046 | return pos; |
| 1047 | } |
| 1048 | |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1049 | static int count_subheaders(struct ctl_table *table) |
| 1050 | { |
| 1051 | int has_files = 0; |
| 1052 | int nr_subheaders = 0; |
| 1053 | struct ctl_table *entry; |
| 1054 | |
| 1055 | /* special case: no directory and empty directory */ |
| 1056 | if (!table || !table->procname) |
| 1057 | return 1; |
| 1058 | |
| 1059 | for (entry = table; entry->procname; entry++) { |
| 1060 | if (entry->child) |
| 1061 | nr_subheaders += count_subheaders(entry->child); |
| 1062 | else |
| 1063 | has_files = 1; |
| 1064 | } |
| 1065 | return nr_subheaders + has_files; |
| 1066 | } |
| 1067 | |
| 1068 | static int register_leaf_sysctl_tables(const char *path, char *pos, |
| 1069 | struct ctl_table_header ***subheader, |
| 1070 | struct ctl_table_root *root, struct nsproxy *namespaces, |
| 1071 | struct ctl_table *table) |
| 1072 | { |
| 1073 | struct ctl_table *ctl_table_arg = NULL; |
| 1074 | struct ctl_table *entry, *files; |
| 1075 | int nr_files = 0; |
| 1076 | int nr_dirs = 0; |
| 1077 | int err = -ENOMEM; |
| 1078 | |
| 1079 | for (entry = table; entry->procname; entry++) { |
| 1080 | if (entry->child) |
| 1081 | nr_dirs++; |
| 1082 | else |
| 1083 | nr_files++; |
| 1084 | } |
| 1085 | |
| 1086 | files = table; |
| 1087 | /* If there are mixed files and directories we need a new table */ |
| 1088 | if (nr_dirs && nr_files) { |
| 1089 | struct ctl_table *new; |
| 1090 | files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1), |
| 1091 | GFP_KERNEL); |
| 1092 | if (!files) |
| 1093 | goto out; |
| 1094 | |
| 1095 | ctl_table_arg = files; |
| 1096 | for (new = files, entry = table; entry->procname; entry++) { |
| 1097 | if (entry->child) |
| 1098 | continue; |
| 1099 | *new = *entry; |
| 1100 | new++; |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | /* Register everything except a directory full of subdirectories */ |
| 1105 | if (nr_files || !nr_dirs) { |
| 1106 | struct ctl_table_header *header; |
| 1107 | header = __register_sysctl_table(root, namespaces, path, files); |
| 1108 | if (!header) { |
| 1109 | kfree(ctl_table_arg); |
| 1110 | goto out; |
| 1111 | } |
| 1112 | |
| 1113 | /* Remember if we need to free the file table */ |
| 1114 | header->ctl_table_arg = ctl_table_arg; |
| 1115 | **subheader = header; |
| 1116 | (*subheader)++; |
| 1117 | } |
| 1118 | |
| 1119 | /* Recurse into the subdirectories. */ |
| 1120 | for (entry = table; entry->procname; entry++) { |
| 1121 | char *child_pos; |
| 1122 | |
| 1123 | if (!entry->child) |
| 1124 | continue; |
| 1125 | |
| 1126 | err = -ENAMETOOLONG; |
| 1127 | child_pos = append_path(path, pos, entry->procname); |
| 1128 | if (!child_pos) |
| 1129 | goto out; |
| 1130 | |
| 1131 | err = register_leaf_sysctl_tables(path, child_pos, subheader, |
| 1132 | root, namespaces, entry->child); |
| 1133 | pos[0] = '\0'; |
| 1134 | if (err) |
| 1135 | goto out; |
| 1136 | } |
| 1137 | err = 0; |
| 1138 | out: |
| 1139 | /* On failure our caller will unregister all registered subheaders */ |
| 1140 | return err; |
| 1141 | } |
| 1142 | |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1143 | /** |
| 1144 | * __register_sysctl_paths - register a sysctl table hierarchy |
| 1145 | * @root: List of sysctl headers to register on |
| 1146 | * @namespaces: Data to compute which lists of sysctl entries are visible |
| 1147 | * @path: The path to the directory the sysctl table is in. |
| 1148 | * @table: the top-level table structure |
| 1149 | * |
| 1150 | * Register a sysctl table hierarchy. @table should be a filled in ctl_table |
| 1151 | * array. A completely 0 filled entry terminates the table. |
| 1152 | * |
| 1153 | * See __register_sysctl_table for more details. |
| 1154 | */ |
| 1155 | struct ctl_table_header *__register_sysctl_paths( |
| 1156 | struct ctl_table_root *root, |
| 1157 | struct nsproxy *namespaces, |
| 1158 | const struct ctl_path *path, struct ctl_table *table) |
| 1159 | { |
Eric W. Biederman | ec6a526 | 2012-01-21 12:35:23 -0800 | [diff] [blame] | 1160 | struct ctl_table *ctl_table_arg = table; |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1161 | int nr_subheaders = count_subheaders(table); |
| 1162 | struct ctl_table_header *header = NULL, **subheaders, **subheader; |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1163 | const struct ctl_path *component; |
| 1164 | char *new_path, *pos; |
| 1165 | |
| 1166 | pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL); |
| 1167 | if (!new_path) |
| 1168 | return NULL; |
| 1169 | |
| 1170 | pos[0] = '\0'; |
| 1171 | for (component = path; component->procname; component++) { |
| 1172 | pos = append_path(new_path, pos, component->procname); |
| 1173 | if (!pos) |
| 1174 | goto out; |
| 1175 | } |
Eric W. Biederman | ec6a526 | 2012-01-21 12:35:23 -0800 | [diff] [blame] | 1176 | while (table->procname && table->child && !table[1].procname) { |
| 1177 | pos = append_path(new_path, pos, table->procname); |
| 1178 | if (!pos) |
| 1179 | goto out; |
| 1180 | table = table->child; |
| 1181 | } |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1182 | if (nr_subheaders == 1) { |
| 1183 | header = __register_sysctl_table(root, namespaces, new_path, table); |
| 1184 | if (header) |
| 1185 | header->ctl_table_arg = ctl_table_arg; |
| 1186 | } else { |
| 1187 | header = kzalloc(sizeof(*header) + |
| 1188 | sizeof(*subheaders)*nr_subheaders, GFP_KERNEL); |
| 1189 | if (!header) |
| 1190 | goto out; |
| 1191 | |
| 1192 | subheaders = (struct ctl_table_header **) (header + 1); |
| 1193 | subheader = subheaders; |
Eric W. Biederman | ec6a526 | 2012-01-21 12:35:23 -0800 | [diff] [blame] | 1194 | header->ctl_table_arg = ctl_table_arg; |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1195 | |
| 1196 | if (register_leaf_sysctl_tables(new_path, pos, &subheader, |
| 1197 | root, namespaces, table)) |
| 1198 | goto err_register_leaves; |
| 1199 | } |
| 1200 | |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1201 | out: |
| 1202 | kfree(new_path); |
| 1203 | return header; |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1204 | |
| 1205 | err_register_leaves: |
| 1206 | while (subheader > subheaders) { |
| 1207 | struct ctl_table_header *subh = *(--subheader); |
| 1208 | struct ctl_table *table = subh->ctl_table_arg; |
| 1209 | unregister_sysctl_table(subh); |
| 1210 | kfree(table); |
| 1211 | } |
| 1212 | kfree(header); |
| 1213 | header = NULL; |
| 1214 | goto out; |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1215 | } |
| 1216 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1217 | /** |
| 1218 | * register_sysctl_table_path - register a sysctl table hierarchy |
| 1219 | * @path: The path to the directory the sysctl table is in. |
| 1220 | * @table: the top-level table structure |
| 1221 | * |
| 1222 | * Register a sysctl table hierarchy. @table should be a filled in ctl_table |
| 1223 | * array. A completely 0 filled entry terminates the table. |
| 1224 | * |
| 1225 | * See __register_sysctl_paths for more details. |
| 1226 | */ |
| 1227 | struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path, |
| 1228 | struct ctl_table *table) |
| 1229 | { |
| 1230 | return __register_sysctl_paths(&sysctl_table_root, current->nsproxy, |
| 1231 | path, table); |
| 1232 | } |
| 1233 | EXPORT_SYMBOL(register_sysctl_paths); |
| 1234 | |
| 1235 | /** |
| 1236 | * register_sysctl_table - register a sysctl table hierarchy |
| 1237 | * @table: the top-level table structure |
| 1238 | * |
| 1239 | * Register a sysctl table hierarchy. @table should be a filled in ctl_table |
| 1240 | * array. A completely 0 filled entry terminates the table. |
| 1241 | * |
| 1242 | * See register_sysctl_paths for more details. |
| 1243 | */ |
| 1244 | struct ctl_table_header *register_sysctl_table(struct ctl_table *table) |
| 1245 | { |
| 1246 | static const struct ctl_path null_path[] = { {} }; |
| 1247 | |
| 1248 | return register_sysctl_paths(null_path, table); |
| 1249 | } |
| 1250 | EXPORT_SYMBOL(register_sysctl_table); |
| 1251 | |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1252 | static void drop_sysctl_table(struct ctl_table_header *header) |
| 1253 | { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1254 | struct ctl_dir *parent = header->parent; |
| 1255 | |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1256 | if (--header->nreg) |
| 1257 | return; |
| 1258 | |
| 1259 | start_unregistering(header); |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1260 | if (!--header->count) |
| 1261 | kfree_rcu(header, rcu); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1262 | |
| 1263 | if (parent) |
| 1264 | drop_sysctl_table(&parent->header); |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1265 | } |
| 1266 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1267 | /** |
| 1268 | * unregister_sysctl_table - unregister a sysctl table hierarchy |
| 1269 | * @header: the header returned from register_sysctl_table |
| 1270 | * |
| 1271 | * Unregisters the sysctl table and all children. proc entries may not |
| 1272 | * actually be removed until they are no longer used by anyone. |
| 1273 | */ |
| 1274 | void unregister_sysctl_table(struct ctl_table_header * header) |
| 1275 | { |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1276 | int nr_subheaders; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1277 | might_sleep(); |
| 1278 | |
| 1279 | if (header == NULL) |
| 1280 | return; |
| 1281 | |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1282 | nr_subheaders = count_subheaders(header->ctl_table_arg); |
| 1283 | if (unlikely(nr_subheaders > 1)) { |
| 1284 | struct ctl_table_header **subheaders; |
| 1285 | int i; |
| 1286 | |
| 1287 | subheaders = (struct ctl_table_header **)(header + 1); |
| 1288 | for (i = nr_subheaders -1; i >= 0; i--) { |
| 1289 | struct ctl_table_header *subh = subheaders[i]; |
| 1290 | struct ctl_table *table = subh->ctl_table_arg; |
| 1291 | unregister_sysctl_table(subh); |
| 1292 | kfree(table); |
| 1293 | } |
| 1294 | kfree(header); |
| 1295 | return; |
| 1296 | } |
| 1297 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1298 | spin_lock(&sysctl_lock); |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1299 | drop_sysctl_table(header); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1300 | spin_unlock(&sysctl_lock); |
| 1301 | } |
| 1302 | EXPORT_SYMBOL(unregister_sysctl_table); |
| 1303 | |
| 1304 | void setup_sysctl_set(struct ctl_table_set *p, |
Eric W. Biederman | 9eb47c2 | 2012-01-22 21:26:00 -0800 | [diff] [blame] | 1305 | struct ctl_table_root *root, |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1306 | int (*is_seen)(struct ctl_table_set *)) |
| 1307 | { |
| 1308 | INIT_LIST_HEAD(&p->list); |
Eric W. Biederman | 9eb47c2 | 2012-01-22 21:26:00 -0800 | [diff] [blame] | 1309 | p->root = root; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1310 | p->is_seen = is_seen; |
| 1311 | } |
| 1312 | |
Eric W. Biederman | 97324cd | 2012-01-09 22:19:13 -0800 | [diff] [blame] | 1313 | void retire_sysctl_set(struct ctl_table_set *set) |
| 1314 | { |
| 1315 | WARN_ON(!list_empty(&set->list)); |
| 1316 | } |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1317 | |
Alexey Dobriyan | 1e0edd3 | 2008-10-17 05:07:44 +0400 | [diff] [blame] | 1318 | int __init proc_sys_init(void) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1319 | { |
Alexey Dobriyan | e167523 | 2008-10-03 00:23:32 +0400 | [diff] [blame] | 1320 | struct proc_dir_entry *proc_sys_root; |
| 1321 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1322 | proc_sys_root = proc_mkdir("sys", NULL); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 1323 | proc_sys_root->proc_iops = &proc_sys_dir_operations; |
| 1324 | proc_sys_root->proc_fops = &proc_sys_dir_file_operations; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1325 | proc_sys_root->nlink = 0; |
Eric W. Biederman | de4e83bd | 2012-01-06 03:34:20 -0800 | [diff] [blame] | 1326 | |
| 1327 | return sysctl_init(); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1328 | } |