sgx-lkl
sgx-lkl copied to clipboard
mprotect system call not setting the errno if it fails
Original Test case: The original test case performs below operation. sub-test1: Invoke mprotect() with an address of 0. Check if error is set to ENOMEM. sub-test2: Invoke mprotect() with an address that is not a multiple of PAGESIZE. EINVAL sub-test3: Mmap a file with only read permission (PROT_READ). Try to set write permission (PROT_WRITE) using mprotect(2). Check that error is set to EACCES.
Link: https://github.com/lsds/ltp/blob/8b47ecbf8c1d954343316245b55e232077abcf9f/testcases/kernel/syscalls/mprotect/mprotect01.c#L83
About mprotect: On success, mprotect() and pkey_mprotect() return zero. On error, these system calls return -1, and errno is set appropriately.
Problem/issue: errno is not set while returning from system call. ocall should also return errno from host. This test invokes mprotect library API which eventually ends up in calling the host side mprotect.
Library API Link: https://github.com/lsds/sgx-lkl-musl/blob/26b0e055074230d0a90aebfefd03836088c7bb66/src/mman/mprotect.c#L5
Host Side Code Link: https://github.com/lsds/sgx-lkl/blob/5a0b5d1a31e1cbd73dc0acfe1058c7ed5d0643b3/src/host_interface/host_interface_calls.c#L92
When it returns from ocall it will pass only the return value, not the error code. After returning from the ocall, error number in the enclave is not set.
Link to ocall: https://github.com/lsds/sgx-lkl/blob/5a0b5d1a31e1cbd73dc0acfe1058c7ed5d0643b3/src/lkl/syscall-overrides-mem.c#L138
Link to setting errno: https://github.com/lsds/sgx-lkl-musl/blob/26b0e055074230d0a90aebfefd03836088c7bb66/src/internal/syscall_ret.c#L6
Complete GDB Logs:
[[ SGX-LKL ]] libc_start_main_stage2(): Calling app main: /ltp/testcases/kernel/syscalls/mprotect/mprotect01
[Switching to Thread 0x7fff91a7b700 (LWP 5791)]
Thread 6 "ENCLAVE" hit Breakpoint 1, main (ac=1, av=0x7fe040afef78) at mprotect01.c:84
84 mprotect01.c: No such file or directory.
(gdb) b mprotect
Breakpoint 2 at 0x7fe00055d46c: mprotect. (2 locations)
(gdb) c
Continuing.
Thread 6 "ENCLAVE" hit Breakpoint 2, __mprotect (addr=0x0, len=4097, prot=1) at src/mman/mprotect.c:8
8 start = (size_t)addr & -PAGE_SIZE;
(gdb) list
3 #include "syscall.h"
4
5 int __mprotect(void *addr, size_t len, int prot)
6 {
7 size_t start, end;
8 start = (size_t)addr & -PAGE_SIZE;
9 end = (size_t)((char *)addr + len + PAGE_SIZE-1) & -PAGE_SIZE;
10 return syscall(SYS_mprotect, start, end-start, prot);
10 return syscall(SYS_mprotect, start, end-start, prot);
11 }
12
(gdb) b mprotect.c:11
Breakpoint 3 at 0x7fe00055d4c6: file src/mman/mprotect.c, line 11.
(gdb) b lkl_sys
lkl_sys_access lkl_sys_fstat lkl_sys_mmap2 lkl_sys_umount
lkl_sys_chdir lkl_sys_fstatat lkl_sys_mount lkl_sys_unlink
lkl_sys_chmod lkl_sys_getpid lkl_sys_newfstatat lkl_sys_unlinkat
lkl_sys_chroot lkl_sys_halt lkl_sys_open lkl_sys_virtio_mmio_device_add
lkl_sys_clock_settime lkl_sys_mkdir lkl_sys_openat lkl_syscall
lkl_sys_close lkl_sys_mkdirat lkl_sys_read lkl_sysctl
lkl_sys_faccessat lkl_sys_mknod lkl_sys_rmdir lkl_sysctl_parse_write
lkl_sys_fchmodat lkl_sys_mknodat lkl_sys_stat
(gdb) b lkl_sysc
lkl_syscall lkl_sysctl lkl_sysctl_parse_write
(gdb) b lkl_syscall
Breakpoint 4 at 0x7fe000088df0: file arch/lkl/kernel/syscalls.c, line 132.
(gdb) b run_
run_cmd run_init_process run_irq run_ksoftirqd run_pages_job run_timer_softirq
run_complete_job run_io_job run_irqs run_local_timers run_posix_cpu_timers run_timer_softirq.part
(gdb) b run_
run_cmd run_init_process run_irq run_ksoftirqd run_pages_job run_timer_softirq
run_complete_job run_io_job run_irqs run_local_timers run_posix_cpu_timers run_timer_softirq.part
(gdb) b syscalls.c:47
Breakpoint 5 at 0x7fe000088da0: file arch/lkl/kernel/syscalls.c, line 47.
(gdb) b syscalls.c:273
Breakpoint 6 at 0x7fe000088f0b: file arch/lkl/kernel/syscalls.c, line 273.
(gdb) b sgxlkl_host_syscall_mprotect
Breakpoint 7 at 0x40016a34: sgxlkl_host_syscall_mprotect. (2 locations)
(gdb) c
Continuing.
Thread 6 "ENCLAVE" hit Breakpoint 4, lkl_syscall (no=226, params=0x7fe040afee20) at arch/lkl/kernel/syscalls.c:132
132 {
(gdb) c
Continuing.
Thread 6 "ENCLAVE" hit Breakpoint 7, sgxlkl_host_syscall_mprotect (_retval=0x7fe040afed98, addr=0x0, len=8192, prot=1) at enclave/sgxlkl_t.c:776
776 oe_result_t _result = OE_FAILURE;
(gdb) c
Continuing.
Thread 6 "ENCLAVE" hit Breakpoint 7, sgxlkl_host_syscall_mprotect (addr=0x0, len=8192, prot=1) at host_interface/host_interface_calls.c:94
94 return mprotect(addr, len, prot);
(gdb) c
Continuing.
Thread 6 "ENCLAVE" hit Breakpoint 2, mprotect () at ../sysdeps/unix/syscall-template.S:78
78 ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) c
Continuing.
Thread 6 "ENCLAVE" hit Breakpoint 6, lkl_syscall (no=226, params=0x0) at arch/lkl/kernel/syscalls.c:274
274 }
(gdb) p ret
$1 = 4294967295
(gdb) p errno
$2 = 0
(gdb) s
__filter_syscall3 (n=226, a1=0, a2=8192, a3=1) at ./src/internal/syscall.h:83
83 return res;
(gdb) p res
$3 = 4294967295
(gdb) p errno
$4 = 0
(gdb) s
84 }
(gdb) s
__syscall_ret (r=4294967295) at src/internal/syscall_ret.c:6
6 if (r > -4096UL) {
(gdb) p r
$5 = 4294967295
(gdb) p -4096UL
$6 = 18446744073709547520
(gdb) s
10 return r;
(gdb) s
11 }
(gdb) s