proc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* proc.c: proc files for key database enumeration
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/fs.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/seq_file.h>
  18. #include <asm/errno.h>
  19. #include "internal.h"
  20. #ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
  21. static int proc_keys_open(struct inode *inode, struct file *file);
  22. static void *proc_keys_start(struct seq_file *p, loff_t *_pos);
  23. static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos);
  24. static void proc_keys_stop(struct seq_file *p, void *v);
  25. static int proc_keys_show(struct seq_file *m, void *v);
  26. static const struct seq_operations proc_keys_ops = {
  27. .start = proc_keys_start,
  28. .next = proc_keys_next,
  29. .stop = proc_keys_stop,
  30. .show = proc_keys_show,
  31. };
  32. static const struct file_operations proc_keys_fops = {
  33. .open = proc_keys_open,
  34. .read = seq_read,
  35. .llseek = seq_lseek,
  36. .release = seq_release,
  37. };
  38. #endif
  39. static int proc_key_users_open(struct inode *inode, struct file *file);
  40. static void *proc_key_users_start(struct seq_file *p, loff_t *_pos);
  41. static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos);
  42. static void proc_key_users_stop(struct seq_file *p, void *v);
  43. static int proc_key_users_show(struct seq_file *m, void *v);
  44. static const struct seq_operations proc_key_users_ops = {
  45. .start = proc_key_users_start,
  46. .next = proc_key_users_next,
  47. .stop = proc_key_users_stop,
  48. .show = proc_key_users_show,
  49. };
  50. static const struct file_operations proc_key_users_fops = {
  51. .open = proc_key_users_open,
  52. .read = seq_read,
  53. .llseek = seq_lseek,
  54. .release = seq_release,
  55. };
  56. /*****************************************************************************/
  57. /*
  58. * declare the /proc files
  59. */
  60. static int __init key_proc_init(void)
  61. {
  62. struct proc_dir_entry *p;
  63. #ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
  64. p = proc_create("keys", 0, NULL, &proc_keys_fops);
  65. if (!p)
  66. panic("Cannot create /proc/keys\n");
  67. #endif
  68. p = proc_create("key-users", 0, NULL, &proc_key_users_fops);
  69. if (!p)
  70. panic("Cannot create /proc/key-users\n");
  71. return 0;
  72. } /* end key_proc_init() */
  73. __initcall(key_proc_init);
  74. /*****************************************************************************/
  75. /*
  76. * implement "/proc/keys" to provides a list of the keys on the system
  77. */
  78. #ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
  79. static int proc_keys_open(struct inode *inode, struct file *file)
  80. {
  81. return seq_open(file, &proc_keys_ops);
  82. }
  83. static void *proc_keys_start(struct seq_file *p, loff_t *_pos)
  84. {
  85. struct rb_node *_p;
  86. loff_t pos = *_pos;
  87. spin_lock(&key_serial_lock);
  88. _p = rb_first(&key_serial_tree);
  89. while (pos > 0 && _p) {
  90. pos--;
  91. _p = rb_next(_p);
  92. }
  93. return _p;
  94. }
  95. static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos)
  96. {
  97. (*_pos)++;
  98. return rb_next((struct rb_node *) v);
  99. }
  100. static void proc_keys_stop(struct seq_file *p, void *v)
  101. {
  102. spin_unlock(&key_serial_lock);
  103. }
  104. static int proc_keys_show(struct seq_file *m, void *v)
  105. {
  106. struct rb_node *_p = v;
  107. struct key *key = rb_entry(_p, struct key, serial_node);
  108. struct timespec now;
  109. unsigned long timo;
  110. char xbuf[12];
  111. int rc;
  112. /* check whether the current task is allowed to view the key (assuming
  113. * non-possession)
  114. * - the caller holds a spinlock, and thus the RCU read lock, making our
  115. * access to __current_cred() safe
  116. */
  117. rc = key_task_permission(make_key_ref(key, 0), current_cred(),
  118. KEY_VIEW);
  119. if (rc < 0)
  120. return 0;
  121. now = current_kernel_time();
  122. rcu_read_lock();
  123. /* come up with a suitable timeout value */
  124. if (key->expiry == 0) {
  125. memcpy(xbuf, "perm", 5);
  126. }
  127. else if (now.tv_sec >= key->expiry) {
  128. memcpy(xbuf, "expd", 5);
  129. }
  130. else {
  131. timo = key->expiry - now.tv_sec;
  132. if (timo < 60)
  133. sprintf(xbuf, "%lus", timo);
  134. else if (timo < 60*60)
  135. sprintf(xbuf, "%lum", timo / 60);
  136. else if (timo < 60*60*24)
  137. sprintf(xbuf, "%luh", timo / (60*60));
  138. else if (timo < 60*60*24*7)
  139. sprintf(xbuf, "%lud", timo / (60*60*24));
  140. else
  141. sprintf(xbuf, "%luw", timo / (60*60*24*7));
  142. }
  143. #define showflag(KEY, LETTER, FLAG) \
  144. (test_bit(FLAG, &(KEY)->flags) ? LETTER : '-')
  145. seq_printf(m, "%08x %c%c%c%c%c%c %5d %4s %08x %5d %5d %-9.9s ",
  146. key->serial,
  147. showflag(key, 'I', KEY_FLAG_INSTANTIATED),
  148. showflag(key, 'R', KEY_FLAG_REVOKED),
  149. showflag(key, 'D', KEY_FLAG_DEAD),
  150. showflag(key, 'Q', KEY_FLAG_IN_QUOTA),
  151. showflag(key, 'U', KEY_FLAG_USER_CONSTRUCT),
  152. showflag(key, 'N', KEY_FLAG_NEGATIVE),
  153. atomic_read(&key->usage),
  154. xbuf,
  155. key->perm,
  156. key->uid,
  157. key->gid,
  158. key->type->name);
  159. #undef showflag
  160. if (key->type->describe)
  161. key->type->describe(key, m);
  162. seq_putc(m, '\n');
  163. rcu_read_unlock();
  164. return 0;
  165. }
  166. #endif /* CONFIG_KEYS_DEBUG_PROC_KEYS */
  167. /*****************************************************************************/
  168. /*
  169. * implement "/proc/key-users" to provides a list of the key users
  170. */
  171. static int proc_key_users_open(struct inode *inode, struct file *file)
  172. {
  173. return seq_open(file, &proc_key_users_ops);
  174. }
  175. static void *proc_key_users_start(struct seq_file *p, loff_t *_pos)
  176. {
  177. struct rb_node *_p;
  178. loff_t pos = *_pos;
  179. spin_lock(&key_user_lock);
  180. _p = rb_first(&key_user_tree);
  181. while (pos > 0 && _p) {
  182. pos--;
  183. _p = rb_next(_p);
  184. }
  185. return _p;
  186. }
  187. static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos)
  188. {
  189. (*_pos)++;
  190. return rb_next((struct rb_node *) v);
  191. }
  192. static void proc_key_users_stop(struct seq_file *p, void *v)
  193. {
  194. spin_unlock(&key_user_lock);
  195. }
  196. static int proc_key_users_show(struct seq_file *m, void *v)
  197. {
  198. struct rb_node *_p = v;
  199. struct key_user *user = rb_entry(_p, struct key_user, node);
  200. unsigned maxkeys = (user->uid == 0) ?
  201. key_quota_root_maxkeys : key_quota_maxkeys;
  202. unsigned maxbytes = (user->uid == 0) ?
  203. key_quota_root_maxbytes : key_quota_maxbytes;
  204. seq_printf(m, "%5u: %5d %d/%d %d/%d %d/%d\n",
  205. user->uid,
  206. atomic_read(&user->usage),
  207. atomic_read(&user->nkeys),
  208. atomic_read(&user->nikeys),
  209. user->qnkeys,
  210. maxkeys,
  211. user->qnbytes,
  212. maxbytes);
  213. return 0;
  214. }