Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4820295ybl; Wed, 22 Jan 2020 05:22:41 -0800 (PST) X-Google-Smtp-Source: APXvYqwdrIk2Lsz2lPbOvRYMdJtO3ZT/Ms3k8kICnHHIvYLYPKJLP/llnmQk1G/C/Mm+WKqOs4XE X-Received: by 2002:a54:4896:: with SMTP id r22mr6832412oic.30.1579699360865; Wed, 22 Jan 2020 05:22:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579699360; cv=none; d=google.com; s=arc-20160816; b=bHIxU0d0HI4h3wUpqqVEkHkTe2PEz+GKeIgj6BaCm382SMOkRLpILMwbOeXCzKIpsm op+WOtbJTMLBnDogi6Satd2w9xUT3IBAkyhcThQrIKxleAYC7d5p7BQyvY+FnCsxHOVa RtYgFAFdMAkpCtA3zHQOBeoYXr3+EF71O58VTeJn+v6q2xuNTPs75S0RHOTVnyVls2qo xwtpJXxrom+DqF6yk+/+hYbJkkWRBN1p+j3UdQwhpgb8P1/xdXFIMa3T8a7Nb2q0LaPc Szz893J0vqptS9N6r6HqgaB0PUgBxyDW33u/XPt3XP7GVJEAvldh9C5rpsqKupEICoM1 Emow== 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:dkim-signature; bh=0Ba5SeA2flD3UW/dGk4ze3sVNgPYn2fZt+ymlUog2Bk=; b=xN9t7Yz3d0mcDXWxAwRHD/ZJyMqRwYOlZQqeKo4+LfTiKRaWsIVt/5v4pLH2pRZ2Ck 5PKBKF7+ZFLKiRazgCaFEscfXq7f3cBP+cqrWph2HJS3tnFzjY19dBPptzRXfD32NVBW Fr3B2Bu9lILNwVT6NgAykWleEMt5J8xANWINud100Q5UIUuFpsohYYrDqoC8VkCnF6XF ErHQHRifqT0iCXXgxv1EhvwKzGRuYX0ECUV+OIbk7tCq4NIQi7/0DJSRBgDeZN2ZrUUc jWhChSuTEAAVfx6ZvuS26EObEtEbnvrYwPcuV4rFlnWT8kckYnR16RinOQk1HS2LdZaT Ni9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=t4Ndmqb6; 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 q72si22157972oic.18.2020.01.22.05.22.29; Wed, 22 Jan 2020 05:22:40 -0800 (PST) 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=t4Ndmqb6; 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 S1730135AbgAVNU7 (ORCPT + 99 others); Wed, 22 Jan 2020 08:20:59 -0500 Received: from mail.kernel.org ([198.145.29.99]:37414 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726054AbgAVNU4 (ORCPT ); Wed, 22 Jan 2020 08:20:56 -0500 Received: from localhost (unknown [84.241.205.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 68B9F2468B; Wed, 22 Jan 2020 13:20:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1579699255; bh=Z5U8oa2rY/Qhnxybo2dbSelwKsx2b8hEe5G6JYAeth4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t4Ndmqb6sfPL4zq2zIdRioeuPber4tf0/LVrjfO630KnR04vVovKqb1UdG2XK+pF0 icNVP6ci3I4i9w6Z4xDZpzHeKB77hI0N+MqMAWgQhJk6ZKhKqDG6sb3BWH704uhG/y oy0CGT+LZ7+pR0BkF5Q/6CsRslb2fPSgVjKAcryA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Graham Cobb , Johannes Thumshirn , Qu Wenruo , Filipe Manana , David Sterba Subject: [PATCH 5.4 089/222] Btrfs: always copy scrub arguments back to user space Date: Wed, 22 Jan 2020 10:27:55 +0100 Message-Id: <20200122092840.100648986@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200122092833.339495161@linuxfoundation.org> References: <20200122092833.339495161@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Filipe Manana commit 5afe6ce748c1ea99e0d648153c05075e1ab93afb upstream. If scrub returns an error we are not copying back the scrub arguments structure to user space. This prevents user space to know how much progress scrub has done if an error happened - this includes -ECANCELED which is returned when users ask for scrub to stop. A particular use case, which is used in btrfs-progs, is to resume scrub after it is canceled, in that case it relies on checking the progress from the scrub arguments structure and then use that progress in a call to resume scrub. So fix this by always copying the scrub arguments structure to user space, overwriting the value returned to user space with -EFAULT only if copying the structure failed to let user space know that either that copying did not happen, and therefore the structure is stale, or it happened partially and the structure is probably not valid and corrupt due to the partial copy. Reported-by: Graham Cobb Link: https://lore.kernel.org/linux-btrfs/d0a97688-78be-08de-ca7d-bcb4c7fb397e@cobb.uk.net/ Fixes: 06fe39ab15a6a4 ("Btrfs: do not overwrite scrub error with fault error in scrub ioctl") CC: stable@vger.kernel.org # 5.1+ Reviewed-by: Johannes Thumshirn Reviewed-by: Qu Wenruo Tested-by: Graham Cobb Signed-off-by: Filipe Manana Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/ioctl.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -4254,7 +4254,19 @@ static long btrfs_ioctl_scrub(struct fil &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 0); - if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) + /* + * Copy scrub args to user space even if btrfs_scrub_dev() returned an + * error. This is important as it allows user space to know how much + * progress scrub has done. For example, if scrub is canceled we get + * -ECANCELED from btrfs_scrub_dev() and return that error back to user + * space. Later user space can inspect the progress from the structure + * btrfs_ioctl_scrub_args and resume scrub from where it left off + * previously (btrfs-progs does this). + * If we fail to copy the btrfs_ioctl_scrub_args structure to user space + * then return -EFAULT to signal the structure was not copied or it may + * be corrupt and unreliable due to a partial copy. + */ + if (copy_to_user(arg, sa, sizeof(*sa))) ret = -EFAULT; if (!(sa->flags & BTRFS_SCRUB_READONLY))