From nobody Tue Nov 17 11:44:35 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jTNF045R5z71J2Q for ; Tue, 17 Nov 2026 11:44:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jTNF01Tpjz4Fhy for ; Tue, 17 Nov 2026 11:44:36 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794915876; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=tfsmgmf+DajyVnmZaD+fSBu7JhaMXvopkNtDoW3ob+M=; b=WjmCHMmSqbvnmoqYmOIRwOeoIL7G78YMyYkVZaAiDLBcabi9UNs5RE6KmAtQzJ4x67nbih O++dndabENsqYPPxOau/PC2dIKFuKZt9Wn+7HKLXBozDRwI1+2CoDgMdvT/Qe5teAlRgno /JvqfistdiA4jYkNd1OTPGMqgfh5lRU0X4hKqwUMzF8+xzpWna1LRJLVsmyu8brtq9xumj 2q+5AG6FYMr0tvJATAHHlWLPzZlJrMfR5TVtEQUmR9NLPUjAXbvEpXDfFzhiNBFFJ+kYG4 D8Iiqtg3deVQpbS5eFhBrzlK01BEpbqadH5p/tqlhik+U7PGc2L9y1bs1fl7hw== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794915876; b=mlNIdXkdk+qaZ9etiIJvF4VWKXnNZZFC/5Lm7ZnONpY2fz5VLTd4pv9aAVjaIkgbP7PjR7 pGZL6lwt7yz4TxpJ6uyoXR4IgcD+yTAyZMBLjfj7HLCIfGwlCG+JDbtNUZys2bkdu2OuDq eUw8hfUYJxNHWHJeKJCQbTSNmGDfqrahQUF6/n2S4AKYnG0jnuJ/z9nXz5+U7HgERc9lys Hh4Q5PXl7dcHLnuhHexD4hEbya3FdHRPBPStyBKTukg7UteKYeeAufj9za28SoaF7gythI /6TtfuxGQLZsRPV7ZvZqKB4Vcyw8zRMJ0Gv0aCze5m7MS0+Mof/CHa5hfskdmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794915876; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=tfsmgmf+DajyVnmZaD+fSBu7JhaMXvopkNtDoW3ob+M=; b=ZkkE3BmiPOv0hyBoaZTSZ1+C8RTcZ4Qg9xlpK7w0VsrTZ9OBvzGXlseri5eC7w7I24yObo KFE2UJZRMO+2Z+c8wgYRXU7Dmvt2t2eZ2vqUfpDmhURz5v6U96Wvz+THetc8NjqKzC64FH 1uKYG1RQaBHowuvXgNEA2oK+nyUJqmkDGGVBfwTxO2vnKRtAxTrr6EdhHfGJWl6sQCw/qJ BroFX+4Ln0COmNXyU22lNV3jVFVRjbVJrAapU3aLFpYfs++JVyUBkLGukQH+cmgWo7FMcd AQlypEfJ/SuOR0bwdCIV9XV8wRw9iXYT5vLDSrZuPntfRAKbE911ZSSr9LymeA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jTNF00ZXmzgvg for ; Tue, 17 Nov 2026 11:44:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1bfc6 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 17 Nov 2026 11:44:35 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling=?utf-8?Q? Sm=C3=B8rg?=rav Subject: git: 243ac45800de - main - libfetch: Fix buffer overruns in FTP code List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 243ac45800de387065246271b13f5b5cb3f6c857 Auto-Submitted: auto-generated Date: Tue, 17 Nov 2026 11:44:35 +0000 Message-Id: <6afc3e23.1bfc6.a0eec3d@gitrepo.freebsd.org> The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=243ac45800de387065246271b13f5b5cb3f6c857 commit 243ac45800de387065246271b13f5b5cb3f6c857 Author: Dag-Erling Smørgrav AuthorDate: 2026-11-17 11:44:02 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2026-11-17 11:44:02 +0000 libfetch: Fix buffer overruns in FTP code * When parsing the reply to a CWD, PWD, or CDUP command, if we ran out of space in the destination buffer, we would correctly terminate the loop, but then incorrectly see the remaining length as non-zero, causing us to write a terminating NUL one past the end of the buffer and report success instead of a protocol error. Additionally, the code would fail to notice an improperly terminated pathname, and was hard to follow, so rewrite the entire loop. * When comparing the current working directory to the intended working directory to calculate the minimal operation to get from one to the other, if the current working directory was an empty string, we would improperly read one byte below the start of the intended working directory. MFC after: 1 week Reported by: Laam Alif at Ibteda Digital Library Differential Revision: https://reviews.freebsd.org/D61502 --- lib/libfetch/ftp.c | 59 +++++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/lib/libfetch/ftp.c b/lib/libfetch/ftp.c index 14abf97b827f..19780dcf154d 100644 --- a/lib/libfetch/ftp.c +++ b/lib/libfetch/ftp.c @@ -66,6 +66,7 @@ #include #include #include +#include #include #include #include @@ -225,38 +226,44 @@ ftp_filename(const char *file, int *len, int *type) } /* - * Get current working directory from the reply to a CWD, PWD or CDUP - * command. + * Get current working directory from the reply to a PWD command. + * + * The server reply is of the form `XYZ "pathname" comment` where XYZ is a + * result code which has already been checked and stored in conn->err and + * "pathname" is the double-quoted current working directory, which may be + * empty. Any double-quote characters occurring in the pathname must be + * doubled to distinguish them from the double-quote character at the end. */ static int -ftp_pwd(conn_t *conn, char *pwd, size_t pwdlen) +ftp_pwd(conn_t *conn, char *dst, size_t dstlen) { - char *src, *dst, *end; - int q; + char *src, *end; + bool dq = false; - if (conn->err != FTP_WORKING_DIRECTORY && - conn->err != FTP_FILE_ACTION_OK) + if (conn->err != FTP_WORKING_DIRECTORY || + conn->linelen < 6 || conn->line[4] != '"') return (FTP_PROTOCOL_ERROR); end = conn->line + conn->linelen; - src = conn->line + 4; - if (src >= end || *src++ != '"') - return (FTP_PROTOCOL_ERROR); - for (q = 0, dst = pwd; src < end && pwdlen--; ++src) { - if (!q && *src == '"') - q = 1; - else if (q && *src != '"') + for (src = conn->line + 5; src < end; ++src) { + if (!dq && *src == '"') { + /* either the first of two or the end */ + dq = true; + continue; + } + if (dq && *src != '"') { + /* the end */ break; - else if (q) - *dst++ = '"', q = 0; - else - *dst++ = *src; + } + /* need space for at list this + null */ + if (dstlen < 2) + return (FTP_PROTOCOL_ERROR); + *dst++ = dq ? '"' : *src; + dq = false; + dstlen--; } - if (!pwdlen) + if (!dq) return (FTP_PROTOCOL_ERROR); *dst = '\0'; -#if 0 - DEBUGF("pwd: [%s]\n", pwd); -#endif return (FTP_OK); } @@ -286,12 +293,10 @@ ftp_cwd(conn_t *conn, const char *file) for (i = 0; i <= len && i <= end - file; ++i) if (pwd[i] != file[i]) break; -#if 0 - DEBUGF("have: [%.*s|%s]\n", i, pwd, pwd + i); - DEBUGF("want: [%.*s|%s]\n", i, file, file + i); -#endif + /* Keep going up a dir until we have a matching prefix. */ - if (pwd[i] == '\0' && (file[i - 1] == '/' || file[i] == '/')) + if (pwd[i] == '\0' && + (file[i] == '/' || (i > 0 && file[i - 1] == '/'))) break; if ((e = ftp_cmd(conn, "CDUP")) != FTP_FILE_ACTION_OK || (e = ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY ||