Skip to contentSkip to conversation

ipc/msg: align compat version-0 msqid_ds with native IPC_OLD

PATCH In review Cheng Yongkang 3 participants 5 messages Oct 29 – Nov 21 Stable branch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipc/msg: align compat version-0 msqid_ds with native IPC_OLD

The compat version-0 IPC_STAT path copies the 64-bit counters into the 16-bit msg_cbytes/msg_qnum/msg_qbytes fields without clamping, and leaves msg_lcbytes/msg_lqbytes zero. A queue with 65536 bytes, 8 messages and q_qbytes=200000 is reported as cbytes=0, qnum=8, qbytes=3392, lcbytes=lqbytes=0, while the native IPC_OLD path reports the clamped counters and the full values in the l fields.

The compat version-0 IPC_SET path reads only the 16-bit msg_qbytes and ignores msg_lqbytes. A caller following the old ABI with msg_qbytes=0 and msg_lqbytes=131072 gets q_qbytes=0 and the queue stops accepting messages, while the native IPC_OLD path falls back to msg_lqbytes.

Mirror the native IPC_OLD handling in both compat version-0 branches:
clamp the counters to USHRT_MAX and fill the l fields on stat, and fall back to msg_lqbytes when the 16-bit qbytes is zero on set. The compat IPC_64 branches and the native paths are unchanged.

Fixes 4693916846269d ("msgctl(): move compat to native")
Assisted-by LLM
---
 ipc/msg.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)
ipc/msg.c +25 -4
index 62996b9..71ededc 100644
@@ -686,10 +686,17 @@ static int copy_compat_msqid_from_user(struct msqid64_ds *out, void __user *buf,
 			return -EFAULT;
 	} else {
 		struct compat_msqid_ds __user *p = buf;
+		compat_ulong_t qbytes;
+
 		if (get_compat_ipc_perm(&out->msg_perm, &p->msg_perm))
 			return -EFAULT;
-		if (get_user(out->msg_qbytes, &p->msg_qbytes))
+		if (get_user(qbytes, &p->msg_qbytes))
 			return -EFAULT;
+		if (qbytes == 0) {
+			if (get_user(qbytes, &p->msg_lqbytes))
+				return -EFAULT;
+		}
+		out->msg_qbytes = qbytes;
 	}
 	return 0;
 }
