Commit 33813d43 authored by Eric Biggers's avatar Eric Biggers Committed by Greg Kroah-Hartman

KEYS: encrypted: fix buffer overread in valid_master_desc()

commit 794b4bc2 upstream.

With the 'encrypted' key type it was possible for userspace to provide a
data blob ending with a master key description shorter than expected,
e.g. 'keyctl add encrypted desc "new x" @s'.  When validating such a
master key description, validate_master_desc() could read beyond the end
of the buffer.  Fix this by using strncmp() instead of memcmp().  [Also
clean up the code to deduplicate some logic.]

Cc: Mimi Zohar <zohar@linux.vnet.ibm.com>
Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
Signed-off-by: default avatarDavid Howells <dhowells@redhat.com>
Signed-off-by: default avatarJames Morris <james.l.morris@oracle.com>
Signed-off-by: default avatarJin Qian <jinqian@google.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 4f8a4e99
...@@ -141,23 +141,22 @@ static int valid_ecryptfs_desc(const char *ecryptfs_desc) ...@@ -141,23 +141,22 @@ static int valid_ecryptfs_desc(const char *ecryptfs_desc)
*/ */
static int valid_master_desc(const char *new_desc, const char *orig_desc) static int valid_master_desc(const char *new_desc, const char *orig_desc)
{ {
if (!memcmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN)) { int prefix_len;
if (strlen(new_desc) == KEY_TRUSTED_PREFIX_LEN)
goto out; if (!strncmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN))
if (orig_desc) prefix_len = KEY_TRUSTED_PREFIX_LEN;
if (memcmp(new_desc, orig_desc, KEY_TRUSTED_PREFIX_LEN)) else if (!strncmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN))
goto out; prefix_len = KEY_USER_PREFIX_LEN;
} else if (!memcmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN)) { else
if (strlen(new_desc) == KEY_USER_PREFIX_LEN) return -EINVAL;
goto out;
if (orig_desc) if (!new_desc[prefix_len])
if (memcmp(new_desc, orig_desc, KEY_USER_PREFIX_LEN)) return -EINVAL;
goto out;
} else if (orig_desc && strncmp(new_desc, orig_desc, prefix_len))
goto out; return -EINVAL;
return 0; return 0;
out:
return -EINVAL;
} }
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment