Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3341388yba; Mon, 6 May 2019 22:50:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqzBvWzZA0YWXLQmdlJPmgdzk4FgobV8UNjFvaxAh5Rnw2CEg9b88Si4AX06KkpQRBg3B2ew X-Received: by 2002:a17:902:7b97:: with SMTP id w23mr38495697pll.335.1557208252374; Mon, 06 May 2019 22:50:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557208252; cv=none; d=google.com; s=arc-20160816; b=EHI1wKWoqSkT1XSzragpDEo/pWE+1SoBwfNvKfwbcUc5+nvihUuLLXdhmDpYpGNa1d 5J+NX/diENZI2RjulDKWKcY12CdM2ey2ePpCW7tkFZ2jFIBZQGuRNyG3ktCyJ3YG8om2 JHAoU8z55Z0ZC9S8sx4IG2OHaXmVh5piRM4k1iErIZc1zYB+BbjZTfBX0m8t/kyQswRF 4+5iss79x0gZcRjg/WjqXjgivtmzMToP1fGasLS/3AsMGNjbqEHXgVfhYslTjOOmsA5N O9TEwrANfl8LRacJZ4L/6ZNre/82YftlsCSRXq5HlfHLeOpNpKtFVEWd1CgWlSTACz5o X+LQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=gDuYppbSs57y9lgcDpLtbq/Rr0kz91GkQfz3bs3/rtY=; b=yZqc7ExCaj62XkTrMM1B4ufs7/cblz0QIdGJ3Zs98Xo/aN/u2CUBd7hXFXDVcz7I6W g6SjzzpjBl438rQXKJ+kyj5R1IcYdCPZC4XZbyy3UiFLeqiuowT935O+yPxp9rY3tFqg cBx1eujRXyTvKf6j9WI5HXJHF0l5ZHn3YYWExDHQceWSgihhTC8wWsq7+FQUU5nHtKtx gazInGieJOF1J0WLZZoCCCa5mJw24YepV2AtFDqMKUYZqFxUvMK+4BozvJy/yHJY+y9u KXm4D0k+DVqLn21hIj2+T6zlIf3uygSWPyAU4SerD8q4L241ofK9DkXZXWNfu5L0aCqo r5ow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=cvSF9SnC; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d34si20114703pld.58.2019.05.06.22.50.36; Mon, 06 May 2019 22:50:52 -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; dkim=pass header.i=@kernel.org header.s=default header.b=cvSF9SnC; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728664AbfEGFso (ORCPT + 99 others); Tue, 7 May 2019 01:48:44 -0400 Received: from mail.kernel.org ([198.145.29.99]:60166 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728404AbfEGFkm (ORCPT ); Tue, 7 May 2019 01:40:42 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id BA5D620578; Tue, 7 May 2019 05:40:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1557207641; bh=wVexugz/PHLlLDRwzGd8Fi0rLwjyVsyR155fbuB14hs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cvSF9SnCWyyvnTbwNzez4HTbzt9VAndcx/R8LbnJIRRqjT+6O6NqUMecSelKQt+i6 EjstU3vmyoRYGzudJPNxbpDFyXIfnYjtfDbNGEU6zGyjimTR6pULymYCTPquAU1Wpk oR/h7Yo83SQJpU4GTQMAaVysCNJcKb3KHvUt7zCg= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Anand Jain , David Sterba , Sasha Levin , linux-btrfs@vger.kernel.org Subject: [PATCH AUTOSEL 4.14 74/95] btrfs: harden agaist duplicate fsid on scanned devices Date: Tue, 7 May 2019 01:38:03 -0400 Message-Id: <20190507053826.31622-74-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190507053826.31622-1-sashal@kernel.org> References: <20190507053826.31622-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Anand Jain [ Upstream commit a9261d4125c97ce8624e9941b75dee1b43ad5df9 ] It's not that impossible to imagine that a device OR a btrfs image is copied just by using the dd or the cp command. Which in case both the copies of the btrfs will have the same fsid. If on the system with automount enabled, the copied FS gets scanned. We have a known bug in btrfs, that we let the device path be changed after the device has been mounted. So using this loop hole the new copied device would appears as if its mounted immediately after it's been copied. For example: Initially.. /dev/mmcblk0p4 is mounted as / $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT mmcblk0 179:0 0 29.2G 0 disk |-mmcblk0p4 179:4 0 4G 0 part / |-mmcblk0p2 179:2 0 500M 0 part /boot |-mmcblk0p3 179:3 0 256M 0 part [SWAP] `-mmcblk0p1 179:1 0 256M 0 part /boot/efi $ btrfs fi show Label: none uuid: 07892354-ddaa-4443-90ea-f76a06accaba Total devices 1 FS bytes used 1.40GiB devid 1 size 4.00GiB used 3.00GiB path /dev/mmcblk0p4 Copy mmcblk0 to sda $ dd if=/dev/mmcblk0 of=/dev/sda And immediately after the copy completes the change in the device superblock is notified which the automount scans using btrfs device scan and the new device sda becomes the mounted root device. $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 1 14.9G 0 disk |-sda4 8:4 1 4G 0 part / |-sda2 8:2 1 500M 0 part |-sda3 8:3 1 256M 0 part `-sda1 8:1 1 256M 0 part mmcblk0 179:0 0 29.2G 0 disk |-mmcblk0p4 179:4 0 4G 0 part |-mmcblk0p2 179:2 0 500M 0 part /boot |-mmcblk0p3 179:3 0 256M 0 part [SWAP] `-mmcblk0p1 179:1 0 256M 0 part /boot/efi $ btrfs fi show / Label: none uuid: 07892354-ddaa-4443-90ea-f76a06accaba Total devices 1 FS bytes used 1.40GiB devid 1 size 4.00GiB used 3.00GiB path /dev/sda4 The bug is quite nasty that you can't either unmount /dev/sda4 or /dev/mmcblk0p4. And the problem does not get solved until you take sda out of the system on to another system to change its fsid using the 'btrfstune -u' command. Signed-off-by: Anand Jain Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/volumes.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 38ed8e259e00..bd1117720fc1 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -696,6 +696,35 @@ static noinline int device_list_add(const char *path, return -EEXIST; } + /* + * We are going to replace the device path for a given devid, + * make sure it's the same device if the device is mounted + */ + if (device->bdev) { + struct block_device *path_bdev; + + path_bdev = lookup_bdev(path); + if (IS_ERR(path_bdev)) { + mutex_unlock(&fs_devices->device_list_mutex); + return ERR_CAST(path_bdev); + } + + if (device->bdev != path_bdev) { + bdput(path_bdev); + mutex_unlock(&fs_devices->device_list_mutex); + btrfs_warn_in_rcu(device->fs_info, + "duplicate device fsid:devid for %pU:%llu old:%s new:%s", + disk_super->fsid, devid, + rcu_str_deref(device->name), path); + return ERR_PTR(-EEXIST); + } + bdput(path_bdev); + btrfs_info_in_rcu(device->fs_info, + "device fsid %pU devid %llu moved old:%s new:%s", + disk_super->fsid, devid, + rcu_str_deref(device->name), path); + } + name = rcu_string_strdup(path, GFP_NOFS); if (!name) return -ENOMEM; -- 2.20.1