From nobody Wed Nov 11 16:49:52 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jPqJ11CDRz70Sg3 for ; Wed, 11 Nov 2026 16:49:53 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jPqJ06JQpz4mlG for ; Wed, 11 Nov 2026 16:49:52 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794415792; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=MHTqSeA71Kfj9GhSVvMcLrJHFBi531sHJ3uC3hHSuHk=; b=sy+0IwrGxzwQCEIehz1Cflre+OYJFhXlFGtnLIBSSWOMGm+swvQF8yl07BG0Q2owHNq7NH tQR6O972ZAoJGuL8ernLWIdgwsZHZ4juPuNmMYQwpJsSj9Q1UypHXY4MWsS3jSKecaXIGR /Xm7SZfrSiq24wnfgH8v+wXWTHi0OJd7+XL1CvgfB2iR90WTT50JeuKRVWL44gGY6d7Dfx z1taKjffjIM7yt/FFopMQ26Fikr/utetrkNIGsOHOE+gfqCZbv0uTtZJr7lVYvOYcFjK7R bLa22LsW0l27LJWIsmb+ZiP2+lGfvOjS4apGIlqwFqjxD1+WVOU9Js5z8K2CbA== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794415792; b=YEcN2XmErDmDwS2JYvBdnuaXpoZP78XO6woqJiruSBk4QM0zK3BEYu0F0OdyNHgmrTcJGd VHvlmuJ1oMJcvS/CJskhtW23IpveBAFB+lcmojfWgLVevkjsdW58McvomqXrlEsSDtwRyM ePeqGfkplSH0igdxlY47QmEnHO+I6MnyhwOnR9Ro5O+E7+6SJ6F72Xj1qWMykb0hqBIoRq GT5q0BKQVMEMDj5p/MtfoX4B37KucwcBReFWngtzboNlRpzHuMZ7/LeAGPw+umqRsdVcMg ubG5i9sWRDPcJk9bZS3Sa7bVDCE2do0t8gvMpGZfCMmuAYAxAMYgCa2XHigzJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794415792; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=MHTqSeA71Kfj9GhSVvMcLrJHFBi531sHJ3uC3hHSuHk=; b=ZhzCz7/SBIxc+R8UjntQEPLWxRogXK/uVj9NZ1pZ2vlJk4ZLahLkRxleXGNYufQip3Ll8a YrZtOingu/aWMWA4X5koH6zzfaDKD0F9ph0p7pK0NVM8SVB59buALfOh1Ine6mBq/vYWO0 124rqjkR7wjjKpBCbaEQvZFB5uvCCV47AwXLhEfH3fvzbpgYtFXXBjdnBpRQVKhQ4hfxwt /c7CI2jZFLGu5sw0AbITip2AZH8ESgzva1meu0ZxMrjnRhwSgcdnvmB5+72vQD3RSbUonU N5iG3plagiDf4orHor9lm6xl+u/uxV1NWvmmQLMO8rqMtTIoC2E3hwyCMxZePQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jPqJ05xgtzMbc for ; Wed, 11 Nov 2026 16:49:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1c816 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Wed, 11 Nov 2026 16:49:52 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 57bee493fdfa - main - sysvmsg: Fix sequence number wraparound races List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 57bee493fdfae53453cdcbb63235067d95127621 Auto-Submitted: auto-generated Date: Wed, 11 Nov 2026 16:49:52 +0000 Message-Id: <6af49cb0.1c816.594319ab@gitrepo.freebsd.org> The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=57bee493fdfae53453cdcbb63235067d95127621 commit 57bee493fdfae53453cdcbb63235067d95127621 Author: Mark Johnston AuthorDate: 2026-11-11 14:14:14 +0000 Commit: Mark Johnston CommitDate: 2026-11-11 16:49:42 +0000 sysvmsg: Fix sequence number wraparound races The SysV message queue code has the same problem that we fixed in the semaphore code in SA-26:54.sysvsem and SA-26:64.sysvsem. That is, kern_msgrcv() and kern_msgsnd() are susceptible to TOCTOU races when they sleep waiting for messages and queue space, respectively. Unlike the semaphore code, however, the message queue array is never resized and there is no notion of message queue sets, so such races do not open up any memory safety bugs. Fix the races, following the pattern from the SysV semaphore changes: use a global sequence number array with wide counters, to avoid changing the KBI which is embedded in userspace tools. Fix kern_msgsnd(), which did not validate the sequence number at all after sleeping. Use a msgvalid() helper to factor out some duplicated checks. Convert a couple of local flag variables to bools. Reported by: Apex Security Research Reviewed by: kib MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D61397 --- sys/kern/sysv_msg.c | 115 +++++++++++++++++++++++----------------------------- 1 file changed, 51 insertions(+), 64 deletions(-) diff --git a/sys/kern/sysv_msg.c b/sys/kern/sysv_msg.c index a545a0a54c25..f4ec7919b476 100644 --- a/sys/kern/sysv_msg.c +++ b/sys/kern/sysv_msg.c @@ -87,6 +87,7 @@ static MALLOC_DEFINE(M_MSG, "msg", "SVID compatible message queues"); static int msginit(void); static int msgunload(void); static int sysvmsg_modload(struct module *, int, void *); +static int msgvalid(int, struct prison *, struct msqid_kernel *); static void msq_remove(struct msqid_kernel *); static struct prison *msg_find_prison(struct ucred *); static int msq_prison_cansee(struct prison *, struct msqid_kernel *); @@ -168,6 +169,7 @@ static char *msgpool; /* MSGMAX byte long msg buffer pool */ static struct msgmap *msgmaps; /* MSGSEG msgmap structures */ static struct msg *msghdrs; /* MSGTQL msg headers */ static struct msqid_kernel *msqids; /* MSGMNI msqid_kernel struct's */ +static uint64_t *msqseq; /* MSGMNI sequence numbers */ static struct mtx msq_mtx; /* global mutex for message queues. */ static unsigned msg_prison_slot;/* prison OSD slot */ @@ -225,6 +227,8 @@ msginit(void) msghdrs = malloc(sizeof(struct msg) * msginfo.msgtql, M_MSG, M_WAITOK); msqids = malloc(sizeof(struct msqid_kernel) * msginfo.msgmni, M_MSG, M_WAITOK | M_ZERO); + msqseq = malloc(sizeof(uint64_t) * msginfo.msgmni, M_MSG, + M_WAITOK | M_ZERO); /* * msginfo.msgssz should be a power of two for efficiency reasons. @@ -344,6 +348,7 @@ msgunload(void) free(msgmaps, M_MSG); free(msghdrs, M_MSG); free(msqids, M_MSG); + free(msqseq, M_MSG); mtx_destroy(&msq_mtx); return (0); } @@ -406,6 +411,15 @@ msg_freehdr(struct msg *msghdr) #endif } +static int +msgvalid(int msqid, struct prison *rpr, struct msqid_kernel *msqkptr) +{ + + return (msqkptr->u.msg_qbytes == 0 || + msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid) || + msq_prison_cansee(rpr, msqkptr) ? EINVAL : 0); +} + static void msq_remove(struct msqid_kernel *msqkptr) { @@ -516,22 +530,8 @@ kern_msgctl(struct thread *td, int msqid, int cmd, struct msqid_ds *msqbuf) msqkptr = &msqids[msqix]; mtx_lock(&msq_mtx); - if (msqkptr->u.msg_qbytes == 0) { - DPRINTF(("no such msqid\n")); - error = EINVAL; - goto done2; - } - if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) { - DPRINTF(("wrong sequence number\n")); - error = EINVAL; - goto done2; - } - - error = msq_prison_cansee(rpr, msqkptr); - if (error != 0) { - DPRINTF(("requester can't see prison\n")); + if ((error = msgvalid(msqid, rpr, msqkptr)) != 0) goto done2; - } #ifdef MAC error = mac_sysvmsq_check_msqctl(td->td_ucred, msqkptr, cmd); @@ -725,7 +725,8 @@ sys_msgget(struct thread *td, struct msgget_args *uap) msqkptr->u.msg_perm.mode = (msgflg & 0777); msqkptr->cred = crhold(cred); /* Make sure that the returned msqid is unique */ - msqkptr->u.msg_perm.seq = (msqkptr->u.msg_perm.seq + 1) & 0x7fff; + msqseq[msqid]++; + msqkptr->u.msg_perm.seq = msqseq[msqid] & 0x7fff; msqkptr->u.__msg_first = NULL; msqkptr->u.__msg_last = NULL; msqkptr->u.msg_cbytes = 0; @@ -770,6 +771,7 @@ kern_msgsnd(struct thread *td, int msqid, const void *msgp, struct msqid_kernel *msqkptr; struct msg *msghdr; struct prison *rpr; + uint64_t seq; short next; #ifdef RACCT size_t saved_msgsz = 0; @@ -792,21 +794,8 @@ kern_msgsnd(struct thread *td, int msqid, const void *msgp, msqkptr = &msqids[msqix]; AUDIT_ARG_SVIPC_PERM(&msqkptr->u.msg_perm); - if (msqkptr->u.msg_qbytes == 0) { - DPRINTF(("no such message queue id\n")); - error = EINVAL; + if ((error = msgvalid(msqid, rpr, msqkptr)) != 0) goto done2; - } - if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) { - DPRINTF(("wrong sequence number\n")); - error = EINVAL; - goto done2; - } - - if ((error = msq_prison_cansee(rpr, msqkptr))) { - DPRINTF(("requester can't see prison\n")); - goto done2; - } if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_W))) { DPRINTF(("requester doesn't have write access\n")); @@ -841,8 +830,9 @@ kern_msgsnd(struct thread *td, int msqid, const void *msgp, segs_needed = howmany(msgsz, msginfo.msgssz); DPRINTF(("msgsz=%zu, msgssz=%d, segs_needed=%d\n", msgsz, msginfo.msgssz, segs_needed)); + seq = msqseq[msqix]; for (;;) { - int need_more_resources = 0; + bool need_more_resources = false; /* * check msgsz @@ -857,23 +847,23 @@ kern_msgsnd(struct thread *td, int msqid, const void *msgp, if (msqkptr->u.msg_perm.mode & MSG_LOCKED) { DPRINTF(("msqid is locked\n")); - need_more_resources = 1; + need_more_resources = true; } if (msgsz + msqkptr->u.msg_cbytes > msqkptr->u.msg_qbytes) { DPRINTF(("msgsz + msg_cbytes > msg_qbytes\n")); - need_more_resources = 1; + need_more_resources = true; } if (segs_needed > nfree_msgmaps) { DPRINTF(("segs_needed > nfree_msgmaps\n")); - need_more_resources = 1; + need_more_resources = true; } if (free_msghdrs == NULL) { DPRINTF(("no more msghdrs\n")); - need_more_resources = 1; + need_more_resources = true; } if (need_more_resources) { - int we_own_it; + bool we_own_it; if ((msgflg & IPC_NOWAIT) != 0) { DPRINTF(("need more resources but caller " @@ -884,20 +874,35 @@ kern_msgsnd(struct thread *td, int msqid, const void *msgp, if ((msqkptr->u.msg_perm.mode & MSG_LOCKED) != 0) { DPRINTF(("we don't own the msqid_ds\n")); - we_own_it = 0; + we_own_it = false; } else { /* Force later arrivals to wait for our request */ DPRINTF(("we own the msqid_ds\n")); msqkptr->u.msg_perm.mode |= MSG_LOCKED; - we_own_it = 1; + we_own_it = true; } + DPRINTF(("msgsnd: goodnight\n")); error = msleep(msqkptr, &msq_mtx, PVFS | PCATCH, "msgsnd", hz); DPRINTF(("msgsnd: good morning, error=%d\n", error)); + if (we_own_it) msqkptr->u.msg_perm.mode &= ~MSG_LOCKED; + + /* + * Make sure that the msq queue still exists. + * The embedded sequence number check isn't + * sufficient to detect reallocation since it's + * too narrow. + */ + if (msqkptr->u.msg_qbytes == 0 || + msqseq[msqix] != seq) { + DPRINTF(("msqid deleted\n")); + error = EIDRM; + goto done3; + } if (error == EWOULDBLOCK) { DPRINTF(("msgsnd: timed out\n")); continue; @@ -908,16 +913,6 @@ kern_msgsnd(struct thread *td, int msqid, const void *msgp, goto done3; } - /* - * Make sure that the msq queue still exists - */ - - if (msqkptr->u.msg_qbytes == 0) { - DPRINTF(("msqid deleted\n")); - error = EIDRM; - goto done3; - } - } else { DPRINTF(("got all the resources that we need\n")); break; @@ -1140,6 +1135,7 @@ kern_msgrcv(struct thread *td, int msqid, void *msgp, size_t msgsz, long msgtyp, struct msqid_kernel *msqkptr; struct msg *msghdr; struct prison *rpr; + uint64_t seq; int msqix, error = 0; short next; @@ -1159,21 +1155,8 @@ kern_msgrcv(struct thread *td, int msqid, void *msgp, size_t msgsz, long msgtyp, msqkptr = &msqids[msqix]; mtx_lock(&msq_mtx); AUDIT_ARG_SVIPC_PERM(&msqkptr->u.msg_perm); - if (msqkptr->u.msg_qbytes == 0) { - DPRINTF(("no such message queue id\n")); - error = EINVAL; - goto done2; - } - if (msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) { - DPRINTF(("wrong sequence number\n")); - error = EINVAL; - goto done2; - } - - if ((error = msq_prison_cansee(rpr, msqkptr))) { - DPRINTF(("requester can't see prison\n")); + if ((error = msgvalid(msqid, rpr, msqkptr)) != 0) goto done2; - } if ((error = ipcperm(td, &msqkptr->u.msg_perm, IPC_R))) { DPRINTF(("requester doesn't have read access\n")); @@ -1186,6 +1169,7 @@ kern_msgrcv(struct thread *td, int msqid, void *msgp, size_t msgsz, long msgtyp, goto done2; #endif + seq = msqseq[msqix]; msghdr = NULL; while (msghdr == NULL) { if (msgtyp == 0) { @@ -1313,11 +1297,14 @@ kern_msgrcv(struct thread *td, int msqid, void *msgp, size_t msgsz, long msgtyp, } /* - * Make sure that the msq queue still exists + * Make sure that the msq queue still exists. + * The embedded sequence number check isn't + * sufficient to detect reallocation since it's + * too narrow. */ if (msqkptr->u.msg_qbytes == 0 || - msqkptr->u.msg_perm.seq != IPCID_TO_SEQ(msqid)) { + msqseq[msqix] != seq) { DPRINTF(("msqid deleted\n")); error = EIDRM; goto done2;