proc.c 8.0 KB

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