Skip to content

Commit c9460fb

Browse files
ebiggersgregkh
authored andcommitted
KEYS: fix keyctl_set_reqkey_keyring() to not leak thread keyrings
commit c9f838d104fed6f2f61d68164712e3204bf5271b upstream. This fixes CVE-2017-7472. Running the following program as an unprivileged user exhausts kernel memory by leaking thread keyrings: #include <keyutils.h> int main() { for (;;) keyctl_set_reqkey_keyring(KEY_REQKEY_DEFL_THREAD_KEYRING); } Fix it by only creating a new thread keyring if there wasn't one before. To make things more consistent, make install_thread_keyring_to_cred() and install_process_keyring_to_cred() both return 0 if the corresponding keyring is already present. Fixes: d84f4f9 ("CRED: Inaugurate COW credentials") Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent eb78d98 commit c9460fb

2 files changed

Lines changed: 31 additions & 24 deletions

File tree

security/keys/keyctl.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,8 +1228,8 @@ long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
12281228
* Read or set the default keyring in which request_key() will cache keys and
12291229
* return the old setting.
12301230
*
1231-
* If a process keyring is specified then this will be created if it doesn't
1232-
* yet exist. The old setting will be returned if successful.
1231+
* If a thread or process keyring is specified then it will be created if it
1232+
* doesn't yet exist. The old setting will be returned if successful.
12331233
*/
12341234
long keyctl_set_reqkey_keyring(int reqkey_defl)
12351235
{
@@ -1254,11 +1254,8 @@ long keyctl_set_reqkey_keyring(int reqkey_defl)
12541254

12551255
case KEY_REQKEY_DEFL_PROCESS_KEYRING:
12561256
ret = install_process_keyring_to_cred(new);
1257-
if (ret < 0) {
1258-
if (ret != -EEXIST)
1259-
goto error;
1260-
ret = 0;
1261-
}
1257+
if (ret < 0)
1258+
goto error;
12621259
goto set;
12631260

12641261
case KEY_REQKEY_DEFL_DEFAULT:

security/keys/process_keys.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,18 @@ int install_user_keyrings(void)
125125
}
126126

127127
/*
128-
* Install a fresh thread keyring directly to new credentials. This keyring is
129-
* allowed to overrun the quota.
128+
* Install a thread keyring to the given credentials struct if it didn't have
129+
* one already. This is allowed to overrun the quota.
130+
*
131+
* Return: 0 if a thread keyring is now present; -errno on failure.
130132
*/
131133
int install_thread_keyring_to_cred(struct cred *new)
132134
{
133135
struct key *keyring;
134136

137+
if (new->thread_keyring)
138+
return 0;
139+
135140
keyring = keyring_alloc("_tid", new->uid, new->gid, new,
136141
KEY_POS_ALL | KEY_USR_VIEW,
137142
KEY_ALLOC_QUOTA_OVERRUN, NULL);
@@ -143,7 +148,9 @@ int install_thread_keyring_to_cred(struct cred *new)
143148
}
144149

145150
/*
146-
* Install a fresh thread keyring, discarding the old one.
151+
* Install a thread keyring to the current task if it didn't have one already.
152+
*
153+
* Return: 0 if a thread keyring is now present; -errno on failure.
147154
*/
148155
static int install_thread_keyring(void)
149156
{
@@ -154,8 +161,6 @@ static int install_thread_keyring(void)
154161
if (!new)
155162
return -ENOMEM;
156163

157-
BUG_ON(new->thread_keyring);
158-
159164
ret = install_thread_keyring_to_cred(new);
160165
if (ret < 0) {
161166
abort_creds(new);
@@ -166,17 +171,17 @@ static int install_thread_keyring(void)
166171
}
167172

168173
/*
169-
* Install a process keyring directly to a credentials struct.
174+
* Install a process keyring to the given credentials struct if it didn't have
175+
* one already. This is allowed to overrun the quota.
170176
*
171-
* Returns -EEXIST if there was already a process keyring, 0 if one installed,
172-
* and other value on any other error
177+
* Return: 0 if a process keyring is now present; -errno on failure.
173178
*/
174179
int install_process_keyring_to_cred(struct cred *new)
175180
{
176181
struct key *keyring;
177182

178183
if (new->process_keyring)
179-
return -EEXIST;
184+
return 0;
180185

181186
keyring = keyring_alloc("_pid", new->uid, new->gid, new,
182187
KEY_POS_ALL | KEY_USR_VIEW,
@@ -189,11 +194,9 @@ int install_process_keyring_to_cred(struct cred *new)
189194
}
190195

191196
/*
192-
* Make sure a process keyring is installed for the current process. The
193-
* existing process keyring is not replaced.
197+
* Install a process keyring to the current task if it didn't have one already.
194198
*
195-
* Returns 0 if there is a process keyring by the end of this function, some
196-
* error otherwise.
199+
* Return: 0 if a process keyring is now present; -errno on failure.
197200
*/
198201
static int install_process_keyring(void)
199202
{
@@ -207,14 +210,18 @@ static int install_process_keyring(void)
207210
ret = install_process_keyring_to_cred(new);
208211
if (ret < 0) {
209212
abort_creds(new);
210-
return ret != -EEXIST ? ret : 0;
213+
return ret;
211214
}
212215

213216
return commit_creds(new);
214217
}
215218

216219
/*
217-
* Install a session keyring directly to a credentials struct.
220+
* Install the given keyring as the session keyring of the given credentials
221+
* struct, replacing the existing one if any. If the given keyring is NULL,
222+
* then install a new anonymous session keyring.
223+
*
224+
* Return: 0 on success; -errno on failure.
218225
*/
219226
int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
220227
{
@@ -249,8 +256,11 @@ int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
249256
}
250257

251258
/*
252-
* Install a session keyring, discarding the old one. If a keyring is not
253-
* supplied, an empty one is invented.
259+
* Install the given keyring as the session keyring of the current task,
260+
* replacing the existing one if any. If the given keyring is NULL, then
261+
* install a new anonymous session keyring.
262+
*
263+
* Return: 0 on success; -errno on failure.
254264
*/
255265
static int install_session_keyring(struct key *keyring)
256266
{

0 commit comments

Comments
 (0)