diff mbox series

[pseudo,09/20] ports/unix/guts/realpath.c: realpath fails if the resolved path doesn't exist

Message ID 1768520616-7289-10-git-send-email-mark.hatle@kernel.crashing.org
State New
Headers show
Series Consolidated pseudo patches | expand

Commit Message

Mark Hatle Jan. 15, 2026, 11:43 p.m. UTC
From: Gauthier HADERER <ghaderer@wyplay.com>

The pseudo implementation of `realpath()' may return a path which doesn't
exist. This is not POSIX compliant and causes troubles with uutils (coreutils
in Rust).

For example, the tail commands tries to determine the file path of its standard
input file descriptor by calling `realpath("/dev/fd/0")'. When the input is a
pipe, the GNU C library returns NULL but pseudo returns
`/proc/<pid>/fd/pipe:[xxxxxx]'. As it got a path, the tail command tries to
open it and fails.

Contributed-by: Gauthier HADERER <ghaderer@wyplay.com> via
yocto-patches@lists.yoctoproject.org

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

Patch

diff --git a/ports/unix/guts/realpath.c b/ports/unix/guts/realpath.c
index 8d8118b..c13eb93 100644
--- a/ports/unix/guts/realpath.c
+++ b/ports/unix/guts/realpath.c
@@ -14,6 +14,15 @@ 
 		errno = ENAMETOOLONG;
 		return NULL;
 	}
+
+	/* We must fail if the target path doesn't exist. */
+	PSEUDO_STATBUF buf;
+
+	if (base_lstat(rname, &buf) == -1) {
+		errno = EINVAL;
+		return NULL;
+	}
+
 		len = strlen(rname);
 		char *ep = rname + len - 1;
 		while (ep > rname && *ep == '/') {