Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp1131593ybd; Sun, 23 Jun 2019 00:42:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqyoEV8emtNYivQpTPBNjR4fdLjpx9nN5hSwwjO+njAXf9zCwQ15VTcWIgiFS9tGmR7M0wIA X-Received: by 2002:a17:902:7603:: with SMTP id k3mr34190864pll.245.1561275726399; Sun, 23 Jun 2019 00:42:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561275726; cv=none; d=google.com; s=arc-20160816; b=lh/seRToxJzCcjU1ioKYw0CfB1Uyx/p7kfuACuX8F+hCTeFXRXUOqODAQ6RTxtlazk z9J7yQTd/ySlWLwiGYPj121gFuLF3N1smNIYO+a2DD8QMUSM/cxqaWRfe1THT9QDcALA wFMMv7lM9O82hCzW4q6dYoh1BccrqIPG/DS9fI/NtrBbEYllHyitm4x5cJp8lq3qGVsH XZ0z6OQZjpu/FwsyL0jQFfUoXuAnz5K/i4yOWtw8VmNMhCzMy+rd0aL8dWOdJgcDKxww jPC8qGNeZhRZgvQiQejbYFo5rVuvklvGoOgfRG8KqlRHzuI9RaP/sUIphaSIwrEpkQtS kLpQ== 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 :date:references:in-reply-to:message-id:cc:to:subject:from :dkim-signature:dkim-signature; bh=dSY8NMzAi0uW7gbo3MkKCEB6rvCWIFKhM/6k9k6ce5E=; b=P2mK/u2pgwE73kTWFsmRG7oJ+RvcyTx5u/hhxes+nw32qOOYtS4J9SAEpnIR1bEhWG 1I4lnpZ7Zv4rzcZqUiwWY8V0Qj380ybpTRtiy3ZbVJnFKc7BEmiiZ/9VsRc+40dykhro rzIYRFFDyCDe3SB3a0wJXoCWcULLWzpUt370tQ/uYYGIvg6c5a6Pa7e4I3IMACbLy2rE ZR5D0LL5/oq1X3yKqw1jblIojjC6nljsUPfgGsUdx4OUYG2gKnj+62Q5oVoAjyrpJg72 asJJXRbzg1FafVaAziPk+iRBejdLu1FfNEeArk72pElIlrvEhDN49F6f1sPx8Wex4G+t pGtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nexedi.com header.s=mandrill header.b=qdl30SBW; dkim=pass header.i=@mandrillapp.com header.s=mandrill header.b=NAKfNnZi; 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 c1si224587plr.405.2019.06.23.00.41.50; Sun, 23 Jun 2019 00:42:06 -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=@nexedi.com header.s=mandrill header.b=qdl30SBW; dkim=pass header.i=@mandrillapp.com header.s=mandrill header.b=NAKfNnZi; 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 S1726414AbfFWHlm (ORCPT + 99 others); Sun, 23 Jun 2019 03:41:42 -0400 Received: from mail10.wdc04.mandrillapp.com ([205.201.139.10]:1361 "EHLO mail10.wdc04.mandrillapp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726056AbfFWHlm (ORCPT ); Sun, 23 Jun 2019 03:41:42 -0400 X-Greylist: delayed 901 seconds by postgrey-1.27 at vger.kernel.org; Sun, 23 Jun 2019 03:41:40 EDT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; s=mandrill; d=nexedi.com; h=From:Subject:To:Cc:Message-Id:In-Reply-To:References:Date:MIME-Version:Content-Type:Content-Transfer-Encoding; i=kirr@nexedi.com; bh=dSY8NMzAi0uW7gbo3MkKCEB6rvCWIFKhM/6k9k6ce5E=; b=qdl30SBWmhx9DIxJm4gPr9bhAmF1Fj9mBA+xccPr0GTjFyRGf6e8Vvv6btalLaeLv2xYAlECzakQ TBBHPj86GaTL/HePtxMB9EbFDsLD6l998s7hbsA4E0kjNnIExsGXLmeSLSOeZi78WkdKQrgsVXVF YhOX5xUAxy/5/uiI7kM= Received: from pmta08.mandrill.prod.suw01.rsglab.com (127.0.0.1) by mail10.wdc04.mandrillapp.com id h1smj81jvmga for ; Sun, 23 Jun 2019 07:26:38 +0000 (envelope-from ) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandrillapp.com; i=@mandrillapp.com; q=dns/txt; s=mandrill; t=1561274798; h=From : Subject : To : Cc : Message-Id : In-Reply-To : References : Date : MIME-Version : Content-Type : Content-Transfer-Encoding : From : Subject : Date : X-Mandrill-User : List-Unsubscribe; bh=dSY8NMzAi0uW7gbo3MkKCEB6rvCWIFKhM/6k9k6ce5E=; b=NAKfNnZiQPSwteksG3ggLqasc3SZ2dfa8MEBGjnA1SKN4GrsysJ4fGLT1l57yY9FJKTk74 qgU1cbraenhPCzJ7jK0u+5jCkAs7jcU0K6vXWYhVD/lhfUZ3WL22kdmVov02zlTG+eFY2Rrm ijFaR6bh4WYlNbC76JrHjQN5s0bs8= From: Kirill Smelkov Subject: [PATCH, RESEND] fuse: require /dev/fuse reads to have enough buffer capacity (take 2) Received: from [87.98.221.171] by mandrillapp.com id 35928b741ae6496da30a567c5491a435; Sun, 23 Jun 2019 07:26:38 +0000 X-Mailer: git-send-email 2.20.1 To: Miklos Szeredi Cc: Kirill Smelkov , Miklos Szeredi , , , , Sander Eikelenboom , Han-Wen Nienhuys , Jakob Unterwurzacher Message-Id: <20190623072619.31037-1-kirr@nexedi.com> In-Reply-To: References: X-Report-Abuse: Please forward a copy of this message, including all headers, to abuse@mandrill.com X-Report-Abuse: You can also report abuse here: http://mandrillapp.com/contact/abuse?id=31050260.35928b741ae6496da30a567c5491a435 X-Mandrill-User: md_31050260 Date: Sun, 23 Jun 2019 07:26:38 +0000 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 [ This retries commit d4b13963f217 which was reverted in 766741fcaa1f. In this version we require only `sizeof(fuse_in_header) + sizeof(fuse_write_in)` instead of 4K for FUSE request header room, because, contrary to libfuse and kernel client behaviour, GlusterFS actually provides only so much room for request header. ] A FUSE filesystem server queues /dev/fuse sys_read calls to get filesystem requests to handle. It does not know in advance what would be that request as it can be anything that client issues - LOOKUP, READ, WRITE, ... Many requests are short and retrieve data from the filesystem. However WRITE and NOTIFY_REPLY write data into filesystem. Before getting into operation phase, FUSE filesystem server and kernel client negotiate what should be the maximum write size the client will ever issue. After negotiation the contract in between server/client is that the filesystem server then should queue /dev/fuse sys_read calls with enough buffer capacity to receive any client request - WRITE in particular, while FUSE client should not, in particular, send WRITE requests with > negotiated max_write payload. FUSE client in kernel and libfuse historically reserve 4K for request header. However an existing filesystem server - GlusterFS - was found which reserves only 80 bytes for header room (= `sizeof(fuse_in_header) + sizeof(fuse_write_in)`). https://lore.kernel.org/linux-fsdevel/20190611202738.GA22556@deco.navytux.spb.ru/ https://github.com/gluster/glusterfs/blob/v3.8.15-0-gd174f021a/xlators/mount/fuse/src/fuse-bridge.c#L4894 Since `sizeof(fuse_in_header) + sizeof(fuse_write_in)` == `sizeof(fuse_in_header) + sizeof(fuse_read_in)` == `sizeof(fuse_in_header) + sizeof(fuse_notify_retrieve_in)` is the absolute minimum any sane filesystem should be using for header room, the contract is that filesystem server should queue sys_reads with `sizeof(fuse_in_header) + sizeof(fuse_write_in)` + max_write buffer. If the filesystem server does not follow this contract, what can happen is that fuse_dev_do_read will see that request size is > buffer size, and then it will return EIO to client who issued the request but won't indicate in any way that there is a problem to filesystem server. This can be hard to diagnose because for some requests, e.g. for NOTIFY_REPLY which mimics WRITE, there is no client thread that is waiting for request completion and that EIO goes nowhere, while on filesystem server side things look like the kernel is not replying back after successful NOTIFY_RETRIEVE request made by the server. We can make the problem easy to diagnose if we indicate via error return to filesystem server when it is violating the contract. This should not practically cause problems because if a filesystem server is using shorter buffer, writes to it were already very likely to cause EIO, and if the filesystem is read-only it should be too following FUSE_MIN_READ_BUFFER minimum buffer size. Please see [1] for context where the problem of stuck filesystem was hit for real (because kernel client was incorrectly sending more than max_write data with NOTIFY_REPLY; see also previous patch), how the situation was traced and for more involving patch that did not make it into the tree. [1] https://marc.info/?l=linux-fsdevel&m=155057023600853&w=2 Signed-off-by: Kirill Smelkov Tested-by: Sander Eikelenboom Cc: Han-Wen Nienhuys Cc: Jakob Unterwurzacher --- fs/fuse/dev.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c index ea8237513dfa..b2b2344eadcf 100644 --- a/fs/fuse/dev.c +++ b/fs/fuse/dev.c @@ -1317,6 +1317,26 @@ static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file, unsigned reqsize; unsigned int hash; + /* + * Require sane minimum read buffer - that has capacity for fixed part + * of any request header + negotiated max_write room for data. If the + * requirement is not satisfied return EINVAL to the filesystem server + * to indicate that it is not following FUSE server/client contract. + * Don't dequeue / abort any request. + * + * Historically libfuse reserves 4K for fixed header room, but e.g. + * GlusterFS reserves only 80 bytes + * + * = `sizeof(fuse_in_header) + sizeof(fuse_write_in)` + * + * which is the absolute minimum any sane filesystem should be using + * for header room. + */ + if (nbytes < max_t(size_t, FUSE_MIN_READ_BUFFER, + sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + + fc->max_write)) + return -EINVAL; + restart: spin_lock(&fiq->waitq.lock); err = -EAGAIN; -- 2.20.1