fs: correctly check for errors from replace_fd() in receive_fd_replace()
Subsystems (2)
PATCH 1/1 fs: correctly check for errors from replace_fd() in receive_fd_replace()
replace_fd() returns either a negative error number or the number of the new file descriptor. The current code misinterprets any positive file descriptor number as an error.
Only check for negative error numbers, so that __receive_sock() is called correctly for valid file descriptors.
---
Untested, it stuck out while reading the code.
--- fs/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/file.c +1 -1
@@ -1387,7 +1387,7 @@ int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags)if (error)return error; error =replace_fd (new_fd, file, o_flags); -if (error) +if (error <0 )return error;__receive_sock (file);return new_fd;
---
base-commit: 89748acdf226fd1a8775ff6fa2703f8412b286c8
change-id: 20250801-fix-receive_fd_replace-7fdd5ce6532d
Best regards,
Signature · 1 line
Thomas Weißschuh <[email protected]>
Quoted 11 lines · Thomas Weißschuh
replace_fd()returns either a negative error number or the number of the new file descriptor. The current code misinterprets any positive file descriptor number as an error.Only check for negative error numbers, so that
__receive_sock()is called correctly for valid file descriptors.Fixes 173817151b15 ("fs: Expand __receive_fd() to accept existing fd")Fixes 42eb0d54c08a ("fs: split receive_fd_replace from __receive_fd")Signed-off-by Thomas Weißschuh <[email protected]>
Indeed. I'm wondering how come nobody noticed... Feel free to add:
Honza
Quoted 28 lines
---
Untested, it stuck out while reading the code.--- fs/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/file.c +1
-2@@ -1387,7 +1387,7 @@ int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags)if (error)return error; error =replace_fd (new_fd, file, o_flags); -if (error) +if (error <0 )return error;__receive_sock (file);return new_fd; ---base-commit:
89748acdf226fd1a8775ff6fa2703f8412b286c8
change-id: 20250801-fix-receive_fd_replace-7fdd5ce6532dBest regards,
Signature · 1 line
Thomas Weißschuh <[email protected]>
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Al Viro
maintainerQuoted 8 lines · Jan Kara
Quoted 7 lines · Thomas Weißschuh
replace_fd()returns either a negative error number or the number of the new file descriptor. The current code misinterprets any positive file descriptor number as an error.Only check for negative error numbers, so that
__receive_sock()is called correctly for valid file descriptors.
Quoted 6 lines
Quoted 4 lines
Fixes 173817151b15 ("fs: Expand __receive_fd() to accept existing fd")Fixes 42eb0d54c08a ("fs: split receive_fd_replace from __receive_fd")Signed-off-by Thomas Weißschuh <[email protected]>Indeed. I'm wondering how come nobody noticed...
One word: seccomp. Considering the background amount of bogus userland behaviour coming with it, I wouldn't expect a... vigorous test coverage for that one ;-/
It's definitely a bug that needs fixing, but I'm not sure this is the right way to fix it.
Look: replace_fd(fd, file, flags) returns fd on success and -E... on failure. Not a single user cares which non-negative value had been returned. What's more, "returns fd on success" is a side effect of using do_dup2() and being lazy about it.
And the entire thing is not on any hot paths. So I suspect that a better fix would be
err = do_dup2(files, file, fd, flags); if (err < 0) return err; return 0;
in replace_fd() in place of
return do_dup2(files, file, fd, flags);
so we don't invite more surprises like that.
Christian Brauner
maintainerQuoted 22 lines · Thomas Weißschuh
replace_fd()returns either a negative error number or the number of the new file descriptor. The current code misinterprets any positive file descriptor number as an error.Only check for negative error numbers, so that
__receive_sock()is called correctly for valid file descriptors.Fixes 173817151b15 ("fs: Expand __receive_fd() to accept existing fd")Fixes 42eb0d54c08a ("fs: split receive_fd_replace from __receive_fd")Signed-off-by Thomas Weißschuh <[email protected]>---
Untested, it stuck out while reading the code.--- fs/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/file.c +0
-0
fs/file.c int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags) · line 1391
Quoted 6 lines
fs/file.c +1
-1@@ -1387,7 +1387,7 @@ int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags)if (error)return error; error =replace_fd (new_fd, file, o_flags); -if (error) +if (error <0 )return error;
What in the holy fsck? Why did the seccomp selftests not fail horrendously explode because of that.