2023-02-06 16:22:56

by Vladislav Efanov

[permalink] [raw]
Subject: [PATCH] udf: KASAN: slab-out-of-bounds in udf_readdir

The KASAN report is:
[ 1922.586560] BUG: KASAN: slab-out-of-bounds in udf_readdir+0xe00/0x19e0
[ 1922.586922] Write of size 89 at addr ffff888000cd9ea6 by task rm/18493

udf_readdir() tries to write file name out of allocated memory
buffer bounds. The UDF_NAME_LEN_CS0 (255) is used as max length
for file name in udf_put_filename(). But UDF_NAME_LEN (254) is
used as the size for buffer allocation in udf_readdir(). As the
result out-of-bounds write happened.

Found by Linux Verification Center (linuxtesting.org) with xfstests

Fixes: 066b9cded00b ("udf: Use separate buffer for copying split names")
Signed-off-by: Vladislav Efanov <[email protected]>
---
fs/udf/dir.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/udf/dir.c b/fs/udf/dir.c
index be640f4b2f2c..b95607c42ad4 100644
--- a/fs/udf/dir.c
+++ b/fs/udf/dir.c
@@ -169,7 +169,7 @@ static int udf_readdir(struct file *file, struct dir_context *ctx)
nameptr = (char *)(fibh.ebh->b_data + poffset - lfi);
} else {
if (!copy_name) {
- copy_name = kmalloc(UDF_NAME_LEN,
+ copy_name = kmalloc(UDF_NAME_LEN_CS0,
GFP_NOFS);
if (!copy_name) {
ret = -ENOMEM;
--
2.34.1



2023-02-07 12:45:00

by Jan Kara

[permalink] [raw]
Subject: Re: [PATCH] udf: KASAN: slab-out-of-bounds in udf_readdir

On Mon 06-02-23 19:22:06, Vladislav Efanov wrote:
> The KASAN report is:
> [ 1922.586560] BUG: KASAN: slab-out-of-bounds in udf_readdir+0xe00/0x19e0
> [ 1922.586922] Write of size 89 at addr ffff888000cd9ea6 by task rm/18493
>
> udf_readdir() tries to write file name out of allocated memory
> buffer bounds. The UDF_NAME_LEN_CS0 (255) is used as max length
> for file name in udf_put_filename(). But UDF_NAME_LEN (254) is
> used as the size for buffer allocation in udf_readdir(). As the
> result out-of-bounds write happened.
>
> Found by Linux Verification Center (linuxtesting.org) with xfstests
>
> Fixes: 066b9cded00b ("udf: Use separate buffer for copying split names")
> Signed-off-by: Vladislav Efanov <[email protected]>

Thanks for the fix Vladislav! But this code is not there anymore in current
version of UDF code. It got fixed as part of the directory code rewrite -
you can check current code state in my tree:

git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs.git for_next

Honza

> ---
> fs/udf/dir.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/udf/dir.c b/fs/udf/dir.c
> index be640f4b2f2c..b95607c42ad4 100644
> --- a/fs/udf/dir.c
> +++ b/fs/udf/dir.c
> @@ -169,7 +169,7 @@ static int udf_readdir(struct file *file, struct dir_context *ctx)
> nameptr = (char *)(fibh.ebh->b_data + poffset - lfi);
> } else {
> if (!copy_name) {
> - copy_name = kmalloc(UDF_NAME_LEN,
> + copy_name = kmalloc(UDF_NAME_LEN_CS0,
> GFP_NOFS);
> if (!copy_name) {
> ret = -ENOMEM;
> --
> 2.34.1
>
--
Jan Kara <[email protected]>
SUSE Labs, CR

2023-02-07 13:18:22

by Vladislav Efanov

[permalink] [raw]
Subject: Re: [PATCH] udf: KASAN: slab-out-of-bounds in udf_readdir

Thank you for the information. Will check this repository in the future.

Vlad.

On 07.02.2023 15:44, Jan Kara wrote:
> On Mon 06-02-23 19:22:06, Vladislav Efanov wrote:
>> The KASAN report is:
>> [ 1922.586560] BUG: KASAN: slab-out-of-bounds in udf_readdir+0xe00/0x19e0
>> [ 1922.586922] Write of size 89 at addr ffff888000cd9ea6 by task rm/18493
>>
>> udf_readdir() tries to write file name out of allocated memory
>> buffer bounds. The UDF_NAME_LEN_CS0 (255) is used as max length
>> for file name in udf_put_filename(). But UDF_NAME_LEN (254) is
>> used as the size for buffer allocation in udf_readdir(). As the
>> result out-of-bounds write happened.
>>
>> Found by Linux Verification Center (linuxtesting.org) with xfstests
>>
>> Fixes: 066b9cded00b ("udf: Use separate buffer for copying split names")
>> Signed-off-by: Vladislav Efanov <[email protected]>
> Thanks for the fix Vladislav! But this code is not there anymore in current
> version of UDF code. It got fixed as part of the directory code rewrite -
> you can check current code state in my tree:
>
> git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs.git for_next
>
> Honza
>
>> ---
>> fs/udf/dir.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/fs/udf/dir.c b/fs/udf/dir.c
>> index be640f4b2f2c..b95607c42ad4 100644
>> --- a/fs/udf/dir.c
>> +++ b/fs/udf/dir.c
>> @@ -169,7 +169,7 @@ static int udf_readdir(struct file *file, struct dir_context *ctx)
>> nameptr = (char *)(fibh.ebh->b_data + poffset - lfi);
>> } else {
>> if (!copy_name) {
>> - copy_name = kmalloc(UDF_NAME_LEN,
>> + copy_name = kmalloc(UDF_NAME_LEN_CS0,
>> GFP_NOFS);
>> if (!copy_name) {
>> ret = -ENOMEM;
>> --
>> 2.34.1
>>