ntfs3.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* [PATCH] ntfs3: harden against integer overflows
@ 2022-09-12 15:08 Dan Carpenter
  2022-09-30 16:07 ` Konstantin Komarov
  0 siblings, 1 reply; 2+ messages in thread
From: Dan Carpenter @ 2022-09-12 15:08 UTC (permalink / raw)
  To: Konstantin Komarov; +Cc: ntfs3, kernel-janitors

Smatch complains that the "add_bytes" is not to be trusted.  Use
size_add() to prevent an integer overflow.

Fixes: be71b5cba2e6 ("fs/ntfs3: Add attrib operations")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
The add_bytes variable comes from:

	add = ALIGN(struct_size(ea_all, name, 1 + name_len + val_size), 4);

This is problematic and has inspired a new static checker warning:

fs/ntfs3/xattr.c:26 unpacked_ea_size() warn: using integer overflow function 'size_add()' for math
fs/ntfs3/xattr.c:290 ntfs_set_ea() warn: using integer overflow function 'size_add()' for math

The issue is that the struct_size() has an integer overflow and we call
ALIGN() on it, then "add" becomes zero.  Is there a bounds check that
we could use here?

	add = struct_size(ea_all, name, 1 + name_len + val_size);
	if (add > SOMETHING_MAX)
		return -EINVAL;

Otherwise the limit would have to be if (add > ULONG_MAX - 3) { which
is ugly.

 fs/ntfs3/xattr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/ntfs3/xattr.c b/fs/ntfs3/xattr.c
index 7de8718c68a9..ea582b4fe1d9 100644
--- a/fs/ntfs3/xattr.c
+++ b/fs/ntfs3/xattr.c
@@ -107,7 +107,7 @@ static int ntfs_read_ea(struct ntfs_inode *ni, struct EA_FULL **ea,
 		return -EFBIG;
 
 	/* Allocate memory for packed Ea. */
-	ea_p = kmalloc(size + add_bytes, GFP_NOFS);
+	ea_p = kmalloc(size_add(size, add_bytes), GFP_NOFS);
 	if (!ea_p)
 		return -ENOMEM;
 
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ntfs3: harden against integer overflows
  2022-09-12 15:08 [PATCH] ntfs3: harden against integer overflows Dan Carpenter
@ 2022-09-30 16:07 ` Konstantin Komarov
  0 siblings, 0 replies; 2+ messages in thread
From: Konstantin Komarov @ 2022-09-30 16:07 UTC (permalink / raw)
  To: Dan Carpenter; +Cc: ntfs3, kernel-janitors



On 9/12/22 18:08, Dan Carpenter wrote:
> Smatch complains that the "add_bytes" is not to be trusted.  Use
> size_add() to prevent an integer overflow.
> 
> Fixes: be71b5cba2e6 ("fs/ntfs3: Add attrib operations")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
> The add_bytes variable comes from:
> 
> 	add = ALIGN(struct_size(ea_all, name, 1 + name_len + val_size), 4);
> 
> This is problematic and has inspired a new static checker warning:
> 
> fs/ntfs3/xattr.c:26 unpacked_ea_size() warn: using integer overflow function 'size_add()' for math
> fs/ntfs3/xattr.c:290 ntfs_set_ea() warn: using integer overflow function 'size_add()' for math
> 
> The issue is that the struct_size() has an integer overflow and we call
> ALIGN() on it, then "add" becomes zero.  Is there a bounds check that
> we could use here?
> 
> 	add = struct_size(ea_all, name, 1 + name_len + val_size);
> 	if (add > SOMETHING_MAX)
> 		return -EINVAL;
> 
> Otherwise the limit would have to be if (add > ULONG_MAX - 3) { which
> is ugly.
> 
>   fs/ntfs3/xattr.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/ntfs3/xattr.c b/fs/ntfs3/xattr.c
> index 7de8718c68a9..ea582b4fe1d9 100644
> --- a/fs/ntfs3/xattr.c
> +++ b/fs/ntfs3/xattr.c
> @@ -107,7 +107,7 @@ static int ntfs_read_ea(struct ntfs_inode *ni, struct EA_FULL **ea,
>   		return -EFBIG;
>   
>   	/* Allocate memory for packed Ea. */
> -	ea_p = kmalloc(size + add_bytes, GFP_NOFS);
> +	ea_p = kmalloc(size_add(size, add_bytes), GFP_NOFS);
>   	if (!ea_p)
>   		return -ENOMEM;
>   

Applied, thanks!

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-09-30 16:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-12 15:08 [PATCH] ntfs3: harden against integer overflows Dan Carpenter
2022-09-30 16:07 ` Konstantin Komarov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).