From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.218.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793225915; b=BF6uKH5ZkvmDHhuAZQzTd5F1njwQoqSyPLNlU+HmH463TtliQo7GkLRfq9C7n6Ptk0l8 Fjp8R1Rs1tv7QuHw9kKu5m3ZZnXdQuXnkSWDS/8FffCrw1CMyIkEA8YyVC+O5wDz51JI1 JrQpGNu9u+5cMLSLdQ9a+qYGKKGLo69/6d3h5b+ph68uYJaEbdt5oqrsGuUfx6e8avQlr 3aTp2GXjUvN8NDYYefemL9cYA6CzYNhcd9BeWzoqjJcJTj4Cct6rdtJTBPzi7JXnsPdnj ssYaD0z2iMy9NngZXWNzS8e7+o2rsTnguDQt3OD0t3/CIJ2GtaECioOuikNlU7XOo2w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793225915; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=O3jI53IvGMjui3qM6yb8MAnyltVbJGBw/n9i8mncKWg=; b=Gy2OjXTrIO1IKoWX+XlzqKJsj2RxjokDKrQiRauNhjjwCINDyUUhm+BUW9xCJBJvFhJZ +eaz6PuIxIGAKA6aJS9SG8eet9an41QEPLdYtXRKcA6juemnMkAh7Y8V7fLiV4ZZUIgPe aqfZ8noiOuaMd/8/o3ctKhv8FiEg9NihOID0GO2/JW/Eu7N4s+QYReLV+bG7F37L4l8px 0QW/7SIht7hHkA1HT4WMLIQ7xV7GZtegVlTdoRlyvGxoBUjdXaf58ScE8xQcM1JAqySGZ bi7k5IRi8NglsckicKg/IYePmRVL3/CPgG2pVMZWBIdlEJFIF62g0c702KVdsL9oYyw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--halg.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=ZocZJ6fm; arc=none smtp.remote-ip=209.85.218.70 Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8582060A5BC3 for ; Wed, 28 Oct 2026 22:18:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8582060A5BC3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--halg.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=ZocZJ6fm Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-c3f04e0e481so151082566b.1 for ; Wed, 28 Oct 2026 15:18:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1793225899; x=1793830699; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=O3jI53IvGMjui3qM6yb8MAnyltVbJGBw/n9i8mncKWg=; b=ZocZJ6fmNbV1TfdYdsOMCEl0qWX8N3eFzH/u4GRw09k6dmxM/tq2H/RruUScFwtoFY LlLlFx/WAwBD+j19yldTxYyQKpxfNoqfQimaMqQ1ZZx9H5MrwNdEgryllmDk834aXdgT n525rXNjQTppvAF3SeM4QeqsRs/nddO07X5aZRYGgacWMN+ovuxez+GkR31/sil69fLK E1MuMlQU8Cvtwpsp+kKlDyunAYnV2JIRJ/WrUnNqwTR6tuTWtZWZsc4pmXwjpr7sK2NK 1QFGq77AxEUSq/bZC4ZovdO1+8DqqbFpKRA60xvycYhhr3jS+O2OjNZPaPNicnj4Vm53 IAnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793225899; x=1793830699; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=O3jI53IvGMjui3qM6yb8MAnyltVbJGBw/n9i8mncKWg=; b=Bt6N4OIwEbuSAEQ2wOoC0kfl/cIlT+uX+nKgKwCawsv6z71QozCwklYQs/3ml+LET2 Rfyl3KEmhUgt6J8KyosmnkiVhh4eXLV7MUS9MKBsXVLDZeZcWPkyuE9xG5EaPQSyiMn4 vrM2HQ1QwuMam1Nvr9Hy5oPySaEN6hSnyS8P90y+1lI+j80zP4sZRX7cESV3qndFv51a jxVE35EcHbKEadYTqY8P1BWFMX2etXWzg8GWzL1ceP6e67bJG8Vws8OMbeXuPJOAZGkw zFSTe8nGiscYgh8VVTudFIk7wgoqNS2Ksyk1Vg4ap1Vb9Kx8yTWm6b8z9x02hTR6kdWz IrsQ== X-Forwarded-Encrypted: i=1; AKwUvByYrP+ME7lQ4nhofQCewJuvq4K5/zlwk+5b69+KAZv5qcIlqu39SFw+6TMbtPmLGXHoEKrOTJRfixo=@vger.kernel.org X-Gm-Message-State: AFq9FYJ9UTmuVeN3/GNbi8VMfH8aeO82Sq7kv0B0ASdHujK70EbEqahj 0viSh0lyN4ap49Eypipn39B4xUyUpAijNwwCA0a726l3QzPy71AVDWRRXTPdcDOBljvqbiAYKA= = X-Received: from ejbch15.prod.google.com ([2002:a17:906:c2cf:b0:c2e:81fe:4522]) (user=halg job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:980c:b0:c2e:4d39:b5a1 with SMTP id a640c23a62f3a-c49bdf2c6efmr324604666b.17.1793225898939; Wed, 28 Oct 2026 15:18:18 -0700 (PDT) Date: Wed, 28 Oct 2026 22:17:05 +0000 In-Reply-To: <20261028221705.3915340-1-halg@google.com> X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261028221705.3915340-1-halg@google.com> X-Mailer: git-send-email 2.56.0.313.g2479322e68-goog Message-ID: <20261028221705.3915340-9-halg@google.com> Subject: [PATCH 8/8] selftests/nsfs: add tests for NS_USER_GET_INFO ioctl From: "Hallgrimur H. Gunnarsson" To: Christian Brauner , Alexander Viro , Paul Moore , Serge Hallyn , Kees Cook , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Jan Kara , James Morris , Simon Horman , Jonathan Corbet , Shuah Khan , Randy Dunlap , Jann Horn , "=?UTF-8?q?G=C3=BCnther=20Noack?=" , KP Singh , Jonathan Calmels , Casey Schaufler , linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org, linux-api@vger.kernel.org, containers@lists.linux.dev, netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, "Hallgrimur H. Gunnarsson" Content-Type: text/plain; charset="UTF-8" Add tests for the NS_USER_GET_INFO ioctl to verify user namespace capability bounding set inspection and validate error paths. Signed-off-by: Hallgrimur H. Gunnarsson --- .../selftests/filesystems/nsfs/.gitignore | 1 + .../selftests/filesystems/nsfs/Makefile | 4 +- .../selftests/filesystems/nsfs/userns.c | 120 ++++++++++++++++++ 3 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/filesystems/nsfs/userns.c diff --git a/tools/testing/selftests/filesystems/nsfs/.gitignore b/tools/testing/selftests/filesystems/nsfs/.gitignore index 92a8249006d1..b0a59f5d49fc 100644 --- a/tools/testing/selftests/filesystems/nsfs/.gitignore +++ b/tools/testing/selftests/filesystems/nsfs/.gitignore @@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only owner pidns +userns iterate_mntns diff --git a/tools/testing/selftests/filesystems/nsfs/Makefile b/tools/testing/selftests/filesystems/nsfs/Makefile index 231aaa7dfd95..9408d7948621 100644 --- a/tools/testing/selftests/filesystems/nsfs/Makefile +++ b/tools/testing/selftests/filesystems/nsfs/Makefile @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only -TEST_GEN_PROGS := owner pidns iterate_mntns +TEST_GEN_PROGS := owner pidns iterate_mntns userns -CFLAGS := -Wall -Werror +CFLAGS += -Wall -Werror $(KHDR_INCLUDES) include ../../lib.mk diff --git a/tools/testing/selftests/filesystems/nsfs/userns.c b/tools/testing/selftests/filesystems/nsfs/userns.c new file mode 100644 index 000000000000..70661a495afc --- /dev/null +++ b/tools/testing/selftests/filesystems/nsfs/userns.c @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../clone3/clone3_selftests.h" +#include "../../kselftest_harness.h" + +TEST(user_get_info_self) +{ + struct user_ns_info info = {}; + int fd; + + fd = open("/proc/self/ns/user", O_RDONLY | O_CLOEXEC); + ASSERT_GE(fd, 0) { + TH_LOG("Failed to open /proc/self/ns/user: %m"); + } + + ASSERT_EQ(0, ioctl(fd, NS_USER_GET_INFO, &info)) { + TH_LOG("ioctl(NS_USER_GET_INFO) failed on self: %m"); + } + close(fd); + + EXPECT_EQ(USER_NS_INFO_SIZE_VER0, info.size); + EXPECT_EQ(0, info.spare); + EXPECT_NE(0ULL, info.cap_bset); +} + +TEST(user_get_info_restricted_bset) +{ + struct __clone_args args = { + .flags = CLONE_NEWUSER | CLONE_NEWUSER_BSET, + .exit_signal = SIGCHLD, + .cap_bset = 0, + }; + int status; + pid_t pid; + + pid = sys_clone3(&args, sizeof(args)); + ASSERT_GE(pid, 0); + + if (pid == 0) { + struct user_ns_info info = {}; + int fd; + + fd = open("/proc/self/ns/user", O_RDONLY | O_CLOEXEC); + if (fd < 0) + _exit(1); + + if (ioctl(fd, NS_USER_GET_INFO, &info) != 0) { + close(fd); + _exit(2); + } + close(fd); + + if (info.cap_bset != 0) + _exit(3); + + _exit(0); + } + + ASSERT_EQ(pid, waitpid(pid, &status, 0)); + ASSERT_TRUE(WIFEXITED(status)); + EXPECT_EQ(0, WEXITSTATUS(status)); +} + +TEST(user_get_info_invalid_ns_type) +{ + struct user_ns_info info = {}; + int mntns_fd; + + mntns_fd = open("/proc/self/ns/mnt", O_RDONLY | O_CLOEXEC); + ASSERT_GE(mntns_fd, 0) { + TH_LOG("Failed to open /proc/self/ns/mnt: %m"); + } + + EXPECT_EQ(-1, ioctl(mntns_fd, NS_USER_GET_INFO, &info)); + EXPECT_EQ(EINVAL, errno); + + close(mntns_fd); +} + +TEST(user_get_info_buffer_too_small) +{ + __u32 small_buf = 0; + int fd; + + fd = open("/proc/self/ns/user", O_RDONLY | O_CLOEXEC); + ASSERT_GE(fd, 0); + + /* Encode a buffer size smaller than USER_NS_INFO_SIZE_VER0 (4B vs 16B) */ + EXPECT_EQ(-1, ioctl(fd, _IOR(NSIO, 14, __u32), &small_buf)); + + close(fd); +} + +TEST(user_get_info_null_arg) +{ + int fd; + + fd = open("/proc/self/ns/user", O_RDONLY | O_CLOEXEC); + ASSERT_GE(fd, 0); + + EXPECT_EQ(-1, ioctl(fd, NS_USER_GET_INFO, NULL)); + EXPECT_EQ(EINVAL, errno); + + close(fd); +} + +TEST_HARNESS_MAIN -- 2.56.0.313.g2479322e68-goog