btrfs-progs: add stat check in open_ctree_fs_info
PATCH 1/1 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.
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-debug-tree
---
This has been build and runtime tested on an x86-64 system with glibc. It has been build tested on x86-64 with uclibc.
It has not been tested on Android or with musl, although it should work there also.
There are other tools that have similarly bad behavior when called incorrectly (btrfs rescue immediately comes to mind), but they don't use open_ctree_fs_info, so this doesn't affect them. I may do followup patches to fix those too if I have the time.
open_ctree_fs_info is also used in cmds-filesystem.c, although I'm not at all sure what exactly is going on there, and btrfs filesystem appears from my testing to behave exactly the same with this change, so I don't think this will have any effect on any of the btrfs filesystem commands.
disk-io.c | 7 +++++++ 1 file changed, 7 insertions(+)
disk-io.c +7 -0
@@ -1310,6 +1310,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 sb; + +stat (filename, &sb); +if (!(((sb.st_mode &S_IFMT ) ==S_IFREG ) || ((sb.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 ;
Signature · 1 line
2.7.3
Austin S. Hemmelgarn posted on Wed, 16 Mar 2016 11:26:11 -0400 as excerpted:
Quoted 10 lines
Currently,
open_ctree_fs_infowill 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.This causes the following commands to provide a helpful error message when run on a FIFO, directory, character device, or socket:
Umm... what about a symlink? A symlink isn't a block device or regular file, but obviously with udev, symlinks to block devices /better/ work. Do you dereference symlinks before doing this check?
Signature · 3 lines
Duncan - List replies preferred. No HTML msgs. "Every nonfree program has a lord, a master -- and if you use the program, he is your master." Richard Stallman
Austin S. Hemmelgarn wrote on 2016/03/16 11:26 -0400:
Quoted 41 lines
Currently,
open_ctree_fs_infowill 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.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-debug-treeSigned-off-by Austin S. Hemmelgarn <[email protected]>---
This has been build and runtime tested on an x86-64 system with glibc. It has been build tested on x86-64 with uclibc.
It has not been tested on Android or with musl, although it should work there also.There are other tools that have similarly bad behavior when called incorrectly (btrfs rescue immediately comes to mind), but they don't use
open_ctree_fs_info, so this doesn't affect them. I may do followup patches to fix those too if I have the time.
open_ctree_fs_infois also used incmds-filesystem.c, although I'm not at all sure what exactly is going on there, and btrfs filesystem appears from my testing to behave exactly the same with this change, so I don't think this will have any effect on any of the btrfs filesystem commands.disk-io.c | 7 +++++++ 1 file changed, 7 insertions(+)disk-io.c +0
-0
disk-io.c struct btrfs_fs_info *open_ctree_fs_info(const char *filename, · line 1319
Quoted 10 lines
disk-io.c +7
-0@@ -1310,6 +1310,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 sb; + +stat (filename, &sb); +if (!(((sb.st_mode &S_IFMT ) ==S_IFREG ) || ((sb.st_mode &S_IFMT ) ==S_IFBLK ))) { +fprintf (stderr,"%s is not a regular file or block device\n" , filename); +return NULL ; + }
This one seems to be too restrict.
I prefer to block char/pipe/dir and some other obvious wrong ones other than only allowing regular and block ones.
Thanks,
Qu
Quoted 4 lines
disk-io.c +0
-0if (!(flags &OPEN_CTREE_WRITES )) oflags =O_RDONLY ;
Quoted 60 lines · Qu Wenruo
Austin S. Hemmelgarn wrote on 2016/03/16 11:26 -0400:
Quoted 44 lines
Currently,
open_ctree_fs_infowill 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.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-debug-treeSigned-off-by Austin S. Hemmelgarn <[email protected]>---
This has been build and runtime tested on an x86-64 system with glibc. It has been build tested on x86-64 with uclibc.
It has not been tested on Android or with musl, although it should work there also.There are other tools that have similarly bad behavior when called incorrectly (btrfs rescue immediately comes to mind), but they don't use
open_ctree_fs_info, so this doesn't affect them. I may do followup patches to fix those too if I have the time.
open_ctree_fs_infois also used incmds-filesystem.c, although I'm not at all sure what exactly is going on there, and btrfs filesystem appears from my testing to behave exactly the same with this change, so I don't think this will have any effect on any of the btrfs filesystem commands.disk-io.c | 7 +++++++ 1 file changed, 7 insertions(+)disk-io.c +0
-0@@ -1310,6 +1310,13 @@ struct btrfs_fs_info *open_ctree_fs_info(constchar *filename, int fp;struct btrfs_fs_info *info;disk-io.c struct btrfs_fs_info *open_ctree_fs_info(const · line 1322
Quoted 10 lines
disk-io.c +7
-0int oflags =O_CREAT |O_RDWR ; +struct stat sb; + +stat (filename, &sb); +if (!(((sb.st_mode &S_IFMT ) ==S_IFREG ) || ((sb.st_mode &S_IFMT ) ==S_IFBLK ))) { +fprintf (stderr,"%s is not a regular file or block device\n", filename); +return NULL ; + }This one seems to be too restrict.
Quoted 2 lines
I prefer to block char/pipe/dir and some other obvious wrong ones other than only allowing regular and block ones.
Running against a directory gives a cryptic error about the superblock having bad info. Running against a pipe is nonsensical, as it can't contain a filesystem. Running against a character device is potentially dangerous (read operations are not guaranteed to be idempotent on character devices, depending on what hardware it is connected to, you could cause all kinds of odd things to happen).
Everything this function gets called on is trying to get info from a unmounted filesystem image, which means that it only makes sense to try to parse things that can contain a unmounted filesystem image.
Quoted 9 lines
Thanks,
QuQuoted 4 lines
if (!(flags &
OPEN_CTREE_WRITES))
oflags =O_RDONLY;
Quoted 11 lines · Duncan
Austin S. Hemmelgarn posted on Wed, 16 Mar 2016 11:26:11 -0400 as excerpted:
Quoted 8 lines
Currently,
open_ctree_fs_infowill 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.
Quoted 8 lines
Quoted 2 lines
This causes the following commands to provide a helpful error message when run on a FIFO, directory, character device, or socket:
Umm... what about a symlink? A symlink isn't a block device or regular file, but obviously with udev, symlinks to block devices /better/ work. Do you dereference symlinks before doing this check?
I'm using stat(2) instead of lstat(2), and stat(2) dereferences symlinks just like open(2) does, so I shouldn't have to dereference them manually. That said, I did check that it works with symlinks to both block devices and regular files, and everything appears to work correctly.
Austin S. Hemmelgarn wrote on 2016/03/17 07:22 -0400:
Quoted 70 lines · Qu Wenruo
Quoted 60 lines
Austin S. Hemmelgarn wrote on 2016/03/16 11:26 -0400:
Quoted 44 lines
Currently,
open_ctree_fs_infowill 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.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-debug-treeSigned-off-by Austin S. Hemmelgarn <[email protected]>---
This has been build and runtime tested on an x86-64 system with glibc. It has been build tested on x86-64 with uclibc.
It has not been tested on Android or with musl, although it should work there also.There are other tools that have similarly bad behavior when called incorrectly (btrfs rescue immediately comes to mind), but they don't use
open_ctree_fs_info, so this doesn't affect them. I may do followup patches to fix those too if I have the time.
open_ctree_fs_infois also used incmds-filesystem.c, although I'm not at all sure what exactly is going on there, and btrfs filesystem appears from my testing to behave exactly the same with this change, so I don't think this will have any effect on any of the btrfs filesystem commands.disk-io.c | 7 +++++++ 1 file changed, 7 insertions(+)disk-io.c +0
-0@@ -1310,6 +1310,13 @@ struct btrfs_fs_info *open_ctree_fs_info(constchar *filename, int fp;struct btrfs_fs_info *info;disk-io.c struct btrfs_fs_info *open_ctree_fs_info(const · line 1322
Quoted 10 lines
disk-io.c +7
-0int oflags =O_CREAT |O_RDWR ; +struct stat sb; + +stat (filename, &sb); +if (!(((sb.st_mode &S_IFMT ) ==S_IFREG ) || ((sb.st_mode &S_IFMT ) ==S_IFBLK ))) { +fprintf (stderr,"%s is not a regular file or block device\n", filename); +return NULL ; + }This one seems to be too restrict.
Quoted 2 lines
I prefer to block char/pipe/dir and some other obvious wrong ones other than only allowing regular and block ones.
Running against a directory gives a cryptic error about the superblock having bad info. Running against a pipe is nonsensical, as it can't contain a filesystem. Running against a character device is potentially dangerous (read operations are not guaranteed to be idempotent on character devices, depending on what hardware it is connected to, you could cause all kinds of odd things to happen).
Quoted 3 lines
Everything this function gets called on is trying to get info from a unmounted filesystem image, which means that it only makes sense to try to parse things that can contain a unmounted filesystem image.
Yes, I understand what you are doing.
Just as I alreayd mentioned, the problem is, your current patch only allowing regular and block device and will block valid soft link. Just as Duncan mentioned, soft link should be allowed too.
I mean to *block/prevent* char/pipe/dir instead of *only allowing* regular/block device.
Thanks,
Qu
Quoted 12 lines
Quoted 9 lines
Thanks,
QuQuoted 4 lines
if (!(flags &
OPEN_CTREE_WRITES))
oflags =O_RDONLY;
Quoted 79 lines · Qu Wenruo
Austin S. Hemmelgarn wrote on 2016/03/17 07:22 -0400:
Quoted 72 lines · Qu Wenruo
Quoted 62 lines
Austin S. Hemmelgarn wrote on 2016/03/16 11:26 -0400:
Quoted 46 lines
Currently,
open_ctree_fs_infowill 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.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-debug-treeSigned-off-by Austin S. Hemmelgarn <[email protected]>---
This has been build and runtime tested on an x86-64 system with glibc. It has been build tested on x86-64 with uclibc.
It has not been tested on Android or with musl, although it should work there also.There are other tools that have similarly bad behavior when called incorrectly (btrfs rescue immediately comes to mind), but they don't use
open_ctree_fs_info, so this doesn't affect them. I may do followup patches to fix those too if I have the time.
open_ctree_fs_infois also used incmds-filesystem.c, although I'm not at all sure what exactly is going on there, and btrfs filesystem appears
from my testing to behave exactly the same with this change, so I don't think this will have any effect on any of the btrfs filesystem commands.disk-io.c | 7 +++++++ 1 file changed, 7 insertions(+)disk-io.c +0
-0@@ -1310,6 +1310,13 @@ struct btrfs_fs_info *open_ctree_fs_info(constchar *filename, int fp;struct btrfs_fs_info *info;disk-io.c struct btrfs_fs_info *open_ctree_fs_info(const · line 1322
Quoted 10 lines
disk-io.c +7
-0int oflags =O_CREAT |O_RDWR ; +struct stat sb; + +stat (filename, &sb); +if (!(((sb.st_mode &S_IFMT ) ==S_IFREG ) || ((sb.st_mode &S_IFMT ) ==S_IFBLK ))) { +fprintf (stderr,"%s is not a regular file or block device\n", filename); +return NULL ; + }This one seems to be too restrict.
Quoted 2 lines
I prefer to block char/pipe/dir and some other obvious wrong ones other than only allowing regular and block ones.
Running against a directory gives a cryptic error about the superblock having bad info. Running against a pipe is nonsensical, as it can't contain a filesystem. Running against a character device is potentially dangerous (read operations are not guaranteed to be idempotent on character devices, depending on what hardware it is connected to, you could cause all kinds of odd things to happen).
Quoted 3 lines
Everything this function gets called on is trying to get info from a unmounted filesystem image, which means that it only makes sense to try to parse things that can contain a unmounted filesystem image.
Quoted 5 lines
Yes, I understand what you are doing.
Just as I alreayd mentioned, the problem is, your current patch only allowing regular and block device and will block valid soft link. Just as Duncan mentioned, soft link should be allowed too.
Symbolic links are allowed though. stat(2) follows symlinks and returns information on their target, not the link itself. I also tested it on symlinks, and it still works to run btrfs check on a link to a block device containing a BTRFS filesystem
Quoted 3 lines
I mean to *block/prevent* char/pipe/dir instead of *only allowing* regular/block device.
I apologize for the misunderstanding, I misread your original message.
As to blacklisting invalid types instead of whitelisting valid ones, I'd personally would prefer whitelisting in this particular case, as it results in both a smaller conditional, and makes the intent of only allowing things that can contain a BTRFS filesystem more clear (at least, it does to me).
Quoted 17 lines
Thanks,
QuQuoted 12 lines
Quoted 9 lines
Thanks,
QuQuoted 4 lines
if (!(flags &
OPEN_CTREE_WRITES))
oflags =O_RDONLY;
Quoted 27 lines · Austin S. Hemmelgarn
Quoted 15 lines
Quoted 11 lines
Quoted 4 lines
This one seems to be too restrict.
I prefer to block char/pipe/dir and some other obvious wrong ones other than only allowing regular and block ones.
Running against a directory gives a cryptic error about the superblock having bad info. Running against a pipe is nonsensical, as it can't contain a filesystem. Running against a character device is potentially dangerous (read operations are not guaranteed to be idempotent on character devices, depending on what hardware it is connected to, you could cause all kinds of odd things to happen).
Quoted 3 lines
Everything this function gets called on is trying to get info from a unmounted filesystem image, which means that it only makes sense to try to parse things that can contain a unmounted filesystem image.
Quoted 5 lines
Yes, I understand what you are doing.
Just as I alreayd mentioned, the problem is, your current patch only allowing regular and block device and will block valid soft link. Just as Duncan mentioned, soft link should be allowed too.
Symbolic links are allowed though. stat(2) follows symlinks and returns information on their target, not the link itself. I also tested it on symlinks, and it still works to run
btrfs checkon a link to a block device containing a BTRFS filesystemQuoted 3 lines
I mean to *block/prevent* char/pipe/dir instead of *only allowing* regular/block device.
Quoted 7 lines
I apologize for the misunderstanding, I misread your original message.
As to blacklisting invalid types instead of whitelisting valid ones, I'd personally would prefer whitelisting in this particular case, as it results in both a smaller conditional, and makes the intent of only allowing things that can contain a BTRFS filesystem more clear (at least, it does to me).
I agree with your reasoning.
Austin S. Hemmelgarn wrote on 2016/03/18 07:17 -0400:
Quoted 86 lines · Qu Wenruo
Quoted 85 lines
Austin S. Hemmelgarn wrote on 2016/03/17 07:22 -0400:
Quoted 76 lines · Qu Wenruo
Quoted 66 lines
Austin S. Hemmelgarn wrote on 2016/03/16 11:26 -0400:
Quoted 50 lines
Currently,
open_ctree_fs_infowill 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.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-debug-treeSigned-off-by Austin S. Hemmelgarn <[email protected]>---
This has been build and runtime tested on an x86-64 system with glibc. It has been build tested on x86-64 with uclibc.
It has not been tested on Android or with musl, although it should work
there also.There are other tools that have similarly bad behavior when called incorrectly (btrfs rescue immediately comes to mind), but they don't use
open_ctree_fs_info, so this doesn't affect them. I may do followup
patches to fix those too if I have the time.
open_ctree_fs_infois also used incmds-filesystem.c, although I'm not at all sure what exactly is going on there, and btrfs filesystem appears
from my testing to behave exactly the same with this change, so I don't
think this will have any effect on any of the btrfs filesystem commands.disk-io.c | 7 +++++++ 1 file changed, 7 insertions(+)disk-io.c +0
-0@@ -1310,6 +1310,13 @@ struct btrfs_fs_info *open_ctree_fs_info(constchar *filename, int fp;struct btrfs_fs_info *info;disk-io.c struct btrfs_fs_info *open_ctree_fs_info(const · line 1322
Quoted 10 lines
disk-io.c +7
-0int oflags =O_CREAT |O_RDWR ; +struct stat sb; + +stat (filename, &sb); +if (!(((sb.st_mode &S_IFMT ) ==S_IFREG ) || ((sb.st_mode &S_IFMT ) ==S_IFBLK ))) { +fprintf (stderr,"%s is not a regular file or block device\n", filename); +return NULL ; + }This one seems to be too restrict.
Quoted 2 lines
I prefer to block char/pipe/dir and some other obvious wrong ones other than only allowing regular and block ones.
Running against a directory gives a cryptic error about the superblock having bad info. Running against a pipe is nonsensical, as it can't contain a filesystem. Running against a character device is potentially dangerous (read operations are not guaranteed to be idempotent on character devices, depending on what hardware it is connected to, you could cause all kinds of odd things to happen).
Quoted 3 lines
Everything this function gets called on is trying to get info from a unmounted filesystem image, which means that it only makes sense to try to parse things that can contain a unmounted filesystem image.
Yes, I understand what you are doing.
Quoted 7 lines
Quoted 3 lines
Just as I alreayd mentioned, the problem is, your current patch only allowing regular and block device and will block valid soft link. Just as Duncan mentioned, soft link should be allowed too.
Symbolic links are allowed though. stat(2) follows symlinks and returns information on their target, not the link itself. I also tested it on symlinks, and it still works to run
btrfs checkon a link to a block device containing a BTRFS filesystem
Then I'm completely OK with current implementation.
Thanks,
Qu
Quoted 34 lines
Quoted 3 lines
I mean to *block/prevent* char/pipe/dir instead of *only allowing* regular/block device.
I apologize for the misunderstanding, I misread your original message.
As to blacklisting invalid types instead of whitelisting valid ones, I'd personally would prefer whitelisting in this particular case, as it results in both a smaller conditional, and makes the intent of only allowing things that can contain a BTRFS filesystem more clear (at least, it does to me).
Quoted 17 lines
Thanks,
QuQuoted 12 lines
Quoted 9 lines
Thanks,
QuQuoted 4 lines
if (!(flags &
OPEN_CTREE_WRITES))
oflags =O_RDONLY;--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html