text
stringlengths
0
1.99k
file is set the user id of the process is set to the owner of the file.
The second half of the if clause exists so that if you start an SUID
binary with a debugger attached it doesn't change the user.
---[ 4.4 - ptrace
ToaruOS has the ability to debug programs in user space. It has a
'ptrace' syscall to do this, similar to the way it works on Linux.
'ptrace' lets you attach to a process - the 'tracee' - and to
manipulate it in various ways as the 'tracer'.
You can read registers, single-step, read or alter memory, etc.
'ptrace_handle()' in '/kernel/sys/ptrace.c' implements it in ToaruOS.
That function is just a huge switch statement based on which of these
operations was requested. Instead let's look at 'ptrace_peek()' and
'ptrace_poke()' for the moment.
'peek' reads a byte and 'poke' writes a byte in the tracee.
Keep in mind that when we are in the 'ptrace' syscall the current
process is the 'tracer', not the 'tracee'.
Let's start with 'ptrace_peek()':
long ptrace_peek(pid_t pid, void * addr, void * data) {
if (!data || ptr_validate(data, "ptrace")) return -EFAULT;
process_t * tracee = process_from_pid(pid);
if (!tracee
|| (tracee->tracer != this_core->current_process->id)
|| !(tracee->flags & PROC_FLAG_SUSPENDED)
)
return -ESRCH;
Again it starts by verifying a user provided pointer 'data'.
But notably it does NOT verify 'addr'. We will get back to that.
Then it looks up the 'tracee' process. If the 'tracee' doesn't exist,
or if we aren't the 'tracer', or if the process isn't in a suspended
state we will error out.
union PML * page_entry = mmu_get_page_other(
tracee->thread.page_directory->directory, (uintptr_t)addr);
if (!page_entry) return -EFAULT;
if (!mmu_page_is_user_readable(page_entry)) return -EFAULT;
Next, it gets the page table entry of the provided address 'addr' in
the 'tracee' process.
The reason 'ptr_validate()' isn't used for 'addr' is that the address
is a pointer to memory in the currently running process, but instead in
the 'tracee'.
If there is no corresponding entry we exit with '-EFAULT'.
If there is an entry we check if it is user readable and if not we
error out as well. The check is implemented in a macro.
#define mmu_page_is_user_readable(p) (p->bits.user)
It checks if the user bit on the page is set. What that means is that
we could just read the page from ring 3, so we can not access anything
new this way.
This all seems sensible, so let's move on.
---[ 4.5 - Poking the first hole
Taking a look at 'ptrace_poke()' it is very similar to 'ptrace_peek()'.
long ptrace_poke(pid_t pid, void * addr, void * data) {
if (!data || ptr_validate(data, "ptrace")) return -EFAULT;
process_t * tracee = process_from_pid(pid);
if (!tracee
|| (tracee->tracer != this_core->current_process->id)
|| !(tracee->flags & PROC_FLAG_SUSPENDED)) return -ESRCH;
union PML * page_entry = mmu_get_page_other(
tracee->thread.page_directory->directory, (uintptr_t)addr);
if (!page_entry) return -EFAULT;
if (!mmu_page_is_user_writable(page_entry)) return -EFAULT;
The only difference is that we check if the page is user writable now
instead of readable, which seems sensible.
But looking at the macro there's a glaring omission:
#define mmu_page_is_user_writable(p) (p->bits.writable)