ipc/msg: prevent msg_qbytes limit bypass in IPC_SET
Subsystems (2)
PATCH v2 1/1 ipc/msg: prevent msg_qbytes limit bypass in IPC_SET
The msg_qbytes field in the msqid64_ds userspace ABI is an unsigned long. Commit 889b331724c82 ("ipc/msg.c: consolidate all xxxctl_down() functions") passed that value to msgctl_down() as an int.
An unprivileged owner can set msg_qbytes to UINT_MAX. The narrowing conversion produces -1 on supported Linux targets, so the check against the int-valued msg_ctlmnb sysctl does not reject the request:
if (msg_qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
The value is then assigned to the unsigned long q_qbytes field, setting it to ULONG_MAX. This bypasses the CAP_SYS_RESOURCE requirement for increasing a queue above MSGMNB.
It also bypasses the effective per-queue message-count limit: zero-length messages consume kernel memory but not queue bytes. An unprivileged queue owner can therefore cause memory pressure or exhaustion beyond the configured per-queue allowance, subject to other resource limits.
Keep msg_qbytes as unsigned long through validation and assignment. Also preserve its type in ipcctl_obtain_check(), which supplies it to the unsigned-long audit field.
Add a regression test which drops CAP_SYS_RESOURCE and verifies that IPC_SET with UINT_MAX fails with EPERM. Skip the test when capabilities or System V message queues are unavailable.
---
Changes in v2:
- Skip the selftest when capability syscalls or System V message queues are unavailable.
- Describe the queued-message-limit and memory-pressure impact.
Link to v1: https://lore.kernel.org/r/[email protected]
--- ipc/msg.c | 2 +- ipc/util.c | 3 +- ipc/util.h | 3 +- tools/testing/selftests/ipc/Makefile | 2 +- tools/testing/selftests/ipc/msgctl_qbytes.c | 81 +++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 4 deletions(-)
ipc/msg.c +1 -1
@@ -399,7 +399,7 @@ copy_msqid_from_user(struct msqid64_ds *out, void __user *buf, int version)* NOTE: no locks must be held, the rwsem is taken inside this function. */ static int msgctl_down (struct ipc_namespace *ns,int msqid,int cmd, -struct ipc64_perm *perm,int msg_qbytes) +struct ipc64_perm *perm,unsigned long msg_qbytes) {struct kern_ipc_perm *ipcp;struct msg_queue *msq;
ipc/util.c +2 -1
@@ -718,7 +718,8 @@ int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)*/ struct kern_ipc_perm *ipcctl_obtain_check (struct ipc_namespace *ns,struct ipc_ids *ids,int id,int cmd, -struct ipc64_perm *perm,int extra_perm) +struct ipc64_perm *perm, +unsigned long extra_perm) {kuid_t euid;int err = -EPERM ;
ipc/util.h +2 -1
@@ -180,7 +180,8 @@ void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);int ipc_update_perm (struct ipc64_perm *in,struct kern_ipc_perm *out);struct kern_ipc_perm *ipcctl_obtain_check (struct ipc_namespace *ns,struct ipc_ids *ids,int id,int cmd, -struct ipc64_perm *perm,int extra_perm); +struct ipc64_perm *perm, +unsigned long extra_perm);static inline void ipc_update_pid (struct pid **pos,struct pid *pid) {
tools/testing/selftests/ipc/Makefile +1 -1
@@ -12,7 +12,7 @@ endif CFLAGS += $(KHDR_INCLUDES) -TEST_GEN_PROGS := msgque +TEST_GEN_PROGS := msgctl_qbytes msgque include ../lib.mk
tools/testing/selftests/ipc/msgctl_qbytes.c new file +81 -0
81 lines not on this page · raw message
---
base-commit: 8a36f53fd5ec3eff01f1b637d42cbdc3821d0493
change-id: 20261027-ipc-msg-qbytes-49b15931c5b6
Best regards,
Signature · 1 line
Shubhang Kaushik (Ampere) <[email protected]>
Quoted 20 lines · On Tue, 27 Oct 2026 23:42:51 -0700 "Shubhang Kaushik (Ampere)" <[email protected]>…
The
msg_qbytesfield in themsqid64_dsuserspace ABI is an unsigned long. Commit889b331724c82("ipc/msg.c: consolidate allxxxctl_down()functions") passed that value tomsgctl_down()as an int.An unprivileged owner can set
msg_qbytestoUINT_MAX. The narrowing conversion produces -1 on supported Linux targets, so the check against the int-valuedmsg_ctlmnbsysctl does not reject the request:if (msg_qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))The value is then assigned to the unsigned long
q_qbytesfield, setting it toULONG_MAX. This bypasses theCAP_SYS_RESOURCErequirement for increasing a queue above MSGMNB.It also bypasses the effective per-queue message-count limit: zero-length messages consume kernel memory but not queue bytes. An unprivileged queue owner can therefore cause memory pressure or exhaustion beyond the configured per-queue allowance, subject to other resource limits.
Quoted 7 lines
Keep
msg_qbytesas unsigned long through validation and assignment. Also preserve its type inipcctl_obtain_check(), which supplies it to the unsigned-long audit field.Add a regression test which drops
CAP_SYS_RESOURCEand verifies thatIPC_SETwithUINT_MAXfails with EPERM. Skip the test when capabilities or System V message queues are unavailable.
Thanks. Sashiko AI review asked a thing: