Kernel Internals - Part 2
SYSCALL_DEFINE<n>
SYSCALL_DEFINE5(example_func)
asmlinkage long sys_exazmple_func
userspace calls syscall
Save Context
We need to make a context switch
Kernel Executes System Call
Restore Context
Context Switching back to user space
kernel calls sysexit
struct file_operations my_fops =
{
.owner = THIS_MODULE,
.read = read_func,
.write = write_func,
.open = open_func,
.ioctl = ioctl_func,
.release = release_func,
};
struct file
is a kernel structure associated with an open file.
struct file_structs
current->files->fd_array[]
You are going to create a character device and interact with it.
On pwn.college I added a character device challenge in Kernel Internals.
Follow the directions in the README and template.
Get the flag and submit your code!
access_ok, user_addr_max
- kernel API to check if address is userspacecopy_from/in/to_iter/user
- Handling transfer/usage of userspace & kernelspace data interactionVirtual Memory Translation
Process 1
Process 2
Process 3
Read 0x10000
Read 0x10000
Read 0x10000
Reads:
Proc 1: 0xcdf1200
Proc 2: 0x7f86c00
Proc 3: 0xab89200
Images from: https://os.phil-opp.com/page-tables/
current->mm->pgd
- Top level of the associated page table
walk_page_range
flush_tlb_all, flush_tlb_page, flush_tlb_range
current->mm
- of type struct mm_struct
current->active_mm
will contain the memory that the anonymous process will be currently attached tocurrent->mm
- contains useful fields
We have various allocators to retrieve free memory
struct kmem_cache
)struct page
alloc_pages
functionkmalloc_caches[kmalloc_type(flags)][kmalloc_slab(size)]
read()
sys_read
EXT4
Hard Drive
read()
sys_read
EXT4
Hard Drive
register_filesystem
- register the filesystem
file_system_type
file_systems
arraysget_userns
struct inode
can be found in /include/linux/fs.hstruct dentry
in /include/linux/dcache.hstruct file
in /include/linux/fs.h