path: root/disk-io.c
diff options
authorAustin S. Hemmelgarn <ahferroin7@gmail.com>2016-03-18 10:03:42 -0400
committerDavid Sterba <dsterba@suse.com>2016-03-18 16:38:14 +0100
commit3519f83574c29a49e7e3e493f1bf4bb8dd5b0e33 (patch)
tree65e11502cc23e289b4776af5700b859c97d7298e /disk-io.c
parentf7fb93d558e4844930a1c74683e9a4725c058192 (diff)
btrfs-progs: add stat check in open_ctree_fs_info
Currently, open_ctree_fs_info will open whatever path you pass it and try to interpret it as a BTRFS filesystem. While this is not nessecarily dangerous (except possibly if done on a character device), it does result in some rather cryptic and non-sensical error messages when trying to run certain commands in ways they weren't intended to be run. Add a check using stat(2) to verify that the path we've been passed is in fact a regular file or a block device, or a symlink pointing to a regular file or block device. This causes the following commands to provide a helpful error message when run on a FIFO, directory, character device, or socket: * btrfs check * btrfs restore * btrfs-image * btrfs-find-root * btrfs inspect-internal dump-tree stat(2) is used instead of lstat(2), as stat(2) follows symlinks just like open(2) does, which means we check the same inode that open(2) opens, and thus don't need special handling for symlinks. Signed-off-by: Austin S. Hemmelgarn <ahferroin7@gmail.com> Signed-off-by: David Sterba <dsterba@suse.com>
Diffstat (limited to 'disk-io.c')
1 files changed, 7 insertions, 0 deletions
diff --git a/disk-io.c b/disk-io.c
index 88015b7..6b47977 100644
--- a/disk-io.c
+++ b/disk-io.c
@@ -1325,6 +1325,13 @@ struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
int fp;
struct btrfs_fs_info *info;
int oflags = O_CREAT | O_RDWR;
+ struct stat st;
+ stat(filename, &st);
+ if (!(((st.st_mode & S_IFMT) == S_IFREG) || ((st.st_mode & S_IFMT) == S_IFBLK))) {
+ fprintf (stderr, "%s is not a regular file or block device\n", filename);
+ return NULL;
+ }
if (!(flags & OPEN_CTREE_WRITES))
oflags = O_RDONLY;