From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 121221 invoked by alias); 5 Sep 2019 17:58:35 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 121182 invoked by uid 89); 5 Sep 2019 17:58:33 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-3.6 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 05 Sep 2019 17:58:31 +0000 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8092AC04BD48; Thu, 5 Sep 2019 17:58:30 +0000 (UTC) Received: from localhost (unused-10-15-17-196.yyz.redhat.com [10.15.17.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id 03CDB5C258; Thu, 5 Sep 2019 17:58:28 +0000 (UTC) From: Sergio Durigan Junior To: Pedro Alves Cc: GDB Patches , Eli Zaretskii , Ruslan Kabatsayev Subject: Re: [PATCH v2] Improve ptrace-error detection on Linux targets References: <20190819032918.3536-1-sergiodj@redhat.com> <20190826183205.19008-1-sergiodj@redhat.com> <28c4f743-91f1-59c3-83ff-3f791811f996@redhat.com> <87mufrai1z.fsf@redhat.com> <87zhjjrh7n.fsf@redhat.com> <87d0gfrewj.fsf@redhat.com> <587b2e6b-15ec-0ba1-f425-82ec45eb4ca7@redhat.com> <875zm7rd9v.fsf@redhat.com> <8b3979cd-8c24-42e0-334b-de02cda2b799@redhat.com> <871rwvrbf5.fsf@redhat.com> <491150f7-636f-4629-ac68-2a4597bd703d@redhat.com> Date: Thu, 05 Sep 2019 17:58:00 -0000 In-Reply-To: <491150f7-636f-4629-ac68-2a4597bd703d@redhat.com> (Pedro Alves's message of "Thu, 5 Sep 2019 13:19:26 +0100") Message-ID: <87mufipquz.fsf@redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-IsSubscribed: yes X-SW-Source: 2019-09/txt/msg00057.txt.bz2 On Thursday, September 05 2019, Pedro Alves wrote: > On 9/4/19 10:36 PM, Sergio Durigan Junior wrote: >> On Wednesday, September 04 2019, Pedro Alves wrote: >> >>> On 9/4/19 9:56 PM, Sergio Durigan Junior wrote: >>>>> You could start gdbserver with the restrictions off (like a long >>>>> lived daemon), and then while gdbserver is running enable >>>>> restrictions, I suppose. >>>> Ah, right, I think that would also work. >>> >>> I'm interested in knowing whether the error percolates to >>> the gdb side in a reasonable form. >> >> The attach error is displayed by GDB, but the information about ptrace >> restrictions is not. >> >> This is what I see on GDB: >> >> (gdb) attach 32378 >> Attaching to process 32378 >> Attaching to process 32378 failed >> >> And this is what I see on gdbserver: >> >> gdbserver: Cannot attach to process 32378: Permission denied (13), >> the SELinux boolean 'deny_ptrace' is enabled, you can disable this >> process attach protection by: (gdb) shell sudo setsebool >> deny_ptrace=0 >> >> >> I think there's something wrong with the terminal settings because >> newlines aren't being respected here. But the message is still there, >> and the user can still understand it, I think. > > Odd. We should fix that... OK, then. > But this made me realize something. Here: > > static int > linux_child_function (void *child_stack) > { > - ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0); > + if (ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0, > + (PTRACE_TYPE_ARG4) 0) != 0) > + trace_start_error_with_name ("ptrace", > + linux_ptrace_me_fail_reason (errno).c_str ()); > + > > This is code that is run by the fork child. Recall that we > had introduced trace_start_error_with_name instead of using > perror_with_name / error, because between after fork and before > exec, we can only safely use async-signal-safe functions. Ah, I didn't remember the exact reason, thanks for refreshing my memory. > But that linux_ptrace_me_fail_reason call is doing a lot of > non-async-signal safe things... Fair enough. > I think the right approach would be to make the child pass back the errno > value to the parent, and then have the parent, do the dlopens, the > std::string/malloc uses, etc. and print the error messages. > > The traditional way to pass data around like this is via a pipe. > darwin-nat.c uses a pipe around fork, see ptrace_fds, though for a > different reason. Funny, at first (when I was trying out the patch) I noticed that this ptrace call was failing, but thought that, because it is being called inside the child, it would be good if we could pass errno back to the parent, as you said. But then I just decided to go the easy way and call trace_start_error_with_name directly there. I'll look into passing the errno back to the parent via pipes, as suggested. >> Ideally we'd have GDB also display the ptrace restriction warning, but I >> think that if the user can see that the attach failed, she will likely >> look at what happened with gdbserver, and will see the error there. > > I don't think that assuming that the user has easy/convenient access > to gdbserver's terminal is a great assumption. Consider IDEs automating > things, or gdbserver really being used as a service in a container, etc. > > Note that at least for Yama, you can have ptrace allow PTRACE_ME, while > PTRACE_ATTACH gets refused. So it's not guaranteed that gdbserver would > exit out early on start up, meaning, I think that handling this > gdbserver + "(gdb) attach" error case isn't being pedantic. > >> If >> we were to show the message on GDB, we'd also have to rewrite it in a >> way that explains that the command needs to be run at the target, which >> may confuse things. > Or it may not confuse things? I don't think that should prevent > improving things. Surely we can find some way to express the message > without causing confusion. I'm not sure it does, but if it does, > maybe we can just append a "on the target system" somewhere? "on the target system" can be confusing for newcomers. But sure, I can certainly find a way to write the warning in a non-confusing way. I'm just thinking about the extra work involved to implement this. I'll design something here and send a v4 when ready. Thanks, -- Sergio GPG key ID: 237A 54B1 0287 28BF 00EF 31F4 D0EB 7628 65FC 5E36 Please send encrypted e-mail if possible http://sergiodj.net/