Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp805971imm; Mon, 21 May 2018 14:50:55 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqVLw/vV/mET40HM/JCJNMtEjrDoienAwGj8yixN1sfzEy6JYVOMnqmu5p81yRatmhjnjKK X-Received: by 2002:a17:902:28e8:: with SMTP id f95-v6mr22599896plb.250.1526939455827; Mon, 21 May 2018 14:50:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526939455; cv=none; d=google.com; s=arc-20160816; b=iZVvgSuBHMvwIxi7LNxrAh15o9EszfKt5HAj6FFb25ttKxV2dcO8opfpAGDG4097gA sOM0eRrkcF02IZjQRlLdak/yRAg9dxpuzzG+Tu92PfWM+HfPe/PD4khGz/9bFQwkX4Ny gFa3BJbMicxiuB0sw1I3eAhIGC/pnBc5LQCbMWC86jGNEVuHoeaFU3iCYjMMqsrcR8OR CeWzstA7Hj3CKcytnuunS7ycDRDVKRth7iqTwsYA10rZdtoY0eAKTL1vVMFK6S94yx1T +FRDCVLtXotaqKonZ4cEMJuxffgg+m7+oTsD9zJIrVVtnD90NrRiWvrmV4ngInGQHhJT NDsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=Hcfngx9tZnH1LObmjBzXhmBfL9MFVo3Tjmji5wEhG9c=; b=D4ANt5T4jiPUsokhD2qQ/oVQoFVkqF9QFZHeGZQbDQzZZo+R9Tazpf8ESjnkLjWt3m eHW+4663zWfIbxXqbbUpyp/JxAmK2BOhIhCesMn7LXDYnmcFfcHMHhFx1INBvYvmB2N1 7/SKBRAEOWzzgjrHU3WprYggcc9GpjemRGq6Bi1gMkZWA/47iMa1QhIxOBkxrPJMTtJH a9gcSVl8uL1LFE7izQRCXXTbr0vQbS6wDNjMV4E7KDXePBQ4TzzijmthuAukHSkrwHCk ziI3+HKAN11lrbGKnJqz9A/JAPv5qj9crf59qOmlLE4pRPhd0q+ZuyIKeLXPE4Kol9Yi HyeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=l+QxA2zU; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d4-v6si4917537pln.206.2018.05.21.14.50.41; Mon, 21 May 2018 14:50:55 -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=@kernel.org header.s=default header.b=l+QxA2zU; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932337AbeEUVtw (ORCPT + 99 others); Mon, 21 May 2018 17:49:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:38100 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751108AbeEUVXN (ORCPT ); Mon, 21 May 2018 17:23:13 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B20D320872; Mon, 21 May 2018 21:23:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1526937793; bh=hNVfSGBpeQWsieNVeHi7aPguOznU4P82Th89BiNLEbU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l+QxA2zU6+Rlm+nYb4nflypLkafuENIewRsEljkZt1ToKYbghiCAQrBSAfbztoNaa Bonm9z1hoWYC8LzdkBM7a/rU2YNVvWpd4WYP6seZwojuDVbaSgS01EjSHj06KJS0oV Id34IqqzvO9m1cMNzdtejJXL0Lyji9NcBBAN3Vdk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Florian Westphal , Pablo Neira Ayuso Subject: [PATCH 4.16 027/110] netfilter: nf_tables: cant fail after linking rule into active rule list Date: Mon, 21 May 2018 23:11:24 +0200 Message-Id: <20180521210506.224261442@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180521210503.823249477@linuxfoundation.org> References: <20180521210503.823249477@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Florian Westphal commit 569ccae68b38654f04b6842b034aa33857f605fe upstream. rules in nftables a free'd using kfree, but protected by rcu, i.e. we must wait for a grace period to elapse. Normal removal patch does this, but nf_tables_newrule() doesn't obey this rule during error handling. It calls nft_trans_rule_add() *after* linking rule, and, if that fails to allocate memory, it unlinks the rule and then kfree() it -- this is unsafe. Switch order -- first add rule to transaction list, THEN link it to public list. Note: nft_trans_rule_add() uses GFP_KERNEL; it will not fail so this is not a problem in practice (spotted only during code review). Fixes: 0628b123c96d12 ("netfilter: nfnetlink: add batch support and use it from nf_tables") Signed-off-by: Florian Westphal Signed-off-by: Pablo Neira Ayuso Signed-off-by: Greg Kroah-Hartman --- net/netfilter/nf_tables_api.c | 59 ++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 27 deletions(-) --- a/net/netfilter/nf_tables_api.c +++ b/net/netfilter/nf_tables_api.c @@ -2357,41 +2357,46 @@ static int nf_tables_newrule(struct net } if (nlh->nlmsg_flags & NLM_F_REPLACE) { - if (nft_is_active_next(net, old_rule)) { - trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE, - old_rule); - if (trans == NULL) { - err = -ENOMEM; - goto err2; - } - nft_deactivate_next(net, old_rule); - chain->use--; - list_add_tail_rcu(&rule->list, &old_rule->list); - } else { + if (!nft_is_active_next(net, old_rule)) { err = -ENOENT; goto err2; } - } else if (nlh->nlmsg_flags & NLM_F_APPEND) - if (old_rule) - list_add_rcu(&rule->list, &old_rule->list); - else - list_add_tail_rcu(&rule->list, &chain->rules); - else { - if (old_rule) - list_add_tail_rcu(&rule->list, &old_rule->list); - else - list_add_rcu(&rule->list, &chain->rules); - } + trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE, + old_rule); + if (trans == NULL) { + err = -ENOMEM; + goto err2; + } + nft_deactivate_next(net, old_rule); + chain->use--; + + if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) { + err = -ENOMEM; + goto err2; + } - if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) { - err = -ENOMEM; - goto err3; + list_add_tail_rcu(&rule->list, &old_rule->list); + } else { + if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) { + err = -ENOMEM; + goto err2; + } + + if (nlh->nlmsg_flags & NLM_F_APPEND) { + if (old_rule) + list_add_rcu(&rule->list, &old_rule->list); + else + list_add_tail_rcu(&rule->list, &chain->rules); + } else { + if (old_rule) + list_add_tail_rcu(&rule->list, &old_rule->list); + else + list_add_rcu(&rule->list, &chain->rules); + } } chain->use++; return 0; -err3: - list_del_rcu(&rule->list); err2: nf_tables_rule_destroy(&ctx, rule); err1: