text
stringlengths
0
1.99k
It does check if the writable bit is set, but it does NOT check for the
user bit.
---[ 4.6 - Flat mapping excursion
Feel free to skip this section, it just clarifies some details about
the way the write into another process works and is a little more
verbose than the rest of the article.
On x86 there is only one page table at a given time (per CPU).
Generally that page table is the one of the address space of the
currently running process.
But 'ptrace_poke' wants to write to a virtual address in the address
space of a different process. You might have noticed earlier that the
function that looks up the page table entry is called
'mmu_get_page_other()'.
The 'page_entry' that the function returns is a physical page that is
very likely not currently mapped anywhere in the address space of the
current process.
Looking at the rest of the ptrace_poke() function will help make things
clearer.
uintptr_t mapped_address =
mmu_map_to_physical(tracee->thread.page_directory->directory,
(uintptr_t)addr);
if ((intptr_t)mapped_address < 0 && (intptr_t)mapped_address > -10)
return -EFAULT;
'mapped_address' is assigned the physical address that the virtual
address 'addr' is mapped to in the 'tracee'.
In order for a kernel to not have to constantly map and unmap pages it
is common to instead have a flat virtual mapping at some offset that
corresponds to every physical address minus that offset.
In ToaruOS that offset is:
#define HIGH_MAP_REGION 0xffffff8000000000UL
void * mmu_map_from_physical(uintptr_t frameaddress) {
return (void*)(frameaddress | HIGH_MAP_REGION);
}
This flat mapping is writable because the kernel is responsible for
performing access checks and because it can not know ahead of time if a
a given physical address may need to be written to in the future.
Finally here is the rest of 'ptrace_poke()'.
uintptr_t blarg = (uintptr_t)mmu_map_from_physical(mapped_address);
*(char*)blarg = *(char*)data;
return 0;
'blarg' becomes the pointer into the flat mapping which is writable and
'data' is written to it.
As mentioned earlier, the access flags of a memory page are a property
of the virtual mapping and not of the page itself.
That is why 'mmu_page_is_user_writable()' needs to be explicitly
checked by the kernel instead of just attempting to write and seeing if
it fails.
---[ 5 - The bug
Why is that a problem and what can we do with it?
On first thought it may seem useless. The page probably doesn't have
the user bit set anyway, so we still can't write to it from user mode.
But during a syscall we aren't in user mode. The kernel handles
syscalls in ring 0 and it is allowed to access non-user pages.
'ptrace()' is exactly such a syscall, which means that if we provide a
valid kernel address and it is writable the write will succeed.
Luckily, the mapping of the kernel itself is read/write/execute.
I suspect because it is a LOT simpler to set it up that way before
remapping the kernel to a high address.
This means that for any address in the kernel itself we will pass the
'mmu_page_is_user_writable()' check.
So this bug gives us a very nice one byte write-what-where primitive.
---[ 6 - Write-what-where, but where?