Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2027240imu; Fri, 14 Dec 2018 04:44:14 -0800 (PST) X-Google-Smtp-Source: AFSGD/VVmFCM1RwLshDOQjpYZR9qha7rHvE/SR5WA3KA13HgrzURpVD/avYJmeAyidYADwflYNtC X-Received: by 2002:a62:8dd9:: with SMTP id p86mr2666884pfk.143.1544791454766; Fri, 14 Dec 2018 04:44:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544791454; cv=none; d=google.com; s=arc-20160816; b=KHStwgsqG922B8gB5pAeLbAP8okKcPnlOiSAH+dGUPbpREcbIKDWyNQ4pJJhMzwXzJ bKo6nOyqjJjEdg/rnMPRS4UjuCYpprHhonO6ToECcLhiZevmHFyE+NOZUFUzIWi4PdoD 6oHG6YuL2LYD9r5JmL6mwKqbAXUgD7Ldg4k1678McLwcD9Gy1mJnjJphFxgfLbfbI8yq aZg2jvQwNP6wO6L+cYZnMqMkAI7aJymCkg1CQY3gEWlDGj4anDSPS6STb1PIO77w+0jK EpHYxc6rV7+LDGfNJzdlKZA+jyyJpSaltVhllMflP/Qh400RQsaWRrK92jrsvIEk0M90 FZtw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=lzevnZfCHuLTsbRNUVo8afCq5lP5qeXtFpw0yzxlogY=; b=uiHtZO54bF2STvWTNUzhesw56iyoRV/v5IRQ86U0tMCgSYlql2r6asAIPvF/1ZjJAm NzbDhI6eIkw3bKHg0JDf+Q1qkv2sEDtcSpiTGf7gFBCyNx8v8YvTUhHpH6q0udWPrJas 9t4VqlgIOxwvnCZ8hrU6sIvQT//ufbISlYVISxgV3JrfUSsLneNZRk85RJmrfoive04a PUfLUH8u0sAqhO9TXg7AoedPBhKUjdZy+48Xr65+sBdpxksaxISO+xeqWYkFSUTquEj6 nzxepF3RP61R11fn15KjZ/aj+mAvDTsp8w5ofAci292dzAp/HHvFiHVsZmI4A3ckP+D1 i2dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vFIHkDiD; 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 i9si4066310plb.35.2018.12.14.04.44.00; Fri, 14 Dec 2018 04:44:14 -0800 (PST) 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=vFIHkDiD; 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 S1730638AbeLNMmA (ORCPT + 99 others); Fri, 14 Dec 2018 07:42:00 -0500 Received: from mail.kernel.org ([198.145.29.99]:46446 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730239AbeLNMDJ (ORCPT ); Fri, 14 Dec 2018 07:03:09 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (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 7B90F2145D; Fri, 14 Dec 2018 12:03:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544788989; bh=Cc/SZgIIKXBXVQD3FWSL8yOSX7pggb+RWRzv5bxOKUA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vFIHkDiDwaOdigj3gnK3PiPAtfmvsAsBfyzTPnn5AkQ+Tu4LZS2rTGW+ahEQtl911 JwxdLUkaGQUuGjyoVM36MkVpnCMDGLvCFinSrNFbETRcWRgHBQoOLvGJ8Xx+XsbN4p 8LmOj2CZrhK/7W58yBcpO9igivt/+N4ClfnJAEZg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Pablo Neira Ayuso , Florian Westphal , Sasha Levin Subject: [PATCH 4.19 043/142] netfilter: nf_tables: fix use-after-free when deleting compat expressions Date: Fri, 14 Dec 2018 12:58:48 +0100 Message-Id: <20181214115748.771491078@linuxfoundation.org> X-Mailer: git-send-email 2.20.0 In-Reply-To: <20181214115747.053633987@linuxfoundation.org> References: <20181214115747.053633987@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 29e3880109e357fdc607b4393f8308cef6af9413 ] nft_compat ops do not have static storage duration, unlike all other expressions. When nf_tables_expr_destroy() returns, expr->ops might have been free'd already, so we need to store next address before calling expression destructor. For same reason, we can't deref match pointer after nft_xt_put(). This can be easily reproduced by adding msleep() before nft_match_destroy() returns. Fixes: 0ca743a55991 ("netfilter: nf_tables: add compatibility layer for x_tables") Reported-by: Pablo Neira Ayuso Signed-off-by: Florian Westphal Signed-off-by: Pablo Neira Ayuso Signed-off-by: Sasha Levin --- net/netfilter/nf_tables_api.c | 5 +++-- net/netfilter/nft_compat.c | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c index 4c016b49fe2b..06ed55cef962 100644 --- a/net/netfilter/nf_tables_api.c +++ b/net/netfilter/nf_tables_api.c @@ -2432,7 +2432,7 @@ static int nf_tables_getrule(struct net *net, struct sock *nlsk, static void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule) { - struct nft_expr *expr; + struct nft_expr *expr, *next; lockdep_assert_held(&ctx->net->nft.commit_mutex); /* @@ -2441,8 +2441,9 @@ static void nf_tables_rule_destroy(const struct nft_ctx *ctx, */ expr = nft_expr_first(rule); while (expr != nft_expr_last(rule) && expr->ops) { + next = nft_expr_next(expr); nf_tables_expr_destroy(ctx, expr); - expr = nft_expr_next(expr); + expr = next; } kfree(rule); } diff --git a/net/netfilter/nft_compat.c b/net/netfilter/nft_compat.c index ad2fe6a7e47d..29d6fc73caf9 100644 --- a/net/netfilter/nft_compat.c +++ b/net/netfilter/nft_compat.c @@ -501,6 +501,7 @@ __nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr, void *info) { struct xt_match *match = expr->ops->data; + struct module *me = match->me; struct xt_mtdtor_param par; par.net = ctx->net; @@ -511,7 +512,7 @@ __nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr, par.match->destroy(&par); if (nft_xt_put(container_of(expr->ops, struct nft_xt, ops))) - module_put(match->me); + module_put(me); } static void -- 2.19.1