Skip to contentSkip to conversation

xfs: avoid underflow in xfs_ioc_trim()

PATCH Superseded Lukas Czerner 3 participants 6 messages Oct 9, 2012 – Oct 11, 2012 XFS filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: avoid underflow in xfs_ioc_trim()

Currently if len argument in xfs_ioc_trim() is smaller than one BB (basic block) the 'end' variable underflow. Avoid that by bailing out if len is smaller than BB.

---
 fs/xfs/xfs_discard.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
fs/xfs/xfs_discard.c +6 -1
index 69cf4fc..54dc58a 100644
@@ -183,8 +183,12 @@ xfs_ioc_trim(
 	    range.minlen > XFS_FSB_TO_B(mp, XFS_ALLOC_AG_MAX_USABLE(mp)))
 		return -XFS_ERROR(EINVAL);
 
+	end = BTOBBT(range.len);
+	if (0 == end)
+		goto out;
+
 	start = BTOBB(range.start);
-	end = start + BTOBBT(range.len) - 1;
+	end += start - 1;
 	minlen = BTOBB(max_t(u64, granularity, range.minlen));
 
 	if (end > XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1)
@@ -203,6 +207,7 @@ xfs_ioc_trim(
 	if (last_error)
 		return last_error;
 
+out:
 	range.len = XFS_FSB_TO_B(mp, blocks_trimmed);
 	if (copy_to_user(urange, &range, sizeof(range)))
 		return -XFS_ERROR(EFAULT);
Signature · 6 lines
1.7.7.6

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Reviewed-by on v1 1/1
#1 lore raw
Quoted 42 lines · Lukas Czerner

Currently if len argument in xfs_ioc_trim() is smaller than one BB (basic block) the 'end' variable underflow. Avoid that by bailing out if len is smaller than BB.

---
 fs/xfs/xfs_discard.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
fs/xfs/xfs_discard.c +6 -1
index 69cf4fc..54dc58a 100644
@@ -183,8 +183,12 @@ xfs_ioc_trim(
 	    range.minlen > XFS_FSB_TO_B(mp, XFS_ALLOC_AG_MAX_USABLE(mp)))
 		return -XFS_ERROR(EINVAL);
 
+	end = BTOBBT(range.len);
+	if (0 == end)
+		goto out;
+
 	start = BTOBB(range.start);
-	end = start + BTOBBT(range.len) - 1;
+	end += start - 1;
 	minlen = BTOBB(max_t(u64, granularity, range.minlen));
 
 	if (end > XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1)
@@ -203,6 +207,7 @@ xfs_ioc_trim(
 	if (last_error)
 		return last_error;
 
+out:
 	range.len = XFS_FSB_TO_B(mp, blocks_trimmed);
 	if (copy_to_user(urange, &range, sizeof(range)))
 		return -XFS_ERROR(EFAULT);
Signature · 6 lines
1.7.7.6

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

Looks good,

Signature · 6 lines
--Carlos

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#2 lore raw
In reply to Lukas Czerner
Quoted 14 lines · Lukas Czerner

Currently if len argument in xfs_ioc_trim() is smaller than one BB (basic block) the 'end' variable underflow. Avoid that by bailing out if len is smaller than BB.

---
 fs/xfs/xfs_discard.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
fs/xfs/xfs_discard.c +0 -0
index 69cf4fc..54dc58a 100644

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

Quoted 6 lines
fs/xfs/xfs_discard.c +3 -0
@@ -183,8 +183,12 @@ xfs_ioc_trim(
 	    range.minlen > XFS_FSB_TO_B(mp, XFS_ALLOC_AG_MAX_USABLE(mp)))
 		return -XFS_ERROR(EINVAL);
 
+	end = BTOBBT(range.len);
+	if (0 == end)
+		goto out;

Uggh. "if (end == 0)", please.

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

Quoted 4 lines
fs/xfs/xfs_discard.c +2 -1
+
 	start = BTOBB(range.start);
-	end = start + BTOBBT(range.len) - 1;
+	end += start - 1;

Better would be to check if end <= start. That way it also catches start+len overflows.

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

Quoted 11 lines
fs/xfs/xfs_discard.c +1 -0
 	minlen = BTOBB(max_t(u64, granularity, range.minlen));
 
 	if (end > XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1)
@@ -203,6 +207,7 @@ xfs_ioc_trim(
 	if (last_error)
 		return last_error;
 
+out:
 	range.len = XFS_FSB_TO_B(mp, blocks_trimmed);
 	if (copy_to_user(urange, &range, sizeof(range)))
 		return -XFS_ERROR(EFAULT);

I think it should return EINVAL, not silently do nothing. If the user application uses a loop that increments start/len based on the returned amount of blocks trimmed, returning zero could send it into an endless loop.

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

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

Date: Wed, 10 Oct 2012 06:39:08 +1100
From: Dave Chinner <[email protected]>
To: Lukas Czerner <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] xfs: avoid underflow in xfs_ioc_trim()

Quoted 17 lines · Lukas Czerner

Currently if len argument in xfs_ioc_trim() is smaller than one BB (basic block) the 'end' variable underflow. Avoid that by bailing out if len is smaller than BB.

---
 fs/xfs/xfs_discard.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
fs/xfs/xfs_discard.c +0 -0
index 69cf4fc..54dc58a 100644
@@ -183,8 +183,12 @@ xfs_ioc_trim(
 	    range.minlen > XFS_FSB_TO_B(mp, XFS_ALLOC_AG_MAX_USABLE(mp)))
 		return -XFS_ERROR(EINVAL);
 
Quoted 5 lines

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

Quoted 3 lines
fs/xfs/xfs_discard.c +3 -0
+	end = BTOBBT(range.len);
+	if (0 == end)
+		goto out;

Uggh. "if (end == 0)", please.

Not a Star Wars fan then ? ;). Ok, I'll change that.

Quoted 7 lines

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

Quoted 4 lines
fs/xfs/xfs_discard.c +2 -1
+
 	start = BTOBB(range.start);
-	end = start + BTOBBT(range.len) - 1;
+	end += start - 1;

Better would be to check if end <= start. That way it also catches start+len overflows.

That's not possible. Even if range.start and range.len would be 2^64-1 (which is not possible for range.start) we always do the BTOBB conversion and ((2^64-1) / 512) * 2 always fits the _s64 type.

Quoted 9 lines
Quoted 9 lines
fs/xfs/xfs_discard.c +0 -0
 	minlen = BTOBB(max_t(u64, granularity, range.minlen));
 
 	if (end > XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1)
@@ -203,6 +207,7 @@ xfs_ioc_trim(
 	if (last_error)
 		return last_error;
 
Quoted 9 lines

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

Quoted 4 lines
fs/xfs/xfs_discard.c +1 -0
+out:
 	range.len = XFS_FSB_TO_B(mp, blocks_trimmed);
 	if (copy_to_user(urange, &range, sizeof(range)))
 		return -XFS_ERROR(EFAULT);

I think it should return EINVAL, not silently do nothing. If the user application uses a loop that increments start/len based on the returned amount of blocks trimmed, returning zero could send it into an endless loop.

That's not what the application would do. At least it would not set len to what's returned as number of blocs discarded, it does not make sense.

However if user specify length smaller than what we're able to discard (in xfs it is BBSIZE if I am not mistaken?), then it probably make sense to return -EINVAL. It is similar situation of minlen, where we return -EINVAL if it is bigger than AG. However this will make it to fail at different threshold on different file system / block sizes so I am on the fence about it. What do you think, is it worth it ?

Thanks!
-Lukas

Quoted 5 lines

Cheers,

Dave.

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

#4 lore raw
Quoted 26 lines · Lukáš Czerner
Quoted 24 lines · Dave Chinner

Date: Wed, 10 Oct 2012 06:39:08 +1100
From: Dave Chinner <[email protected]>
To: Lukas Czerner <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] xfs: avoid underflow in xfs_ioc_trim()

Quoted 17 lines · Lukas Czerner

Currently if len argument in xfs_ioc_trim() is smaller than one BB (basic block) the 'end' variable underflow. Avoid that by bailing out if len is smaller than BB.

---
 fs/xfs/xfs_discard.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
fs/xfs/xfs_discard.c +0 -0
index 69cf4fc..54dc58a 100644
@@ -183,8 +183,12 @@ xfs_ioc_trim(
 	    range.minlen > XFS_FSB_TO_B(mp, XFS_ALLOC_AG_MAX_USABLE(mp)))
 		return -XFS_ERROR(EINVAL);
 
Quoted 7 lines
Quoted 5 lines

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

Quoted 3 lines
fs/xfs/xfs_discard.c +3 -0
+	end = BTOBBT(range.len);
+	if (0 == end)
+		goto out;

Uggh. "if (end == 0)", please.

Not a Star Wars fan then ? ;). Ok, I'll change that.

