From nobody Fri Apr 01 18:02:09 2022 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 B54FE1A5489D; Fri, 1 Apr 2022 18:02:10 +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 4KVSg62CJbz4tKT; Fri, 1 Apr 2022 18:02:10 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1648836130; 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=qY6yaJEepnQ0VmxwoVPiL+RRgeOZwnqnwMJUcEuVjzM=; b=TZ05VlzouZHU58RR6HS1deWHWwgy5HPQI+ZxzZ/ffhqnVD8MwdOFLOyD0NiBm3qGHK/O6C Ha1q/huic085AF1Y7eo+UbCQ+CEhVPr9hBXBELjDdpMZ9Lzvf3kEDom8kYX0Alz0WdzFcY 1msH2mm1/6cMkZVpljgSmEYnOKVYKNZvAUzPBhGYFizXXdgK8YjgWmB1YDo3ZLqXZ4jda1 Wg3X+hT+OQCtrWFH722TswQsuHOq2coUEzknUy01JTBZXxGYFYQSKBw6hHYvs35i8zRKtt 4XBwPYALrgXeOJf1+AeNWyI3DzugAYqTSvK7nz3Vd4G8APJnaa2fI3doZ2YS0w== 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 0578218EF1; Fri, 1 Apr 2022 18:02:10 +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 231I2970007250; Fri, 1 Apr 2022 18:02:09 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 231I29L5007249; Fri, 1 Apr 2022 18:02:09 GMT (envelope-from git) Date: Fri, 1 Apr 2022 18:02:09 GMT Message-Id: <202204011802.231I29L5007249@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mateusz Guzik Subject: git: c4a08ef2af6f - main - pf: handle duplicate rules gracefully List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mjg X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: c4a08ef2af6fe0b0d1b03f7ec4f20bed96de20e8 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1648836130; 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=qY6yaJEepnQ0VmxwoVPiL+RRgeOZwnqnwMJUcEuVjzM=; b=DoZzxTYbQZHLF1rOsDkGaGBGWP09Rpn6sWMQyBwcEeJ6aqbP8D4BIT4E3aUNs5AFJgCRSK J7AgqEznQQlVzZmBSV/0FDUrdts/oKfWZEXV4WA2YfN0rygkVEXxV+KTsdbLuVjXjqusNF M3yoWpNvLxhcwVxUa0XT4zl69MPi/4Gf/ieMcqpe8HQSG5qGOfO2d1ewUw8gE3ZyMGnUAT SnirrYIVq8Y5jj82BmGMrtBkHS2TUvbDNZ9Ks25TU6Fn7ReNAARBCc5P7Awfu3H3b0pqlq 1+IXzC94hnxTeVUuf3MaaOR4Pz/Po3vKMkM1c2YGJAd4+prJgWkwOWVrE8vYlA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1648836130; a=rsa-sha256; cv=none; b=u2AWxIUpZLSjOBaqsqi8XGKQs7pICnYM2kdlm2EkTNwn9HnUGypdofNBaHb898fpbIkUrc kCpnQFuWbMCNZ/xXFW68gzrE/vmxxoXWDHQ7GbbWrVkL5AAeBySCt1DNRRWvZKsRbs69+h aXatkI/rfNTwByShFwSQLZ45GMRkGAikThD0rEZDyr16c8jXTgznJX0aMlfxiBI3Voy+Yg uT3ea88meF5GYnC4ELGFiEUQ/7sEs7eOnsDF/6no9o19rPWS8/nejcSPVM91yeI3sbtpJT gzOfXbs9kovfUubHXR65YAcUcQSfAzR1eAuJ6bE9Y5Nhj0fNgfM6Il3Gpq3j5g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by mjg: URL: https://cgit.FreeBSD.org/src/commit/?id=c4a08ef2af6fe0b0d1b03f7ec4f20bed96de20e8 commit c4a08ef2af6fe0b0d1b03f7ec4f20bed96de20e8 Author: Mateusz Guzik AuthorDate: 2022-04-01 15:04:03 +0000 Commit: Mateusz Guzik CommitDate: 2022-04-01 18:01:48 +0000 pf: handle duplicate rules gracefully Reviewed by: kp Reported by: dch PR: 262971 Sponsored by: Rubicon Communications, LLC ("Netgate") --- lib/libpfctl/libpfctl.c | 2 ++ sbin/pfctl/pfctl.c | 19 +++++++++++++++++-- sys/netpfil/pf/pf_ioctl.c | 5 +++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/libpfctl/libpfctl.c b/lib/libpfctl/libpfctl.c index 8696ef1ace25..991d3fce9780 100644 --- a/lib/libpfctl/libpfctl.c +++ b/lib/libpfctl/libpfctl.c @@ -961,6 +961,8 @@ pfctl_add_rule(int dev, const struct pfctl_rule *r, const char *anchor, nv.size = nv.len; ret = ioctl(dev, DIOCADDRULENV, &nv); + if (ret == -1) + ret = errno; free(nv.data); nvlist_destroy(nvl); diff --git a/sbin/pfctl/pfctl.c b/sbin/pfctl/pfctl.c index 67358a325f77..13e8e825c1ab 100644 --- a/sbin/pfctl/pfctl.c +++ b/sbin/pfctl/pfctl.c @@ -1846,6 +1846,8 @@ pfctl_load_rule(struct pfctl *pf, char *path, struct pfctl_rule *r, int depth) u_int32_t ticket; char anchor[PF_ANCHOR_NAME_SIZE]; int len = strlen(path); + int error; + bool was_present; /* set up anchor before adding to path for anchor_call */ if ((pf->opts & PF_OPT_NOACTION) == 0) @@ -1867,12 +1869,23 @@ pfctl_load_rule(struct pfctl *pf, char *path, struct pfctl_rule *r, int depth) } else name = ""; + was_present = false; if ((pf->opts & PF_OPT_NOACTION) == 0) { if (pfctl_add_pool(pf, &r->rpool, r->af)) return (1); - if (pfctl_add_rule(pf->dev, r, anchor, name, ticket, - pf->paddr.ticket)) + error = pfctl_add_rule(pf->dev, r, anchor, name, ticket, + pf->paddr.ticket); + switch (error) { + case 0: + /* things worked, do nothing */ + break; + case EEXIST: + /* an identical rule is already present */ + was_present = true; + break; + default: err(1, "DIOCADDRULENV"); + } } if (pf->opts & PF_OPT_VERBOSE) { @@ -1880,6 +1893,8 @@ pfctl_load_rule(struct pfctl *pf, char *path, struct pfctl_rule *r, int depth) print_rule(r, r->anchor ? r->anchor->name : "", pf->opts & PF_OPT_VERBOSE2, pf->opts & PF_OPT_NUMERIC); + if (was_present) + printf(" -- rule was already present"); } path[len] = '\0'; pfctl_clear_pool(&r->rpool); diff --git a/sys/netpfil/pf/pf_ioctl.c b/sys/netpfil/pf/pf_ioctl.c index eae7b3bf1fa0..c170a270454b 100644 --- a/sys/netpfil/pf/pf_ioctl.c +++ b/sys/netpfil/pf/pf_ioctl.c @@ -2240,10 +2240,11 @@ pf_ioctl_addrule(struct pf_krule *rule, uint32_t ticket, pf_hash_rule(rule); if (RB_INSERT(pf_krule_global, ruleset->rules[rs_num].inactive.tree, rule) != NULL) { PF_RULES_WLOCK(); + TAILQ_REMOVE(ruleset->rules[rs_num].inactive.ptr, rule, entries); + ruleset->rules[rs_num].inactive.rcount--; pf_free_rule(rule); rule = NULL; - error = EINVAL; - ERROUT(error); + ERROUT(EEXIST); } PF_CONFIG_UNLOCK();