From nobody Sat Oct 05 09:51:51 2024 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 4XLLKh0J83z5Y4G9; Sat, 05 Oct 2024 09:51:52 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XLLKg6Nrfz4kNL; Sat, 5 Oct 2024 09:51:51 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1728121911; 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=pWBFHBQFt/CLN5TYEB3OqT1OTPeQyJVOMI8dFJeGRi4=; b=SfGv546e6b1cLxPHM8RmhmzSA9WJQZkSLcZRxeGOK158316ODQv7WjOY/kqbjf+tD7PHLO NvmJZos2R/S8gtTdiEWk7OyFa/uFBvvqNGch1jP4j3eutlINE36+T5nrGTmPAztnj0hWP7 xBqwU9QF4WqWphq2UnPmDenpVYDN81HuFMUtdBP8pYw7cEXXikpCfZGIRaMhWiJFQlGDIH HRJTHxUGkqi+Og7lkGjTY5dJ0Ie67JesBC9SS6QsdE0o4PqRx7IsEZHXaDL0PfNhAyCq41 xHEL/1PXne8q+q11qHhHh8sOK5Zh3Upf6nYRqnczCeulT7CmI4miWkt+B3K3SA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1728121911; 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=pWBFHBQFt/CLN5TYEB3OqT1OTPeQyJVOMI8dFJeGRi4=; b=cZLFhrZzGR5x5ETyIHVY+K4A4kzjKOpEh7H3RDXud8Am0R3dYP3Ygjb6DTf6aBS0Jl2PZ6 +NNo9CZCTiu+I6nMRaVG+NTGCcumtcCdFJNcriGKhTK+HLJiJHBN8qbXQSApu9COEpvFB0 yMaOK5KC4Tw4hyFGrvKergkNK8amyYBKYtZXw7u4NQKzKCqjDMXuZs5h2oGj005LytHsf7 hTpl+5vxW31xvBP2AoCzlwXMUr2G2th34F+iR2zNl1bulunzXhER/wVMJgBod3qyjG07Vy hoYzDTEY/sYrZ+8RJ4mltLIFdWg4VdI2MDAsgV01NWKj9O/Wzj36el8GtCYFoA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1728121911; a=rsa-sha256; cv=none; b=MwFDzfTRt8oLR5HRDHs7EjNF/fP8K041V7j0eTEdcdaHO69SwxYmtkCa/MRww6SOQoi8e2 4IuC31RFheAK1Ck2ddY7o/Iftl0OCwKJPGuUzS9JjEmJCyPid21M454Fm+syuc+6MkAnRW aj8uZIGnZpq8Oa8fu1HHHf+fg/IfVVx292Mki8ATrog5nYc8cY0xFOfYWWJWSl/SBVLU7g zrET2XipFx/UfuoZj5gRBue01CGxiTdaOPq27LDvwdwq6b+gldiMvDQQBvDy6rrw10gU1J 8tDjo5cDWhxRXfEbpX7u9SwixYOq1m7vruj1L0gALwX304o/IluHkeUAH0Xjkg== 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 4XLLKg5hKKz1Cqy; Sat, 5 Oct 2024 09:51:51 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4959ppcI076883; Sat, 5 Oct 2024 09:51:51 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4959ppFH076880; Sat, 5 Oct 2024 09:51:51 GMT (envelope-from git) Date: Sat, 5 Oct 2024 09:51:51 GMT Message-Id: <202410050951.4959ppFH076880@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Igor Ostapenko Subject: git: 9f146a81d2b3 - main - dummymbuf: Validate syntax upon write to net.dummymbuf.rules sysctl 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: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: igoro X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 9f146a81d2b33cfed0d5a494e798100c4e4f2a72 Auto-Submitted: auto-generated The branch main has been updated by igoro: URL: https://cgit.FreeBSD.org/src/commit/?id=9f146a81d2b33cfed0d5a494e798100c4e4f2a72 commit 9f146a81d2b33cfed0d5a494e798100c4e4f2a72 Author: Igor Ostapenko AuthorDate: 2024-10-05 08:04:08 +0000 Commit: Igor Ostapenko CommitDate: 2024-10-05 08:04:08 +0000 dummymbuf: Validate syntax upon write to net.dummymbuf.rules sysctl For now, opargs are not validated due to their runtime nature. Reviewed by: kp Approved by: kp (mentor) Differential Revision: https://reviews.freebsd.org/D46496 --- sys/net/dummymbuf.c | 61 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/sys/net/dummymbuf.c b/sys/net/dummymbuf.c index d4ba00b13235..99b6e6944752 100644 --- a/sys/net/dummymbuf.c +++ b/sys/net/dummymbuf.c @@ -40,6 +40,8 @@ #include #include +static int validate_rules(const char *); + /* * Separate sysctl sub-tree */ @@ -65,6 +67,7 @@ VNET_DEFINE_STATIC(struct sx, dmb_rules_lock); #define DMB_RULES_SUNLOCK() sx_sunlock(&V_dmb_rules_lock) #define DMB_RULES_XLOCK() sx_xlock(&V_dmb_rules_lock) #define DMB_RULES_XUNLOCK() sx_xunlock(&V_dmb_rules_lock) +#define DMB_RULES_LOCK_ASSERT() sx_assert(&V_dmb_rules_lock, SA_LOCKED) static int dmb_sysctl_handle_rules(SYSCTL_HANDLER_ARGS) @@ -86,12 +89,14 @@ dmb_sysctl_handle_rules(SYSCTL_HANDLER_ARGS) } else { /* read and write */ DMB_RULES_XLOCK(); - if (*rulesp == NULL) { - *rulesp = malloc(arg2, M_DUMMYMBUF_RULES, - M_WAITOK | M_ZERO); - } - arg1 = *rulesp; + arg1 = malloc(arg2, M_DUMMYMBUF_RULES, M_WAITOK | M_ZERO); error = sysctl_handle_string(oidp, arg1, arg2, req); + if (error == 0 && (error = validate_rules(arg1)) == 0) { + free(*rulesp, M_DUMMYMBUF_RULES); + *rulesp = arg1; + arg1 = NULL; + } + free(arg1, M_DUMMYMBUF_RULES); DMB_RULES_XUNLOCK(); } @@ -135,7 +140,13 @@ VNET_DEFINE_STATIC(pfil_hook_t, dmb_pfil_ethernet_hook); * Logging */ -#define FEEDBACK(pfil_type, pfil_flags, ifp, rule, msg) \ +#define FEEDBACK_RULE(rule, msg) \ + printf("dummymbuf: %s: %.*s\n", \ + (msg), \ + (rule).syntax_len, (rule).syntax_begin \ + ) + +#define FEEDBACK_PFIL(pfil_type, pfil_flags, ifp, rule, msg) \ printf("dummymbuf: %s %b %s: %s: %.*s\n", \ (pfil_type == PFIL_TYPE_IP4 ? "PFIL_TYPE_IP4" : \ pfil_type == PFIL_TYPE_IP6 ? "PFIL_TYPE_IP6" : \ @@ -198,7 +209,7 @@ bad: } static bool -read_rule(const char **cur, struct rule *rule) +read_rule(const char **cur, struct rule *rule, bool *eof) { // {inet | inet6 | ethernet} {in | out} [ ]; @@ -276,11 +287,37 @@ read_rule(const char **cur, struct rule *rule) return (false); rule->opargs = *cur; + // the next rule & eof *cur = delim + 1; + while (**cur == ' ') + (*cur)++; + *eof = strlen(*cur) == 0; return (true); } +static int +validate_rules(const char *rules) +{ + const char *cursor = rules; + bool parsed; + struct rule rule; + bool eof = false; + + DMB_RULES_LOCK_ASSERT(); + + while (!eof && (parsed = read_rule(&cursor, &rule, &eof))) { + /* noop */ + } + + if (!parsed) { + FEEDBACK_RULE(rule, "rule parsing failed"); + return (EINVAL); + } + + return (0); +} + static pfil_return_t dmb_pfil_mbuf_chk(int pfil_type, struct mbuf **mp, struct ifnet *ifp, int flags, void *ruleset, void *unused) @@ -289,26 +326,26 @@ dmb_pfil_mbuf_chk(int pfil_type, struct mbuf **mp, struct ifnet *ifp, const char *cursor; bool parsed; struct rule rule; + bool eof = false; DMB_RULES_SLOCK(); cursor = V_dmb_rules; - while ((parsed = read_rule(&cursor, &rule))) { + while (!eof && (parsed = read_rule(&cursor, &rule, &eof))) { if (rule.pfil_type == pfil_type && rule.pfil_dir == (flags & rule.pfil_dir) && strcmp(rule.ifname, ifp->if_xname) == 0) { m = rule.op(m, &rule); if (m == NULL) { - FEEDBACK(pfil_type, flags, ifp, rule, + FEEDBACK_PFIL(pfil_type, flags, ifp, rule, "mbuf operation failed"); break; } counter_u64_add(V_dmb_hits, 1); } - if (strlen(cursor) == 0) - break; } if (!parsed) { - FEEDBACK(pfil_type, flags, ifp, rule, "rule parsing failed"); + FEEDBACK_PFIL(pfil_type, flags, ifp, rule, + "rule parsing failed"); m_freem(m); m = NULL; }