1 /* General persistent per-UID keyrings register
3 * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/user_namespace.h>
13 #include <linux/cred.h>
17 unsigned persistent_keyring_expiry = 3 * 24 * 3600; /* Expire after 3 days of non-use */
19 static struct key_acl persistent_register_keyring_acl = {
20 .usage = REFCOUNT_INIT(1),
23 KEY_POSSESSOR_ACE(KEY_ACE_SEARCH | KEY_ACE_WRITE),
24 KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ),
28 static struct key_acl persistent_keyring_acl = {
29 .usage = REFCOUNT_INIT(1),
31 .possessor_viewable = true,
33 KEY_POSSESSOR_ACE(KEY_ACE_VIEW | KEY_ACE_READ | KEY_ACE_WRITE |
34 KEY_ACE_SEARCH | KEY_ACE_LINK |
35 KEY_ACE_CLEAR | KEY_ACE_INVAL),
36 KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ),
41 * Create the persistent keyring register for the current user namespace.
43 * Called with the namespace's sem locked for writing.
45 static int key_create_persistent_register(struct user_namespace *ns)
47 struct key *reg = keyring_alloc(".persistent_register",
48 KUIDT_INIT(0), KGIDT_INIT(0),
50 &persistent_register_keyring_acl,
51 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
55 ns->persistent_keyring_register = reg;
60 * Create the persistent keyring for the specified user.
62 * Called with the namespace's sem locked for writing.
64 static key_ref_t key_create_persistent(struct user_namespace *ns, kuid_t uid,
65 struct keyring_index_key *index_key)
67 struct key *persistent;
68 key_ref_t reg_ref, persistent_ref;
70 if (!ns->persistent_keyring_register) {
71 long err = key_create_persistent_register(ns);
75 reg_ref = make_key_ref(ns->persistent_keyring_register, true);
76 persistent_ref = find_key_to_update(reg_ref, index_key);
78 return persistent_ref;
81 persistent = keyring_alloc(index_key->description,
82 uid, INVALID_GID, current_cred(),
83 &persistent_keyring_acl,
84 KEY_ALLOC_NOT_IN_QUOTA, NULL,
85 ns->persistent_keyring_register);
86 if (IS_ERR(persistent))
87 return ERR_CAST(persistent);
89 return make_key_ref(persistent, true);
93 * Get the persistent keyring for a specific UID and link it to the nominated
96 static long key_get_persistent(struct user_namespace *ns, kuid_t uid,
99 struct keyring_index_key index_key;
100 struct key *persistent;
101 key_ref_t reg_ref, persistent_ref;
105 /* Look in the register if it exists */
106 memset(&index_key, 0, sizeof(index_key));
107 index_key.type = &key_type_keyring;
108 index_key.description = buf;
109 index_key.desc_len = sprintf(buf, "_persistent.%u", from_kuid(ns, uid));
110 key_set_index_key(&index_key);
112 if (ns->persistent_keyring_register) {
113 reg_ref = make_key_ref(ns->persistent_keyring_register, true);
114 down_read(&ns->keyring_sem);
115 persistent_ref = find_key_to_update(reg_ref, &index_key);
116 up_read(&ns->keyring_sem);
122 /* It wasn't in the register, so we'll need to create it. We might
123 * also need to create the register.
125 down_write(&ns->keyring_sem);
126 persistent_ref = key_create_persistent(ns, uid, &index_key);
127 up_write(&ns->keyring_sem);
128 if (!IS_ERR(persistent_ref))
131 return PTR_ERR(persistent_ref);
134 ret = key_task_permission(persistent_ref, current_cred(), KEY_NEED_LINK);
136 persistent = key_ref_to_ptr(persistent_ref);
137 ret = key_link(key_ref_to_ptr(dest_ref), persistent);
139 key_set_timeout(persistent, persistent_keyring_expiry);
140 ret = persistent->serial;
144 key_ref_put(persistent_ref);
149 * Get the persistent keyring for a specific UID and link it to the nominated
152 long keyctl_get_persistent(uid_t _uid, key_serial_t destid)
154 struct user_namespace *ns = current_user_ns();
159 /* -1 indicates the current user */
160 if (_uid == (uid_t)-1) {
163 uid = make_kuid(ns, _uid);
167 /* You can only see your own persistent cache if you're not
168 * sufficiently privileged.
170 if (!uid_eq(uid, current_uid()) &&
171 !uid_eq(uid, current_euid()) &&
172 !ns_capable(ns, CAP_SETUID))
176 /* There must be a destination keyring */
177 dest_ref = lookup_user_key(destid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
178 if (IS_ERR(dest_ref))
179 return PTR_ERR(dest_ref);
180 if (key_ref_to_ptr(dest_ref)->type != &key_type_keyring) {
185 ret = key_get_persistent(ns, uid, dest_ref);
188 key_ref_put(dest_ref);