commit:     d8b9a41d76de38cab079951cd494cdb491b55126
Author:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Sun Oct 31 23:50:01 2021 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Sun Oct 31 23:50:01 2021 +0000
URL:        https://gitweb.gentoo.org/proj/sandbox.git/commit/?id=d8b9a41d

libsandbox: do not use ptrace if it returns ENOSYS

QEMU's linux-user does not implement ptrace for any architecture, and
any attempt to call it fails with ENOSYS.  Detect that scenario.

Closes: https://bugs.gentoo.org/648516
Signed-off-by: Mike Frysinger <vapier <AT> gentoo.org>

 libsandbox/trace.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/libsandbox/trace.c b/libsandbox/trace.c
index 4e01f6e..4ae58aa 100644
--- a/libsandbox/trace.c
+++ b/libsandbox/trace.c
@@ -584,8 +584,14 @@ static char *flatten_args(char *const argv[])
 
 bool trace_possible(const char *filename, char *const argv[], const void *data)
 {
-       if (_trace_possible(data))
-               return true;
+       if (_trace_possible(data)) {
+               /* If we're in an environment like QEMU where ptrace doesn't 
work, then
+                * don't try to use it.  If ptrace does work, this should fail 
with ESRCH.
+                */
+               errno = 0;
+               ptrace(PTRACE_CONT, 0, NULL, NULL);
+               return errno == ENOSYS ? false : true;
+       }
 
        char *args = flatten_args(argv);
        sb_eqawarn("Unable to trace static ELF: %s: %s\n", filename, args);

Reply via email to