I might be, but I don't get the reference - something to do with the second moon of Endor, or just letting the wookie win so you don't come to an armless end? :)

As it is, I don't like reverse logic notation because it makes code strange and unreadable. It doesn't provide any real benefit because the compiler will warn about typos that result in assignments in such statements (i.e. if (end = 0)). Hence writing code so that the compilation will fail if you make a typo isn't necessary - Clear, consistent and easily readable code is much more important....

Quoted 8 lines
Quoted 7 lines

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

Quoted 4 lines
fs/xfs/xfs_discard.c +2 -1
+
 	start = BTOBB(range.start);
-	end = start + BTOBBT(range.len) - 1;
+	end += start - 1;

Better would be to check if end <= start. That way it also catches start+len overflows.

Quoted 4 lines

That's not possible. Even if range.start and range.len would be 2^64-1 (which is not possible for range.start) we always do the BTOBB conversion and ((2^64-1) / 512) * 2 always fits the _s64 type.

Regardless, checking that the end of the range is after the start is a much more robust check than individual underflow/overflow tests. We do that all over the place in XFS when converting byte ranges to block based start/end pairs.

Quoted 17 lines
Quoted 7 lines
Quoted 7 lines
fs/xfs/xfs_discard.c +0 -0
 	minlen = BTOBB(max_t(u64, granularity, range.minlen));
 
 	if (end > XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1)
