git: e8dd46ee96c8 - stable/13 - fusefs: add more readdir tests for misbehaving servers

From: Alan Somers <asomers_at_FreeBSD.org>
Date: Thu, 30 Nov 2023 23:48:47 UTC
The branch stable/13 has been updated by asomers:

URL: https://cgit.FreeBSD.org/src/commit/?id=e8dd46ee96c82ed06e5d79f503203a40719fb8d8

commit e8dd46ee96c82ed06e5d79f503203a40719fb8d8
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2023-10-05 16:13:05 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2023-11-30 20:41:53 +0000

    fusefs: add more readdir tests for misbehaving servers
    
    Inspired by PR 274268
    
    Sponsored by:   Axcient
    
    (cherry picked from commit 6a773a0582ba936cc19734b21ee5a7bed49cfdec)
---
 tests/sys/fs/fusefs/readdir.cc | 87 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 87 insertions(+)

diff --git a/tests/sys/fs/fusefs/readdir.cc b/tests/sys/fs/fusefs/readdir.cc
index 5fd8f25e8bb7..6b78e3a70697 100644
--- a/tests/sys/fs/fusefs/readdir.cc
+++ b/tests/sys/fs/fusefs/readdir.cc
@@ -321,6 +321,50 @@ TEST_F(Readdir, nodots)
 	leakdir(dir);
 }
 
+/*
+ * FUSE_READDIR returns a path with an embedded NUL. Obviously illegal, but
+ * nothing bad should happen.
+ */
+TEST_F(Readdir, nul)
+{
+	const char FULLPATH[] = "mountpoint/some_dir";
+	const char RELPATH[] = "some_dir";
+	uint64_t ino = 42;
+	DIR *dir;
+	struct dirent *de;
+	vector<struct dirent> ents(1);
+	vector<struct dirent> empty_ents(0);
+	const char nul[] = "foo\0bar";
+
+	expect_lookup(RELPATH, ino);
+	expect_opendir(ino);
+	ents[0].d_fileno = 4;
+	ents[0].d_off = 4000;
+	ents[0].d_namlen = sizeof(nul);
+	ents[0].d_type = DT_REG;
+	strncpy(ents[0].d_name, nul, ents[0].d_namlen);
+	expect_readdir(ino, 0, ents);
+	expect_readdir(ino, 4000, empty_ents);
+
+	errno = 0;
+	dir = opendir(FULLPATH);
+	ASSERT_NE(nullptr, dir) << strerror(errno);
+
+	errno = 0;
+	de = readdir(dir);
+	ASSERT_NE(nullptr, de) << strerror(errno);
+	EXPECT_EQ(4ul, de->d_fileno);
+	EXPECT_EQ(DT_REG, de->d_type);
+	EXPECT_EQ(sizeof(nul), de->d_namlen);
+	EXPECT_EQ(0, strcmp(nul, de->d_name));
+
+	ASSERT_EQ(nullptr, readdir(dir));
+	ASSERT_EQ(0, errno);
+
+	leakdir(dir);
+}
+
+
 /* telldir(3) and seekdir(3) should work with fuse */
 TEST_F(Readdir, seekdir)
 {
@@ -397,6 +441,49 @@ TEST_F(Readdir, seekdir)
 	leakdir(dir);
 }
 
+/*
+ * FUSE_READDIR returns a path with an embedded /. Obviously illegal, but
+ * nothing bad should happen.
+ */
+TEST_F(Readdir, slash)
+{
+	const char FULLPATH[] = "mountpoint/some_dir";
+	const char RELPATH[] = "some_dir";
+	uint64_t ino = 42;
+	DIR *dir;
+	struct dirent *de;
+	vector<struct dirent> ents(1);
+	vector<struct dirent> empty_ents(0);
+	const char foobar[] = "foo/bar";
+
+	expect_lookup(RELPATH, ino);
+	expect_opendir(ino);
+	ents[0].d_fileno = 4;
+	ents[0].d_off = 4000;
+	ents[0].d_namlen = sizeof(foobar);
+	ents[0].d_type = DT_REG;
+	strncpy(ents[0].d_name, foobar, ents[0].d_namlen);
+	expect_readdir(ino, 0, ents);
+	expect_readdir(ino, 4000, empty_ents);
+
+	errno = 0;
+	dir = opendir(FULLPATH);
+	ASSERT_NE(nullptr, dir) << strerror(errno);
+
+	errno = 0;
+	de = readdir(dir);
+	ASSERT_NE(nullptr, de) << strerror(errno);
+	EXPECT_EQ(4ul, de->d_fileno);
+	EXPECT_EQ(DT_REG, de->d_type);
+	EXPECT_EQ(sizeof(foobar), de->d_namlen);
+	EXPECT_EQ(0, strcmp(foobar, de->d_name));
+
+	ASSERT_EQ(nullptr, readdir(dir));
+	ASSERT_EQ(0, errno);
+
+	leakdir(dir);
+}
+
 TEST_F(Readdir_7_8, nodots)
 {
 	const char FULLPATH[] = "mountpoint/some_dir";