Creating segfault from custom syscall

I am doing copy_to_user() from syscall.

How can I get it to generate segfault or sigbus on error, as if the user space were trying to access the same memory?

+6
source share
1 answer

Well, generally speaking, you cannot. If you do not plan to rewrite the kernel.

When the kernel accesses user-mode addresses, it uses a secure form, usually copy_from_user , copy_to_user , get_user , ... - as you mentioned. These macros have a return value that the kernel checks, and in most cases will return -EFAULT .

Then usually libc comes in and sets the return value to match the man page, which means that it sets errno if the result is wrong (depending on which syscall call was called).

For example, a common fragment in the kernel looks like this:

 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t)))) return -EFAULT; 

(taken from sendfile64 syscall implementation in fs / read_write.c)

As you can see, when the kernel does not read with usermode, it returns -EFAULT .

Given that it is possible to pass several pointers to some system calls, it is not possible to determine which one called -EFAULT . Thus, there is no general usermode way to send SIGSEGV when checking for invalid memory access in the kernel name.


If, however, you write the syscall kernel yourself and you want to trigger a signal, which is not at all difficult. I have not calculated too much in the kernel, but calling something from the send_sig_info lines (or another suitable function in kernel / signal.c) is what you are looking for.

0
source

Source: https://habr.com/ru/post/910188/


All Articles