From patchwork Mon Nov 11 20:17:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13871222 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 D70DFD3ABF5 for ; Mon, 11 Nov 2024 20:19:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ADB766B009F; Mon, 11 Nov 2024 15:19:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A63D36B00A0; Mon, 11 Nov 2024 15:19:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B7ED6B00A1; Mon, 11 Nov 2024 15:19:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6BEF36B009F for ; Mon, 11 Nov 2024 15:19:17 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 27AED1C73BF for ; Mon, 11 Nov 2024 20:19:17 +0000 (UTC) X-FDA: 82774927536.22.90BF54B Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) by imf09.hostedemail.com (Postfix) with ESMTP id E4B82140006 for ; Mon, 11 Nov 2024 20:18:46 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=XoEeiUQQ; spf=none (imf09.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.160.178) smtp.mailfrom=josef@toxicpanda.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731356123; 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=vqB9yXQqmSu2fdFt06tpBDtWwHudP1dE+kW/6EzrLBA=; b=gDZ916JGdBxupy3366ObeIyotk8qB3aMn/fYcVF9cfoa7qqkyLzkoEkW13f8mmUP20gkx2 9YIU9WMSOPct0GLiyAUue6lZm1ACdBDHChFdpLkKF93fq2dsrU0xaUskzIFimEzb+a6U5y Z9fDPePxNumfgMiGmazUEugwI6swPy0= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=XoEeiUQQ; spf=none (imf09.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.160.178) smtp.mailfrom=josef@toxicpanda.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731356123; a=rsa-sha256; cv=none; b=gTBycMCgeYasyrTULw2r3h4VsW41PrUvrY7E8AzjwFLKtWfK5K445vyglVcPQVKySYkco7 vCy3HFF7Y8bS1dhPb6QAiUxl+MWbPpF/dDMvbqWkuQojGHibTxwKy1gNshY900k5BoIJ6/ G45KceoHlV4q6MX4i8qUhrdR/r+dDZU= Received: by mail-qt1-f178.google.com with SMTP id d75a77b69052e-46096aadaf0so31077771cf.2 for ; Mon, 11 Nov 2024 12:19:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1731356354; x=1731961154; 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=vqB9yXQqmSu2fdFt06tpBDtWwHudP1dE+kW/6EzrLBA=; b=XoEeiUQQBLmB7+YzPSiiT2JkfwJ2ac/45W3dojggl4ll8hlV/TEk32yKyjSnZQHVrF IJHHif2v9tGtp1Oc2gvsr+ieGSCyd7bLlYajgPX+pyqh2GMBpdoOlll3cCEM2NciEXxM sTsfGheNONpW+kflxX20OelXWJibwx5SklKoAuqhqcdxQntu0lZubt6B6Oc8lj6m8Eyo tO5u9X+94PHftRlNkze4LzHLWgFOKkndGFD7fmG3Ux9OtS1099GebDzRp+12QIAT06hI lzR9Dxs8W916Gzz2J6iOrMepQJnvi/zgCyBMtPliwo8TulFKzYXbIzMr2niytIT1tKSG tlqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731356354; x=1731961154; 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=vqB9yXQqmSu2fdFt06tpBDtWwHudP1dE+kW/6EzrLBA=; b=cT3e99IGezR994nhbe9nEeJwdamw834btIpT6SQtnX0wN9ePxDO6BwEevdUIwxvR5t Cv09otodVK1XPqhnl6YLR3yZDiiBekxq/39+YaCjeOkq7FPGgJgBnxzpKd3ZOOm1kTI+ NpaRiwRIe62nxebgSAmOa6LQCIzIkeLkLEOTdz5a4cySIRNtArHuy0SavWulixcovkHd O2U92QD4ZA0lBIZgFfn0jeCU6DsR2kCtAI0r0ENSQlf2FRbABUW70TGsrRwn2RHFykT+ 4tOGsZSl+rVbuMRhcCR9gkb1ZbBv9MbmevZA3Iiiu+yTIHuSUiKXWhLqsrMYCHAK0OdV osGA== X-Forwarded-Encrypted: i=1; AJvYcCXELMAbOo9D7thf1tAxIEeLT1q/G/YFmDyBL4y9aefeT4YfEE5qQH0zGjsCbghFKjMx5yQpyegKmA==@kvack.org X-Gm-Message-State: AOJu0YzQyKZ102FGZHu75oWzlHYeu4fkXFDbX7RYWHq4fEoKHuOsdAF+ 0WbPHXZaUQVb2qX5m4N4baQlsvX5CjKup073AxqStbXW7eDqBexHIQIJPYJ6oOA= X-Google-Smtp-Source: AGHT+IFyVEZ8nNhuWSqUTWzgqbTv35BuUsnpAhULP/H0sf68MY9l9ZgPUoCV23Vx6WfGGQrB4k4Iag== X-Received: by 2002:ac8:5a4b:0:b0:461:7467:e9f1 with SMTP id d75a77b69052e-4634022f337mr184071cf.26.1731356354292; Mon, 11 Nov 2024 12:19:14 -0800 (PST) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-462ff3df143sm66588861cf.5.2024.11.11.12.19.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:19:13 -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 v6 04/17] fsnotify: introduce pre-content permission events Date: Mon, 11 Nov 2024 15:17:53 -0500 Message-ID: <09566e774319a45108594fe51349c1bb511204f2.1731355931.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: E4B82140006 X-Stat-Signature: wft3xibkpmnjyn3zxxrksjz64rhoruo9 X-Rspam-User: X-HE-Tag: 1731356326-3679 X-HE-Meta: U2FsdGVkX1/pzu7a95wMxARnNmosYWde8XicPPKJLD/ahCcNfWSD5JAxcFkjqTve+V7yV6suIKdlZGitX3qu4E5YBsSj/SP8OdKxxyoGhl1K7ReN1yjoFDI106/1QJH9D75XV3IzC998e8JTxK5MA93bV+UeVRtkpjch56ma/ev0xHPViUEmKmOCAMaVp7r34587IC7omVULleFOn4t9gTcK9+m19XNBsOyliSehysH7YvVBvlfQYlB0KIHe9UaUSvbriSAIpaULTSzfMAdb/WDMYHXX1puOuUGTC3jQPTR5cFUcc/O+lHFATdz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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 --- fs/notify/fsnotify.c | 2 +- include/linux/fs.h | 1 + include/linux/fsnotify.h | 37 ++++++++++++++++++++++++++++---- include/linux/fsnotify_backend.h | 12 +++++++++-- security/selinux/hooks.c | 3 ++- 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 82ae8254c068..0696c1771b2a 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -624,7 +624,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 3559446279c1..1b9f74bda43c 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1230,6 +1230,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 278620e063ab..7c641161b281 100644 --- a/include/linux/fsnotify.h +++ b/include/linux/fsnotify.h @@ -132,14 +132,29 @@ static inline int fsnotify_file(struct file *file, __u32 mask) } #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS +static inline int fsnotify_pre_content(struct file *file) +{ + struct inode *inode = file_inode(file); + + /* + * Pre-content events are only reported for regular files and dirs + * if there are any pre-content event watchers on this sb. + */ + if ((!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)) || + !(inode->i_sb->s_iflags & SB_I_ALLOW_HSM) || + !fsnotify_sb_has_priority_watchers(inode->i_sb, + FSNOTIFY_PRIO_PRE_CONTENT)) + return 0; + + return fsnotify_file(file, FS_PRE_ACCESS); +} + /* - * fsnotify_file_area_perm - permission hook before access to file range + * fsnotify_file_area_perm - permission hook before access of file range */ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, const loff_t *ppos, size_t count) { - __u32 fsnotify_mask = FS_ACCESS_PERM; - /* * filesystem may be modified in the context of permission events * (e.g. by HSM filling a file on access), so sb freeze protection @@ -147,10 +162,24 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, */ lockdep_assert_once(file_write_not_started(file)); + /* + * read()/write and other types of access generate pre-content events. + */ + if (perm_mask & (MAY_READ | MAY_WRITE | MAY_ACCESS)) { + int ret = fsnotify_pre_content(file); + + if (ret) + return ret; + } + if (!(perm_mask & MAY_READ)) return 0; - return fsnotify_file(file, fsnotify_mask); + /* + * read() also generates the legacy FS_ACCESS_PERM event, so content + * scanners can inspect the content filled by pre-content event. + */ + return fsnotify_file(file, FS_ACCESS_PERM); } /* diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 53d5d0e02943..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,8 +80,14 @@ */ #define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME) -#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \ - FS_OPEN_EXEC_PERM) +/* 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 (FS_PRE_ACCESS) + +#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);