diff mbox series

[pseudo,08/11] ports/unix: realpath: Fix chroot processing

Message ID 1777312601-1393-9-git-send-email-mark.hatle@kernel.crashing.org
State New
Headers show
Series Various fixes, release 1.9.6 | expand

Commit Message

Mark Hatle April 27, 2026, 5:56 p.m. UTC
From: Mark Hatle <mark.hatle@amd.com>

When running realpath from within a chroot, the returned path must be
sanitized to appear as if it's within the chroot.  Use the existing
pseudo_chroot settings to identify and clear the path.

AI-Generated: Fix suggested by github copilot (claude opus 4.6)

Signed-off-by: Mark Hatle <mark.hatle@amd.com>
Signed-off-by: Mark Hatle <mark.hatle@kernel.crashing.org>
---
 ports/unix/guts/realpath.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
diff mbox series

Patch

diff --git a/ports/unix/guts/realpath.c b/ports/unix/guts/realpath.c
index 62a92b2..4f91220 100644
--- a/ports/unix/guts/realpath.c
+++ b/ports/unix/guts/realpath.c
@@ -30,6 +30,21 @@ 
 		*(ep--) = '\0';
 	}
 
+	/* If in a chroot, strip the chroot prefix so the caller sees
+	 * a path relative to the chroot root.
+	 */
+	if (pseudo_chroot_len &&
+	    (size_t)len >= pseudo_chroot_len &&
+	    !memcmp(rname, pseudo_chroot, pseudo_chroot_len) &&
+	    (rname[pseudo_chroot_len] == '/' || rname[pseudo_chroot_len] == '\0')) {
+		rname += pseudo_chroot_len;
+		len -= pseudo_chroot_len;
+		if (len == 0) {
+			rname = "/";
+			len = 1;
+		}
+	}
+
 	if (len >= pseudo_sys_path_max()) {
 		errno = ENAMETOOLONG;
 		return NULL;