Received: by 10.213.65.68 with SMTP id h4csp37026imn; Mon, 19 Mar 2018 18:45:01 -0700 (PDT) X-Google-Smtp-Source: AG47ELurIsceLjbzdiyZWa/IKdx6j//zBt+MC3XTw/khnZldSd+/03Jk1O45RSXB1yuSknWSdGhT X-Received: by 2002:a17:902:6e01:: with SMTP id u1-v6mr4748899plk.96.1521510301876; Mon, 19 Mar 2018 18:45:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521510301; cv=none; d=google.com; s=arc-20160816; b=DacQ2XDkbfl6Ypk6XfZYrK/wKv8uOR0+Agov8tWWvc/Lf8GQb77MHx34UZcfc6vnZm xDM6QhQHqiDPqruelh/ytE4Y5o5gq/7GAi1uHhvi8Y0bAknHzMuK/BLv7NhC8Oq1jBY+ 5yjMRPRdVdHe1dUZcy3TmiRwWfSp4iRebEkpkjJoESSHu9nVSrH6UmuwEspc57pMv7w2 Id6HhfDEzsBl2Tgq5IEFNfaw0KUS4kiIhTUNubDfkekCp4NdrmJZUBdqvXvBXAnRwjQf KmpRZQUlEnaK3uYel6IXSSPbxhc3Dh2zODO6O7EHc/Edg7cdFX0UAOom8sz3Sgjr1HFI XicQ== 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=j77+pOrSbWWWonSEXU06S2QQmjsAnkUuFQ/eidNjZEI=; b=ztyGquIRDIQ1pDssDknTbYfm3y7dTYqHiAzj68u9eaChKvGwzVfozGkNAfISPkSYvo dNKzwyK81UKHmRh40IOId8TEjsxGhSc7J88OMIMWhBSYihNUkF5yKu/pFaqQ4r9LsZVf Gz9cZLyNuYKoP64/PnhWDxVvpv5mpHcCE7LQ6f478eTtKq9e/z+tSoorPexspNBDf+Fd Rlp5DDjInsHfsu4nWDSqsJBBaR8IY42ay4dm84xTy8WXtrx2PzRb2kGdu4/2efLBiW8t bymc9bUnr/pO9ZHUru2kFNeP5NDRLRGm5vZsCUEsL0V25Cd8/E1cQdzq3YF6PLOGmRkW n/Fg== 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 m11-v6si502563pls.520.2018.03.19.18.44.48; Mon, 19 Mar 2018 18:45:01 -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; 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 S935716AbeCSSoT (ORCPT + 99 others); Mon, 19 Mar 2018 14:44:19 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:53912 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S970264AbeCSScT (ORCPT ); Mon, 19 Mar 2018 14:32:19 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id CA62112A8; Mon, 19 Mar 2018 18:32:18 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nikolay Borisov , Anand Jain , David Sterba Subject: [PATCH 4.15 35/52] btrfs: Fix use-after-free when cleaning up fs_devs with a single stale device Date: Mon, 19 Mar 2018 19:08:33 +0100 Message-Id: <20180319180736.971722766@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180319180734.976730813@linuxfoundation.org> References: <20180319180734.976730813@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.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Nikolay Borisov commit fd649f10c3d21ee9d7542c609f29978bdf73ab94 upstream. Commit 4fde46f0cc71 ("Btrfs: free the stale device") introduced btrfs_free_stale_device which iterates the device lists for all registered btrfs filesystems and deletes those devices which aren't mounted. In a btrfs_devices structure has only 1 device attached to it and it is unused then btrfs_free_stale_devices will proceed to also free the btrfs_fs_devices struct itself. Currently this leads to a use after free since list_for_each_entry will try to perform a check on the already freed memory to see if it has to terminate the loop. The fix is to use 'break' when we know we are freeing the current fs_devs. Fixes: 4fde46f0cc71 ("Btrfs: free the stale device") Signed-off-by: Nikolay Borisov Reviewed-by: Anand Jain Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/volumes.c | 1 + 1 file changed, 1 insertion(+) --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -574,6 +574,7 @@ static void btrfs_free_stale_device(stru btrfs_sysfs_remove_fsid(fs_devs); list_del(&fs_devs->list); free_fs_devices(fs_devs); + break; } else { fs_devs->num_devices--; list_del(&dev->dev_list);