The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
uaccess.c File Reference

Functions

bool user_access_ok (const void *ptr, size_t len, bool write)
 safe access to user memory from syscalls.
kerr_t copy_from_user (void *dst, const void *user_src, size_t len)
 copy bytes from user memory into a kernel buffer.
kerr_t copy_to_user (void *user_dst, const void *src, size_t len)
 copy bytes from a kernel buffer into user memory.
int32_t strncpy_from_user (char *dst, const char *user_src, size_t size)
 copy a NUL-terminated user string into a kernel buffer, checking each page as the copy reaches it.

Function Documentation

◆ user_access_ok()

bool user_access_ok ( const void * ptr,
size_t len,
bool write )

safe access to user memory from syscalls.

a pointer from user space can be anything: unmapped, kernel memory, or a buffer that runs off the end of a mapping. touching it blindly page-faults in the kernel (a panic) or lets a program read or write kernel memory. these helpers check the current process's page tables first, so a bad pointer becomes KERR_FAULT (-EFAULT) instead. pages an area allows but that are not backed yet (demand paging) are backed before the access.

the check and the access are not atomic, which is fine for now: there is one CPU and one thread per address space, and only the process itself can change its mappings, never while it is inside a syscall. check that the current process could access a whole user range.

Parameters
ptrstart of the range (a user address).
lenlength in bytes; 0 is always accessible.
writetrue if the kernel will write to the range.
Returns
true if every byte is mapped user memory with that access.

◆ copy_from_user()

kerr_t copy_from_user ( void * dst,
const void * user_src,
size_t len )

copy bytes from user memory into a kernel buffer.

Parameters
dstkernel destination.
user_srcuser source.
lennumber of bytes.
Returns
KERR_OK, or KERR_FAULT (nothing copied) if the source is bad.

◆ copy_to_user()

kerr_t copy_to_user ( void * user_dst,
const void * src,
size_t len )

copy bytes from a kernel buffer into user memory.

Parameters
user_dstuser destination (must be writable).
srckernel source.
lennumber of bytes.
Returns
KERR_OK, or KERR_FAULT (nothing copied) if the destination is bad.

◆ strncpy_from_user()

int32_t strncpy_from_user ( char * dst,
const char * user_src,
size_t size )

copy a NUL-terminated user string into a kernel buffer, checking each page as the copy reaches it.

Parameters
dstkernel destination.
user_srcuser string.
sizesize of dst, including room for the NUL.
Returns
the string length (without the NUL), KERR_FAULT if the string runs into memory the process cannot read, or KERR_RANGE if it does not fit in size bytes.