Skip to contentSkip to conversation

mm/usercopy: Use memory range to be accessed for wraparound check

PATCH Applied Isaac J. Manjarres 2 participants 2 messages Jul 30, 2019 – Jul 31, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/usercopy: Use memory range to be accessed for wraparound check

Currently, when checking to see if accessing n bytes starting at address "ptr" will cause a wraparound in the memory addresses, the check in check_bogus_address() adds an extra byte, which is incorrect, as the range of addresses that will be accessed is [ptr, ptr + (n - 1)].

This can lead to incorrectly detecting a wraparound in the memory address, when trying to read 4 KB from memory that is mapped to the the last possible page in the virtual address space, when in fact, accessing that range of memory would not cause a wraparound to occur.

Use the memory range that will actually be accessed when considering if accessing a certain amount of bytes will cause the memory address to wrap around.

---
 mm/usercopy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/usercopy.c +1 -1
index 2a09796..98e92486 100644
@@ -147,7 +147,7 @@ static inline void check_bogus_address(const unsigned long ptr, unsigned long n,
 				       bool to_user)
 {
 	/* Reject if object wraps past end of memory. */
-	if (ptr + n < ptr)
+	if (ptr + (n - 1) < ptr)
 		usercopy_abort("wrapped address", NULL, to_user, 0, ptr + n);
 
 	/* Reject if NULL or ZERO-allocation. */
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#1 lore raw
Quoted 16 lines · Isaac J. Manjarres

Currently, when checking to see if accessing n bytes starting at address "ptr" will cause a wraparound in the memory addresses, the check in check_bogus_address() adds an extra byte, which is incorrect, as the range of addresses that will be accessed is [ptr, ptr + (n - 1)].

This can lead to incorrectly detecting a wraparound in the memory address, when trying to read 4 KB from memory that is mapped to the the last possible page in the virtual address space, when in fact, accessing that range of memory would not cause a wraparound to occur.

Use the memory range that will actually be accessed when considering if accessing a certain amount of bytes will cause the memory address to wrap around.

Quoted 7 lines

Ah, thanks for the reminder! (I got surprised by seeing my Ack in this email -- next time please use "v2" or "RESEND" to jog my memory.) This got lost last year; my bad.

Andrew, can you take this or should I send it directly to Linus?

-Kees

Quoted 21 lines
---
 mm/usercopy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/usercopy.c +1 -1
index 2a09796..98e92486 100644
@@ -147,7 +147,7 @@ static inline void check_bogus_address(const unsigned long ptr, unsigned long n,
 				       bool to_user)
 {
 	/* Reject if object wraps past end of memory. */
-	if (ptr + n < ptr)
+	if (ptr + (n - 1) < ptr)
 		usercopy_abort("wrapped address", NULL, to_user, 0, ptr + n);
 
 	/* Reject if NULL or ZERO-allocation. */
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
Signature · 1 line
Kees Cook