From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 110C2EDF7 for ; Tue, 13 Dec 2022 13:33:17 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id E373C1F78A for ; Tue, 13 Dec 2022 13:33:16 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Tue, 13 Dec 2022 13:33:15 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 9A57544FBE for ; Tue, 13 Dec 2022 13:33:15 +0100 (CET) Date: Tue, 13 Dec 2022 13:33:14 +0100 From: Wolfgang Bumiller To: Stefan Hrdlicka Cc: pve-devel@lists.proxmox.com Message-ID: <20221213123314.ywxipwbxwvfomchf@casey.proxmox.com> References: <20221129160152.1119611-1-s.hrdlicka@proxmox.com> <20221129160152.1119611-2-s.hrdlicka@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221129160152.1119611-2-s.hrdlicka@proxmox.com> X-SPAM-LEVEL: Spam detection results: 0 AWL 0.222 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [ipset.pm, firewall.pm] Subject: Re: [pve-devel] [PATCH V2 firewall 1/2] allow non zero ip address host bits to be entered X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 13 Dec 2022 12:33:17 -0000 On Tue, Nov 29, 2022 at 05:01:51PM +0100, Stefan Hrdlicka wrote: > They can already be set directly via the cluster.fw file. Net::IP is just a > bit more picky with what it allows: > For example: > error: 192.168.1.155/24 > correct: 192.168.1.0/24 > > This cleans the entered IP and removes the non zero host bits. > > Signed-off-by: Stefan Hrdlicka > --- > src/PVE/API2/Firewall/IPSet.pm | 2 +- > src/PVE/Firewall.pm | 29 ++++++++++++++++++++++++++++- > 2 files changed, 29 insertions(+), 2 deletions(-) > > diff --git a/src/PVE/API2/Firewall/IPSet.pm b/src/PVE/API2/Firewall/IPSet.pm > index a5f69e9..14bcfcb 100644 > --- a/src/PVE/API2/Firewall/IPSet.pm > +++ b/src/PVE/API2/Firewall/IPSet.pm > @@ -199,7 +199,7 @@ sub register_create_ip { > > my ($cluster_conf, $fw_conf, $ipset) = $class->load_config($param); > > - my $cidr = $param->{cidr}; > + my $cidr = PVE::Firewall::clean_cidr($param->{cidr}); > if ($cidr =~ m/^${PVE::Firewall::ip_alias_pattern}$/) { > # make sure alias exists (if $cidr is an alias) > PVE::Firewall::resolve_alias($cluster_conf, $fw_conf, $cidr); > diff --git a/src/PVE/Firewall.pm b/src/PVE/Firewall.pm > index d40a9b1..3c35b44 100644 > --- a/src/PVE/Firewall.pm > +++ b/src/PVE/Firewall.pm > @@ -69,8 +69,12 @@ sub pve_verify_ip_or_cidr { > my ($cidr, $noerr) = @_; > > if ($cidr =~ m!^(?:$IPV6RE|$IPV4RE)(/(\d+))?$!) { > - return $cidr if Net::IP->new($cidr); > + # Net::IP throws an error if the masked CIDR part isn't zero, e.g., `192.168.1.155/24` > + # fails but `192.168.1.0/24` succeeds. clean_cidr removes the non zero bits from the CIDR. > + my $clean_cidr = clean_cidr($cidr); > + return $cidr if Net::IP->new($clean_cidr); > return undef if $noerr; > + > die Net::IP::Error() . "\n"; > } > return undef if $noerr; > @@ -86,6 +90,29 @@ sub pve_verify_ip_or_cidr_or_alias { > return pve_verify_ip_or_cidr($cidr, $noerr); > } > > +sub clean_cidr { > + my ($cidr) = @_; > + my ($ip, $len) = split('/', $cidr); > + return $cidr if !$len; > + > + my $clean_func = sub { > + my ($ver) = @_; > + my $bin_ip = Net::IP::ip_iptobin( Net::IP::ip_expand_address($ip, $ver), $ver); > + my $bin_mask = Net::IP::ip_get_mask($len, $ver); > + my $clean_ip = Net::IP::ip_compress_address( Net::IP::ip_bintoip($bin_ip & $bin_mask, $ver), $ver); > + > + return "${clean_ip}/$len"; > + }; > + > + if ($ip =~ m!^$IPV4RE$!) { > + return &$clean_func(4) > + } elsif ($ip =~ m!^$IPV6RE$!) { > + return &$clean_func(6); > + } IMO we should either say "we know our input was validated already" and check only one (then we don't need the closure and can just do my $ver = ($ip =~ v4re...) ? 4 : 6; or we also handle the `else` case here with an error. Otherwise, LGTM > + > + return $cidr; > +} > + > PVE::JSONSchema::register_standard_option('ipset-name', { > description => "IP set name.", > type => 'string', > -- > 2.30.2