Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* procfs files for key database enumeration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Written by David Howells (dhowells@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <asm/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) static void *proc_keys_start(struct seq_file *p, loff_t *_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static void proc_keys_stop(struct seq_file *p, void *v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static int proc_keys_show(struct seq_file *m, void *v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static const struct seq_operations proc_keys_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	.start	= proc_keys_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	.next	= proc_keys_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	.stop	= proc_keys_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	.show	= proc_keys_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static void *proc_key_users_start(struct seq_file *p, loff_t *_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static void proc_key_users_stop(struct seq_file *p, void *v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static int proc_key_users_show(struct seq_file *m, void *v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static const struct seq_operations proc_key_users_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	.start	= proc_key_users_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	.next	= proc_key_users_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	.stop	= proc_key_users_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	.show	= proc_key_users_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * Declare the /proc files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static int __init key_proc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct proc_dir_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	p = proc_create_seq("keys", 0, NULL, &proc_keys_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		panic("Cannot create /proc/keys\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	p = proc_create_seq("key-users", 0, NULL, &proc_key_users_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		panic("Cannot create /proc/key-users\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) __initcall(key_proc_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * Implement "/proc/keys" to provide a list of the keys on the system that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  * grant View permission to the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static struct rb_node *key_serial_next(struct seq_file *p, struct rb_node *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct user_namespace *user_ns = seq_user_ns(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	n = rb_next(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	while (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		struct key *key = rb_entry(n, struct key, serial_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		if (kuid_has_mapping(user_ns, key->user->uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		n = rb_next(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static struct key *find_ge_key(struct seq_file *p, key_serial_t id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	struct user_namespace *user_ns = seq_user_ns(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct rb_node *n = key_serial_tree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	struct key *minkey = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	while (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		struct key *key = rb_entry(n, struct key, serial_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		if (id < key->serial) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			if (!minkey || minkey->serial > key->serial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 				minkey = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			n = n->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		} else if (id > key->serial) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			n = n->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			minkey = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (!minkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		if (kuid_has_mapping(user_ns, minkey->user->uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			return minkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		n = rb_next(&minkey->serial_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		minkey = rb_entry(n, struct key, serial_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static void *proc_keys_start(struct seq_file *p, loff_t *_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	__acquires(key_serial_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	key_serial_t pos = *_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	spin_lock(&key_serial_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (*_pos > INT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	key = find_ge_key(p, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (!key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	*_pos = key->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	return &key->serial_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static inline key_serial_t key_node_serial(struct rb_node *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct key *key = rb_entry(n, struct key, serial_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	return key->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static void *proc_keys_next(struct seq_file *p, void *v, loff_t *_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct rb_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	n = key_serial_next(p, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		*_pos = key_node_serial(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		(*_pos)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static void proc_keys_stop(struct seq_file *p, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	__releases(key_serial_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	spin_unlock(&key_serial_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static int proc_keys_show(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	struct rb_node *_p = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	struct key *key = rb_entry(_p, struct key, serial_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	key_ref_t key_ref, skey_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	time64_t now, expiry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	char xbuf[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	short state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	u64 timo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	struct keyring_search_context ctx = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		.index_key		= key->index_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		.cred			= m->file->f_cred,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		.match_data.cmp		= lookup_user_key_possessed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		.match_data.raw_data	= key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		.match_data.lookup_type	= KEYRING_SEARCH_LOOKUP_DIRECT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		.flags			= (KEYRING_SEARCH_NO_STATE_CHECK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 					   KEYRING_SEARCH_RECURSE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	key_ref = make_key_ref(key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	/* determine if the key is possessed by this process (a test we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	 * skip if the key does not indicate the possessor can view it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	if (key->perm & KEY_POS_VIEW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		skey_ref = search_cred_keyrings_rcu(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		if (!IS_ERR(skey_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			key_ref_put(skey_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			key_ref = make_key_ref(key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	/* check whether the current task is allowed to view the key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	rc = key_task_permission(key_ref, ctx.cred, KEY_NEED_VIEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	now = ktime_get_real_seconds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	/* come up with a suitable timeout value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	expiry = READ_ONCE(key->expiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (expiry == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		memcpy(xbuf, "perm", 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	} else if (now >= expiry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		memcpy(xbuf, "expd", 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		timo = expiry - now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		if (timo < 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			sprintf(xbuf, "%llus", timo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		else if (timo < 60*60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			sprintf(xbuf, "%llum", div_u64(timo, 60));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		else if (timo < 60*60*24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			sprintf(xbuf, "%lluh", div_u64(timo, 60 * 60));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		else if (timo < 60*60*24*7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			sprintf(xbuf, "%llud", div_u64(timo, 60 * 60 * 24));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			sprintf(xbuf, "%lluw", div_u64(timo, 60 * 60 * 24 * 7));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	state = key_read_state(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) #define showflag(FLAGS, LETTER, FLAG) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	((FLAGS & (1 << FLAG)) ? LETTER : '-')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	flags = READ_ONCE(key->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	seq_printf(m, "%08x %c%c%c%c%c%c%c %5d %4s %08x %5d %5d %-9.9s ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		   key->serial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		   state != KEY_IS_UNINSTANTIATED ? 'I' : '-',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		   showflag(flags, 'R', KEY_FLAG_REVOKED),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		   showflag(flags, 'D', KEY_FLAG_DEAD),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		   showflag(flags, 'Q', KEY_FLAG_IN_QUOTA),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		   showflag(flags, 'U', KEY_FLAG_USER_CONSTRUCT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		   state < 0 ? 'N' : '-',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		   showflag(flags, 'i', KEY_FLAG_INVALIDATED),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		   refcount_read(&key->usage),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		   xbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		   key->perm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		   from_kuid_munged(seq_user_ns(m), key->uid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		   from_kgid_munged(seq_user_ns(m), key->gid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		   key->type->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) #undef showflag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (key->type->describe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		key->type->describe(key, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	seq_putc(m, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static struct rb_node *__key_user_next(struct user_namespace *user_ns, struct rb_node *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	while (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		struct key_user *user = rb_entry(n, struct key_user, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		if (kuid_has_mapping(user_ns, user->uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		n = rb_next(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static struct rb_node *key_user_next(struct user_namespace *user_ns, struct rb_node *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	return __key_user_next(user_ns, rb_next(n));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static struct rb_node *key_user_first(struct user_namespace *user_ns, struct rb_root *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	struct rb_node *n = rb_first(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	return __key_user_next(user_ns, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static void *proc_key_users_start(struct seq_file *p, loff_t *_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	__acquires(key_user_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	struct rb_node *_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	loff_t pos = *_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	spin_lock(&key_user_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	_p = key_user_first(seq_user_ns(p), &key_user_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	while (pos > 0 && _p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		pos--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		_p = key_user_next(seq_user_ns(p), _p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	return _p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static void *proc_key_users_next(struct seq_file *p, void *v, loff_t *_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	(*_pos)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	return key_user_next(seq_user_ns(p), (struct rb_node *)v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static void proc_key_users_stop(struct seq_file *p, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	__releases(key_user_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	spin_unlock(&key_user_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static int proc_key_users_show(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	struct rb_node *_p = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	struct key_user *user = rb_entry(_p, struct key_user, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	unsigned maxkeys = uid_eq(user->uid, GLOBAL_ROOT_UID) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		key_quota_root_maxkeys : key_quota_maxkeys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	unsigned maxbytes = uid_eq(user->uid, GLOBAL_ROOT_UID) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		key_quota_root_maxbytes : key_quota_maxbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	seq_printf(m, "%5u: %5d %d/%d %d/%d %d/%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		   from_kuid_munged(seq_user_ns(m), user->uid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		   refcount_read(&user->usage),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		   atomic_read(&user->nkeys),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		   atomic_read(&user->nikeys),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		   user->qnkeys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		   maxkeys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		   user->qnbytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		   maxbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }