Skip to contentSkip to conversation

mmap04: Make sure the scanf address format is at least 8 hex chars

PATCH In review Mark Wielaard 3 participants 6 messages Sep 18, 2025 – Sep 22, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mmap04: Make sure the scanf address format is at least 8 hex chars

The addresses in /proc/self/maps are at least 8 hex chars. Zeros are added to the front of the address when shorted (both on 32bit and 64bit systems.

Under valgrind the mmaps used in kernel/syscalls/mmap/mmap04.c come out very low in the address space and might be shorter than 8 hex chars. This causes the scanf to fail:
mmap04.c:62: TBROK: Expected 1 conversions got 0 FILE '/proc/self/maps'

Fix this by using "%08" PRIxPTR when creating the fmt used.

---
 testcases/kernel/syscalls/mmap/mmap04.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/mmap/mmap04.c +2 -1
index 4a050b7b50da..5b28180df29b 100644
@@ -58,7 +58,8 @@ static void run(unsigned int i)
 
 	addr2 = SAFE_MMAP(addr1 + pagesize, pagesize, tc->prot, tc->flags | MAP_FIXED, -1, 0);
 
-	sprintf(fmt, "%" PRIxPTR "-%%*x %%s", (uintptr_t)addr2);
+	/* A /proc/self/maps address is at least 8 hex (left zero padded) */
+	sprintf(fmt, "%08" PRIxPTR "-%%*x %%s", (uintptr_t)addr2);
 	SAFE_FILE_LINES_SCANF("/proc/self/maps", fmt, perms);
 
 	if (!strcmp(perms, tc->exp_perms)) {
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi,

Sorry, I cannot type...

Quoted 2 lines · Mark Wielaard

The addresses in /proc/self/maps are at least 8 hex chars. Zeros are added to the front of the address when shorted (both on 32bit and

^ should be shorter.

Quoted 1 line

64bit systems.

^ missing closing bracket ).

Quoted 27 lines

Under valgrind the mmaps used in kernel/syscalls/mmap/mmap04.c come out very low in the address space and might be shorter than 8 hex chars. This causes the scanf to fail:
mmap04.c:62: TBROK: Expected 1 conversions got 0 FILE '/proc/self/maps'

Fix this by using "%08" PRIxPTR when creating the fmt used.

---
 testcases/kernel/syscalls/mmap/mmap04.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/mmap/mmap04.c +2 -1
index 4a050b7b50da..5b28180df29b 100644
@@ -58,7 +58,8 @@ static void run(unsigned int i)
 
 	addr2 = SAFE_MMAP(addr1 + pagesize, pagesize, tc->prot, tc->flags | MAP_FIXED, -1, 0);
 
-	sprintf(fmt, "%" PRIxPTR "-%%*x %%s", (uintptr_t)addr2);
+	/* A /proc/self/maps address is at least 8 hex (left zero padded) */
+	sprintf(fmt, "%08" PRIxPTR "-%%*x %%s", (uintptr_t)addr2);
 	SAFE_FILE_LINES_SCANF("/proc/self/maps", fmt, perms);
 
 	if (!strcmp(perms, tc->exp_perms)) {
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
In reply to Mark Wielaard

Hi!

Quoted 8 lines

The addresses in /proc/self/maps are at least 8 hex chars. Zeros are added to the front of the address when shorted (both on 32bit and 64bit systems.

Under valgrind the mmaps used in kernel/syscalls/mmap/mmap04.c come out very low in the address space and might be shorter than 8 hex chars. This causes the scanf to fail:
mmap04.c:62: TBROK: Expected 1 conversions got 0 FILE '/proc/self/maps'

I guess I do not understand the problem here. The PRIxPTR translates to "x", "lx", or "llx" depending on architecture and as far as I can tell the %x modifier handles leading zeroes just fine.

Quoted 1 line

Fix this by using "%08" PRIxPTR when creating the fmt used.

Unfortunately this is not universally true. For example the vsyscall page is mapped at very high address on x86_64:

ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]

Quoted 25 lines
---
 testcases/kernel/syscalls/mmap/mmap04.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/mmap/mmap04.c +2 -1
index 4a050b7b50da..5b28180df29b 100644
@@ -58,7 +58,8 @@ static void run(unsigned int i)
 
 	addr2 = SAFE_MMAP(addr1 + pagesize, pagesize, tc->prot, tc->flags | MAP_FIXED, -1, 0);
 
-	sprintf(fmt, "%" PRIxPTR "-%%*x %%s", (uintptr_t)addr2);
+	/* A /proc/self/maps address is at least 8 hex (left zero padded) */
+	sprintf(fmt, "%08" PRIxPTR "-%%*x %%s", (uintptr_t)addr2);
 	SAFE_FILE_LINES_SCANF("/proc/self/maps", fmt, perms);
 
 	if (!strcmp(perms, tc->exp_perms)) {
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Hi Cyril,

Quoted 4 lines · Cyril Hrubis
Quoted 4 lines

The addresses in /proc/self/maps are at least 8 hex chars. Zeros are added to the front of the address when shorted (both on 32bit and 64bit systems.

Quoted 8 lines
Quoted 4 lines

Under valgrind the mmaps used in kernel/syscalls/mmap/mmap04.c come out very low in the address space and might be shorter than 8 hex chars. This causes the scanf to fail:
mmap04.c:62: TBROK: Expected 1 conversions got 0 FILE '/proc/self/maps'

I guess I do not understand the problem here. The PRIxPTR translates to "x", "lx", or "llx" depending on architecture and as far as I can tell the %x modifier handles leading zeroes just fine.

The problem is that we want to match (scanf) an absolute address (addr2) at the start of the line. It is this absolute/literal address that doesn't match (because it might not have leading zeros).

e.g. We might want to match the address 403a000 and want to match
against: 0403a000-04048000 rw-p

When creating the fmt which we want to use for scanf we currently
generate: "403a000-%*x %s" Which doesn't match because it is missing the leading zero (the "-%*x %s" would match the rest, except that the start of the line doesn't). So with the "%08" fix we would generate:
"0403a000-%*x %s" which does match because it has the same number of leading zeros.

Quoted 6 lines
Quoted 1 line

Fix this by using "%08" PRIxPTR when creating the fmt used.

Unfortunately this is not universally true. For example the vsyscall page is mapped at very high address on x86_64:

ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]

Assuming we want to match the starting address ffffffffff600000 this will work fine. "%08" PRIxPTR will generate that exact address without extra leading zeros because it is already > 8 hex chars.

Hope that explains what is going on. The confusion might be because we first use a patterns with sprintf to generate the pattern that we are then going to use with scanf to parse both an actual/literal address (plus hex address and perm string).

Cheers,

Mark

Quoted 29 lines
Quoted 25 lines
---
 testcases/kernel/syscalls/mmap/mmap04.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/mmap/mmap04.c +2 -1
index 4a050b7b50da..5b28180df29b 100644
@@ -58,7 +58,8 @@ static void run(unsigned int i)
 
 	addr2 = SAFE_MMAP(addr1 + pagesize, pagesize, tc->prot, tc->flags | MAP_FIXED, -1, 0);
 
-	sprintf(fmt, "%" PRIxPTR "-%%*x %%s", (uintptr_t)addr2);
+	/* A /proc/self/maps address is at least 8 hex (left zero padded) */
+	sprintf(fmt, "%08" PRIxPTR "-%%*x %%s", (uintptr_t)addr2);
 	SAFE_FILE_LINES_SCANF("/proc/self/maps", fmt, perms);
 
 	if (!strcmp(perms, tc->exp_perms)) {
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by
#4 lore raw

Hi!

Quoted 20 lines
Quoted 4 lines
Quoted 4 lines

The addresses in /proc/self/maps are at least 8 hex chars. Zeros are added to the front of the address when shorted (both on 32bit and 64bit systems.

Quoted 8 lines
Quoted 4 lines

Under valgrind the mmaps used in kernel/syscalls/mmap/mmap04.c come out very low in the address space and might be shorter than 8 hex chars. This causes the scanf to fail:
mmap04.c:62: TBROK: Expected 1 conversions got 0 FILE '/proc/self/maps'

I guess I do not understand the problem here. The PRIxPTR translates to "x", "lx", or "llx" depending on architecture and as far as I can tell the %x modifier handles leading zeroes just fine.

The problem is that we want to match (scanf) an absolute address (addr2) at the start of the line. It is this absolute/literal address that doesn't match (because it might not have leading zeros).

e.g. We might want to match the address 403a000 and want to match
against: 0403a000-04048000 rw-p

Quoted 6 lines

When creating the fmt which we want to use for scanf we currently
generate: "403a000-%*x %s" Which doesn't match because it is missing the leading zero (the "-%*x %s" would match the rest, except that the start of the line doesn't). So with the "%08" fix we would generate:
"0403a000-%*x %s" which does match because it has the same number of leading zeros.

Ah right, I'm blind, we generate the fmt on the fly. In that case padding to eight zeroes will match what kernel does.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Hi Mark, Cyril,

Quoted 28 lines

Hi!

Quoted 20 lines
Quoted 4 lines
Quoted 3 lines

The addresses in /proc/self/maps are at least 8 hex chars. Zeros are added to the front of the address when shorted (both on 32bit and 64bit systems.

Quoted 8 lines
Quoted 4 lines

Under valgrind the mmaps used in kernel/syscalls/mmap/mmap04.c come out very low in the address space and might be shorter than 8 hex chars. This causes the scanf to fail:
mmap04.c:62: TBROK: Expected 1 conversions got 0 FILE '/proc/self/maps'

I guess I do not understand the problem here. The PRIxPTR translates to "x", "lx", or "llx" depending on architecture and as far as I can tell the %x modifier handles leading zeroes just fine.

The problem is that we want to match (scanf) an absolute address (addr2) at the start of the line. It is this absolute/literal address that doesn't match (because it might not have leading zeros).

e.g. We might want to match the address 403a000 and want to match
against: 0403a000-04048000 rw-p

Quoted 6 lines

When creating the fmt which we want to use for scanf we currently
generate: "403a000-%*x %s" Which doesn't match because it is missing the leading zero (the "-%*x %s" would match the rest, except that the start of the line doesn't). So with the "%08" fix we would generate:
"0403a000-%*x %s" which does match because it has the same number of leading zeros.

Quoted 2 lines

Ah right, I'm blind, we generate the fmt on the fly. In that case padding to eight zeroes will match what kernel does.

Mark, thanks for fixing our test! Merged.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp