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=-16.6 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 B10DEC43381 for ; Fri, 15 Mar 2019 03:19:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7E0B5218A1 for ; Fri, 15 Mar 2019 03:19:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="lnu3YujY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726969AbfCODTx (ORCPT ); Thu, 14 Mar 2019 23:19:53 -0400 Received: from mail-pg1-f201.google.com ([209.85.215.201]:49515 "EHLO mail-pg1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726571AbfCODTw (ORCPT ); Thu, 14 Mar 2019 23:19:52 -0400 Received: by mail-pg1-f201.google.com with SMTP id e5so8589669pgc.16 for ; Thu, 14 Mar 2019 20:19:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=Em20OcjY8GCBQerrJZx4AAW+lj6AWJja8X1/3KPZ9G8=; b=lnu3YujYBlbK2elOA4bW2LEbF1A4mhNSGnS1xhgTsM5R7iKHn32XcIM/NLIKL23Jfd TlHwAxeknvQzFnI1kdg+3T5h+6nQaIByEKKx7b0oDqT3EftXwBF9oO0QuJZyIvFoHtws z8xAD94uiN/gUMomnWbhjOZf6bY+LsHlHsZBm3OunxZ1z1TXDr3WXnN+MyF6TAqaD+A/ vZ1Ho8i98tlfFnWtNfbxLTB38B7vOwhigbpSFM4NlrIhoonObqHOwI+dpJd9+T6NAo5m MEEv3gynipTK2wb6oDnMsYrT49sf7FJtJomcwT99tnTcIzvc7eTHJuaq2Ol4Mhv1rmkK 68uQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=Em20OcjY8GCBQerrJZx4AAW+lj6AWJja8X1/3KPZ9G8=; b=AbuDkpQvyMKfNx5+0lKYJLC2VasvePMm65YMZ5F+UydY5N9a+UjbZyIhRvX06IVqxb Wgc5KF9W6cNjnyQsJC5Da3bWiFVWQwBCWd/1f3LeGyjHn2jZWQgyu4ChIhP392r0qq2i OCV9QZ2W0fm3NdohhIEf6yICYep10H/oBhf5DSbNbSZsgB3s9Y4PLtLwe9k6BN+2Muy5 ICTdgo6Yd/8YEft7AvL/aQdUE8p6SOt0i/zOjPm3bC7aEWajRXx10B+h3pccy1wLxOht vOyofTOLBEKI/nn9Oc9dSDzw/CvT8JXlKxGpB/71j6vCTDCkw+k+3SYNlAp8opiIV1uL u2wg== X-Gm-Message-State: APjAAAWRxHomeu8fx0eoD2H/3jqHH3yrxJnSJGCN8zTLuXW5SaCqi2Iz nGLgWtO491vYYrCOwcXHcWR/w7QXyuPtSA== X-Google-Smtp-Source: APXvYqwmjKBOaioZ8hQGn4VVAavCR5e8kF1Q9LjHF2JiQvOOInMVstLQVRsEwYQixP5PaV8cSJ7wTvXOWvPrAw== X-Received: by 2002:a17:902:503:: with SMTP id 3mr482152plf.15.1552619991630; Thu, 14 Mar 2019 20:19:51 -0700 (PDT) Date: Thu, 14 Mar 2019 20:19:47 -0700 Message-Id: <20190315031947.63245-1-edumazet@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.21.0.360.g471c308f928-goog Subject: [PATCH net] tun: properly test for IFF_UP From: Eric Dumazet To: "David S . Miller" Cc: netdev , Eric Dumazet , Eric Dumazet , syzbot Content-Type: text/plain; charset="UTF-8" Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Same reasons than the ones explained in commit 4179cb5a4c92 ("vxlan: test dev->flags & IFF_UP before calling netif_rx()") netif_rx_ni() or napi_gro_frags() must be called under a strict contract. At device dismantle phase, core networking clears IFF_UP and flush_all_backlogs() is called after rcu grace period to make sure no incoming packet might be in a cpu backlog and still referencing the device. A similar protocol is used for gro layer. Most drivers call netif_rx() from their interrupt handler, and since the interrupts are disabled at device dismantle, netif_rx() does not have to check dev->flags & IFF_UP Virtual drivers do not have this guarantee, and must therefore make the check themselves. Fixes: 1bd4978a88ac ("tun: honor IFF_UP in tun_get_user()") Signed-off-by: Eric Dumazet Reported-by: syzbot --- drivers/net/tun.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 1d68921723dc08532b3f5321a52865076ad66336..0d343359f647ff58fee35462358827e61857c837 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -1763,9 +1763,6 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, int skb_xdp = 1; bool frags = tun_napi_frags_enabled(tfile); - if (!(tun->dev->flags & IFF_UP)) - return -EIO; - if (!(tun->flags & IFF_NO_PI)) { if (len < sizeof(pi)) return -EINVAL; @@ -1867,6 +1864,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, err = skb_copy_datagram_from_iter(skb, 0, from, len); if (err) { + err = -EFAULT; +drop: this_cpu_inc(tun->pcpu_stats->rx_dropped); kfree_skb(skb); if (frags) { @@ -1874,7 +1873,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, mutex_unlock(&tfile->napi_mutex); } - return -EFAULT; + return err; } } @@ -1958,6 +1957,12 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, !tfile->detached) rxhash = __skb_get_hash_symmetric(skb); + rcu_read_lock(); + if (unlikely(!(tun->dev->flags & IFF_UP))) { + err = -EIO; + goto drop; + } + if (frags) { /* Exercise flow dissector code path. */ u32 headlen = eth_get_headlen(skb->data, skb_headlen(skb)); @@ -1965,6 +1970,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, if (unlikely(headlen > skb_headlen(skb))) { this_cpu_inc(tun->pcpu_stats->rx_dropped); napi_free_frags(&tfile->napi); + rcu_read_unlock(); mutex_unlock(&tfile->napi_mutex); WARN_ON(1); return -ENOMEM; @@ -1992,6 +1998,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, } else { netif_rx_ni(skb); } + rcu_read_unlock(); stats = get_cpu_ptr(tun->pcpu_stats); u64_stats_update_begin(&stats->syncp); -- 2.21.0.360.g471c308f928-goog