Linux Internals: How /proc/self/mem writes to unwritable memory (2021)
12 hours ago
- #Memory Management
- #System Programming
- #Linux Kernel
- The /proc/*/mem pseudofile has 'punch through' semantics allowing writes to unwritable virtual memory, used by projects like Julia JIT compiler and rr debugger.
- A code example demonstrates writing to read-only memory and modifying libc's executable code via /proc/self/mem, proving the feature's functionality.
- Hardware mechanisms like CR0.WP and SMAP control kernel memory access but can be bypassed by the kernel's control over virtual memory.
- The kernel's implementation of /proc/*/mem bypasses MMU restrictions by translating virtual to physical addresses and remapping with writable permissions.
- Memory permissions are tied to virtual addresses, not physical memory, allowing the kernel to manipulate memory access as needed.
- The post explores the nuanced relationship between the OS and hardware, highlighting the kernel's ability to circumvent CPU-imposed constraints.