Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1614580 > unrolled thread
| Started by | Eric Biggers <ebiggers3@gmail.com> |
|---|---|
| First post | 2017-04-01 21:00 +0200 |
| Last post | 2017-04-03 19:50 +0200 |
| Articles | 3 — 2 participants |
Back to article view | Back to linux.kernel
[PATCH] KEYS: put keyring if install_session_keyring_to_cred() fails Eric Biggers <ebiggers3@gmail.com> - 2017-04-01 21:00 +0200
Re: [PATCH] KEYS: put keyring if install_session_keyring_to_cred() fails David Howells <dhowells@redhat.com> - 2017-04-03 17:50 +0200
Re: [PATCH] KEYS: put keyring if install_session_keyring_to_cred() fails Eric Biggers <ebiggers3@gmail.com> - 2017-04-03 19:50 +0200
| From | Eric Biggers <ebiggers3@gmail.com> |
|---|---|
| Date | 2017-04-01 21:00 +0200 |
| Subject | [PATCH] KEYS: put keyring if install_session_keyring_to_cred() fails |
| Message-ID | <trwpI-6vo-13@gated-at.bofh.it> |
From: Eric Biggers <ebiggers@google.com>
In join_session_keyring(), if install_session_keyring_to_cred() were to
fail, we would leak the keyring reference, just like in the bug fixed by
commit 23567fd052a9 ("KEYS: Fix keyring ref leak in
join_session_keyring()"). Fortunately this cannot happen currently, but
we really should be more careful. Do this by adding and using a new
error label at which the keyring reference is dropped.
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
security/keys/process_keys.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/security/keys/process_keys.c b/security/keys/process_keys.c
index b6fdd22205b1..fc60f998c74d 100644
--- a/security/keys/process_keys.c
+++ b/security/keys/process_keys.c
@@ -799,15 +799,14 @@ long join_session_keyring(const char *name)
ret = PTR_ERR(keyring);
goto error2;
} else if (keyring == new->session_keyring) {
- key_put(keyring);
ret = 0;
- goto error2;
+ goto error3;
}
/* we've got a keyring - now to install it */
ret = install_session_keyring_to_cred(new, keyring);
if (ret < 0)
- goto error2;
+ goto error3;
commit_creds(new);
mutex_unlock(&key_session_mutex);
@@ -817,6 +816,8 @@ long join_session_keyring(const char *name)
okay:
return ret;
+error3:
+ key_put(keyring);
error2:
mutex_unlock(&key_session_mutex);
error:
--
2.12.1
[toc] | [next] | [standalone]
| From | David Howells <dhowells@redhat.com> |
|---|---|
| Date | 2017-04-03 17:50 +0200 |
| Message-ID | <tscoW-jE-21@gated-at.bofh.it> |
| In reply to | #1614580 |
Eric Biggers <ebiggers3@gmail.com> wrote:
> +error3:
> + key_put(keyring);
> error2:
> mutex_unlock(&key_session_mutex);
I would prefer the put to be outside of the locked section. How about the
attached instead?
David
---
commit 5f9e6fc4fdbbf9ada9bd03640b1c3ca50a3e5415
Author: Eric Biggers <ebiggers@google.com>
Date: Sat Apr 1 11:54:16 2017 -0700
KEYS: put keyring if install_session_keyring_to_cred() fails
In join_session_keyring(), if install_session_keyring_to_cred() were to
fail, we would leak the keyring reference, just like in the bug fixed by
commit 23567fd052a9 ("KEYS: Fix keyring ref leak in
join_session_keyring()"). Fortunately this cannot happen currently, but
we really should be more careful. Do this by adding and using a new
error label at which the keyring reference is dropped.
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: David Howells <dhowells@redhat.com>
diff --git a/security/keys/process_keys.c b/security/keys/process_keys.c
index 44451af828c0..8e98e5d1b5fb 100644
--- a/security/keys/process_keys.c
+++ b/security/keys/process_keys.c
@@ -793,21 +793,20 @@ long join_session_keyring(const char *name)
KEY_ALLOC_IN_QUOTA, NULL, NULL);
if (IS_ERR(keyring)) {
ret = PTR_ERR(keyring);
- goto error2;
+ goto error_unlock_no_keyring;
}
} else if (IS_ERR(keyring)) {
ret = PTR_ERR(keyring);
- goto error2;
+ goto error_unlock_no_keyring;
} else if (keyring == new->session_keyring) {
- key_put(keyring);
ret = 0;
- goto error2;
+ goto error_unlock;
}
/* we've got a keyring - now to install it */
ret = install_session_keyring_to_cred(new, keyring);
if (ret < 0)
- goto error2;
+ goto error_unlock;
commit_creds(new);
mutex_unlock(&key_session_mutex);
@@ -817,8 +816,11 @@ long join_session_keyring(const char *name)
okay:
return ret;
-error2:
+error_unlock_no_keyring:
+ keyring = NULL;
+error_unlock:
mutex_unlock(&key_session_mutex);
+ key_put(keyring);
error:
abort_creds(new);
return ret;
[toc] | [prev] | [next] | [standalone]
| From | Eric Biggers <ebiggers3@gmail.com> |
|---|---|
| Date | 2017-04-03 19:50 +0200 |
| Subject | Re: [PATCH] KEYS: put keyring if install_session_keyring_to_cred() fails |
| Message-ID | <tseh3-1yt-11@gated-at.bofh.it> |
| In reply to | #1615349 |
Hi David,
On Mon, Apr 03, 2017 at 04:42:05PM +0100, David Howells wrote:
> Eric Biggers <ebiggers3@gmail.com> wrote:
>
> > +error3:
> > + key_put(keyring);
> > error2:
> > mutex_unlock(&key_session_mutex);
>
> I would prefer the put to be outside of the locked section. How about the
> attached instead?
[...]
> diff --git a/security/keys/process_keys.c b/security/keys/process_keys.c
> index 44451af828c0..8e98e5d1b5fb 100644
> --- a/security/keys/process_keys.c
> +++ b/security/keys/process_keys.c
> @@ -793,21 +793,20 @@ long join_session_keyring(const char *name)
> KEY_ALLOC_IN_QUOTA, NULL, NULL);
> if (IS_ERR(keyring)) {
> ret = PTR_ERR(keyring);
> - goto error2;
> + goto error_unlock_no_keyring;
> }
> } else if (IS_ERR(keyring)) {
> ret = PTR_ERR(keyring);
> - goto error2;
> + goto error_unlock_no_keyring;
> } else if (keyring == new->session_keyring) {
> - key_put(keyring);
> ret = 0;
> - goto error2;
> + goto error_unlock;
> }
>
> /* we've got a keyring - now to install it */
> ret = install_session_keyring_to_cred(new, keyring);
> if (ret < 0)
> - goto error2;
> + goto error_unlock;
>
> commit_creds(new);
> mutex_unlock(&key_session_mutex);
> @@ -817,8 +816,11 @@ long join_session_keyring(const char *name)
> okay:
> return ret;
>
> -error2:
> +error_unlock_no_keyring:
> + keyring = NULL;
> +error_unlock:
> mutex_unlock(&key_session_mutex);
> + key_put(keyring);
> error:
> abort_creds(new);
> return ret;
I guess I'm okay with either patch, but I think jumping to the error labels in
the way I proposed is more straightforward since it keeps things in the logical
order. And I doubt there's much advantage in moving key_put() outside of the
mutex, given that key_session_mutex is only used for joining session keyrings so
shouldn't be contended, and key_put() hardly does any work anyway.
- Eric
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web