@@ -203,6 +207,7 @@ xfs_ioc_trim(
 	if (last_error)
 		return last_error;
 
Quoted 9 lines

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

Quoted 4 lines
fs/xfs/xfs_discard.c +1 -0
+out:
 	range.len = XFS_FSB_TO_B(mp, blocks_trimmed);
 	if (copy_to_user(urange, &range, sizeof(range)))
 		return -XFS_ERROR(EFAULT);

I think it should return EINVAL, not silently do nothing. If the user application uses a loop that increments start/len based on the returned amount of blocks trimmed, returning zero could send it into an endless loop.

Quoted 3 lines

That's not what the application would do. At least it would not set len to what's returned as number of blocs discarded, it does not make sense.

Oh, right, my mistake.

Quoted 7 lines

However if user specify length smaller than what we're able to discard (in xfs it is BBSIZE if I am not mistaken?), then it probably make sense to return -EINVAL. It is similar situation of minlen, where we return -EINVAL if it is bigger than AG. However this will make it to fail at different threshold on different file system / block sizes so I am on the fence about it. What do you think, is it worth it ?

Actually, the minimum length that can be discarded is the filesystem block size, and applications can get that easily enough from stat(2). IOWs, we probably should be rejecting minlen < 1 FSB, range < 1 FSB with EINVAL....

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#5 lore raw
Quoted 39 lines · Dave Chinner

Date: Thu, 11 Oct 2012 10:22:41 +1100
From: Dave Chinner <[email protected]>
To: Lukáš Czerner <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] xfs: avoid underflow in xfs_ioc_trim()

Quoted 32 lines · Lukáš Czerner
Quoted 29 lines · Dave Chinner

Date: Wed, 10 Oct 2012 06:39:08 +1100
From: Dave Chinner <[email protected]>
To: Lukas Czerner <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] xfs: avoid underflow in xfs_ioc_trim()

Quoted 14 lines · Lukas Czerner

