Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp3939804imm; Tue, 29 May 2018 17:36:15 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLxxh1YSsGUsvIp8T2pVgazPZZuXzW2/ZXmceor168WDTF28yHfA8reZw/ZQcO/GMFDomZS X-Received: by 2002:a62:1fd6:: with SMTP id l83-v6mr587183pfj.182.1527640575156; Tue, 29 May 2018 17:36:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527640575; cv=none; d=google.com; s=arc-20160816; b=nZ3zufwlDi13oXC3bGCWYi6Ssnx9BOSuefLiw4sYiTN2cxieQzEBlCw9F2BLnQ1qjY QhnO5f0mo7tFTWkGl/vxrqQ2cOcGMlFgt9qcejjmBruVKHs3lN761Q8UNZiWPstbyFnA oxI5lv5thRggMNrha2Sa/gW+GRr9ubWej1SFloTnAKZWGGt301S2Xz3fuXskaLxhRqWF ccsjH5pFatpqbcONp67mIq9/zCkvDNpqaihOt+sAAuuJzDdS6NjCsNGuM60Dx+mpMGfp s+EI/KS7xHRPmiCnR/ikbKUM5q2QJ1Ip8r1BtQtj3SEDtPrLbP7hXrcl6NehBStKDO74 hnyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition:mime-version :message-id:subject:cc:to:from:date:dkim-signature :arc-authentication-results; bh=q7J1rlwh70neq5f26YgQ/3ZTZbXafl8FrukwZC6aNhM=; b=npA+tZlZ/Pc2AdSX4+H+mHhf4CgvIP8qJ2VxDTU3GZK1RyBvoPHsYuavCpoXpIQLHs jQTI8VUG7ZH+M3Q+9u4cDxq6nxwglH6zGuaUyU3Lt7kkDakth0HNrmVzIQdnNRFhx+sU xOSTCC5Eir66c9lV9AjeuJStBYu1/3x/kjxpX/bDY4Sc+cdVJhCg/6MuMNA9ksqsJLHa 03i84twVIrXR4aao1Kx1fxTO4nKvaVNoZvuxR0uYhlFm7mPEOMugZLxnqzeBdHifxq46 cVyZPpYYmF4HNpmV4CwdwbcvnTSFnFiCDDKA4xROHEtdRwpwiw5y/B8nVXwEbqYSsHzl y3Hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kqNdQfwo; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a64-v6si34066990pla.530.2018.05.29.17.36.00; Tue, 29 May 2018 17:36:15 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kqNdQfwo; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966552AbeE3Afb (ORCPT + 99 others); Tue, 29 May 2018 20:35:31 -0400 Received: from mail-pf0-f195.google.com ([209.85.192.195]:41792 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935750AbeE3Af2 (ORCPT ); Tue, 29 May 2018 20:35:28 -0400 Received: by mail-pf0-f195.google.com with SMTP id v63-v6so8082837pfk.8 for ; Tue, 29 May 2018 17:35:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=q7J1rlwh70neq5f26YgQ/3ZTZbXafl8FrukwZC6aNhM=; b=kqNdQfwo6tJcgUJ6APJJeRyeq32avO6zQfv/2jaFZJSGWKWXKwhN+alsLFvkqTNsWi E3tidIUoCCMF8AAWQb3skGpPXYABmp/3hZGDzsx/UhyJyTMFcMJrpfhg6pu94laLCGys o0hEuyK+9QEnKZSWDtwWSIe5cxEkF2x5G231w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=q7J1rlwh70neq5f26YgQ/3ZTZbXafl8FrukwZC6aNhM=; b=FDE+Tf0ZGjGx3XP2kOlywcuSx5RCaztKBitmkPDAijzSp7TsBir7df241TdhpjXdGO YYaY4UG9049DYmNwFWlxSda6t1t1pfGuxgCGJuHg+0pbsLp7qMzY/n3uvYXPnBQ9682S eDMiA0I4NYZxalaSd0RwtvQSjlmw3vX97Lf8sWhphT7SPkv6RNAMvpfIxGKHZ331hC3f jXP+R7bOkN9ZcSgSEW1EDsqwdsWyF4arg6zQ/NyavYJ8kTikxDfQluMQIMkoBKqVsgJe eWr1R8r5hh3lh5lNBrYxiUsHO67NuHRUPa/XRnYK/2UE6MbGHLO3Kh/VKKabmnoAw+j0 3LDg== X-Gm-Message-State: ALKqPwdZbKmZkt2yFM0S83ww4IZ5p5kPN0UpQ1r0o/59VUrNLB9rAYLQ +nEKmO2lCdSnjrg5oQaEu1WJ5w== X-Received: by 2002:a62:f24a:: with SMTP id y10-v6mr567641pfl.75.1527640527767; Tue, 29 May 2018 17:35:27 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id l67-v6sm19350422pfj.66.2018.05.29.17.35.25 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 29 May 2018 17:35:26 -0700 (PDT) Date: Tue, 29 May 2018 17:35:25 -0700 From: Kees Cook To: Pablo Neira Ayuso Cc: Jozsef Kadlecsik , Florian Westphal , "David S. Miller" , netfilter-devel@vger.kernel.org, coreteam@netfilter.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] netfilter: nfnetlink: Remove VLA usage Message-ID: <20180530003525.GA18642@beast> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In the quest to remove all stack VLA usage from the kernel[1], this allocates the maximum size expected for all possible attrs and adds a sanity-check to make sure nothing gets out of sync. [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com Signed-off-by: Kees Cook --- net/netfilter/nfnetlink.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/net/netfilter/nfnetlink.c b/net/netfilter/nfnetlink.c index 03ead8a9e90c..0cb395f9627e 100644 --- a/net/netfilter/nfnetlink.c +++ b/net/netfilter/nfnetlink.c @@ -28,6 +28,7 @@ #include #include +#include MODULE_LICENSE("GPL"); MODULE_AUTHOR("Harald Welte "); @@ -37,6 +38,11 @@ MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER); rcu_dereference_protected(table[(id)].subsys, \ lockdep_nfnl_is_held((id))) +#define NFTA_MAX_ATTR max(max(max(NFTA_CHAIN_MAX, NFTA_FLOWTABLE_MAX),\ + max(NFTA_OBJ_MAX, NFTA_RULE_MAX)), \ + max(NFTA_TABLE_MAX, \ + max(NFTA_SET_ELEM_LIST_MAX, NFTA_SET_MAX))) + static struct { struct mutex mutex; const struct nfnetlink_subsystem __rcu *subsys; @@ -185,11 +191,17 @@ static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, { int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type); - struct nlattr *cda[ss->cb[cb_id].attr_count + 1]; + struct nlattr *cda[NFTA_MAX_ATTR + 1]; struct nlattr *attr = (void *)nlh + min_len; int attrlen = nlh->nlmsg_len - min_len; __u8 subsys_id = NFNL_SUBSYS_ID(type); + /* Sanity-check NFTA_MAX_ATTR */ + if (ss->cb[cb_id].attr_count > NFTA_MAX_ATTR) { + rcu_read_unlock(); + return -ENOMEM; + } + err = nla_parse(cda, ss->cb[cb_id].attr_count, attr, attrlen, ss->cb[cb_id].policy, extack); if (err < 0) { @@ -379,10 +391,16 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, { int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type); - struct nlattr *cda[ss->cb[cb_id].attr_count + 1]; + struct nlattr *cda[NFTA_MAX_ATTR + 1]; struct nlattr *attr = (void *)nlh + min_len; int attrlen = nlh->nlmsg_len - min_len; + /* Sanity-check NFTA_MAX_ATTR */ + if (ss->cb[cb_id].attr_count > NFTA_MAX_ATTR) { + err = -ENOMEM; + goto ack; + } + err = nla_parse(cda, ss->cb[cb_id].attr_count, attr, attrlen, ss->cb[cb_id].policy, NULL); if (err < 0) -- 2.17.0 -- Kees Cook Pixel Security