Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02B5019E7D1 for ; Thu, 10 Jul 2025 04:22:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=67.231.145.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752121334; cv=none; b=b6sW8hlh4X81cAifIokXZoC6w55bPZ3rsFuY132wOgYs6osZacpzX4bTs2aiNhm4KvuDpxvEPwV4MHkqKlbstnjJbpUFL0FOzGt/qjo+Cdp9A1BcTeBMUBEWHZ6Kd3CgicyAe7kSdxtIHr6V6sEDaFetvI3uFPOT8i0seMYV39w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752121334; c=relaxed/simple; bh=jAitch/h/pQcm57RxIuj1pWG39Laccn7Aqk9tDih6wc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=EQ/VpLNi/PTK9gQ6n0l85t8/DS4YsUIEwizCF6Foip+hCnLRpfw+P+qM+p9YvEoOkzbZNV+3xWmbUfcLZT6xmn6iqB6HEeXbBWpa9jOr6oi6tvEdwTPyn9GNtBgEfIumCHse0g+YRTX4Bch61/ahAXxsbg0voD6AbPcUOFHktYM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com; spf=pass smtp.mailfrom=meta.com; dkim=pass (2048-bit key) header.d=meta.com header.i=@meta.com header.b=c215Wuwt; arc=none smtp.client-ip=67.231.145.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=meta.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=meta.com header.i=@meta.com header.b="c215Wuwt" Received: from pps.filterd (m0044010.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 56A2q0Cu006287 for ; Wed, 9 Jul 2025 21:22:12 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=meta.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=s2048-2025-q2; bh=PiNnD+zXUq34RWglD9nvaoXqy/rpa7SUOrv/Hd58fx4=; b=c215Wuwto37n ySZ7cBnTQcG8znZuMDywS5g4ivtcEJKibYCtuy2egnoYDAWDli9p4OWXTEGjK7w0 bSmpQmk3sOragSSt08MWLoeCIdKNKDBRk2cqOoAe7lF6TWQ8vz3W49hAwhJFoedz DQH+9Sb5jqKBvWF8UigusAUcz1UEQbhClR8AkIuoWjlg+U41kivbOF1X4E8SA3Zm GQr1LFXoF5Vgz3lWWTV6jkPv6otlBmM6FldODI70DR7+AVX6S8g2S6ylUuMYGEPw AG833E9Wp9gW4KcO9NmX0yBkDY2vrFqG6bW3pFY63UvD9xqCBYiUr7MaCcJcTHU5 OPHVCMmeVQ== Received: from mail.thefacebook.com ([163.114.134.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 47t53jrbag-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 09 Jul 2025 21:22:12 -0700 (PDT) Received: from twshared57752.46.prn1.facebook.com (2620:10d:c085:208::7cb7) by mail.thefacebook.com (2620:10d:c08b:78::c78f) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.1748.24; Thu, 10 Jul 2025 04:22:10 +0000 Received: by devvm18334.vll0.facebook.com (Postfix, from userid 202792) id 1422531E90ACE; Wed, 9 Jul 2025 21:19:11 -0700 (PDT) From: Ibrahim Jirdeh To: CC: , <=amir73il@gmail.com>, , , , , ibrahimjirdeh , Amir Goldstein Subject: [PATCH 2/3] [PATCH v2 2/3] fanotify: allow pre-content events with fid info Date: Wed, 9 Jul 2025 21:18:34 -0700 Message-ID: <20250710041835.3692987-3-ibrahimjirdeh@meta.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250710041835.3692987-1-ibrahimjirdeh@meta.com> References: <20250710041835.3692987-1-ibrahimjirdeh@meta.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-ORIG-GUID: oZ5DAi5UKqzMjUCvR_yCGLLVqYhjvZD5 X-Authority-Analysis: v=2.4 cv=e48GSbp/ c=1 sm=1 tr=0 ts=686f3ff4 cx=c_pps a=CB4LiSf2rd0gKozIdrpkBw==:117 a=CB4LiSf2rd0gKozIdrpkBw==:17 a=Wb1JkmetP80A:10 a=FOH2dFAWAAAA:8 a=pGLkceISAAAA:8 a=F9UTiOOtMm9Vir6t1p8A:9 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNzEwMDAzNCBTYWx0ZWRfX8bNUkNvMynX/ kAqYSiNe0gZ0229KodjW/p/PuMfTPA2SBLjYcPOgmOtNXyH9KoAP/IjfB2up96XEBFKsngkb4ef S9l4iRnp42GxhcNmn/DSQpSO3RbyVYLExoU7P5TQymr2odVtCrx0WF3vZEXLNFXo+dgwEoE5dvQ zxnuhaoKxfUm47tSHrRiJf7pcnIm6wkDnwHAyBl3mG6GDIv2os7DR3DJmPR3pYZ+JzIEElFMF2Z WVNtlx5RuPUXGWYv/P3itIK53jtkB+wh3YSM5LPmeW6Hs5T98Fwzpad7ssM7XfPYDcTuN3Y1Lwr xjML3txfDnTkAO47dIOZzRI9tzpKHd0bvb1VvcP0bJXPjtKpPdG1nbR+FV+o10yTqvFYHKTj15u 34CBCq97JBLSkjE7prylNasJBHm/y5tH7QZan5vyZo9hq27IcJQsd5vFN9N1o4AZWRsMxU/f X-Proofpoint-GUID: oZ5DAi5UKqzMjUCvR_yCGLLVqYhjvZD5 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.1.7,FMLib:17.12.80.40 definitions=2025-07-09_05,2025-07-09_01,2025-03-28_01 From: ibrahimjirdeh Summary: From Amir Goldstein Until now, the high priority classes (FAN_CLASS_*CONTENT), which are required for permission and pre-content events, were not allowed to report events with fid info. This is partly because the event->fd is used as a key for the the permission response and partly because we needed to chose between allocating a permission event of fid event struct. Allow reporting fid info with pre-content class with some restrictions: 1. Only pre-content events are allowed with such groups 2. No event flags are allowed (i.e. FAN_EVENT_ON_CHILD) The flag FAN_EVENT_ON_CHILD is anyway ignored on sb/mount marks and on an non-dir inode mark. On a directory inode mark, FAN_PRE_ACCESS makes no sense without FAN_EVENT_ON_CHILD, so in this case, this flag is implied. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify_user.c | 70 ++++++++++++++++++++++------- include/linux/fsnotify_backend.h | 1 + include/uapi/linux/fanotify.h | 4 +- tools/include/uapi/linux/fanotify.h | 4 +- 4 files changed, 62 insertions(+), 17 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fano= tify_user.c index b192ee068a7a..19d3f2d914fe 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -353,7 +353,7 @@ static int process_access_response(struct fsnotify_gr= oup *group, break; case FAN_DENY: /* Custom errno is supported only for pre-content groups */ - if (errno && group->priority !=3D FSNOTIFY_PRIO_PRE_CONTENT) + if (errno && group->priority < FSNOTIFY_PRIO_PRE_CONTENT) return -EINVAL; =20 /* @@ -1383,6 +1383,16 @@ static int fanotify_group_init_error_pool(struct f= snotify_group *group) sizeof(struct fanotify_error_event)); } =20 +/* Check for forbidden events/flags combinations */ +static bool fanotify_mask_is_valid(__u64 mask) +{ + /* Pre-content events do not support event flags */ + if (mask & FANOTIFY_PRE_CONTENT_EVENTS && mask & FAN_ONDIR) + return false; + + return true; +} + static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_m= ark, __u32 mask, unsigned int fan_flags) { @@ -1411,9 +1421,9 @@ static int fanotify_may_update_existing_mark(struct= fsnotify_mark *fsn_mark, fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY) return -EEXIST; =20 - /* For now pre-content events are not generated for directories */ + /* Check for forbidden event combinations after update */ mask |=3D fsn_mark->mask; - if (mask & FANOTIFY_PRE_CONTENT_EVENTS && mask & FAN_ONDIR) + if (!fanotify_mask_is_valid(mask)) return -EEXIST; =20 return 0; @@ -1564,7 +1574,13 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags= , unsigned int, event_f_flags) return -EINVAL; } =20 - if (fid_mode && class !=3D FAN_CLASS_NOTIF) + /* + * Async events support any fid report mode. + * Permission events do not support any fid report mode. + * Pre-content events support only FAN_REPORT_DFID_NAME_TARGET mode. + */ + if (fid_mode && class !=3D FAN_CLASS_NOTIF && + (class | fid_mode) !=3D FAN_CLASS_PRE_CONTENT_FID) return -EINVAL; =20 /* @@ -1633,7 +1649,12 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags= , unsigned int, event_f_flags) group->priority =3D FSNOTIFY_PRIO_CONTENT; break; case FAN_CLASS_PRE_CONTENT: - group->priority =3D FSNOTIFY_PRIO_PRE_CONTENT; + /* + * FAN_CLASS_PRE_CONTENT_FID is exclusively for pre-content + * events, so it gets a higher priority. + */ + group->priority =3D fid_mode ? FSNOTIFY_PRIO_PRE_CONTENT_FID : + FSNOTIFY_PRIO_PRE_CONTENT; break; default: fd =3D -EINVAL; @@ -1750,6 +1771,9 @@ static int fanotify_events_supported(struct fsnotif= y_group *group, (mask & FAN_RENAME) || (flags & FAN_MARK_IGNORE); =20 + if (!fanotify_mask_is_valid(mask)) + return -EINVAL; + /* * Filesystems need to opt-into pre-content evnets (a.k.a HSM) * and they are only supported on regular files and directories. @@ -1911,13 +1935,27 @@ static int do_fanotify_mark(int fanotify_fd, unsi= gned int flags, __u64 mask, /* * Permission events are not allowed for FAN_CLASS_NOTIF. * Pre-content permission events are not allowed for FAN_CLASS_CONTENT. + * Only pre-content events are allowed for FAN_CLASS_PRE_CONTENT_FID. */ - if (mask & FANOTIFY_PERM_EVENTS && - group->priority =3D=3D FSNOTIFY_PRIO_NORMAL) - return -EINVAL; - else if (mask & FANOTIFY_PRE_CONTENT_EVENTS && - group->priority =3D=3D FSNOTIFY_PRIO_CONTENT) + fid_mode =3D FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); + switch (group->priority) { + case FSNOTIFY_PRIO_NORMAL: + if (mask & FANOTIFY_PERM_EVENTS) + return -EINVAL; + break; + case FSNOTIFY_PRIO_CONTENT: + if (mask & FANOTIFY_PRE_CONTENT_EVENTS) + return -EINVAL; + break; + case FSNOTIFY_PRIO_PRE_CONTENT: + break; + case FSNOTIFY_PRIO_PRE_CONTENT_FID: + if (mask & ~FANOTIFY_PRE_CONTENT_EVENTS) + return -EINVAL; + break; + default: return -EINVAL; + } =20 if (mask & FAN_FS_ERROR && mark_type !=3D FAN_MARK_FILESYSTEM) @@ -1938,7 +1976,6 @@ static int do_fanotify_mark(int fanotify_fd, unsign= ed int flags, __u64 mask, * carry enough information (i.e. path) to be filtered by mount * point. */ - fid_mode =3D FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); if (mask & ~(FANOTIFY_FD_EVENTS|FANOTIFY_MOUNT_EVENTS|FANOTIFY_EVENT_FL= AGS) && (!fid_mode || mark_type =3D=3D FAN_MARK_MOUNT)) return -EINVAL; @@ -1951,10 +1988,6 @@ static int do_fanotify_mark(int fanotify_fd, unsig= ned int flags, __u64 mask, if (mask & FAN_RENAME && !(fid_mode & FAN_REPORT_NAME)) return -EINVAL; =20 - /* Pre-content events are not currently generated for directories. */ - if (mask & FANOTIFY_PRE_CONTENT_EVENTS && mask & FAN_ONDIR) - return -EINVAL; - if (mark_cmd =3D=3D FAN_MARK_FLUSH) { fsnotify_clear_marks_by_group(group, obj_type); return 0; @@ -2041,6 +2074,13 @@ static int do_fanotify_mark(int fanotify_fd, unsig= ned int flags, __u64 mask, if ((fid_mode & FAN_REPORT_DIR_FID) && (flags & FAN_MARK_ADD) && !ignore) mask |=3D FAN_EVENT_ON_CHILD; + } else if (fid_mode && (mask & FANOTIFY_PRE_CONTENT_EVENTS)) { + /* + * Pre-content events on directory inode mask implies that + * we are watching access to children. + */ + mask |=3D FAN_EVENT_ON_CHILD; + umask =3D FAN_EVENT_ON_CHILD; } =20 /* create/update an inode mark */ diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_ba= ckend.h index d4034ddaf392..832d94d783d9 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -201,6 +201,7 @@ enum fsnotify_group_prio { FSNOTIFY_PRIO_NORMAL =3D 0, /* normal notifiers, no permissions */ FSNOTIFY_PRIO_CONTENT, /* fanotify permission events */ FSNOTIFY_PRIO_PRE_CONTENT, /* fanotify pre-content events */ + FSNOTIFY_PRIO_PRE_CONTENT_FID, /* fanotify pre-content events with fid = */ __FSNOTIFY_PRIO_NUM }; =20 diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.= h index e710967c7c26..3201d8d6c51a 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -73,7 +73,9 @@ /* Convenience macro - FAN_REPORT_TARGET_FID requires all other FID flag= s */ #define FAN_REPORT_DFID_NAME_TARGET (FAN_REPORT_DFID_NAME | \ FAN_REPORT_FID | FAN_REPORT_TARGET_FID) - +/* Convenience macro - FAN_CLASS_PRE_CONTENT requires all or no FID flag= s */ +#define FAN_CLASS_PRE_CONTENT_FID (FAN_CLASS_PRE_CONTENT | \ + FAN_REPORT_DFID_NAME_TARGET) /* Deprecated - do not use this in programs and do not add new flags her= e! */ #define FAN_ALL_INIT_FLAGS (FAN_CLOEXEC | FAN_NONBLOCK | \ FAN_ALL_CLASS_BITS | FAN_UNLIMITED_QUEUE |\ diff --git a/tools/include/uapi/linux/fanotify.h b/tools/include/uapi/lin= ux/fanotify.h index e710967c7c26..3201d8d6c51a 100644 --- a/tools/include/uapi/linux/fanotify.h +++ b/tools/include/uapi/linux/fanotify.h @@ -73,7 +73,9 @@ /* Convenience macro - FAN_REPORT_TARGET_FID requires all other FID flag= s */ #define FAN_REPORT_DFID_NAME_TARGET (FAN_REPORT_DFID_NAME | \ FAN_REPORT_FID | FAN_REPORT_TARGET_FID) - +/* Convenience macro - FAN_CLASS_PRE_CONTENT requires all or no FID flag= s */ +#define FAN_CLASS_PRE_CONTENT_FID (FAN_CLASS_PRE_CONTENT | \ + FAN_REPORT_DFID_NAME_TARGET) /* Deprecated - do not use this in programs and do not add new flags her= e! */ #define FAN_ALL_INIT_FLAGS (FAN_CLOEXEC | FAN_NONBLOCK | \ FAN_ALL_CLASS_BITS | FAN_UNLIMITED_QUEUE |\ --=20 2.47.1