mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] fix fexecve fallback code when fd is negative
@ 2021-07-08  0:15 Érico Nogueira
  2021-07-08  1:39 ` Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: Érico Nogueira @ 2021-07-08  0:15 UTC (permalink / raw)
  To: musl; +Cc: Érico Nogueira

even though passing negative fd values to fexecve shouldn't happen on
the part of any application, if it does happen and the current kernel
doesn't implement SYS_execveat, musl will call execve on
"/proc/self/fd/(unsigned)fd", which might be an existing, unrelated,
file.

this was introduced in commit c8c0844f7fbcb955848ca84432e5ffcf71f1cef1,
when fexecve started using __procfdname, which takes an unsigned integer
instead of signed one.

this also fixes the POSIX-compliance of the function, since POSIX
specifies:

  The fexecve() function shall fail if:

  EBADF  The fd argument is not a valid file descriptor open for
  executing.

this corner case was found while investigating usage of __procfdname.
all other cases are correct:

- fchmod,fchdir,fchown -> will fail the direct syscall first
- fchmodat -> is in the *at family so fd can be -1, only uses procfdname with another fd
- fstatat -> checks for fd>=0 in the branch that uses procfdname
- ttyname_r -> fd goes through isatty()
---
 src/process/fexecve.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/process/fexecve.c b/src/process/fexecve.c
index 554c1981..ab81b2d3 100644
--- a/src/process/fexecve.c
+++ b/src/process/fexecve.c
@@ -8,6 +8,8 @@ int fexecve(int fd, char *const argv[], char *const envp[])
 {
 	int r = __syscall(SYS_execveat, fd, "", argv, envp, AT_EMPTY_PATH);
 	if (r != -ENOSYS) return __syscall_ret(r);
+	else if (fd < 0) return __syscall_ret(-EBADF);
+
 	char buf[15 + 3*sizeof(int)];
 	__procfdname(buf, fd);
 	execve(buf, argv, envp);
-- 
2.32.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [musl] [PATCH] fix fexecve fallback code when fd is negative
  2021-07-08  0:15 [musl] [PATCH] fix fexecve fallback code when fd is negative Érico Nogueira
@ 2021-07-08  1:39 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2021-07-08  1:39 UTC (permalink / raw)
  To: musl

On Wed, Jul 07, 2021 at 09:15:19PM -0300, Érico Nogueira wrote:
> even though passing negative fd values to fexecve shouldn't happen on
> the part of any application, if it does happen and the current kernel
> doesn't implement SYS_execveat, musl will call execve on
> "/proc/self/fd/(unsigned)fd", which might be an existing, unrelated,
> file.
> 
> this was introduced in commit c8c0844f7fbcb955848ca84432e5ffcf71f1cef1,
> when fexecve started using __procfdname, which takes an unsigned integer
> instead of signed one.
> 
> this also fixes the POSIX-compliance of the function, since POSIX
> specifies:
> 
>   The fexecve() function shall fail if:
> 
>   EBADF  The fd argument is not a valid file descriptor open for
>   executing.
> 
> this corner case was found while investigating usage of __procfdname.
> all other cases are correct:
> 
> - fchmod,fchdir,fchown -> will fail the direct syscall first
> - fchmodat -> is in the *at family so fd can be -1, only uses procfdname with another fd
> - fstatat -> checks for fd>=0 in the branch that uses procfdname
> - ttyname_r -> fd goes through isatty()
> ---
>  src/process/fexecve.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/src/process/fexecve.c b/src/process/fexecve.c
> index 554c1981..ab81b2d3 100644
> --- a/src/process/fexecve.c
> +++ b/src/process/fexecve.c
> @@ -8,6 +8,8 @@ int fexecve(int fd, char *const argv[], char *const envp[])
>  {
>  	int r = __syscall(SYS_execveat, fd, "", argv, envp, AT_EMPTY_PATH);
>  	if (r != -ENOSYS) return __syscall_ret(r);
> +	else if (fd < 0) return __syscall_ret(-EBADF);
> +
>  	char buf[15 + 3*sizeof(int)];
>  	__procfdname(buf, fd);
>  	execve(buf, argv, envp);
> -- 
> 2.32.0

To document what we discussed on IRC, if x has type int and x<0,
(unsigned)x > INT_MAX, so it cannot masquerade as a value in the range
0..INT_MAX (which a fd must be). This means the code is already
working, and the line following the above context:

	if (errno == ENOENT) errno = EBADF;

handles conversion to the expected error code.

Rich

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-07-08  1:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-08  0:15 [musl] [PATCH] fix fexecve fallback code when fd is negative Érico Nogueira
2021-07-08  1:39 ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).