From nobody Sun Oct 5 09:10:54 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 2E45E63CF; Wed, 6 Aug 2025 05:31:28 +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=1754458290; cv=none; b=arhRlEcsoFm3H9AEvk5y/7S1bQRvM0qpUNI+b3YAfYMlTttgLrHfcxbmfLbdrS/L2H4PABJiKOcUW/ulbpdY7KMXTlZ9r7ukjonTDovmRnHOCOzCagFhJVCaUxLE5dokVGJmuVUBBIP3EX3Jvz8Od+LyaRPWRZQdmim24R3l2gE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754458290; c=relaxed/simple; bh=grK8qXho5eJRNhUBpVWJrruv3CCOc+X2tolA7nBflUg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lPLpw/eLdpX2qFfRwAZdjWw/7IWNK51jt/gAoGEL5+xFME/ObtISaQ4CmP0Vbd2NTND/r3rgvA/c9XeOIxwz8bpkG+I+TbCTCql4uljT7VtfY52ayV+sRG3/CvGCXSaofyhMyykPCjF/RQLIPpEJxYVHGllG7tg6Mv15JrWb0Jk= 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=ONxbjG7l; 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="ONxbjG7l" Received: from smtp102.mailbox.org (smtp102.mailbox.org [10.196.197.102]) (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 4bxf6P2gJfz9tlP; Wed, 6 Aug 2025 07:31:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cyphar.com; s=MBO0001; t=1754458285; 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=q7MzN3ay8IG6Jjs4hHxY8KUxDAJt90+4JEoWvq0gBos=; b=ONxbjG7lyvDXO08sTSvdNMc24C2fvZprSEf5JWO2FC/eTPsZM4EDcXfmO8e0Z/I5SGh3xD Jxa/I0aLz29mMe0VYQe9nAtF8p3lVAV4lyAeWrpoHrcDwJDcNLMJscE1TUuyk5uJHXRCzC sHh6mL21bKifjXAz7bMzgevhgHj9ZmI0LvesId0vwpNUxIzCHHSukvSl7gDcivPLXK3WYr fVqFHyNEPIq/dQQO9L1S4ItVY8yyXtEAXi3+ZHw/E3spAwRAfIqunpBlya57KPIhlHkFOT a/5L89Dt3iPq1LlA0VuYYt35ZgfOh6A8nVrhbfxONe5vvGOPNETBasdqxsczAg== From: Aleksa Sarai Date: Wed, 06 Aug 2025 15:31:10 +1000 Subject: [PATCH 1/2] fscontext: do not consume log entries for -EMSGSIZE case 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-v1-1-880597d42a5a@cyphar.com> References: <20250806-fscontext-log-cleanups-v1-0-880597d42a5a@cyphar.com> In-Reply-To: <20250806-fscontext-log-cleanups-v1-0-880597d42a5a@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=1864; i=cyphar@cyphar.com; h=from:subject:message-id; bh=grK8qXho5eJRNhUBpVWJrruv3CCOc+X2tolA7nBflUg=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMWRMerFo7d7JDMq5VR1/MvwOZod5rWXdW1O6YP7Gf/+Ob ilc0h+c3VHKwiDGxSArpsiyzc8zdNP8xVeSP61kg5nDygQyhIGLUwAmIv+C4Z/COr5CRffdSvmK jZPjJ87tUU6asjFBWzk+sUDrW/MBHlaGvzJqaott67/O26L5Om3aVA37mOx9+5clrJAMkXoQyv7 +IxMA X-Developer-Key: i=cyphar@cyphar.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 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. Fixes: 007ec26cdc9f ("vfs: Implement logging through fs_context") Cc: David Howells Cc: # v5.2+ Signed-off-by: Aleksa Sarai --- fs/fsopen.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/fs/fsopen.c b/fs/fsopen.c index 1aaf4cb2afb2..f5fdaa97965b 100644 --- a/fs/fsopen.c +++ b/fs/fsopen.c @@ -36,23 +36,25 @@ static ssize_t fscontext_read(struct file *file, if (ret < 0) return ret; =20 - if (log->head =3D=3D log->tail) { - mutex_unlock(&fc->uapi_mutex); - return -ENODATA; - } + ret =3D -ENODATA; + if (log->head =3D=3D log->tail) + goto err_unlock_nomsg; =20 index =3D log->tail & (logsize - 1); p =3D log->buffer[index]; + n =3D strlen(p); + + ret =3D -EMSGSIZE; + if (n > len) + goto err_unlock_nomsg; + + /* Consume the message from the queue. */ need_free =3D log->need_free & (1 << index); log->buffer[index] =3D NULL; log->need_free &=3D ~(1 << index); log->tail++; mutex_unlock(&fc->uapi_mutex); =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; @@ -62,6 +64,10 @@ static ssize_t fscontext_read(struct file *file, if (need_free) kfree(p); return ret; + +err_unlock_nomsg: + mutex_unlock(&fc->uapi_mutex); + return ret; } =20 static int fscontext_release(struct inode *inode, struct file *file) --=20 2.50.1 From nobody Sun Oct 5 09:10:54 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 75C941FECD4; Wed, 6 Aug 2025 05:31:33 +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=1754458295; cv=none; b=p1xSkmKn6awDxmWgs+oHaxSfaX3biQeyvpMWsyAQpb5/qBrutmdhwRCE0uVGCogIgSDtds37OgGMVD1qRJcW59OpTdMrxGl+EQLJss/VxDWT4fZ0jnmvbDuuxysAIdrtuLiDhA9HfQxigRUpfgNKDjSrppfMX9UISnhlardkL8w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754458295; c=relaxed/simple; bh=Yv4RRmNeO9isqficU05qBD/VkyxIM3He3e4z12DA3e0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PJpfd3TDJGL7giZLCWc5ZKqQo140ENAbrgCSrYIjM57gFBxEh2rvPunIU4sslNnspSn2TCpkOdTEuTQ2TrNf5pBO3tV+G27k2E0p2QukGnrMAbWZMXUgDRQ+ibciApd0xV+5E9RgFlOBB09Xh0jY7yjYby/452dou+IhfniC91Y= 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=FfjkQBy5; 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="FfjkQBy5" Received: from smtp102.mailbox.org (smtp102.mailbox.org [10.196.197.102]) (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 4bxf6T6Hcdz9t87; Wed, 6 Aug 2025 07:31:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cyphar.com; s=MBO0001; t=1754458289; 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=68QMYqi1n6KdQADbsGhGyxYlrXRfqj2JDm4Cojhf0m8=; b=FfjkQBy5Etc7NtLUvteqVpw1gYJPzEOV1uE48u0+W9/OOZpzsxhEfU5KlvYsvvi7mhIfpS 72cSUxo7nKXatkNrzYJbQg8l1U0nrR0ySB7Cmm1Op38SrujEfy+7Z4JVJC0Ov4Mc3F1kRJ X1bsSaU2F2T4YqQBd0YEA7V0/9pUfwVcevPHSaR+JHAnHwG9UuReOqfmtEU1hDdXIN8osJ 0FJ+gMZAxxLPgELFy0WCTWizTAAVXP1PbjXZENp2t4hMqBGVrklTm7nkS/UcFtr81L279+ 78g71Li7a0veMeuR5VtrSpPVAvTccJi8d2nqDrVFGfWfPmyQ03jjYPBFk2muIw== From: Aleksa Sarai Date: Wed, 06 Aug 2025 15:31:11 +1000 Subject: [PATCH 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-v1-2-880597d42a5a@cyphar.com> References: <20250806-fscontext-log-cleanups-v1-0-880597d42a5a@cyphar.com> In-Reply-To: <20250806-fscontext-log-cleanups-v1-0-880597d42a5a@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=5406; i=cyphar@cyphar.com; h=from:subject:message-id; bh=Yv4RRmNeO9isqficU05qBD/VkyxIM3He3e4z12DA3e0=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMWRMerFYT2BSikb9/41TfVbe+MQs9OCnp4FPIUf9q/33F G1vZb4X7ihlYRDjYpAVU2TZ5ucZumn+4ivJn1aywcxhZQIZwsDFKQATeSPPyDDBWv9WQ6fbiYwV e++JnyvY/kgi6Iubr2Pb/eyFPAG12p8YGWZ+nP578dYyHta7mybYXfvd0WkgGye1W/pcbNanRc7 CixkB 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 | 137 +++++++++++++++++++++= ++++ 3 files changed, 139 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..2bdb6d6a1ff1 --- /dev/null +++ b/tools/testing/selftests/filesystems/fclog.c @@ -0,0 +1,137 @@ +// 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 +#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. + */ + for (int i =3D 0; i < 16; i++) + ASSERT_ERRNO_EQ(-EMSGSIZE, read(fsfd, buf, 1)); + 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