Skip to contentSkip to conversation

fs: compat select/pselect for x32 arch

RFC PATCH Superseded Lance Richardson 1 participant 1 message Feb 23, 2018

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 fs: compat select/pselect for x32 arch

The definition of fd_set in X32 ABI user-space uses a 32-bit base data type for the fd array while the kernel uses a 64-bit base data type. For applications using the glibc implementation of select(2)/pselect(2), the size of fd_set is an integer multiple of both base types, so there is no issue.

For applications using fd_set sizes different from the glibc default size, we can have an overrun of the user-space fd_set buffer when the user-space buffer size is an odd multiple of 4 bytes (e.g. user-space can pass a 12-byte fd_set to the kernel and the kernel will copy 16 bytes to user-space before returning from select/pselect system calls). OpenSSH is one example of an application using fd_set sizes different from the default.

Since these system calls need 32-bit fd_set definitions and 64-bit struct timeval, struct timespec, and sigset_t, we cannot use the existing compat functions. Add new x32-specific compat implementations for these system calls.

Note that existing versions of glibc will continue to use the x86_64 select/pselect entry point, so backwards ABI compatibility should be preserved.

Reproducer (compiled with gcc -mx32):

int test(int nfds)

{
    fd_set *fds;
    struct timeval tv = {0, 0};

    printf("Calling select with nfds = %d\n", nfds);
    fds = calloc(howmany(nfds, __NFDBITS), sizeof(__fd_mask));
    select(nfds, fds, NULL, NULL, &tv);
    free(fds);
    printf("Success\n");

    return 0;
}

int main(int argc, char **argv)

{
    mcheck_pedantic(NULL);

    test(64);
    test(65);
    return 0;
}

/* Expected output without this patch:
*
* Calling select with nfds = 64
* Success
* Calling select with nfds = 65
* memory clobbered past end of allocated block
* Aborted (core dumped)
*/

---
Some questions:
- Are compat_sys_x32_select/compat_sys_x32_pselect6 OK names?
- This code is (afaik) only applicable to a single arch, is it appropriate for a common file?
- Are there other lists/individuals this should be copied to?

 arch/x86/entry/syscalls/syscall_64.tbl |  6 ++-
 fs/select.c                            | 91 ++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+), 2 deletions(-)
arch/x86/entry/syscalls/syscall_64.tbl +4 -2
index 5aef183e2f85..de637336ed10 100644
@@ -29,7 +29,7 @@
 20	64	writev			sys_writev
 21	common	access			sys_access
 22	common	pipe			sys_pipe
-23	common	select			sys_select
+23	64	select			sys_select
 24	common	sched_yield		sys_sched_yield
 25	common	mremap			sys_mremap
 26	common	msync			sys_msync
@@ -276,7 +276,7 @@
 267	common	readlinkat		sys_readlinkat
 268	common	fchmodat		sys_fchmodat
 269	common	faccessat		sys_faccessat
-270	common	pselect6		sys_pselect6
+270	64	pselect6		sys_pselect6
 271	common	ppoll			sys_ppoll
 272	common	unshare			sys_unshare
 273	64	set_robust_list		sys_set_robust_list
@@ -380,3 +380,5 @@
 545	x32	execveat		compat_sys_execveat/ptregs
 546	x32	preadv2			compat_sys_preadv64v2
 547	x32	pwritev2		compat_sys_pwritev64v2
+548	x32	select			compat_sys_x32_select
+549	x32	pselect6		compat_sys_x32_pselect6
fs/select.c +91 -0

103 lines not on this page · raw message

Signature · 1 line
2.14.1