From owner-svn-src-head@freebsd.org Fri Apr 14 00:14:42 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 14321D3CC23; Fri, 14 Apr 2017 00:14:42 +0000 (UTC) (envelope-from cem@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 mx1.freebsd.org (Postfix) with ESMTPS id C0A54EA8; Fri, 14 Apr 2017 00:14:41 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v3E0EeBE015640; Fri, 14 Apr 2017 00:14:40 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v3E0EeC2015639; Fri, 14 Apr 2017 00:14:40 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201704140014.v3E0EeC2015639@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: Conrad Meyer Date: Fri, 14 Apr 2017 00:14:40 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r316799 - head/sbin/restore X-SVN-Group: head 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.23 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: Fri, 14 Apr 2017 00:14:42 -0000 Author: cem Date: Fri Apr 14 00:14:40 2017 New Revision: 316799 URL: https://svnweb.freebsd.org/changeset/base/316799 Log: restore(8): Prevent some heap overflows The environment variable TMPDIR was copied unchecked into a fixed-size heap buffer. Use a length-limiting snprintf in place of ordinary sprintf to prevent the overflow. Long TMPDIR variables can still cause odd truncated filenames, which may be undesirable. Reported by: Coverity (CWE-120) CIDs: 1006706, 1006707 Sponsored by: Dell EMC Isilon Modified: head/sbin/restore/dirs.c Modified: head/sbin/restore/dirs.c ============================================================================== --- head/sbin/restore/dirs.c Fri Apr 14 00:13:33 2017 (r316798) +++ head/sbin/restore/dirs.c Fri Apr 14 00:14:40 2017 (r316799) @@ -140,7 +140,8 @@ extractdirs(int genmode) vprintf(stdout, "Extract directories from tape\n"); if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0') tmpdir = _PATH_TMP; - (void) sprintf(dirfile, "%s/rstdir%jd", tmpdir, (intmax_t)dumpdate); + (void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%jd", tmpdir, + (intmax_t)dumpdate); if (command != 'r' && command != 'R') { (void) strcat(dirfile, "-XXXXXX"); fd = mkstemp(dirfile); @@ -153,8 +154,8 @@ extractdirs(int genmode) done(1); } if (genmode != 0) { - (void) sprintf(modefile, "%s/rstmode%jd", tmpdir, - (intmax_t)dumpdate); + (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%jd", + tmpdir, (intmax_t)dumpdate); if (command != 'r' && command != 'R') { (void) strcat(modefile, "-XXXXXX"); fd = mkstemp(modefile); @@ -568,8 +569,8 @@ setdirmodes(int flags) if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0') tmpdir = _PATH_TMP; if (command == 'r' || command == 'R') - (void) sprintf(modefile, "%s/rstmode%jd", tmpdir, - (intmax_t)dumpdate); + (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%jd", + tmpdir, (intmax_t)dumpdate); if (modefile[0] == '#') { panic("modefile not defined\n"); fprintf(stderr, "directory mode, owner, and times not set\n");