[PATCH] staprun: Check getcwd() before using output path

Mikhail Dmitrichenko m.dmitrichenko222@gmail.com
Tue Jun 16 14:33:59 GMT 2026


get_abspath() assumes that getcwd() succeeds and passes its result
directly to strlen(). If staprun is started from a removed or
otherwise unreachable current working directory, getcwd() can fail and
return NULL, causing a crash while parsing a relative -o path.

Check the return value before using it, preserve errno for callers, and
report the failure with perror-style diagnostics. Keep ENAMETOOLONG for
the existing path length failure so that callers still get a useful
error.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Signed-off-by: Mikhail Dmitrichenko <m.dmitrichenko222@gmail.com>
---
 staprun/common.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/staprun/common.c b/staprun/common.c
index 901875259..b83b44e4d 100644
--- a/staprun/common.c
+++ b/staprun/common.c
@@ -62,13 +62,21 @@ int control_channel = -1; /* NB: fd==0 possible */
 static char path_buf[PATH_MAX];
 static char *get_abspath(char *path)
 {
+	char *cwd;
 	int len;
+
 	if (path[0] == '/')
 		return path;
 
-	len = strlen(getcwd(path_buf, PATH_MAX));
-	if (len + 2 + strlen(path) >= PATH_MAX)
+	cwd = getcwd(path_buf, PATH_MAX);
+	if (cwd == NULL)
+		return NULL;
+
+	len = strlen(cwd);
+	if (len + 2 + strlen(path) >= PATH_MAX) {
+		errno = ENAMETOOLONG;
 		return NULL;
+	}
 	path_buf[len] = '/';
 	/* Note that this strcpy() call is OK, since we checked
 	 * the length earlier to make sure the string would fit. */
@@ -279,7 +287,7 @@ void parse_args(int argc, char **argv)
 		int ret;
 		outfile_name = get_abspath(outfile_name);
 		if (outfile_name == NULL) {
-			err(_("File name is too long.\n"));
+			perr(_("Unable to make output file name absolute"));
 			usage(argv[0],1);
 		}
 		ret = stap_strfloctime(tmp, PATH_MAX - 21,
-- 
2.43.0



More information about the Systemtap mailing list