git: 8f926f291143 - main - unix: Do not copy out internalized SCM_RIGHTS on MSG_PEEK
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 17 Nov 2026 17:33:23 UTC
The branch main has been updated by glebius:
URL: https://cgit.FreeBSD.org/src/commit/?id=8f926f291143d5a785f9b7d95d5d73648b043f4c
commit 8f926f291143d5a785f9b7d95d5d73648b043f4c
Author: Patrick Crandol <patrick@crandol.com>
AuthorDate: 2026-11-17 16:57:16 +0000
Commit: Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2026-11-17 17:32:07 +0000
unix: Do not copy out internalized SCM_RIGHTS on MSG_PEEK
recvmsg(2) with MSG_PEEK on a queued SCM_RIGHTS message copied the
control data out in its internalized form, an in-kernel struct file
pointer per descriptor, instead of as file descriptors. The
stream/seqpacket receive method noted this in an XXXGL comment; the
datagram peek path behaved the same way.
On a peek, return the SCM_RIGHTS message with -1 in place of each
descriptor. A descriptor is a non-negative integer, so -1 tells the
caller that descriptors are queued without installing any; they are
delivered by the next non-peek receive. Other control messages (e.g.
SCM_CREDS) are still returned. Document this in unix(4).
Reviewed by: glebius, markj
Suggested by: glebius
Pull Request: https://github.com/freebsd/freebsd-src/pull/2469
Signed-off-by: Patrick Crandol <patrick@crandol.com>
---
share/man/man4/unix.4 | 13 +++++++-
sys/kern/uipc_usrreq.c | 81 +++++++++++++++++++++++++++++++++++++++-----------
2 files changed, 75 insertions(+), 19 deletions(-)
diff --git a/share/man/man4/unix.4 b/share/man/man4/unix.4
index 807f7fe4410f..cd524832782b 100644
--- a/share/man/man4/unix.4
+++ b/share/man/man4/unix.4
@@ -25,7 +25,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd August 5, 2026
+.Dd October 22, 2026
.Dt UNIX 4
.Os
.Sh NAME
@@ -264,6 +264,17 @@ is passed in the
.Xr recvmsg 2
call.
.Pp
+If a message carrying
+.Dv SCM_RIGHTS
+is received with
+.Dv MSG_PEEK ,
+no descriptors are created in the receiving process and each element of
+the array is returned as \-1.
+The descriptors stay queued and are delivered by a later
+.Xr recvmsg 2
+call without
+.Dv MSG_PEEK .
+.Pp
Descriptors that are awaiting delivery, or that are
purposely not received, are automatically closed by the system
when the destination socket is closed.
diff --git a/sys/kern/uipc_usrreq.c b/sys/kern/uipc_usrreq.c
index 7d3edd7e32d2..51514342ce98 100644
--- a/sys/kern/uipc_usrreq.c
+++ b/sys/kern/uipc_usrreq.c
@@ -312,6 +312,7 @@ static void unp_internalize_fp(struct file *);
static int unp_externalize(const struct socket *, struct mbuf *,
struct mbuf **, int);
static int unp_externalize_fp(struct file *);
+static struct mbuf **unp_peek_control(struct mbuf *, struct mbuf **);
static void unp_addsockcred(struct thread *, struct mchain *, int);
static void unp_process_defers(void * __unused, int);
@@ -1689,20 +1690,11 @@ restart:
}
} else {
/*
- * XXXGL
- *
- * In MSG_PEEK case control is not externalized. This
- * means we are leaking some kernel pointers to the
- * userland. They are useless to a law-abiding
- * application, but may be useful to a malware. This
- * is what the historical implementation in the
- * soreceive_generic() did. To be improved?
+ * In MSG_PEEK case control is not externalized. See
+ * unp_peek_control().
*/
- if (controlp != NULL) {
- *controlp = m_copym(control, 0, control->m_len,
- M_WAITOK);
- controlp = &(*controlp)->m_next;
- }
+ if (controlp != NULL)
+ controlp = unp_peek_control(control, controlp);
control = STAILQ_NEXT(control, m_stailq);
}
}
@@ -2274,13 +2266,12 @@ uipc_peek_dgram(struct socket *so, struct mbuf *m, struct sockaddr **psa,
KASSERT(m, ("%s: no data or control after soname", __func__));
/*
- * With MSG_PEEK the control isn't executed, just copied.
+ * With MSG_PEEK the control isn't externalized. See
+ * unp_peek_control().
*/
while (m != NULL && m->m_type == MT_CONTROL) {
- if (controlp != NULL) {
- *controlp = m_copym(m, 0, m->m_len, M_WAITOK);
- controlp = &(*controlp)->m_next;
- }
+ if (controlp != NULL)
+ controlp = unp_peek_control(m, controlp);
m = m->m_next;
}
KASSERT(m == NULL || m->m_type == MT_DATA,
@@ -3884,6 +3875,60 @@ next:
return (error);
}
+/*
+ * Copy control messages out for MSG_PEEK. They aren't externalized, and the
+ * internalized SCM_RIGHTS holds kernel pointers (struct filedescent *), which
+ * must not be copied out. Return -1 in place of each descriptor instead; the
+ * descriptors are delivered by a later receive without MSG_PEEK. Anything
+ * else is copied across, as unp_externalize() does. Returns the new tail of
+ * the chain.
+ */
+static struct mbuf **
+unp_peek_control(struct mbuf *control, struct mbuf **controlp)
+{
+ struct cmsghdr *cm;
+ socklen_t clen;
+
+ cm = mtod(control, struct cmsghdr *);
+ clen = control->m_len;
+ while (cm != NULL) {
+ void *data;
+ socklen_t datalen;
+ int *fdp, newfds;
+
+ MPASS(clen >= sizeof(*cm) && clen >= cm->cmsg_len);
+
+ data = CMSG_DATA(cm);
+ datalen = (char *)cm + cm->cmsg_len - (char *)data;
+ if (cm->cmsg_level == SOL_SOCKET &&
+ cm->cmsg_type == SCM_RIGHTS) {
+ newfds = datalen / sizeof(struct filedescent *);
+ if (newfds > 0) {
+ *controlp = sbcreatecontrol(NULL,
+ newfds * sizeof(int), SCM_RIGHTS,
+ SOL_SOCKET, M_WAITOK);
+ fdp = (int *)CMSG_DATA(mtod(*controlp,
+ struct cmsghdr *));
+ for (int i = 0; i < newfds; i++)
+ fdp[i] = -1;
+ controlp = &(*controlp)->m_next;
+ }
+ } else {
+ *controlp = sbcreatecontrol(data, datalen,
+ cm->cmsg_type, cm->cmsg_level, M_WAITOK);
+ controlp = &(*controlp)->m_next;
+ }
+
+ if (CMSG_SPACE(datalen) < clen) {
+ clen -= CMSG_SPACE(datalen);
+ cm = (struct cmsghdr *)
+ ((char *)cm + CMSG_SPACE(datalen));
+ } else
+ break;
+ }
+ return (controlp);
+}
+
static void
unp_zone_change(void *tag)
{