ecryptfs: validate key payload size and session key length
PATCH 1/1 ecryptfs: validate key payload size and session key length
Syzkaller reported an out-of-bounds read, which sometimes manifests as a slab-use-after-free, in ecryptfs_write_tag_70_packet(). This is caused by two structural validation failures when handling malformed user keys.
First, eCryptfs blindly casts the user key payload into a struct ecryptfs_auth_tok without verifying the physical size of the allocation. If a user provides a key that is significantly smaller than sizeof(struct ecryptfs_auth_tok), accessing struct fields located deep within the memory layout (such as session_key_encryption_key_bytes) forces the kernel to read past the end of the allocated slab object. If the adjacent slab object was recently freed, KASAN reports this as a use-after-free.
Second, eCryptfs does not validate the internal length field session_key_encryption_key_bytes before passing it to md5(). A maliciously large value causes md5() to read out of bounds.
Fix this by explicitly checking the user_key_payload datalen to ensure the physical memory is large enough to hold the expected structure, and subsequently bounding session_key_encryption_key_bytes to the actual size of the session_key_encryption_key array.
--- fs/ecryptfs/keystore.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
fs/ecryptfs/keystore.c +15 -0
@@ -621,6 +621,7 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes, {struct ecryptfs_write_tag_70_packet_silly_stack *s;struct key *auth_tok_key =NULL ; +struct user_key_payload *ukp;int rc =0 ; s =kzalloc_obj (*s);
@@ -738,6 +739,20 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes, goto out_free_unlock; } + ukp =user_key_payload_locked (auth_tok_key); +if (!ukp || ukp->datalen <sizeof (struct ecryptfs_auth_tok )) { +printk (KERN_ERR "Error key payload too small\n" ); + rc = -EINVAL ; + goto out_free_unlock; + } + +if (s->auth_tok->token.password.session_key_encryption_key_bytes > +sizeof (s->auth_tok->token.password.session_key_encryption_key)) { +printk (KERN_ERR "Error FNEK key size too large\n" ); + rc = -EINVAL ; + goto out_free_unlock; + } +md5 (s->auth_tok->token.password.session_key_encryption_key, s->auth_tok->token.password.session_key_encryption_key_bytes, s->hash);
Signature · 1 line
2.47.3
Tyler Hicks
maintainerQuoted 22 lines · Svyatoslav Nikolenko
Syzkaller reported an out-of-bounds read, which sometimes manifests as a slab-use-after-free, in
ecryptfs_write_tag_70_packet(). This is caused by two structural validation failures when handling malformed user keys.First, eCryptfs blindly casts the user key payload into a
struct ecryptfs_auth_tokwithout verifying the physical size of the allocation. If a user provides a key that is significantly smaller thansizeof(struct ecryptfs_auth_tok), accessing struct fields located deep within the memory layout (such assession_key_encryption_key_bytes) forces the kernel to read past the end of the allocated slab object. If the adjacent slab object was recently freed, KASAN reports this as a use-after-free.Second, eCryptfs does not validate the internal length field
session_key_encryption_key_bytesbefore passing it tomd5(). A maliciously large value causesmd5()to read out of bounds.Fix this by explicitly checking the
user_key_payloaddatalento ensure the physical memory is large enough to hold the expected structure, and subsequently boundingsession_key_encryption_key_bytesto the actual size of thesession_key_encryption_keyarray.
Quoted 5 lines
Reported-by [email protected]Tested-by [email protected]Fixes 0bbb838f385a ("ecryptfs: Use MD5 library instead of crypto_shash")Signed-off-by Svyatoslav Nikolenko <[email protected]>
Hello and thank you for the patch! Unfortunately, these checks are added in the wrong place to protect other packet types (like tag 3). I've already taken the user key payload fix from another contributor and will take the session key encryption key check from a separate contributor. You can find the links here:
* https://lore.kernel.org/ecryptfs/[email protected]/
* https://lore.kernel.org/ecryptfs/[email protected]/
Thanks again for taking the time to submit a patch!
Tyler
Quoted 40 lines
--- fs/ecryptfs/keystore.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)fs/ecryptfs/keystore.c +15
-0@@ -621,6 +621,7 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes, {struct ecryptfs_write_tag_70_packet_silly_stack *s;struct key *auth_tok_key =NULL ; +struct user_key_payload *ukp;int rc =0 ; s =kzalloc_obj (*s);@@ -738,6 +739,20 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes, goto out_free_unlock; } + ukp =user_key_payload_locked (auth_tok_key); +if (!ukp || ukp->datalen <sizeof (struct ecryptfs_auth_tok )) { +printk (KERN_ERR "Error key payload too small\n" ); + rc = -EINVAL ; + goto out_free_unlock; + } + +if (s->auth_tok->token.password.session_key_encryption_key_bytes > +sizeof (s->auth_tok->token.password.session_key_encryption_key)) { +printk (KERN_ERR "Error FNEK key size too large\n" ); + rc = -EINVAL ; + goto out_free_unlock; + } +md5 (s->auth_tok->token.password.session_key_encryption_key, s->auth_tok->token.password.session_key_encryption_key_bytes, s->hash);Signature · 1 line
2.47.3