From patchwork Wed Aug 28 14:42:42 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Liebler X-Patchwork-Id: 34300 Received: (qmail 105440 invoked by alias); 28 Aug 2019 14:42:52 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 105431 invoked by uid 89); 28 Aug 2019 14:42:52 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mx0a-001b2d01.pphosted.com Subject: Re: [PATCH] Add UNSUPPORTED check in elf/tst-pldd. To: Florian Weimer Cc: libc-alpha@sourceware.org, Adhemerval Zanella References: <877e6yiqsn.fsf@oldenburg2.str.redhat.com> <3b9cb5e4-7c9a-c9a4-449e-43ba98a6ad01@linaro.org> <32961942-e67b-c356-a92d-b6e45c8aaf9d@linux.ibm.com> <87blw9fxrp.fsf@oldenburg2.str.redhat.com> From: Stefan Liebler Date: Wed, 28 Aug 2019 16:42:42 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <87blw9fxrp.fsf@oldenburg2.str.redhat.com> x-cbid: 19082814-0012-0000-0000-00000343EB81 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19082814-0013-0000-0000-0000217E28E2 Message-Id: <1d419974-c973-c4c1-f1cd-4bbbf8b074f8@linux.ibm.com> On 8/28/19 11:24 AM, Florian Weimer wrote: > * Stefan Liebler: > >> static void >> target_process (void *arg) >> { >> + if (ptrace_scope == 1) >> + { >> + /* YAMA is configured to "restricted ptrace". >> + Disable the restriction for this subprocess. */ >> + support_ptrace_process_set_ptracer_any (); >> + } >> + >> pause (); >> } > > I think this has a race condition if pldd attaches to the process before > the support_ptrace_process_set_ptracer_any call. I have no idea how > hard it is in practice to hit this race. It should be possible to use a > process-shared barrier or some other form of synchronization to avoid > this issue. > > Thanks, > Florian > I've added a synchronization with stdatomic.h on a shared memory mapping. I've not used pthread* functions as I don't want to link against libpthread.so. Then further adjustments are needed. Or should I just restrict the test ptrace_scope 0 as Adhemerval has proposed in his post? Bye. Stefan commit e2aed097192c423dcf1882cb1c39567676ef2255 Author: Stefan Liebler Date: Mon Aug 26 15:45:07 2019 +0200 Add UNSUPPORTED check in elf/tst-pldd. The testcase forks a child process and runs pldd with PID of this child. On systems where /proc/sys/kernel/yama/ptrace_scope differs from zero, pldd will fail with /usr/bin/pldd: cannot attach to process 3: Operation not permitted This patch checks if ptrace_scope exists, is zero "classic ptrace permissions" or one "restricted ptrace". In case of "restricted ptrace", we can effectively disable the restriction by using prctl (PR_SET_PTRACER_ANY). If ptrace_scope exists and has a higher restriction, then the test is marked as UNSUPPORTED. ChangeLog: * elf/tst-pldd.c (ptrace_scope, target_barrier): New variable. (do_test): Add UNSUPPORTED check. (target_process): Disable restricted ptrace. * support/Makefile (libsupport-routines): Add support_ptrace. * support/ptrace.h: New file. * support/support_ptrace.c: Likewise. diff --git a/elf/tst-pldd.c b/elf/tst-pldd.c index 6b7c94a1c0..130d1f51db 100644 --- a/elf/tst-pldd.c +++ b/elf/tst-pldd.c @@ -30,10 +30,27 @@ #include #include #include +#include +#include +#include +#include + +static int ptrace_scope; +static atomic_int *target_barrier; static void target_process (void *arg) { + if (ptrace_scope == 1) + { + /* YAMA is configured to "restricted ptrace". + Disable the restriction for this subprocess. */ + support_ptrace_process_set_ptracer_any (); + } + + /* Ensure that pldd is called after ptrace restriction has been disabled. */ + *target_barrier = 1; + pause (); } @@ -52,9 +69,24 @@ in_str_list (const char *libname, const char *const strlist[]) static int do_test (void) { + /* Check if our subprocess can be debugged with ptrace. */ + ptrace_scope = support_ptrace_scope (); + if (ptrace_scope >= 2) + FAIL_UNSUPPORTED ("/proc/sys/kernel/yama/ptrace_scope >= 2"); + + target_barrier = (atomic_int *) xmmap (NULL, sizeof (int), PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, -1); + atomic_init (target_barrier, 0); + /* Create a copy of current test to check with pldd. */ struct support_subprocess target = support_subprocess (target_process, NULL); + /* Ensure that pldd is called after ptrace restriction has been disabled in + target_process. Do not use pthread synchronization functions here as the + test below expects not to be linked against libpthread. */ + while (*target_barrier == 0) + ; + /* Run 'pldd' on test subprocess. */ struct support_capture_subprocess pldd; { @@ -129,6 +161,7 @@ do_test (void) support_capture_subprocess_free (&pldd); support_process_terminate (&target); + xmunmap (target_barrier, sizeof (int)); return 0; } diff --git a/support/Makefile b/support/Makefile index ab66913a02..34d14eb1bb 100644 --- a/support/Makefile +++ b/support/Makefile @@ -56,6 +56,7 @@ libsupport-routines = \ support_format_hostent \ support_format_netent \ support_isolate_in_subprocess \ + support_ptrace \ support_openpty \ support_paths \ support_quote_blob \ diff --git a/support/ptrace.h b/support/ptrace.h new file mode 100644 index 0000000000..82f79ff25c --- /dev/null +++ b/support/ptrace.h @@ -0,0 +1,36 @@ +/* Support functions handling ptrace_scope. + Copyright (C) 2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef SUPPORT_PTRACE_H +#define SUPPORT_PTRACE_H + +#include + +__BEGIN_DECLS + +/* Return the current YAMA mode set on the machine (0 to 3) or -1 + if YAMA is not supported. */ +int support_ptrace_scope (void); + +/* Effectively disables YAMA restriction for the calling process + if support_ptrace_scope returns 1 "restricted ptrace". */ +void support_ptrace_process_set_ptracer_any (void); + +__END_DECLS + +#endif diff --git a/support/support_ptrace.c b/support/support_ptrace.c new file mode 100644 index 0000000000..e9410384b5 --- /dev/null +++ b/support/support_ptrace.c @@ -0,0 +1,58 @@ +/* Support functions handling ptrace_scope. + Copyright (C) 2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +int +support_ptrace_scope (void) +{ + int ptrace_scope = -1; + +#ifdef __linux__ + /* YAMA may be not enabled. Otherwise it contains a value from 0 to 3: + - 0 classic ptrace permissions + - 1 restricted ptrace + - 2 admin-only attach + - 3 no attach */ + FILE *f = fopen ("/proc/sys/kernel/yama/ptrace_scope", "r"); + if (f != NULL) + { + TEST_COMPARE (fscanf (f, "%d", &ptrace_scope), 1); + xfclose (f); + } +#endif + + return ptrace_scope; +} + +void +support_ptrace_process_set_ptracer_any (void) +{ +#ifdef __linux__ + int ret = prctl (PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0); + if (ret != 0) + FAIL_EXIT1 ("Failed to disable YAMA restriction. (prctl returned %d: %m)", + ret); +#else + FAIL_UNSUPPORTED ("prctl (PR_SET_PTRACER, PR_SET_PTRACER_ANY, ...) " + "not supported!"); +#endif +}