linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs/read_write.c: Fix a broken signed integer overflow check.
@ 2022-02-07 12:07 Ari Sundholm
  2022-02-07 14:58 ` Al Viro
  2022-02-07 15:50 ` David Laight
  0 siblings, 2 replies; 7+ messages in thread
From: Ari Sundholm @ 2022-02-07 12:07 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Ari Sundholm, linux-kernel, linux-fsdevel, stable,
	Anton Altaparmakov

The function generic_copy_file_checks() checks that the ends of the
input and output file ranges do not overflow. Unfortunately, there is
an issue with the check itself.

Due to the integer promotion rules in C, the expressions
(pos_in + count) and (pos_out + count) have an unsigned type because
the count variable has the type uint64_t. Thus, in many cases where we
should detect signed integer overflow to have occurred (and thus one or
more of the ranges being invalid), the expressions will instead be
interpreted as large unsigned integers. This means the check is broken.

Fix this by explicitly casting the expressions to loff_t.

Cc: linux-kernel@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org
Cc: stable@vger.kernel.org
Reviewed-by: Anton Altaparmakov <anton@tuxera.com>
Signed-off-by: Ari Sundholm <ari@tuxera.com>
---
 fs/read_write.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/read_write.c b/fs/read_write.c
index 0074afa7ecb3..64166e74adc5 100644
--- a/fs/read_write.c
+++ b/fs/read_write.c
@@ -1431,7 +1431,8 @@ static int generic_copy_file_checks(struct file *file_in, loff_t pos_in,
 		return -ETXTBSY;
 
 	/* Ensure offsets don't wrap. */
-	if (pos_in + count < pos_in || pos_out + count < pos_out)
+	if ((loff_t)(pos_in + count) < pos_in ||
+			(loff_t)(pos_out + count) < pos_out)
 		return -EOVERFLOW;
 
 	/* Shorten the copy to EOF */
-- 
2.20.1


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

end of thread, other threads:[~2022-02-18 12:52 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-07 12:07 [PATCH] fs/read_write.c: Fix a broken signed integer overflow check Ari Sundholm
2022-02-07 14:58 ` Al Viro
2022-02-07 15:22   ` Al Viro
2022-02-07 16:44   ` Ari Sundholm
2022-02-07 17:07     ` Al Viro
2022-02-18 12:52       ` Ari Sundholm
2022-02-07 15:50 ` David Laight

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).