Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID:
 <TY4P301MB1554F56F20B3BFFFD43E0915EE4D2@TY4P301MB1554.JPNP301.PROD.OUTLOOK.COM>
Date: Wed, 2 Dec 2026 07:07:41 +0800
From: Curtis Wu <alr_public@...mail.com>
To: Rich Felker <dalias@...c.org>, musl@...ts.openwall.com
Subject: Re: [PATCH] x32: fix syscall param layouts for
 pwrite/pwritev/preadv

On 11/17/26 07:38, Rich Felker wrote:
> On Mon, Nov 16, 2026 at 05:43:41PM +0100, Szabolcs Nagy wrote:
>> * Curtis Wu <alr_public@...mail.com> [2026-11-16 09:56:11 +0800]:
>>> The pwrite(3) implementation in musl libc, in the general case, uses the
>>> split-offset convention for param layout:
>>>
>>>       __syscall_cp(SYS_pwritev2, fd, iov, count, ofs_lo, ofs_hi, flags)
>>>
>>> This applies even on 64-bit architectures such as x86_64. Unfortunately, x32
>>> doesn't follow such convention, and x32 kernel syscall ABI takes instead
>>> just one syscall param for offset, routed to compat_sys_pwritev64v2() in
>>> kernel:
>>>
>>>      __syscall_cp(SYS_pwritev2, fd, iov, count, ofs, flags)
>>>
>>> Therefore the pwrite(3) unistd function is incorrect on x32. Same applies to
>>> pwritev(3)/preadv(3) in unistd/ and the pwritev2(2)/preadv2(2) syscall
>>> wrappers.
>>>
>>
>> i can confirm this and that x32 is special
>> https://github.com/torvalds/linux/blob/v7.3/arch/x86/include/asm/unistd.h#L29
>> https://github.com/torvalds/linux/blob/v7.3/fs/read_write.c#L1280
> 
> It looks like the source of the breakage is commit
> 4ee5c0d05ce9b4e48e586a1ee168f166d191ddda, wherein the author thought
> the native 64-bit preadv/pwritev used single 64-bit arguments and was
> trying to make x32 follow same convention.
> 
> We should check if any other 32-bit-ABI-on=64-bit archs are affected,
> like mips n32.

Hi,

I checked the Linux source.

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/mips/kernel/syscalls/syscall_n32.tbl?id=19cc08c1a6a650f8e54c0461ff198be6448f425d#n304
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/read_write.c?id=19cc08c1a6a650f8e54c0461ff198be6448f425d#n1210

On mipsn32, preadv(2) is routed to kernel function compact_sys_preadv(), 
which is defined using the split-offset convention. 
pwritev(2)/preadv2(2)/pwritev2(2) likewise. This is akin to other archs 
in musl.

Only x32 is special in the preadv(2)/pwritev(2)/preadv2(2)/pwritev2(2) 
case, in the two 64-bit ILP32 archs that musl supports.

> I really dislike copying whole source files to x32-specific versions
> like this, since they could easily be overlooked when making future
> changes. But I also dislike adding hacks specific to one bogus arch to
> a shared file. If there are other archs affected tho, the choice to
> put the conditional logic in a shared file would be more clear.
> 
> It might make sense to remove the offending syscall macros on x32 and
> replace them with the 64-suffixed ones like the kernel uses:
> 
> -__NR_preadv
> -__NR_pwritev
> -__NR_preadv2
> -__NR_pwritev2
> +__NR_preadv64
> +__NR_pwritev64
> +__NR_preadv64v2
> +__NR_pwritev64v2
> 
> Then the shared file could use the alternate argument convention when
> these are present.

That makes sense. Are we to use the following dispatch? The musl source 
tree has precedents of this idiom in, for example, 
src/linux/sync_file_range.c.

#if defined(SYS_preadv)
     return syscall(SYS_preadv, /* ... */)
#elif defined(SYS_preadv64)
     return syscall(SYS_preadv64, /* ... */)
#else
     return __syscall_ret(-ENOSYS)
#endif

But there is only one arch impacted. If changing the syscall dispatch 
logic of the 4 syscalls for just one arch is an overkill, could we use 
an arch-specific macro, like __SYSCALL_LL_PRWV/PRWV2(ofs) instead? On 
x32 this should be a single ofs, while on other archs this is supposed 
to be split.

(There is already an override for sh in __SYSCALL_LL_PRW(x), which is 
specially defined.)

>>> diff --git a/src/linux/x32/preadv2.c b/src/linux/x32/preadv2.c
>>> new file mode 100644
>>> index 0000000..11e368c
>>> --- /dev/null
>>> +++ b/src/linux/x32/preadv2.c
>>> @@ -0,0 +1,15 @@
>>> +#define _GNU_SOURCE
>>> +#include <sys/uio.h>
>>> +#include <unistd.h>
>>> +#include "syscall.h"
>>> +
>>> +ssize_t preadv2(int fd, const struct iovec *iov, int count, off_t ofs, int
>>> flags)
>>> +{
>>> +#ifdef SYS_preadv
>>
>> not sure why this #ifdef exists in the generic code,
>> maybe for future targets. in any case x32 has preadv.
> 
> Indeed if we do go with x32-specific fork of the code, this #ifdef
> doesn't need to be in the x32 version. It's purely for future archs
> which I'd expect to lack the old syscall.

Agreed. I was being conservative when submitting v1.

> Rich

(Sorry for the double post, my email client was muddling with the reply 
addresses.)

Curtis

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.