Received: by 2002:a25:2c96:0:0:0:0:0 with SMTP id s144csp133207ybs; Sun, 24 May 2020 00:05:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzzktr6rnxSjtGOmULHk6ggAzJ6CpXyEFcr4Z5ijIRRJI6tg1+uAQaj3vddLcr1xzRmkeqa X-Received: by 2002:a17:906:bcf3:: with SMTP id op19mr14618688ejb.537.1590303926912; Sun, 24 May 2020 00:05:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590303926; cv=none; d=google.com; s=arc-20160816; b=u90m3c8Hgh6TvE95aQ5XNkkTn5os7eXjtlBbr5p0vytz1n5JZIiCRpJomRYM8fiBtK I8PfDxAOV3jfIdLTD78ax4OGEgABVyagotQg2wtu0sk146OB95sJjaRk5Bg3CkwDiRx6 yS+W1YrdKdp8M+t84QaWNc6Gy6hJ3snZC/m+V1HBZ87CJ0Glc8aSw8pbM3xbyuMGxysb d4LXarooYZNc+RRVNknQQ+E1iL+nAAVCJel+2CINwM4Fzi424109s0yq4Um5UIGPA30/ +ElPNR4is0bJIHMwx3G0uEQxBn7l657h8qh68vcGrZUADj6DXIVz+qaDNVyC1hZUTrqR bvmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=noxFunX2uI/FVRTYdrIrdHqbpc9T4lCMyDInJvvG8rs=; b=pSw34LsiJS3iD1HBzLS2t2djRzR0vbbN5G1RgvWUa21coou6jwlo5ZjOx2k7hwVLik O4e1CvRg5oyeScrkYoUAUXG02vszsM2zqBRktVDkqWYmo++UV1CWlebDlZTRp+h6MPVX ky9svSxToeb8VCwcrbt54KIvQluaUBrUsI08LGyJn92XN6KdC7ITKiBCZxJ8rmZSHRyR 02QFo0dH3BWAQG5JuR3H/N4ToqoracxcbgP55IlC+G7kaGXSgzSv0m9d8L+Erc7oDZn9 jXYM+UO8clzeC64Fw6dMJwGamZAr4330OFzHkFppakmY4D0G0y2IG4t98vQgMrUJhjm9 WIJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@juliacomputing-com.20150623.gappssmtp.com header.s=20150623 header.b=sKmEqVf0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t16si7007358edr.373.2020.05.24.00.04.25; Sun, 24 May 2020 00:05:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@juliacomputing-com.20150623.gappssmtp.com header.s=20150623 header.b=sKmEqVf0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728106AbgEXG5M (ORCPT + 99 others); Sun, 24 May 2020 02:57:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726331AbgEXG5M (ORCPT ); Sun, 24 May 2020 02:57:12 -0400 Received: from mail-io1-xd41.google.com (mail-io1-xd41.google.com [IPv6:2607:f8b0:4864:20::d41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E9F1C061A0E for ; Sat, 23 May 2020 23:57:12 -0700 (PDT) Received: by mail-io1-xd41.google.com with SMTP id f3so15857791ioj.1 for ; Sat, 23 May 2020 23:57:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=juliacomputing-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=noxFunX2uI/FVRTYdrIrdHqbpc9T4lCMyDInJvvG8rs=; b=sKmEqVf0g2OLue9Rp1A/WkSXf70RjVaTuI3MbjU8wS/C7acN/fmBvNE67EieNmqvMf jRe1vfe9CkK76yclpUEWyB5YXYSqHDFJ772rO2kUs4rM8fmV6bBR7xZPMoIXLwN4m3hq 81SmFj/W6fm/8MOEpkzOpx48SdpvSfHm/VTOXCmuHsaFKpvKF9qbFyprxsq8P8Gd2i8i 3MT3a3JnCibxEtC7K1Gjrw0WVb8WJpIPXqWIPvI9qnsqInY1p0gQ7hSWbyLLxNTNn1ta ezpvkeuMoxqR9Um0oPBP4N58/efb+RFrBlPbDxXOdHsAe7izw0VhQD7R3e4ueAIVhQX9 VlPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=noxFunX2uI/FVRTYdrIrdHqbpc9T4lCMyDInJvvG8rs=; b=Nv/tSBMUqpREV+XQ0PWzJJ2m/KAB6LdNVm6bg9HU3WtSw9coFub2JfXvAluKpO82/V 0AMA52pQaLKRPvAZbOI5EVZ/2rrOnbNH4B49UnR0qGPEAqzo4kDgNpT7pk6L1nx8IJv1 m5aNiBDdKcThDkXufZnj2UkFQVH9K24DrpYSOFpBK2KVKNigMMACnCtKhtyjmYE3FLwR cMb3bvWzy8x3OZvECeQySLnqSFPOql3ARmF6asic8gqt1gd/f8i7eykZYeAW2IxdFr0w 35uBSkNt/50vd/QPx3VgRbJEV/7JhuMsr2GBo5jFG6iYd0pO0YBZwdxbM/DlfQfXLES2 XwzA== X-Gm-Message-State: AOAM5309SUiih/il20ICY2k9av6g/vbW1ZvF1tTXe88kJ8ZIiaBEtVnx DcoXuBH8bo4VcfbNLUw1xL8Xj5opavARcPdVDwwV0w== X-Received: by 2002:a6b:6b04:: with SMTP id g4mr9182371ioc.75.1590303430943; Sat, 23 May 2020 23:57:10 -0700 (PDT) MIME-Version: 1.0 References: <20200519081551.GA9980@willie-the-truck> <20200520174149.GB27629@willie-the-truck> In-Reply-To: From: Keno Fischer Date: Sun, 24 May 2020 02:56:35 -0400 Message-ID: Subject: Re: arm64: Register modification during syscall entry/exit stop To: Will Deacon Cc: linux-arm-kernel@lists.infradead.org, Catalin Marinas , Oleg Nesterov , Linux Kernel Mailing List , Kyle Huey Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Just ran into this issue again, with what I think may be most compelling example yet why this is problematic: The tracee incurred a signal, we PTRACE_SYSEMU'd to the rt_sigreturn, which the tracer tried to emulate by applying the state from the signal frame. However, the PTRACE_SYSEMU stop is a syscall-stop, so the tracer's write to x7 was ignored and x7 retained the value it had in the signal handler, which broke the tracee. Keno On Sat, May 23, 2020 at 1:35 AM Keno Fischer wrote: > > I got bitten by this again, so I decided to write up a simple example > that shows the problem: > > https://gist.github.com/Keno/cde691b26e32373307fb7449ad305739 > > This runs the same child twice. First vanilla where it prints "Hello world". > The second time, using a textbook ptrace example, to only print "world". > The problem here is that by the time the ptracer gets around to restoring > the registers, it's no longer in a syscall stop, so the write to x7 does not > get ignored and the correct value of x7 gets clobbered. > I copied the syscall definition from musl, so the compiler thinks x7 is > live, and we can see an assertion. > > Output on my machine (will depend on compiler version, etc.): > ``` > $ gcc -g3 -O3 ptrace_lies.c > $ ./a.out > Hello World > World > a.out: ptrace_lies.c:49: do_child: Assertion `v3 == values[2]' failed. > a.out: ptrace_lies.c:134: main: Assertion `WIFEXITED(status) && > WEXITSTATUS(status) == 0' failed. > Aborted (core dumped) > ``` > > However, I don't think that whether or not the compiler thinks that x7 is > live is the problem here. The problem is entirely that this mechanism > prevents the ptracer from precisely controlling the register state. While > basic ptracers don't need this feature (strace), > more advanced ptracers (think criu, etc.) absolutely do want to precisely > control what the register state is. > The ptracer I'm working on (https://rr-project.org/) > happens to be an extreme case of this, where it wants *bitwise* equivalent > register states such that it can run the same code many times and get > the exact same results. > > Also, if the issue was just that the kernel clobbered x7, that would be fine > we could deal with that no problem. However, it's much worse than that, > because the behavior of the kernel with respect to x7 depends on what > kind of ptrace stop we're in and even worse, in some kinds of stop, > there's absolutely no way to get at the actual value of x7. > > > Hmm, does that actually result in the SVC instruction getting inlined? I > > think that's quite dangerous, since we document that we can trash the SVE > > register state on a system call, for example. I'm also surprised that > > the register variables are honoured by compilers if that inlining can occur. > > I haven't gotten to trying SVE yet, so I appreciate the warning :). That said, > deterministic clobbering of registers is fine. Even changing the registers to > random junk is fine. We're happy to read those registers through ptrace. > The problem here is that the kernel lies about what the contents of the x7 > register is and discards any writes to it. > > I really hope we can come up with a solution here, I'm already dreading > the next time I unexpectedly run into this and have to add yet > another special case :(. > > Keno