From nobody Fri Oct 22 12:32:43 2021 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 4866717F7EB8; Fri, 22 Oct 2021 12:32:44 +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 4HbNzJ0N5Wz4ZV4; Fri, 22 Oct 2021 12:32:43 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 B83EA6D08; Fri, 22 Oct 2021 12:32:43 +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 19MCWh5t038540; Fri, 22 Oct 2021 12:32:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 19MCWh0V038539; Fri, 22 Oct 2021 12:32:43 GMT (envelope-from git) Date: Fri, 22 Oct 2021 12:32:43 GMT Message-Id: <202110221232.19MCWh0V038539@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kristof Provost Subject: git: 35237559a6a0 - stable/13 - pfctl: delay label macro expansion until after rule optimisation 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: kp X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 35237559a6a0ad0b16abcc6809c2e145931c5e9a Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=35237559a6a0ad0b16abcc6809c2e145931c5e9a commit 35237559a6a0ad0b16abcc6809c2e145931c5e9a Author: Kristof Provost AuthorDate: 2021-10-13 14:06:47 +0000 Commit: Kristof Provost CommitDate: 2021-10-22 07:34:25 +0000 pfctl: delay label macro expansion until after rule optimisation We used to expand the $nr macro in labels into the rule number prior to the optimisation step. This would occasionally produce incorrect rule numbers in the labels. Delay all macro expansion until after the optimisation step to ensure that we expand the correct values. MFC after: 1 week Reported by: Özkan KIRIK Differential Revision: https://reviews.freebsd.org/D32488 (cherry picked from commit 09c7f2386973f8c36978895a7600320916bc3d07) --- sbin/pfctl/parse.y | 85 +++++++++++++++++++++++------------------------------- sbin/pfctl/pfctl.c | 6 ++++ sbin/pfctl/pfctl.h | 2 ++ 3 files changed, 44 insertions(+), 49 deletions(-) diff --git a/sbin/pfctl/parse.y b/sbin/pfctl/parse.y index 7457e4346895..c8a310688ca7 100644 --- a/sbin/pfctl/parse.y +++ b/sbin/pfctl/parse.y @@ -330,14 +330,12 @@ int process_tabledef(char *, struct table_opts *); void expand_label_str(char *, size_t, const char *, const char *); void expand_label_if(const char *, char *, size_t, const char *); void expand_label_addr(const char *, char *, size_t, u_int8_t, - struct node_host *); + struct pf_rule_addr *); void expand_label_port(const char *, char *, size_t, - struct node_port *); + struct pf_rule_addr *); void expand_label_proto(const char *, char *, size_t, u_int8_t); -void expand_label_nr(const char *, char *, size_t); -void expand_label(char *, size_t, const char *, u_int8_t, - struct node_host *, struct node_port *, struct node_host *, - struct node_port *, u_int8_t); +void expand_label_nr(const char *, char *, size_t, + struct pfctl_rule *); void expand_rule(struct pfctl_rule *, struct node_if *, struct node_host *, struct node_proto *, struct node_os *, struct node_host *, struct node_port *, struct node_host *, @@ -4983,17 +4981,17 @@ expand_label_if(const char *name, char *label, size_t len, const char *ifname) void expand_label_addr(const char *name, char *label, size_t len, sa_family_t af, - struct node_host *h) + struct pf_rule_addr *addr) { char tmp[64], tmp_not[66]; if (strstr(label, name) != NULL) { - switch (h->addr.type) { + switch (addr->addr.type) { case PF_ADDR_DYNIFTL: - snprintf(tmp, sizeof(tmp), "(%s)", h->addr.v.ifname); + snprintf(tmp, sizeof(tmp), "(%s)", addr->addr.v.ifname); break; case PF_ADDR_TABLE: - snprintf(tmp, sizeof(tmp), "<%s>", h->addr.v.tblname); + snprintf(tmp, sizeof(tmp), "<%s>", addr->addr.v.tblname); break; case PF_ADDR_NOROUTE: snprintf(tmp, sizeof(tmp), "no-route"); @@ -5002,18 +5000,18 @@ expand_label_addr(const char *name, char *label, size_t len, sa_family_t af, snprintf(tmp, sizeof(tmp), "urpf-failed"); break; case PF_ADDR_ADDRMASK: - if (!af || (PF_AZERO(&h->addr.v.a.addr, af) && - PF_AZERO(&h->addr.v.a.mask, af))) + if (!af || (PF_AZERO(&addr->addr.v.a.addr, af) && + PF_AZERO(&addr->addr.v.a.mask, af))) snprintf(tmp, sizeof(tmp), "any"); else { char a[48]; int bits; - if (inet_ntop(af, &h->addr.v.a.addr, a, + if (inet_ntop(af, &addr->addr.v.a.addr, a, sizeof(a)) == NULL) snprintf(tmp, sizeof(tmp), "?"); else { - bits = unmask(&h->addr.v.a.mask, af); + bits = unmask(&addr->addr.v.a.mask, af); if ((af == AF_INET && bits < 32) || (af == AF_INET6 && bits < 128)) snprintf(tmp, sizeof(tmp), @@ -5029,7 +5027,7 @@ expand_label_addr(const char *name, char *label, size_t len, sa_family_t af, break; } - if (h->not) { + if (addr->neg) { snprintf(tmp_not, sizeof(tmp_not), "! %s", tmp); expand_label_str(label, len, name, tmp_not); } else @@ -5039,30 +5037,30 @@ expand_label_addr(const char *name, char *label, size_t len, sa_family_t af, void expand_label_port(const char *name, char *label, size_t len, - struct node_port *port) + struct pf_rule_addr *addr) { char a1[6], a2[6], op[13] = ""; if (strstr(label, name) != NULL) { - snprintf(a1, sizeof(a1), "%u", ntohs(port->port[0])); - snprintf(a2, sizeof(a2), "%u", ntohs(port->port[1])); - if (!port->op) + snprintf(a1, sizeof(a1), "%u", ntohs(addr->port[0])); + snprintf(a2, sizeof(a2), "%u", ntohs(addr->port[1])); + if (!addr->port_op) ; - else if (port->op == PF_OP_IRG) + else if (addr->port_op == PF_OP_IRG) snprintf(op, sizeof(op), "%s><%s", a1, a2); - else if (port->op == PF_OP_XRG) + else if (addr->port_op == PF_OP_XRG) snprintf(op, sizeof(op), "%s<>%s", a1, a2); - else if (port->op == PF_OP_EQ) + else if (addr->port_op == PF_OP_EQ) snprintf(op, sizeof(op), "%s", a1); - else if (port->op == PF_OP_NE) + else if (addr->port_op == PF_OP_NE) snprintf(op, sizeof(op), "!=%s", a1); - else if (port->op == PF_OP_LT) + else if (addr->port_op == PF_OP_LT) snprintf(op, sizeof(op), "<%s", a1); - else if (port->op == PF_OP_LE) + else if (addr->port_op == PF_OP_LE) snprintf(op, sizeof(op), "<=%s", a1); - else if (port->op == PF_OP_GT) + else if (addr->port_op == PF_OP_GT) snprintf(op, sizeof(op), ">%s", a1); - else if (port->op == PF_OP_GE) + else if (addr->port_op == PF_OP_GE) snprintf(op, sizeof(op), ">=%s", a1); expand_label_str(label, len, name, op); } @@ -5086,29 +5084,27 @@ expand_label_proto(const char *name, char *label, size_t len, u_int8_t proto) } void -expand_label_nr(const char *name, char *label, size_t len) +expand_label_nr(const char *name, char *label, size_t len, + struct pfctl_rule *r) { char n[11]; if (strstr(label, name) != NULL) { - snprintf(n, sizeof(n), "%u", pf->anchor->match); + snprintf(n, sizeof(n), "%u", r->nr); expand_label_str(label, len, name, n); } } void -expand_label(char *label, size_t len, const char *ifname, sa_family_t af, - struct node_host *src_host, struct node_port *src_port, - struct node_host *dst_host, struct node_port *dst_port, - u_int8_t proto) +expand_label(char *label, size_t len, struct pfctl_rule *r) { - expand_label_if("$if", label, len, ifname); - expand_label_addr("$srcaddr", label, len, af, src_host); - expand_label_addr("$dstaddr", label, len, af, dst_host); - expand_label_port("$srcport", label, len, src_port); - expand_label_port("$dstport", label, len, dst_port); - expand_label_proto("$proto", label, len, proto); - expand_label_nr("$nr", label, len); + expand_label_if("$if", label, len, r->ifname); + expand_label_addr("$srcaddr", label, len, r->af, &r->src); + expand_label_addr("$dstaddr", label, len, r->af, &r->dst); + expand_label_port("$srcport", label, len, &r->src); + expand_label_port("$dstport", label, len, &r->dst); + expand_label_proto("$proto", label, len, r->proto); + expand_label_nr("$nr", label, len, r); } int @@ -5442,15 +5438,6 @@ expand_rule(struct pfctl_rule *r, if (strlcpy(r->match_tagname, match_tagname, sizeof(r->match_tagname)) >= sizeof(r->match_tagname)) errx(1, "expand_rule: strlcpy"); - for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) - expand_label(r->label[i], PF_RULE_LABEL_SIZE, - r->ifname, r->af, src_host, src_port, dst_host, - dst_port, proto->proto); - expand_label(r->tagname, PF_TAG_NAME_SIZE, r->ifname, r->af, - src_host, src_port, dst_host, dst_port, proto->proto); - expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r->ifname, - r->af, src_host, src_port, dst_host, dst_port, - proto->proto); error += check_netmask(src_host, r->af); error += check_netmask(dst_host, r->af); diff --git a/sbin/pfctl/pfctl.c b/sbin/pfctl/pfctl.c index d7bde0012e9b..a0eec1b09289 100644 --- a/sbin/pfctl/pfctl.c +++ b/sbin/pfctl/pfctl.c @@ -1528,6 +1528,12 @@ pfctl_load_ruleset(struct pfctl *pf, char *path, struct pfctl_ruleset *rs, while ((r = TAILQ_FIRST(rs->rules[rs_num].active.ptr)) != NULL) { TAILQ_REMOVE(rs->rules[rs_num].active.ptr, r, entries); + + for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) + expand_label(r->label[i], PF_RULE_LABEL_SIZE, r); + expand_label(r->tagname, PF_TAG_NAME_SIZE, r); + expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r); + if ((error = pfctl_load_rule(pf, path, r, depth))) goto error; if (r->anchor) { diff --git a/sbin/pfctl/pfctl.h b/sbin/pfctl/pfctl.h index 80ef184fa90f..606eb729cd44 100644 --- a/sbin/pfctl/pfctl.h +++ b/sbin/pfctl/pfctl.h @@ -138,6 +138,8 @@ void pf_remove_if_empty_ruleset(struct pfctl_ruleset *); struct pfctl_ruleset *pf_find_ruleset(const char *); struct pfctl_ruleset *pf_find_or_create_ruleset(const char *); +void expand_label(char *, size_t, struct pfctl_rule *); + const char *pfctl_proto2name(int); #endif /* _PFCTL_H_ */