From patchwork Tue Nov 12 17:55:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13872734 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 925C7D42BBA for ; Tue, 12 Nov 2024 17:56:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE9296B00A2; Tue, 12 Nov 2024 12:56:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D9B896B00A3; Tue, 12 Nov 2024 12:56:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BECF56B00A7; Tue, 12 Nov 2024 12:56:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 9861E6B00A2 for ; Tue, 12 Nov 2024 12:56:45 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 1C739120486 for ; Tue, 12 Nov 2024 17:56:45 +0000 (UTC) X-FDA: 82778196438.08.FB6C1CE Received: from mail-yw1-f181.google.com (mail-yw1-f181.google.com [209.85.128.181]) by imf08.hostedemail.com (Postfix) with ESMTP id 5DA5F160023 for ; Tue, 12 Nov 2024 17:56:15 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b="F/5EYwAr"; dmarc=none; spf=none (imf08.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.128.181) smtp.mailfrom=josef@toxicpanda.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731434074; a=rsa-sha256; cv=none; b=L29rC39Sd7T7Kxi/HKvReNUPQGo9BjhfaItOrQlVNfJp+u9fy7gYDH56PtjQ1Rd85AAdZF piOMa5Hd2WjpOzOdGoJ2PHuq14yk2HJLfninxZFbrLBCXZaMz5KZEc06W+/3JzXc85zNJ9 WHfFg2/xRDY9vh0v7mD438YOlOxuHEE= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b="F/5EYwAr"; dmarc=none; spf=none (imf08.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.128.181) smtp.mailfrom=josef@toxicpanda.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731434074; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=F1sPenjaPSV3IbCyPSQxlkDnDJhzpgD7KqSaJNnzDjc=; b=VALonfcIWx7BB6f1l0ETy8Y9LKyD1Av40kq0xKtuD72oQ+KK3V5gSB9Fh/ZwDII0MWQX4e Tbqlw0GQLlY2FcBUUY06sKXihmHLxUfnsBdqikExwN2qqt0Xu8VCieZkATPZ/vqu5/dvfx Z22/W7PPUAUCHn184eeZsIpg97cEtYw= Received: by mail-yw1-f181.google.com with SMTP id 00721157ae682-6e9ba45d67fso55430587b3.1 for ; Tue, 12 Nov 2024 09:56:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1731434202; x=1732039002; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=F1sPenjaPSV3IbCyPSQxlkDnDJhzpgD7KqSaJNnzDjc=; b=F/5EYwAroUizapWlfNVuf48ncarfclSlnyeHSvTTtGyLnoaMXHLu06ADUnTeaNp/gu dXKw+BtV7NXOygK7Nv5LvaJDpkZqXBP7EfYn2LY06IzvXcfGcNBzmPaf9jBwgaZMbfdq 8VMLmzYa5+gu0CwhJlY1nKBBPq7qdxTlBH7fHQFLMb/yqHJ6HupZ4S0Jy7DolAwPJSly wzLvCx54TrNiM9C1zqj7zRPmb/uCzS9aZGhsmQr86y6To2yVePW3/eAY5IV9tE2NMVEJ 9h+B3gZAwjUUOeZbC6cCjEtY8sTxpE3cj+M59QkfFFOG2fenhKRp9AggPAh+PytQ+6tS 4IOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731434202; x=1732039002; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=F1sPenjaPSV3IbCyPSQxlkDnDJhzpgD7KqSaJNnzDjc=; b=KowEQ87mAhFnFNypxoLY7c8L5PgHLGMNpf14aC4sxk98y5/12YCKonZVau0dxJMvJ8 bbeBlJznUs4KubYYTgcAqpn5MOstlG/mrPIKfUYMQwQhlk98X4XfCbEVCn3P12MtWIk2 ss5aClcMgQGOdTIv2eT+8xgfJ9LphJsc1e1bUyTvheiOOiHnpj+evbpf6rl9ugLkC4p+ vbG0j+bTfGgmVwLjeiFWp72tkb3Msoh6TOL+aC3cRKi8fUhxPS58i0rNfei8e6v4CF+R a3lEWSiXxYcoeP6dFuxIv03cF/ouviom/zkE4SB1X6SGssdq+YLbuj60Tu+DmX1+1shk P9eQ== X-Forwarded-Encrypted: i=1; AJvYcCUoPabbBLHinl4uLnD3+e9I8UVkyHgqQcv3cAqm9G+1j+0WXm5UZI0b8DVj6X2NJIAL2jpNcKsl4A==@kvack.org X-Gm-Message-State: AOJu0YyFrGLUUrGbRyLxbTYBiWDwsDth99/09UVQ0OPI2uaxYLM+Ua4G 43GtQqz4kitFVJsnol37lXI1I1SlB8xUsgsSQ3GUJ/P4SpnkP9kjpIbyOHCOoqs= X-Google-Smtp-Source: AGHT+IGfl+HQnQ1oPuK9Fv3A9ej7wwMHJ/S0LDz/YIf0NJPMnTA+szi6pHRPPnhRlITEFHjDE4KLTA== X-Received: by 2002:a05:690c:7007:b0:6e5:cb46:4641 with SMTP id 00721157ae682-6eca4660862mr42510667b3.13.1731434202410; Tue, 12 Nov 2024 09:56:42 -0800 (PST) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6eaceb0939asm26349277b3.61.2024.11.12.09.56.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 09:56:41 -0800 (PST) From: Josef Bacik To: kernel-team@fb.com, linux-fsdevel@vger.kernel.org, jack@suse.cz, amir73il@gmail.com, brauner@kernel.org, torvalds@linux-foundation.org, linux-xfs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-mm@kvack.org, linux-ext4@vger.kernel.org Subject: [PATCH v7 09/18] fanotify: introduce FAN_PRE_ACCESS permission event Date: Tue, 12 Nov 2024 12:55:24 -0500 Message-ID: <8de8e335e07502f31011a18ec91583467dff51eb.1731433903.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 5DA5F160023 X-Stat-Signature: n3e9beno13po4kr55x6mo1zg6u9a8tos X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1731434175-271629 X-HE-Meta: U2FsdGVkX18HAJ5vXtN+Xebcalzelk5309R9lNIi7Pwohfgj+PsI5KizZa8mdbyqUItEesszEcuXCFUUkAbH7yVbJmLCl7/KsZG4CYjV05Lj87t02jKs8UdUqMFn7hQCZXFpbEVltR7DXYLmjjQLFIT7qCMnXOboZfsQ716RaPZgMS29BbzMYXnBGf3z1vj+8rEKd7K3FcqsCvsCodpHKzRPfpQ4yv7KKIjeTjCDKQJQbDcFva/BORo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tRS+ERrI1RtuY7fSUY1ph4a8Jx6UEd2M/38rUkRzIgVlp0xRhNit X-Bogosity: Ham, tests=bogofilter, spamicity=0.000169, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Amir Goldstein Similar to FAN_ACCESS_PERM permission event, but it is only allowed with class FAN_CLASS_PRE_CONTENT and only allowed on regular files and dirs. Unlike FAN_ACCESS_PERM, it is safe to write to the file being accessed in the context of the event handler. This pre-content event is meant to be used by hierarchical storage managers that want to fill the content of files on first read access. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.c | 3 ++- fs/notify/fanotify/fanotify_user.c | 22 +++++++++++++++++++--- include/linux/fanotify.h | 14 ++++++++++---- include/uapi/linux/fanotify.h | 2 ++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 2e6ba94ec405..da6c3c1c7edf 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -916,8 +916,9 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask, BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM); BUILD_BUG_ON(FAN_FS_ERROR != FS_ERROR); BUILD_BUG_ON(FAN_RENAME != FS_RENAME); + BUILD_BUG_ON(FAN_PRE_ACCESS != FS_PRE_ACCESS); - BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 21); + BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 22); mask = fanotify_group_event_mask(group, iter_info, &match_mask, mask, data, data_type, dir); diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 9cc4a9ac1515..2ec0cc9c85cf 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1633,11 +1633,23 @@ static int fanotify_events_supported(struct fsnotify_group *group, unsigned int flags) { unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS; + bool is_dir = d_is_dir(path->dentry); /* Strict validation of events in non-dir inode mask with v5.17+ APIs */ bool strict_dir_events = FAN_GROUP_FLAG(group, FAN_REPORT_TARGET_FID) || (mask & FAN_RENAME) || (flags & FAN_MARK_IGNORE); + /* + * Filesystems need to opt-into pre-content evnets (a.k.a HSM) + * and they are only supported on regular files and directories. + */ + if (mask & FANOTIFY_PRE_CONTENT_EVENTS) { + if (!(path->mnt->mnt_sb->s_iflags & SB_I_ALLOW_HSM)) + return -EINVAL; + if (!is_dir && !d_is_reg(path->dentry)) + return -EINVAL; + } + /* * Some filesystems such as 'proc' acquire unusual locks when opening * files. For them fanotify permission events have high chances of @@ -1670,7 +1682,7 @@ static int fanotify_events_supported(struct fsnotify_group *group, * but because we always allowed it, error only when using new APIs. */ if (strict_dir_events && mark_type == FAN_MARK_INODE && - !d_is_dir(path->dentry) && (mask & FANOTIFY_DIRONLY_EVENT_BITS)) + !is_dir && (mask & FANOTIFY_DIRONLY_EVENT_BITS)) return -ENOTDIR; return 0; @@ -1771,10 +1783,14 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, return -EPERM; /* - * Permission events require minimum priority FAN_CLASS_CONTENT. + * Permission events are not allowed for FAN_CLASS_NOTIF. + * Pre-content permission events are not allowed for FAN_CLASS_CONTENT. */ if (mask & FANOTIFY_PERM_EVENTS && - group->priority < FSNOTIFY_PRIO_CONTENT) + group->priority == FSNOTIFY_PRIO_NORMAL) + return -EINVAL; + else if (mask & FANOTIFY_PRE_CONTENT_EVENTS && + group->priority == FSNOTIFY_PRIO_CONTENT) return -EINVAL; if (mask & FAN_FS_ERROR && diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h index 89ff45bd6f01..c747af064d2c 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -89,6 +89,16 @@ #define FANOTIFY_DIRENT_EVENTS (FAN_MOVE | FAN_CREATE | FAN_DELETE | \ FAN_RENAME) +/* Content events can be used to inspect file content */ +#define FANOTIFY_CONTENT_PERM_EVENTS (FAN_OPEN_PERM | FAN_OPEN_EXEC_PERM | \ + FAN_ACCESS_PERM) +/* Pre-content events can be used to fill file content */ +#define FANOTIFY_PRE_CONTENT_EVENTS (FAN_PRE_ACCESS) + +/* Events that require a permission response from user */ +#define FANOTIFY_PERM_EVENTS (FANOTIFY_CONTENT_PERM_EVENTS | \ + FANOTIFY_PRE_CONTENT_EVENTS) + /* Events that can be reported with event->fd */ #define FANOTIFY_FD_EVENTS (FANOTIFY_PATH_EVENTS | FANOTIFY_PERM_EVENTS) @@ -104,10 +114,6 @@ FANOTIFY_INODE_EVENTS | \ FANOTIFY_ERROR_EVENTS) -/* Events that require a permission response from user */ -#define FANOTIFY_PERM_EVENTS (FAN_OPEN_PERM | FAN_ACCESS_PERM | \ - FAN_OPEN_EXEC_PERM) - /* Extra flags that may be reported with event or control handling of events */ #define FANOTIFY_EVENT_FLAGS (FAN_EVENT_ON_CHILD | FAN_ONDIR) diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h index 79072b6894f2..7596168c80eb 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -27,6 +27,8 @@ #define FAN_OPEN_EXEC_PERM 0x00040000 /* File open/exec in perm check */ /* #define FAN_DIR_MODIFY 0x00080000 */ /* Deprecated (reserved) */ +#define FAN_PRE_ACCESS 0x00100000 /* Pre-content access hook */ + #define FAN_EVENT_ON_CHILD 0x08000000 /* Interested in child events */ #define FAN_RENAME 0x10000000 /* File was renamed */