Files
semi-libc/src/ipc/semtimedop.c
T
Alexey Izbyshev 9b12982d52 semtimedop: fix timespec kernel ABI mismatch for 32-bit timeouts on x32
For time64 support, musl normally defines SYS_foo to the time32 variant
of that syscall on arches that have it, and to the time64 variant
otherwise, so that "SYS_foo == SYS_foo_time64" implies that the arch is
time64-only. However, SYS_semtimedop is an odd case: some arches define
only SYS_semtimedop_time64, yet they are not time64-only, because the
time32 variant is provided via SYS_ipc instead. For such arches,
defining SYS_semtimedop to SYS_semtimedop_time64 would break the
implication above, so commit 4bbd7baea7
doesn't do this. Commit eb2e298cdc
attempts to detect time64-only arches by checking that both
SYS_semtimedop and SYS_ipc are undefined, but this doesn't work for
x32, because it's a time64-only arch that does define SYS_semtimedop.
As a result, 32-bit timeouts trigger the fallback path that passes
a 32-bit timespec to the kernel while it expects a 64-bit one, so
the effective tv_sec is formed by interpreting 32-bit tv_sec and
tv_nsec as a single long long, and the effective tv_nsec is whatever
is located in the next 64 bits of the stack.

Fix this by expanding the time64-only check to include arches where
SYS_semtimedop is the time64 variant of the syscall.
2023-04-11 09:21:41 -04:00

37 lines
999 B
C

#define _GNU_SOURCE
#include <sys/sem.h>
#include <errno.h>
#include "syscall.h"
#include "ipc.h"
#define IS32BIT(x) !((x)+0x80000000ULL>>32)
#define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63))
#if !defined(SYS_semtimedop) && !defined(SYS_ipc) || \
SYS_semtimedop == SYS_semtimedop_time64
#define NO_TIME32 1
#else
#define NO_TIME32 0
#endif
int semtimedop(int id, struct sembuf *buf, size_t n, const struct timespec *ts)
{
#ifdef SYS_semtimedop_time64
time_t s = ts ? ts->tv_sec : 0;
long ns = ts ? ts->tv_nsec : 0;
int r = -ENOSYS;
if (NO_TIME32 || !IS32BIT(s))
r = __syscall(SYS_semtimedop_time64, id, buf, n,
ts ? ((long long[]){s, ns}) : 0);
if (NO_TIME32 || r!=-ENOSYS) return __syscall_ret(r);
ts = ts ? (void *)(long[]){CLAMP(s), ns} : 0;
#endif
#if defined(SYS_ipc)
return syscall(SYS_ipc, IPCOP_semtimedop, id, n, 0, buf, ts);
#elif defined(SYS_semtimedop)
return syscall(SYS_semtimedop, id, buf, n, ts);
#else
return __syscall_ret(-ENOSYS);
#endif
}