From: Eric Biggers <ebiggers@kernel.org>
To: keyrings@vger.kernel.org
Subject: [PATCH RESEND] keyctl: use keyctl_read_alloc() in dump_key_tree_aux()
Date: Thu, 10 Jan 2019 20:31:16 +0000 [thread overview]
Message-ID: <20190110203116.85397-1-ebiggers@kernel.org> (raw)
From: Eric Biggers <ebiggers@google.com>
dump_key_tree_aux() (part of 'keyctl show') was racy: it allocated a
buffer for the keyring contents, then read the keyring. But it's
possible that keys are added to the keyring concurrently. This is
problematic for two reasons. First, when keyctl_read() is passed a
buffer that is too small, it is unspecified whether it is filled or not.
Second, even if the buffer is filled, some keys (not necessarily even
the newest ones) would be omitted from the listing.
Switch to keyctl_read_alloc() which handles the "buffer too small" case
correctly by retrying the read.
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
keyctl.c | 22 ++++++----------------
1 file changed, 6 insertions(+), 16 deletions(-)
diff --git a/keyctl.c b/keyctl.c
index 2c8fdff..fa9e601 100644
--- a/keyctl.c
+++ b/keyctl.c
@@ -2231,29 +2231,18 @@ static int dump_key_tree_aux(key_serial_t key, int depth, int more, int hex_key_
/* if it's a keyring then we're going to want to recursively
* display it if we can */
if (strcmp(type, "keyring") = 0) {
- /* find out how big the keyring is */
- ret = keyctl_read(key, NULL, 0);
- if (ret < 0)
- error("keyctl_read");
- if (ret = 0)
- return 0;
- ringlen = ret;
/* read its contents */
- payload = malloc(ringlen);
- if (!payload)
- error("malloc");
-
- ret = keyctl_read(key, payload, ringlen);
+ ret = keyctl_read_alloc(key, &payload);
if (ret < 0)
- error("keyctl_read");
+ error("keyctl_read_alloc");
- ringlen = ret < ringlen ? ret : ringlen;
+ ringlen = ret;
kcount = ringlen / sizeof(key_serial_t);
/* walk the keyring */
pk = payload;
- do {
+ while (ringlen >= sizeof(key_serial_t)) {
key = *pk++;
/* recurse into next keyrings */
@@ -2281,7 +2270,8 @@ static int dump_key_tree_aux(key_serial_t key, int depth, int more, int hex_key_
hex_key_IDs);
}
- } while (ringlen -= 4, ringlen >= sizeof(key_serial_t));
+ ringlen -= sizeof(key_serial_t);
+ }
free(payload);
}
--
2.20.1.97.g81188d93c3-goog
next reply other threads:[~2019-01-10 20:31 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-10 20:31 Eric Biggers [this message]
2019-01-16 15:26 ` [PATCH RESEND] keyctl: use keyctl_read_alloc() in dump_key_tree_aux() David Howells
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190110203116.85397-1-ebiggers@kernel.org \
--to=ebiggers@kernel.org \
--cc=keyrings@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.