Linux NILFS development
 help / color / mirror / Atom feed
* [PATCH] nilfs2: fix possible int overflows in nilfs_fiemap()
@ 2025-01-15 17:08 Nikita Zhandarovich
  2025-01-15 19:06 ` Ryusuke Konishi
  0 siblings, 1 reply; 3+ messages in thread
From: Nikita Zhandarovich @ 2025-01-15 17:08 UTC (permalink / raw)
  To: Ryusuke Konishi
  Cc: Nikita Zhandarovich, linux-nilfs, linux-kernel, lvc-project

Since nilfs_bmap_lookup_contig() in nilfs_fiemap() calculates its
result by being prepared to go through potentially
maxblocks == INT_MAX blocks, the value in n may experience an
overflow caused by left shift of blkbits.

While it is extremely unlikely to occur, play it safe and cast right
hand expression to wider type to mitigate the issue.

Found by Linux Verification Center (linuxtesting.org) with static
analysis tool SVACE.

Fixes: 622daaff0a89 ("nilfs2: fiemap support")
Cc: stable@vger.kernel.org
Signed-off-by: Nikita Zhandarovich <n.zhandarovich@fintech.ru>
---
 fs/nilfs2/inode.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/nilfs2/inode.c b/fs/nilfs2/inode.c
index 23f3a75edd50..81abb58dcbd8 100644
--- a/fs/nilfs2/inode.c
+++ b/fs/nilfs2/inode.c
@@ -1188,7 +1188,7 @@ int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
 			if (size) {
 				if (phys && blkphy << blkbits == phys + size) {
 					/* The current extent goes on */
-					size += n << blkbits;
+					size += (u64)n << blkbits;
 				} else {
 					/* Terminate the current extent */
 					ret = fiemap_fill_next_extent(
@@ -1201,14 +1201,14 @@ int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
 					flags = FIEMAP_EXTENT_MERGED;
 					logical = blkoff << blkbits;
 					phys = blkphy << blkbits;
-					size = n << blkbits;
+					size = (u64)n << blkbits;
 				}
 			} else {
 				/* Start a new extent */
 				flags = FIEMAP_EXTENT_MERGED;
 				logical = blkoff << blkbits;
 				phys = blkphy << blkbits;
-				size = n << blkbits;
+				size = (u64)n << blkbits;
 			}
 			blkoff += n;
 		}

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

* Re: [PATCH] nilfs2: fix possible int overflows in nilfs_fiemap()
  2025-01-15 17:08 Nikita Zhandarovich
@ 2025-01-15 19:06 ` Ryusuke Konishi
  0 siblings, 0 replies; 3+ messages in thread
From: Ryusuke Konishi @ 2025-01-15 19:06 UTC (permalink / raw)
  To: Nikita Zhandarovich; +Cc: linux-nilfs, linux-kernel, lvc-project

On Thu, Jan 16, 2025 at 2:08 AM Nikita Zhandarovich wrote:
>
> Since nilfs_bmap_lookup_contig() in nilfs_fiemap() calculates its
> result by being prepared to go through potentially
> maxblocks == INT_MAX blocks, the value in n may experience an
> overflow caused by left shift of blkbits.
>
> While it is extremely unlikely to occur, play it safe and cast right
> hand expression to wider type to mitigate the issue.
>
> Found by Linux Verification Center (linuxtesting.org) with static
> analysis tool SVACE.
>
> Fixes: 622daaff0a89 ("nilfs2: fiemap support")
> Cc: stable@vger.kernel.org
> Signed-off-by: Nikita Zhandarovich <n.zhandarovich@fintech.ru>
> ---
>  fs/nilfs2/inode.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/fs/nilfs2/inode.c b/fs/nilfs2/inode.c
> index 23f3a75edd50..81abb58dcbd8 100644
> --- a/fs/nilfs2/inode.c
> +++ b/fs/nilfs2/inode.c
> @@ -1188,7 +1188,7 @@ int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
>                         if (size) {
>                                 if (phys && blkphy << blkbits == phys + size) {
>                                         /* The current extent goes on */
> -                                       size += n << blkbits;
> +                                       size += (u64)n << blkbits;
>                                 } else {
>                                         /* Terminate the current extent */
>                                         ret = fiemap_fill_next_extent(
> @@ -1201,14 +1201,14 @@ int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
>                                         flags = FIEMAP_EXTENT_MERGED;
>                                         logical = blkoff << blkbits;
>                                         phys = blkphy << blkbits;
> -                                       size = n << blkbits;
> +                                       size = (u64)n << blkbits;
>                                 }
>                         } else {
>                                 /* Start a new extent */
>                                 flags = FIEMAP_EXTENT_MERGED;
>                                 logical = blkoff << blkbits;
>                                 phys = blkphy << blkbits;
> -                               size = n << blkbits;
> +                               size = (u64)n << blkbits;
>                         }
>                         blkoff += n;
>                 }

