From nobody Thu Apr 25 04:56:20 2024 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 4VQ3Tw4BzQz5JvdZ; Thu, 25 Apr 2024 04:56:20 +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 4VQ3Tw1r7Bz4Qm6; Thu, 25 Apr 2024 04:56:20 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1714020980; 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=zS7Pp+nF1y39Is75NoCqjJWIg+bubIqtqDX2PwMoCYc=; b=lFcv4BvoKm1FDPhUFEQVZZTOnoEY6fSgLnrgNdPZ9FNlYSHl2JAEonabs5iDhACvSvA2cE K19fSJlZFiDPsePoz4bQ1ys8t5EblybnSXFM3cqvKyrXUz6yexN1rBNrq92BxKkknxBz5i 6E0RT6XpMjkb7VFUrD1oL5iQPlfCXJrcGhgiKJCRforceAB0Ew23wXzG5gSw7ZsKXvY7Tl qwl/ATw+0YJT2RKnzp1wg6d4ZIjBiD+S4Aq7/b+uKZGZsEZq5dvsafDK4Yy+BrHGB1EfIU ohMQpZgWCjGMOD9rEZAJii2ous/FrP1TiEg6lawN8qgDQoUnOQpV4U+o9eQYUA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1714020980; a=rsa-sha256; cv=none; b=cQ1JMBrnDHdzRqxMxpoUwA6ddP5aaVGOgnpdYbDdnpHJTXllPfygeaSL4O2gn7t01l82+1 Zv5NhJU0W76zynyZA3Axomd9Rlw4lPUgVvY5h3PI1b9ckcvwfbXX5e0w9pECVt70buoVyv 6z0Eqf1e9PEh7FXaEbxCLUSvxeuz2B2ivhcyijP14Qk/vyPiVPIXhZ0C+aT2DmycOBjBuG McQ7JBcG1Acxrhfw3OEcJAsbEVEyon9CL1Esre4JyGIXK1KBs5WRFvHQk4bbTDSzFkJ/0r D8cqTAH1msb0KWyJEDaP4E2S4Fj7TIEK1tKSLlXn4Uq311iUG2377h2GNBCrNw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1714020980; 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=zS7Pp+nF1y39Is75NoCqjJWIg+bubIqtqDX2PwMoCYc=; b=ir0St3Ydhz2YGwmKssFAY+zrE95tGG+3MSuX8Uyea3AJm8CWBK+ZMp09uV8QkLcFlBXkME 7FeCJHtwlNzRcvqg1a4n550Pv9VVXsDh61lTtd/EJ2g2M+6m5DRKa+z2fzab/FgRafB0Q+ V51Q4UGYXn4lo0dmFfQN2Nkd0Xtry5UTyb2Xz9Xf7XjzoDVSwA9HJUpfIBNNjxEE7oJr89 qa1wB3pQ6qp1OX86xx33LLycKqUAjFvGpQeWRP9FMT3UUvU7x1+UTjvUiWH6SuM+1oWi4X Dwh/NFVj9nJaD4r75cgVztcTjWv0aEWKefvneXZAQssKFEKxWIbT+iKCRJqS7g== 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 4VQ3Tw1S3Gzm0d; Thu, 25 Apr 2024 04:56:20 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 43P4uKdK047425; Thu, 25 Apr 2024 04:56:20 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43P4uKKd047422; Thu, 25 Apr 2024 04:56:20 GMT (envelope-from git) Date: Thu, 25 Apr 2024 04:56:20 GMT Message-Id: <202404250456.43P4uKKd047422@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: 19307b86d31b - main - accept_filter: return different errors for non-listener and a busy socket 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: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-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: 19307b86d31b7dea4bb5c3f7e233ee0e59049258 Auto-Submitted: auto-generated The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=19307b86d31b7dea4bb5c3f7e233ee0e59049258 commit 19307b86d31b7dea4bb5c3f7e233ee0e59049258 Author: Gleb Smirnoff AuthorDate: 2024-04-25 04:50:58 +0000 Commit: Gleb Smirnoff CommitDate: 2024-04-25 04:55:58 +0000 accept_filter: return different errors for non-listener and a busy socket The fact that an accept filter needs to be cleared first before setting to a different one isn't properly documented. The requirement that the socket needs already be listening, although trivial, isn't documented either. At least return a more meaningful error than EINVAL for an existing filter. Cover this with a test case. --- sys/kern/uipc_accf.c | 6 +++++- tests/sys/kern/socket_accf.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/sys/kern/uipc_accf.c b/sys/kern/uipc_accf.c index c63b5a1179bc..9e30e7839103 100644 --- a/sys/kern/uipc_accf.c +++ b/sys/kern/uipc_accf.c @@ -276,10 +276,14 @@ accept_filt_setopt(struct socket *so, struct sockopt *sopt) * without first removing it. */ SOCK_LOCK(so); - if (!SOLISTENING(so) || so->sol_accept_filter != NULL) { + if (__predict_false(!SOLISTENING(so))) { error = EINVAL; goto out; } + if (__predict_false(so->sol_accept_filter != NULL)) { + error = EBUSY; + goto out; + } /* * Invoke the accf_create() method of the filter if required. The diff --git a/tests/sys/kern/socket_accf.c b/tests/sys/kern/socket_accf.c index 747bcda87010..ae6522397cf7 100644 --- a/tests/sys/kern/socket_accf.c +++ b/tests/sys/kern/socket_accf.c @@ -209,11 +209,43 @@ ATF_TC_BODY(tls, tc) ATF_REQUIRE((a = accept(l, NULL, 0)) > 0); } +/* Check changing to a different filter. */ +ATF_TC_WITHOUT_HEAD(change); +ATF_TC_BODY(change, tc) +{ + struct accept_filter_arg dfa = { + .af_name = "dataready" + }; + struct accept_filter_arg hfa = { + .af_name = "httpready" + }; + struct sockaddr_in sin; + int n, l; + + l = listensock(&sin); + accfon(l, &dfa); + + /* Refuse to change filter without explicit removal of the old one. */ + ATF_REQUIRE(setsockopt(l, SOL_SOCKET, SO_ACCEPTFILTER, &hfa, + sizeof(hfa)) != 0 && errno == EBUSY); + + /* But allow after clearing. */ + ATF_REQUIRE(setsockopt(l, SOL_SOCKET, SO_ACCEPTFILTER, NULL, 0) == 0); + ATF_REQUIRE(setsockopt(l, SOL_SOCKET, SO_ACCEPTFILTER, &hfa, + sizeof(hfa)) == 0); + + /* Must be listening socket. */ + ATF_REQUIRE((n = socket(PF_INET, SOCK_STREAM, 0)) > 0); + ATF_REQUIRE(setsockopt(n, SOL_SOCKET, SO_ACCEPTFILTER, &dfa, + sizeof(dfa)) != 0 && errno == EINVAL); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, data); ATF_TP_ADD_TC(tp, http); ATF_TP_ADD_TC(tp, tls); + ATF_TP_ADD_TC(tp, change); return (atf_no_error()); }