Skip to content

Commit cadb3d4

Browse files
ebiggerspopcornmix
authored andcommitted
KEYS: fix writing past end of user-supplied buffer in keyring_read()
commit e645016 upstream. Userspace can call keyctl_read() on a keyring to get the list of IDs of keys in the keyring. But if the user-supplied buffer is too small, the kernel would write the full list anyway --- which will corrupt whatever userspace memory happened to be past the end of the buffer. Fix it by only filling the space that is available. Fixes: b2a4df2 ("KEYS: Expand the capacity of a keyring") 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 418f4c9 commit cadb3d4

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

security/keys/keyring.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ static void keyring_describe(const struct key *keyring, struct seq_file *m)
416416
}
417417

418418
struct keyring_read_iterator_context {
419-
size_t qty;
419+
size_t buflen;
420420
size_t count;
421421
key_serial_t __user *buffer;
422422
};
@@ -428,9 +428,9 @@ static int keyring_read_iterator(const void *object, void *data)
428428
int ret;
429429

430430
kenter("{%s,%d},,{%zu/%zu}",
431-
key->type->name, key->serial, ctx->count, ctx->qty);
431+
key->type->name, key->serial, ctx->count, ctx->buflen);
432432

433-
if (ctx->count >= ctx->qty)
433+
if (ctx->count >= ctx->buflen)
434434
return 1;
435435

436436
ret = put_user(key->serial, ctx->buffer);
@@ -465,16 +465,12 @@ static long keyring_read(const struct key *keyring,
465465
return 0;
466466

467467
/* Calculate how much data we could return */
468-
ctx.qty = nr_keys * sizeof(key_serial_t);
469-
470468
if (!buffer || !buflen)
471-
return ctx.qty;
472-
473-
if (buflen > ctx.qty)
474-
ctx.qty = buflen;
469+
return nr_keys * sizeof(key_serial_t);
475470

476471
/* Copy the IDs of the subscribed keys into the buffer */
477472
ctx.buffer = (key_serial_t __user *)buffer;
473+
ctx.buflen = buflen;
478474
ctx.count = 0;
479475
ret = assoc_array_iterate(&keyring->keys, keyring_read_iterator, &ctx);
480476
if (ret < 0) {

0 commit comments

Comments
 (0)