From patchwork Tue Nov 12 17:55:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13872736 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 37C11D42BBE for ; Tue, 12 Nov 2024 17:57:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0CADB6B0103; Tue, 12 Nov 2024 12:56:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ED11D6B0105; Tue, 12 Nov 2024 12:56:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CAF436B0106; Tue, 12 Nov 2024 12:56:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id A49C36B0103 for ; Tue, 12 Nov 2024 12:56:48 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 6003214039A for ; Tue, 12 Nov 2024 17:56:48 +0000 (UTC) X-FDA: 82778197278.24.CB48015 Received: from mail-yb1-f171.google.com (mail-yb1-f171.google.com [209.85.219.171]) by imf24.hostedemail.com (Postfix) with ESMTP id 13D4E180002 for ; Tue, 12 Nov 2024 17:56:40 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=M4P7Zam4; dmarc=none; spf=none (imf24.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.219.171) smtp.mailfrom=josef@toxicpanda.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731434144; a=rsa-sha256; cv=none; b=w9l4v5MqbnlRhth/M2hhjaQ2lCxX7MfQ5PSiP3FTGKEbIlWrtwYS94reS7gQ6t5Kv73/O8 KVq/wh3b70facMPajwg2T2MF84Ud4Kv/eKv3eq2xHfUL/1mjT3Tf/eSYx8Vfrd0btHjMnq GmK6TCDFdtouFWDwVSOdy8DghrrcBjY= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=M4P7Zam4; dmarc=none; spf=none (imf24.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.219.171) smtp.mailfrom=josef@toxicpanda.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731434144; 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=wpMEZ8etGpHeGwDSCw0SFADsZn6aen0TJzBYBdUeEVI=; b=zyA6SER8of7vW1G7L1htFvmdw093NbI1cWlvfrfV9V+9CHesONo0GkvGxiB15CoL//UV6I G6w/qu3CNMbvukeNXm1Ok5qZS6Cqmxg2LwlopeVaWM0dJ8nVY+VzrbrbwW/zdW7fHVh1xX 4XvL7XxLhB3+yJvG0xK1K7+IZ61u+NQ= Received: by mail-yb1-f171.google.com with SMTP id 3f1490d57ef6-e2e444e355fso5304541276.1 for ; Tue, 12 Nov 2024 09:56:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1731434205; x=1732039005; 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=wpMEZ8etGpHeGwDSCw0SFADsZn6aen0TJzBYBdUeEVI=; b=M4P7Zam47PQu6DuhoV2EchwEv4dudBwSAXv6coiz1S5msHBnfxPxTTxVbhRxKz6PTO p4+xZ1NFM5ix+FbojDr/U2ysXyBY2HNf2p+go1YPwOuRxIbe4CgRQphWUWy9TftZKdE6 4a0KQzAKfNTMLFDPzdXQzjPzhLcF59iEl56DL/IvfsHy5oPn6rQDNz29N+YSsQx2Vfwk ZRJPANhMP3r7dnK96ZuBNLa98g5x/tD062U5myXffZzk5FK7D7j4mtyKQZrDxOBlcdkD jRKdzx0caaHd+z49Og3QbgsFPcF6cQJbXKnmultn2cJsFagefYtGYwJ1Hnl0sUXN4wLg tFBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731434205; x=1732039005; 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=wpMEZ8etGpHeGwDSCw0SFADsZn6aen0TJzBYBdUeEVI=; b=WvXmxuBaI6Od19sBMJewXEKwi1xJ5jWgGwJU0MatXUsOEsfbGy43gvL8eEQ3nyKimT POU9Ay49LyWOJm/WGePJ7iDawER2LU2/6dUrICrKxLlrejA1rEiv/a99a/rb75iwRbiM HsRt+DXzoUAW/sXFIFgB/QGCqwZIxABi8Ekl0UVeNfnV26azs0Vb/n4ip6Oz6m8zBP++ yReE+KmJREm8aU/ZpNbL5LWYAjUOYAByhnvT/M78BLWN6EGTwMGTRXpgM7NFqqrHF0/i pzY9UiyXvRQ1YybRARVjSHgRR/ydaaxSy1j8gnjt8McoHKIAAufXglLoG4g4M4mvXZir +yRw== X-Forwarded-Encrypted: i=1; AJvYcCUiGJ/GMGxHKi1Im/9CPUUGDAViEeUUivzGGVaCsxjZccTjQJto1gqXFX0UOwgeRnUxn4kbVSP7qQ==@kvack.org X-Gm-Message-State: AOJu0Yz6vodjTALt22A2kMpwtbSjh5qny/i0tpL5EjejMDLLaDkzxA42 Q+E5IENHfWEoF27tUWJGy2hqOvjzCCD+Fke79Cv1VWC48lqrIxiIK+mNrMCkOck= X-Google-Smtp-Source: AGHT+IFx6UWdvrNKDaX/OENaRjq87dwE63td/svMIV9HfNIfAMFBzFtM680J2ZNXHuwnb0+bit+2+g== X-Received: by 2002:a25:6b06:0:b0:e29:27db:a1ac with SMTP id 3f1490d57ef6-e337e17cdb1mr14809403276.17.1731434205572; Tue, 12 Nov 2024 09:56:45 -0800 (PST) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e336ee15578sm2749556276.1.2024.11.12.09.56.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 09:56:44 -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 v7 11/18] fanotify: allow to set errno in FAN_DENY permission response Date: Tue, 12 Nov 2024 12:55:26 -0500 Message-ID: <6f1748cdfa0b2849361223563ac3c8ee417b230c.1731433903.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 13D4E180002 X-Rspamd-Server: rspam11 X-Stat-Signature: 33gihqgqhpirychn75rirqpjzrgpctwn X-HE-Tag: 1731434200-473540 X-HE-Meta: U2FsdGVkX18MOd9NKsXfcx147ttCPTzVx0GjUeEl5BieV1J3HTl1REo9fR3jCx0DwKorqWTtPKFCwVe77TF/2JTna1su4u2w77HJlMsLV9GCWbZXN2jcMGqpVG5EI3CmwOB8nJRg4H9YROlwewKywfzUtXgvSw8NFwDuNT0lPbs6EGY18+R3x7FE847BhCn2bF+gkua+7dFV4+LC3Rb6KtiOdoaLQJZCZVGty4oJ8hsqc1HDRvv0En7j9nVOjAll6odgm/0Ba7RzArk0p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BuZPqMe1Hv3lLgBBJrU5gm2s30otUpwkL6GxDqigHF8NNhz0GzSRZy+Ew30RMBcOMVeMr4U7UyGZI+owPh47QkpkWMh//54QiHymtcvBIj6Fm2uXQcCmGvDHwUhs32BKBiDkfVd5zILr31y3Xmuw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000013, 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 With FAN_DENY response, user trying to perform the filesystem operation gets an error with errno set to EPERM. It is useful for hierarchical storage management (HSM) service to be able to deny access for reasons more diverse than EPERM, for example EAGAIN, if HSM could retry the operation later. Allow fanotify groups with priority FAN_CLASSS_PRE_CONTENT to responsd to permission events with the response value FAN_DENY_ERRNO(errno), instead of FAN_DENY to return a custom error. Limit custom error values to errors expected on read(2)/write(2) and open(2) of regular files. This list could be extended in the future. Userspace can test for legitimate values of FAN_DENY_ERRNO(errno) by writing a response to an fanotify group fd with a value of FAN_NOFD in the fd field of the response. The change in fanotify_response is backward compatible, because errno is written in the high 8 bits of the 32bit response field and old kernels reject respose value with high bits set. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.c | 19 +++++++++++---- fs/notify/fanotify/fanotify.h | 5 ++++ fs/notify/fanotify/fanotify_user.c | 37 ++++++++++++++++++++++++++---- include/linux/fanotify.h | 5 +++- include/uapi/linux/fanotify.h | 7 ++++++ 5 files changed, 62 insertions(+), 11 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index da6c3c1c7edf..e3d04d77caba 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -223,7 +223,8 @@ static int fanotify_get_response(struct fsnotify_group *group, struct fanotify_perm_event *event, struct fsnotify_iter_info *iter_info) { - int ret; + int ret, errno; + u32 decision; pr_debug("%s: group=%p event=%p\n", __func__, group, event); @@ -256,20 +257,28 @@ static int fanotify_get_response(struct fsnotify_group *group, goto out; } + decision = event->response & + (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS); /* userspace responded, convert to something usable */ - switch (event->response & FANOTIFY_RESPONSE_ACCESS) { + switch (decision & FANOTIFY_RESPONSE_ACCESS) { case FAN_ALLOW: ret = 0; break; case FAN_DENY: + /* Check custom errno from pre-content events */ + errno = fanotify_get_response_errno(event->response); + if (errno) { + ret = -errno; + break; + } + fallthrough; default: ret = -EPERM; } /* Check if the response should be audited */ - if (event->response & FAN_AUDIT) - audit_fanotify(event->response & ~FAN_AUDIT, - &event->audit_rule); + if (decision & FAN_AUDIT) + audit_fanotify(decision & ~FAN_AUDIT, &event->audit_rule); pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, group, event, ret); diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h index 7f06355afa1f..9e93aba210c9 100644 --- a/fs/notify/fanotify/fanotify.h +++ b/fs/notify/fanotify/fanotify.h @@ -528,3 +528,8 @@ static inline unsigned int fanotify_mark_user_flags(struct fsnotify_mark *mark) return mflags; } + +static inline u32 fanotify_get_response_errno(int res) +{ + return res >> FAN_ERRNO_SHIFT; +} diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 5ab9ad69915a..b83fefc8aa2d 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -329,11 +329,14 @@ static int process_access_response(struct fsnotify_group *group, struct fanotify_perm_event *event; int fd = response_struct->fd; u32 response = response_struct->response; + u32 decision = response & + (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS); + int errno = fanotify_get_response_errno(response); int ret = info_len; struct fanotify_response_info_audit_rule friar; - pr_debug("%s: group=%p fd=%d response=%u buf=%p size=%zu\n", __func__, - group, fd, response, info, info_len); + pr_debug("%s: group=%p fd=%d response=%x errno=%d buf=%p size=%zu\n", + __func__, group, fd, response, errno, info, info_len); /* * make sure the response is valid, if invalid we do nothing and either * userspace can send a valid response or we will clean it up after the @@ -342,18 +345,42 @@ static int process_access_response(struct fsnotify_group *group, if (response & ~FANOTIFY_RESPONSE_VALID_MASK) return -EINVAL; - switch (response & FANOTIFY_RESPONSE_ACCESS) { + switch (decision & FANOTIFY_RESPONSE_ACCESS) { case FAN_ALLOW: + if (errno) + return -EINVAL; + break; case FAN_DENY: + /* Custom errno is supported only for pre-content groups */ + if (errno && group->priority != FSNOTIFY_PRIO_PRE_CONTENT) + return -EINVAL; + + /* + * Limit errno to values expected on open(2)/read(2)/write(2) + * of regular files. + */ + switch (errno) { + case 0: + case EIO: + case EPERM: + case EBUSY: + case ETXTBSY: + case EAGAIN: + case ENOSPC: + case EDQUOT: + break; + default: + return -EINVAL; + } break; default: return -EINVAL; } - if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT)) + if ((decision & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT)) return -EINVAL; - if (response & FAN_INFO) { + if (decision & FAN_INFO) { ret = process_access_response_info(info, info_len, &friar); if (ret < 0) return ret; diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h index c747af064d2c..d9bb48976b53 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -132,7 +132,10 @@ /* These masks check for invalid bits in permission responses. */ #define FANOTIFY_RESPONSE_ACCESS (FAN_ALLOW | FAN_DENY) #define FANOTIFY_RESPONSE_FLAGS (FAN_AUDIT | FAN_INFO) -#define FANOTIFY_RESPONSE_VALID_MASK (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS) +#define FANOTIFY_RESPONSE_ERRNO (FAN_ERRNO_MASK << FAN_ERRNO_SHIFT) +#define FANOTIFY_RESPONSE_VALID_MASK \ + (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS | \ + FANOTIFY_RESPONSE_ERRNO) /* Do not use these old uapi constants internally */ #undef FAN_ALL_CLASS_BITS diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h index 0636a9c85dd0..bd8167979707 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -235,6 +235,13 @@ struct fanotify_response_info_audit_rule { /* Legit userspace responses to a _PERM event */ #define FAN_ALLOW 0x01 #define FAN_DENY 0x02 +/* errno other than EPERM can specified in upper byte of deny response */ +#define FAN_ERRNO_BITS 8 +#define FAN_ERRNO_SHIFT (32 - FAN_ERRNO_BITS) +#define FAN_ERRNO_MASK ((1 << FAN_ERRNO_BITS) - 1) +#define FAN_DENY_ERRNO(err) \ + (FAN_DENY | ((((__u32)(err)) & FAN_ERRNO_MASK) << FAN_ERRNO_SHIFT)) + #define FAN_AUDIT 0x10 /* Bitmask to create audit record for result */ #define FAN_INFO 0x20 /* Bitmask to indicate additional information */