Skip to content

Commit aa9d443

Browse files
committed
KEYS: Fix the size of the key description passed to/from userspace
When a key description argument is imported into the kernel from userspace, as happens in add_key(), request_key(), KEYCTL_JOIN_SESSION_KEYRING, KEYCTL_SEARCH, the description is copied into a buffer up to PAGE_SIZE in size. PAGE_SIZE, however, is a variable quantity, depending on the arch. Fix this at 4096 instead (ie. 4095 plus a NUL termination) and define a constant (KEY_MAX_DESC_SIZE) to this end. When reading the description back with KEYCTL_DESCRIBE, a PAGE_SIZE internal buffer is allocated into which the information and description will be rendered. This means that the description will get truncated if an extremely long description it has to be crammed into the buffer with the stringified information. There is no particular need to copy the description into the buffer, so just copy it directly to userspace in a separate operation. Reported-by: Christian Kastner <debian@kvr.at> Signed-off-by: David Howells <dhowells@redhat.com> Tested-by: Christian Kastner <debian@kvr.at>
1 parent 009d043 commit aa9d443

1 file changed

Lines changed: 26 additions & 30 deletions

File tree

‎security/keys/keyctl.c‎

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <asm/uaccess.h>
2727
#include "internal.h"
2828

29+
#define KEY_MAX_DESC_SIZE 4096
30+
2931
static int key_get_type_from_user(char *type,
3032
const char __user *_type,
3133
unsigned len)
@@ -78,7 +80,7 @@ SYSCALL_DEFINE5(add_key, const char __user *, _type,
7880

7981
description = NULL;
8082
if (_description) {
81-
description = strndup_user(_description, PAGE_SIZE);
83+
description = strndup_user(_description, KEY_MAX_DESC_SIZE);
8284
if (IS_ERR(description)) {
8385
ret = PTR_ERR(description);
8486
goto error;
@@ -177,7 +179,7 @@ SYSCALL_DEFINE4(request_key, const char __user *, _type,
177179
goto error;
178180

179181
/* pull the description into kernel space */
180-
description = strndup_user(_description, PAGE_SIZE);
182+
description = strndup_user(_description, KEY_MAX_DESC_SIZE);
181183
if (IS_ERR(description)) {
182184
ret = PTR_ERR(description);
183185
goto error;
@@ -287,7 +289,7 @@ long keyctl_join_session_keyring(const char __user *_name)
287289
/* fetch the name from userspace */
288290
name = NULL;
289291
if (_name) {
290-
name = strndup_user(_name, PAGE_SIZE);
292+
name = strndup_user(_name, KEY_MAX_DESC_SIZE);
291293
if (IS_ERR(name)) {
292294
ret = PTR_ERR(name);
293295
goto error;
@@ -562,8 +564,9 @@ long keyctl_describe_key(key_serial_t keyid,
562564
{
563565
struct key *key, *instkey;
564566
key_ref_t key_ref;
565-
char *tmpbuf;
567+
char *infobuf;
566568
long ret;
569+
int desclen, infolen;
567570

568571
key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
569572
if (IS_ERR(key_ref)) {
@@ -586,38 +589,31 @@ long keyctl_describe_key(key_serial_t keyid,
586589
}
587590

588591
okay:
589-
/* calculate how much description we're going to return */
590-
ret = -ENOMEM;
591-
tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
592-
if (!tmpbuf)
593-
goto error2;
594-
595592
key = key_ref_to_ptr(key_ref);
593+
desclen = strlen(key->description);
596594

597-
ret = snprintf(tmpbuf, PAGE_SIZE - 1,
598-
"%s;%d;%d;%08x;%s",
599-
key->type->name,
600-
from_kuid_munged(current_user_ns(), key->uid),
601-
from_kgid_munged(current_user_ns(), key->gid),
602-
key->perm,
603-
key->description ?: "");
604-
605-
/* include a NUL char at the end of the data */
606-
if (ret > PAGE_SIZE - 1)
607-
ret = PAGE_SIZE - 1;
608-
tmpbuf[ret] = 0;
609-
ret++;
595+
/* calculate how much information we're going to return */
596+
ret = -ENOMEM;
597+
infobuf = kasprintf(GFP_KERNEL,
598+
"%s;%d;%d;%08x;",
599+
key->type->name,
600+
from_kuid_munged(current_user_ns(), key->uid),
601+
from_kgid_munged(current_user_ns(), key->gid),
602+
key->perm);
603+
if (!infobuf)
604+
goto error2;
605+
infolen = strlen(infobuf);
606+
ret = infolen + desclen + 1;
610607

611608
/* consider returning the data */
612-
if (buffer && buflen > 0) {
613-
if (buflen > ret)
614-
buflen = ret;
615-
616-
if (copy_to_user(buffer, tmpbuf, buflen) != 0)
609+
if (buffer && buflen >= ret) {
610+
if (copy_to_user(buffer, infobuf, infolen) != 0 ||
611+
copy_to_user(buffer + infolen, key->description,
612+
desclen + 1) != 0)
617613
ret = -EFAULT;
618614
}
619615

620-
kfree(tmpbuf);
616+
kfree(infobuf);
621617
error2:
622618
key_ref_put(key_ref);
623619
error:
@@ -649,7 +645,7 @@ long keyctl_keyring_search(key_serial_t ringid,
649645
if (ret < 0)
650646
goto error;
651647

652-
description = strndup_user(_description, PAGE_SIZE);
648+
description = strndup_user(_description, KEY_MAX_DESC_SIZE);
653649
if (IS_ERR(description)) {
654650
ret = PTR_ERR(description);
655651
goto error;

0 commit comments

Comments
 (0)