Received: by 10.213.65.68 with SMTP id h4csp1717702imn; Mon, 19 Mar 2018 11:19:15 -0700 (PDT) X-Google-Smtp-Source: AG47ELuxuFhzFrQphlBxSkVMaOsyIs/XKkDYjBywWZg206KmW7O0RLZyUrcVcMhwSV6a7q5+YZQw X-Received: by 10.99.163.9 with SMTP id s9mr3332756pge.179.1521483555625; Mon, 19 Mar 2018 11:19:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521483555; cv=none; d=google.com; s=arc-20160816; b=OybO4sN75nXR5uKknkMofSrXtuSZaAm9TtUOjUBvZMydk/W3A1EaOluVLn+xDMfDbJ EMf+JMef+btZMM708usCVzRsCLHQzaqiBy16iYQGWBIxNLqbkxGEcP2Fe4e5cNKa8ldc oV7iLtXktIaoGcNHUJtdmjidDkjogboJZrLjka7o79TewPn3m1NIhPLWsSyR1QWO2fXj 8FzyB8vu+Nm5mZ+hhPcQ5lGF3vcC0rKGe/+SOwZNZTQGfmLvBwxbWFLHriySHYNDGrNQ PtkyXHJo4uTOqyvhH0/9T6hSqnHZxLWIOXfQKw442gGAFRU4mMsXRXDojAJghK3vmeJ2 pmvA== 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=SJhIuVqjczZ37dhGkKVESOehXEQxSHs8jxFmRgvL9Nk=; b=GHYWMiHaGDmnGLWtN9YvlXsoRldlxjZsxXnTIz5QPxyVDTb4aXZp6HW1wcqBVVoZLC mnhXEH9Zi+GbXa1LjO4OI4R2oKLOY1qaoZjER7xUGby+6YTu9OzExtlFgagfiEFaf3Xh JuY1lnrZT+9qV2n+0rfSVkSnxgUuzKSRbQ0xrjrH2cg0xsiJ4IYtlyd7QtAH9Li8PsjO F5RAsQKiU8BBhNGM898VbPSNlDYNZmCvjz0nSZC027p6eTtDoeazvJH+TN4Mk6xUn0PI Q/IN6gvalK2ma6nDZfzQo3uW22NyMKpWttp5hL0AeWEw+/R35o+o1gZsdbAULH0LjGwb 6jcA== 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 i35-v6si409447plg.48.2018.03.19.11.18.58; Mon, 19 Mar 2018 11:19: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; 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 S935360AbeCSSRm (ORCPT + 99 others); Mon, 19 Mar 2018 14:17:42 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:45620 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S969171AbeCSSRi (ORCPT ); Mon, 19 Mar 2018 14:17:38 -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 C68D81244; Mon, 19 Mar 2018 18:17:37 +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.4 131/134] btrfs: Fix use-after-free when cleaning up fs_devs with a single stale device Date: Mon, 19 Mar 2018 19:06:54 +0100 Message-Id: <20180319171908.164096609@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180319171849.024066323@linuxfoundation.org> References: <20180319171849.024066323@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.4-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 @@ -568,6 +568,7 @@ void btrfs_free_stale_device(struct btrf 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);