From patchwork Wed Sep 4 20:27:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791358 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 60F13CD4F25 for ; Wed, 4 Sep 2024 20:29:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2CE396B00C4; Wed, 4 Sep 2024 16:29:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 25B636B00E1; Wed, 4 Sep 2024 16:29:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0839D6B00CA; Wed, 4 Sep 2024 16:29:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D3A336B0093 for ; Wed, 4 Sep 2024 16:29:23 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 8C3D414053B for ; Wed, 4 Sep 2024 20:29:23 +0000 (UTC) X-FDA: 82528195806.17.C296E10 Received: from mail-qv1-f50.google.com (mail-qv1-f50.google.com [209.85.219.50]) by imf11.hostedemail.com (Postfix) with ESMTP id C7C0D4001E for ; Wed, 4 Sep 2024 20:29:21 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=CDKQjeSO; spf=none (imf11.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.219.50) 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=1725481685; 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=wY5GQe96iyZHYx9I8f9pL6l7vs2y5u4HNtMRHVuMKgs=; b=8DfLGzLLfGBdG/DKl+hCIpv/lxP7RoyIzcvI2UqeAZ4KedDZLht0DQPbbOXp1HpJg701uf wNFEbAIhtl5y6u9r1jmJGPex/h5iRiRGRlu8H28dRzpHvT2V7N2NZYFnQ/y8IPkYjuwcUi gpQs2nU8CUb/iKewD8OP0mmcajQ0vOo= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=CDKQjeSO; spf=none (imf11.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.219.50) smtp.mailfrom=josef@toxicpanda.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725481685; a=rsa-sha256; cv=none; b=hutjUhnxNhTBgsMQ4G4Uoq07TL6It4lRdJKjrXDwrgt/nVgsjQuc7dDASIQqvzvVGiRwz9 cvJe7mfGF++usecM3r3jy72wY1ADFKcU5WJJsnDCxlQEYWEUWxHJnCs2pTadw1xtsEtepU OB4dRvyTyLz+IKsobnADHp5u60XkBhI= Received: by mail-qv1-f50.google.com with SMTP id 6a1803df08f44-6c159150ff4so37386d6.2 for ; Wed, 04 Sep 2024 13:29:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481761; x=1726086561; 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=wY5GQe96iyZHYx9I8f9pL6l7vs2y5u4HNtMRHVuMKgs=; b=CDKQjeSOtO9g0nJglxJN6L2nkxfNfLyjKe++iWHkvWbnrbMOCeT4GHxWJfLPdkrZMX UlVnXXQZBtbDOWtd7dZJKKPh3WUK8HFQSpzpN+emufTvM4699xQlj8uIEUWLu4X9JeG3 jnZC4xGgKC380Qe4aaT9XWxYmL95NqGMQruf5k9yXHVUOYmXR05HjUqb0drilo0dUewD 0uQhO5IhxE+dqcbgVNpmAmadBYMubrixXyM7RCF9RgwSt8JBYh6NwcHdtESy8P7ilgsh 070NyEQpNq6GFlLx8AKPIdciblDoAsOHWTHJKF8o/NxiTqDa6URI+4Bj3kXAz0iGtiIA ingQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481761; x=1726086561; 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=wY5GQe96iyZHYx9I8f9pL6l7vs2y5u4HNtMRHVuMKgs=; b=D06r9yAS6/P8O0cg74H2zOKnM4dvseOrZKYbokAVWeaERnlx8TUNLfUpSnF03G+rpf ctYdHmruAywrpisDSrRrSMUMPpVunlp+i31wcWqCFoTVpFjNcftWjvJiVLsthJOKknnC Hsv3ucH9l6kaGd7Zk7MEBfIPc4ekMrGg/f3nYx88WeHST1WfkCqURyJS/Yp6JwIbOK0r Bx9rYH7GAauWJU8EnluV4+kmeAUfQ1R8SxnAb87JKFvWxn87oEXmjLumJSgEtHRFDRP5 Qe+VkxLzkt8gOi+q2z/jeqtJuKZUGqKTqyaKJMyBFKbkKpFUziup4eUJmWEtuvq1t1B8 GCCA== X-Forwarded-Encrypted: i=1; AJvYcCWOm3xlr3A5rZTgHp0Uzd4s0dmg86QRw5lZWfTFNJGX5ot5RsXtWIANVOv02n48Yzv3vaT1OOiBCA==@kvack.org X-Gm-Message-State: AOJu0Yyum6ml8Zhn4qjXNaW7294tYp7tv9/QR9ZTGxTivbZcePxNSym9 rQRvyu4tynPJzFsNmsKisIErZw5g5FZDWZGQbbGVwaiOOT0t5bvWYGykM0WVm4I= X-Google-Smtp-Source: AGHT+IHn2YN4Yn9s+Pe3cKQRQlM1ZQppCBWH1jZTlFjC6uGrUZZrr+lLwvttH1GqC8If9lYaSoNH2g== X-Received: by 2002:a05:6214:3a89:b0:6c3:626f:2366 with SMTP id 6a1803df08f44-6c3626f243amr168729666d6.34.1725481760916; Wed, 04 Sep 2024 13:29:20 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c520315369sm1569046d6.96.2024.09.04.13.29.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:20 -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 04/18] fanotify: introduce FAN_PRE_ACCESS permission event Date: Wed, 4 Sep 2024 16:27:54 -0400 Message-ID: <19cb8b3b5b93760bc6c6d47a8cbcd277952d7006.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: j91dnf46xf18hgzb9kmjcehhtnyygbp5 X-Rspam-User: X-Rspamd-Queue-Id: C7C0D4001E X-Rspamd-Server: rspam02 X-HE-Tag: 1725481761-811819 X-HE-Meta: U2FsdGVkX19C9pPZnhHEwsoNbLaSc4YIuHFLPE4bc0c70ZTnmKikyHUhoLa7sjeAaAB2AXpaFmDmzi9SPBsgswmNROEPl/nlbWxrwgOFT8N3XoDe4X3at6s6AcHgdNQeZyGP81Bzin8OeRIVMGBuPrP5BD4kI1RFc4CtLA+gQPxJ32oBObA67bIhKUYGDcfvoJHKDv4uF9zr8jjADLzJA4r3rzGqG9ZAU7HKKKPbp95WqE84Mjmp8UdMaPySF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w/GpsgSPcaSdmOw5YBeDZXJ/pmdF5EuIv0r3cT1ESSFzwurXgLVObngMfuxMxB0Jp/Xj+7yMb3kAGtw5I+z5dTvM/s35gF0B X-Bogosity: Ham, tests=bogofilter, spamicity=0.001814, 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 Reviewed-by: Christian Brauner --- fs/notify/fanotify/fanotify.c | 3 ++- fs/notify/fanotify/fanotify_user.c | 17 ++++++++++++++--- include/linux/fanotify.h | 14 ++++++++++---- include/uapi/linux/fanotify.h | 2 ++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 224bccaab4cc..7dac8e4486df 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -910,8 +910,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 2e2fba8a9d20..c294849e474f 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1628,6 +1628,7 @@ 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) || @@ -1665,9 +1666,15 @@ 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; + /* Pre-content events are only supported on regular files and dirs */ + if (mask & FANOTIFY_PRE_CONTENT_EVENTS) { + if (!is_dir && !d_is_reg(path->dentry)) + return -EINVAL; + } + return 0; } @@ -1769,11 +1776,15 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, goto fput_and_out; /* - * 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. */ ret = -EINVAL; if (mask & FANOTIFY_PERM_EVENTS && - group->priority < FSNOTIFY_PRIO_CONTENT) + group->priority == FSNOTIFY_PRIO_NORMAL) + goto fput_and_out; + else if (mask & FANOTIFY_PRE_CONTENT_EVENTS && + group->priority == FSNOTIFY_PRIO_CONTENT) goto fput_and_out; if (mask & FAN_FS_ERROR && diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h index 4f1c4f603118..5c811baf44d2 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -88,6 +88,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) @@ -103,10 +113,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 a37de58ca571..bcada21a3a2e 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -26,6 +26,8 @@ #define FAN_ACCESS_PERM 0x00020000 /* File accessed in perm check */ #define FAN_OPEN_EXEC_PERM 0x00040000 /* File open/exec in perm check */ +#define FAN_PRE_ACCESS 0x00080000 /* Pre-content access hook */ + #define FAN_EVENT_ON_CHILD 0x08000000 /* Interested in child events */ #define FAN_RENAME 0x10000000 /* File was renamed */