From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795477188; b=sodyA9Yuz96W8TKCVZRkH9kRDdpoHw/qoPw0CB0unVkaW3CiAUi+gOM1UrMvgDDNNNv3 QOo+h+KzYVKsFXBviP/wX5jQDH6Pcj39V0DbBuzjNac/CskMxro1HZQ2zwcrL6MoJZP3v eohtsytoXln9+L5d3takgbxxJWT/uhcvLsz/2CX4ERB0uFx0A23K8j7YlMGN6T9bBN0ya ffpBgQ9u2oZ4ULV/AFpcy9uRfAi2E2xZ/DnKoretzpAXbn/xad5nIX1CuZTjGCt4IjpdS iiYSyXC5oeoRU/MpFlC6BvlyQq7MSAjmJS0zv2MJOIZ3MqbznmuHZ9PkIMZ5w5nhEaw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795477188; 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=/0gvYOUOsBh9zCqNwor8+pPkFTru+bs+6gLwfR5yOEk=; b=FkZabEbpkMY244ByA3qUmlOMVrdGUpCla5XWMZxUW/txnl4cFnOycrKfCuqCf8oxM6va 5FCmQMwJE4ZPcLc7+gwQphtXGxWzIFjgsVXTC+edFfXvhVvMqNQc9uBQqj4IiPi//2IQN TvZ3An0M7EXZ7aBs2W8bJdp6fcTbh8aAyOUa/o8myUckheO2lilvHlYm1/ca/7/7qH9c/ 8mcBALpBZs5pZKQxFaaig+yuj4TzFtwTy8vC0KvhDvkP2Q4Sp5Hh6auYkn/uq7UmMhZuq GMx5sFzeTuSj2EHXKZkW9rPc3M2M8aPfk/UP5Pi6TTPX1dUOdvrjnjaVulsp5o4OF+g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=HzaivfEU; arc=none smtp.remote-ip=209.85.167.199 Received: from mail-oi1-f199.google.com (mail-oi1-f199.google.com [209.85.167.199]) (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 2E00F606A6A6 for ; Mon, 23 Nov 2026 23:39:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2E00F606A6A6 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=HzaivfEU Received: by mail-oi1-f199.google.com with SMTP id 5614622812f47-57e469f8312so255267b6e.1 for ; Mon, 23 Nov 2026 15:39:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795477187; x=1796081987; 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=/0gvYOUOsBh9zCqNwor8+pPkFTru+bs+6gLwfR5yOEk=; b=HzaivfEUfByy0uF0tJXga71Iaqrr/7oshwFMCZljWJzC+eME/ZvczCn5RZU01iEa+Y mWJmFnUzQfkzVfLoL33AF0GY959K5dulr5eR4e32X+Dj++fx0/lsDvRew87xDVQQfWXq fzfqI6AG5AdQiwsed3LSnj03/PKGj/d5Ovjrfmrcw9ubelJwId8ZbllSra9jymQ9+nrN oMPb0RKlAYIAN8SdkqHWFjPVLM9PGne7L/7I/u1TYisaoQxNne7PfQlERx7E8P/Gkf3c eyaBl5TGLRkRg+1rXai8MNNU1VdfodPyy80izb16qvDDUBEsZokEhY20ZMM0qELfuoQa ecDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795477187; x=1796081987; 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=/0gvYOUOsBh9zCqNwor8+pPkFTru+bs+6gLwfR5yOEk=; b=OzDAcT9iPuJCE6ewy2dQHc3ySw/gn/FJ0miCKVMl86wDJm5/PGpIaUUml7Kwzp54Wk kxC3nnudSvXGKIVdqGjxCwlPQjziPzeZg8faoOx1mWqVvG7K7aNfFJk6IZxH28nVqY03 Yv6DHZdgPX9qQDOwY5IQmNUCG2r4j443L0St/ODrmE0hpexNLIgNlaOtQ15DrpFhxnw9 k9dc/x3dCg1ZVjczXhDKCDDoi3OrWR2NFxBgdJyDsRrHi9RgKAyGi0ceh+ltt5TCXAfh OsBtGMXFB3WkZXgDHbFpdQCixaS1y3IGdDYoJONBYIuKjDEBUN9KGXeu4Bspk123815O lYIw== X-Forwarded-Encrypted: i=1; AL+hErGUJBUtzH3CqcpAIvwQ7T2qhZH2yUf17r+WXvcG3FdTGJOzXMK7OCF72encm8LXQzZr8qT3MviN96BNBFM=@vger.kernel.org X-Gm-Message-State: APuU4Z/UDYtJq3kjgyLubgZ1kTj+CJzMeoFCoDxlIVAQ5K4bW8fxPLul qK8mmLtCtqonQkZ6zrF5daHMvOR9DM00S/YAJcDx4Wslz5FvntBqsHELkjQfA37S+PhQF3+fWSW YfpPg4YUDEorT3Q== X-Received: from ilben18-n1.prod.google.com ([2002:a05:6e02:5112:10b0:54e:8e95:90ae]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:4488:b0:4f9:6348:cd3b with SMTP id 5614622812f47-57a0b4413abmr14381949b6e.41.1795477186803; Mon, 23 Nov 2026 15:39:46 -0800 (PST) Date: Mon, 23 Nov 2026 23:39:18 +0000 In-Reply-To: <20261123233924.3970932-1-dmatlack@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261123233924.3970932-1-dmatlack@google.com> X-Mailer: git-send-email 2.56.1.809.ga0b61035b5-goog Message-ID: <20261123233924.3970932-14-dmatlack@google.com> Subject: [PATCH v5 13/18] selftests/liveupdate: Test which tmpfs files can be preserved From: David Matlack To: kexec@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org Cc: Alexander Graf , Andrew Morton , Baolin Wang , Christian Brauner , David Hildenbrand , Hugh Dickins , Jonathan Corbet , "Liam R. Howlett" , Lorenzo Stoakes , Michal Hocko , Mike Rapoport , Pasha Tatashin , Pratyush Yadav , Randy Dunlap , Samiullah Khawaja , Shuah Khan , Suren Baghdasaryan , Vlastimil Babka , David Matlack Content-Type: text/plain; charset="UTF-8" Add tests to liveupdate.c for the checks done when preserving a tmpfs file: A file can be preserved through a read-only fd, but not a write-only fd. Preserving it requires permission to write to it, except for the owner of an immutable file or of a file on a read-only superblock. Append-only files can't be preserved, and files can be preserved through a read-only mount. Also check that operations that would change the size of a preserved file fail with -EPERM until the file is unpreserved, while reads and writes within the file work. The errors of these preserve failures aren't documented, so the tests only check that the preserve fails. These checks don't need a kexec, so they run as part of the regular liveupdate selftest. Enable CONFIG_TMPFS_XATTR, which tmpfs needs for FS_IOC_SETFLAGS. The test cases that use it are skipped without it. Signed-off-by: David Matlack --- tools/testing/selftests/liveupdate/config | 1 + .../testing/selftests/liveupdate/liveupdate.c | 316 ++++++++++++++++++ 2 files changed, 317 insertions(+) diff --git a/tools/testing/selftests/liveupdate/config b/tools/testing/selftests/liveupdate/config index 016d009dba13..37230f4c488f 100644 --- a/tools/testing/selftests/liveupdate/config +++ b/tools/testing/selftests/liveupdate/config @@ -9,4 +9,5 @@ CONFIG_LIVEUPDATE=y CONFIG_LIVEUPDATE_TEST=y CONFIG_MEMFD_CREATE=y CONFIG_TMPFS=y +CONFIG_TMPFS_XATTR=y CONFIG_SHMEM=y diff --git a/tools/testing/selftests/liveupdate/liveupdate.c b/tools/testing/selftests/liveupdate/liveupdate.c index 8b6e61695a35..094e7e22c1fb 100644 --- a/tools/testing/selftests/liveupdate/liveupdate.c +++ b/tools/testing/selftests/liveupdate/liveupdate.c @@ -16,15 +16,24 @@ * - Resource preservation: successfully preserving individual and multiple memfds, * verifying contents remain accessible. * - Complex multi-session scenarios involving mixed empty and populated files. + * - Which tmpfs files can be preserved, and the frozen size of preserved files. */ #include #include +#include #include +#include #include #include +#include +#include +#include #include +#include +#include + #include #include @@ -578,4 +587,311 @@ TEST_F(liveupdate_device, preserve_memfd_with_arg) ASSERT_EQ(close(mem_fd2), 0); } +#define NOBODY 65534 + +FIXTURE(preserve_tmpfs) { + char dir[32]; + int luo_fd; + int session_fd; + bool mounted; +}; + +FIXTURE_SETUP(preserve_tmpfs) +{ + self->luo_fd = -1; + self->session_fd = -1; + self->mounted = false; + + self->luo_fd = open(LIVEUPDATE_DEV, O_RDWR); + if (self->luo_fd < 0 && errno == ENOENT) + SKIP(return, "%s does not exist", LIVEUPDATE_DEV); + ASSERT_GE(self->luo_fd, 0); + + self->session_fd = luo_create_session(self->luo_fd, "preserve-tmpfs"); + ASSERT_GE(self->session_fd, 0); + + strcpy(self->dir, "/tmp/luo-tmpfs-XXXXXX"); + ASSERT_NE(mkdtemp(self->dir), NULL); + if (mount("tmpfs", self->dir, "tmpfs", 0, "size=1M")) { + int err = errno; + + rmdir(self->dir); + SKIP(return, "Failed to mount tmpfs: %s", strerror(err)); + } + self->mounted = true; + ASSERT_EQ(chdir(self->dir), 0); +} + +FIXTURE_TEARDOWN(preserve_tmpfs) +{ + if (self->session_fd >= 0) + close(self->session_fd); + if (self->luo_fd >= 0) + close(self->luo_fd); + if (chdir("/")) + return; + if (self->mounted) + umount2(self->dir, MNT_DETACH); + rmdir(self->dir); +} + +/* Create the file @path with some contents and the mode @mode. */ +static int create_file(const char *path, mode_t mode) +{ + int fd; + + fd = open(path, O_CREAT | O_EXCL | O_RDWR, mode); + if (fd < 0) + return -1; + + if (write(fd, "data", 4) != 4 || fchmod(fd, mode)) { + close(fd); + return -1; + } + + return fd; +} + +/* + * Open @path with @flags and preserve it. Return 0 if the preserve succeeds, + * -errno if it fails, or 1 if @path can't be opened. + */ +static int preserve_path(int session_fd, const char *path, int flags, + __u64 token) +{ + int fd, ret; + + fd = open(path, flags); + if (fd < 0) + return 1; + + ret = luo_session_preserve_fd(session_fd, fd, token); + close(fd); + return ret; +} + +/* + * Like preserve_path() with O_RDONLY, but as the user nobody. Also return 1 if + * the child can't become nobody. + */ +static int preserve_as_nobody(int session_fd, const char *path, __u64 token) +{ + int status, ret; + pid_t pid; + + pid = fork(); + if (pid < 0) + return 1; + + if (!pid) { + if (setgroups(0, NULL) || setresgid(NOBODY, NOBODY, NOBODY) || + setresuid(NOBODY, NOBODY, NOBODY)) + _exit(255); + ret = preserve_path(session_fd, path, O_RDONLY, token); + _exit(ret > 0 ? 255 : -ret); + } + + if (waitpid(pid, &status, 0) != pid || !WIFEXITED(status) || + WEXITSTATUS(status) == 255) + return 1; + + return -WEXITSTATUS(status); +} + +/* Add the inode flag @flag, e.g. FS_IMMUTABLE_FL, to the file @fd. */ +static int add_inode_flag(int fd, int flag) +{ + int flags; + + if (ioctl(fd, FS_IOC_GETFLAGS, &flags)) + return -errno; + + flags |= flag; + if (ioctl(fd, FS_IOC_SETFLAGS, &flags)) + return -errno; + + return 0; +} + +/* + * Test Case: Preserve Through Read-Only and Write-Only Fds + * + * A file can be preserved through a read-only fd, but not through a + * write-only fd, since the retrieved file can be read. + */ +TEST_F(preserve_tmpfs, access_mode) +{ + int fd; + + fd = create_file("file", 0644); + ASSERT_GE(fd, 0); + close(fd); + + EXPECT_LT(preserve_path(self->session_fd, "file", O_WRONLY, 1), 0); + EXPECT_EQ(preserve_path(self->session_fd, "file", O_RDONLY, 2), 0); +} + +/* + * Test Case: Preserve Without Write Permission + * + * Preserving a file freezes its size for all of its users, so it requires + * permission to write to the file. + */ +TEST_F(preserve_tmpfs, unwritable) +{ + int fd; + + fd = create_file("file", 0644); + ASSERT_GE(fd, 0); + close(fd); + + EXPECT_LT(preserve_as_nobody(self->session_fd, "file", 1), 0); + EXPECT_EQ(preserve_path(self->session_fd, "file", O_RDONLY, 2), 0); +} + +/* + * Test Case: Preserve an Append-Only File + * + * Freezing the size of an append-only file would stop all writes to it, so + * append-only files can't be preserved. + */ +TEST_F(preserve_tmpfs, append_only) +{ + int fd, ret; + + fd = create_file("file", 0644); + ASSERT_GE(fd, 0); + ret = add_inode_flag(fd, FS_APPEND_FL); + close(fd); + if (ret == -ENOTTY || ret == -EOPNOTSUPP) + SKIP(return, "FS_IOC_SETFLAGS is not supported"); + ASSERT_EQ(ret, 0); + + EXPECT_LT(preserve_path(self->session_fd, "file", O_RDONLY, 1), 0); +} + +/* + * Test Case: Preserve an Immutable File + * + * The size of an immutable file can't change anyway, so its owner may preserve + * it. Others still need permission to write to it. + */ +TEST_F(preserve_tmpfs, immutable) +{ + int fd, ret; + + fd = create_file("own", 0644); + ASSERT_GE(fd, 0); + ASSERT_EQ(fchown(fd, NOBODY, NOBODY), 0); + ret = add_inode_flag(fd, FS_IMMUTABLE_FL); + close(fd); + if (ret == -ENOTTY || ret == -EOPNOTSUPP) + SKIP(return, "FS_IOC_SETFLAGS is not supported"); + ASSERT_EQ(ret, 0); + + fd = create_file("other", 0666); + ASSERT_GE(fd, 0); + ASSERT_EQ(add_inode_flag(fd, FS_IMMUTABLE_FL), 0); + close(fd); + + EXPECT_EQ(preserve_as_nobody(self->session_fd, "own", 1), 0); + EXPECT_LT(preserve_as_nobody(self->session_fd, "other", 2), 0); +} + +/* + * Test Case: Preserve Through a Read-Only Mount + * + * Preserving a file only needs permission to write to the file, not a writable + * mount, so it works through a read-only mount. + */ +TEST_F(preserve_tmpfs, rdonly_mount) +{ + int fd; + + ASSERT_EQ(mkdir("src", 0755), 0); + fd = create_file("src/file", 0644); + ASSERT_GE(fd, 0); + close(fd); + + ASSERT_EQ(mkdir("ro", 0755), 0); + ASSERT_EQ(mount("src", "ro", NULL, MS_BIND, NULL), 0); + ASSERT_EQ(mount(NULL, "ro", NULL, MS_REMOUNT | MS_BIND | MS_RDONLY, + NULL), 0); + + EXPECT_EQ(preserve_path(self->session_fd, "ro/file", O_RDONLY, 1), 0); + umount2("ro", MNT_DETACH); +} + +/* + * Test Case: Preserve on a Read-Only Superblock + * + * The size of a file on a read-only superblock can't change anyway, so its + * owner may preserve it. Others still need permission to write to it. + */ +TEST_F(preserve_tmpfs, rdonly_sb) +{ + int fd; + + fd = create_file("own", 0644); + ASSERT_GE(fd, 0); + ASSERT_EQ(fchown(fd, NOBODY, NOBODY), 0); + close(fd); + + fd = create_file("other", 0666); + ASSERT_GE(fd, 0); + close(fd); + + ASSERT_EQ(mount(NULL, self->dir, NULL, MS_REMOUNT | MS_RDONLY, NULL), + 0); + + EXPECT_EQ(preserve_as_nobody(self->session_fd, "own", 1), 0); + EXPECT_LT(preserve_as_nobody(self->session_fd, "other", 2), 0); +} + +/* + * Test Case: Operations on a Preserved File + * + * While a file is preserved, operations that would change its size or punch + * holes into it fail with -EPERM. Reads and writes within the file still work. + * Closing the session unpreserves the file, after which its size can change + * again. + */ +TEST_F(preserve_tmpfs, preserved_ops) +{ + long page_size = getpagesize(); + struct stat st; + char c; + int fd; + + fd = create_file("file", 0644); + ASSERT_GE(fd, 0); + ASSERT_EQ(ftruncate(fd, 2 * page_size), 0); + ASSERT_EQ(luo_session_preserve_fd(self->session_fd, fd, 1), 0); + + EXPECT_EQ(ftruncate(fd, page_size), -1); + EXPECT_EQ(errno, EPERM); + EXPECT_EQ(ftruncate(fd, 4 * page_size), -1); + EXPECT_EQ(errno, EPERM); + EXPECT_EQ(fallocate(fd, 0, 0, 4 * page_size), -1); + EXPECT_EQ(errno, EPERM); + EXPECT_EQ(fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, + page_size), -1); + EXPECT_EQ(errno, EPERM); + EXPECT_EQ(pwrite(fd, "x", 1, 2 * page_size), -1); + EXPECT_EQ(errno, EPERM); + + /* The size didn't change, but writes within the file work. */ + ASSERT_EQ(fstat(fd, &st), 0); + EXPECT_EQ(st.st_size, 2 * page_size); + EXPECT_EQ(pwrite(fd, "x", 1, page_size), 1); + EXPECT_EQ(pread(fd, &c, 1, page_size), 1); + EXPECT_EQ(c, 'x'); + + ASSERT_EQ(close(self->session_fd), 0); + self->session_fd = -1; + EXPECT_EQ(ftruncate(fd, page_size), 0); + + close(fd); +} + TEST_HARNESS_MAIN -- 2.56.1.809.ga0b61035b5-goog