0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/init.h>
0009 #include <linux/sched.h>
0010 #include <linux/fs.h>
0011 #include <linux/proc_fs.h>
0012 #include <linux/seq_file.h>
0013 #include <asm/errno.h>
0014 #include "internal.h"
0015
0016 static void *proc_keys_start(struct seq_file *p, loff_t *_pos);
0017 static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos);
0018 static void proc_keys_stop(struct seq_file *p, void *v);
0019 static int proc_keys_show(struct seq_file *m, void *v);
0020
0021 static const struct seq_operations proc_keys_ops = {
0022 .start = proc_keys_start,
0023 .next = proc_keys_next,
0024 .stop = proc_keys_stop,
0025 .show = proc_keys_show,
0026 };
0027
0028 static void *proc_key_users_start(struct seq_file *p, loff_t *_pos);
0029 static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos);
0030 static void proc_key_users_stop(struct seq_file *p, void *v);
0031 static int proc_key_users_show(struct seq_file *m, void *v);
0032
0033 static const struct seq_operations proc_key_users_ops = {
0034 .start = proc_key_users_start,
0035 .next = proc_key_users_next,
0036 .stop = proc_key_users_stop,
0037 .show = proc_key_users_show,
0038 };
0039
0040
0041
0042
0043 static int __init key_proc_init(void)
0044 {
0045 struct proc_dir_entry *p;
0046
0047 p = proc_create_seq("keys", 0, NULL, &proc_keys_ops);
0048 if (!p)
0049 panic("Cannot create /proc/keys\n");
0050
0051 p = proc_create_seq("key-users", 0, NULL, &proc_key_users_ops);
0052 if (!p)
0053 panic("Cannot create /proc/key-users\n");
0054
0055 return 0;
0056 }
0057
0058 __initcall(key_proc_init);
0059
0060
0061
0062
0063
0064 static struct rb_node *key_serial_next(struct seq_file *p, struct rb_node *n)
0065 {
0066 struct user_namespace *user_ns = seq_user_ns(p);
0067
0068 n = rb_next(n);
0069 while (n) {
0070 struct key *key = rb_entry(n, struct key, serial_node);
0071 if (kuid_has_mapping(user_ns, key->user->uid))
0072 break;
0073 n = rb_next(n);
0074 }
0075 return n;
0076 }
0077
0078 static struct key *find_ge_key(struct seq_file *p, key_serial_t id)
0079 {
0080 struct user_namespace *user_ns = seq_user_ns(p);
0081 struct rb_node *n = key_serial_tree.rb_node;
0082 struct key *minkey = NULL;
0083
0084 while (n) {
0085 struct key *key = rb_entry(n, struct key, serial_node);
0086 if (id < key->serial) {
0087 if (!minkey || minkey->serial > key->serial)
0088 minkey = key;
0089 n = n->rb_left;
0090 } else if (id > key->serial) {
0091 n = n->rb_right;
0092 } else {
0093 minkey = key;
0094 break;
0095 }
0096 key = NULL;
0097 }
0098
0099 if (!minkey)
0100 return NULL;
0101
0102 for (;;) {
0103 if (kuid_has_mapping(user_ns, minkey->user->uid))
0104 return minkey;
0105 n = rb_next(&minkey->serial_node);
0106 if (!n)
0107 return NULL;
0108 minkey = rb_entry(n, struct key, serial_node);
0109 }
0110 }
0111
0112 static void *proc_keys_start(struct seq_file *p, loff_t *_pos)
0113 __acquires(key_serial_lock)
0114 {
0115 key_serial_t pos = *_pos;
0116 struct key *key;
0117
0118 spin_lock(&key_serial_lock);
0119
0120 if (*_pos > INT_MAX)
0121 return NULL;
0122 key = find_ge_key(p, pos);
0123 if (!key)
0124 return NULL;
0125 *_pos = key->serial;
0126 return &key->serial_node;
0127 }
0128
0129 static inline key_serial_t key_node_serial(struct rb_node *n)
0130 {
0131 struct key *key = rb_entry(n, struct key, serial_node);
0132 return key->serial;
0133 }
0134
0135 static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos)
0136 {
0137 struct rb_node *n;
0138
0139 n = key_serial_next(p, v);
0140 if (n)
0141 *_pos = key_node_serial(n);
0142 else
0143 (*_pos)++;
0144 return n;
0145 }
0146
0147 static void proc_keys_stop(struct seq_file *p, void *v)
0148 __releases(key_serial_lock)
0149 {
0150 spin_unlock(&key_serial_lock);
0151 }
0152
0153 static int proc_keys_show(struct seq_file *m, void *v)
0154 {
0155 struct rb_node *_p = v;
0156 struct key *key = rb_entry(_p, struct key, serial_node);
0157 unsigned long flags;
0158 key_ref_t key_ref, skey_ref;
0159 time64_t now, expiry;
0160 char xbuf[16];
0161 short state;
0162 u64 timo;
0163 int rc;
0164
0165 struct keyring_search_context ctx = {
0166 .index_key = key->index_key,
0167 .cred = m->file->f_cred,
0168 .match_data.cmp = lookup_user_key_possessed,
0169 .match_data.raw_data = key,
0170 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
0171 .flags = (KEYRING_SEARCH_NO_STATE_CHECK |
0172 KEYRING_SEARCH_RECURSE),
0173 };
0174
0175 key_ref = make_key_ref(key, 0);
0176
0177
0178
0179
0180 if (key->perm & KEY_POS_VIEW) {
0181 rcu_read_lock();
0182 skey_ref = search_cred_keyrings_rcu(&ctx);
0183 rcu_read_unlock();
0184 if (!IS_ERR(skey_ref)) {
0185 key_ref_put(skey_ref);
0186 key_ref = make_key_ref(key, 1);
0187 }
0188 }
0189
0190
0191 rc = key_task_permission(key_ref, ctx.cred, KEY_NEED_VIEW);
0192 if (rc < 0)
0193 return 0;
0194
0195 now = ktime_get_real_seconds();
0196
0197 rcu_read_lock();
0198
0199
0200 expiry = READ_ONCE(key->expiry);
0201 if (expiry == 0) {
0202 memcpy(xbuf, "perm", 5);
0203 } else if (now >= expiry) {
0204 memcpy(xbuf, "expd", 5);
0205 } else {
0206 timo = expiry - now;
0207
0208 if (timo < 60)
0209 sprintf(xbuf, "%llus", timo);
0210 else if (timo < 60*60)
0211 sprintf(xbuf, "%llum", div_u64(timo, 60));
0212 else if (timo < 60*60*24)
0213 sprintf(xbuf, "%lluh", div_u64(timo, 60 * 60));
0214 else if (timo < 60*60*24*7)
0215 sprintf(xbuf, "%llud", div_u64(timo, 60 * 60 * 24));
0216 else
0217 sprintf(xbuf, "%lluw", div_u64(timo, 60 * 60 * 24 * 7));
0218 }
0219
0220 state = key_read_state(key);
0221
0222 #define showflag(FLAGS, LETTER, FLAG) \
0223 ((FLAGS & (1 << FLAG)) ? LETTER : '-')
0224
0225 flags = READ_ONCE(key->flags);
0226 seq_printf(m, "%08x %c%c%c%c%c%c%c %5d %4s %08x %5d %5d %-9.9s ",
0227 key->serial,
0228 state != KEY_IS_UNINSTANTIATED ? 'I' : '-',
0229 showflag(flags, 'R', KEY_FLAG_REVOKED),
0230 showflag(flags, 'D', KEY_FLAG_DEAD),
0231 showflag(flags, 'Q', KEY_FLAG_IN_QUOTA),
0232 showflag(flags, 'U', KEY_FLAG_USER_CONSTRUCT),
0233 state < 0 ? 'N' : '-',
0234 showflag(flags, 'i', KEY_FLAG_INVALIDATED),
0235 refcount_read(&key->usage),
0236 xbuf,
0237 key->perm,
0238 from_kuid_munged(seq_user_ns(m), key->uid),
0239 from_kgid_munged(seq_user_ns(m), key->gid),
0240 key->type->name);
0241
0242 #undef showflag
0243
0244 if (key->type->describe)
0245 key->type->describe(key, m);
0246 seq_putc(m, '\n');
0247
0248 rcu_read_unlock();
0249 return 0;
0250 }
0251
0252 static struct rb_node *__key_user_next(struct user_namespace *user_ns, struct rb_node *n)
0253 {
0254 while (n) {
0255 struct key_user *user = rb_entry(n, struct key_user, node);
0256 if (kuid_has_mapping(user_ns, user->uid))
0257 break;
0258 n = rb_next(n);
0259 }
0260 return n;
0261 }
0262
0263 static struct rb_node *key_user_next(struct user_namespace *user_ns, struct rb_node *n)
0264 {
0265 return __key_user_next(user_ns, rb_next(n));
0266 }
0267
0268 static struct rb_node *key_user_first(struct user_namespace *user_ns, struct rb_root *r)
0269 {
0270 struct rb_node *n = rb_first(r);
0271 return __key_user_next(user_ns, n);
0272 }
0273
0274 static void *proc_key_users_start(struct seq_file *p, loff_t *_pos)
0275 __acquires(key_user_lock)
0276 {
0277 struct rb_node *_p;
0278 loff_t pos = *_pos;
0279
0280 spin_lock(&key_user_lock);
0281
0282 _p = key_user_first(seq_user_ns(p), &key_user_tree);
0283 while (pos > 0 && _p) {
0284 pos--;
0285 _p = key_user_next(seq_user_ns(p), _p);
0286 }
0287
0288 return _p;
0289 }
0290
0291 static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos)
0292 {
0293 (*_pos)++;
0294 return key_user_next(seq_user_ns(p), (struct rb_node *)v);
0295 }
0296
0297 static void proc_key_users_stop(struct seq_file *p, void *v)
0298 __releases(key_user_lock)
0299 {
0300 spin_unlock(&key_user_lock);
0301 }
0302
0303 static int proc_key_users_show(struct seq_file *m, void *v)
0304 {
0305 struct rb_node *_p = v;
0306 struct key_user *user = rb_entry(_p, struct key_user, node);
0307 unsigned maxkeys = uid_eq(user->uid, GLOBAL_ROOT_UID) ?
0308 key_quota_root_maxkeys : key_quota_maxkeys;
0309 unsigned maxbytes = uid_eq(user->uid, GLOBAL_ROOT_UID) ?
0310 key_quota_root_maxbytes : key_quota_maxbytes;
0311
0312 seq_printf(m, "%5u: %5d %d/%d %d/%d %d/%d\n",
0313 from_kuid_munged(seq_user_ns(m), user->uid),
0314 refcount_read(&user->usage),
0315 atomic_read(&user->nkeys),
0316 atomic_read(&user->nikeys),
0317 user->qnkeys,
0318 maxkeys,
0319 user->qnbytes,
0320 maxbytes);
0321
0322 return 0;
0323 }