proc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 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 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 = create_proc_entry("keys", 0, NULL);
  65. if (!p)
  66. panic("Cannot create /proc/keys\n");
  67. p->proc_fops = &proc_keys_fops;
  68. #endif
  69. p = create_proc_entry("key-users", 0, NULL);
  70. if (!p)
  71. panic("Cannot create /proc/key-users\n");
  72. p->proc_fops = &proc_key_users_fops;
  73. return 0;
  74. } /* end key_proc_init() */
  75. __initcall(key_proc_init);
  76. /*****************************************************************************/
  77. /*
  78. * implement "/proc/keys" to provides a list of the keys on the system
  79. */
  80. #ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
  81. static int proc_keys_open(struct inode *inode, struct file *file)
  82. {
  83. return seq_open(file, &proc_keys_ops);
  84. }
  85. static void *proc_keys_start(struct seq_file *p, loff_t *_pos)
  86. {
  87. struct rb_node *_p;
  88. loff_t pos = *_pos;
  89. spin_lock(&key_serial_lock);
  90. _p = rb_first(&key_serial_tree);
  91. while (pos > 0 && _p) {
  92. pos--;
  93. _p = rb_next(_p);
  94. }
  95. return _p;
  96. }
  97. static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos)
  98. {
  99. (*_pos)++;
  100. return rb_next((struct rb_node *) v);
  101. }
  102. static void proc_keys_stop(struct seq_file *p, void *v)
  103. {
  104. spin_unlock(&key_serial_lock);
  105. }
  106. static int proc_keys_show(struct seq_file *m, void *v)
  107. {
  108. struct rb_node *_p = v;
  109. struct key *key = rb_entry(_p, struct key, serial_node);
  110. struct timespec now;
  111. unsigned long timo;
  112. char xbuf[12];
  113. int rc;
  114. /* check whether the current task is allowed to view the key (assuming
  115. * non-possession) */
  116. rc = key_task_permission(make_key_ref(key, 0), current, KEY_VIEW);
  117. if (rc < 0)
  118. return 0;
  119. now = current_kernel_time();
  120. rcu_read_lock();
  121. /* come up with a suitable timeout value */
  122. if (key->expiry == 0) {
  123. memcpy(xbuf, "perm", 5);
  124. }
  125. else if (now.tv_sec >= key->expiry) {
  126. memcpy(xbuf, "expd", 5);
  127. }
  128. else {
  129. timo = key->expiry - now.tv_sec;
  130. if (timo < 60)
  131. sprintf(xbuf, "%lus", timo);
  132. else if (timo < 60*60)
  133. sprintf(xbuf, "%lum", timo / 60);
  134. else if (timo < 60*60*24)
  135. sprintf(xbuf, "%luh", timo / (60*60));
  136. else if (timo < 60*60*24*7)
  137. sprintf(xbuf, "%lud", timo / (60*60*24));
  138. else
  139. sprintf(xbuf, "%luw", timo / (60*60*24*7));
  140. }
  141. #define showflag(KEY, LETTER, FLAG) \
  142. (test_bit(FLAG, &(KEY)->flags) ? LETTER : '-')
  143. seq_printf(m, "%08x %c%c%c%c%c%c %5d %4s %08x %5d %5d %-9.9s ",
  144. key->serial,
  145. showflag(key, 'I', KEY_FLAG_INSTANTIATED),
  146. showflag(key, 'R', KEY_FLAG_REVOKED),
  147. showflag(key, 'D', KEY_FLAG_DEAD),
  148. showflag(key, 'Q', KEY_FLAG_IN_QUOTA),
  149. showflag(key, 'U', KEY_FLAG_USER_CONSTRUCT),
  150. showflag(key, 'N', KEY_FLAG_NEGATIVE),
  151. atomic_read(&key->usage),
  152. xbuf,
  153. key->perm,
  154. key->uid,
  155. key->gid,
  156. key->type->name);
  157. #undef showflag
  158. if (key->type->describe)
  159. key->type->describe(key, m);
  160. seq_putc(m, '\n');
  161. rcu_read_unlock();
  162. return 0;
  163. }
  164. #endif /* CONFIG_KEYS_DEBUG_PROC_KEYS */
  165. /*****************************************************************************/
  166. /*
  167. * implement "/proc/key-users" to provides a list of the key users
  168. */
  169. static int proc_key_users_open(struct inode *inode, struct file *file)
  170. {
  171. return seq_open(file, &proc_key_users_ops);
  172. }
  173. static void *proc_key_users_start(struct seq_file *p, loff_t *_pos)
  174. {
  175. struct rb_node *_p;
  176. loff_t pos = *_pos;
  177. spin_lock(&key_user_lock);
  178. _p = rb_first(&key_user_tree);
  179. while (pos > 0 && _p) {
  180. pos--;
  181. _p = rb_next(_p);
  182. }
  183. return _p;
  184. }
  185. static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos)
  186. {
  187. (*_pos)++;
  188. return rb_next((struct rb_node *) v);
  189. }
  190. static void proc_key_users_stop(struct seq_file *p, void *v)
  191. {
  192. spin_unlock(&key_user_lock);
  193. }
  194. static int proc_key_users_show(struct seq_file *m, void *v)
  195. {
  196. struct rb_node *_p = v;
  197. struct key_user *user = rb_entry(_p, struct key_user, node);
  198. seq_printf(m, "%5u: %5d %d/%d %d/%d %d/%d\n",
  199. user->uid,
  200. atomic_read(&user->usage),
  201. atomic_read(&user->nkeys),
  202. atomic_read(&user->nikeys),
  203. user->qnkeys,
  204. KEYQUOTA_MAX_KEYS,
  205. user->qnbytes,
  206. KEYQUOTA_MAX_BYTES
  207. );
  208. return 0;
  209. }