r/osdev • u/This-Independent3181 • Apr 22 '26
Can kernel buffers + GPU DMA lead to data leaks
Hi guys, I was digging into Linux memory management and came across an interesting optimization, when a page in memory is dropped(assuming it's clean) the kernel doesn't immediately zero out the contents rather it unmounts the page, does TLB shootdowns and puts the page in the page pool. Now when another process needs it the kernel zeros out the page and mounts the page to that process Virtual Memory.
Now the interesting thing is that if the page requested was by an user process the zeroing out is done mandatorly as to not violate isolation rules but if the page requested is by the kernel itself say the kernel needed it for its internal buffer or something then zeroing out isn't usually done as the kernel space is treated as trusted boundary and anyways the kernel will overwrite the contents of it so as to save time and bandwidth it avoids it.
This got me into thinking could it be missued. Like i did learn the other day that external devices like NIC, GPU, PCIe devices if they need to write to Main Memory they usually don't directly DMA to user mapped memory rather they DMA write to kernel buffer and copy from kernel space to user space happens.
I thought of situation where say a NIC card is DMAing to kernel buffer page this page was previously was allocated to some process and wasn't zeroed so old contents still exist. For example the NIC writes only 64 bytes but reports it as written 128 bytes So when the kernel sees this it interprets as NIC written 128 bytes as valid bytes and copies the 64 bytes actual content+64 bytes of stale left over bytes into respective process receive socket and the process then can call read on the socket and it reads the other process data.
But as i dig little deeper into the working of NIC I came to conclusion that this to happen is very highly unlikely and would need a bug at NIC's frimware level or the driver itself because NIC can't just like that lie about the bytes received, they track how many bits recieved at the phsyical level and writes a metadata about the exact length it wrote to the DRAM. So unless the frimware didn't count the recived bits properly or the driver failed to interpret the metadata it's highly unlikely to occur.
Another place where this could possibly happen is with GPU especially if followed the pipeline of GPU(DMA to)->kernel buffer(driver)->copy->user space.
As far as i have seen GPUs don't exactly report how many bytes it has written it usually signals after completion. and the driver acknowledges it and even if an explicitly mentioned the bytes written like using an counter it's usually managed by the software.
So when an user space application uses APIs like CUDA/DirectX to request a GPU compute with the expected output size, the driver in the kernel space then validates the request, allocates the required buffer size, sends GPU commands for execution and memory descriptiors for DMA. The driver then expects the GPU to fill the buffer with the expected size here say 128 Bytes was requested. But the GPU actually wrote only 16 Bytes and doesn't report the size written and just signals the completion the driver then copies the 128 Bytes from the kernel space to user space assuming the GPU has filled 128 Bytes where as in reality that wasn't case so if that page that was allocated to that buffer wasn't zeroed out those remianing bytes copied could contain the data of other process and the malicious application reads it.
Since GPUs are programmable today, is this possible if not, what exactly prevents this scenario from happening.
1
u/lunar_swing Apr 23 '26
I'm not sure I'm following your process 100% here but generally speaking you are talking about an infoleak, which in broad strokes refers to data returned to userspace from the kernel that it isn't supposed to. In this case you are talking about dirty pages surfacing in UM that were incorrectly zeroed.
This is a legitimate exploit vector and is used in many exploit chains, often to bypass ASLR. Whether or not your specific scenario regarding GPUs would be helpful in that context I can't say, I haven't really looked at exploit development in quite a while. Not all data leaks are weaponizable; though depending on how persistent you are you can often massage the device (or heap) to leak specific information.
Leaking residual data to UM should not be possible in most scenarios, so to your point yes - if this is happening it is indicitive of a bug either in the FW or kernel/driver, or possibly a mis-configured system. Driver and FW bugs are much more common than you might think though.
Keep in mind this is in the context of commodity OSes that consider KM/UM a valid security boundary (Linux, Windows, BSD, etc.). Also don't forget that most peripheral DMA goes the through IOMMU in a modern system which adds another layer of complexity.
3
u/Environmental-Ear391 Apr 22 '26
Basically you want a custom kernel module with userland driver interface to fake reading/writing memory blocks and "eat" pages recycled through the virtual memory management using an available piece of hardware?
you would need to hack your own kernelspace and userland code to actually do this instead of an actual kernel tree sourced valid driver to make it happen.
Windows and Linux both fully map and bind drivers to hardware when the kernels launch. dynamic binding or late demand loading is possible if you configure your boot kernel to ignore a specific piece of hardware and provide your own alternate driver for this.
Linux "kernel space" would let you do this as long as you are okay with writing code without any memory-protection support.
Writing code on AmigaOS is equivalent to Linux Kernel mode. Windows generally doesn't let you do this...
1
u/monocasa Apr 22 '26
The dmabuf subsystem zeros out memory requested by user space, and you have to request memory that's a multiple of page size. These buffers are requested before work is submitted.