xref: /OK3568_Linux_fs/kernel/security/keys/persistent.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* General persistent per-UID keyrings register
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
5*4882a593Smuzhiyun  * Written by David Howells (dhowells@redhat.com)
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/user_namespace.h>
9*4882a593Smuzhiyun #include <linux/cred.h>
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include "internal.h"
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun unsigned persistent_keyring_expiry = 3 * 24 * 3600; /* Expire after 3 days of non-use */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun  * Create the persistent keyring register for the current user namespace.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * Called with the namespace's sem locked for writing.
19*4882a593Smuzhiyun  */
key_create_persistent_register(struct user_namespace * ns)20*4882a593Smuzhiyun static int key_create_persistent_register(struct user_namespace *ns)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun 	struct key *reg = keyring_alloc(".persistent_register",
23*4882a593Smuzhiyun 					KUIDT_INIT(0), KGIDT_INIT(0),
24*4882a593Smuzhiyun 					current_cred(),
25*4882a593Smuzhiyun 					((KEY_POS_ALL & ~KEY_POS_SETATTR) |
26*4882a593Smuzhiyun 					 KEY_USR_VIEW | KEY_USR_READ),
27*4882a593Smuzhiyun 					KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
28*4882a593Smuzhiyun 	if (IS_ERR(reg))
29*4882a593Smuzhiyun 		return PTR_ERR(reg);
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 	ns->persistent_keyring_register = reg;
32*4882a593Smuzhiyun 	return 0;
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun  * Create the persistent keyring for the specified user.
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * Called with the namespace's sem locked for writing.
39*4882a593Smuzhiyun  */
key_create_persistent(struct user_namespace * ns,kuid_t uid,struct keyring_index_key * index_key)40*4882a593Smuzhiyun static key_ref_t key_create_persistent(struct user_namespace *ns, kuid_t uid,
41*4882a593Smuzhiyun 				       struct keyring_index_key *index_key)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	struct key *persistent;
44*4882a593Smuzhiyun 	key_ref_t reg_ref, persistent_ref;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	if (!ns->persistent_keyring_register) {
47*4882a593Smuzhiyun 		long err = key_create_persistent_register(ns);
48*4882a593Smuzhiyun 		if (err < 0)
49*4882a593Smuzhiyun 			return ERR_PTR(err);
50*4882a593Smuzhiyun 	} else {
51*4882a593Smuzhiyun 		reg_ref = make_key_ref(ns->persistent_keyring_register, true);
52*4882a593Smuzhiyun 		persistent_ref = find_key_to_update(reg_ref, index_key);
53*4882a593Smuzhiyun 		if (persistent_ref)
54*4882a593Smuzhiyun 			return persistent_ref;
55*4882a593Smuzhiyun 	}
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	persistent = keyring_alloc(index_key->description,
58*4882a593Smuzhiyun 				   uid, INVALID_GID, current_cred(),
59*4882a593Smuzhiyun 				   ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
60*4882a593Smuzhiyun 				    KEY_USR_VIEW | KEY_USR_READ),
61*4882a593Smuzhiyun 				   KEY_ALLOC_NOT_IN_QUOTA, NULL,
62*4882a593Smuzhiyun 				   ns->persistent_keyring_register);
63*4882a593Smuzhiyun 	if (IS_ERR(persistent))
64*4882a593Smuzhiyun 		return ERR_CAST(persistent);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	return make_key_ref(persistent, true);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun  * Get the persistent keyring for a specific UID and link it to the nominated
71*4882a593Smuzhiyun  * keyring.
72*4882a593Smuzhiyun  */
key_get_persistent(struct user_namespace * ns,kuid_t uid,key_ref_t dest_ref)73*4882a593Smuzhiyun static long key_get_persistent(struct user_namespace *ns, kuid_t uid,
74*4882a593Smuzhiyun 			       key_ref_t dest_ref)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	struct keyring_index_key index_key;
77*4882a593Smuzhiyun 	struct key *persistent;
78*4882a593Smuzhiyun 	key_ref_t reg_ref, persistent_ref;
79*4882a593Smuzhiyun 	char buf[32];
80*4882a593Smuzhiyun 	long ret;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	/* Look in the register if it exists */
83*4882a593Smuzhiyun 	memset(&index_key, 0, sizeof(index_key));
84*4882a593Smuzhiyun 	index_key.type = &key_type_keyring;
85*4882a593Smuzhiyun 	index_key.description = buf;
86*4882a593Smuzhiyun 	index_key.desc_len = sprintf(buf, "_persistent.%u", from_kuid(ns, uid));
87*4882a593Smuzhiyun 	key_set_index_key(&index_key);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	if (ns->persistent_keyring_register) {
90*4882a593Smuzhiyun 		reg_ref = make_key_ref(ns->persistent_keyring_register, true);
91*4882a593Smuzhiyun 		down_read(&ns->keyring_sem);
92*4882a593Smuzhiyun 		persistent_ref = find_key_to_update(reg_ref, &index_key);
93*4882a593Smuzhiyun 		up_read(&ns->keyring_sem);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 		if (persistent_ref)
96*4882a593Smuzhiyun 			goto found;
97*4882a593Smuzhiyun 	}
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	/* It wasn't in the register, so we'll need to create it.  We might
100*4882a593Smuzhiyun 	 * also need to create the register.
101*4882a593Smuzhiyun 	 */
102*4882a593Smuzhiyun 	down_write(&ns->keyring_sem);
103*4882a593Smuzhiyun 	persistent_ref = key_create_persistent(ns, uid, &index_key);
104*4882a593Smuzhiyun 	up_write(&ns->keyring_sem);
105*4882a593Smuzhiyun 	if (!IS_ERR(persistent_ref))
106*4882a593Smuzhiyun 		goto found;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	return PTR_ERR(persistent_ref);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun found:
111*4882a593Smuzhiyun 	ret = key_task_permission(persistent_ref, current_cred(), KEY_NEED_LINK);
112*4882a593Smuzhiyun 	if (ret == 0) {
113*4882a593Smuzhiyun 		persistent = key_ref_to_ptr(persistent_ref);
114*4882a593Smuzhiyun 		ret = key_link(key_ref_to_ptr(dest_ref), persistent);
115*4882a593Smuzhiyun 		if (ret == 0) {
116*4882a593Smuzhiyun 			key_set_timeout(persistent, persistent_keyring_expiry);
117*4882a593Smuzhiyun 			ret = persistent->serial;
118*4882a593Smuzhiyun 		}
119*4882a593Smuzhiyun 	}
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	key_ref_put(persistent_ref);
122*4882a593Smuzhiyun 	return ret;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /*
126*4882a593Smuzhiyun  * Get the persistent keyring for a specific UID and link it to the nominated
127*4882a593Smuzhiyun  * keyring.
128*4882a593Smuzhiyun  */
keyctl_get_persistent(uid_t _uid,key_serial_t destid)129*4882a593Smuzhiyun long keyctl_get_persistent(uid_t _uid, key_serial_t destid)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun 	struct user_namespace *ns = current_user_ns();
132*4882a593Smuzhiyun 	key_ref_t dest_ref;
133*4882a593Smuzhiyun 	kuid_t uid;
134*4882a593Smuzhiyun 	long ret;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	/* -1 indicates the current user */
137*4882a593Smuzhiyun 	if (_uid == (uid_t)-1) {
138*4882a593Smuzhiyun 		uid = current_uid();
139*4882a593Smuzhiyun 	} else {
140*4882a593Smuzhiyun 		uid = make_kuid(ns, _uid);
141*4882a593Smuzhiyun 		if (!uid_valid(uid))
142*4882a593Smuzhiyun 			return -EINVAL;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 		/* You can only see your own persistent cache if you're not
145*4882a593Smuzhiyun 		 * sufficiently privileged.
146*4882a593Smuzhiyun 		 */
147*4882a593Smuzhiyun 		if (!uid_eq(uid, current_uid()) &&
148*4882a593Smuzhiyun 		    !uid_eq(uid, current_euid()) &&
149*4882a593Smuzhiyun 		    !ns_capable(ns, CAP_SETUID))
150*4882a593Smuzhiyun 			return -EPERM;
151*4882a593Smuzhiyun 	}
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	/* There must be a destination keyring */
154*4882a593Smuzhiyun 	dest_ref = lookup_user_key(destid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
155*4882a593Smuzhiyun 	if (IS_ERR(dest_ref))
156*4882a593Smuzhiyun 		return PTR_ERR(dest_ref);
157*4882a593Smuzhiyun 	if (key_ref_to_ptr(dest_ref)->type != &key_type_keyring) {
158*4882a593Smuzhiyun 		ret = -ENOTDIR;
159*4882a593Smuzhiyun 		goto out_put_dest;
160*4882a593Smuzhiyun 	}
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	ret = key_get_persistent(ns, uid, dest_ref);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun out_put_dest:
165*4882a593Smuzhiyun 	key_ref_put(dest_ref);
166*4882a593Smuzhiyun 	return ret;
167*4882a593Smuzhiyun }
168