proc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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/fs.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/seq_file.h>
  17. #include <asm/errno.h>
  18. #include "internal.h"
  19. #ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
  20. static int proc_keys_open(struct inode *inode, struct file *file);
  21. static void *proc_keys_start(struct seq_file *p, loff_t *_pos);
  22. static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos);
  23. static void proc_keys_stop(struct seq_file *p, void *v);
  24. static int proc_keys_show(struct seq_file *m, void *v);
  25. static const struct seq_operations proc_keys_ops = {
  26. .start = proc_keys_start,
  27. .next = proc_keys_next,
  28. .stop = proc_keys_stop,
  29. .show = proc_keys_show,
  30. };
  31. static const struct file_operations proc_keys_fops = {
  32. .open = proc_keys_open,
  33. .read = seq_read,
  34. .llseek = seq_lseek,
  35. .release = seq_release,
  36. };
  37. #endif
  38. static int proc_key_users_open(struct inode *inode, struct file *file);
  39. static void *proc_key_users_start(struct seq_file *p, loff_t *_pos);
  40. static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos);
  41. static void proc_key_users_stop(struct seq_file *p, void *v);
  42. static int proc_key_users_show(struct seq_file *m, void *v);
  43. static const struct seq_operations proc_key_users_ops = {
  44. .start = proc_key_users_start,
  45. .next = proc_key_users_next,
  46. .stop = proc_key_users_stop,
  47. .show = proc_key_users_show,
  48. };
  49. static const struct file_operations proc_key_users_fops = {
  50. .open = proc_key_users_open,
  51. .read = seq_read,
  52. .llseek = seq_lseek,
  53. .release = seq_release,
  54. };
  55. /*
  56. * declare the /proc files
  57. */
  58. static int __init key_proc_init(void)
  59. {
  60. struct proc_dir_entry *p;
  61. #ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
  62. p = proc_create("keys", 0, NULL, &proc_keys_fops);
  63. if (!p)
  64. panic("Cannot create /proc/keys\n");
  65. #endif
  66. p = proc_create("key-users", 0, NULL, &proc_key_users_fops);
  67. if (!p)
  68. panic("Cannot create /proc/key-users\n");
  69. return 0;
  70. }
  71. __initcall(key_proc_init);
  72. /*
  73. * implement "/proc/keys" to provides a list of the keys on the system
  74. */
  75. #ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
  76. static struct rb_node *key_serial_next(struct rb_node *n)
  77. {
  78. struct user_namespace *user_ns = current_user_ns();
  79. n = rb_next(n);
  80. while (n) {
  81. struct key *key = rb_entry(n, struct key, serial_node);
  82. if (key->user->user_ns == user_ns)
  83. break;
  84. n = rb_next(n);
  85. }
  86. return n;
  87. }
  88. static int proc_keys_open(struct inode *inode, struct file *file)
  89. {
  90. return seq_open(file, &proc_keys_ops);
  91. }
  92. static struct key *find_ge_key(key_serial_t id)
  93. {
  94. struct user_namespace *user_ns = current_user_ns();
  95. struct rb_node *n = key_serial_tree.rb_node;
  96. struct key *minkey = NULL;
  97. while (n) {
  98. struct key *key = rb_entry(n, struct key, serial_node);
  99. if (id < key->serial) {
  100. if (!minkey || minkey->serial > key->serial)
  101. minkey = key;
  102. n = n->rb_left;
  103. } else if (id > key->serial) {
  104. n = n->rb_right;
  105. } else {
  106. minkey = key;
  107. break;
  108. }
  109. key = NULL;
  110. }
  111. if (!minkey)
  112. return NULL;
  113. for (;;) {
  114. if (minkey->user->user_ns == user_ns)
  115. return minkey;
  116. n = rb_next(&minkey->serial_node);
  117. if (!n)
  118. return NULL;
  119. minkey = rb_entry(n, struct key, serial_node);
  120. }
  121. }
  122. static void *proc_keys_start(struct seq_file *p, loff_t *_pos)
  123. __acquires(key_serial_lock)
  124. {
  125. key_serial_t pos = *_pos;
  126. struct key *key;
  127. spin_lock(&key_serial_lock);
  128. if (*_pos > INT_MAX)
  129. return NULL;
  130. key = find_ge_key(pos);
  131. if (!key)
  132. return NULL;
  133. *_pos = key->serial;
  134. return &key->serial_node;
  135. }
  136. static inline key_serial_t key_node_serial(struct rb_node *n)
  137. {
  138. struct key *key = rb_entry(n, struct key, serial_node);
  139. return key->serial;
  140. }
  141. static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos)
  142. {
  143. struct rb_node *n;
  144. n = key_serial_next(v);
  145. if (n)
  146. *_pos = key_node_serial(n);
  147. return n;
  148. }
  149. static void proc_keys_stop(struct seq_file *p, void *v)
  150. __releases(key_serial_lock)
  151. {
  152. spin_unlock(&key_serial_lock);
  153. }
  154. static int proc_keys_show(struct seq_file *m, void *v)
  155. {
  156. const struct cred *cred = current_cred();
  157. struct rb_node *_p = v;
  158. struct key *key = rb_entry(_p, struct key, serial_node);
  159. struct timespec now;
  160. unsigned long timo;
  161. key_ref_t key_ref, skey_ref;
  162. char xbuf[12];
  163. int rc;
  164. key_ref = make_key_ref(key, 0);
  165. /* determine if the key is possessed by this process (a test we can
  166. * skip if the key does not indicate the possessor can view it
  167. */
  168. if (key->perm & KEY_POS_VIEW) {
  169. skey_ref = search_my_process_keyrings(key->type, key,
  170. lookup_user_key_possessed,
  171. cred);
  172. if (!IS_ERR(skey_ref)) {
  173. key_ref_put(skey_ref);
  174. key_ref = make_key_ref(key, 1);
  175. }
  176. }
  177. /* check whether the current task is allowed to view the key (assuming
  178. * non-possession)
  179. * - the caller holds a spinlock, and thus the RCU read lock, making our
  180. * access to __current_cred() safe
  181. */
  182. rc = key_task_permission(key_ref, cred, KEY_VIEW);
  183. if (rc < 0)
  184. return 0;
  185. now = current_kernel_time();
  186. rcu_read_lock();
  187. /* come up with a suitable timeout value */
  188. if (key->expiry == 0) {
  189. memcpy(xbuf, "perm", 5);
  190. } else if (now.tv_sec >= key->expiry) {
  191. memcpy(xbuf, "expd", 5);
  192. } else {
  193. timo = key->expiry - now.tv_sec;
  194. if (timo < 60)
  195. sprintf(xbuf, "%lus", timo);
  196. else if (timo < 60*60)
  197. sprintf(xbuf, "%lum", timo / 60);
  198. else if (timo < 60*60*24)
  199. sprintf(xbuf, "%luh", timo / (60*60));
  200. else if (timo < 60*60*24*7)
  201. sprintf(xbuf, "%lud", timo / (60*60*24));
  202. else
  203. sprintf(xbuf, "%luw", timo / (60*60*24*7));
  204. }
  205. #define showflag(KEY, LETTER, FLAG) \
  206. (test_bit(FLAG, &(KEY)->flags) ? LETTER : '-')
  207. seq_printf(m, "%08x %c%c%c%c%c%c %5d %4s %08x %5d %5d %-9.9s ",
  208. key->serial,
  209. showflag(key, 'I', KEY_FLAG_INSTANTIATED),
  210. showflag(key, 'R', KEY_FLAG_REVOKED),
  211. showflag(key, 'D', KEY_FLAG_DEAD),
  212. showflag(key, 'Q', KEY_FLAG_IN_QUOTA),
  213. showflag(key, 'U', KEY_FLAG_USER_CONSTRUCT),
  214. showflag(key, 'N', KEY_FLAG_NEGATIVE),
  215. atomic_read(&key->usage),
  216. xbuf,
  217. key->perm,
  218. key->uid,
  219. key->gid,
  220. key->type->name);
  221. #undef showflag
  222. if (key->type->describe)
  223. key->type->describe(key, m);
  224. seq_putc(m, '\n');
  225. rcu_read_unlock();
  226. return 0;
  227. }
  228. #endif /* CONFIG_KEYS_DEBUG_PROC_KEYS */
  229. static struct rb_node *__key_user_next(struct rb_node *n)
  230. {
  231. while (n) {
  232. struct key_user *user = rb_entry(n, struct key_user, node);
  233. if (user->user_ns == current_user_ns())
  234. break;
  235. n = rb_next(n);
  236. }
  237. return n;
  238. }
  239. static struct rb_node *key_user_next(struct rb_node *n)
  240. {
  241. return __key_user_next(rb_next(n));
  242. }
  243. static struct rb_node *key_user_first(struct rb_root *r)
  244. {
  245. struct rb_node *n = rb_first(r);
  246. return __key_user_next(n);
  247. }
  248. /*
  249. * implement "/proc/key-users" to provides a list of the key users
  250. */
  251. static int proc_key_users_open(struct inode *inode, struct file *file)
  252. {
  253. return seq_open(file, &proc_key_users_ops);
  254. }
  255. static void *proc_key_users_start(struct seq_file *p, loff_t *_pos)
  256. __acquires(key_user_lock)
  257. {
  258. struct rb_node *_p;
  259. loff_t pos = *_pos;
  260. spin_lock(&key_user_lock);
  261. _p = key_user_first(&key_user_tree);
  262. while (pos > 0 && _p) {
  263. pos--;
  264. _p = key_user_next(_p);
  265. }
  266. return _p;
  267. }
  268. static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos)
  269. {
  270. (*_pos)++;
  271. return key_user_next((struct rb_node *)v);
  272. }
  273. static void proc_key_users_stop(struct seq_file *p, void *v)
  274. __releases(key_user_lock)
  275. {
  276. spin_unlock(&key_user_lock);
  277. }
  278. static int proc_key_users_show(struct seq_file *m, void *v)
  279. {
  280. struct rb_node *_p = v;
  281. struct key_user *user = rb_entry(_p, struct key_user, node);
  282. unsigned maxkeys = (user->uid == 0) ?
  283. key_quota_root_maxkeys : key_quota_maxkeys;
  284. unsigned maxbytes = (user->uid == 0) ?
  285. key_quota_root_maxbytes : key_quota_maxbytes;
  286. seq_printf(m, "%5u: %5d %d/%d %d/%d %d/%d\n",
  287. user->uid,
  288. atomic_read(&user->usage),
  289. atomic_read(&user->nkeys),
  290. atomic_read(&user->nikeys),
  291. user->qnkeys,
  292. maxkeys,
  293. user->qnbytes,
  294. maxbytes);
  295. return 0;
  296. }