From patchwork Sun Aug 20 00:34:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Wang X-Patchwork-Id: 803632 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="SZErLJbF"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3xZdB13FtJz9t2r for ; Sun, 20 Aug 2017 10:34:33 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752363AbdHTAeZ (ORCPT ); Sat, 19 Aug 2017 20:34:25 -0400 Received: from mail-pf0-f180.google.com ([209.85.192.180]:32881 "EHLO mail-pf0-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752274AbdHTAeY (ORCPT ); Sat, 19 Aug 2017 20:34:24 -0400 Received: by mail-pf0-f180.google.com with SMTP id d5so6085811pfg.0 for ; Sat, 19 Aug 2017 17:34:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=dXdyl3p4mzX7IbOpVLoZcIHBULSDQuE2zqmsMdx+87g=; b=SZErLJbFFzUm/+in1t/EhjThGcSS6jpgXFysgvUtgixplOJ7U0N0wVJaQgHExo0YaZ 7oErdohyK1HKJ6PBTv/io8R9Mmvs27sM37pkIwnbv9nD4BZCfaIn6XcZ7J4M20BgL0Sz exWv0JMAzxU0kpcvtG2Zd1n9dDujti2BBQrsqNSzAEIDWIlZE44ui8UXLIiu2fdP/UPJ JVucNLHWFCqdQSUo3oGeSiwXBj69VBfyIx4UO9bYtF2a3f0ak/qPAZSnS0u+pG2QDIz9 qtqcst/6eyn9V/wmx+AlIBcct5c3ImKOfmc3q73vbKuFV38BJzu8NTJGQKMVJttcKo2S e4vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=dXdyl3p4mzX7IbOpVLoZcIHBULSDQuE2zqmsMdx+87g=; b=S2DYP9vik9qDl5TBED4/xgjEw0LGFxvniLStAUcbk2bHyJBMMXi8nnEXL4rhX+G+gf LR8g4TuuNsItGqxsb+nbV0MwlcmbmY+WGpvWyGkMGuobUiJwfC11cJEr3pyzOZYMd1Id i6tyZg2G5oe3/v2pFSmyuZvaQ2M/kWGlqD4850co00K04jYYXuakAwDygj+vaVhhYidD T/5x/jPyqbicLd8mQSYZbcVSFKS23lMlnHe93q3HmwfKWGs2lUXIdjwKOUFpO8V+vC3x 1JdLfTxKNNz+4XTY1hL4MG7NxJRusKxj+tF72fxy6QY+L93qgcByybOOWJzcAInLT/Sl u3MA== X-Gm-Message-State: AHYfb5jePsA2pEszTPfMRa3bSHo8QoJnnKyfCjquYfHQO9Dmql1DIoh+ 1FgsQ/4ho9vdrUw9 X-Received: by 10.99.169.25 with SMTP id u25mr12939161pge.128.1503189263402; Sat, 19 Aug 2017 17:34:23 -0700 (PDT) Received: from localhost ([2620:15c:2cb:201:3108:7d51:1182:eeb8]) by smtp.gmail.com with ESMTPSA id n16sm14852007pgc.84.2017.08.19.17.34.21 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Sat, 19 Aug 2017 17:34:22 -0700 (PDT) From: Wei Wang X-Google-Original-From: Wei Wang To: David Miller , netdev@vger.kernel.org Cc: Eric Dumazet , Martin KaFai Lau , Wei Wang Subject: [PATCH net v2] ipv6: add rcu grace period before freeing fib6_node Date: Sat, 19 Aug 2017 17:34:08 -0700 Message-Id: <20170820003408.133176-1-tracywwnj@gmail.com> X-Mailer: git-send-email 2.14.1.480.gb18f417b89-goog Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Wei Wang We currently keep rt->rt6i_node pointing to the fib6_node for the route. And some functions make use of this pointer to dereference the fib6_node from rt structure, e.g. rt6_check(). However, as there is neither refcount nor rcu taken when dereferencing rt->rt6i_node, it could potentially cause crashes as rt->rt6i_node could be set to NULL by other CPUs when doing a route deletion. This patch introduces an rcu grace period before freeing fib6_node and makes sure the functions that dereference it takes rcu_read_lock(). Note: there is no "Fixes" tag because this bug was there in a very early stage. Signed-off-by: Wei Wang Acked-by: Eric Dumazet Acked-by: Martin KaFai Lau --- v2: removed one extra empty line include/net/ip6_fib.h | 30 +++++++++++++++++++++++++++++- net/ipv6/ip6_fib.c | 20 ++++++++++++++++---- net/ipv6/route.c | 14 +++++++++++--- 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/include/net/ip6_fib.h b/include/net/ip6_fib.h index 71c1646298ae..977a86e3a8d9 100644 --- a/include/net/ip6_fib.h +++ b/include/net/ip6_fib.h @@ -72,6 +72,7 @@ struct fib6_node { __u16 fn_flags; int fn_sernum; struct rt6_info *rr_ptr; + struct rcu_head rcu; }; #ifndef CONFIG_IPV6_SUBTREES @@ -171,13 +172,40 @@ static inline void rt6_update_expires(struct rt6_info *rt0, int timeout) rt0->rt6i_flags |= RTF_EXPIRES; } +/* Function to safely get fn->sernum for passed in rt + * and store result in passed in cookie. + * Return true if we can get cookie safely + * Return false if not + */ +static inline bool rt6_get_cookie_safe(const struct rt6_info *rt, + u32 *cookie) +{ + struct fib6_node *fn; + bool status = false; + + rcu_read_lock(); + fn = rcu_dereference(rt->rt6i_node); + + if (fn) { + *cookie = fn->fn_sernum; + status = true; + } + + rcu_read_unlock(); + return status; +} + static inline u32 rt6_get_cookie(const struct rt6_info *rt) { + u32 cookie = 0; + if (rt->rt6i_flags & RTF_PCPU || (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from)) rt = (struct rt6_info *)(rt->dst.from); - return rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0; + rt6_get_cookie_safe(rt, &cookie); + + return cookie; } static inline void ip6_rt_put(struct rt6_info *rt) diff --git a/net/ipv6/ip6_fib.c b/net/ipv6/ip6_fib.c index 549aacc3cb2c..a9821c230e4e 100644 --- a/net/ipv6/ip6_fib.c +++ b/net/ipv6/ip6_fib.c @@ -149,11 +149,23 @@ static struct fib6_node *node_alloc(void) return fn; } -static void node_free(struct fib6_node *fn) +static void node_free_immediate(struct fib6_node *fn) +{ + kmem_cache_free(fib6_node_kmem, fn); +} + +static void node_free_rcu(struct rcu_head *head) { + struct fib6_node *fn = container_of(head, struct fib6_node, rcu); + kmem_cache_free(fib6_node_kmem, fn); } +static void node_free(struct fib6_node *fn) +{ + call_rcu(&fn->rcu, node_free_rcu); +} + void rt6_free_pcpu(struct rt6_info *non_pcpu_rt) { int cpu; @@ -697,9 +709,9 @@ static struct fib6_node *fib6_add_1(struct fib6_node *root, if (!in || !ln) { if (in) - node_free(in); + node_free_immediate(in); if (ln) - node_free(ln); + node_free_immediate(ln); return ERR_PTR(-ENOMEM); } @@ -1138,7 +1150,7 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt, root, and then (in failure) stale node in main tree. */ - node_free(sfn); + node_free_immediate(sfn); err = PTR_ERR(sn); goto failure; } diff --git a/net/ipv6/route.c b/net/ipv6/route.c index bec12ae3e6b7..4de2d793c4b8 100644 --- a/net/ipv6/route.c +++ b/net/ipv6/route.c @@ -1289,7 +1289,9 @@ static void rt6_dst_from_metrics_check(struct rt6_info *rt) static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) { - if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie)) + u32 rt_cookie; + + if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie) return NULL; if (rt6_check_expired(rt)) @@ -1357,8 +1359,14 @@ static void ip6_link_failure(struct sk_buff *skb) if (rt->rt6i_flags & RTF_CACHE) { if (dst_hold_safe(&rt->dst)) ip6_del_rt(rt); - } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) { - rt->rt6i_node->fn_sernum = -1; + } else { + struct fib6_node *fn; + + rcu_read_lock(); + fn = rcu_dereference(rt->rt6i_node); + if (fn && (rt->rt6i_flags & RTF_DEFAULT)) + fn->fn_sernum = -1; + rcu_read_unlock(); } } }