From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E4B08C433E0 for ; Wed, 27 May 2020 14:39:30 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8BE4420873 for ; Wed, 27 May 2020 14:39:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="RRN5z/Li"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="aYhE1Olg" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8BE4420873 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender:Content-Type: Content-Transfer-Encoding:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date:Message-ID:From: References:To:Subject:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=MhSCuH+KIzvglUFtKIRdWY3HIcKbplD5lNR5CpyvruA=; b=RRN5z/Lil5Tv4JWyUDfQb8hZj MLUBbPcFhPMMpegu1XCA7y5XlT0cWVASmHMLrkri8yXrNAybREnW7SQaN/Znl06I+VbWLZCERuq/6 Ke5qk+kwgVcIxe28A/mEO8Ww+g10t3IQxeTo+mt1kVB1qVnulXzilBwJISVNn9hSPWg+i2tOMHoIQ MdKfGSfRdLOfLKDiCGrDVaw7x+JBfq3adh89/stLHzwpl2kNcvUMyWolxXAVFyEBnCavxJ+TdiDtm VTLnEBgSyvHSUjlImtbN8qpe+Y4Y0aYmqFUxBsKH+VMl+FCuJEE68XqliF4xrwJBqSKEM87Rk7od5 FnCrrc1tw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jdxDE-0007m7-ML; Wed, 27 May 2020 14:39:20 +0000 Received: from mail-qt1-x841.google.com ([2607:f8b0:4864:20::841]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jdxDB-0007lX-5j for linux-arm-kernel@lists.infradead.org; Wed, 27 May 2020 14:39:18 +0000 Received: by mail-qt1-x841.google.com with SMTP id x29so6109740qtv.4 for ; Wed, 27 May 2020 07:39:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=c3iUyv2ACu3GpcAgyaKwrhyX2JOcPpWzj8/UV5t9xCg=; b=aYhE1Olgr+ev3gdMLXXeSwYykA/jP5JIRtVKuD/FBn/IgJV5b55wDqPcDi1g/8JYND wkJPzi+iiE1K6SML3E8pWoK9nPiayL/l+hLzt3AFswGG0f2JqcVGKYymQLEGrmb0xFzY QBGz2N7497XPfQVZE+zjRTK8f0I2Ny7/bhpdfGNIVuiKi7MF78oJQSGjceyqdnkZo+pr kgEttGtonZrrA/Ff7Nh2+0Ld84fO/v1lzsZP11mnT4Y42Chrm+lBMyWR4oW9h/BSm4mg dclMwUS9oDc6zF3VweR06s0D/jszxi618kGcYNgaN5pzVCfMoje3gruLA5lnz73eQ3q2 fhgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=c3iUyv2ACu3GpcAgyaKwrhyX2JOcPpWzj8/UV5t9xCg=; b=C0uFCAGDeP/NFofaTgjB0P7Taasggn5709aipSt5RO25d/u/OYyKQCixPhVtL3HAI8 Fw1Wq4Ae58QSYXOYVsz6YrUSfXEjUlMA4+8UpmUBdAQgB14iE7EK0u1R2OcYL1eztqYV NvCc2CK3/tnpqt0ckTaE7WjG8EdsuR8rE+1Nf686MQv48HhLlumhCBRv9MovSyzWuT0L aPnK63a73VLjdJ7BQ0PWXbGL8gKVbY5Adm6S3lS39guJElS1LZ2ue0TJLhTihydiVeQc 4fRN+My+obH+kCK2RKauMCoBnI/FzDn9P1k1te2kPM0jn8l8bG7tlH21mcZsx0ubtUC1 EE6w== X-Gm-Message-State: AOAM531fyiLVE2xrT+bbxvW+75+rosGYYVL9c6oxpdRqZK46Z4Tq4dkB ONuWqU8KY6K5VQ1HsdKbWiNOxJ0dh4o= X-Google-Smtp-Source: ABdhPJwPl9yEmykceJY0i74+YGfu60qsPW57q1HRJOLGF2NEzt9QKY5A34dowe7j/nYE8lSqKayMmg== X-Received: by 2002:aed:23d2:: with SMTP id k18mr4653133qtc.224.1590590354891; Wed, 27 May 2020 07:39:14 -0700 (PDT) Received: from [192.168.0.185] ([191.34.87.30]) by smtp.gmail.com with ESMTPSA id r77sm21209qke.6.2020.05.27.07.39.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 27 May 2020 07:39:13 -0700 (PDT) Subject: Re: [arm64, debug] PTRACE_SINGLESTEP does not single-step a valid instruction To: Mark Rutland , Will Deacon References: <1eed6d69-d53d-9657-1fc9-c089be07f98c@linaro.org> <20191118131525.GA4180@willie-the-truck> <307ece3d-4e9d-21c4-0abf-9f4f3b313e74@linaro.org> <82cb3dea-db82-1c71-3b08-957102b85c93@linaro.org> <20200213120115.GD1405@willie-the-truck> <20200220130222.GA28634@lakrids.cambridge.arm.com> <20200220132941.GB14459@willie-the-truck> <20200221111652.GB45022@lakrids.cambridge.arm.com> From: Luis Machado Message-ID: Date: Wed, 27 May 2020 11:39:10 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <20200221111652.GB45022@lakrids.cambridge.arm.com> Content-Language: en-US X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200527_073917_216293_47BBC3EE X-CRM114-Status: GOOD ( 37.44 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arm-kernel@lists.infradead.org Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi, On 2/21/20 8:16 AM, Mark Rutland wrote: > On Thu, Feb 20, 2020 at 01:29:42PM +0000, Will Deacon wrote: >> Hi Mark, >> >> Thanks for having a look. >> >> On Thu, Feb 20, 2020 at 01:02:22PM +0000, Mark Rutland wrote: >>> On Thu, Feb 13, 2020 at 12:01:16PM +0000, Will Deacon wrote: >>>> diff --git a/arch/arm64/kernel/ptrace.c b/arch/arm64/kernel/ptrace.c >>>> index cd6e5fa48b9c..d479fbcbd0d2 100644 >>>> --- a/arch/arm64/kernel/ptrace.c >>>> +++ b/arch/arm64/kernel/ptrace.c >>>> @@ -1934,8 +1934,8 @@ static int valid_native_regs(struct user_pt_regs *regs) >>>> */ >>>> int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task) >>>> { >>>> - if (!test_tsk_thread_flag(task, TIF_SINGLESTEP)) >>>> - regs->pstate &= ~DBG_SPSR_SS; >>>> + /* https://lore.kernel.org/lkml/20191118131525.GA4180@willie-the-truck */ >>>> + user_regs_reset_single_step(regs, task); >>> >>> I think this change means we do the right thing for signal entry/return >>> and ptrace messing with the regs. Instruction emulation seems to do the >>> right thing via skip_faulting_instruction(). >>> >>> I think there are a few more single-step edge cases lying around (e.g. >>> uprobes, rseq), but it looks like those have to be fixed separately. I >>> fear fixing uprobes might require a largler structural change to single >>> step, but ignoring uprobes the changes above seem to be sound. >> >> Rseq should just abort when delivering the step signal and I'm not sure I >> see the issue with uprobes. Can you elaborate on your concerns a bit, >> please? > > For rseq I wasn't sure what state PSTATE.SS should be when we head to > the abort handler -- I think the sensible thing would be that it > immediately triggers a single-step exception, but I don't see where we'd > clear PSTATE.SS to ensure that. > > For uprobes I fear that the uprobes xol single-stepping might end up > conflicting with the regular ptrace single-stepping, and that the > uprobes emulation might not always advance the state machine correctly. > >>> If userspace doesn't consume the SS value today, I wonder if we should >>> hide it when dumping the SPSR to userspace, so that userspace has a >>> consistent view regardless of whether it's being stepped. >> >> You can't really hide it though, because '0' has a meaning so I don't think >> it gains us a lot other than increasing the scope of the change. > > I think that it reduces the likelihood that single-stepping a program > changes its behaviour unexpectedly. This patch makes the kernel > disregard the PSTATE.SS value provided by userspace, so what is gained > by exposing PSTATE.SS to userspace at all? > > I do agree that there are potentially subtle landmines here; I just > can't see a legitimate reason for userspace to need the value. > >>> I'll try to dig into the uprobes stuff this afternoon, just in case >>> that >>> needs us to do something substantially different. >> >> Thanks. > > I didn't get the chance to do this yesterday, but I did think of another > potential problem. > > I *think* that when attempting to single-step a syscall, if prior to > return from the syscall the tracer messed with the tracee's regs (e.g. > to mess with arguments or the retun value) then valid_user_regs() will > set the SS bit, and upon return from the syscall the next instruction > would be executed rather than first raising a single-step exception. > > This patch relies on valid_user_regs() being a signal that PSTATE.SS is > stale, but that's not always the case. To handle that generally I > suspect we need two bits of state rather than just TIF_SINGLESTEP. > >>> The existing logic in valid_user_regs() doesn't make sense to me, given >>> SPSR_EL1.SS is immaterial unless MSCDR_EL1.SS == 1. I'm not sure if that >>> was overzealous or I've forgotten an edge case that we cared about in >>> the past. >> >> I think it was just part of sanitising the registers to a consistent value, >> but I agree that it wouldn't have a functional impact. > > Thanks for confirming my understanding. I guess this may have minimized > the cases where userspace saw PSTATE.SS set. > >>>> diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c >>>> index 339882db5a91..bc54bdbfd760 100644 >>>> --- a/arch/arm64/kernel/signal.c >>>> +++ b/arch/arm64/kernel/signal.c >>>> @@ -505,8 +505,12 @@ static int restore_sigframe(struct pt_regs *regs, >>>> forget_syscall(regs); >>>> >>>> err |= !valid_user_regs(®s->user_regs, current); >>>> - if (err == 0) >>>> + >>>> + if (err == 0) { >>>> + /* Make it look like we stepped the sigreturn system call */ >>>> + user_fastforward_single_step(current); >>>> err = parse_user_sigframe(&user, sf); >>>> + } >>> >>> I don't understand this. AFAICT we don't likewise for other SVCs, so >>> either I'm missing that, or there's something else I'm missing. >>> >>> Why do we need to step sigreturn but not SVC generally? >> >> Because we restore the SPSR from the sigframe during sigreturn, so we will >> end up with PSTATE.SS set when it should be cleared. > > Ah, I see. As above, I think we can hit a similar case when > single-stepping an SVC for a regular syscall. > > Thanks, > Mark. > Did we have any further developments on this front? Has a patch made its way upstream for review? _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel