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=-8.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT 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 6EFC8C43381 for ; Fri, 22 Mar 2019 23:26:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2F643218D4 for ; Fri, 22 Mar 2019 23:26:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="G5aB/y13" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727539AbfCVX00 (ORCPT ); Fri, 22 Mar 2019 19:26:26 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:44200 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726037AbfCVX00 (ORCPT ); Fri, 22 Mar 2019 19:26:26 -0400 Received: by mail-pg1-f194.google.com with SMTP id i2so2488223pgj.11 for ; Fri, 22 Mar 2019 16:26:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=/tQDuAX9hy92izeeEBIJWsf15q27PebxAatBFj6aNws=; b=G5aB/y132X3J4TjJyRSSnllOsZuP7sfSnj9f2N8HdWazFoLwISjYzkaq1t8V6ReAgx 6EgZVL6LvelYaHYY8W8JMpn/mtXcJH0c1hfzg7fYEw0f0EfVcyYrr/3BaTvoAftKzfKC tMVjaJcYmYaeqlqpNaxLCN8CrAcXRnpSyIFo9P4tdKdemLRGM/NTVRoFJ8tuJEGG/lbB HFz9xQgO5MUigDXxybvrmdFBYF0HSrIhuLqEMYxzpwSk11oYc9/2V8WrLOiX6+tboN1d /1cYvw+jGp79THkyjLJkhwSfteNFELaeMAuurLZ5mYLedqoh05K1ar+lAUM/0iOSwd8r SXTA== 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:mime-version :content-transfer-encoding; bh=/tQDuAX9hy92izeeEBIJWsf15q27PebxAatBFj6aNws=; b=EyV2GKGfvE/VZRIwjGBYe5kTWHV3CAalGad66TUTvrdn9ubGjYnVCE9Dfdk06hSgQe l4LWvnozttbSsqEzljBQ3YtRzkhrXhPWPpTmXQmAkDLpcKHZRDHnxa5uFDrbIothVWFS Kxs1TUuVWyNX1cQz7CtwY2QACBPvxMi0rfgtgJzWch04/0Cef2IGFnorQt5h2rPfdh3v QmtiU3QgCoKDbANpM5po2CCD5pupuzF1T5w5FuqzfsQnur40TYPNstMSSIBFmN2qKsCB If0Pesp0Y8rKgMZIqA6BZWk7s0R7eRPDt4NF6Bt42MfIF4FN/1lYeWOH8VivPTTDzi8+ 1Cbg== X-Gm-Message-State: APjAAAXtXVRFZZHniCpwu8qWo4O6o8bQYMtrO1KrAxdn12bfHMcQxeYv +bp7emFWb+dtW5rL+XfQMdzQIM4W X-Google-Smtp-Source: APXvYqzmPe6cpvPDjAPFOrdEHeBvrFKdYa6TyZQmcScV0779FLa0bgAhVeOAkvd6w5P71UleVSM94w== X-Received: by 2002:aa7:85d9:: with SMTP id z25mr1195113pfn.31.1553297185659; Fri, 22 Mar 2019 16:26:25 -0700 (PDT) Received: from tw-172-25-17-123.office.twttr.net ([8.25.197.27]) by smtp.gmail.com with ESMTPSA id z189sm12822096pfb.146.2019.03.22.16.26.24 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 22 Mar 2019 16:26:24 -0700 (PDT) From: Cong Wang To: netdev@vger.kernel.org Cc: Cong Wang , syzbot+0bf0519d6e0de15914fe@syzkaller.appspotmail.com, Steffen Klassert , Herbert Xu Subject: [Patch net v2] xfrm: clean up xfrm protocol checks Date: Fri, 22 Mar 2019 16:26:19 -0700 Message-Id: <20190322232619.11148-1-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org In commit 6a53b7593233 ("xfrm: check id proto in validate_tmpl()") I introduced a check for xfrm protocol, but according to Herbert IPSEC_PROTO_ANY should only be used as a wildcard for lookup, so it should be removed from validate_tmpl(). And, IPSEC_PROTO_ANY is expected to only match 3 IPSec-specific protocols, this is why xfrm_state_flush() could still miss IPPROTO_ROUTING, which leads that those entries are left in net->xfrm.state_all before exit net. Fix this by replacing IPSEC_PROTO_ANY with zero. This patch also extracts the check from validate_tmpl() to xfrm_id_proto_valid() and uses it in parse_ipsecrequest(). With this, no other protocols should be added into xfrm. Fixes: 6a53b7593233 ("xfrm: check id proto in validate_tmpl()") Reported-by: syzbot+0bf0519d6e0de15914fe@syzkaller.appspotmail.com Cc: Steffen Klassert Cc: Herbert Xu Signed-off-by: Cong Wang --- include/net/xfrm.h | 17 +++++++++++++++++ net/ipv6/xfrm6_tunnel.c | 2 +- net/key/af_key.c | 4 +++- net/xfrm/xfrm_state.c | 2 +- net/xfrm/xfrm_user.c | 14 +------------- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/include/net/xfrm.h b/include/net/xfrm.h index 85386becbaea..902437dfbce7 100644 --- a/include/net/xfrm.h +++ b/include/net/xfrm.h @@ -1404,6 +1404,23 @@ static inline int xfrm_state_kern(const struct xfrm_state *x) return atomic_read(&x->tunnel_users); } +static inline bool xfrm_id_proto_valid(u8 proto) +{ + switch (proto) { + case IPPROTO_AH: + case IPPROTO_ESP: + case IPPROTO_COMP: +#if IS_ENABLED(CONFIG_IPV6) + case IPPROTO_ROUTING: + case IPPROTO_DSTOPTS: +#endif + return true; + default: + return false; + } +} + +/* IPSEC_PROTO_ANY only matches 3 IPsec protocols, 0 could match all. */ static inline int xfrm_id_proto_match(u8 proto, u8 userproto) { return (!userproto || proto == userproto || diff --git a/net/ipv6/xfrm6_tunnel.c b/net/ipv6/xfrm6_tunnel.c index bc65db782bfb..eb2b844b9796 100644 --- a/net/ipv6/xfrm6_tunnel.c +++ b/net/ipv6/xfrm6_tunnel.c @@ -345,7 +345,7 @@ static void __net_exit xfrm6_tunnel_net_exit(struct net *net) unsigned int i; xfrm_flush_gc(); - xfrm_state_flush(net, IPSEC_PROTO_ANY, false, true); + xfrm_state_flush(net, 0, false, true); for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++) WARN_ON_ONCE(!hlist_empty(&xfrm6_tn->spi_byaddr[i])); diff --git a/net/key/af_key.c b/net/key/af_key.c index 5651c29cb5bd..4af1e1d60b9f 100644 --- a/net/key/af_key.c +++ b/net/key/af_key.c @@ -1951,8 +1951,10 @@ parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq) if (rq->sadb_x_ipsecrequest_mode == 0) return -EINVAL; + if (!xfrm_id_proto_valid(rq->sadb_x_ipsecrequest_proto)) + return -EINVAL; - t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */ + t->id.proto = rq->sadb_x_ipsecrequest_proto; if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0) return -EINVAL; t->mode = mode; diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c index 1bb971f46fc6..178baaa037e5 100644 --- a/net/xfrm/xfrm_state.c +++ b/net/xfrm/xfrm_state.c @@ -2384,7 +2384,7 @@ void xfrm_state_fini(struct net *net) flush_work(&net->xfrm.state_hash_work); flush_work(&xfrm_state_gc_work); - xfrm_state_flush(net, IPSEC_PROTO_ANY, false, true); + xfrm_state_flush(net, 0, false, true); WARN_ON(!list_empty(&net->xfrm.state_all)); diff --git a/net/xfrm/xfrm_user.c b/net/xfrm/xfrm_user.c index a131f9ff979e..067ac9ed2918 100644 --- a/net/xfrm/xfrm_user.c +++ b/net/xfrm/xfrm_user.c @@ -1513,20 +1513,8 @@ static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family) return -EINVAL; } - switch (ut[i].id.proto) { - case IPPROTO_AH: - case IPPROTO_ESP: - case IPPROTO_COMP: -#if IS_ENABLED(CONFIG_IPV6) - case IPPROTO_ROUTING: - case IPPROTO_DSTOPTS: -#endif - case IPSEC_PROTO_ANY: - break; - default: + if (!xfrm_id_proto_valid(ut[i].id.proto)) return -EINVAL; - } - } return 0; -- 2.20.1