proc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 struct rb_node *__key_serial_next(struct rb_node *n)
  80. {
  81. while (n) {
  82. struct key *key = rb_entry(n, struct key, serial_node);
  83. if (key->user->user_ns == current_user_ns())
  84. break;
  85. n = rb_next(n);
  86. }
  87. return n;
  88. }
  89. static struct rb_node *key_serial_next(struct rb_node *n)
  90. {
  91. return __key_serial_next(rb_next(n));
  92. }
  93. static struct rb_node *key_serial_first(struct rb_root *r)
  94. {
  95. struct rb_node *n = rb_first(r);
  96. return __key_serial_next(n);
  97. }
  98. static int proc_keys_open(struct inode *inode, struct file *file)
  99. {
  100. return seq_open(file, &proc_keys_ops);
  101. }
  102. static void *proc_keys_start(struct seq_file *p, loff_t *_pos)
  103. {
  104. struct rb_node *_p;
  105. loff_t pos = *_pos;
  106. spin_lock(&key_serial_lock);
  107. _p = key_serial_first(&key_serial_tree);
  108. while (pos > 0 && _p) {
  109. pos--;
  110. _p = key_serial_next(_p);
  111. }
  112. return _p;
  113. }
  114. static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos)
  115. {
  116. (*_pos)++;
  117. return key_serial_next((struct rb_node *) v);
  118. }
  119. static void proc_keys_stop(struct seq_file *p, void *v)
  120. {
  121. spin_unlock(&key_serial_lock);
  122. }
  123. static int proc_keys_show(struct seq_file *m, void *v)
  124. {
  125. struct rb_node *_p = v;
  126. struct key *key = rb_entry(_p, struct key, serial_node);
  127. struct timespec now;
  128. unsigned long timo;
  129. char xbuf[12];
  130. int rc;
  131. /* check whether the current task is allowed to view the key (assuming
  132. * non-possession)
  133. * - the caller holds a spinlock, and thus the RCU read lock, making our
  134. * access to __current_cred() safe
  135. */
  136. rc = key_task_permission(make_key_ref(key, 0), current_cred(),
  137. KEY_VIEW);
  138. if (rc < 0)
  139. return 0;
  140. now = current_kernel_time();
  141. rcu_read_lock();
  142. /* come up with a suitable timeout value */
  143. if (key->expiry == 0) {
  144. memcpy(xbuf, "perm", 5);
  145. }
  146. else if (now.tv_sec >= key->expiry) {
  147. memcpy(xbuf, "expd", 5);
  148. }
  149. else {
  150. timo = key->expiry - now.tv_sec;
  151. if (timo < 60)
  152. sprintf(xbuf, "%lus", timo);
  153. else if (timo < 60*60)
  154. sprintf(xbuf, "%lum", timo / 60);
  155. else if (timo < 60*60*24)
  156. sprintf(xbuf, "%luh", timo / (60*60));
  157. else if (timo < 60*60*24*7)
  158. sprintf(xbuf, "%lud", timo / (60*60*24));
  159. else
  160. sprintf(xbuf, "%luw", timo / (60*60*24*7));
  161. }
  162. #define showflag(KEY, LETTER, FLAG) \
  163. (test_bit(FLAG, &(KEY)->flags) ? LETTER : '-')
  164. seq_printf(m, "%08x %c%c%c%c%c%c %5d %4s %08x %5d %5d %-9.9s ",
  165. key->serial,
  166. showflag(key, 'I', KEY_FLAG_INSTANTIATED),
  167. showflag(key, 'R', KEY_FLAG_REVOKED),
  168. showflag(key, 'D', KEY_FLAG_DEAD),
  169. showflag(key, 'Q', KEY_FLAG_IN_QUOTA),
  170. showflag(key, 'U', KEY_FLAG_USER_CONSTRUCT),
  171. showflag(key, 'N', KEY_FLAG_NEGATIVE),
  172. atomic_read(&key->usage),
  173. xbuf,
  174. key->perm,
  175. key->uid,
  176. key->gid,
  177. key->type->name);
  178. #undef showflag
  179. if (key->type->describe)
  180. key->type->describe(key, m);
  181. seq_putc(m, '\n');
  182. rcu_read_unlock();
  183. return 0;
  184. }
  185. #endif /* CONFIG_KEYS_DEBUG_PROC_KEYS */
  186. static struct rb_node *__key_user_next(struct rb_node *n)
  187. {
  188. while (n) {
  189. struct key_user *user = rb_entry(n, struct key_user, node);
  190. if (user->user_ns == current_user_ns())
  191. break;
  192. n = rb_next(n);
  193. }
  194. return n;
  195. }
  196. static struct rb_node *key_user_next(struct rb_node *n)
  197. {
  198. return __key_user_next(rb_next(n));
  199. }
  200. static struct rb_node *key_user_first(struct rb_root *r)
  201. {
  202. struct rb_node *n = rb_first(r);
  203. return __key_user_next(n);
  204. }
  205. /*****************************************************************************/
  206. /*
  207. * implement "/proc/key-users" to provides a list of the key users
  208. */
  209. static int proc_key_users_open(struct inode *inode, struct file *file)
  210. {
  211. return seq_open(file, &proc_key_users_ops);
  212. }
  213. static void *proc_key_users_start(struct seq_file *p, loff_t *_pos)
  214. {
  215. struct rb_node *_p;
  216. loff_t pos = *_pos;
  217. spin_lock(&key_user_lock);
  218. _p = key_user_first(&key_user_tree);
  219. while (pos > 0 && _p) {
  220. pos--;
  221. _p = key_user_next(_p);
  222. }
  223. return _p;
  224. }
  225. static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos)
  226. {
  227. (*_pos)++;
  228. return key_user_next((struct rb_node *) v);
  229. }
  230. static void proc_key_users_stop(struct seq_file *p, void *v)
  231. {
  232. spin_unlock(&key_user_lock);
  233. }
  234. static int proc_key_users_show(struct seq_file *m, void *v)
  235. {
  236. struct rb_node *_p = v;
  237. struct key_user *user = rb_entry(_p, struct key_user, node);
  238. unsigned maxkeys = (user->uid == 0) ?
  239. key_quota_root_maxkeys : key_quota_maxkeys;
  240. unsigned maxbytes = (user->uid == 0) ?
  241. key_quota_root_maxbytes : key_quota_maxbytes;
  242. seq_printf(m, "%5u: %5d %d/%d %d/%d %d/%d\n",
  243. user->uid,
  244. atomic_read(&user->usage),
  245. atomic_read(&user->nkeys),
  246. atomic_read(&user->nikeys),
  247. user->qnkeys,
  248. maxkeys,
  249. user->qnbytes,
  250. maxbytes);
  251. return 0;
  252. }