]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
swapon: fix stat(3) and open(3) race
authorSami Kerola <kerolasa@iki.fi>
Sat, 31 Oct 2015 19:21:14 +0000 (19:21 +0000)
committerKarel Zak <kzak@redhat.com>
Mon, 9 Nov 2015 08:56:14 +0000 (09:56 +0100)
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
sys-utils/swapon.c

index 32a3107fd697268b078e26fd3b392de4653cbd58..c4d15442059f63d62d93d9586367ba4df3ffb625 100644 (file)
@@ -475,7 +475,13 @@ static int swapon_checks(const char *special)
        unsigned long long devsize = 0;
        int permMask;
 
-       if (stat(special, &st) < 0) {
+       fd = open(special, O_RDONLY);
+       if (fd == -1) {
+               warn(_("cannot open %s"), special);
+               goto err;
+       }
+
+       if (fstat(fd, &st) < 0) {
                warn(_("stat of %s failed"), special);
                goto err;
        }
@@ -500,12 +506,6 @@ static int swapon_checks(const char *special)
                devsize = st.st_size;
        }
 
-       fd = open(special, O_RDONLY);
-       if (fd == -1) {
-               warn(_("cannot open %s"), special);
-               goto err;
-       }
-
        if (S_ISBLK(st.st_mode) && blkdev_get_size(fd, &devsize)) {
                warnx(_("%s: get size failed"), special);
                goto err;