@@ -720,9 +727,23 @@ static int copy_compat_msqid_to_user(void __user *buf, struct msqid64_ds *in,
 		v.msg_stime = in->msg_stime;
 		v.msg_rtime = in->msg_rtime;
 		v.msg_ctime = in->msg_ctime;
-		v.msg_cbytes = in->msg_cbytes;
-		v.msg_qnum = in->msg_qnum;
-		v.msg_qbytes = in->msg_qbytes;
+		if (in->msg_cbytes > USHRT_MAX)
+			v.msg_cbytes = USHRT_MAX;
+		else
+			v.msg_cbytes = in->msg_cbytes;
+		v.msg_lcbytes = in->msg_cbytes;
+
+		if (in->msg_qnum > USHRT_MAX)
+			v.msg_qnum = USHRT_MAX;
+		else
+			v.msg_qnum = in->msg_qnum;
+
+		if (in->msg_qbytes > USHRT_MAX)
+			v.msg_qbytes = USHRT_MAX;
+		else
+			v.msg_qbytes = in->msg_qbytes;
+		v.msg_lqbytes = in->msg_qbytes;
+
 		v.msg_lspid = in->msg_lspid;
 		v.msg_lrpid = in->msg_lrpid;
 		return copy_to_user(buf, &v, sizeof(v));
Signature · 1 line
2.34.1
#1 lore raw
Quoted 12 lines · On Thu, 29 Oct 2026 07:12:20 -0700 Cheng Yongkang <[email protected]> wrote:

The compat version-0 IPC_STAT path copies the 64-bit counters into the 16-bit msg_cbytes/msg_qnum/msg_qbytes fields without clamping, and leaves msg_lcbytes/msg_lqbytes zero. A queue with 65536 bytes, 8 messages and q_qbytes=200000 is reported as cbytes=0, qnum=8, qbytes=3392, lcbytes=lqbytes=0, while the native IPC_OLD path reports the clamped counters and the full values in the l fields.

The compat version-0 IPC_SET path reads only the 16-bit msg_qbytes and ignores msg_lqbytes. A caller following the old ABI with msg_qbytes=0 and msg_lqbytes=131072 gets q_qbytes=0 and the queue stops accepting messages, while the native IPC_OLD path falls back to msg_lqbytes.

Quoted 5 lines

Mirror the native IPC_OLD handling in both compat version-0 branches:
clamp the counters to USHRT_MAX and fill the l fields on stat, and fall back to msg_lqbytes when the 16-bit qbytes is zero on set. The compat IPC_64 branches and the native paths are unchanged.

Thanks. I had LLM poke at this and it had things to say:

: The conversion changes look right, but the Fixes tag needs correcting. : 4693916846269d only moved the compat code. Both omissions are already : present in 1da177e4c3f4 ("Linux-2.6.12-rc2"), the initial Git import. : Please drop the incorrect attribution; the actual introduction predates : that history. Keep the stable CC.
:
: Please also coordinate this with the unsigned-long msgctl_down() fix : from Shubhang Kaushik's "[PATCH v2] ipc/msg: prevent msg_qbytes limit : bypass in IPC_SET".
: (https://lore.kernel.org/[email protected])
: On the current base, your new fallback lets a
: privileged old-ABI caller supply msg_lqbytes=UINT_MAX, but msgctl_down() : still narrows that to int and installs ULONG_MAX on a 64-bit kernel. : That downstream bug already affects other interfaces; this patch makes : it reachable through the repaired fallback too.
:
: The original bugs give legacy 32-bit programs wrapped queue statistics : or unexpectedly set the queue capacity to zero. Subsequent sends then : block or return EAGAIN with IPC_NOWAIT, even for zero-length messages. : The ordinary capacity permission checks still apply to the long value.

Quoted 50 lines
ipc/msg.c +25 -4
@@ -686,10 +686,17 @@ static int copy_compat_msqid_from_user(struct msqid64_ds *out, void __user *buf,
 			return -EFAULT;
 	} else {
 		struct compat_msqid_ds __user *p = buf;
+		compat_ulong_t qbytes;
+
 		if (get_compat_ipc_perm(&out->msg_perm, &p->msg_perm))
 			return -EFAULT;
-		if (get_user(out->msg_qbytes, &p->msg_qbytes))
+		if (get_user(qbytes, &p->msg_qbytes))
 			return -EFAULT;
+		if (qbytes == 0) {
+			if (get_user(qbytes, &p->msg_lqbytes))
+				return -EFAULT;
+		}
+		out->msg_qbytes = qbytes;
 	}
 	return 0;
 }
@@ -720,9 +727,23 @@ static int copy_compat_msqid_to_user(void __user *buf, struct msqid64_ds *in,
 		v.msg_stime = in->msg_stime;
 		v.msg_rtime = in->msg_rtime;
 		v.msg_ctime = in->msg_ctime;
-		v.msg_cbytes = in->msg_cbytes;
-		v.msg_qnum = in->msg_qnum;
-		v.msg_qbytes = in->msg_qbytes;
+		if (in->msg_cbytes > USHRT_MAX)
+			v.msg_cbytes = USHRT_MAX;
+		else
+			v.msg_cbytes = in->msg_cbytes;
+		v.msg_lcbytes = in->msg_cbytes;
+
+		if (in->msg_qnum > USHRT_MAX)
+			v.msg_qnum = USHRT_MAX;
+		else
+			v.msg_qnum = in->msg_qnum;
+
+		if (in->msg_qbytes > USHRT_MAX)
+			v.msg_qbytes = USHRT_MAX;
+		else
+			v.msg_qbytes = in->msg_qbytes;
+		v.msg_lqbytes = in->msg_qbytes;
+
 		v.msg_lspid = in->msg_lspid;
 		v.msg_lrpid = in->msg_lrpid;
 		return copy_to_user(buf, &v, sizeof(v));
Signature · 1 line
2.34.1
#2 lore raw

Hi Andrew,

Thanks for the review.

I'll fix the Fixes tag: 4693916846269d only moved the compat code, so I'll drop that attribution. Both omissions are present from the initial import (1da177e4c3f4) and predate git history. I'll keep the stable Cc.

For the dependency, I'll hold the v2 and rebase it on Shubhang's unsigned-long msgctl_down() fix once that lands, so the repaired compat fallback cannot reach the int truncation. If you would rather queue them together, I can send the v2 on top of his patch with an explicit dependency note.

Shubhang, please keep the unsigned-long correction in your respin.

Thanks,
Cheng Yongkang

#3 lore raw

Hi Cheng Yongkang,

Quoted 14 lines · Cheng Yongkang

Hi Andrew,

Thanks for the review.

I'll fix the Fixes tag: 4693916846269d only moved the compat code, so I'll drop that attribution. Both omissions are present from the initial import (1da177e4c3f4) and predate git history. I'll keep the stable Cc.

For the dependency, I'll hold the v2 and rebase it on Shubhang's unsigned-long msgctl_down() fix once that lands, so the repaired compat fallback cannot reach the int truncation. If you would rather queue them together, I can send the v2 on top of his patch with an explicit dependency note.

Quoted 5 lines

Shubhang, please keep the unsigned-long correction in your respin.

Thanks,
Cheng Yongkang

The unsigned-long msg_qbytes correction is retained in v3:

https://lore.kernel.org/lkml/[email protected]/

Thanks,
Shubhang Kaushik

tree notice
#4 lore raw

+ ipc-msg-align-compat-version-0-msqid_ds-with-native-ipc_old.patch added to mm-nonmm-unstable branch

In reply to Cheng Yongkang
The patch titled Subject: ipc/msg: align compat version-0 msqid_ds with native IPC_OLD has been added to the -mm mm-nonmm-unstable branch. Its filename is…

The patch titled
Subject: ipc/msg: align compat version-0 msqid_ds with native IPC_OLD
has been added to the -mm mm-nonmm-unstable branch. Its filename is
ipc-msg-align-compat-version-0-msqid_ds-with-native-ipc_old.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/ipc-msg-align-compat-version-0-msqid_ds-with-native-ipc_old.patch

This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Cheng Yongkang <[email protected]>
Subject: ipc/msg: align compat version-0 msqid_ds with native IPC_OLD
Date: Thu, 29 Oct 2026 07:12:20 -0700

The compat version-0 IPC_STAT path copies the 64-bit counters into the 16-bit msg_cbytes/msg_qnum/msg_qbytes fields without clamping, and leaves msg_lcbytes/msg_lqbytes zero. A queue with 65536 bytes, 8 messages and q_qbytes=200000 is reported as cbytes=0, qnum=8, qbytes=3392, lcbytes=lqbytes=0, while the native IPC_OLD path reports the clamped counters and the full values in the l fields.

The compat version-0 IPC_SET path reads only the 16-bit msg_qbytes and ignores msg_lqbytes. A caller following the old ABI with msg_qbytes=0 and msg_lqbytes=131072 gets q_qbytes=0 and the queue stops accepting messages, while the native IPC_OLD path falls back to msg_lqbytes.

Mirror the native IPC_OLD handling in both compat version-0 branches:
clamp the counters to USHRT_MAX and fill the l fields on stat, and fall back to msg_lqbytes when the 16-bit qbytes is zero on set. The compat IPC_64 branches and the native paths are unchanged.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc Christopher Lameter (Ampere) <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Jan Koester <[email protected]>
Cc Lu Shuaibing <[email protected]>
Cc Manfred Spraul <[email protected]>
Cc Shuah Khan <[email protected]>
Cc "Shubhang Kaushik (Ampere)" <[email protected]>
Cc Shubhang Kaushik <[email protected]>
 ipc/msg.c |   29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)
ipc/msg.c renamed from ipc/msg.c~ipc-msg-align-compat-version-0-msqid_ds-with-native-ipc_old +25 -4
@@ -687,10 +687,17 @@ static int copy_compat_msqid_from_user(s
 			return -EFAULT;
 	} else {
 		struct compat_msqid_ds __user *p = buf;
+		compat_ulong_t qbytes;
+
 		if (get_compat_ipc_perm(&out->msg_perm, &p->msg_perm))
 			return -EFAULT;
-		if (get_user(out->msg_qbytes, &p->msg_qbytes))
+		if (get_user(qbytes, &p->msg_qbytes))
 			return -EFAULT;
+		if (qbytes == 0) {
+			if (get_user(qbytes, &p->msg_lqbytes))
+				return -EFAULT;
+		}
+		out->msg_qbytes = qbytes;
 	}
 	return 0;
 }
