Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp820991lqh; Thu, 28 Mar 2024 18:58:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWrRS9B8SfKKffFY9fOtUKIEVOZStHgY8NFyIaeJa9DYEyPKRNxoScQ/n4eRLnAiLt98vWUnS/57bGc7o4l3aulXPrxg9CgjLC+tQ1Ieg== X-Google-Smtp-Source: AGHT+IGciMS1Ks8XHMKur6P3vPoQvpl16lctOsC/Sc4TqV381CgGYsIOg+jNzxKq4l9OuLDjvcbU X-Received: by 2002:a05:6808:1308:b0:3c3:d75c:c900 with SMTP id y8-20020a056808130800b003c3d75cc900mr1114696oiv.0.1711677517296; Thu, 28 Mar 2024 18:58:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711677517; cv=pass; d=google.com; s=arc-20160816; b=CmuvB2u9M6EflTOPITWC2FqjCHvjlZSePBcObLo+CCAnxQJRSqMfugNg6rLUhtHKwP 9KMoJmg4w6XE2QTs321JLOtxlzasnFNd+Bungcn2RE0SbpeDc0YXk9uw39BNtaSdYb2N ZkM/3tUH0Mb8NaztoNrRT0ThXaWy4NwyuKWGzzhH5UNJzCELcJE8PpNA12gJU+1MoNWc /FTRfUtUZ27wgHzFKN0AB5RLfzpGK/HDKc7niPG7e03VHaFO7o6uSWCIoNw9F/cfRqYb ydm+fDEFSWD2tDxGz1PZzr4NGy2ym8hdL2rCadQlCrdEy/doHD3BKdoiNYCZsq6HEZHc 9gew== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=rxMPvHmZ0NJBpckZ6zd2n+WU0SbXQuOXvLdtYgKifzc=; fh=b+qaGUEhKy8dYpMt9gL6hWWOXbASh7qUa+yEg228t94=; b=xIPoiAHu1r4qTLFLDLrSSKg/BfxtP5rwFN2r/jBTPRtzu88lFQ/V/BMzsJueRWmqWX qDZlkxlob2Q7te9uqJXqFfBmCIgituKsiKT6O6BtKClM7dmwjwG+u1/Imm1Pj+0Aco4h W15u1yBPZ/yCGt6At0irqBZRCGFmN+5pw+vOngCeawV+Awena+zuOJ4rdUJWDRaPuSHX 9N74D3rQLF4En5kdtHGL7Xuq5r6HW0StEvRGr2W4l7bHu+rRtAHtpuPCp2o3XqT6EMEy 4rw4pC3rHjBg+H2VLWLAS5iqXbfRT2T90DOdXUEEZ8F5EOfYxGzvL7IwLxwsoOrCluH6 nl2A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=uCjZBU7q; arc=pass (i=1 spf=pass spfdomain=flex--drosen.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-123984-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123984-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id u35-20020a056a0009a300b006e6b6887211si2722627pfg.377.2024.03.28.18.58.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 18:58:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-123984-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=uCjZBU7q; arc=pass (i=1 spf=pass spfdomain=flex--drosen.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-123984-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123984-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id D3D90B23187 for ; Fri, 29 Mar 2024 01:56:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 55A9238FA1; Fri, 29 Mar 2024 01:54:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uCjZBU7q" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 63BFF37160 for ; Fri, 29 Mar 2024 01:54:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711677253; cv=none; b=PhEw18dEXLlVgC+2Eu96LKmsGSCZ0DVIs2oaF7jTtawEncn+To+QwpAHCTyWh/2pKJHClNsHlxR5dZLaTYXDj/o6CTC3JWhkdQXuCmTPjm3gLu/jKBSxdtAB31uyC/k5Zacs/J/zUcUseVQN33zVd4BYNkPabHRqmluycW5M54s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711677253; c=relaxed/simple; bh=Ib5M0mE4oOOU/5nwAtZYmwkWiH40UcIcM9efdJ1OU34=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=a2hHOk5oVUu700Eq3CSHViISwkQ1H6OQzZ/0CYKmLxK6xRxOjBl5DOCYw+Bza9q2YkPghiU9be5lGvxSaosR2RpzK4fnl+Y/v81n6eoXWpGydrHDEh7GJo51BBl91UZW609uVdJeWmx1bM92ekzqId7V7nYtGtaqQwzrliR5KUk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--drosen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=uCjZBU7q; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--drosen.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dbe9e13775aso2513966276.1 for ; Thu, 28 Mar 2024 18:54:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711677250; x=1712282050; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=rxMPvHmZ0NJBpckZ6zd2n+WU0SbXQuOXvLdtYgKifzc=; b=uCjZBU7qN3mc0RtSMPWB1HF43nsmquKuWZwCiMPQmW1bNx5+Yr86QSapo54ubxdWHm FnEvwZPTRGQW3aUHyh5+pxDyz/QcKv2V8P+P7h5osplxGCmSVRJ+WZx8rQBApuYD7lZ/ qrN/GDyIYtJOTQufZUehG5iYJX7r8IoUJ5LoCxJE9vna58PpT+vWgzJM52qe3WkB28b+ w+6GRKI8xy/8z5ZWWlxlslpn6clmPK9xqnme043p97zou5witIEteQrCLW4Kh8bvlEcO MR+gY9WeW6Q/gYEBrlwi0JvcIaqnAuNrUvXmEiD//xC22Ena2Uz5t9cwrWuNt69ix4h2 5LxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711677250; x=1712282050; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rxMPvHmZ0NJBpckZ6zd2n+WU0SbXQuOXvLdtYgKifzc=; b=HHy2G2VSdIIa0T7xSCsNzkiPajR6pl0YyKulvnDQl+jbEreqhuYReHzj7KMugfjbsJ XkPeJZ/nIuyJYDp5fnHjfmm5bLNeYxb9aYSgDMVdOp1tezirfN6A/2Fq26UFKpomlGB3 80M9iM4vz1ac1qzD9Aqa1G3JISGFdD4wx9eAhYi+24xzoHDGUOqzUbd2JdzHQrZLkHLt Pt/ZiP/+PQxhbRG9i99MnafT0khn6tyTuOXGMEn+FrSyw0WoMa+jllINwH77DaWguaeu YTBqtFLhH6gyfLVULGDjQ6q50KS1BscnT59XXKHmwWZ38V06VODwAZbQViTX8ucThTb6 GZ7A== X-Forwarded-Encrypted: i=1; AJvYcCW0U1xUU3FDiOINqjQ3gZ/rDbB2oo7o4ehqd0qTg4giwnpgUT0/ya5sq/6Jj/EnLeNx01VuOBb83hKKYbq7rbNp1i3qyfNt+PGKWnVT X-Gm-Message-State: AOJu0YzubiwyE+nHuBAB0UvT8fMIHaq730tu+GXYKz7cWQhIJjrjkSJM JOSRmvnZmUb2+6srMFbEvOLZ9FLDBEdlCQk03MeAAq6XkpaEH5ZXUOMeyR9iRA/d/mXGadD6Poh R2A== X-Received: from drosen.mtv.corp.google.com ([2620:15c:211:201:fcce:d6ab:804c:b94b]) (user=drosen job=sendgmr) by 2002:a05:6902:2291:b0:dc7:42:ecd with SMTP id dn17-20020a056902229100b00dc700420ecdmr315101ybb.6.1711677250595; Thu, 28 Mar 2024 18:54:10 -0700 (PDT) Date: Thu, 28 Mar 2024 18:53:20 -0700 In-Reply-To: <20240329015351.624249-1-drosen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240329015351.624249-1-drosen@google.com> X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240329015351.624249-6-drosen@google.com> Subject: [RFC PATCH v4 05/36] fuse-bpf: Add ioctl interface for /dev/fuse From: Daniel Rosenberg To: Miklos Szeredi , bpf@vger.kernel.org, Alexei Starovoitov Cc: Amir Goldstein , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-unionfs@vger.kernel.org, Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Eduard Zingerman , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Jonathan Corbet , Joanne Koong , Mykola Lysenko , Christian Brauner , kernel-team@android.com, Daniel Rosenberg Content-Type: text/plain; charset="UTF-8" This introduces an alternative method of responding to fuse requests. Lookups supplying a backing fd or bpf will need to call through the ioctl to ensure there can be no attempts to fool priveledged processes into inadvertantly performing other actions. Signed-off-by: Daniel Rosenberg --- fs/fuse/dev.c | 56 ++++++++++++++++++++++++++++++++------- fs/fuse/fuse_i.h | 1 + include/uapi/linux/fuse.h | 1 + 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c index b413e0bfd61c..e51ec198af3f 100644 --- a/fs/fuse/dev.c +++ b/fs/fuse/dev.c @@ -1016,18 +1016,19 @@ static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size) /* Copy the fuse-bpf lookup args and verify them */ #ifdef CONFIG_FUSE_BPF -static int fuse_copy_lookup(struct fuse_copy_state *cs, void *val, unsigned size) +static int fuse_copy_lookup(struct fuse_copy_state *cs, unsigned via_ioctl, void *val, unsigned size) { struct fuse_bpf_entry_out *fbeo = (struct fuse_bpf_entry_out *)val; struct fuse_bpf_entry *feb = container_of(fbeo, struct fuse_bpf_entry, out[0]); int num_entries = size / sizeof(*fbeo); int err; - if (size && size % sizeof(*fbeo) != 0) + if (size && (size % sizeof(*fbeo) != 0 || !via_ioctl)) return -EINVAL; if (num_entries > FUSE_BPF_MAX_ENTRIES) return -EINVAL; + err = fuse_copy_one(cs, val, size); if (err) return err; @@ -1036,7 +1037,7 @@ static int fuse_copy_lookup(struct fuse_copy_state *cs, void *val, unsigned size return err; } #else -static int fuse_copy_lookup(struct fuse_copy_state *cs, void *val, unsigned size) +static int fuse_copy_lookup(struct fuse_copy_state *cs, unsigned via_ioctl, void *val, unsigned size) { return fuse_copy_one(cs, val, size); } @@ -1045,7 +1046,7 @@ static int fuse_copy_lookup(struct fuse_copy_state *cs, void *val, unsigned size /* Copy request arguments to/from userspace buffer */ static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs, unsigned argpages, struct fuse_arg *args, - int zeroing, unsigned is_lookup) + int zeroing, unsigned is_lookup, unsigned via_ioct) { int err = 0; unsigned i; @@ -1055,7 +1056,7 @@ static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs, if (i == numargs - 1 && argpages) err = fuse_copy_pages(cs, arg->size, zeroing); else if (i == numargs - 1 && is_lookup) - err = fuse_copy_lookup(cs, arg->value, arg->size); + err = fuse_copy_lookup(cs, via_ioct, arg->value, arg->size); else err = fuse_copy_one(cs, arg->value, arg->size); } @@ -1333,7 +1334,7 @@ static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file, err = fuse_copy_one(cs, &req->in.h, sizeof(req->in.h)); if (!err) err = fuse_copy_args(cs, args->in_numargs, args->in_pages, - (struct fuse_arg *) args->in_args, 0, 0); + (struct fuse_arg *) args->in_args, 0, 0, 0); fuse_copy_finish(cs); spin_lock(&fpq->lock); clear_bit(FR_LOCKED, &req->flags); @@ -1872,7 +1873,8 @@ static int copy_out_args(struct fuse_copy_state *cs, struct fuse_args *args, lastarg->size -= diffsize; } return fuse_copy_args(cs, args->out_numargs, args->out_pages, - args->out_args, args->page_zeroing, args->is_lookup); + args->out_args, args->page_zeroing, args->is_lookup, + args->via_ioctl); } /* @@ -1882,7 +1884,7 @@ static int copy_out_args(struct fuse_copy_state *cs, struct fuse_args *args, * it from the list and copy the rest of the buffer to the request. * The request is finished by calling fuse_request_end(). */ -static ssize_t fuse_dev_do_write(struct fuse_dev *fud, +static ssize_t fuse_dev_do_write(struct fuse_dev *fud, bool from_ioctl, struct fuse_copy_state *cs, size_t nbytes) { int err; @@ -1954,6 +1956,7 @@ static ssize_t fuse_dev_do_write(struct fuse_dev *fud, if (!req->args->page_replace) cs->move_pages = 0; + req->args->via_ioctl = from_ioctl; if (oh.error) err = nbytes != sizeof(oh) ? -EINVAL : 0; else @@ -1992,7 +1995,7 @@ static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from) fuse_copy_init(&cs, 0, from); - return fuse_dev_do_write(fud, &cs, iov_iter_count(from)); + return fuse_dev_do_write(fud, false, &cs, iov_iter_count(from)); } static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe, @@ -2073,7 +2076,7 @@ static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe, if (flags & SPLICE_F_MOVE) cs.move_pages = 1; - ret = fuse_dev_do_write(fud, &cs, len); + ret = fuse_dev_do_write(fud, false, &cs, len); pipe_lock(pipe); out_free: @@ -2286,6 +2289,33 @@ static int fuse_device_clone(struct fuse_conn *fc, struct file *new) return 0; } +// Provides an alternate means to respond to a fuse request +static int fuse_handle_ioc_response(struct fuse_dev *dev, void *buff, uint32_t size) +{ + struct fuse_copy_state cs; + struct iovec *iov = NULL; + struct iov_iter iter; + int res; + + if (size > PAGE_SIZE) + return -EINVAL; + iov = (struct iovec *) __get_free_page(GFP_KERNEL); + if (!iov) + return -ENOMEM; + + iov->iov_base = buff; + iov->iov_len = size; + + iov_iter_init(&iter, READ, iov, 1, size); + fuse_copy_init(&cs, 0, &iter); + + + res = fuse_dev_do_write(dev, true, &cs, size); + free_page((unsigned long) iov); + + return res; +} + static long fuse_dev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { @@ -2319,6 +2349,12 @@ static long fuse_dev_ioctl(struct file *file, unsigned int cmd, fdput(f); break; default: + if (_IOC_TYPE(cmd) == FUSE_DEV_IOC_MAGIC + && _IOC_NR(cmd) == _IOC_NR(FUSE_DEV_IOC_BPF_RESPONSE(0)) + && _IOC_DIR(cmd) == _IOC_WRITE) { + res = fuse_handle_ioc_response(fuse_get_dev(file), (void *) arg, _IOC_SIZE(cmd)); + break; + } res = -ENOTTY; break; } diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index ac61f08fd85d..dd62e78e474c 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -336,6 +336,7 @@ struct fuse_args { bool may_block:1; bool is_ext:1; bool is_lookup:1; + bool via_ioctl:1; struct fuse_in_arg in_args[3]; struct fuse_arg out_args[2]; void (*end)(struct fuse_mount *fm, struct fuse_args *args, int error); diff --git a/include/uapi/linux/fuse.h b/include/uapi/linux/fuse.h index 0c0a9a8b5c26..74bc15e1d0b7 100644 --- a/include/uapi/linux/fuse.h +++ b/include/uapi/linux/fuse.h @@ -1075,6 +1075,7 @@ struct fuse_notify_retrieve_in { /* Device ioctls: */ #define FUSE_DEV_IOC_MAGIC 229 #define FUSE_DEV_IOC_CLONE _IOR(FUSE_DEV_IOC_MAGIC, 0, uint32_t) +#define FUSE_DEV_IOC_BPF_RESPONSE(N) _IOW(FUSE_DEV_IOC_MAGIC, 125, char[N]) struct fuse_lseek_in { uint64_t fh; -- 2.44.0.478.gd926399ef9-goog