mailing list of musl libc
 help / color / mirror / code / Atom feed
From: "Érico Nogueira" <ericonr@disroot.org>
To: musl@lists.openwall.com
Cc: "Érico Nogueira" <ericonr@disroot.org>
Subject: [musl] [PATCH] fix fexecve fallback code when fd is negative
Date: Wed,  7 Jul 2021 21:15:19 -0300	[thread overview]
Message-ID: <20210708001519.649-1-ericonr@disroot.org> (raw)

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


             reply	other threads:[~2021-07-08  0:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-08  0:15 Érico Nogueira [this message]
2021-07-08  1:39 ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210708001519.649-1-ericonr@disroot.org \
    --to=ericonr@disroot.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).