proc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. } else if (now.tv_sec >= key->expiry) {
  180. memcpy(xbuf, "expd", 5);
  181. } else {
  182. timo = key->expiry - now.tv_sec;
  183. if (timo < 60)
  184. sprintf(xbuf, "%lus", timo);
  185. else if (timo < 60*60)
  186. sprintf(xbuf, "%lum", timo / 60);
  187. else if (timo < 60*60*24)
  188. sprintf(xbuf, "%luh", timo / (60*60));
  189. else if (timo < 60*60*24*7)
  190. sprintf(xbuf, "%lud", timo / (60*60*24));
  191. else
  192. sprintf(xbuf, "%luw", timo / (60*60*24*7));
  193. }
  194. #define showflag(KEY, LETTER, FLAG) \
  195. (test_bit(FLAG, &(KEY)->flags) ? LETTER : '-')
  196. seq_printf(m, "%08x %c%c%c%c%c%c %5d %4s %08x %5d %5d %-9.9s ",
  197. key->serial,
  198. showflag(key, 'I', KEY_FLAG_INSTANTIATED),
  199. showflag(key, 'R', KEY_FLAG_REVOKED),
  200. showflag(key, 'D', KEY_FLAG_DEAD),
  201. showflag(key, 'Q', KEY_FLAG_IN_QUOTA),
  202. showflag(key, 'U', KEY_FLAG_USER_CONSTRUCT),
  203. showflag(key, 'N', KEY_FLAG_NEGATIVE),
  204. atomic_read(&key->usage),
  205. xbuf,
  206. key->perm,
  207. key->uid,
  208. key->gid,
  209. key->type->name);
  210. #undef showflag
  211. if (key->type->describe)
  212. key->type->describe(key, m);
  213. seq_putc(m, '\n');
  214. rcu_read_unlock();
  215. return 0;
  216. }
  217. #endif /* CONFIG_KEYS_DEBUG_PROC_KEYS */
  218. static struct rb_node *__key_user_next(struct rb_node *n)
  219. {
  220. while (n) {
  221. struct key_user *user = rb_entry(n, struct key_user, node);
  222. if (user->user_ns == current_user_ns())
  223. break;
  224. n = rb_next(n);
  225. }
  226. return n;
  227. }
  228. static struct rb_node *key_user_next(struct rb_node *n)
  229. {
  230. return __key_user_next(rb_next(n));
  231. }
  232. static struct rb_node *key_user_first(struct rb_root *r)
  233. {
  234. struct rb_node *n = rb_first(r);
  235. return __key_user_next(n);
  236. }
  237. /*****************************************************************************/
  238. /*
  239. * implement "/proc/key-users" to provides a list of the key users
  240. */
  241. static int proc_key_users_open(struct inode *inode, struct file *file)
  242. {
  243. return seq_open(file, &proc_key_users_ops);
  244. }
  245. static void *proc_key_users_start(struct seq_file *p, loff_t *_pos)
  246. __acquires(key_user_lock)
  247. {
  248. struct rb_node *_p;
  249. loff_t pos = *_pos;
  250. spin_lock(&key_user_lock);
  251. _p = key_user_first(&key_user_tree);
  252. while (pos > 0 && _p) {
  253. pos--;
  254. _p = key_user_next(_p);
  255. }
  256. return _p;
  257. }
  258. static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos)
  259. {
  260. (*_pos)++;
  261. return key_user_next((struct rb_node *) v);
  262. }
  263. static void proc_key_users_stop(struct seq_file *p, void *v)
  264. __releases(key_user_lock)
  265. {
  266. spin_unlock(&key_user_lock);
  267. }
  268. static int proc_key_users_show(struct seq_file *m, void *v)
  269. {
  270. struct rb_node *_p = v;
  271. struct key_user *user = rb_entry(_p, struct key_user, node);
  272. unsigned maxkeys = (user->uid == 0) ?
  273. key_quota_root_maxkeys : key_quota_maxkeys;
  274. unsigned maxbytes = (user->uid == 0) ?
  275. key_quota_root_maxbytes : key_quota_maxbytes;
  276. seq_printf(m, "%5u: %5d %d/%d %d/%d %d/%d\n",
  277. user->uid,
  278. atomic_read(&user->usage),
  279. atomic_read(&user->nkeys),
  280. atomic_read(&user->nikeys),
  281. user->qnkeys,
  282. maxkeys,
  283. user->qnbytes,
  284. maxbytes);
  285. return 0;
  286. }