Skip to contentSkip to conversation

btrfs-progs: fix fi du so it works in more cases

PATCH Applied Austin S. Hemmelgarn 3 participants 4 messages Mar 21, 2016 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs-progs: fix fi du so it works in more cases

Currently, btrfs fi du uses open_file_or_dir(), which tries to open it's argument with o_RDWR. Because of POSIX semantics, this fails for non-root users when the file is read-only or is an executable that is being run currently, or for all users (including root) when the filesystem is read-only. THis results in a somewhat confusing 'Unknown error -1' message when trying to check such files. Switch to using open_file_or_dir3() with O_RDONLY passed in the flags, as this avoids the limitations listed above, and we have no need to write to the files anyway (and thus shouldn't be opening them writable).

---
Build and runtime tested on x86-64 with glibc.

I intend to take the time at some point this week to audit all users of open_file_or_dir() and similarly change any that don't need to write to what they're opening, possibly adding a helper function to do a read-only open.

 cmds-fi-du.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cmds-fi-du.c +1 -1
index 2ffd917..168fc72 100644
@@ -438,7 +438,7 @@ static int du_add_file(const char *filename, int dirfd,
 		ret = sprintf(pathp, "/%s", filename);
 	pathp += ret;
 
-	fd = open_file_or_dir(path, &dirstream);
+	fd = open_file_or_dir3(path, &dirstream, O_RDONLY);
 	if (fd < 0) {
 		ret = fd;
 		goto out;
Signature · 1 line
2.7.4
#1 lore raw

Works for me, thanks.

Martin Volf

Quoted 42 lines · Austin S. Hemmelgarn

Currently, btrfs fi du uses open_file_or_dir(), which tries to open it's argument with o_RDWR. Because of POSIX semantics, this fails for non-root users when the file is read-only or is an executable that is being run currently, or for all users (including root) when the filesystem is read-only. THis results in a somewhat confusing 'Unknown error -1' message when trying to check such files. Switch to using open_file_or_dir3() with O_RDONLY passed in the flags, as this avoids the limitations listed above, and we have no need to write to the files anyway (and thus shouldn't be opening them writable).

---
Build and runtime tested on x86-64 with glibc.

I intend to take the time at some point this week to audit all users of open_file_or_dir() and similarly change any that don't need to write to what they're opening, possibly adding a helper function to do a read-only open.

 cmds-fi-du.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cmds-fi-du.c +1 -2
index 2ffd917..168fc72 100644
@@ -438,7 +438,7 @@ static int du_add_file(const char *filename, int dirfd,
                ret = sprintf(pathp, "/%s", filename);
        pathp += ret;
 
-       fd = open_file_or_dir(path, &dirstream);
+       fd = open_file_or_dir3(path, &dirstream, O_RDONLY);
        if (fd < 0) {
                ret = fd;
                goto out;
--

2.7.4

--
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

Signature · 1 line
Martin Volf
Applied
#2 lore raw
In reply to Austin S. Hemmelgarn
Quoted 11 lines · Austin S. Hemmelgarn

Currently, btrfs fi du uses open_file_or_dir(), which tries to open it's argument with o_RDWR. Because of POSIX semantics, this fails for non-root users when the file is read-only or is an executable that is being run currently, or for all users (including root) when the filesystem is read-only. THis results in a somewhat confusing 'Unknown error -1' message when trying to check such files. Switch to using open_file_or_dir3() with O_RDONLY passed in the flags, as this avoids the limitations listed above, and we have no need to write to the files anyway (and thus shouldn't be opening them writable).

Applied, thanks.

Quoted 7 lines

---
Build and runtime tested on x86-64 with glibc.

I intend to take the time at some point this week to audit all users of open_file_or_dir() and similarly change any that don't need to write to what they're opening, possibly adding a helper function to do a read-only open.

Thanks. I think using open_file_or_dir3(path, &dirstream, O_RDONLY) is ok, no need for a helper.

#3 lore raw
Quoted 18 lines · David Sterba
Quoted 11 lines · Austin S. Hemmelgarn

Currently, btrfs fi du uses open_file_or_dir(), which tries to open it's argument with o_RDWR. Because of POSIX semantics, this fails for non-root users when the file is read-only or is an executable that is being run currently, or for all users (including root) when the filesystem is read-only. THis results in a somewhat confusing 'Unknown error -1' message when trying to check such files. Switch to using open_file_or_dir3() with O_RDONLY passed in the flags, as this avoids the limitations listed above, and we have no need to write to the files anyway (and thus shouldn't be opening them writable).

Applied, thanks.

Quoted 3 lines

---
Build and runtime tested on x86-64 with glibc.

Quoted 8 lines
Quoted 4 lines

I intend to take the time at some point this week to audit all users of open_file_or_dir() and similarly change any that don't need to write to what they're opening, possibly adding a helper function to do a read-only open.

Thanks. I think using open_file_or_dir3(path, &dirstream, O_RDONLY) is ok, no need for a helper.

Agreed, especially since this appears to be the only place that used open_file_or_dir() that doesn't potentially need write access.