[PATCH] btrfs-progs: prevent silent damage when add dev to an invalid mntpnt

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Problem:
	# mkfs.btrfs -f /dev/sda1
	# btrfs dev add /dev/sda1 /dir -f   <== dir is not a mntpnt

btrfs dev add just report invalid ioctl but it has already made
changes to /dev/sda1 with @btrfs_prepare_device(), so the fs on
/dev/sda1 is damaged.

We could check whether /dev/sda1 is a valid mntpnt by calling
@find_mount_root() to prevent this silent damage.

Signed-off-by: Gui Hecheng <guihc.fnst@xxxxxxxxxxxxxx>
---
 cmds-device.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/cmds-device.c b/cmds-device.c
index a728f21..65815c3 100644
--- a/cmds-device.c
+++ b/cmds-device.c
@@ -53,6 +53,7 @@ static int cmd_add_dev(int argc, char **argv)
 	int discard = 1;
 	int force = 0;
 	char estr[100];
+	char rmntpnt[PATH_MAX];
 
 	while (1) {
 		int long_index;
@@ -84,6 +85,22 @@ static int cmd_add_dev(int argc, char **argv)
 
 	mntpnt = argv[optind + argc - 1];
 
+	if (!realpath(mntpnt, rmntpnt)) {
+		fprintf(stderr, "ERROR: %s\n", strerror(errno));
+		return 1;
+	}
+
+	ret = find_mount_root(rmntpnt, &mntpnt);
+	if (ret < 0) {
+		fprintf(stderr, "ERROR: find_mount_root failed on '%s': %s\n",
+				rmntpnt, strerror(-ret));
+		return 1;
+	} else if (ret > 0) {
+		fprintf(stderr, "ERROR: '%s' doesn't belong to btrfs mount point\n",
+			rmntpnt);
+		return 1;
+	}
+
 	fdmnt = open_file_or_dir(mntpnt, &dirstream);
 	if (fdmnt < 0) {
 		fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
-- 
1.8.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Filesystem Development]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux