From patchwork Thu Nov 21 11:22:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 13881872 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 B04F7D6ED0A for ; Thu, 21 Nov 2024 11:22:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4C546B007B; Thu, 21 Nov 2024 06:22:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9797B6B0095; Thu, 21 Nov 2024 06:22:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 386346B0099; Thu, 21 Nov 2024 06:22:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 913D06B008C for ; Thu, 21 Nov 2024 06:22:28 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 1FD798023F for ; Thu, 21 Nov 2024 11:22:28 +0000 (UTC) X-FDA: 82809862296.10.6F84038 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf04.hostedemail.com (Postfix) with ESMTP id 5B28840006 for ; Thu, 21 Nov 2024 11:21:19 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf04.hostedemail.com: domain of jack@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=jack@suse.cz ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732187960; a=rsa-sha256; cv=none; b=iiDdRRGVuArdkQHfM5T+AdxnOvpj8WDEsQz4sMXduBj4SRTsjznwazA27IUHfvoAIF/Rht 53Xe1Z1Naps9fdIXp38I1xNTiJrfj7mFCsYetWISkS60KfC6EIP9l0dGdarsMXg0rAlkH8 ufLH2U7ySi7xOtZJ4x7v5nYxv3Ij9zU= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf04.hostedemail.com: domain of jack@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=jack@suse.cz ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732187960; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=f89vRdtOxUtDEfyFcS8zWgAlbsqq7jwKRDduXJOUaag=; b=OHc62Y7ZIWPPrv4uWqRZ9MK1YWqcodc1hPDokKt+hMoN57egf6PZwtUFN846/5a8cDyheW B+G78JE3ZtMlPqRSOrxBsQvQB4LV5g+2zUSVjCf1Rt7HzKVH/UcYMEvZEiaT936c2ZlO+g HUxcx6qNHi1lRChEHy08yqMhc5u4T7o= Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 7AEBB21A01; Thu, 21 Nov 2024 11:22:24 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 6A86313ACE; Thu, 21 Nov 2024 11:22:24 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id ceABGvAXP2cvfwAAD6G6ig (envelope-from ); Thu, 21 Nov 2024 11:22:24 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 00B4DA0907; Thu, 21 Nov 2024 12:22:23 +0100 (CET) From: Jan Kara To: Cc: Amir Goldstein , Josef Bacik , brauner@kernel.org, Linus Torvalds , Al Viro , linux-xfs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-mm@kvack.org, Jan Kara Subject: [PATCH 07/19] fsnotify: introduce pre-content permission events Date: Thu, 21 Nov 2024 12:22:06 +0100 Message-Id: <20241121112218.8249-8-jack@suse.cz> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20241121112218.8249-1-jack@suse.cz> References: <20241121112218.8249-1-jack@suse.cz> MIME-Version: 1.0 X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Action: no action X-Stat-Signature: wucutodeuctj98ypz8ca1yqz5ndeaur9 X-Rspamd-Queue-Id: 5B28840006 X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1732188079-971446 X-HE-Meta: U2FsdGVkX1+MX2XXz7FABK4nldGbBZW2/G7kYBK882L3ZbmNSGYbMiJNXO1jN6MfNsYR+FvZCG4XDri9bLsrRjNF+l57CEDRYRGaAYYX+F7l7rOQdJwxV0D0F+HN8otXNPwkJa6aMSPOet0S37cO8+B7Yy7FE3s1Pe0vWPZvDIl34oVjbIIgVI7s/mGMRgUwojFydFgqrzbWWwAh1hhfwt64QIu0qL1tDaANmG/0HJv33DvRxJKlk6TPA7b5HPXWhORdJCO4ksCKvLXi33MKH8tedvbr1AurBFISV4PGB+tRxluZkR7s2YuD05KgBYw0Be7Q+YxWrcC5fCJAuF4X2PtlrlyEw6JYrXQWPJd85KveODhViBLLd9ZDH5ZEyldwDEODPf/2cjvyfudH7YM5GVDMHjxSdCceCmmUqBSR+XngSKvjM0I8Fb/1MeGq01QljO/PRXeXoRrJEHrQe17ECGbgFy3OvQbmHc4xTO98uuFAn1h98P+r5nNHzZyRUVQQgdPflh8xcJp+KKsOcnBgAXp3OOOnFjeXfcXRP59lCxR0IaY+Ho3QCNg5jf7lqQdCobGNEv1Mma44djiaDOd4L9ACu06mRKQ8A5CIWZ2D+KCcofla/ylXLB6VkxeDeIW/JqtXOrGGXQE0+qVwESQgujhlHaTkH+DnYbXOwW3u8SYPtZnghhSPqU04lB88vCN1OqSBKvmRoEo2lukWTTOfiH1Srl/tAuyn3tRY494QYx58gJ+JebgDWCgcT0vicENs6pzRW2JAggmJV8BirUDpnlGwKN5Eos+6g9rHQhMDa/QOAiXpturIja2dHT8nTjNqMCuCdc9JxOzg00OGVdjkquEja6s2L01/x2DV8/+he/UUjbG2a5+hdtaaYvJE2sgUuHJICJczxTxjQNs3/lQW/bgg2W+Y5j++aSbHitSlQUsV0s8Ah/2XetfQTNclOWK3eVFmgnEiLgfEhw6ocUY nXq+ytN2 Qd0uk5u4rnuqYLplEe5L+iuoGc8Vaxv2wW+sAB2QeybCzHuksOhfofTutHQS6UKPUNtGx0TsLlp2WE+l7EBlNkVR7Y3tAgb5UHSSlpyPGjucPto2U9BSV9yaVDFLUPgsdWqca6J2faRQtF2S5feJnfhypGPVXb3x7IbSkVAqA04dIj7pgCRn/9HhE3ZbemCIIm1+3kkXpK/NcMqNlwdcVravbKTN1r+4NJ0PI5NzZ6ZC/qOKpzS8hbDNJy9b2iVq11cJMdduDdueWz9Q= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, 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 The new FS_PRE_ACCESS permission event is similar to FS_ACCESS_PERM, but it meant for a different use case of filling file content before access to a file range, so it has slightly different semantics. Generate FS_PRE_ACCESS/FS_ACCESS_PERM as two seperate events, so content scanners could inspect the content filled by pre-content event handler. Unlike FS_ACCESS_PERM, FS_PRE_ACCESS is also called before a file is modified by syscalls as write() and fallocate(). FS_ACCESS_PERM is reported also on blockdev and pipes, but the new pre-content events are only reported for regular files and dirs. The pre-content events are meant to be used by hierarchical storage managers that want to fill the content of files on first access. There are some specific requirements from filesystems that could be used with pre-content events, so add a flag for fs to opt-in for pre-content events explicitly before they can be used. Signed-off-by: Amir Goldstein Signed-off-by: Jan Kara Link: https://patch.msgid.link/b934c5e3af205abc4e0e4709f6486815937ddfdf.1731684329.git.josef@toxicpanda.com --- fs/notify/fsnotify.c | 2 +- include/linux/fs.h | 1 + include/linux/fsnotify.h | 19 ++++++++++++++++++- include/linux/fsnotify_backend.h | 11 ++++++++--- security/selinux/hooks.c | 3 ++- 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index dd1dffd89fd6..d61f6bc679f1 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -688,7 +688,7 @@ static __init int fsnotify_init(void) { int ret; - BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 23); + BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 24); ret = init_srcu_struct(&fsnotify_mark_srcu); if (ret) diff --git a/include/linux/fs.h b/include/linux/fs.h index 6a170c2c5326..d9bccec6cc4d 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1257,6 +1257,7 @@ extern int send_sigurg(struct file *file); #define SB_I_RETIRED 0x00000800 /* superblock shouldn't be reused */ #define SB_I_NOUMASK 0x00001000 /* VFS does not apply umask */ #define SB_I_NOIDMAP 0x00002000 /* No idmapped mounts on this superblock */ +#define SB_I_ALLOW_HSM 0x00004000 /* Allow HSM events on this superblock */ /* Possible states of 'frozen' field */ enum { diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h index 8d1849137a96..d91aa064f0e4 100644 --- a/include/linux/fsnotify.h +++ b/include/linux/fsnotify.h @@ -144,12 +144,29 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, */ lockdep_assert_once(file_write_not_started(file)); - if (!(perm_mask & MAY_READ)) + if (!(perm_mask & (MAY_READ | MAY_WRITE | MAY_ACCESS))) return 0; if (likely(!FMODE_FSNOTIFY_PERM(file->f_mode))) return 0; + /* + * read()/write() and other types of access generate pre-content events. + */ + if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode))) { + int ret = fsnotify_path(&file->f_path, FS_PRE_ACCESS); + + if (ret) + return ret; + } + + if (!(perm_mask & MAY_READ)) + return 0; + + /* + * read() also generates the legacy FS_ACCESS_PERM event, so content + * scanners can inspect the content filled by pre-content event. + */ return fsnotify_path(&file->f_path, FS_ACCESS_PERM); } diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index c38762b62bf1..9bda354b5538 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -57,6 +57,8 @@ #define FS_OPEN_EXEC_PERM 0x00040000 /* open/exec event in a permission hook */ /* #define FS_DIR_MODIFY 0x00080000 */ /* Deprecated (reserved) */ +#define FS_PRE_ACCESS 0x00100000 /* Pre-content access hook */ + /* * Set on inode mark that cares about things that happen to its children. * Always set for dnotify and inotify. @@ -78,11 +80,14 @@ */ #define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME) +/* Content events can be used to inspect file content */ +#define FSNOTIFY_CONTENT_PERM_EVENTS (FS_OPEN_PERM | FS_OPEN_EXEC_PERM | \ + FS_ACCESS_PERM) /* Pre-content events can be used to fill file content */ -#define FSNOTIFY_PRE_CONTENT_EVENTS 0 +#define FSNOTIFY_PRE_CONTENT_EVENTS (FS_PRE_ACCESS) -#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \ - FS_OPEN_EXEC_PERM) +#define ALL_FSNOTIFY_PERM_EVENTS (FSNOTIFY_CONTENT_PERM_EVENTS | \ + FSNOTIFY_PRE_CONTENT_EVENTS) /* * This is a list of all events that may get sent to a parent that is watching diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index fc926d3cac6e..c6f38705c715 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -3404,7 +3404,8 @@ static int selinux_path_notify(const struct path *path, u64 mask, perm |= FILE__WATCH_WITH_PERM; /* watches on read-like events need the file:watch_reads permission */ - if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE)) + if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_PRE_ACCESS | + FS_CLOSE_NOWRITE)) perm |= FILE__WATCH_READS; return path_has_perm(current_cred(), path, perm);