[PATCH v2] blkdiscard: check that the file is a block device before opening
Ari Sundholm
ari at tuxera.com
Tue Jan 5 11:55:06 UTC 2016
We need to enforce that the opened file is a block device, as
opening a file with the O_EXCL flag on and the O_CREAT flag off has
undefined behavior unless the file is a block device.
v2: Fix typo: O_CREAT, not O_CREATE.
Signed-off-by: Ari Sundholm <ari at tuxera.com>
---
util-linux/blkdiscard.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/util-linux/blkdiscard.c b/util-linux/blkdiscard.c
index ace88a1..af16e0f 100644
--- a/util-linux/blkdiscard.c
+++ b/util-linux/blkdiscard.c
@@ -38,7 +38,7 @@ int blkdiscard_main(int argc UNUSED_PARAM, char **argv)
uint64_t offset; /* Leaving these two variables out does not */
uint64_t length; /* shrink code size and hampers readability. */
uint64_t range[2];
-// struct stat st;
+ struct stat st;
int fd;
enum {
@@ -51,11 +51,15 @@ int blkdiscard_main(int argc UNUSED_PARAM, char **argv)
opts = getopt32(argv, "o:l:s", &offset_str, &length_str);
argv += optind;
+ /* We need to enforce that the opened file is a block device, as
+ * opening a file with the O_EXCL flag on and the O_CREAT flag off has
+ * undefined behavior unless the file is a block device.
+ */
+ xstat(argv[0], &st);
+ if (!S_ISBLK(st.st_mode))
+ bb_error_msg_and_die("%s: not a block device", argv[0]);
+
fd = xopen(argv[0], O_RDWR|O_EXCL);
-//Why bother, BLK[SEC]DISCARD will fail on non-blockdevs anyway?
-// xfstat(fd, &st);
-// if (!S_ISBLK(st.st_mode))
-// bb_error_msg_and_die("%s: not a block device", argv[0]);
offset = xatoull_sfx(offset_str, kMG_suffixes);
--
1.9.1
More information about the busybox
mailing list