From nobody Wed May 25 20:30:06 2022 X-Original-To: dev-commits-src-main@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 C03CE1B4C226; Wed, 25 May 2022 20:30:06 +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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4L7jNt52Y0z3hmw; Wed, 25 May 2022 20:30:06 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1653510606; 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=NKba6jd6wfv08gsa/CTwGhv09bYHhUESovKg4nbxLxY=; b=qyGveMRxSoTms6C0wtNHRgnMdtPUAyEAdjIeQDG0k43vtKm3NZbi2H47z5xQ0fVIH97QGc bnpsOQC3SvhaoZoxnrYH4mhA5mwc/GM81UGT0m3YoQNrm4JaXGJa34UjiHoRwGERt/F5ry pby6vBgZ7XG/yjw5LJCEoW/EYhaR1GZyIa6gKJn12cAGAyjFxepSFGn1J2FBtAYwHsIieS dffX1FIV1m50W6hcjOSD9cQm4+9KdMkOUqi9TDJtfWYmIw/nweXAjns0z7vYfPSOsxT3rR 6PDB6bNu5fqRzHd610Mcj7BUj/y+z4pM6PCpPR1cpEeU663F8bXV4Z3vD1vlrQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 8CB3020065; Wed, 25 May 2022 20:30:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 24PKU6TA040134; Wed, 25 May 2022 20:30:06 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 24PKU6Ue040131; Wed, 25 May 2022 20:30:06 GMT (envelope-from git) Date: Wed, 25 May 2022 20:30:06 GMT Message-Id: <202205252030.24PKU6Ue040131@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Gleb Smirnoff Subject: git: 23402c83f4fc - main - tests/unix_passfd: sending many and too many SCM_RIGHTS List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: glebius X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 23402c83f4fc2db45f0435ed106791828a6522df Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1653510606; 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=NKba6jd6wfv08gsa/CTwGhv09bYHhUESovKg4nbxLxY=; b=mS5AnAAEKd/B+afYESkhWmG+ie57RcVGqD3d9BYAQXY+90U0feiSyGzf4OzfKd+gUEOfbi VKofF8e83h8JlJd5JHJMf5mzknFnFupBGwjEtRgEcDTWufCZe1oaBrDx+eaJOJg54kCi8Q N85re4YdJ9RJWk41L4AHwk0kna85fGPeb+okfx5Fvk/vNqE76K6siBXxkrIG2NNV0fEwNC NW4KtTy47LRSZbqbOQ/DNLzuiWRHhKt5TqOH9jQ2Est0trKmU8j8cDBeYBF3PQq/y91+Jx DJAAXTieVUTkhqX42bmZbAQzhTiEUzIu4LvjaPWNZ2YVHamhOrVJnsfH0st6CQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1653510606; a=rsa-sha256; cv=none; b=OytvhTV7MEfA1iEqkN9racB9PyNuBM1JRHMcuIs4cv9kNew22z1m3+G4Z4xHjt4Ecf1ub2 gIwv3niTpHj8AVEOmisW5xDrIsIUBshy8cGDa3WLR4wIfQYJdDv9ZkJDQdjsF5zZkhy7T3 QMREpCDacJ9zNEBn8UEOh++5HdiE5g0cSe0hbWU8nFdkMWNYJbUHr006j8Bd0BJCyywC+5 5Cc3VMbL/Bw8x2Xhkn7ElujFPMv2iBxKWctsTTUv7Lr9NIXMExY5FEQ3Ctn7uhGOMsofqA tNvXE1hrq1BTHLSDbtdyZZgNj8sutW2VgIL9m6iQx8g2KW8YzcnC+4cbKYQaxg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=23402c83f4fc2db45f0435ed106791828a6522df commit 23402c83f4fc2db45f0435ed106791828a6522df Author: Gleb Smirnoff AuthorDate: 2022-05-25 20:28:40 +0000 Commit: Gleb Smirnoff CommitDate: 2022-05-25 20:28:40 +0000 tests/unix_passfd: sending many and too many SCM_RIGHTS o Exercise bounds checking when sending large set of file descriptors, that can't fit into single control mbuf. o Exercise resource limits checks when receiving a large sets. o Check that socket isn't left in a completely stuck state when we can't receive SCM_RIGHTS due to limits. Current SOCK_STREAM socket would free the control, but leave the data in. This seems to be a legit behavior for a stream socket, as we don't want holes in the data. PR: 239250 Reviewed by: markj Differential revision: https://reviews.freebsd.org/D35315 --- tests/sys/kern/unix_passfd_test.c | 69 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/tests/sys/kern/unix_passfd_test.c b/tests/sys/kern/unix_passfd_test.c index 13b569479af8..da7052a7fcf4 100644 --- a/tests/sys/kern/unix_passfd_test.c +++ b/tests/sys/kern/unix_passfd_test.c @@ -28,10 +28,12 @@ #include __FBSDID("$FreeBSD$"); -#include +#include #include #include #include +#include +#include #include #include @@ -369,6 +371,70 @@ ATF_TC_BODY(send_and_shutdown, tc) closesocketpair(fd); } +/* + * Send maximum possible SCM_RIGHTS message. + * Internally the file descriptors are converted from integers to pointers + * and stored in a single mbuf cluster. Check that we can not send too much + * and that we can successfully send maximum possible amount. Check that we + * can not exploit getrlimit(3). + */ +#define MAXFDS ((MCLBYTES - _ALIGN(sizeof(struct cmsghdr)))/sizeof(void *)) +ATF_TC_WITHOUT_HEAD(send_a_lot); +ATF_TC_BODY(send_a_lot, tc) +{ + struct msghdr msghdr; + struct iovec iov; + struct rlimit rlim; + int fd[2], nfds; + char *cmsg, ch; + + domainsocketpair(fd); + cmsg = malloc(CMSG_SPACE((MAXFDS + 1) * sizeof(int))); + ATF_REQUIRE(cmsg != NULL); + iov.iov_base = &ch; + iov.iov_len = sizeof(ch); + msghdr = (struct msghdr ){ + .msg_control = cmsg, + .msg_controllen = CMSG_LEN((MAXFDS + 1) * sizeof(int)), + .msg_iov = &iov, + .msg_iovlen = 1, + }; + + /* Sending too much fails. */ + putfds(cmsg, fd[0], MAXFDS + 1); + ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == -1); + ATF_REQUIRE(errno == EMSGSIZE); + + /* Sending just the right amount works and everything is received. */ + putfds(cmsg, fd[0], MAXFDS); + msghdr.msg_controllen = CMSG_LEN(MAXFDS * sizeof(int)); + ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == 1); + nfds = getnfds(); + ATF_REQUIRE(recvmsg(fd[1], &msghdr, 0) == 1); + ATF_REQUIRE(getnfds() == nfds + MAXFDS); + + /* Limit our process open files... */ + ATF_REQUIRE(getrlimit(RLIMIT_NOFILE, &rlim) == 0); + nfds = rlim.rlim_cur = getnfds(); + ATF_REQUIRE(setrlimit(RLIMIT_NOFILE, &rlim) == 0); + + /* ... and try to receive a single descriptor. */ + putfds(cmsg, fd[0], 1); + msghdr.msg_controllen = CMSG_LEN(sizeof(int)); + ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == 1); + ATF_REQUIRE(recvmsg(fd[1], &msghdr, 0) == -1); + /* Such attempt shall fail with EMSGSIZE. */ + ATF_REQUIRE(errno == EMSGSIZE); + ATF_REQUIRE(getnfds() == nfds); + /* + * For the SOCK_STREAM the above attempt shall free the control in + * the kernel, so that socket isn't left in a stuck state. Next read + * shall bring us the normal data only. + */ + ATF_REQUIRE(recvmsg(fd[1], &msghdr, 0) == 1); + ATF_REQUIRE(msghdr.msg_controllen == 0); +} + /* * Send two files. Then receive them. Make sure they are returned in the * right order, and both get there. @@ -756,6 +822,7 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, send_and_close); ATF_TP_ADD_TC(tp, send_and_cancel); ATF_TP_ADD_TC(tp, send_and_shutdown); + ATF_TP_ADD_TC(tp, send_a_lot); ATF_TP_ADD_TC(tp, two_files); ATF_TP_ADD_TC(tp, bundle); ATF_TP_ADD_TC(tp, bundle_cancel);