From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 85804138350 for ; Sat, 25 Jan 2020 12:11:52 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id ADA08E0831; Sat, 25 Jan 2020 12:11:51 +0000 (UTC) Received: from smtp.gentoo.org (dev.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 927FCE0831 for ; Sat, 25 Jan 2020 12:11:51 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 6648634E2A7 for ; Sat, 25 Jan 2020 12:11:50 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id D75BBAC for ; Sat, 25 Jan 2020 12:11:48 +0000 (UTC) From: "Fabian Groffen" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Fabian Groffen" Message-ID: <1579954221.e612f0db4ea58be77ffd7b953ee3363831b61a59.grobian@gentoo> Subject: [gentoo-commits] proj/portage-utils:master commit in: / X-VCS-Repository: proj/portage-utils X-VCS-Files: qpkg.c X-VCS-Directories: / X-VCS-Committer: grobian X-VCS-Committer-Name: Fabian Groffen X-VCS-Revision: e612f0db4ea58be77ffd7b953ee3363831b61a59 X-VCS-Branch: master Date: Sat, 25 Jan 2020 12:11:48 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 1cd8d990-0a61-483b-8b46-26d92a45cf86 X-Archives-Hash: 37fb91cb7f4e86ee2c4fe7fd8abc1cae commit: e612f0db4ea58be77ffd7b953ee3363831b61a59 Author: Fabian Groffen gentoo org> AuthorDate: Sat Jan 25 12:10:21 2020 +0000 Commit: Fabian Groffen gentoo org> CommitDate: Sat Jan 25 12:10:21 2020 +0000 URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=e612f0db qpkg: try to fix Coverity 125940 Time of check time of use first perform the unlink, then open the object, and perform stat + chmod on it, if necessary Signed-off-by: Fabian Groffen gentoo.org> qpkg.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/qpkg.c b/qpkg.c index 3602acf..1b654a6 100644 --- a/qpkg.c +++ b/qpkg.c @@ -349,6 +349,7 @@ int qpkg_main(int argc, char **argv) depend_atom *atom; int restrict_chmod = 0; int qclean = 0; + int fd; qpkg_bindir = pkgdir; while ((i = GETOPT_LONG(QPKG, qpkg, "")) != -1) { @@ -374,20 +375,21 @@ int qpkg_main(int argc, char **argv) /* setup temp dirs */ if (qpkg_bindir[0] != '/') err("'%s' is not a valid package destination", qpkg_bindir); - for (i = 0; i <= 1; i++) { - if (mkdir(qpkg_bindir, 0750) == -1) { - if (lstat(qpkg_bindir, &st) == 0 && !S_ISDIR(st.st_mode)) { - unlink(qpkg_bindir); - continue; - } - if (!restrict_chmod) - if (chmod(qpkg_bindir, 0750)) - errp("could not chmod(0750) temp bindir '%s'", qpkg_bindir); - } - break; - } - if (i == 2) + /* brute force just unlink any file or symlink, if this fails, it's + * actually good ;) */ + unlink(qpkg_bindir); + fd = open(qpkg_bindir, O_RDONLY); + if ((fd == -1 && mkdir(qpkg_bindir, 0750) == -1) || + (fd != -1 && (fstat(fd, &st) == -1 || !S_ISDIR(st.st_mode)))) + { errp("could not create temp bindir '%s'", qpkg_bindir); + } else { + /* fd is valid, pointing to a directory */ + if (!restrict_chmod) + if (fchmod(fd, 0750) < 0) + errp("could not chmod(0750) temp bindir '%s'", qpkg_bindir); + } + close(fd); /* we have to change to the root so that we can feed the full paths * to tar when we create the binary package. */