From nobody Sat Jan 1 19:30:57 2022 X-Original-To: bugs@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 691071925C69 for ; Sat, 1 Jan 2022 19:30:57 +0000 (UTC) (envelope-from bugzilla-noreply@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 4JRBv501hBz4dBD for ; Sat, 1 Jan 2022 19:30:57 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org (kenobi.freebsd.org [IPv6:2610:1c1:1:606c::50:1d]) (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 D547424591 for ; Sat, 1 Jan 2022 19:30:56 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org ([127.0.1.5]) by kenobi.freebsd.org (8.15.2/8.15.2) with ESMTP id 201JUu4R055110 for ; Sat, 1 Jan 2022 19:30:56 GMT (envelope-from bugzilla-noreply@freebsd.org) Received: (from www@localhost) by kenobi.freebsd.org (8.15.2/8.15.2/Submit) id 201JUutx055109 for bugs@FreeBSD.org; Sat, 1 Jan 2022 19:30:56 GMT (envelope-from bugzilla-noreply@freebsd.org) X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f From: bugzilla-noreply@freebsd.org To: bugs@FreeBSD.org Subject: [Bug 260867] [pf][patch] divert-to packets infinitely loop when written back to divert socket Date: Sat, 01 Jan 2022 19:30:57 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: Base System X-Bugzilla-Component: kern X-Bugzilla-Version: CURRENT X-Bugzilla-Keywords: X-Bugzilla-Severity: Affects Some People X-Bugzilla-Who: damjan.jov@gmail.com X-Bugzilla-Status: New X-Bugzilla-Resolution: X-Bugzilla-Priority: --- X-Bugzilla-Assigned-To: bugs@FreeBSD.org X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version rep_platform op_sys bug_status bug_severity priority component assigned_to reporter attachments.mimetype attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: https://bugs.freebsd.org/bugzilla/ Auto-Submitted: auto-generated List-Id: Bug reports List-Archive: https://lists.freebsd.org/archives/freebsd-bugs List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-bugs@freebsd.org MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1641065457; 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=SEHZsKE3qxLKka1NGHJjLLaNUPR8fpNz1gNcBwmhPe8=; b=m7rd1xvoVhaR0YztdqZP4ay6lkud7oss0DPeGPtD5voAg1qPXd3p5gZrGdb5JnrW6NqXLZ Orpf/FS5RubvLOKcXfC8STIUnsRjP9nVckl1w61ZQfHXqNX4Cq8f0W0L98x+tOenkODzwc K3RapYiqKBHq3fH1PSRi7MJ4fopR7fIWCsyV0LISXbdnzS5ABAcuu31CPuvZe+sTGSqkWy SriiyzWqYTgN/sd2UuABYqbblszgBgRm9xzkkuh+Hd7mZy5aopsY+4ItlfcGsqw0zcgC75 VoWpNOlkAelXjFYHQAPTIuqXfkHnWhTNgPuBIQ0zNa/Wj+C9m8eXnt3QO3Q8wA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1641065457; a=rsa-sha256; cv=none; b=g2QweMUDanWE023iIz6W6NiRTXxTvts6nhqDVbHFdWgsf/4fhiiMJ9d6TERdEO9UUm/653 Rs+GPsfMuCwalWuODdgD5K+Ym1nJcElJaJvQ2hEiY+wpXIa1OtOOZ1TmBlX+FoaHCBaX5d 5Ju8qIa4J83csM7vujQ1DzPqAM1CgpKKO7PMnY/lDN6rdCfADR2iHLD/vwCVLRW1lTlKYH 2umPgOneaBgz+hh3SOBQR6ZTRAXEI4zSXX2RSbtrca0R0BbFUiet3ekWuixlAEgetO0F2j zyWDQDx0/zYsoVu0rNwb385JNh8N6S3zt/52XNLAWQP4qZmmKBG5C8uWT6+CZQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D260867 Bug ID: 260867 Summary: [pf][patch] divert-to packets infinitely loop when written back to divert socket Product: Base System Version: CURRENT Hardware: Any OS: Any Status: New Severity: Affects Some People Priority: --- Component: kern Assignee: bugs@FreeBSD.org Reporter: damjan.jov@gmail.com Attachment #230608 text/plain mime type: Created attachment 230608 --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=3D230608&action= =3Dedit Divert socket test code On https://forums.freebsd.org/threads/pf-divert-to-loop-problem.81508 the poster describes how the "divert-to" rule creates packet loops on FreeBSD 1= 2.2, and I also independently reproduced this bug on 13.0 and 14-CURRENT too. It can be reproduced with this pf rule: pass out on em0 divert-to 0.0.0.0 port 2000 while running the attached C code, which binds a divert socket to 0.0.0.0:2= 000 and reads packets and writes them back unchanged. Adding some logging to the pf kernel module, I noticed that the PF_PACKET_LOOPED flag never gets set in the pf_test() function. Checking for conditions that set it which aren't being met, I think I found out why. The following one line change fixes the issue for me: ---snip--- diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c index 1686def4626..bd71d338517 100644 --- a/sys/netpfil/pf/pf.c +++ b/sys/netpfil/pf/pf.c @@ -6496,7 +6496,7 @@ pf_test(int dir, int pflags, struct ifnet *ifp, struct mbuf **m0, struct inpcb * if (__predict_false(ip_divert_ptr !=3D NULL) && ((ipfwtag =3D m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL)) !=3D NU= LL)) { struct ipfw_rule_ref *rr =3D (struct ipfw_rule_ref *)(ipfwt= ag+1); - if (rr->info & IPFW_IS_DIVERT && rr->rulenum =3D=3D 0) { + if (rr->info & IPFW_IS_DIVERT /*&& rr->rulenum =3D=3D 0*/) { if (pd.pf_mtag =3D=3D NULL && ((pd.pf_mtag =3D pf_get_mtag(m)) =3D=3D NULL)) { action =3D PF_DROP; ---snip--- Why does that work? It appears that the "rulenum" field is only written to in this one place: ((struct ipfw_rule_ref *)(ipfwtag+1))->rulenum =3D = dir; and if "dir" is what I think it is, then as per /usr/include/netpfil/pf/pf.= h: enum { PF_INOUT, PF_IN, PF_OUT }; the 0 in "rr->rulenum =3D=3D 0" would be PF_INOUT, which packets never are.= However checking for values 1 and 2 instead, didn't seem to fix the issue either. O= nly deleting the entire rr->rulenum check seems to fix it. --=20 You are receiving this mail because: You are the assignee for the bug.=