From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: Florian Weimer <fweimer@redhat.com>,
Adhemerval Zanella via Libc-alpha <libc-alpha@sourceware.org>
Subject: Re: [PATCH] Linux: Add execveat system call wrapper
Date: Thu, 30 Apr 2020 16:03:20 -0300 [thread overview]
Message-ID: <8a81946b-c1c2-94c2-27c1-f6033df8f36e@linaro.org> (raw)
In-Reply-To: <87y2qdku94.fsf@oldenburg2.str.redhat.com>
On 30/04/2020 09:55, Florian Weimer wrote:
> * Adhemerval Zanella via Libc-alpha:
>
>>> So I think we have to do this:
>>>
>>> * If there are more flags than just the two, fail with EINVAL.
>>>
>>> * To handle AT_EMPTY_PATH, do not open a new file descriptor (using
>>> openat) if AT_EMPTY_PATH is specified *and* the file name is "".
>>>
>>> * To handle AT_SYMLINK_NOFOLLOW, openat needs to be called with
>>> O_NOFOLLOW in that case (in addition to O_CLOEXEC).
>>
>> These will surely need to be on the testcase.
>
> Yes, these permutations need to be tested.
>
>>> The behavior with AT_EMPTY_PATH/"" and AT_SYMLINK_NOFOLLOW at the same
>>> time is not immedately obvious from the kernel code, so I wrote a small
>>> test program (/bin/sh is a symbolic link to /bin/bash on this system):
>>>
>>> #include <err.h>
>>> #include <fcntl.h>
>>> #include <sys/syscall.h>
>>> #include <unistd.h>
>>>
>>> int
>>> main (void)
>>> {
>>> int fd = open ("/bin/sh", O_PATH | O_NOFOLLOW);
>>> if (fd < 0)
>>> err (1, "open");
>>> static char *const argv[] = { "sh", "-c", "exit 0", NULL };
>>> static char *const envp[] = { NULL };
>>> syscall (SYS_execveat, fd, "", argv, envp,
>>> AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
>>> err (1, "execveat");
>>> }
>>>
>>> This fails:
>>>
>>> openat(AT_FDCWD, "/bin/sh", O_RDONLY|O_NOFOLLOW|O_PATH) = 3
>>> execveat(3, "", ["sh", "-c", "exit 0"], 0x402040 /* 0 vars */, AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) = -1 ELOOP (Too many levels of symbolic links)
>>> […]
>>> execveat-opath-symlink: execveat: Too many levels of symbolic links
>>>
>>> So I think for the AT_EMPTY_PATH/"" and AT_SYMLINK_NOFOLLOW case, we
>>> need to call fstatat64 with AT_EMPTY_PATH and see if st_mode indicates
>>> that the descriptor refers to a symbolic link. If it does, the function
>>> needs to fail with ELOOP.
>>
>> I think execve would handle it:
>>
>> openat(AT_FDCWD, "/bin/sh", O_RDONLY|O_NOFOLLOW|O_PATH) = 3
>> execve("/proc/self/fd/3", ["sh", "-c", "echo test"], 0x556815e580a8 /* 0 vars */) = -1 ELOOP (Too many levels of symbolic links)
>
> And execveat fails even without AT_SYMLINK_NOFOLLOW:
>
> openat(AT_FDCWD, "/bin/sh", O_RDONLY|O_NOFOLLOW|O_PATH) = 3
> execveat(3, "", ["sh", "-c", "exit 0"], 0x402040 /* 0 vars */, AT_EMPTY_PATH) = -1 ELOOP (Too many levels of symbolic links)
>
> Does this mean we do not need a special case for an O_PATH|O_NOFOLLOW
> open of a symbolic link?
I think current proposal has some issues regarding handling the
path and flags, with the following code for fallback:
---
int fd;
if (path[0] == '\0' && flags & AT_EMPTY_PATH)
fd = dirfd;
else
{
int oflags = O_CLOEXEC;
if (flags & AT_SYMLINK_NOFOLLOW)
oflags |= O_NOFOLLOW;
fd = openat (dirfd, path, oflags);
}
if (fd < 0)
return -1;
struct fd_to_filename fdfilename;
const char *gfilename = __fd_to_filename (fd, &fdfilename);
/* We do not need the return value. */
execve (gfilename, argv, envp);
---
It seems to follow execveat syscall semantic. For instance:
int fd = open ("/bin/sh", O_RDONLY|O_NOFOLLOW|O_PATH);
[...]
execveat (fd, "", argv, envp, AT_EMPTY_PATH);
produces:
openat(AT_FDCWD, "/bin/sh", O_RDONLY|O_NOFOLLOW|O_PATH) = 3
execve("/proc/self/fd/3", ["sh", "-c", "echo test"], 0x5624523db098 /* 0 vars */) = -1 ELOOP
And:
int fd = open ("/bin/sh", O_RDONLY|O_NOFOLLOW|O_PATH);
[...]
execveat (fd, "", argv, envp, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
also produces:
openat(AT_FDCWD, "/bin/sh", O_RDONLY|O_NOFOLLOW|O_PATH) = 3
execve("/proc/self/fd/3", ["sh", "-c", "echo test"], 0x563e8f1d0098 /* 0 vars */) = -1 ELOOP
next prev parent reply other threads:[~2020-04-30 19:03 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-28 12:20 Alexandra Hájková
2020-04-28 14:17 ` Florian Weimer
2020-04-28 15:03 ` Joseph Myers
2020-04-28 15:03 ` Joseph Myers
2020-04-28 15:08 ` Florian Weimer
2020-04-28 15:29 ` Joseph Myers
2020-04-28 17:15 ` Florian Weimer
2020-04-28 17:19 ` Joseph Myers
2020-04-28 17:44 ` Adhemerval Zanella
2020-04-28 17:50 ` Florian Weimer
2020-04-28 18:31 ` Adhemerval Zanella
2020-04-30 11:15 ` Florian Weimer
2020-04-30 12:28 ` Adhemerval Zanella
2020-04-30 12:55 ` Florian Weimer
2020-04-30 19:03 ` Adhemerval Zanella [this message]
2020-04-30 12:32 ` Adhemerval Zanella
2020-11-06 21:03 ` Alexandra Hájková
2020-11-06 22:15 ` Joseph Myers
2020-11-09 18:43 ` Florian Weimer
2020-11-09 21:34 ` Yann Droneaud
2020-11-26 11:31 ` Alexandra Petlanova Hajkova
2020-11-09 20:39 ` Adhemerval Zanella
2020-12-03 13:55 ` Florian Weimer
2020-11-26 21:28 ` Alexandra Hájková
2020-11-27 14:58 ` Adhemerval Zanella
2020-11-27 17:32 ` Florian Weimer
2020-11-27 17:38 ` Adhemerval Zanella
2020-12-03 14:20 ` Alexandra Hájková
2020-12-03 14:37 ` Andreas Schwab
2020-12-08 14:44 ` Adhemerval Zanella
2020-12-08 15:18 ` Florian Weimer
2020-12-08 16:41 ` Adhemerval Zanella
2021-03-15 21:42 ` Alexandra Hájková
2021-03-15 22:12 ` Joseph Myers
2021-03-15 22:17 ` Dmitry V. Levin
2021-03-24 13:54 ` Alexandra Hájková
2021-03-26 20:36 ` Adhemerval Zanella
2021-04-02 12:13 ` Alexandra Hájková
2021-04-02 13:29 ` Adhemerval Zanella
2021-04-05 16:32 ` Alexandra Hájková
2021-04-12 10:26 ` Alexandra Hájková
2021-04-12 11:14 ` Andreas Schwab
2021-04-12 19:26 ` Alexandra Hájková
2021-04-13 19:27 ` Adhemerval Zanella
2021-04-21 18:11 ` Alexandra Hájková
2021-05-03 19:20 ` Adhemerval Zanella
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=8a81946b-c1c2-94c2-27c1-f6033df8f36e@linaro.org \
--to=adhemerval.zanella@linaro.org \
--cc=fweimer@redhat.com \
--cc=libc-alpha@sourceware.org \
/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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).