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

The musl pwrite(3) implementation uses the split-offset convention for 
syscall param layout:

     __syscall_cp(SYS_pwritev2, fd, iov, count, ofs_lo, ofs_hi, flags)

Unfortunately, the x32 kernel syscall ABI takes just one param for offset:

     __syscall_cp(SYS_pwritev2, fd, iov, count, ofs, flags)

Therefore on x32 the pwrite(3) unistd function is incorrect. Same 
applies to pwritev(3)/preadv(3) in unistd and the pwritev2(2)/preadv2(2) 
syscall wrappers.

---
v2: remove #ifdef guards, because x32 always has preadv(2) and 
pwritev(2) syscalls

  src/linux/x32/preadv2.c  | 13 +++++++++++++
  src/linux/x32/pwritev2.c | 13 +++++++++++++
  src/unistd/x32/preadv.c  |  9 +++++++++
  src/unistd/x32/pwrite.c  | 18 ++++++++++++++++++
  src/unistd/x32/pwritev.c | 16 ++++++++++++++++
  5 files changed, 69 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..aea2c4a
--- /dev/null
+++ b/src/linux/x32/preadv2.c
@@ -0,0 +1,13 @@
+#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)
+{
+	if (!flags) {
+		if (ofs==-1) return readv(fd, iov, count);
+		return syscall_cp(SYS_preadv, fd, iov, count, ofs);
+	}
+	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..2d6ea5c
--- /dev/null
+++ b/src/linux/x32/pwritev2.c
@@ -0,0 +1,13 @@
+#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)
+{
+	if (!flags) {
+		if (ofs==-1) return writev(fd, iov, count);
+		return syscall_cp(SYS_pwritev, fd, iov, count, ofs);
+	}
+	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..c2b3982
--- /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, __SYSCALL_LL_PRW(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.