Received: by 10.223.176.5 with SMTP id f5csp696890wra; Fri, 9 Feb 2018 05:52:21 -0800 (PST) X-Google-Smtp-Source: AH8x226YZOXhnUj41uvf7YiRYy9dbI40Elj99OD3/Mf+DB3U8Z6CVm4TU4RfQTELnx4noJdts2gq X-Received: by 10.99.55.1 with SMTP id e1mr2435890pga.237.1518184341692; Fri, 09 Feb 2018 05:52:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518184341; cv=none; d=google.com; s=arc-20160816; b=dPWch0W81y7rLeMneFe6cBxH4gGSn6WKi2ENokpNO8JY067yHsoXFv2wdfCvxkGNzZ XUX2a5L/bWTd9C9Qr6tANFZepYI1+U2nd7I2G+PAeQ9LcIpJkQKlEYQ6VB7l9kDE36Ml +tOA5Y8E4/JH7Cz+Z+1oaomxv1oL+uXpMZw2Skxl3mv/oJxPcMfGdXHd3nGkCY7nPdgY EjE4JvWG4dw0Sgg5EFuoImyw2jDVeibMaliOE9YgJVxkME15rFzhDi2ZMZMe1ypfIJSh lGUtK5NImUlcutJbeyCRXK8nJmQSCsvAfQGvPgCnM9Nl5hNS7JQcByS5PdY6v+Y4ogvt xK0w== 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 :arc-authentication-results; bh=x8o5i7OrcIrOQoiygLrvTFnlQKj0qZ7qHfFPJvqhmJI=; b=VRuowZ5vbFVdWCe/eol01DIcEFCgsc7T5/u81lPoXIlW+vUNVAiV4LJCRvopXigrP/ rXe8TGwk//if5XiIdu4QnOb8ISCmOZA/D7xxTK3+M7WPkp08mFaTtETH8zce0eKthtHT ixGzlQeAvJ7UpuS/URlJohAv3kAaAXcQcYJfi4BPfib0FDw04TCqajwuzhSOAzdCUu0D +PLGfRrVFmjOBsFRRdAK4a+W6q0lgsv49gGgptJ6LgaYa7TXIr92OwBOQU/XwwQpGt3X +3hhsjVE5FIpc3JwKUjxE4gxqaUa6SSs5i/oL2zlFdoZUJMEYTyrI37aWzx+djNhkYMj z51A== ARC-Authentication-Results: i=1; mx.google.com; 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 e3si1413015pgt.217.2018.02.09.05.52.07; Fri, 09 Feb 2018 05:52:21 -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; 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 S932201AbeBINqi (ORCPT + 99 others); Fri, 9 Feb 2018 08:46:38 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:53212 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932190AbeBINqf (ORCPT ); Fri, 9 Feb 2018 08:46:35 -0500 Received: from localhost (LFbn-1-12258-90.w90-92.abo.wanadoo.fr [90.92.71.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 0C2FBDA4; Fri, 9 Feb 2018 13:46:34 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jiri Pirko , "David S. Miller" Subject: [PATCH 4.15 18/23] net: sched: fix use-after-free in tcf_block_put_ext Date: Fri, 9 Feb 2018 14:40:16 +0100 Message-Id: <20180209133939.222647136@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180209133938.366024920@linuxfoundation.org> References: <20180209133938.366024920@linuxfoundation.org> User-Agent: quilt/0.65 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.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jiri Pirko [ Upstream commit df45bf84e4f5a48f23d4b1a07d21d566e8b587b2 ] Since the block is freed with last chain being put, once we reach the end of iteration of list_for_each_entry_safe, the block may be already freed. I'm hitting this only by creating and deleting clsact: [ 202.171952] ================================================================== [ 202.180182] BUG: KASAN: use-after-free in tcf_block_put_ext+0x240/0x390 [ 202.187590] Read of size 8 at addr ffff880225539a80 by task tc/796 [ 202.194508] [ 202.196185] CPU: 0 PID: 796 Comm: tc Not tainted 4.15.0-rc2jiri+ #5 [ 202.203200] Hardware name: Mellanox Technologies Ltd. "MSN2100-CB2F"/"SA001017", BIOS 5.6.5 06/07/2016 [ 202.213613] Call Trace: [ 202.216369] dump_stack+0xda/0x169 [ 202.220192] ? dma_virt_map_sg+0x147/0x147 [ 202.224790] ? show_regs_print_info+0x54/0x54 [ 202.229691] ? tcf_chain_destroy+0x1dc/0x250 [ 202.234494] print_address_description+0x83/0x3d0 [ 202.239781] ? tcf_block_put_ext+0x240/0x390 [ 202.244575] kasan_report+0x1ba/0x460 [ 202.248707] ? tcf_block_put_ext+0x240/0x390 [ 202.253518] tcf_block_put_ext+0x240/0x390 [ 202.258117] ? tcf_chain_flush+0x290/0x290 [ 202.262708] ? qdisc_hash_del+0x82/0x1a0 [ 202.267111] ? qdisc_hash_add+0x50/0x50 [ 202.271411] ? __lock_is_held+0x5f/0x1a0 [ 202.275843] clsact_destroy+0x3d/0x80 [sch_ingress] [ 202.281323] qdisc_destroy+0xcb/0x240 [ 202.285445] qdisc_graft+0x216/0x7b0 [ 202.289497] tc_get_qdisc+0x260/0x560 Fix this by holding the block also by chain 0 and put chain 0 explicitly, out of the list_for_each_entry_safe loop at the very end of tcf_block_put_ext. Fixes: efbf78973978 ("net_sched: get rid of rcu_barrier() in tcf_block_put_ext()") Signed-off-by: Jiri Pirko Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/sched/cls_api.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) --- a/net/sched/cls_api.c +++ b/net/sched/cls_api.c @@ -343,23 +343,24 @@ void tcf_block_put_ext(struct tcf_block if (!block) return; - /* Hold a refcnt for all chains, except 0, so that they don't disappear + /* Hold a refcnt for all chains, so that they don't disappear * while we are iterating. */ list_for_each_entry(chain, &block->chain_list, list) - if (chain->index) - tcf_chain_hold(chain); + tcf_chain_hold(chain); list_for_each_entry(chain, &block->chain_list, list) tcf_chain_flush(chain); tcf_block_offload_unbind(block, q, ei); - /* At this point, all the chains should have refcnt >= 1. Block will be - * freed after all chains are gone. - */ + /* At this point, all the chains should have refcnt >= 1. */ list_for_each_entry_safe(chain, tmp, &block->chain_list, list) tcf_chain_put(chain); + + /* Finally, put chain 0 and allow block to be freed. */ + chain = list_first_entry(&block->chain_list, struct tcf_chain, list); + tcf_chain_put(chain); } EXPORT_SYMBOL(tcf_block_put_ext);