Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5539370imu; Mon, 26 Nov 2018 01:47:12 -0800 (PST) X-Google-Smtp-Source: AJdET5ea9lHL4MDJUwEMSAyALu1Xmnk+nQ6DWxjuJu+bEBfVAPYUo40EyTm5hNh5rUmrNqSHrHGS X-Received: by 2002:a62:3888:: with SMTP id f130mr26608663pfa.132.1543225632468; Mon, 26 Nov 2018 01:47:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543225632; cv=none; d=google.com; s=arc-20160816; b=Wa2VUhGcUWWpuVgtm3bG+/Qv9wFNWkWkpSs4Gc6I9T6YLqCjS9IcX6guYn7uAYecNT VoebNdRavNLkWDt1hs7mQVxLO67YTTiAbwyWUl/ep7VZIbDpWdUG+SSY+3RoakxEUzh7 CthJ5dGLSPwLJBmD7Uk+N8auWZqTCPli/gadoFC+Vw0StUEJdVWJMrsBvogtOzzELssc Jx86DTVJz9jQzjO0v/bvN5Wcs7MxKHOmNA+VIgD7nf5OcTCWQOWpDZV7wCs0/vKe1cst 10Y3NXARgTscH42ABDMKpYuE/dsWo0E9+YvpjMPBv2gAwVkj2p7VLBWmNzm58e3/ivUC r2rA== 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:message-id:date:to:from:subject; bh=CYvt6nXKEzpftCLutKOiGNHW8wWxO1icVWY1qFd5zgs=; b=lssF4LowwOQ2wmciGTX3oVufV7+2rqJZ87FY5sud2VVKsjhjHbQ7SEPBlhfaOjDAzd Byvunq2fjgp5HJyaF8DZPrnXq2cdFD6l/B33VlpIqa0zTBrA3w+FiIJJyf0jjWOLx/Jc bqj/br6Jyik9TFqxBtV2H6lkiibG6+zyi3ia5e5VDdOMwwrGBkjYW0fpJ2qckDpl+yJ7 2wr/zk9soxm26jnEPAt7foPyVOKh6qSXUCG/0IdyWOyBevsFR4B2nqagP4je+WdQsGog wG7NHrjiGVXgNxmY/RTT3ihP437iVCKlUgIecGXBSiIMeSsmDsctUbsHbHSQ+9uU2J0i ltOA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=virtuozzo.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e9si50687208plt.330.2018.11.26.01.46.57; Mon, 26 Nov 2018 01:47:12 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=virtuozzo.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726399AbeKZUjv (ORCPT + 99 others); Mon, 26 Nov 2018 15:39:51 -0500 Received: from relay.sw.ru ([185.231.240.75]:49788 "EHLO relay.sw.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726203AbeKZUju (ORCPT ); Mon, 26 Nov 2018 15:39:50 -0500 Received: from [172.16.25.169] (helo=localhost.localdomain) by relay.sw.ru with esmtp (Exim 4.91) (envelope-from ) id 1gRDT2-0007Bu-DM; Mon, 26 Nov 2018 12:46:12 +0300 Subject: [PATCH 1/2] fuse: Fix race in fuse_writepage_in_flight() From: Kirill Tkhai To: miklos@szeredi.hu, ktkhai@virtuozzo.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Date: Mon, 26 Nov 2018 12:46:10 +0300 Message-ID: <154322517208.18737.3297786654135648324.stgit@localhost.localdomain> User-Agent: StGit/0.18 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Checking for FR_PENDING in fuse_writepage_in_flight() is racy. It does not guarantee the first request in misc.write.next list is not in userspace, since there we take fc->lock, while fuse_dev_do_read() takes fiq->waitq.lock: fuse_dev_read() fuse_writepage_in_flight() test_bit(FR_PENDING) clear_bit(FR_PENDING) handle old_req->pages[0] in userspace copy_highpage(old_req->pages[0], page) ^^^^^ userspace never sees this pages The only reliable way to determ, whether we are able to replace old_req's page, is to completely skip the first request in the list. This patch makes the function to do that. Signed-off-by: Kirill Tkhai --- fs/fuse/file.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/fuse/file.c b/fs/fuse/file.c index b52f9baaa3e7..c6650c68b31a 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -1740,6 +1740,7 @@ static bool fuse_writepage_in_flight(struct fuse_req *new_req, { struct fuse_conn *fc = get_fuse_conn(new_req->inode); struct fuse_inode *fi = get_fuse_inode(new_req->inode); + struct fuse_req *first_req; struct fuse_req *tmp; struct fuse_req *old_req; bool found = false; @@ -1764,6 +1765,7 @@ static bool fuse_writepage_in_flight(struct fuse_req *new_req, } new_req->num_pages = 1; + first_req = old_req; for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) { BUG_ON(tmp->inode != new_req->inode); curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT; @@ -1773,7 +1775,7 @@ static bool fuse_writepage_in_flight(struct fuse_req *new_req, } } - if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) { + if (old_req->num_pages == 1 && old_req != first_req) { struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host); copy_highpage(old_req->pages[0], page);