Skip to contentSkip to conversation

client: strip trailing slashes when opening an ignored path

PATCH In review Ross Burton 2 participants 2 messages Apr 8, 2021 – Apr 9, 2021 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 client: strip trailing slashes when opening an ignored path

The pseudo client path map stores paths that have been sanitised, but in the ignored-path (PSEUDO_IGNORE_PATHS) codepath for open() calls this sanitising wasn't performed so it is possible for paths that end with a trailing slash to be entered.

This then subsequently interacts badly with path manipulation, resulting in the situation where doing:

fd = open("/some/path/")
parent_fd = openat(fd, "../)

results in parent_fd actually pointing at /some/path still.

Solve this by ensuring that any trailing slashes are removed from the path when adding to the map in the ignore short-circuit.

Also add a test case for this to ensure that it doesn't regress in the future.

---
 Makefile.in         |  1 +
 pseudo_client.c     | 11 ++++++++-
 test/test-openat.c  | 55 +++++++++++++++++++++++++++++++++++++++++++++
 test/test-openat.sh |  7 ++++++
 4 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100644 test/test-openat.c
 create mode 100755 test/test-openat.sh
Makefile.in +1 -0
index 6eff522..cf13010 100644
@@ -76,6 +76,7 @@ all: $(LIBPSEUDO) $(PSEUDO) $(PSEUDODB) $(PSEUDOLOG) $(PSEUDO_PROFILE)
 
 test: all | $(BIN) $(LIB) $(LOCALSTATE)
 	$(CC) $(CFLAGS) $(CFLAGS_PSEUDO) -o test/test-rename-fstat test/test-rename-fstat.c
+	$(CC) $(CFLAGS) $(CFLAGS_PSEUDO) -o test/test-openat test/test-openat.c
 	@./run_tests.sh -v
 
 install-lib: $(LIBPSEUDO)
pseudo_client.c +10 -1
index f846d54..579db33 100644
@@ -1629,7 +1629,16 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *path
 	if (op != OP_CHROOT && op != OP_CHDIR && op != OP_CLOSE && op != OP_DUP
 			&& pseudo_client_ignore_path_chroot(path, 0)) {
 		if (op == OP_OPEN) {
-			pseudo_client_path(fd, path);
+			/* Sanitise the path to have no trailing slash as this is convention in the database */
+			char *stripped_path;
+			pathlen = strlen(path);
+			if (pathlen > 2 && (path[pathlen - 1]) == '/') {
+				stripped_path = strndup(path, pathlen-1);
+				pseudo_client_path(fd, stripped_path);
+				free(stripped_path);
+			} else {
+				pseudo_client_path(fd, path);
+			}
 		}
 		/* reenable wrappers */
 		pseudo_magic();
test/test-openat.c new file +55 -0

55 lines not on this page · raw message

test/test-openat.sh new file +7 -0
new file mode 100755
index 0000000..0455586
@@ -0,0 +1,7 @@
+#! /bin/sh
+
+# Test with and without paths being ignored. The bug was with paths being ignored.
+
+./test/test-openat
+
+PSEUDO_IGNORE_PATHS=/ ./test/test-openat
Signature · 1 line
2.25.1
#1 lore raw
Quoted 20 lines · Ross Burton

The pseudo client path map stores paths that have been sanitised, but in the ignored-path (PSEUDO_IGNORE_PATHS) codepath for open() calls this sanitising wasn't performed so it is possible for paths that end with a trailing slash to be entered.

This then subsequently interacts badly with path manipulation, resulting in the situation where doing:

  fd = open("/some/path/")
  parent_fd = openat(fd, "../)

results in parent_fd actually pointing at /some/path still.

Solve this by ensuring that any trailing slashes are removed from the path when adding to the map in the ignore short-circuit.

Also add a test case for this to ensure that it doesn't regress in the future.

Quoted 8 lines

---

 Makefile.in         |  1 +
 pseudo_client.c     | 11 ++++++++-
 test/test-openat.c  | 55 +++++++++++++++++++++++++++++++++++++++++++++
 test/test-openat.sh |  7 ++++++
 4 files changed, 73 insertions(+), 1 deletion(-)

 create mode 100644 test/test-openat.c
 create mode 100755 test/test-openat.sh

Merged to the oe-core branch, thanks. Bonus kudos for a new test! :)

Cheers,

Richard