From patchwork Wed Sep 4 20:27: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: 13791363 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 C53CACD4F24 for ; Wed, 4 Sep 2024 20:29:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2F69D6B0130; Wed, 4 Sep 2024 16:29:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 27FF26B0131; Wed, 4 Sep 2024 16:29:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 082876B0132; Wed, 4 Sep 2024 16:29:32 -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 D21F46B0130 for ; Wed, 4 Sep 2024 16:29:32 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 861218123C for ; Wed, 4 Sep 2024 20:29:32 +0000 (UTC) X-FDA: 82528196184.15.DC2B301 Received: from mail-oa1-f45.google.com (mail-oa1-f45.google.com [209.85.160.45]) by imf01.hostedemail.com (Postfix) with ESMTP id B165040008 for ; Wed, 4 Sep 2024 20:29:30 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=IchPAQsS; spf=none (imf01.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.160.45) 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=1725481675; 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=9jRcB1XulXFmj7DKhtdmn8n+Hih5C6C9O3bk81xEiZw=; b=QkJuDfBhq9nHeu5/ZBf1BbbiC1PjAItRd8JlLRSmdR0UYKCreHkCt1+rEMHHU3Yx5KO9XC jEtpRpejsA7T/ypfe2jYHfU4jLvAiVgFpbxRkuCOCYLap6Q/I+a68kCWHFdFUzXYo/5yFb VH6o3TCH/AJKqhsaHCGj7wP//V4zIz0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725481675; a=rsa-sha256; cv=none; b=s4rvdot1k759xsjgr96Lj5cPIvgLPWLoXUyelfOxJ95YVYNW9EtcQzSY8R4JB5XOpkdruY wQSW/ravlTgntGOXkPXXNBCqWQpIzTztKDE5IOeeLgHxnCbf9I/xuRpv0cnVm1zNN1pKUA +AXihpC5He2bgmsfkBT5TOR35RyT2aQ= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=IchPAQsS; spf=none (imf01.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.160.45) smtp.mailfrom=josef@toxicpanda.com; dmarc=none Received: by mail-oa1-f45.google.com with SMTP id 586e51a60fabf-2781cb281abso52578fac.1 for ; Wed, 04 Sep 2024 13:29:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481769; x=1726086569; 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=9jRcB1XulXFmj7DKhtdmn8n+Hih5C6C9O3bk81xEiZw=; b=IchPAQsSUawfsZAduoRck7x/bwpbtJObc1Wd+8PNbnPJQbrBsQX23v0S3n60bmLPAk KThRg0q2SeKDso1S6y4yRG54RYHb+MV9yo+cluAQTrGIab1c09KeHe1WkL/8hTxle8CC uUFVOqQOGpen4DyedMhCbEAKVQsqo9FNp/G3Z1bL48+c0YM56RBajLMhc6QQknyW2Hyx jcrCxb1TzfNtcUBEhO/2ot0BnCen4EN45id4Wwsi5ZFQyLYQB8z/GfHeLF6RWZ4kJTbR 3+aK1oMjCoFL5FJLPCSxk42r9cH6viPdxXgEp8R3Xl/rVhx38yKNmJfuzW6IdPtyA8xJ nufA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481769; x=1726086569; 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=9jRcB1XulXFmj7DKhtdmn8n+Hih5C6C9O3bk81xEiZw=; b=g3rH9ofDcMSzQNCYll5sOrslHZAMdCtLha/1Fv9Og5tqcMcP8TpPLWanweu6CtFw7Q i1DWmgKw1q4u9lb9u+Wq1Px5zAbJNZigpCJw4P8FULvZaiBNHShryw9b3MyZMYBPHjho FTufXAgavZz9KS0n4JwkADxiP1VLhfmd/XNYhDeOFzZZ2MtDGZ52tuCEy5zoAeBH/pTH 2fHx2/D4viCEemTxxX608N8e9/rPjjh8T4puI+EtvSBBoYMHgl4Cl40RwtVD3P0GQDBr VUEMwQdeQaxVGwrVEqx3o8pZ4iW2BFJRgrWr0CCyDhtXKBsk7Hj7dKROQ/4pZS9zytFR VViA== X-Forwarded-Encrypted: i=1; AJvYcCWbrBDbu259JCpCfzAqZdQrIgnCLD/OLiUT7Yr1qaoAuD+fpSsR31WRmDjwedVoejcBA8bzIrT47A==@kvack.org X-Gm-Message-State: AOJu0YwaG+rf+NibONGFZnbf+YAVjkYLs2sY7qxyGPKhEvrAuOaiASb+ Xo8FOHnkeBWKq1he/SkTWUFkWg72pk7LLsTzpu2fh0DsnNNhs+BfYCYaRsr67rg= X-Google-Smtp-Source: AGHT+IFWlV+ptqOFoXLlPgR/wzgAif5CenGcM7a3jH83CCzPXpqmv1KE+P96IBtwqQpEGOVQpckR6A== X-Received: by 2002:a05:6870:a1a1:b0:270:5f17:b34 with SMTP id 586e51a60fabf-277900b31a5mr23054551fac.11.1725481769655; Wed, 04 Sep 2024 13:29:29 -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-7a98efed964sm15316985a.73.2024.09.04.13.29.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:29 -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 09/18] fanotify: allow to set errno in FAN_DENY permission response Date: Wed, 4 Sep 2024 16:27:59 -0400 Message-ID: <8c5ace4661500573fdc078b5e595b9397c13b859.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: B165040008 X-Stat-Signature: f9iaerctm51o1hyqoyofyc46cyrtym1e X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1725481770-415167 X-HE-Meta: U2FsdGVkX1/GQ7kQo5zfBcQSPSpb9DOd1Tv626LnvwJPCnF3P1xW1Crdx7s9y/pNcLRmDeh2NGF8VLlE82sstl01Vd0GnU1d3Mq1aFninQcpT8eKL0tg49TWlUeKMeB1ijIHHYfhG4HTm1I0yDVqQpK3Pdo7VuZgSDjCFiFpsx239LC/P8QsulOUWH5c8fuQdLEhgX/dd1NzrMpueN2KCRN8ic4E2aPbIPQX9qW6fFqwb30Nb6/ZgveA99zRifu4nHI98JKx/I0lDfRqDkI4+Y2Zr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O81jRywsTKt6WC X-Bogosity: Ham, tests=bogofilter, spamicity=0.000057, 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 4e8dce39fa8f..2969f45a08dc 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 ed56fe6f5ec7..53eee8af34a0 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -337,11 +337,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 @@ -350,18 +353,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 ae6cb2688d52..547514542669 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 6136e8a9f9f3..34b5d634de1f 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -234,6 +234,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 */