]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/proc/generic.c
UBUNTU: SAUCE: Revert "audit: fix auditd/kernel connection state tracking"
[mirror_ubuntu-zesty-kernel.git] / fs / proc / generic.c
1 /*
2 * proc/fs/generic.c --- generic routines for the proc-fs
3 *
4 * This file contains generic proc-fs routines for handling
5 * directories and files.
6 *
7 * Copyright (C) 1991, 1992 Linus Torvalds.
8 * Copyright (C) 1997 Theodore Ts'o
9 */
10
11 #include <linux/errno.h>
12 #include <linux/time.h>
13 #include <linux/proc_fs.h>
14 #include <linux/stat.h>
15 #include <linux/mm.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/printk.h>
19 #include <linux/mount.h>
20 #include <linux/init.h>
21 #include <linux/idr.h>
22 #include <linux/bitops.h>
23 #include <linux/spinlock.h>
24 #include <linux/completion.h>
25 #include <linux/uaccess.h>
26
27 #include "internal.h"
28
29 static DEFINE_RWLOCK(proc_subdir_lock);
30
31 static int proc_match(unsigned int len, const char *name, struct proc_dir_entry *de)
32 {
33 if (len < de->namelen)
34 return -1;
35 if (len > de->namelen)
36 return 1;
37
38 return memcmp(name, de->name, len);
39 }
40
41 static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir)
42 {
43 return rb_entry_safe(rb_first(&dir->subdir), struct proc_dir_entry,
44 subdir_node);
45 }
46
47 static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir)
48 {
49 return rb_entry_safe(rb_next(&dir->subdir_node), struct proc_dir_entry,
50 subdir_node);
51 }
52
53 static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,
54 const char *name,
55 unsigned int len)
56 {
57 struct rb_node *node = dir->subdir.rb_node;
58
59 while (node) {
60 struct proc_dir_entry *de = container_of(node,
61 struct proc_dir_entry,
62 subdir_node);
63 int result = proc_match(len, name, de);
64
65 if (result < 0)
66 node = node->rb_left;
67 else if (result > 0)
68 node = node->rb_right;
69 else
70 return de;
71 }
72 return NULL;
73 }
74
75 static bool pde_subdir_insert(struct proc_dir_entry *dir,
76 struct proc_dir_entry *de)
77 {
78 struct rb_root *root = &dir->subdir;
79 struct rb_node **new = &root->rb_node, *parent = NULL;
80
81 /* Figure out where to put new node */
82 while (*new) {
83 struct proc_dir_entry *this =
84 container_of(*new, struct proc_dir_entry, subdir_node);
85 int result = proc_match(de->namelen, de->name, this);
86
87 parent = *new;
88 if (result < 0)
89 new = &(*new)->rb_left;
90 else if (result > 0)
91 new = &(*new)->rb_right;
92 else
93 return false;
94 }
95
96 /* Add new node and rebalance tree. */
97 rb_link_node(&de->subdir_node, parent, new);
98 rb_insert_color(&de->subdir_node, root);
99 return true;
100 }
101
102 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
103 {
104 struct inode *inode = d_inode(dentry);
105 struct proc_dir_entry *de = PDE(inode);
106 struct user_namespace *s_user_ns;
107 int error;
108
109 /* Don't let anyone mess with weird proc files */
110 s_user_ns = inode->i_sb->s_user_ns;
111 if (!kuid_has_mapping(s_user_ns, inode->i_uid) ||
112 !kgid_has_mapping(s_user_ns, inode->i_gid))
113 return -EPERM;
114
115 error = setattr_prepare(dentry, iattr);
116 if (error)
117 return error;
118
119 setattr_copy(inode, iattr);
120 mark_inode_dirty(inode);
121
122 proc_set_user(de, inode->i_uid, inode->i_gid);
123 de->mode = inode->i_mode;
124 return 0;
125 }
126
127 static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
128 struct kstat *stat)
129 {
130 struct inode *inode = d_inode(dentry);
131 struct proc_dir_entry *de = PDE(inode);
132 if (de && de->nlink)
133 set_nlink(inode, de->nlink);
134
135 generic_fillattr(inode, stat);
136 return 0;
137 }
138
139 static const struct inode_operations proc_file_inode_operations = {
140 .setattr = proc_notify_change,
141 };
142
143 /*
144 * This function parses a name such as "tty/driver/serial", and
145 * returns the struct proc_dir_entry for "/proc/tty/driver", and
146 * returns "serial" in residual.
147 */
148 static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
149 const char **residual)
150 {
151 const char *cp = name, *next;
152 struct proc_dir_entry *de;
153 unsigned int len;
154
155 de = *ret;
156 if (!de)
157 de = &proc_root;
158
159 while (1) {
160 next = strchr(cp, '/');
161 if (!next)
162 break;
163
164 len = next - cp;
165 de = pde_subdir_find(de, cp, len);
166 if (!de) {
167 WARN(1, "name '%s'\n", name);
168 return -ENOENT;
169 }
170 cp += len + 1;
171 }
172 *residual = cp;
173 *ret = de;
174 return 0;
175 }
176
177 static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
178 const char **residual)
179 {
180 int rv;
181
182 read_lock(&proc_subdir_lock);
183 rv = __xlate_proc_name(name, ret, residual);
184 read_unlock(&proc_subdir_lock);
185 return rv;
186 }
187
188 static DEFINE_IDA(proc_inum_ida);
189 static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
190
191 #define PROC_DYNAMIC_FIRST 0xF0000000U
192
193 /*
194 * Return an inode number between PROC_DYNAMIC_FIRST and
195 * 0xffffffff, or zero on failure.
196 */
197 int proc_alloc_inum(unsigned int *inum)
198 {
199 unsigned int i;
200 int error;
201
202 retry:
203 if (!ida_pre_get(&proc_inum_ida, GFP_KERNEL))
204 return -ENOMEM;
205
206 spin_lock_irq(&proc_inum_lock);
207 error = ida_get_new(&proc_inum_ida, &i);
208 spin_unlock_irq(&proc_inum_lock);
209 if (error == -EAGAIN)
210 goto retry;
211 else if (error)
212 return error;
213
214 if (i > UINT_MAX - PROC_DYNAMIC_FIRST) {
215 spin_lock_irq(&proc_inum_lock);
216 ida_remove(&proc_inum_ida, i);
217 spin_unlock_irq(&proc_inum_lock);
218 return -ENOSPC;
219 }
220 *inum = PROC_DYNAMIC_FIRST + i;
221 return 0;
222 }
223
224 void proc_free_inum(unsigned int inum)
225 {
226 unsigned long flags;
227 spin_lock_irqsave(&proc_inum_lock, flags);
228 ida_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
229 spin_unlock_irqrestore(&proc_inum_lock, flags);
230 }
231
232 /*
233 * Don't create negative dentries here, return -ENOENT by hand
234 * instead.
235 */
236 struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
237 struct dentry *dentry)
238 {
239 struct inode *inode;
240
241 read_lock(&proc_subdir_lock);
242 de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len);
243 if (de) {
244 pde_get(de);
245 read_unlock(&proc_subdir_lock);
246 inode = proc_get_inode(dir->i_sb, de);
247 if (!inode)
248 return ERR_PTR(-ENOMEM);
249 d_set_d_op(dentry, &simple_dentry_operations);
250 d_add(dentry, inode);
251 return NULL;
252 }
253 read_unlock(&proc_subdir_lock);
254 return ERR_PTR(-ENOENT);
255 }
256
257 struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
258 unsigned int flags)
259 {
260 return proc_lookup_de(PDE(dir), dir, dentry);
261 }
262
263 /*
264 * This returns non-zero if at EOF, so that the /proc
265 * root directory can use this and check if it should
266 * continue with the <pid> entries..
267 *
268 * Note that the VFS-layer doesn't care about the return
269 * value of the readdir() call, as long as it's non-negative
270 * for success..
271 */
272 int proc_readdir_de(struct proc_dir_entry *de, struct file *file,
273 struct dir_context *ctx)
274 {
275 int i;
276
277 if (!dir_emit_dots(file, ctx))
278 return 0;
279
280 read_lock(&proc_subdir_lock);
281 de = pde_subdir_first(de);
282 i = ctx->pos - 2;
283 for (;;) {
284 if (!de) {
285 read_unlock(&proc_subdir_lock);
286 return 0;
287 }
288 if (!i)
289 break;
290 de = pde_subdir_next(de);
291 i--;
292 }
293
294 do {
295 struct proc_dir_entry *next;
296 pde_get(de);
297 read_unlock(&proc_subdir_lock);
298 if (!dir_emit(ctx, de->name, de->namelen,
299 de->low_ino, de->mode >> 12)) {
300 pde_put(de);
301 return 0;
302 }
303 read_lock(&proc_subdir_lock);
304 ctx->pos++;
305 next = pde_subdir_next(de);
306 pde_put(de);
307 de = next;
308 } while (de);
309 read_unlock(&proc_subdir_lock);
310 return 1;
311 }
312
313 int proc_readdir(struct file *file, struct dir_context *ctx)
314 {
315 struct inode *inode = file_inode(file);
316
317 return proc_readdir_de(PDE(inode), file, ctx);
318 }
319
320 /*
321 * These are the generic /proc directory operations. They
322 * use the in-memory "struct proc_dir_entry" tree to parse
323 * the /proc directory.
324 */
325 static const struct file_operations proc_dir_operations = {
326 .llseek = generic_file_llseek,
327 .read = generic_read_dir,
328 .iterate_shared = proc_readdir,
329 };
330
331 /*
332 * proc directories can do almost nothing..
333 */
334 static const struct inode_operations proc_dir_inode_operations = {
335 .lookup = proc_lookup,
336 .getattr = proc_getattr,
337 .setattr = proc_notify_change,
338 };
339
340 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
341 {
342 int ret;
343
344 ret = proc_alloc_inum(&dp->low_ino);
345 if (ret)
346 return ret;
347
348 write_lock(&proc_subdir_lock);
349 dp->parent = dir;
350 if (pde_subdir_insert(dir, dp) == false) {
351 WARN(1, "proc_dir_entry '%s/%s' already registered\n",
352 dir->name, dp->name);
353 write_unlock(&proc_subdir_lock);
354 proc_free_inum(dp->low_ino);
355 return -EEXIST;
356 }
357 write_unlock(&proc_subdir_lock);
358
359 return 0;
360 }
361
362 static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
363 const char *name,
364 umode_t mode,
365 nlink_t nlink)
366 {
367 struct proc_dir_entry *ent = NULL;
368 const char *fn;
369 struct qstr qstr;
370
371 if (xlate_proc_name(name, parent, &fn) != 0)
372 goto out;
373 qstr.name = fn;
374 qstr.len = strlen(fn);
375 if (qstr.len == 0 || qstr.len >= 256) {
376 WARN(1, "name len %u\n", qstr.len);
377 return NULL;
378 }
379 if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
380 WARN(1, "create '/proc/%s' by hand\n", qstr.name);
381 return NULL;
382 }
383 if (is_empty_pde(*parent)) {
384 WARN(1, "attempt to add to permanently empty directory");
385 return NULL;
386 }
387
388 ent = kzalloc(sizeof(struct proc_dir_entry) + qstr.len + 1, GFP_KERNEL);
389 if (!ent)
390 goto out;
391
392 memcpy(ent->name, fn, qstr.len + 1);
393 ent->namelen = qstr.len;
394 ent->mode = mode;
395 ent->nlink = nlink;
396 ent->subdir = RB_ROOT;
397 atomic_set(&ent->count, 1);
398 spin_lock_init(&ent->pde_unload_lock);
399 INIT_LIST_HEAD(&ent->pde_openers);
400 proc_set_user(ent, (*parent)->uid, (*parent)->gid);
401
402 out:
403 return ent;
404 }
405
406 struct proc_dir_entry *proc_symlink(const char *name,
407 struct proc_dir_entry *parent, const char *dest)
408 {
409 struct proc_dir_entry *ent;
410
411 ent = __proc_create(&parent, name,
412 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
413
414 if (ent) {
415 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
416 if (ent->data) {
417 strcpy((char*)ent->data,dest);
418 ent->proc_iops = &proc_link_inode_operations;
419 if (proc_register(parent, ent) < 0) {
420 kfree(ent->data);
421 kfree(ent);
422 ent = NULL;
423 }
424 } else {
425 kfree(ent);
426 ent = NULL;
427 }
428 }
429 return ent;
430 }
431 EXPORT_SYMBOL(proc_symlink);
432
433 struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
434 struct proc_dir_entry *parent, void *data)
435 {
436 struct proc_dir_entry *ent;
437
438 if (mode == 0)
439 mode = S_IRUGO | S_IXUGO;
440
441 ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
442 if (ent) {
443 ent->data = data;
444 ent->proc_fops = &proc_dir_operations;
445 ent->proc_iops = &proc_dir_inode_operations;
446 parent->nlink++;
447 if (proc_register(parent, ent) < 0) {
448 kfree(ent);
449 parent->nlink--;
450 ent = NULL;
451 }
452 }
453 return ent;
454 }
455 EXPORT_SYMBOL_GPL(proc_mkdir_data);
456
457 struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
458 struct proc_dir_entry *parent)
459 {
460 return proc_mkdir_data(name, mode, parent, NULL);
461 }
462 EXPORT_SYMBOL(proc_mkdir_mode);
463
464 struct proc_dir_entry *proc_mkdir(const char *name,
465 struct proc_dir_entry *parent)
466 {
467 return proc_mkdir_data(name, 0, parent, NULL);
468 }
469 EXPORT_SYMBOL(proc_mkdir);
470
471 struct proc_dir_entry *proc_create_mount_point(const char *name)
472 {
473 umode_t mode = S_IFDIR | S_IRUGO | S_IXUGO;
474 struct proc_dir_entry *ent, *parent = NULL;
475
476 ent = __proc_create(&parent, name, mode, 2);
477 if (ent) {
478 ent->data = NULL;
479 ent->proc_fops = NULL;
480 ent->proc_iops = NULL;
481 if (proc_register(parent, ent) < 0) {
482 kfree(ent);
483 parent->nlink--;
484 ent = NULL;
485 }
486 }
487 return ent;
488 }
489 EXPORT_SYMBOL(proc_create_mount_point);
490
491 struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
492 struct proc_dir_entry *parent,
493 const struct file_operations *proc_fops,
494 void *data)
495 {
496 struct proc_dir_entry *pde;
497 if ((mode & S_IFMT) == 0)
498 mode |= S_IFREG;
499
500 if (!S_ISREG(mode)) {
501 WARN_ON(1); /* use proc_mkdir() */
502 return NULL;
503 }
504
505 BUG_ON(proc_fops == NULL);
506
507 if ((mode & S_IALLUGO) == 0)
508 mode |= S_IRUGO;
509 pde = __proc_create(&parent, name, mode, 1);
510 if (!pde)
511 goto out;
512 pde->proc_fops = proc_fops;
513 pde->data = data;
514 pde->proc_iops = &proc_file_inode_operations;
515 if (proc_register(parent, pde) < 0)
516 goto out_free;
517 return pde;
518 out_free:
519 kfree(pde);
520 out:
521 return NULL;
522 }
523 EXPORT_SYMBOL(proc_create_data);
524
525 void proc_set_size(struct proc_dir_entry *de, loff_t size)
526 {
527 de->size = size;
528 }
529 EXPORT_SYMBOL(proc_set_size);
530
531 void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
532 {
533 de->uid = uid;
534 de->gid = gid;
535 }
536 EXPORT_SYMBOL(proc_set_user);
537
538 static void free_proc_entry(struct proc_dir_entry *de)
539 {
540 proc_free_inum(de->low_ino);
541
542 if (S_ISLNK(de->mode))
543 kfree(de->data);
544 kfree(de);
545 }
546
547 void pde_put(struct proc_dir_entry *pde)
548 {
549 if (atomic_dec_and_test(&pde->count))
550 free_proc_entry(pde);
551 }
552
553 /*
554 * Remove a /proc entry and free it if it's not currently in use.
555 */
556 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
557 {
558 struct proc_dir_entry *de = NULL;
559 const char *fn = name;
560 unsigned int len;
561
562 write_lock(&proc_subdir_lock);
563 if (__xlate_proc_name(name, &parent, &fn) != 0) {
564 write_unlock(&proc_subdir_lock);
565 return;
566 }
567 len = strlen(fn);
568
569 de = pde_subdir_find(parent, fn, len);
570 if (de)
571 rb_erase(&de->subdir_node, &parent->subdir);
572 write_unlock(&proc_subdir_lock);
573 if (!de) {
574 WARN(1, "name '%s'\n", name);
575 return;
576 }
577
578 proc_entry_rundown(de);
579
580 if (S_ISDIR(de->mode))
581 parent->nlink--;
582 de->nlink = 0;
583 WARN(pde_subdir_first(de),
584 "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n",
585 __func__, de->parent->name, de->name, pde_subdir_first(de)->name);
586 pde_put(de);
587 }
588 EXPORT_SYMBOL(remove_proc_entry);
589
590 int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
591 {
592 struct proc_dir_entry *root = NULL, *de, *next;
593 const char *fn = name;
594 unsigned int len;
595
596 write_lock(&proc_subdir_lock);
597 if (__xlate_proc_name(name, &parent, &fn) != 0) {
598 write_unlock(&proc_subdir_lock);
599 return -ENOENT;
600 }
601 len = strlen(fn);
602
603 root = pde_subdir_find(parent, fn, len);
604 if (!root) {
605 write_unlock(&proc_subdir_lock);
606 return -ENOENT;
607 }
608 rb_erase(&root->subdir_node, &parent->subdir);
609
610 de = root;
611 while (1) {
612 next = pde_subdir_first(de);
613 if (next) {
614 rb_erase(&next->subdir_node, &de->subdir);
615 de = next;
616 continue;
617 }
618 write_unlock(&proc_subdir_lock);
619
620 proc_entry_rundown(de);
621 next = de->parent;
622 if (S_ISDIR(de->mode))
623 next->nlink--;
624 de->nlink = 0;
625 if (de == root)
626 break;
627 pde_put(de);
628
629 write_lock(&proc_subdir_lock);
630 de = next;
631 }
632 pde_put(root);
633 return 0;
634 }
635 EXPORT_SYMBOL(remove_proc_subtree);
636
637 void *proc_get_parent_data(const struct inode *inode)
638 {
639 struct proc_dir_entry *de = PDE(inode);
640 return de->parent->data;
641 }
642 EXPORT_SYMBOL_GPL(proc_get_parent_data);
643
644 void proc_remove(struct proc_dir_entry *de)
645 {
646 if (de)
647 remove_proc_subtree(de->name, de->parent);
648 }
649 EXPORT_SYMBOL(proc_remove);
650
651 void *PDE_DATA(const struct inode *inode)
652 {
653 return __PDE_DATA(inode);
654 }
655 EXPORT_SYMBOL(PDE_DATA);