proc.c 8.8 KB

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