Currently if len argument in xfs_ioc_trim() is smaller than one BB (basic block) the 'end' variable underflow. Avoid that by bailing out if len is smaller than BB.

---
 fs/xfs/xfs_discard.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
fs/xfs/xfs_discard.c +0 -0
index 69cf4fc..54dc58a 100644

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

Quoted 6 lines
fs/xfs/xfs_discard.c +3 -0
@@ -183,8 +183,12 @@ xfs_ioc_trim(
 	    range.minlen > XFS_FSB_TO_B(mp, XFS_ALLOC_AG_MAX_USABLE(mp)))
 		return -XFS_ERROR(EINVAL);
 
+	end = BTOBBT(range.len);
+	if (0 == end)
+		goto out;

Uggh. "if (end == 0)", please.

Quoted 5 lines
Quoted 1 line

Not a Star Wars fan then ? ;). Ok, I'll change that.

I might be, but I don't get the reference - something to do with the second moon of Endor, or just letting the wookie win so you don't come to an armless end? :)

I know it under the name "yoda notation", or "yoda condition", probably based on the Yoda's strange sentence composition which is usually kind of backwards :).

Quoted 7 lines

As it is, I don't like reverse logic notation because it makes code strange and unreadable. It doesn't provide any real benefit because the compiler will warn about typos that result in assignments in such statements (i.e. if (end = 0)). Hence writing code so that the compilation will fail if you make a typo isn't necessary - Clear, consistent and easily readable code is much more important....

Good point.

Quoted 14 lines
Quoted 9 lines
Quoted 8 lines

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

Quoted 5 lines
fs/xfs/xfs_discard.c +2 -1
+
 	start = BTOBB(range.start);
-	end = start + BTOBBT(range.len) - 1;
+	end += start - 1;

Better would be to check if end <= start. That way it also catches start+len overflows.

Quoted 4 lines

That's not possible. Even if range.start and range.len would be 2^64-1 (which is not possible for range.start) we always do the BTOBB conversion and ((2^64-1) / 512) * 2 always fits the _s64 type.

Quoted 4 lines

Regardless, checking that the end of the range is after the start is a much more robust check than individual underflow/overflow tests. We do that all over the place in XFS when converting byte ranges to block based start/end pairs.

Fair enough.

Quoted 32 lines
Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
fs/xfs/xfs_discard.c +0 -0
 	minlen = BTOBB(max_t(u64, granularity, range.minlen));
 
 	if (end > XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1)
@@ -203,6 +207,7 @@ xfs_ioc_trim(
 	if (last_error)
 		return last_error;
 
Quoted 9 lines

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

Quoted 4 lines
fs/xfs/xfs_discard.c +1 -0
+out:
 	range.len = XFS_FSB_TO_B(mp, blocks_trimmed);
 	if (copy_to_user(urange, &range, sizeof(range)))
 		return -XFS_ERROR(EFAULT);

I think it should return EINVAL, not silently do nothing. If the user application uses a loop that increments start/len based on the returned amount of blocks trimmed, returning zero could send it into an endless loop.

Quoted 3 lines

That's not what the application would do. At least it would not set len to what's returned as number of blocs discarded, it does not make sense.

Oh, right, my mistake.

Quoted 7 lines

However if user specify length smaller than what we're able to discard (in xfs it is BBSIZE if I am not mistaken?), then it probably make sense to return -EINVAL. It is similar situation of minlen, where we return -EINVAL if it is bigger than AG. However this will make it to fail at different threshold on different file system / block sizes so I am on the fence about it. What do you think, is it worth it ?

Quoted 4 lines

Actually, the minimum length that can be discarded is the filesystem block size, and applications can get that easily enough from stat(2). IOWs, we probably should be rejecting minlen < 1 FSB, range < 1 FSB with EINVAL....

Agreed, we should return EINVAL when range < 1 FSB. However 'minlen' is a different case, when minlen < 1 FSB. Minlen says that we should not discard ranges smaller than certain value which is perfectly feasible with minlen < 1 FSB.

I'll resend the patch, thanks!
-Lukas

Quoted 5 lines

Cheers,

Dave.

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