Skip to contentSkip to conversation

x86/vdso: fix incorrect size in munmap during map_vdso failure

PATCH Applied Guilherme Giacomo Simoes 3 participants 5 messages May 3 – May 19

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

PATCH 1/1 x86/vdso: fix incorrect size in munmap during map_vdso failure

In the map_vdso function, if a failure occurs during the installation of the VVAR mappings, the error path attempts to clean up previously allocated mappings using do_munmap. However, the cleanup for the VVAR mapping was incorrectly using image->size (the size of the vDSO text) instead of the actual size allocated for the VVAR area.

Replace the incorrect image->size parameter with the constant VDSO_NR_PAGES * PAGE_SIZE in the do_munmap call. Ensure the unmap size exactly matches the size used during the vdso_install_vvar_mapping() phase to provide a symmetrical and complete teardown of the memory region.

---
 arch/x86/entry/vdso/vma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/entry/vdso/vma.c +1 -1
index a6bfcc8243cd..d903bce24f15 100644
@@ -178,7 +178,7 @@ static int map_vdso(const struct vdso_image *image, unsigned long addr)
 	if (IS_ERR(vma)) {
 		ret = PTR_ERR(vma);
 		do_munmap(mm, text_start, image->size, NULL);
-		do_munmap(mm, addr, image->size, NULL);
+		do_munmap(mm, addr, VDSO_NR_PAGES * PAGE_SIZE, NULL);
 		goto up_fail;
 	}
 
Signature · 1 line
2.52.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Guilherme Giacomo Simoes

In the map_vdso function, if a failure occurs during the installation of the VVAR mappings, the error path attempts to clean up previously allocated mappings using do_munmap. However, the cleanup for the VVAR mapping was incorrectly using image->size (the size of the vDSO text) instead of the actual size allocated for the VVAR area.

Replace the incorrect image->size parameter with the constant VDSO_NR_PAGES * PAGE_SIZE in the do_munmap call. Ensure the unmap size exactly matches the size used during the vdso_install_vvar_mapping() phase to provide a symmetrical and complete teardown of the memory region.

Out of curiosity, did you encounter this in the real world?

Quoted 1 line
Fixes e93d2521b27f ("x86/vdso: Split virtual clock pages into dedicated mapping")
Quoted 20 lines
---
 arch/x86/entry/vdso/vma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/entry/vdso/vma.c +1 -1
index a6bfcc8243cd..d903bce24f15 100644
@@ -178,7 +178,7 @@ static int map_vdso(const struct vdso_image *image, unsigned long addr)
 	if (IS_ERR(vma)) {
 		ret = PTR_ERR(vma);
 		do_munmap(mm, text_start, image->size, NULL);
-		do_munmap(mm, addr, image->size, NULL);
+		do_munmap(mm, addr, VDSO_NR_PAGES * PAGE_SIZE, NULL);
 		goto up_fail;
 	}
 
Signature · 1 line
2.52.0
#2 lore raw
Quoted 7 lines · Guilherme Giacomo Simoes
Quoted 6 lines

In the map_vdso function, if a failure occurs during the installation of the VVAR mappings, the error path attempts to clean up previously allocated mappings using do_munmap. However, the cleanup for the VVAR mapping was incorrectly using image->size (the size of the vDSO text) instead of the actual size allocated for the VVAR area.

Quoted 7 lines
Quoted 5 lines

Replace the incorrect image->size parameter with the constant VDSO_NR_PAGES * PAGE_SIZE in the do_munmap call. Ensure the unmap size exactly matches the size used during the vdso_install_vvar_mapping() phase to provide a symmetrical and complete teardown of the memory region.

Out of curiosity, did you encounter this in the real world?

When I was debug another problem (another unreferenced object), I compile the kernel, up a qemu with a very small RAM (348MB) and I was run a simple .c program to debug with a kmemleak scan:

```
./seupai & while true; do echo scan > /sys/kernel/debug/kmemleak; cat /sys/kernel/debug/kmemleak; sleep 10; done
```

But, I accidentally stumbled upon on this "unreferenced code" in the vdso.

I saw this stack:

```
unreferenced object 0xff110000168eed80 (size 192):
  comm "seupai", pid 7917, jiffies 4294975318
  hex dump (first 32 bytes):
    00 90 f9 2b 7a 7f 00 00 00 b0 f9 2b 7a 7f 00 00  ...+z......+z...
    00 80 a2 12 00 00 11 ff 25 00 00 00 00 00 00 00  ........%.......
  backtrace (crc ee5fc346):
    kmem_cache_alloc_noprof+0x278/0x4c0
    vm_area_alloc+0x20/0x80
    _install_special_mapping+0x2a/0x160
    map_vdso+0x115/0x250
    load_elf_binary+0x109f/0x15c0
    bprm_execve+0x2d2/0x720
    do_execveat_common+0x519/0x580
    __x64_sys_execve+0x38/0x50
    do_syscall_64+0x60/0x1e0
    entry_SYSCALL_64_after_hwframe+0x76/0x7e
```

And I started thinking about this problem, until I found this little bug.

But I had a problem, I couldn't reproduce that bug again... For test my solution I manually force a problem on third __install_special_mapping from map_vdso() putting vma = -ENOMEM limiting by process name:

```
if (strcmp(current->comm, "seupai") == 0)
	vma = -ENOMEM
```
#4 lore raw

[tip: x86/urgent] x86/vdso: Fix incorrect size in munmap() on map_vdso() failure

In reply to Guilherme Giacomo Simoes
The following commit has been merged into the x86/urgent branch of tip:

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     b088fe35019433541225d315263d8477899e0657
Gitweb:        https://git.kernel.org/tip/b088fe35019433541225d315263d8477899e0657
Author:        Guilherme Giacomo Simoes <[email protected]>
AuthorDate:    Sun, 03 May 2026 16:16:09 -03:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 19 May 2026 16:36:34 +02:00

x86/vdso: Fix incorrect size in munmap() on map_vdso() failure

In map_vdso(), if a failure occurs during the installation of the VVAR mappings, the error path attempts to clean up previously allocated mappings using do_munmap(). However, the cleanup for the VVAR mapping is incorrectly using image->size (the size of the vDSO text) instead of the actual size allocated for the VVAR area.

Replace the incorrect do_munmap() image->size parameter with the constant VDSO_NR_PAGES * PAGE_SIZE. Ensure the unmap size exactly matches the size used during the vdso_install_vvar_mapping() phase to provide a symmetrical and complete teardown of the memory region.

Fixes e93d2521b27f ("x86/vdso: Split virtual clock pages into dedicated mapping")
---
 arch/x86/entry/vdso/vma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/entry/vdso/vma.c +1 -1
index a6bfcc8..d903bce 100644
@@ -178,7 +178,7 @@ static int map_vdso(const struct vdso_image *image, unsigned long addr)
 	if (IS_ERR(vma)) {
 		ret = PTR_ERR(vma);
 		do_munmap(mm, text_start, image->size, NULL);
-		do_munmap(mm, addr, image->size, NULL);
+		do_munmap(mm, addr, VDSO_NR_PAGES * PAGE_SIZE, NULL);
 		goto up_fail;
 	}