@@ -721,9 +728,23 @@ static int copy_compat_msqid_to_user(voi
 		v.msg_stime = in->msg_stime;
 		v.msg_rtime = in->msg_rtime;
 		v.msg_ctime = in->msg_ctime;
-		v.msg_cbytes = in->msg_cbytes;
-		v.msg_qnum = in->msg_qnum;
-		v.msg_qbytes = in->msg_qbytes;
+		if (in->msg_cbytes > USHRT_MAX)
+			v.msg_cbytes = USHRT_MAX;
+		else
+			v.msg_cbytes = in->msg_cbytes;
+		v.msg_lcbytes = in->msg_cbytes;
+
+		if (in->msg_qnum > USHRT_MAX)
+			v.msg_qnum = USHRT_MAX;
+		else
+			v.msg_qnum = in->msg_qnum;
+
+		if (in->msg_qbytes > USHRT_MAX)
+			v.msg_qbytes = USHRT_MAX;
+		else
+			v.msg_qbytes = in->msg_qbytes;
+		v.msg_lqbytes = in->msg_qbytes;
+
 		v.msg_lspid = in->msg_lspid;
 		v.msg_lrpid = in->msg_lrpid;
 		return copy_to_user(buf, &v, sizeof(v));

_

Patches currently in -mm which might be from [email protected] are

ipc-msg-align-compat-version-0-msqid_ds-with-native-ipc_old.patch