From patchwork Mon Nov 11 20:17:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13871228 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 0D6D8D3ABF4 for ; Mon, 11 Nov 2024 20:19:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F20F18D0007; Mon, 11 Nov 2024 15:19:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ED1908D0001; Mon, 11 Nov 2024 15:19:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD4248D0007; Mon, 11 Nov 2024 15:19:28 -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 ADDB58D0001 for ; Mon, 11 Nov 2024 15:19:28 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 73F70A1B18 for ; Mon, 11 Nov 2024 20:19:28 +0000 (UTC) X-FDA: 82774927494.27.31DCA36 Received: from mail-qt1-f173.google.com (mail-qt1-f173.google.com [209.85.160.173]) by imf27.hostedemail.com (Postfix) with ESMTP id D8C574000D for ; Mon, 11 Nov 2024 20:18:44 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=RTnn3Wxx; dmarc=none; spf=none (imf27.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.160.173) smtp.mailfrom=josef@toxicpanda.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731356192; 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=bnm9HoX75xrhKnowV+mgCQOJIE5h2IvV5FM7hUYuNNY=; b=TtrRg9OsKeN0ocN5nxaw50ZPfAAMyxHsc9kFdBRy71gtMeE7SURH9/063f0eDu8UDIFcbx adSiMEtw16wC91RjF2HzpYxn5fkbvglZhnOxzKeDPcql/mBYjMMPBVqHM5lMGlSKoPPq3W /jvcjhgAZE6hv4O0du2a6om4hqfSySg= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=RTnn3Wxx; dmarc=none; spf=none (imf27.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.160.173) smtp.mailfrom=josef@toxicpanda.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731356192; a=rsa-sha256; cv=none; b=yTAfmC0AHVCGieyLtI4WA6l3LlDg5uRMO2qLNEG0bdrovgoYXcK5Dm3/fJVItEa7wNHbf9 gwFpR0wiz8/5FJS8/Zu9faYqlq9RLrMaTQ6iM4nyV7m4Bn5CL4SwhripXyc5YgJo4EQKXx D43vLEaIRvgu5TJ3wAxkILHGMP/9X5g= Received: by mail-qt1-f173.google.com with SMTP id d75a77b69052e-4613162181dso36959301cf.3 for ; Mon, 11 Nov 2024 12:19:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1731356366; x=1731961166; 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=bnm9HoX75xrhKnowV+mgCQOJIE5h2IvV5FM7hUYuNNY=; b=RTnn3WxxIWA9CHkLNV4qBN1no2FGQjQ1njcELr4dqc6dnWS3ZVZ4W29/xhDbPb96eV 6qE2Td4K1zD4Touzz8s5lvap0WRaRAmCwYdbh756yEI6wlEqTdWeHKZ7qrWdEzBBlcPO 70LVI3RKn1qfchkiexZOWVekHxdachCjSwqYDvfk0r8mxFfFxRS2rdtzf75rJVsRBmNd WPCO3gKMI1H7W/cSIh4sa97iZgfSfk+Ih/F6u8Ee9xo2rmwtnL39NiBjrkS8gQoLKwlh oQwGMOPGqR72s537odI56WK79V8ed09B2AtFIpdYxneGFCbqMISom1yqf1XPSSP1oR5f Zgvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731356366; x=1731961166; 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=bnm9HoX75xrhKnowV+mgCQOJIE5h2IvV5FM7hUYuNNY=; b=jEOwMZV6L+PsPa+EPLQgjczFnWIcRe0Qf5OF8sMcUJriuRis1Jvxtoom7wwHo5XX9N W+cLQpZisj59wagOgMzTloj8eZwWKnfQhGxrmeZFoSKOFWs36Fgx1Lprz8xIoCXAnZrl 4Iy6Y/mV3BwFjwIQEMX3IThzqnyn88BOxq+yqUWkaO79MRNCUAoQIi+4sEhhNOA6xOGF szJK9fQjqB6EZmbtGetkEY5f6DEmv7hf3IIAXx+kBNM5wxYwt3pR/04L44SEjImG44WS dX1+ki3UtGaZE9K/UMvc9YTG780ua2q6y65JWWEKR6omHFaH6OwG/dQb7cSZ5HFAudyL u2hQ== X-Forwarded-Encrypted: i=1; AJvYcCUqEunf3Db6xzRlrr74AhoijfiXbnQJHBvkkdoS98wrSfPtj0jOjDtMfb12zA2UxIsR9SeqigcBpw==@kvack.org X-Gm-Message-State: AOJu0Yyfh5QuJ7sIrR/SjSTVo1IZbtoZPrQX6o6asl/qvPyFJEf0fifc /aGNIKpcvbnc+bsBz8V9PhQ51/qePxnkE3wEESa2M1dpMmEKFaHdSaME40NzBxc= X-Google-Smtp-Source: AGHT+IEMnK5byD6bzAeE8nnbH3/s1NiwYi6YVeXR6nrdTlyrutHvVhB1PYsiGKz3eP2xCUnFM1iy+Q== X-Received: by 2002:a05:622a:1a15:b0:460:ac6f:477a with SMTP id d75a77b69052e-46309396323mr199480931cf.33.1731356365794; Mon, 11 Nov 2024 12:19:25 -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-462ff57e935sm66717941cf.57.2024.11.11.12.19.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:19:25 -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 10/17] fanotify: allow to set errno in FAN_DENY permission response Date: Mon, 11 Nov 2024 15:17:59 -0500 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: D8C574000D X-Stat-Signature: snhhi73hes7n18cjqn7dcqeme13yqzbx X-Rspam-User: X-HE-Tag: 1731356324-37651 X-HE-Meta: U2FsdGVkX1/NcQ0I4MGbvR/bGu7ZQca0Mig35uvX73uQQkYosJtZ0w3nZIaq6uv/FLKMS0aQlmpfnBa29mQ8VxoPvX/2Wkb2EkXra8pUMGW6nkTMToieHw7HAj7j+dIul4hGByeuLh98K2J/LWU3SeC9dRcSGB23DBqQYH1dwW/D2s7XOn5lo25hT0yh80eDsVd/pczZTv6iQIgj4YfmPoOchJVi0vyzTi4YAacRmz9KiJxcwq2CFTPTDspK7zzVlR5u0ts+VeZN7veP9VeW3PS/zbkb3ASKgPBhBm2fQ4+9R+W+fgJ2/TUWQGF3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ests=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 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 5e05410ddb9f..17af1b822791 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -224,7 +224,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); @@ -257,20 +258,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 17402f9e8609..ca8cc2103b5d 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -330,11 +330,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 @@ -343,18 +346,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 */