From patchwork Wed Sep 4 20:27:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791356 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 3089CCD4F26 for ; Wed, 4 Sep 2024 20:29:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 25B476B0348; Wed, 4 Sep 2024 16:29:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1BD156B0349; Wed, 4 Sep 2024 16:29:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F00846B034A; Wed, 4 Sep 2024 16:29:20 -0400 (EDT) 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 C82406B0348 for ; Wed, 4 Sep 2024 16:29:20 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 68BA4120544 for ; Wed, 4 Sep 2024 20:29:20 +0000 (UTC) X-FDA: 82528195680.28.0D1D8E5 Received: from mail-vs1-f49.google.com (mail-vs1-f49.google.com [209.85.217.49]) by imf17.hostedemail.com (Postfix) with ESMTP id 9C7174000C for ; Wed, 4 Sep 2024 20:29:18 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=QodfRb0x; spf=none (imf17.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.217.49) 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=1725481710; 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=G5YjJnCjztcen/Z+X5lHX0n7xhNQ/QripYlvqYqvblA=; b=WrL4DtgZGCtydO+5mLdOgqMELTMDocyux1iefAeNUo9Lpy4BRzb7S2aOUhS6VcT33lW1XP eETo2bY+ZZwpwoTfu9zFlD5Zc+BE+neHhZAw4zm6O+FOS5B7eh/PhkvSQgZ1UFGIgb0cCi e63XpS41KE7y7b+76f+sKsetv0XMRYI= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=QodfRb0x; spf=none (imf17.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.217.49) smtp.mailfrom=josef@toxicpanda.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725481710; a=rsa-sha256; cv=none; b=S+DtOCbA2BqtVwgaQUqCLrSzP57OTh0Z0weeDMIPXSuBCuL0+gj6Uj4IfpJmnehkyWRct0 7WJe/QEE1DoBjDiqiI//uegKviVZtYx6saF0t65tF/WEoTZk4LShVOuNBFlftqUuYFCLAh HVie6n1nLhLszhW9vfWvKTgP0Y7hios= Received: by mail-vs1-f49.google.com with SMTP id ada2fe7eead31-49bc42bec6dso289366137.0 for ; Wed, 04 Sep 2024 13:29:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481757; x=1726086557; 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=G5YjJnCjztcen/Z+X5lHX0n7xhNQ/QripYlvqYqvblA=; b=QodfRb0xFqcjOtP2Af2J7YOtdiorXV9de0WmL0jz3GOfx2cvwspq8+1xl8Ds21BeT+ mGr3EhH6sWl4Pq4206pG1RUOj8Av+N1YdqoXaPYU02Jo1JSLO1iyBAF+1x7u2t9zRASQ 6Gwx3kgNHPnnuIdy46g22SGgtZxQwQhEq1W4P/5azlrKQXonaUTtzHsGRpHAzAVDpgLh 2j4TdjydGWWcaoT6YlKYhDpyefeVk4F4J1EpuELYO7gc3TaWIqkE4fmzHorB5e+PUD6Q P1LtZo4MdWN2QRQ017BpV+I1stBthF+EbKHNes6g778HzuHcxfZK4EPDO42hX8c0R5bj rLXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481757; x=1726086557; 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=G5YjJnCjztcen/Z+X5lHX0n7xhNQ/QripYlvqYqvblA=; b=bcLBFWdEOAird3skwuQcAStVbrJ/txi6wn2dezjhgMo33Hjoz3f9lVgpvmsObdtZR8 phoM+6q9wrWM3BAVMd7x315IZEgB8ubTFYMFHlIMa3/UlODcG2VtHYPKE5GTZLM47dnI 3KTBdPnwD5bm5gOqwRJprh66Ro5afj5GfI6RU/v7BPjro0KVUoQHSBOMqz7pyXXMrZ+3 Fv7fXP844F21dlotg2tUFozFDSOtSVOkiT3y/uCAnmCboubFyQ1Yd2AueEfkSYgN31Y1 wUpGRNn419XAPZDWZCG6tdkkSA8qochF7bcuLsgSL7DynjPRh9jyRYxl+vcmu7OG2sxR bV6Q== X-Forwarded-Encrypted: i=1; AJvYcCX0LcRWfwfCpSMDIMLaxIktaCucyvucNs9NUiu13pnHFZuHiB81Kjrz0Sbr/qQMnYXz1Ap6o6XS0w==@kvack.org X-Gm-Message-State: AOJu0YywUwiNFQ0eYQM3Da0A8qdQHYXjWbhzl75nEco48134pfxzsaxb oGhdATbKPgs8CooVnWUusb49gM4WIB4cYlUv8poE1A9s/uvbdE/AMoOfgyqWolY= X-Google-Smtp-Source: AGHT+IHvcict8JxrlQWrQU5plI6x5zmPl5v4oP6wc1pvF95llWkCs8Ce7IJ9EEARO1WLH0fb9o1Srw== X-Received: by 2002:a05:6102:c91:b0:498:d39d:9b7c with SMTP id ada2fe7eead31-49a77940af7mr14221150137.2.1725481757506; Wed, 04 Sep 2024 13:29:17 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a98f00acf7sm15095385a.107.2024.09.04.13.29.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:16 -0700 (PDT) From: Josef Bacik To: kernel-team@fb.com, linux-fsdevel@vger.kernel.org, jack@suse.cz, amir73il@gmail.com, brauner@kernel.org, linux-xfs@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v5 02/18] fsnotify: introduce pre-content permission event Date: Wed, 4 Sep 2024 16:27:52 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 9C7174000C X-Stat-Signature: nhremadne6dqfsm641c6htxs68mrqki1 X-HE-Tag: 1725481758-738882 X-HE-Meta: U2FsdGVkX18G8RLa7Mj9g2h+bJXVw8Gv7vHH2G0id2ZKVdmQ7i86gd8F0d72WxXSzp3tSlXyrSaHEJ0yV7Aqvnzud5k6suO1pGPqpCa6rIUZSuYaGdepY/9D7CK8f/iqlR9SH1PgsGUAX6eA/PDNnGhqlIG9RcB+G5qEX+yVGnJ2GE7BNh8pFNs6iQSUgEcooKnOG2wNM6lRRPWGrB+UwmtkjUH5/AGI6IjZ871RNvcttkJObjALvUuTeXGIXeEdG6GrcBzjZ9Gg6+oGG9Hazp3mpUQqWgscRWi9IcIvfPE7tk6YndzO+UndTKd+Gn69nUhnG9fT5Z/Atro9NYFsR4WABg25u3lHYWPY3A4mNM/IrqJB61CuoUbrxJPtk8VTyJpa3clbek7SZKs+P6CGX2SQcPpofswIBHil+IGSuHgtevKDTAAuDd7k5Hd8nHmh8NrrECMXjoy8/iV4XJNDTcQdrKk10km6eCu2zPpr/mR4QuIdxmyX9ArgMa0eRfXsg8v6M0Uv4kOto7CQb6+zMhRjjFTZGoR94QKlAP/GaQpmWFkJ+NyoiEmLHJkNC2Pln+i8HwbkBHpomJ9jHfyFOKR3ymVEkLEayex0J0mi+WCTInIroQw72MEKxSw2FHBLLZI5S3mxLu/1ArVueTkiArqWWsgyNKY9w47LO5ue5E+sMEiH1iLu8UBitkBusBEFYv5tqZuF/tSsMNO1Q80raEbLwuHukx707SPQyJe0KS3h8T1KCkY8v21ZXDDMRxlR6KRaT6nF+f5u/0Oz9ZvNDYfe+8KodESxH3hQ+89+55+OQnU4JKHXXR/9E/gNVjc/e0ER+80vlYwbzdDOLsHG5jJ6KEkJq9RCq97/eFu1kIZYokUOXWWDo6ix5ezZj16lkW62q0iaihGzrCaWvceqVB9STixjU81PXH3yq2X/yGX2V6bvmKIyyUdkLWD/0PateP1Bbi3lxdWTwJrOWqb uPmpuA6D 8GObm4AszzW2GZV9x/vNsXDo0jhcXwQHAmfjEllR73YfN1aJY+brAN9ZhExhcP6di8TfBXKlp2yrPXbPado2OxgFMHo6MTfnpmc/lNrfR2iJDDQOd4z41ZoH3pDkkYHvpvGKy4LIah50rbECAd4yUsYrmjjkpoRCznA3+TnnGCkdYgApOFfIpSHGj2QYhvHpv/HcTUHM/j5jG9gsoDk5C3x7rVrt+jJMvqa6f6rtjsAz3UHiN1YTrNpnGjPzNXCOXkvFy8mRerbiHTxBIIg0Xsyj2+qoefjgx95nC09p9oPZmfVtzz54T8krWNg== 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, same as we did for FS_OPEN_PERM/FS_OPEN_EXEC_PERM. FS_PRE_MODIFY is a new permission event, with similar semantics as FS_PRE_ACCESS, which is called before a file is modified. 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. Signed-off-by: Amir Goldstein --- fs/notify/fsnotify.c | 2 +- include/linux/fsnotify.h | 27 ++++++++++++++++++++++++--- include/linux/fsnotify_backend.h | 13 +++++++++++-- security/selinux/hooks.c | 3 ++- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 272c8a1dab3c..1ca4a8da7f29 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -621,7 +621,7 @@ static __init int fsnotify_init(void) { int ret; - BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 23); + BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 25); ret = init_srcu_struct(&fsnotify_mark_srcu); if (ret) diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h index 278620e063ab..7600a0c045ba 100644 --- a/include/linux/fsnotify.h +++ b/include/linux/fsnotify.h @@ -133,12 +133,13 @@ static inline int fsnotify_file(struct file *file, __u32 mask) #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS /* - * fsnotify_file_area_perm - permission hook before access to file range + * fsnotify_file_area_perm - permission hook before access/modify 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; + struct inode *inode = file_inode(file); + __u32 fsnotify_mask; /* * filesystem may be modified in the context of permission events @@ -147,7 +148,27 @@ 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)) + /* + * Generate FS_PRE_ACCESS/FS_ACCESS_PERM as two seperate events. + */ + if (perm_mask & MAY_READ) { + int ret = fsnotify_file(file, FS_ACCESS_PERM); + + if (ret) + return ret; + } + + /* + * Pre-content events are only reported for regular files and dirs. + */ + if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)) + return 0; + + if (perm_mask & MAY_WRITE) + fsnotify_mask = FS_PRE_MODIFY; + else if (perm_mask & (MAY_READ | MAY_ACCESS)) + fsnotify_mask = FS_PRE_ACCESS; + else return 0; return fsnotify_file(file, fsnotify_mask); diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 8be029bc50b1..200a5e3b1cd4 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -56,6 +56,9 @@ #define FS_ACCESS_PERM 0x00020000 /* access event in a permissions hook */ #define FS_OPEN_EXEC_PERM 0x00040000 /* open/exec event in a permission hook */ +#define FS_PRE_ACCESS 0x00080000 /* Pre-content access hook */ +#define FS_PRE_MODIFY 0x00100000 /* Pre-content modify hook */ + /* * Set on inode mark that cares about things that happen to its children. * Always set for dnotify and inotify. @@ -77,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 | FS_PRE_MODIFY) + +#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 55c78c318ccd..2997edf3e7cd 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -3406,7 +3406,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);