diff options
author | Sami Kerola | 2015-10-31 20:21:14 +0100 |
---|---|---|
committer | Karel Zak | 2015-11-09 09:56:14 +0100 |
commit | 8df370d340a9c4aceb36662aeecd63d0c08b44c8 (patch) | |
tree | 37ac99c745b6e7b255aa02ce4b65c65165b4c650 /sys-utils/swapon.c | |
parent | login: fix stat(2) race (diff) | |
download | kernel-qcow2-util-linux-8df370d340a9c4aceb36662aeecd63d0c08b44c8.tar.gz kernel-qcow2-util-linux-8df370d340a9c4aceb36662aeecd63d0c08b44c8.tar.xz kernel-qcow2-util-linux-8df370d340a9c4aceb36662aeecd63d0c08b44c8.zip |
swapon: fix stat(3) and open(3) race
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'sys-utils/swapon.c')
-rw-r--r-- | sys-utils/swapon.c | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/sys-utils/swapon.c b/sys-utils/swapon.c index 32a3107fd..c4d154420 100644 --- a/sys-utils/swapon.c +++ b/sys-utils/swapon.c @@ -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; |