[openssh-commits] [openssh] 13/14: upstream: run local sftp "lls" command using

git+noreply at mindrot.org git+noreply at mindrot.org
Thu Nov 26 10:20:58 AEDT 2026


This is an automated email from the git hooks/post-receive script.

djm pushed a commit to branch master
in repository openssh.

commit 9e4fbc60413b28b6152989ea7db0bf3c41d52d3f
Author: djm at openbsd.org <djm at openbsd.org>
AuthorDate: Wed Nov 25 22:58:30 2026 +0000

    upstream: run local sftp "lls" command using
    
    makeargv()+glob()+exec rather than system(); avoids situation where a user
    could do a tab expansion in a directory with adversarial content and end up
    executing a commandline with backticks, etc in it
    
    Anthropic ANT-2026-QFGM82BB
    
    OpenBSD-Commit-ID: c54d801d0a3e51cbd6756eee45f1a0d207bbb8d1
---
 sftp.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 55 insertions(+), 17 deletions(-)

diff --git a/sftp.c b/sftp.c
index 82fbf7e40..fb115fa7b 100644
--- a/sftp.c
+++ b/sftp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sftp.c,v 1.266 2026/11/25 20:48:31 djm Exp $ */
+/* $OpenBSD: sftp.c,v 1.267 2026/11/25 22:58:30 djm Exp $ */
 /*
  * Copyright (c) 2001-2004 Damien Miller <djm at openbsd.org>
  *
@@ -347,22 +347,6 @@ local_do_shell(const char *args)
 		error("Shell exited with status %d", WEXITSTATUS(status));
 }
 
-static void
-local_do_ls(const char *args)
-{
-	if (!args || !*args)
-		local_do_shell(_PATH_LS);
-	else {
-		int len = strlen(_PATH_LS " ") + strlen(args) + 1;
-		char *buf = xmalloc(len);
-
-		/* XXX: quoting - rip quoting code from ftp? */
-		snprintf(buf, len, _PATH_LS " %s", args);
-		local_do_shell(buf);
-		free(buf);
-	}
-}
-
 /* Strip one path (usually the pwd) from the start of another */
 static char *
 path_strip(const char *path, const char *strip)
@@ -1379,6 +1363,60 @@ makeargv(const char *arg, int *argcp, int sloppy, char *lastquote,
 	return argv;
 }
 
+/*
+ * Run ls(1) on the local system. The arguments use the same quoting and
+ * glob(3) rules as every other command (and so match what filename
+ * completion produces); they are never passed to a shell, which would
+ * interpret them differently.
+ */
+static void
+local_do_ls(const char *args)
+{
+	char **argv;
+	int i, j, argc, status;
+	arglist ls_args;
+	glob_t g;
+	pid_t pid;
+
+	if ((argv = makeargv(args, &argc, 0, NULL, NULL)) == NULL)
+		return;
+
+	memset(&ls_args, 0, sizeof(ls_args));
+	addargs(&ls_args, "%s", _PATH_LS);
+	for (i = 0; i < argc; i++) {
+		memset(&g, 0, sizeof(g));
+		/* GLOB_NOCHECK: options and non-matching words pass through */
+		if (glob(argv[i], GLOB_NOCHECK|GLOB_TILDE, NULL, &g) != 0) {
+			error("Couldn't expand \"%s\"", argv[i]);
+			globfree(&g);
+			freeargs(&ls_args);
+			return;
+		}
+		for (j = 0; g.gl_pathv[j] != NULL; j++)
+			addargs(&ls_args, "%s", g.gl_pathv[j]);
+		globfree(&g);
+	}
+
+	if ((pid = fork()) == -1)
+		fatal("Couldn't fork: %s", strerror(errno));
+
+	if (pid == 0) {
+		execvp(_PATH_LS, ls_args.list);
+		fprintf(stderr, "Couldn't execute \"%s\": %s\n", _PATH_LS,
+		    strerror(errno));
+		_exit(1);
+	}
+	freeargs(&ls_args);
+	while (waitpid(pid, &status, 0) == -1)
+		if (errno != EINTR)
+			fatal("Couldn't wait for child: %s", strerror(errno));
+	if (!WIFEXITED(status))
+		error("%s exited abnormally", _PATH_LS);
+	else if (WEXITSTATUS(status))
+		error("%s exited with status %d", _PATH_LS,
+		    WEXITSTATUS(status));
+}
+
 static int
 parse_args(const char **cpp, int *ignore_errors, int *disable_echo, int *aflag,
 	  int *fflag, int *hflag, int *iflag, int *lflag, int *pflag,

-- 
To stop receiving notification emails like this one, please contact
djm at mindrot.org.


More information about the openssh-commits mailing list