From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.3 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8EBD1C352A4 for ; Mon, 10 Feb 2020 22:26:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6CAFD20733 for ; Mon, 10 Feb 2020 22:26:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727518AbgBJW0V (ORCPT ); Mon, 10 Feb 2020 17:26:21 -0500 Received: from Chamillionaire.breakpoint.cc ([193.142.43.52]:33496 "EHLO Chamillionaire.breakpoint.cc" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727385AbgBJW0V (ORCPT ); Mon, 10 Feb 2020 17:26:21 -0500 Received: from fw by Chamillionaire.breakpoint.cc with local (Exim 4.92) (envelope-from ) id 1j1HVO-0000cw-PD; Mon, 10 Feb 2020 23:26:14 +0100 Date: Mon, 10 Feb 2020 23:26:14 +0100 From: Florian Westphal To: "Jason A. Donenfeld" Cc: Florian Westphal , Netdev , David Miller Subject: Re: [PATCH v2 net 1/5] icmp: introduce helper for NAT'd source address in network device context Message-ID: <20200210222614.GJ2991@breakpoint.cc> References: <20200210141423.173790-1-Jason@zx2c4.com> <20200210141423.173790-2-Jason@zx2c4.com> <20200210213259.GI2991@breakpoint.cc> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Jason A. Donenfeld wrote: > Hi Florian, > > On Mon, Feb 10, 2020 at 10:33 PM Florian Westphal wrote: > > > > Jason A. Donenfeld wrote: > > > On Mon, Feb 10, 2020 at 3:15 PM Jason A. Donenfeld wrote: > > > > + ip_hdr(skb_in)->saddr = ct->tuplehash[0].tuple.src.u3.ip; > > > > + } > > > > + icmp_send(skb_in, type, code, info); > > > > > > According to the comments in icmp_send, access to > > > ip_hdr(skb_in)->saddr requires first checking for `if > > > (skb_network_header(skb_in) < skb_in->head || > > > (skb_network_header(skb_in) + sizeof(struct iphdr)) > > > > skb_tail_pointer(skb_in))` first to be safe. > > > > You will probably also need skb_ensure_writable() to handle cloned skbs. > > > > I also suggest to check "ct->status & IPS_NAT_MASK", nat is only done if > > those bits are set. > > Thanks for the suggestions. I've made these changes and they're queued > up for a v3, currently staged in wireguard-linux.git's stable branch: > https://git.zx2c4.com/wireguard-linux/log/?h=stable I think this is a bit too conservative, f.e. i don't see how ndo-called skbs could be shared (tx path needs to be able to change skb list pointers)? If its needed it looks ok. Otherwise, I would suggest something like this: void icmp_ndo_send(struct sk_buff *skb_in, int type, int code, __be32 info) { enum ip_conntrack_info ctinfo; struct nf_conn *ct; __be32 orig_ip; ct = nf_ct_get(skb_in, &ctinfo); if (!ct || ((ct->status & IPS_NAT_MASK) == 0) { icmp_send(skb_in, type, code, info); return; } /* avoid reallocations */ if (skb_network_header(skb_in) < skb_in->head || (skb_network_header(skb_in) + sizeof(struct iphdr)) > skb_tail_pointer(skb_in)) return; /* handle clones. NB: if reallocations are to be avoided, then * if (skb_cloned(skb_in) && * !skb_clone_writable(skb_in, skb_network_offset(skb_in) + iphlen)) * * ... should be placed here instead: */ if (unlikely(skb_ensure_writable(skb_in, skb_network_offset(skb_in) + sizeof(struct iphdr)))) return; orig_ip = ip_hdr(skb_in)->saddr; ip_hdr(skb_in)->saddr = ct->tuplehash[0].tuple.src.u3.ip; icmp_send(skb_in, type, code, info); ip_hdr(skb_in)->saddr = orig_ip; }