From patchwork Mon Jan 14 13:30:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 10762537 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 14837746 for ; Mon, 14 Jan 2019 13:30:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 041E528BAB for ; Mon, 14 Jan 2019 13:30:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ECEEA28BAE; Mon, 14 Jan 2019 13:30:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9614328BAB for ; Mon, 14 Jan 2019 13:30:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726664AbfANNaj (ORCPT ); Mon, 14 Jan 2019 08:30:39 -0500 Received: from mail.kernel.org ([198.145.29.99]:58356 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726538AbfANNaj (ORCPT ); Mon, 14 Jan 2019 08:30:39 -0500 Received: from localhost.localdomain (bl8-197-74.dsl.telepac.pt [85.241.197.74]) (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 7958120651; Mon, 14 Jan 2019 13:30:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1547472638; bh=4gB7XwOMACJCe52+XR2rZsNt8DKp1imswRYIghkN48I=; h=From:To:Cc:Subject:Date:From; b=nlSRV2kaf7P9YXkd57cX5MxQYmZeEA+7jDZs8S3wiVfzTOPRrsAEbUNoTg81IIB9T sJB7B7VUHpsAHUAbcyRW4GaBWBSxc0J66uYkkp92L3zIgLExkTfkdYtfDsbo8FtKvx IXilWsvcGcBkHlF9vZ5Md3H7O4YXsLH7/jovQQRA= From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Cc: ddis@suse.com, Filipe Manana Subject: [PATCH 1/3] Btrfs-progs: fix mount point detection due to partial prefix match Date: Mon, 14 Jan 2019 13:30:24 +0000 Message-Id: <20190114133024.18320-1-fdmanana@kernel.org> X-Mailer: git-send-email 2.11.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Filipe Manana When attempting to find the mount point of a path we can end up returning an incorrect mount point. This happens because we consider a mount point valid for the given path even if it matches only partially the patch. Consider the following example, which makes btrfs receive fail: $ truncate -s 1G disk1 $ truncate -s 1G disk2 $ losetup /dev/loop1 disk1 $ losetup /dev/loop2 disk2 $ mkfs.btrfs -f /dev/loop1 $ mkfs.btrfs -f /dev/loop2 $ mount /dev/loop1 /mnt $ mkdir /mnt/ddis $ mkdir /mnt/ddis-not-a-mount $ mount /dev/loop2 /mnt/ddis $ echo "some data" > /mnt/ddis/file $ btrfs subvolume snapshot -r /mnt/ddis /mnt/ddis/snap $ btrfs send -f /tmp/send.data /mnt/ddis/snap $ btrfs receive -f /tmp/send.data /mnt/ddis-not-a-mount At subvol snap ERROR: chown failed: No such file or directory In that example btrfs receive passes the path "/mnt/ddis-not-a-mount" to find_mount_root() which picks "/mnt/ddis" as the mount point instead of "/mnt". The wrong decision happens because "/mnt/ddis" is the longest string found that is a prefix of "/mnt/ddis-not-a-mount", however it shouldn't be considered valid because what follows the substring "ddis" in the given path is not a path separator ("/") nor the null character ('\0'). So fix find_mount_root() to check for the presence of a path separator or a null byte character after if finds a mount point string that matches the given path. A test case will follow soon in a separate patch. Reported-by: David Disseldorp Signed-off-by: Filipe Manana Reviewed-by: David Disseldorp --- utils.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/utils.c b/utils.c index 3a4bc92a..6616630b 100644 --- a/utils.c +++ b/utils.c @@ -2064,7 +2064,8 @@ int find_mount_root(const char *path, char **mount_root) while ((ent = getmntent(mnttab))) { len = strlen(ent->mnt_dir); - if (strncmp(ent->mnt_dir, path, len) == 0) { + if (strncmp(ent->mnt_dir, path, len) == 0 && + (path[len] == '/' || path[len] == '\0')) { /* match found and use the latest match */ if (longest_matchlen <= len) { free(longest_match); From patchwork Mon Jan 14 13:31:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 10762539 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 676596C2 for ; Mon, 14 Jan 2019 13:31:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5856428BB6 for ; Mon, 14 Jan 2019 13:31:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 49B7928BBE; Mon, 14 Jan 2019 13:31:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C26F228BA8 for ; Mon, 14 Jan 2019 13:31:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726579AbfANNbj (ORCPT ); Mon, 14 Jan 2019 08:31:39 -0500 Received: from mail.kernel.org ([198.145.29.99]:58838 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726467AbfANNbj (ORCPT ); Mon, 14 Jan 2019 08:31:39 -0500 Received: from localhost.localdomain (bl8-197-74.dsl.telepac.pt [85.241.197.74]) (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 2EEBE20651; Mon, 14 Jan 2019 13:31:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1547472699; bh=1IgkYnH4NoPBt1CFSxWMIPgb+d8cbnUnbUnanGk77hY=; h=From:To:Cc:Subject:Date:From; b=KaZqizlyegcdvuXuytcjStthytD6QPNFDp2RFSj7y73YhrzgAxn+HSei2ccpznzwI iG73fxxnWcp273v7lt0Bq37p62CE+rVHH40rco0QoiIOJvpa6zszlPQDy9AlQZBLe4 30sEtYRgY4sy4o96z43AGh+0ANnWlVVcsKCG1wGY= From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Cc: ddis@suse.com, Filipe Manana Subject: [PATCH 2/3] Btrfs-progs: add missing error handling in find_mount_root() Date: Mon, 14 Jan 2019 13:31:34 +0000 Message-Id: <20190114133134.18374-1-fdmanana@kernel.org> X-Mailer: git-send-email 2.11.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Filipe Manana The strdup() function can fail, in which case it returns NULL and sets errno [1]. Therefore add the missing error check after calling strdup() at find_mount_root(). [1] - http://man7.org/linux/man-pages/man3/strdup.3p.html Signed-off-by: Filipe Manana Reviewed-by: David Disseldorp --- utils.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/utils.c b/utils.c index 6616630b..3bc6bc3b 100644 --- a/utils.c +++ b/utils.c @@ -2048,7 +2048,7 @@ int find_mount_root(const char *path, char **mount_root) int fd; struct mntent *ent; int len; - int ret; + int ret = 0; int not_btrfs = 1; int longest_matchlen = 0; char *longest_match = NULL; @@ -2071,12 +2071,18 @@ int find_mount_root(const char *path, char **mount_root) free(longest_match); longest_matchlen = len; longest_match = strdup(ent->mnt_dir); + if (!longest_match) { + ret = -errno; + break; + } not_btrfs = strcmp(ent->mnt_type, "btrfs"); } } } endmntent(mnttab); + if (ret) + return ret; if (!longest_match) return -ENOENT; if (not_btrfs) { From patchwork Mon Jan 14 13:31:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 10762541 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 251106C2 for ; Mon, 14 Jan 2019 13:31:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1632328BAF for ; Mon, 14 Jan 2019 13:31:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0AB0828B6D; Mon, 14 Jan 2019 13:31:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 86E6628BBB for ; Mon, 14 Jan 2019 13:31:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726581AbfANNbw (ORCPT ); Mon, 14 Jan 2019 08:31:52 -0500 Received: from mail.kernel.org ([198.145.29.99]:58942 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726467AbfANNbw (ORCPT ); Mon, 14 Jan 2019 08:31:52 -0500 Received: from localhost.localdomain (bl8-197-74.dsl.telepac.pt [85.241.197.74]) (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 C290B20651; Mon, 14 Jan 2019 13:31:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1547472711; bh=uArKxEItyt6m/gASFUd4HzKQiCvoLNBcKUqG8kCFK/Q=; h=From:To:Cc:Subject:Date:From; b=1NVKVt20wZCNRy7VIGRFLlExD5nwNKwFGU8SOVq5I8k8fV7KT8kM4vjA09oSOqaFv VWuiJM5g7tUn5LiNi1fexHMp0njpfkr+xQs2EHJjUKclfAruQIH12bWFL0FRZzskNI fcJGfI6DHpRMvTKcBQJrG211/QBC9QwPNL2jMiE8= From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Cc: ddis@suse.com, Filipe Manana Subject: [PATCH 3/3] Btrfs-progs: add test for receive Date: Mon, 14 Jan 2019 13:31:46 +0000 Message-Id: <20190114133146.18424-1-fdmanana@kernel.org> X-Mailer: git-send-email 2.11.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Filipe Manana Add a test for a scenario that used to fail due to find_mount_root() incorrectly determining the mount point for the receive path due to the fact that a different mount point with a path that is a prefix of the receive path exists. This is fixed by a recent patch titled: "Btrfs-progs: fix mount point detection due to partial prefix match" Reported-by: David Disseldorp Signed-off-by: Filipe Manana Reviewed-by: David Disseldorp --- .../034-receive-common-mount-point-prefix/test.sh | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 tests/misc-tests/034-receive-common-mount-point-prefix/test.sh diff --git a/tests/misc-tests/034-receive-common-mount-point-prefix/test.sh b/tests/misc-tests/034-receive-common-mount-point-prefix/test.sh new file mode 100755 index 00000000..e2a6fdfc --- /dev/null +++ b/tests/misc-tests/034-receive-common-mount-point-prefix/test.sh @@ -0,0 +1,47 @@ +#!/bin/bash +# Test that receive determines the correct mount point path when there is +# another mount point that matches the destination's path as a prefix. + +source "$TEST_TOP/common" + +check_prereq btrfs +check_prereq mkfs.btrfs + +setup_root_helper + +rm -f dev1 dev2 +run_check truncate -s 1G dev1 +run_check truncate -s 1G dev2 +chmod a+w dev1 dev2 + +loop1=$(run_check_stdout $SUDO_HELPER losetup --find --show dev1) +loop2=$(run_check_stdout $SUDO_HELPER losetup --find --show dev2) + +run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f "$loop1" +run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f "$loop2" + +run_check $SUDO_HELPER mount "$loop1" "$TEST_MNT" +mkdir "${TEST_MNT}/ddis" +mkdir "${TEST_MNT}/ddis-not-a-mount" +run_check $SUDO_HELPER mount "$loop2" "${TEST_MNT}/ddis" + +echo "some data" > "${TEST_MNT}/ddis/file" + +run_check $SUDO_HELPER "$TOP/btrfs" subvolume snapshot -r \ + "${TEST_MNT}/ddis" "${TEST_MNT}/ddis/snap" + +rm -f send.data +run_check $SUDO_HELPER "$TOP/btrfs" send -f send.data "${TEST_MNT}/ddis/snap" + +# The following receive used to fail because it incorrectly determined the mount +# point of the destination path to be ${TEST_MNT}/ddis and not $TEST_MNT. +run_check $SUDO_HELPER "$TOP/btrfs" receive -f send.data \ + "${TEST_MNT}/ddis-not-a-mount" + +run_check $SUDO_HELPER umount "${TEST_MNT}/ddis" +run_check $SUDO_HELPER umount "$TEST_MNT" + +# Cleanup loop devices and send data. +run_check $SUDO_HELPER losetup -d "$loop1" +run_check $SUDO_HELPER losetup -d "$loop2" +rm -f dev1 dev2 send.data