proc.c 8.7 KB

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