Gentoo Archives: gentoo-commits

From: Fabian Groffen <grobian@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/portage-utils:master commit in: /
Date: Sat, 25 Jan 2020 12:11:52
Message-Id: 1579954221.e612f0db4ea58be77ffd7b953ee3363831b61a59.grobian@gentoo
1 commit: e612f0db4ea58be77ffd7b953ee3363831b61a59
2 Author: Fabian Groffen <grobian <AT> gentoo <DOT> org>
3 AuthorDate: Sat Jan 25 12:10:21 2020 +0000
4 Commit: Fabian Groffen <grobian <AT> gentoo <DOT> org>
5 CommitDate: Sat Jan 25 12:10:21 2020 +0000
6 URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=e612f0db
7
8 qpkg: try to fix Coverity 125940 Time of check time of use
9
10 first perform the unlink, then open the object, and perform stat + chmod
11 on it, if necessary
12
13 Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>
14
15 qpkg.c | 28 +++++++++++++++-------------
16 1 file changed, 15 insertions(+), 13 deletions(-)
17
18 diff --git a/qpkg.c b/qpkg.c
19 index 3602acf..1b654a6 100644
20 --- a/qpkg.c
21 +++ b/qpkg.c
22 @@ -349,6 +349,7 @@ int qpkg_main(int argc, char **argv)
23 depend_atom *atom;
24 int restrict_chmod = 0;
25 int qclean = 0;
26 + int fd;
27
28 qpkg_bindir = pkgdir;
29 while ((i = GETOPT_LONG(QPKG, qpkg, "")) != -1) {
30 @@ -374,20 +375,21 @@ int qpkg_main(int argc, char **argv)
31 /* setup temp dirs */
32 if (qpkg_bindir[0] != '/')
33 err("'%s' is not a valid package destination", qpkg_bindir);
34 - for (i = 0; i <= 1; i++) {
35 - if (mkdir(qpkg_bindir, 0750) == -1) {
36 - if (lstat(qpkg_bindir, &st) == 0 && !S_ISDIR(st.st_mode)) {
37 - unlink(qpkg_bindir);
38 - continue;
39 - }
40 - if (!restrict_chmod)
41 - if (chmod(qpkg_bindir, 0750))
42 - errp("could not chmod(0750) temp bindir '%s'", qpkg_bindir);
43 - }
44 - break;
45 - }
46 - if (i == 2)
47 + /* brute force just unlink any file or symlink, if this fails, it's
48 + * actually good ;) */
49 + unlink(qpkg_bindir);
50 + fd = open(qpkg_bindir, O_RDONLY);
51 + if ((fd == -1 && mkdir(qpkg_bindir, 0750) == -1) ||
52 + (fd != -1 && (fstat(fd, &st) == -1 || !S_ISDIR(st.st_mode))))
53 + {
54 errp("could not create temp bindir '%s'", qpkg_bindir);
55 + } else {
56 + /* fd is valid, pointing to a directory */
57 + if (!restrict_chmod)
58 + if (fchmod(fd, 0750) < 0)
59 + errp("could not chmod(0750) temp bindir '%s'", qpkg_bindir);
60 + }
61 + close(fd);
62
63 /* we have to change to the root so that we can feed the full paths
64 * to tar when we create the binary package. */