mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Colin Cross <>
Subject: [musl] Increase sendmsg internal buffer to match kernel SCM_MAX_FD limit
Date: Thu, 9 Feb 2023 15:08:50 -0800	[thread overview]
Message-ID: <> (raw)

[-- Attachment #1: Type: text/plain, Size: 624 bytes --]

I came across a test at;l=954;drc=68a556190553a4060babf4a4e5cb1bb16ae61ab2
that verifies that some fd passing code can handle passing SCM_MAX_FD
fds through a unix socket.  SCM_MAX_FD is an arbitrary 253 fd limit
imposed by the kernel since 2.6.38 (before that it was 255).  An
SCM_RIGHTS ancillary message contiang 253 fds is only slightly larger
than the existing 1024 byte internal buffer in sendmsg, so this patch
slightly increases the arbitrary limit in musl to match an arbitrary
limit in the kernel.

[-- Attachment #2: 0001-Increase-sendmsg-internal-buffer-to-support-SCM_MAX_.patch --]
[-- Type: text/x-patch, Size: 1648 bytes --]

From 4a9c1a5b14fddd3924561e9cc5d126111ea881c4 Mon Sep 17 00:00:00 2001
From: Colin Cross <>
Date: Thu, 9 Feb 2023 14:50:49 -0800
Subject: [PATCH] Increase sendmsg internal buffer to support SCM_MAX_FD

The kernel defines a limit on the number of fds that can be passed
through an SCM_RIGHTS ancillary message as SCM_MAX_FD.  The value
has been 253 since kernel 2.6.38 (before that it was 255).  On x86_64,
and SCM_RIGHTS ancillary message with 253 fds requires 1032 bytes,
slightly more than the current 1024 byte internal buffer in sendmsg.
1024 is an arbitrary size, so increase it to match the the arbitrary
size limit in the kernel.  This fixes tests that are verifying they
support up to SCM_MAX_FD fds.
 src/network/sendmsg.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/network/sendmsg.c b/src/network/sendmsg.c
index 80cc5f41..b5ce6629 100644
--- a/src/network/sendmsg.c
+++ b/src/network/sendmsg.c
@@ -8,13 +8,16 @@ ssize_t sendmsg(int fd, const struct msghdr *msg, int flags)
 	struct msghdr h;
-	struct cmsghdr chbuf[1024/sizeof(struct cmsghdr)+1], *c;
+	/* Kernels since 2.6.38 set SCM_MAX_FD to 253, allocate enough
+	 * space to support an SCM_RIGHTS ancillary message with 253 fds. */
+	const size_t chbufsize = CMSG_SPACE(253*sizeof(int));
+	struct cmsghdr chbuf[chbufsize/sizeof(struct cmsghdr)+1], *c;
 	if (msg) {
 		h = *msg;
 		h.__pad1 = h.__pad2 = 0;
 		msg = &h;
 		if (h.msg_controllen) {
-			if (h.msg_controllen > 1024) {
+			if (h.msg_controllen > chbufsize) {
 				errno = ENOMEM;
 				return -1;

             reply	other threads:[~2023-02-09 23:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-09 23:08 Colin Cross [this message]
2023-02-10  0:26 ` Rich Felker
2023-02-10 22:35   ` Colin Cross

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).