Skip to contentSkip to conversation

xfs: fix possible overflow in xfs_ioc_trim()

PATCH Superseded Lukas Czerner 3 participants 5 messages Sep 5, 2011 – Sep 7, 2011 XFS filesystem

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: fix possible overflow in xfs_ioc_trim()

In xfs_ioc_trim it is possible that start+len might overflow. Fix it by decrementing the len so that start+len equals to the file system size in the worst case.

---
 fs/xfs/xfs_discard.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
fs/xfs/xfs_discard.c +3 -1
index 244e797..0fab107 100644
@@ -146,6 +146,7 @@ xfs_ioc_trim(
 	unsigned int		granularity = q->limits.discard_granularity;
 	struct fstrim_range	range;
 	xfs_fsblock_t		start, len, minlen;
+	xfs_fsblock_t		max_blks =  XFS_MAX_DBLOCKS(&(mp->m_sb));
 	xfs_agnumber_t		start_agno, end_agno, agno;
 	__uint64_t		blocks_trimmed = 0;
 	int			error, last_error = 0;
@@ -171,7 +172,8 @@ xfs_ioc_trim(
 	start_agno = XFS_FSB_TO_AGNO(mp, start);
 	if (start_agno >= mp->m_sb.sb_agcount)
 		return -XFS_ERROR(EINVAL);
-
+	if (len > max_blks)
+		len = max_blks - start;
 	end_agno = XFS_FSB_TO_AGNO(mp, start + len);
 	if (end_agno >= mp->m_sb.sb_agcount)
 		end_agno = mp->m_sb.sb_agcount - 1;
Signature · 6 lines
1.7.4.4

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#1 lore raw
Quoted 3 lines · Lukas Czerner

In xfs_ioc_trim it is possible that start+len might overflow. Fix it by decrementing the len so that start+len equals to the file system size in the worst case.

The idea of the check looks reasonable, but I think it needs to be done a bit different. Was this caught by the new testcase you just sent?

fs/xfs/xfs_discard.c xfs_ioc_trim( · line 149

Quoted 1 line
fs/xfs/xfs_discard.c +1 -0
+	xfs_fsblock_t		max_blks =  XFS_MAX_DBLOCKS(&(mp->m_sb));

XFS_MAX_DBLOCKS is the maximum number of blocks that the given geometry could support. But the last AG could be shorter than the others. I think you really want to check against mp->m_sb.sb_dblocks.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#2 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Lukas Czerner

In xfs_ioc_trim it is possible that start+len might overflow. Fix it by decrementing the len so that start+len equals to the file system size in the worst case.

The idea of the check looks reasonable, but I think it needs to be done a bit different. Was this caught by the new testcase you just sent?

Actually I have found the problem in other file systems too (ext4 and btrfs) while doing test similar to the one sent recently to xfstests. Then I fixed the problems, and then I have created the xfstests test for it so it is clear what the correct behaviour should look like. So yes, the testcase would have caught the problem.

Quoted 7 lines

fs/xfs/xfs_discard.c xfs_ioc_trim( · line 149

Quoted 2 lines
fs/xfs/xfs_discard.c +1 -0
+	xfs_fsblock_t		max_blks =  XFS_MAX_DBLOCKS(&(mp->m_sb));

XFS_MAX_DBLOCKS is the maximum number of blocks that the given geometry could support. But the last AG could be shorter than the others. I think you really want to check against mp->m_sb.sb_dblocks.

ah, ok, I'll change that. Thanks!

-Lukas

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#3 lore raw
In reply to Lukas Czerner
Quoted 22 lines · Lukas Czerner

In xfs_ioc_trim it is possible that start+len might overflow. Fix it by decrementing the len so that start+len equals to the file system size in the worst case.

---
 fs/xfs/xfs_discard.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
fs/xfs/xfs_discard.c +1 -0
index 244e797..0fab107 100644
@@ -146,6 +146,7 @@ xfs_ioc_trim(
 	unsigned int		granularity = q->limits.discard_granularity;
 	struct fstrim_range	range;
 	xfs_fsblock_t		start, len, minlen;
+	xfs_fsblock_t		max_blks =  XFS_MAX_DBLOCKS(&(mp->m_sb));
 	xfs_agnumber_t		start_agno, end_agno, agno;
 	__uint64_t		blocks_trimmed = 0;
 	int			error, last_error = 0;

fs/xfs/xfs_discard.c xfs_ioc_trim( · line 175

Quoted 5 lines
fs/xfs/xfs_discard.c +1 -1
@@ -171,7 +172,8 @@ xfs_ioc_trim(
 	start_agno = XFS_FSB_TO_AGNO(mp, start);
 	if (start_agno >= mp->m_sb.sb_agcount)
 		return -XFS_ERROR(EINVAL);
-
+	if (len > max_blks)

just wondering....

shouldn't this be ((start + len) > max_blks) ?

fs/xfs/xfs_discard.c xfs_ioc_trim( · line 179

Quoted 4 lines
fs/xfs/xfs_discard.c +1 -0
+		len = max_blks - start;
 	end_agno = XFS_FSB_TO_AGNO(mp, start + len);
 	if (end_agno >= mp->m_sb.sb_agcount)
 		end_agno = mp->m_sb.sb_agcount - 1;

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#4 lore raw
Quoted 31 lines · Chandra Seetharaman
Quoted 22 lines · Lukas Czerner

In xfs_ioc_trim it is possible that start+len might overflow. Fix it by decrementing the len so that start+len equals to the file system size in the worst case.

---
 fs/xfs/xfs_discard.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
fs/xfs/xfs_discard.c +1 -0
index 244e797..0fab107 100644
@@ -146,6 +146,7 @@ xfs_ioc_trim(
 	unsigned int		granularity = q->limits.discard_granularity;
 	struct fstrim_range	range;
 	xfs_fsblock_t		start, len, minlen;
+	xfs_fsblock_t		max_blks =  XFS_MAX_DBLOCKS(&(mp->m_sb));
 	xfs_agnumber_t		start_agno, end_agno, agno;
 	__uint64_t		blocks_trimmed = 0;
 	int			error, last_error = 0;

fs/xfs/xfs_discard.c xfs_ioc_trim( · line 175

Quoted 5 lines
fs/xfs/xfs_discard.c +1 -1
@@ -171,7 +172,8 @@ xfs_ioc_trim(
 	start_agno = XFS_FSB_TO_AGNO(mp, start);
 	if (start_agno >= mp->m_sb.sb_agcount)
 		return -XFS_ERROR(EINVAL);
-
+	if (len > max_blks)

just wondering....

Quoted 1 line

shouldn't this be ((start + len) > max_blks) ?

Yes the patch is not correct. See

http://www.spinics.net/lists/xfs/msg06897.html

Quoted 8 lines

fs/xfs/xfs_discard.c xfs_ioc_trim( · line 179

Quoted 5 lines
fs/xfs/xfs_discard.c +1 -0
+		len = max_blks - start;
 	end_agno = XFS_FSB_TO_AGNO(mp, start + len);
 	if (end_agno >= mp->m_sb.sb_agcount)
 		end_agno = mp->m_sb.sb_agcount - 1;

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs