From: Austin S. Hemmelgarn Date: Fri, 18 Mar 2016 14:03:42 +0000 (-0400) Subject: btrfs-progs: add stat check in open_ctree_fs_info X-Git-Tag: upstream/4.16.1~1659 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3519f83574c29a49e7e3e493f1bf4bb8dd5b0e33;p=platform%2Fupstream%2Fbtrfs-progs.git 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 Signed-off-by: David Sterba --- 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;