Lind-Project / lind_project

Lind: Secure Lightweight Adaptive Isolation
https://hub.docker.com/r/securesystemslab/lind
Apache License 2.0
23 stars 8 forks source link

Fix libc errno return for multiple system calls #317

Open rennergade opened 1 year ago

rennergade commented 1 year ago

I've found a bug with errnos that may be prevalent for multiple system calls. I found this while testing the poll() call in a situation where we should return an errno. In this case it was EINTR which should return -1 and set the errno appropriately to 4.

Here we appropriately return -4 from lind_poll and NaClSysPoll .

But [here](static int nacl_irt_poll_lind (struct pollfd *fds, nfds_t nfds, int timeout) { int rv = NACL_SYSCALL (poll) (fds, nfds, timeout); if (rv < 0) return -rv; return rv; }) when we return to the IRT we negate the return value if its negative.

Then here in [poll.c](https://github.com/Lind-Project/Lind-GlibC/blob/5f73bc6e9b451a2bcef24f6967ece7fbedc41da2/sysdeps/nacl/poll.c#L10 it negates the errno again.

This ends up incorrectly setting the errno and the return value. poll() thinks were returning a non-error value of 4.

I think most of this code is a remnant of changes even before my time, and may be happening for a number of syscalls. We should comb through this code and do some tests to see what syscalls are affected when returning errors and fix those.

@yizhuoliang @Yaxuan-w can you address this as a project while I'm away?

If you have any questions about how the libc functions work @RusherRG and @kuzeyardabulut should be able to answer most questions.

rennergade commented 1 year ago

@Yaxuan-w fixed most of these but theres a few other ones left. This possibly could be an issue for @abunav6 to finish up.

Yaxuan-w commented 8 months ago

Here're list of syscalls need to be checked:

nacl_irt_exit nacl_irt_gettod nacl_irt_clock nacl_irt_nanosleep nacl_irt_sched_yield nacl_irt_sysconf nacl_irt_chdir nacl_irt_chmod nacl_irt_getuid nacl_irt_geteuid nacl_irt_getgid nacl_irt_getegid nacl_irt_getcwd __nacl_irt_fcntl_get nacl_irt_fcntl_set nacl_irt_ioctl nacl_irt_poll nacl_irt_ppoll nacl_irt_recvfrom nacl_irt_select nacl_irt_pselect nacl_irt_getdents nacl_irt_sysbrk nacl_irt_dyncode_create __nacl_irt_dyncode_modify nacl_irt_dyncode_delete nacl_irt_thread_create __nacl_irt_thread_exit nacl_irt_thread_nice nacl_irt_mutex_create __nacl_irt_mutex_destroy nacl_irt_mutex_lock nacl_irt_mutex_unlock __nacl_irt_mutex_trylock nacl_irt_cond_create nacl_irt_cond_destroy __nacl_irt_cond_signal nacl_irt_cond_broadcast nacl_irt_cond_wait __nacl_irt_cond_timed_wait_abs nacl_irt_tls_init nacl_irt_tls_get __nacl_irt_open_resource nacl_irt_clock_getres __nacl_irt_clock_gettime nacl_irt_gethostname nacl_irt_getpid nacl_irt_getppid nacl_irt_fork nacl_irt_dup nacl_irt_dup2 nacl_irt_dup3 nacl_irt_waitpid nacl_irt_wait nacl_irt_wait4 nacl_irt_pipe nacl_irt_pipe2 nacl_irt_execve nacl_irt_execv nacl_irt_sigprocmask nacl_irt_flock