From nobody Sun Oct 5 09:06:19 2025 Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) (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 180251F561D; Wed, 6 Aug 2025 06:50:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.151 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754463025; cv=none; b=jAqYlfqy3bFXNw4H16oS1H/HpLGzVsj9JwjnqD/zQFACqk3ZpDDpj9ANLwny9XLlYOaGSMfa/b0mSVEDSmZbesP4fwa4wPj6+mNCUuvsk+Q6zgdU4dvvzJFLkprn5lqZqeRDQJSNsyKOMyGyAV+G698AsqlKXUj8sRWhELJ/NNY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754463025; c=relaxed/simple; bh=z68HCvfkXK90e/VOiKW5nRZ0qgXVCLpKrldZh1HS3so=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FjZG87xwWqDUGuRqzhGIEW1qy09rB9Ib8uweRQS4fzw/TRqzD2J+xzEsbA5FR79VKkT5UcRxuy0W2Xjbc3qJTSGHvmYiKszPQwKvAE63RaD4SXeRPPnanKFdyE4zws9BiZJv5utKvnx/EiYUAI96+2rnLslwllCkkQSjIfK9OC0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cyphar.com; spf=pass smtp.mailfrom=cyphar.com; dkim=pass (2048-bit key) header.d=cyphar.com header.i=@cyphar.com header.b=vZHGV4hj; arc=none smtp.client-ip=80.241.56.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cyphar.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cyphar.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cyphar.com header.i=@cyphar.com header.b="vZHGV4hj" Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4bxgsR3YWJz9slH; Wed, 6 Aug 2025 08:50:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cyphar.com; s=MBO0001; t=1754463019; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TE0rtw3lb0X3gYduAmyvXOk6AuwnMINma8AtPhbNONI=; b=vZHGV4hjRwAnWsufU6ayff84TKynMHaFNoyhbBnMX/J+0aBMuXwthHrhKVvDcSG0TM8e0K XGH5WCd0ybJRuPr+pNRdfId5gO6BnthNKCTWkcZGbOdojTOxvxviuI2opwX4g+kJe5wVvM U9zP870HDKVGxflaJbIc81nMHVWHG/zUEOwXuDgxx5WsDmwJc0J76ncvhNto6lPyscdOwp qdO8OA6tSARTOiPaPtqhRUZIpsQdtc0O/CmfKXYtSmC41k4hK9WhKq5iHx2NKamc6KTCw5 0eDPYmy/gwFsDnjL8/4URjGCLNGOfU4Pa5bO4CvQvz7F2XisKeb4Ib0cyfERng== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of cyphar@cyphar.com designates 2001:67c:2050:b231:465::1 as permitted sender) smtp.mailfrom=cyphar@cyphar.com From: Aleksa Sarai Date: Wed, 06 Aug 2025 16:49:54 +1000 Subject: [PATCH v2 1/2] fscontext: do not consume log entries when returning -EMSGSIZE Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250806-fscontext-log-cleanups-v2-1-88e9d34d142f@cyphar.com> References: <20250806-fscontext-log-cleanups-v2-0-88e9d34d142f@cyphar.com> In-Reply-To: <20250806-fscontext-log-cleanups-v2-0-88e9d34d142f@cyphar.com> To: Alexander Viro , Christian Brauner , Jan Kara , David Howells , Shuah Khan Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Aleksa Sarai , stable@vger.kernel.org X-Developer-Signature: v=1; a=openpgp-sha256; l=2838; i=cyphar@cyphar.com; h=from:subject:message-id; bh=z68HCvfkXK90e/VOiKW5nRZ0qgXVCLpKrldZh1HS3so=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMWRM+i3e7L3rDWOXfWxZ0/WGf/4XjmbMrWRvnSyf1/bqZ tn+XxundpSyMIhxMciKKbJs8/MM3TR/8ZXkTyvZYOawMoEMYeDiFICJuG9iZFi/Vcv49uNpL67I njgkXNY61VThxm3Xmx9XPXZxW9FobizMyDDLOnevVsijebs518rMKzmZePBtjcLU874HLd0OXzg YcZkfAA== X-Developer-Key: i=cyphar@cyphar.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 X-Rspamd-Queue-Id: 4bxgsR3YWJz9slH Userspace generally expects APIs that return -EMSGSIZE to allow for them to adjust their buffer size and retry the operation. However, the fscontext log would previously clear the message even in the -EMSGSIZE case. Given that it is very cheap for us to check whether the buffer is too small before we remove the message from the ring buffer, let's just do that instead. While we're at it, refactor some fscontext_read() into a separate helper to make the ring buffer logic a bit easier to read. Fixes: 007ec26cdc9f ("vfs: Implement logging through fs_context") Cc: David Howells Cc: # v5.2+ Signed-off-by: Aleksa Sarai --- fs/fsopen.c | 54 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/fs/fsopen.c b/fs/fsopen.c index 1aaf4cb2afb2..538fdf814fbf 100644 --- a/fs/fsopen.c +++ b/fs/fsopen.c @@ -18,47 +18,53 @@ #include "internal.h" #include "mount.h" =20 +static inline const char *fetch_message_locked(struct fc_log *log, size_t = len, + bool *need_free) +{ + const char *p; + int index; + + if (unlikely(log->head =3D=3D log->tail)) + return ERR_PTR(-ENODATA); + + index =3D log->tail & (ARRAY_SIZE(log->buffer) - 1); + p =3D log->buffer[index]; + if (unlikely(strlen(p) > len)) + return ERR_PTR(-EMSGSIZE); + + log->buffer[index] =3D NULL; + *need_free =3D log->need_free & (1 << index); + log->need_free &=3D ~(1 << index); + log->tail++; + + return p; +} + /* * Allow the user to read back any error, warning or informational message= s. + * Only one message is returned for each read(2) call. */ static ssize_t fscontext_read(struct file *file, char __user *_buf, size_t len, loff_t *pos) { struct fs_context *fc =3D file->private_data; - struct fc_log *log =3D fc->log.log; - unsigned int logsize =3D ARRAY_SIZE(log->buffer); ssize_t ret; - char *p; + const char *p; bool need_free; - int index, n; + int n; =20 ret =3D mutex_lock_interruptible(&fc->uapi_mutex); if (ret < 0) return ret; - - if (log->head =3D=3D log->tail) { - mutex_unlock(&fc->uapi_mutex); - return -ENODATA; - } - - index =3D log->tail & (logsize - 1); - p =3D log->buffer[index]; - need_free =3D log->need_free & (1 << index); - log->buffer[index] =3D NULL; - log->need_free &=3D ~(1 << index); - log->tail++; + p =3D fetch_message_locked(fc->log.log, len, &need_free); mutex_unlock(&fc->uapi_mutex); + if (IS_ERR(p)) + return PTR_ERR(p); =20 - ret =3D -EMSGSIZE; n =3D strlen(p); - if (n > len) - goto err_free; - ret =3D -EFAULT; - if (copy_to_user(_buf, p, n) !=3D 0) - goto err_free; + if (copy_to_user(_buf, p, n)) + n =3D -EFAULT; ret =3D n; - -err_free: if (need_free) kfree(p); return ret; --=20 2.50.1 From nobody Sun Oct 5 09:06:19 2025 Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) (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 ACFB5215F4A; Wed, 6 Aug 2025 06:50:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.152 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754463030; cv=none; b=CmYoEfUSmglJAVvWdEGjgxZ7uhseTrxgumrrG4zOv/V6xS83cXCo7LahchI8CeHHTE0G0jejOA1MBQRCsvKEq/kaU6U+N7dxPHjSGjKb7+JwRYKGDuSYw6mFhkppWKRoxeStc8XOq/WUSkkIIfZ612MVliSxT/jDrksyjohZrD8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754463030; c=relaxed/simple; bh=ywwewS385OKjgO2wdr1q2wlVq4LCbsBdwndMRD40U3c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=di74ZjOm/6iP7AYV9BP87G/5KErMhWyuOb8xYV2GjsACYUJQEJ1bwEwImUAW4VEJMlJuPJbnLxErvyGdIuwkAEQOmaS0gY87oQlafMH0QVzNfXYYv7qvLGNUQPmXhreBnC87aHllmZmrZmVoV9WfxdLAEUMWAr08VanMmPjqrqo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cyphar.com; spf=pass smtp.mailfrom=cyphar.com; dkim=pass (2048-bit key) header.d=cyphar.com header.i=@cyphar.com header.b=1A7Hy5M2; arc=none smtp.client-ip=80.241.56.152 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cyphar.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cyphar.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cyphar.com header.i=@cyphar.com header.b="1A7Hy5M2" Received: from smtp1.mailbox.org (smtp1.mailbox.org [10.196.197.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4bxgsX1cz8z9tgH; Wed, 6 Aug 2025 08:50:24 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cyphar.com; s=MBO0001; t=1754463024; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8fqmLqILN4T5IaIJwYDk6Qayp/CB0VtXSWMEFdEUZF0=; b=1A7Hy5M2P/MKtq2oqlcPowZkUFrKm4bm2d9RqNQVWcAMRy096zGMiwq3C8eowEMV2A8mwu +yUumg4uC1/mPwwNQVu9/FYEruwJETvo0uX4aU4syjkr03Te+0qwSPDXH0n5LmRYTepF1D Xj2+Li2PpTC4huQuQbslYgq7pzOlhgESwaCNk/mI9GDw/P+sJIkjc9Lj9KMTUKu4EQrGJv nxOMJkX3wKiKKra6JQ1tSAJckTCQmqTAFdeXyMqJzeTo+Zrgz91nyxMEr8C7aHc8bNCGa7 szKGqgt/a7pfXeyL33CIJiezJM0ljmC3/FjeooCZ4Re4AporiwW9p+Au/n22tg== From: Aleksa Sarai Date: Wed, 06 Aug 2025 16:49:55 +1000 Subject: [PATCH v2 2/2] selftests/filesystems: add basic fscontext log tests Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250806-fscontext-log-cleanups-v2-2-88e9d34d142f@cyphar.com> References: <20250806-fscontext-log-cleanups-v2-0-88e9d34d142f@cyphar.com> In-Reply-To: <20250806-fscontext-log-cleanups-v2-0-88e9d34d142f@cyphar.com> To: Alexander Viro , Christian Brauner , Jan Kara , David Howells , Shuah Khan Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Aleksa Sarai X-Developer-Signature: v=1; a=openpgp-sha256; l=5412; i=cyphar@cyphar.com; h=from:subject:message-id; bh=ywwewS385OKjgO2wdr1q2wlVq4LCbsBdwndMRD40U3c=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMWRM+i2es9lK29NyHtNmvRrnI+y5u96cbi60XB24fHr62 /W7fy6b0VHKwiDGxSArpsiyzc8zdNP8xVeSP61kg5nDygQyhIGLUwAmUviBkeHd75/l9bPnfgpK +Hb0v8mvtm7p7J/Oq3ged3RHKshkKBow/OE9+q4jIzGpd/GF9TWHa4J2u2kUxT488Ph00dr4mjM Lc7kB X-Developer-Key: i=cyphar@cyphar.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 Signed-off-by: Aleksa Sarai --- tools/testing/selftests/filesystems/.gitignore | 1 + tools/testing/selftests/filesystems/Makefile | 2 +- tools/testing/selftests/filesystems/fclog.c | 135 +++++++++++++++++++++= ++++ 3 files changed, 137 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/filesystems/.gitignore b/tools/testing= /selftests/filesystems/.gitignore index fcbdb1297e24..64ac0dfa46b7 100644 --- a/tools/testing/selftests/filesystems/.gitignore +++ b/tools/testing/selftests/filesystems/.gitignore @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only dnotify_test devpts_pts +fclog file_stressor anon_inode_test kernfs_test diff --git a/tools/testing/selftests/filesystems/Makefile b/tools/testing/s= elftests/filesystems/Makefile index 73d4650af1a5..85427d7f19b9 100644 --- a/tools/testing/selftests/filesystems/Makefile +++ b/tools/testing/selftests/filesystems/Makefile @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 =20 CFLAGS +=3D $(KHDR_INCLUDES) -TEST_GEN_PROGS :=3D devpts_pts file_stressor anon_inode_test kernfs_test +TEST_GEN_PROGS :=3D devpts_pts file_stressor anon_inode_test kernfs_test f= clog TEST_GEN_PROGS_EXTENDED :=3D dnotify_test =20 include ../lib.mk diff --git a/tools/testing/selftests/filesystems/fclog.c b/tools/testing/se= lftests/filesystems/fclog.c new file mode 100644 index 000000000000..b1084432f195 --- /dev/null +++ b/tools/testing/selftests/filesystems/fclog.c @@ -0,0 +1,135 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Author: Aleksa Sarai + * Copyright (C) 2025 SUSE LLC. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest_harness.h" + +#define ASSERT_ERRNO(expected, _t, seen) \ + __EXPECT(expected, #expected, \ + ({__typeof__(seen) _tmp_seen =3D (seen); \ + _tmp_seen >=3D 0 ? _tmp_seen : -errno; }), #seen, _t, 1) + +#define ASSERT_ERRNO_EQ(expected, seen) \ + ASSERT_ERRNO(expected, =3D=3D, seen) + +#define ASSERT_SUCCESS(seen) \ + ASSERT_ERRNO(0, <=3D, seen) + +FIXTURE(ns) +{ + int host_mntns; +}; + +FIXTURE_SETUP(ns) +{ + /* Stash the old mntns. */ + self->host_mntns =3D open("/proc/self/ns/mnt", O_RDONLY|O_CLOEXEC); + ASSERT_SUCCESS(self->host_mntns); + + /* Create a new mount namespace and make it private. */ + ASSERT_SUCCESS(unshare(CLONE_NEWNS)); + ASSERT_SUCCESS(mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL)); +} + +FIXTURE_TEARDOWN(ns) +{ + ASSERT_SUCCESS(setns(self->host_mntns, CLONE_NEWNS)); + ASSERT_SUCCESS(close(self->host_mntns)); +} + +TEST_F(ns, fscontext_log_enodata) +{ + int fsfd =3D fsopen("tmpfs", FSOPEN_CLOEXEC); + ASSERT_SUCCESS(fsfd); + + /* A brand new fscontext has no log entries. */ + char buf[128] =3D {}; + for (int i =3D 0; i < 16; i++) + ASSERT_ERRNO_EQ(-ENODATA, read(fsfd, buf, sizeof(buf))); + + ASSERT_SUCCESS(close(fsfd)); +} + +TEST_F(ns, fscontext_log_errorfc) +{ + int fsfd =3D fsopen("tmpfs", FSOPEN_CLOEXEC); + ASSERT_SUCCESS(fsfd); + + ASSERT_ERRNO_EQ(-EINVAL, fsconfig(fsfd, FSCONFIG_SET_STRING, "invalid-arg= ", "123", 0)); + + char buf[128] =3D {}; + ASSERT_SUCCESS(read(fsfd, buf, sizeof(buf))); + + printf("fsconfig(): %s\n", buf); + ASSERT_EQ(strcmp(buf, "e tmpfs: Unknown parameter 'invalid-arg'\n"), 0); + + /* The message has been consumed. */ + ASSERT_ERRNO_EQ(-ENODATA, read(fsfd, buf, sizeof(buf))); + ASSERT_SUCCESS(close(fsfd)); +} + +TEST_F(ns, fscontext_log_errorfc_after_fsmount) +{ + int fsfd =3D fsopen("tmpfs", FSOPEN_CLOEXEC); + ASSERT_SUCCESS(fsfd); + + ASSERT_ERRNO_EQ(-EINVAL, fsconfig(fsfd, FSCONFIG_SET_STRING, "invalid-arg= ", "123", 0)); + + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)); + int mfd =3D fsmount(fsfd, FSMOUNT_CLOEXEC, MOUNT_ATTR_NOEXEC | MOUNT_ATTR= _NOSUID); + ASSERT_SUCCESS(mfd); + ASSERT_SUCCESS(move_mount(mfd, "", AT_FDCWD, "/tmp", MOVE_MOUNT_F_EMPTY_P= ATH)); + + /* + * The fscontext log should still contain data even after + * FSCONFIG_CMD_CREATE and fsmount(). + */ + char buf[128] =3D {}; + ASSERT_SUCCESS(read(fsfd, buf, sizeof(buf))); + + printf("fsconfig(): %s\n", buf); + ASSERT_EQ(strcmp(buf, "e tmpfs: Unknown parameter 'invalid-arg'\n"), 0); + + /* The message has been consumed. */ + ASSERT_ERRNO_EQ(-ENODATA, read(fsfd, buf, sizeof(buf))); + ASSERT_SUCCESS(close(fsfd)); +} + +TEST_F(ns, fscontext_log_emsgsize) +{ + int fsfd =3D fsopen("tmpfs", FSOPEN_CLOEXEC); + ASSERT_SUCCESS(fsfd); + + ASSERT_ERRNO_EQ(-EINVAL, fsconfig(fsfd, FSCONFIG_SET_STRING, "invalid-arg= ", "123", 0)); + + char buf[128] =3D {}; + /* + * Attempting to read a message with too small a buffer should not + * result in the message getting consumed. + */ + ASSERT_ERRNO_EQ(-EMSGSIZE, read(fsfd, buf, 0)); + ASSERT_ERRNO_EQ(-EMSGSIZE, read(fsfd, buf, 1)); + for (int i =3D 0; i < 16; i++) + ASSERT_ERRNO_EQ(-EMSGSIZE, read(fsfd, buf, 16)); + ASSERT_SUCCESS(read(fsfd, buf, sizeof(buf))); + + printf("fsconfig(): %s\n", buf); + ASSERT_EQ(strcmp(buf, "e tmpfs: Unknown parameter 'invalid-arg'\n"), 0); + + /* The message has been consumed. */ + ASSERT_ERRNO_EQ(-ENODATA, read(fsfd, buf, sizeof(buf))); + ASSERT_SUCCESS(close(fsfd)); +} + +TEST_HARNESS_MAIN --=20 2.50.1