Thank you Nikita.

I'll take this patch and send it upstream.

Since nilfs2 extents (contiguous blocks) cannot cross segment
boundaries, these overflows do not occur in normal format.  However,
in an environment where the segments are tuned to be larger, these
overflows can theoretically occur, so as you pointed out, I believe
this fix is necessary.

Thanks,
Ryusuke Konishi

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

* [PATCH] nilfs2: fix possible int overflows in nilfs_fiemap()
@ 2025-01-24 22:20 Ryusuke Konishi
  0 siblings, 0 replies; 3+ messages in thread
From: Ryusuke Konishi @ 2025-01-24 22:20 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-nilfs, linux-kernel, Nikita Zhandarovich

From: Nikita Zhandarovich <n.zhandarovich@fintech.ru>

Since nilfs_bmap_lookup_contig() in nilfs_fiemap() calculates its
result by being prepared to go through potentially
maxblocks == INT_MAX blocks, the value in n may experience an
overflow caused by left shift of blkbits.

While it is extremely unlikely to occur, play it safe and cast right
hand expression to wider type to mitigate the issue.

Found by Linux Verification Center (linuxtesting.org) with static
analysis tool SVACE.

Fixes: 622daaff0a89 ("nilfs2: fiemap support")
Cc: stable@vger.kernel.org
Signed-off-by: Nikita Zhandarovich <n.zhandarovich@fintech.ru>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
---
Hi Andrew, please apply this as a bug fix.

This fixes integer overflows in the fiemap ioctl that don't happen
with normal FS formats, but can happen in special circumstances where
the segment size is tuned to be extra large.

Thanks,
Ryusuke Konishi

 fs/nilfs2/inode.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/nilfs2/inode.c b/fs/nilfs2/inode.c
index e8015d24a82c..6613b8fcceb0 100644
--- a/fs/nilfs2/inode.c
+++ b/fs/nilfs2/inode.c
@@ -1186,7 +1186,7 @@ int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
 			if (size) {
 				if (phys && blkphy << blkbits == phys + size) {
 					/* The current extent goes on */
-					size += n << blkbits;
+					size += (u64)n << blkbits;
 				} else {
 					/* Terminate the current extent */
 					ret = fiemap_fill_next_extent(
@@ -1199,14 +1199,14 @@ int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
 					flags = FIEMAP_EXTENT_MERGED;
 					logical = blkoff << blkbits;
 					phys = blkphy << blkbits;
-					size = n << blkbits;
+					size = (u64)n << blkbits;
 				}
 			} else {
 				/* Start a new extent */
 				flags = FIEMAP_EXTENT_MERGED;
 				logical = blkoff << blkbits;
 				phys = blkphy << blkbits;
-				size = n << blkbits;
+				size = (u64)n << blkbits;
 			}
 			blkoff += n;
 		}
-- 
2.43.0


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

end of thread, other threads:[~2025-01-24 22:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-24 22:20 [PATCH] nilfs2: fix possible int overflows in nilfs_fiemap() Ryusuke Konishi
  -- strict thread matches above, loose matches on Subject: below --
2025-01-15 17:08 Nikita Zhandarovich
2025-01-15 19:06 ` Ryusuke Konishi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox