text stringlengths 0 1.99k |
|---|
in '/kernel/sys/syscall.c'. |
long sys_open(const char * file, long flags, long mode) { |
PTR_VALIDATE(file); |
if (!file) return -EFAULT; |
fs_node_t * node = kopen((char *)file, flags); |
int access_bits = 0; |
if (node && (flags & O_CREAT) && (flags & O_EXCL)) { |
close_fs(node); |
return -EEXIST; |
} |
... |
The first thing the kernel does is to check that 'file' is a valid user |
space pointer. |
The 'ptr_validate()' function checks that the address is in user space |
and is mapped with appropriate flags. This will be important later. |
It then tries to open that file with 'kopen' and then performs access |
checks to determine if the file already exists. Afterwards, it continues |
to perform access checks. |
This is how the OS enforces file system access permissions. If you want |
to open a file it will check all of the permissions before the file is |
ever visible in user mode. |
... |
int fd = process_append_fd(this_core->current_process, node); |
... |
return fd; |
} |
If all the checks have passed 'process_append_fd()' is called and the |
file descriptor is now visible in the user mode process. |
'fd' is then returned from the system call and the libc then returns it |
from 'open()'. |
Since the checks here look sane, we need to change either the files |
permissions or elevate our privileges. Let's take a look at elevating |
privileges. |
---[ 4.2 - Becoming root normally |
You may have wondered how 'sudo' can make you 'root' on a Linux system. |
It is definitely one of those "obvious" things I mentioned earlier, so |
you may never have given it a second thought. But if you do, it seems a |
little odd. |
'sudo' is a program that runs in 'user mode' in ring 3 like any other. |
It can't issue a magic CPU instruction that changes the user and it |
can't write in kernel memory. If it could then so could any other user |
mode process. |
Clearly it uses the 'setuid()' libc function, but using it to switch to |
another user requires privileges. |
But we can run 'sudo' as a low-privileged user to become root, so what |
makes 'sudo' special? |
You probably already know that the way it works is that the file system |
doesn't just store permissions for read/write/execute access, but can |
also store flags and capabilities. |
Particularly the SUID flag denotes that a program should be executed |
not as the user that starts it, but as the user that owns the file. |
On ToaruOS it works exactly the same way as it does on Linux: |
local@livecd ~$ ls -al /bin/sudo |
-r-sr-xr-x 1 root root 10384 Mar 16 17:26 /bin/sudo |
Note that instead of 'x' it shows 's' for the execute permission, |
showing the SUID bit is set. |
---[ 4.3 - SUID on the kernel side |
The implementation of the SUID bit is very straight-forward in ToaruOS |
and can be found in 'elf_exec()' in '/kernel/misc/elf64.c'. |
if ((file->mask & S_ISUID) && |
!(this_core->current_process->flags & |
(PROC_FLAG_TRACE_SYSCALLS | PROC_FLAG_TRACE_SIGNALS))) |
{ |
/* setuid */ |
this_core->current_process->user = file->uid; |
} |
This is already the full implementation. If the 'S_ISUID' flag of the |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.