Skip to contentSkip to conversation

xfs: check for possible overflow in xfs_ioc_trim

PATCH Superseded Tomas Racek 2 participants 3 messages Jul 30, 2012 – Jul 31, 2012 XFS filesystem

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: check for possible overflow in xfs_ioc_trim

If range.start argument was between ULLONG_MAX - BBSIZE and ULLONG_MAX, BTOBB macro resulted in overflow which caused start to be set to 0. Now, invalid argument error is returned instead.

---
 fs/xfs/xfs_discard.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
fs/xfs/xfs_discard.c +4 -0
index f9c3fe3..0ef7dd4 100644
@@ -179,6 +179,10 @@ xfs_ioc_trim(
 	 * used by the fstrim application.  In the end it really doesn't
 	 * matter as trimming blocks is an advisory interface.
 	 */
+
+	if (range.start > ULLONG_MAX - BBSIZE)
+		return -XFS_ERROR(EINVAL);
+
 	start = BTOBB(range.start);
 	end = start + BTOBBT(range.len) - 1;
 	minlen = BTOBB(max_t(u64, granularity, range.minlen));
Signature · 6 lines
1.7.7.6

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

If range.start argument was between ULLONG_MAX - BBSIZE and ULLONG_MAX, BTOBB macro resulted in overflow which caused start to be set to 0. Now, invalid argument error is returned instead.

---
 fs/xfs/xfs_discard.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
fs/xfs/xfs_discard.c +0 -0
index f9c3fe3..0ef7dd4 100644

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

Quoted 7 lines
fs/xfs/xfs_discard.c +4 -0
@@ -179,6 +179,10 @@ xfs_ioc_trim(
 	 * used by the fstrim application.  In the end it really doesn't
 	 * matter as trimming blocks is an advisory interface.
 	 */
+
+	if (range.start > ULLONG_MAX - BBSIZE)
+		return -XFS_ERROR(EINVAL);
+

There's no point checking for overflow on the range start - what we need to check is whether it is larger than the size of the filesystem. We do that after the conversion of range.start to basic blocks, so that check needs to be promoted to before this. i.e.

	if (range.start >= XFS_FSB_TO_B(mp, mp->m_sb.sb_dblocks))
		return -XFS_ERROR(EINVAL);

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

Quoted 3 lines
fs/xfs/xfs_discard.c +0 -0
 	start = BTOBB(range.start);
 	end = start + BTOBBT(range.len) - 1;
 	minlen = BTOBB(max_t(u64, granularity, range.minlen));

And that will prevent the overflow in BTOBB() just as effectively...

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#2 lore raw
Quoted 10 lines

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

Quoted 4 lines
fs/xfs/xfs_discard.c +4 -0
+
+	if (range.start > ULLONG_MAX - BBSIZE)
+		return -XFS_ERROR(EINVAL);
+

There's no point checking for overflow on the range start - what we need to check is whether it is larger than the size of the filesystem. We do that after the conversion of range.start to basic blocks, so that check needs to be promoted to before this. i.e.

Quoted 8 lines
	if (range.start >= XFS_FSB_TO_B(mp, mp->m_sb.sb_dblocks))
		return -XFS_ERROR(EINVAL);

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

Quoted 3 lines
fs/xfs/xfs_discard.c +0 -0
 	start = BTOBB(range.start);
 	end = start + BTOBBT(range.len) - 1;
 	minlen = BTOBB(max_t(u64, granularity, range.minlen));

And that will prevent the overflow in BTOBB() just as effectively...

You're right, that's a far better way, I'll change it so.

Thanks!

Tomas

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