Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID:
 <TY4P301MB1554EB14DAC96184636C48E5EE7C2@TY4P301MB1554.JPNP301.PROD.OUTLOOK.COM>
Date: Mon, 16 Nov 2026 09:56:11 +0800
From: Curtis Wu <alr_public@...mail.com>
To: musl@...ts.openwall.com
Subject: [PATCH] x32: fix syscall param layouts for pwrite/pwritev/preadv

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.

Take pwrite(3) as example. When you pass ofs=5GB(0x140000000) as 
argument, unfixed musl would split it into ofs_lo and ofs_hi, which 
would be regarded by the kernel as ofs and flags respectively. So a 
5GB-positioned write would mistakenly happen on offset 1GB(0x40000000).

Fix by adding x32-specific implementations.

This is found when porting e2fsprogs to x32. I didn't even fathom why 
kernel would bail with errors on a freshly made filesystem by mkfs.ext2, 
until I found this bug.

---
  src/linux/x32/preadv2.c  | 15 +++++++++++++++
  src/linux/x32/pwritev2.c | 15 +++++++++++++++
  src/unistd/x32/preadv.c  |  9 +++++++++
  src/unistd/x32/pwrite.c  | 18 ++++++++++++++++++
  src/unistd/x32/pwritev.c | 16 ++++++++++++++++
  5 files changed, 73 insertions(+)
  create mode 100644 src/linux/x32/preadv2.c
  create mode 100644 src/linux/x32/pwritev2.c
  create mode 100644 src/unistd/x32/preadv.c
  create mode 100644 src/unistd/x32/pwrite.c
  create mode 100644 src/unistd/x32/pwritev.c

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
+	if (!flags) {
+		if (ofs==-1) return readv(fd, iov, count);
+		return syscall_cp(SYS_preadv, fd, iov, count, ofs);
+	}
+#endif
+	return syscall_cp(SYS_preadv2, fd, iov, count, ofs, flags);
+}
diff --git a/src/linux/x32/pwritev2.c b/src/linux/x32/pwritev2.c
new file mode 100644
index 0000000..4855b71
--- /dev/null
+++ b/src/linux/x32/pwritev2.c
@@ -0,0 +1,15 @@
+#define _GNU_SOURCE
+#include <sys/uio.h>
+#include <unistd.h>
+#include "syscall.h"
+
+ssize_t pwritev2(int fd, const struct iovec *iov, int count, off_t ofs, 
int flags)
+{
+#ifdef SYS_pwritev
+	if (!flags) {
+		if (ofs==-1) return writev(fd, iov, count);
+		return syscall_cp(SYS_pwritev, fd, iov, count, ofs);
+	}
+#endif
+	return syscall_cp(SYS_pwritev2, fd, iov, count, ofs, flags);
+}
diff --git a/src/unistd/x32/preadv.c b/src/unistd/x32/preadv.c
new file mode 100644
index 0000000..d3c09c9
--- /dev/null
+++ b/src/unistd/x32/preadv.c
@@ -0,0 +1,9 @@
+#define _BSD_SOURCE
+#include <sys/uio.h>
+#include <unistd.h>
+#include "syscall.h"
+
+ssize_t preadv(int fd, const struct iovec *iov, int count, off_t ofs)
+{
+	return syscall_cp(SYS_preadv, fd, iov, count, ofs);
+}
diff --git a/src/unistd/x32/pwrite.c b/src/unistd/x32/pwrite.c
new file mode 100644
index 0000000..a57b815
--- /dev/null
+++ b/src/unistd/x32/pwrite.c
@@ -0,0 +1,18 @@
+#define _GNU_SOURCE
+#include <unistd.h>
+#include <sys/uio.h>
+#include <fcntl.h>
+#include "syscall.h"
+
+ssize_t pwrite(int fd, const void *buf, size_t size, off_t ofs)
+{
+	if (ofs == -1) ofs--;
+	int r = __syscall_cp(SYS_pwritev2, fd,
+		(&(struct iovec){ .iov_base = (void *)buf, .iov_len = size }),
+		1, ofs, RWF_NOAPPEND);
+	if (r != -EOPNOTSUPP && r != -ENOSYS)
+		return __syscall_ret(r);
+	if (fcntl(fd, F_GETFL) & O_APPEND)
+		return __syscall_ret(-EOPNOTSUPP);
+	return syscall_cp(SYS_pwrite, fd, buf, size, ofs);
+}
diff --git a/src/unistd/x32/pwritev.c b/src/unistd/x32/pwritev.c
new file mode 100644
index 0000000..9bdc0c4
--- /dev/null
+++ b/src/unistd/x32/pwritev.c
@@ -0,0 +1,16 @@
+#define _GNU_SOURCE
+#include <sys/uio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include "syscall.h"
+
+ssize_t pwritev(int fd, const struct iovec *iov, int count, off_t ofs)
+{
+	if (ofs == -1) ofs--;
+	int r = __syscall_cp(SYS_pwritev2, fd, iov, count, ofs, RWF_NOAPPEND);
+	if (r != -EOPNOTSUPP && r != -ENOSYS)
+		return __syscall_ret(r);
+	if (fcntl(fd, F_GETFL) & O_APPEND)
+		return __syscall_ret(-EOPNOTSUPP);
+	return syscall_cp(SYS_pwritev, fd, iov, count, ofs);
+}
-- 
2.47.3

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.