From owner-svn-src-head@freebsd.org Sat Feb 15 19:47:50 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 5E28A246AD7; Sat, 15 Feb 2020 19:47:50 +0000 (UTC) (envelope-from kevans@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) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 48KglB1lWRz3FWZ; Sat, 15 Feb 2020 19:47:50 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 363C91A08B; Sat, 15 Feb 2020 19:47:50 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 01FJloF2005198; Sat, 15 Feb 2020 19:47:50 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 01FJloGc005197; Sat, 15 Feb 2020 19:47:50 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <202002151947.01FJloGc005197@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Sat, 15 Feb 2020 19:47:50 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r357979 - head/lib/libfetch X-SVN-Group: head X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: head/lib/libfetch X-SVN-Commit-Revision: 357979 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 15 Feb 2020 19:47:50 -0000 Author: kevans Date: Sat Feb 15 19:47:49 2020 New Revision: 357979 URL: https://svnweb.freebsd.org/changeset/base/357979 Log: fetch(3): don't leak sockshost on failure fetch_socks5_getenv will allocate memory for the host (or set it to NULL) in all cases through the function; the caller is responsible for freeing it if we end up allocating. While I'm here, I've eliminated a label that just jumps to the next line... Modified: head/lib/libfetch/common.c Modified: head/lib/libfetch/common.c ============================================================================== --- head/lib/libfetch/common.c Sat Feb 15 19:39:50 2020 (r357978) +++ head/lib/libfetch/common.c Sat Feb 15 19:47:49 2020 (r357979) @@ -557,8 +557,10 @@ fetch_socks5_getenv(char **host, int *port) *host = strndup(socks5env, ext - socks5env); } - if (*host == NULL) - goto fail; + if (*host == NULL) { + fprintf(stderr, "Failure to allocate memory, exiting.\n"); + return (-1); + } if (ext == NULL) { *port = 1080; /* Default port as defined in RFC1928 */ } else { @@ -567,16 +569,14 @@ fetch_socks5_getenv(char **host, int *port) *port = strtoimax(ext, (char **)&endptr, 10); if (*endptr != '\0' || errno != 0 || *port < 0 || *port > 65535) { + free(*host); + *host = NULL; socks5_seterr(SOCKS5_ERR_BAD_PORT); return (0); } } return (2); - -fail: - fprintf(stderr, "Failure to allocate memory, exiting.\n"); - return (-1); } @@ -595,7 +595,11 @@ fetch_connect(const char *host, int port, int af, int DEBUGF("---> %s:%d\n", host, port); - /* Check if SOCKS5_PROXY env variable is set */ + /* + * Check if SOCKS5_PROXY env variable is set. fetch_socks5_getenv + * will either set sockshost = NULL or allocate memory in all cases. + */ + sockshost = NULL; if (!fetch_socks5_getenv(&sockshost, &socksport)) goto fail; @@ -662,7 +666,7 @@ fetch_connect(const char *host, int port, int af, int "failed to connect to SOCKS5 server %s:%d", sockshost, socksport); socks5_seterr(SOCKS5_ERR_CONN_REFUSED); - goto syserr1; + goto fail; } goto syserr; } @@ -680,9 +684,8 @@ fetch_connect(const char *host, int port, int af, int return (conn); syserr: fetch_syserr(); -syserr1: - goto fail; fail: + free(sockshost); if (sd >= 0) close(sd); if (cais != NULL)