Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp741095imm; Thu, 13 Sep 2018 07:06:06 -0700 (PDT) X-Google-Smtp-Source: ANB0VdY7b9tODkbh40AR1mk/VOLP2r/e7QNdmhr8oEaciUMbEfY93yKZ0bpsHoVnCypjv026OX3b X-Received: by 2002:a62:174a:: with SMTP id 71-v6mr7754446pfx.217.1536847566310; Thu, 13 Sep 2018 07:06:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536847566; cv=none; d=google.com; s=arc-20160816; b=xbNuSeR+lDFci81oqBsOIYuW/HFMjbMX1X6uKeXSpps/TqXNpE5XEfkxd4crYlY+YN ierkF/LxYGb0jBtbgKtuZgRxEMampk3l58aKP6PvscSjs5TtTj0ovYtiqbqY/u3V3vOB md4z5kkjLeUqEmZBMi3H2AR4F0aBJrNK+TmFClZk5pc4TKneslKchkDoMnNolQiLuoGi nKRA2CLDbXY6/WEgAO7DLstQVjKL6xnoihrMxTY1ajxsZSVrqQUxF2eH+j5xWfLAu9Fs 4w5quZ2FvCtN9C8bsV1XFYtJsFbBfdu0dzNfaI4epNE/JClYaeF9BoRJ9CfLfQ4+93Vu rwuw== 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; bh=Y8oX4kwjvWwQvCDDUdTVhZHP3w8jxxwRhzOZ7z/Dkhs=; b=K3HLGXhaFXZqSDg1y7qVhd0pNyJIeliO6aB9fevMKMGidKW9hm/zdYQi59iNkj+ujS pu5VKDt1P3d2jwEVmuqMn9kY+cPIEYHAk1hsIoRdGoBG6J77U2UPoKGhi/FlHqcO4V+/ AAaa9wMkAlXl4k5N9SE1XmRlfrSF6d04JvfOaCcGOhNJCquKgHXJCzPmHspCivTybmho GsX7qp9LVSY9frLWSXfxb/Jl/aCDvPS8bWr3IlDyB7ap3Fyk3dXK8WY1xdDXPjguAekb LtOPuOrRZZZMYy5hzVE+zElhNjJZCnH5CmqN9h5YmnKJME8Y+imeh7gVp9+rLq/bMl6f h0eQ== 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 k20-v6si4425886pgb.115.2018.09.13.07.05.47; Thu, 13 Sep 2018 07:06:06 -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 S1731942AbeIMTPO (ORCPT + 99 others); Thu, 13 Sep 2018 15:15:14 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:35502 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731329AbeIMTPN (ORCPT ); Thu, 13 Sep 2018 15:15:13 -0400 Received: from localhost (ip-213-127-77-73.ip.prioritytelecom.net [213.127.77.73]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id CF96FD39; Thu, 13 Sep 2018 14:05:33 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Anand Jain , David Sterba , Sasha Levin Subject: [PATCH 4.18 151/197] btrfs: fix mount and ioctl device scan ioctl race Date: Thu, 13 Sep 2018 15:31:40 +0200 Message-Id: <20180913131847.596642460@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180913131841.568116777@linuxfoundation.org> References: <20180913131841.568116777@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review 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.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: David Sterba [ Upstream commit 81ffd56b5745355b70d54ca4e1bdd0d64a66ff9f ] Technically this extends the critical section covered by uuid_mutex to: - parse early mount options -- here we can call device scan on paths that can be passed as 'device=/dev/...' - scan the device passed to mount - open the devices related to the fs_devices -- this increases fs_devices::opened The race can happen when mount calls one of the scans and there's another one called eg. by mkfs or 'btrfs dev scan': Mount Scan ----- ---- scan_one_device (dev1, fsid1) scan_one_device (dev2, fsid1) add the device free stale devices fsid1 fs_devices::opened == 0 find fsid1:dev1 free fsid1:dev1 if it's the last one, free fs_devices of fsid1 too open_devices (dev1, fsid1) dev1 not found When fixed, the uuid mutex will make sure that mount will increase fs_devices::opened and this will not be touched by the racing scan ioctl. Reported-and-tested-by: syzbot+909a5177749d7990ffa4@syzkaller.appspotmail.com Reported-and-tested-by: syzbot+ceb2606025ec1cc3479c@syzkaller.appspotmail.com Reviewed-by: Anand Jain Signed-off-by: David Sterba Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/super.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c @@ -1557,19 +1557,19 @@ static struct dentry *btrfs_mount_root(s mutex_lock(&uuid_mutex); error = btrfs_parse_early_options(data, mode, fs_type, &fs_devices); - mutex_unlock(&uuid_mutex); - if (error) + if (error) { + mutex_unlock(&uuid_mutex); goto error_fs_info; + } - mutex_lock(&uuid_mutex); error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); - mutex_unlock(&uuid_mutex); - if (error) + if (error) { + mutex_unlock(&uuid_mutex); goto error_fs_info; + } fs_info->fs_devices = fs_devices; - mutex_lock(&uuid_mutex); error = btrfs_open_devices(fs_devices, mode, fs_type); mutex_unlock(&uuid_mutex); if (error)