Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp3396649ybp; Sun, 6 Oct 2019 11:01:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqyxVxrbzwy/0wS4L57Aeuj4v/gzWsiErFQUzQdUXdJ+vVmPOQGBBZBgDFB3yenOE+rFTrx8 X-Received: by 2002:a17:906:9703:: with SMTP id k3mr20370258ejx.159.1570384918586; Sun, 06 Oct 2019 11:01:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570384918; cv=none; d=google.com; s=arc-20160816; b=LYsCLl23lZYW3KRSSY4FyAk8On/yES5ThgJCeCNwbbjeK4FpkETJfSYn8mJQvmUW4a +8h7CVes+U7G0Y2IeX+5+jpR8W4RWcDuY0HmJJChbumL59viAERjQ1UB44sBGDLfop7S ITaaGxjtUf9O4vV0Sh8CyxtRa5GsTMz9QITFxB7XjLYCnojxSqEdhXXImOWMCHC50Boh Ty1XdPJ+q6V+lEJaJlV6vP2vpDDA2Z9x3rK3uRBhX2Y9ISnPYyxQmbqkyfS54AB8iKep FbCbOaTUqr5nc81UfFO0aoWME9hklsUc52Hwo1k7ndKjf8WObKWgurvB2GrTfAlzQvVM N7Eg== 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=d0ALvCUZFfJ4KSSnxpe0d41LB/WZCjl6n8NUG+Kfb0c=; b=N7z1w+Q1jn63JjftGfd3qxDQ6ZE96xd0gtUx/tXdiXOOafMOhweSlsqB5XDqP49rVc kEz7uHFwVAu5Dck7/7oH9Zt52FnG45S8c/TxjNnP0ixMJfAgNQBe2cG3Pyy0noT3U+7e BFMKTRPROD3N+AnSK2S/MJRZq+LjVM1N4WFawVoC3gOGHaKw49XWYCCE5IeoxjB52jQU vgZPhDTpNArtpZtPlYXI0bjrZFJ5ynI+xGgk9QMsx/sSZLhN/FYqMps8uXnspXuMh0nq IsAui/YvcKgHshQdWTs323Nkiz/JBe2GbI08ndmyVs75lio0VDonU8DuM5w9Y/QiaQkD /DrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=aQDRvX4F; 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 e25si7628958edb.308.2019.10.06.11.01.34; Sun, 06 Oct 2019 11:01:58 -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=aQDRvX4F; 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 S1728121AbfJFRa7 (ORCPT + 99 others); Sun, 6 Oct 2019 13:30:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:56886 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729311AbfJFRax (ORCPT ); Sun, 6 Oct 2019 13:30:53 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 184282080F; Sun, 6 Oct 2019 17:30:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1570383051; bh=BN/UoRKBV+sw1VEV1yj7fUb/TE6RODNCU0K+r99yFyo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aQDRvX4F0PqNcVYDDgMECgO/ZPC72rC8SKKJBvsZjceFxEVy68GSAwhVboH++WzDI wd7F+Ep2YXaGLml66Hm9omKOD2CnESkmjOQpU7h9sF1gK937F2HigMHK/dyoiXmiLy 7FQB6BmG2JELL7oz1ENNb+btDxEjRlHU1L7Bv1hc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, OGAWA Hirofumi , Jan Stancek , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 4.19 067/106] fat: work around race with userspaces read via blockdev while mounting Date: Sun, 6 Oct 2019 19:21:13 +0200 Message-Id: <20191006171151.739982449@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191006171124.641144086@linuxfoundation.org> References: <20191006171124.641144086@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: OGAWA Hirofumi [ Upstream commit 07bfa4415ab607e459b69bd86aa7e7602ce10b4f ] If userspace reads the buffer via blockdev while mounting, sb_getblk()+modify can race with buffer read via blockdev. For example, FS userspace bh = sb_getblk() modify bh->b_data read ll_rw_block(bh) fill bh->b_data by on-disk data /* lost modified data by FS */ set_buffer_uptodate(bh) set_buffer_uptodate(bh) Userspace should not use the blockdev while mounting though, the udev seems to be already doing this. Although I think the udev should try to avoid this, workaround the race by small overhead. Link: http://lkml.kernel.org/r/87pnk7l3sw.fsf_-_@mail.parknet.co.jp Signed-off-by: OGAWA Hirofumi Reported-by: Jan Stancek Tested-by: Jan Stancek Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- fs/fat/dir.c | 13 +++++++++++-- fs/fat/fatent.c | 3 +++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/fs/fat/dir.c b/fs/fat/dir.c index 7f5f3699fc6c0..de60c05c0ca1d 100644 --- a/fs/fat/dir.c +++ b/fs/fat/dir.c @@ -1097,8 +1097,11 @@ static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used, err = -ENOMEM; goto error; } + /* Avoid race with userspace read via bdev */ + lock_buffer(bhs[n]); memset(bhs[n]->b_data, 0, sb->s_blocksize); set_buffer_uptodate(bhs[n]); + unlock_buffer(bhs[n]); mark_buffer_dirty_inode(bhs[n], dir); n++; @@ -1155,6 +1158,8 @@ int fat_alloc_new_dir(struct inode *dir, struct timespec64 *ts) fat_time_unix2fat(sbi, ts, &time, &date, &time_cs); de = (struct msdos_dir_entry *)bhs[0]->b_data; + /* Avoid race with userspace read via bdev */ + lock_buffer(bhs[0]); /* filling the new directory slots ("." and ".." entries) */ memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME); memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME); @@ -1177,6 +1182,7 @@ int fat_alloc_new_dir(struct inode *dir, struct timespec64 *ts) de[0].size = de[1].size = 0; memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de)); set_buffer_uptodate(bhs[0]); + unlock_buffer(bhs[0]); mark_buffer_dirty_inode(bhs[0], dir); err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE); @@ -1234,11 +1240,14 @@ static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots, /* fill the directory entry */ copy = min(size, sb->s_blocksize); + /* Avoid race with userspace read via bdev */ + lock_buffer(bhs[n]); memcpy(bhs[n]->b_data, slots, copy); - slots += copy; - size -= copy; set_buffer_uptodate(bhs[n]); + unlock_buffer(bhs[n]); mark_buffer_dirty_inode(bhs[n], dir); + slots += copy; + size -= copy; if (!size) break; n++; diff --git a/fs/fat/fatent.c b/fs/fat/fatent.c index f58c0cacc531d..4c6c635bc8aaa 100644 --- a/fs/fat/fatent.c +++ b/fs/fat/fatent.c @@ -390,8 +390,11 @@ static int fat_mirror_bhs(struct super_block *sb, struct buffer_head **bhs, err = -ENOMEM; goto error; } + /* Avoid race with userspace read via bdev */ + lock_buffer(c_bh); memcpy(c_bh->b_data, bhs[n]->b_data, sb->s_blocksize); set_buffer_uptodate(c_bh); + unlock_buffer(c_bh); mark_buffer_dirty_inode(c_bh, sbi->fat_inode); if (sb->s_flags & SB_SYNCHRONOUS) err = sync_dirty_buffer(c_bh); -- 2.20.1