git: 69c72a57af84 - main - sftp: avoid leaking path arg in calls to make_absolute_pwd_glob

From: Ed Maste <emaste_at_FreeBSD.org>
Date: Wed, 22 Mar 2023 15:05:43 UTC
The branch main has been updated by emaste:

URL: https://cgit.FreeBSD.org/src/commit/?id=69c72a57af843267b220f8367c4cc7162a12d696

commit 69c72a57af843267b220f8367c4cc7162a12d696
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2022-11-03 17:17:40 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2023-03-22 14:54:35 +0000

    sftp: avoid leaking path arg in calls to make_absolute_pwd_glob
    
    As Coverity reports:
        Overwriting tmp in tmp = make_absolute_pwd_glob(tmp, remote_path)
        leaks the storage that tmp points to.
    
    Consume the first arg in make_absolute_pwd_glob, and add xstrdup() to
    the one case which did not assign to the same variable that was passed
    in. With this change make_absolute() and make_absolute_pwd_glob() have
    the same semantics with respect to freeing the input string.
    
    This change was reported to OpenSSH in
    https://lists.mindrot.org/pipermail/openssh-unix-dev/2022-November/040497.html
    but was not acted on. It appears that OpenBSD subsequently received a
    Coverity report for the same issue (their Coverity ID 405196) but fixed
    only the specific instance reported by Coverity.
    
    This change reverts OpenBSD's sftp.c 1.228 / OpenSSH-portable
    commit 36c6c3eff5e4.
    
    Reported by:    Coverity Scan
    CID:            1500409
    Reviewed by:    markj
    MFC after:      1 month
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D37253
---
 crypto/openssh/sftp.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/crypto/openssh/sftp.c b/crypto/openssh/sftp.c
index 29081db3d434..ce7ab7f8f2fe 100644
--- a/crypto/openssh/sftp.c
+++ b/crypto/openssh/sftp.c
@@ -617,14 +617,14 @@ escape_glob(const char *s)
 }
 
 static char *
-make_absolute_pwd_glob(const char *p, const char *pwd)
+make_absolute_pwd_glob(char *p, const char *pwd)
 {
 	char *ret, *escpwd;
 
 	escpwd = escape_glob(pwd);
 	if (p == NULL)
 		return escpwd;
-	ret = make_absolute(xstrdup(p), escpwd);
+	ret = make_absolute(p, escpwd);
 	free(escpwd);
 	return ret;
 }
@@ -637,7 +637,7 @@ process_get(struct sftp_conn *conn, const char *src, const char *dst,
 	glob_t g;
 	int i, r, err = 0;
 
-	abs_src = make_absolute_pwd_glob(src, pwd);
+	abs_src = make_absolute_pwd_glob(xstrdup(src), pwd);
 	memset(&g, 0, sizeof(g));
 
 	debug3("Looking up %s", abs_src);
@@ -1997,9 +1997,7 @@ complete_match(EditLine *el, struct sftp_conn *conn, char *remote_path,
 
 	memset(&g, 0, sizeof(g));
 	if (remote != LOCAL) {
-		tmp2 = make_absolute_pwd_glob(tmp, remote_path);
-		free(tmp);
-		tmp = tmp2;
+		tmp = make_absolute_pwd_glob(tmp, remote_path);
 		remote_glob(conn, tmp, GLOB_DOOFFS|GLOB_MARK, NULL, &g);
 	} else
 		glob(tmp, GLOB_DOOFFS|GLOB_MARK, NULL, &g);