From patchwork Wed May 12 18:53:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 43420 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 80720398D053; Wed, 12 May 2021 18:53:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 80720398D053 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1620845600; bh=PvAbDf+R2COiTrMES45cY1rpHN6BzadNTAfVKT5i68M=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=YP5+Dha/8qQQJj3VHVbBEePd0iH4dZx3BS6PxF2aQkkZFUBcZOph1NSBsOzdSbDGc MlwxQuiOs/GpITX9rSyHoBzzA8/9LYpVon1fDOq4tp3gaMcyIlTmGT7O/HsLpE522t h/DxhLaVCmtPYp3YvZ9jlaE+Y+V9iTTj6Snh4BGo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x834.google.com (mail-qt1-x834.google.com [IPv6:2607:f8b0:4864:20::834]) by sourceware.org (Postfix) with ESMTPS id A98B4398D053 for ; Wed, 12 May 2021 18:53:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A98B4398D053 Received: by mail-qt1-x834.google.com with SMTP id f8so13824725qth.6 for ; Wed, 12 May 2021 11:53:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PvAbDf+R2COiTrMES45cY1rpHN6BzadNTAfVKT5i68M=; b=KsiYP/bU4/S3Wq4S/THeEXVYH9eJcBkkrnEnwhPaipHmgKmXromW9jFd7tKQj6W5nk M+BYsWDS8brSo30euXv9BQdJ9+2V265fLotMvtgf/KIkXRXnSriGK5tKgJdS/dnkVl7s V4+KjmjNGmT7YJnqeKF2vBQCgnG2U/+svF57xYpiqoLYNWuxEIKCeQBidNZ6PV75AAvH r543CkSTgpY5r9HYquCs4qzhlUipgeslZj25oWMVSDdPLi84sEXlVa9jrKDUhgWRS45E oTGmQVFQ9Sb7JJz3PfLJZp7YNezW5hcjb20mw71FHa7oexciRAn+EojIV8gTiohyzQY3 4Lkg== X-Gm-Message-State: AOAM533LEmRTLVc9tA9KRRArF8zzlAqVbLHIeVa/o500kF8EU4K6SnRJ gOmfxyZmcNA1vk95Evw6UIeyvdBFQ3JWSw== X-Google-Smtp-Source: ABdhPJwbOMGpw42qnWHQdaYwJJ9Ra1m/ezYr9se8c55fB6cPhvXPRTDD/O4SFKV3raIx3+gJiy5fDQ== X-Received: by 2002:ac8:505a:: with SMTP id h26mr25013961qtm.252.1620845596939; Wed, 12 May 2021 11:53:16 -0700 (PDT) Received: from birita.. ([177.194.37.86]) by smtp.googlemail.com with ESMTPSA id b8sm632966qtj.37.2021.05.12.11.53.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 May 2021 11:53:16 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2] nptl: Fix tst-pthread-gdb-attach for ptrace_scope equal 1 Date: Wed, 12 May 2021 15:53:12 -0300 Message-Id: <20210512185312.938871-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <87sg2rq1fi.fsf@oldenburg.str.redhat.com> References: <87sg2rq1fi.fsf@oldenburg.str.redhat.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" This is similar to the fix for elf/tst-pldd (2f9046fb059e94fe25): it checks ptrace_scope value (values higher than 2 are too restrictive to allow the test to run) and it rearranges the spawned processes to make the target process the gdb child. Checked on x86_64-linux-gnu with ptrace_scope set to 1. --- nptl/tst-pthread-gdb-attach.c | 59 +++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/nptl/tst-pthread-gdb-attach.c b/nptl/tst-pthread-gdb-attach.c index 901a120034..3c6eed07f6 100644 --- a/nptl/tst-pthread-gdb-attach.c +++ b/nptl/tst-pthread-gdb-attach.c @@ -23,16 +23,21 @@ #include #include #include +#include #include #include #include +#include #include +#include +#include #include #include #include #include #include #include +#include #include /* Starts out as zero, changed to 1 or 2 by the debugger, depending on @@ -141,7 +146,7 @@ subprocess_thread (void *closure) second thread, waiting for its value to change to 2, and checks that the main thread also changed its value to 1. */ static void -in_subprocess (void) +in_subprocess (void *arg) { pthread_t thr = xpthread_create (NULL, subprocess_thread, NULL); TEST_VERIFY (xpthread_join (thr) == NULL); @@ -149,6 +154,26 @@ in_subprocess (void) _exit (0); } +static void +gdb_process (const char *gdb_path, const char *gdbscript, pid_t *tested_pid) +{ + /* Create a copy of current test to check with gdb. As the + target_process is a child of this gdb_process, gdb is also able + to attach to target_process if YAMA is configured to 1 = + "restricted ptrace". */ + struct support_subprocess target = support_subprocess (in_subprocess, NULL); + + write_gdbscript (gdbscript, target.pid); + *tested_pid = target.pid; + + xdup2 (STDOUT_FILENO, STDERR_FILENO); + execl (gdb_path, "gdb", "-nx", "-batch", "-x", gdbscript, NULL); + if (errno == ENOENT) + _exit (EXIT_UNSUPPORTED); + else + _exit (1); +} + static int do_test (void) { @@ -179,25 +204,25 @@ do_test (void) free (threaddb_path); } - pid_t tested_pid = xfork (); - if (tested_pid == 0) - in_subprocess (); - char *tested_pid_string = xasprintf ("%d", tested_pid); + /* Check if our subprocess can be debugged with ptrace. */ + { + int ptrace_scope = support_ptrace_scope (); + if (ptrace_scope >= 2) + FAIL_UNSUPPORTED ("/proc/sys/kernel/yama/ptrace_scope >= 2"); + } char *gdbscript; xclose (create_temp_file ("tst-pthread-gdb-attach-", &gdbscript)); - write_gdbscript (gdbscript, tested_pid); + + /* Run 'gdb' on test subprocess which will be created in gdb_process. + The pid of the subprocess will be written to 'tested_pid'. */ + pid_t *tested_pid = (pid_t *) xmmap (NULL, sizeof (pid_t), + PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, -1); pid_t gdb_pid = xfork (); if (gdb_pid == 0) - { - xdup2 (STDOUT_FILENO, STDERR_FILENO); - execl (gdb_path, "gdb", "-nx", "-batch", "-x", gdbscript, NULL); - if (errno == ENOENT) - _exit (EXIT_UNSUPPORTED); - else - _exit (1); - } + gdb_process (gdb_path, gdbscript, tested_pid); int status; TEST_COMPARE (xwaitpid (gdb_pid, &status, 0), gdb_pid); @@ -205,10 +230,10 @@ do_test (void) /* gdb is not installed. */ return EXIT_UNSUPPORTED; TEST_COMPARE (status, 0); - TEST_COMPARE (xwaitpid (tested_pid, &status, 0), tested_pid); - TEST_COMPARE (status, 0); - free (tested_pid_string); + kill (*tested_pid, SIGKILL); + + xmunmap (tested_pid, sizeof (pid_t)); free (gdbscript); free (gdb_path); return 0;