Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp507312pxb; Wed, 3 Feb 2021 10:22:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJwmOgGIUlsgcee7ut/7OYjU2YOH2JTujtvyLepajOsI+qPOU9EpHRYky9/VhGccZ1RLhl4G X-Received: by 2002:a17:907:3da0:: with SMTP id he32mr4417585ejc.265.1612376558240; Wed, 03 Feb 2021 10:22:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612376558; cv=none; d=google.com; s=arc-20160816; b=AvN5ITZrMZYtSnYFPtzrDAUYl5vVUIQMtzCl0zXRS3t2g0308Y74jd8yq2Udz0xw0J rs5c4eK0hbkIUD2Dfmd4ulh826PTOGDUtdVrTsFznvFfVUY5//xIStcVtbv9kTfJBewj 1OAKZ4QZH7VZxanp2zCsvjJV/BkKlSm0vRsO4c0Aimf0CJH63R0GfH7ckiNSHKXG5q7V zmieZnujPj/vMLBe7G2bL3VmH71efEYvTNiXzNc6fur4bKrQaG5YTwE+w1TLjXUWWj43 my9Aa2V74lBNrm4KSaADRweuySNh2tiIqelEpFibiFGjHpY8zOYyS7pHn68Ko34xCT+w q5vQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=GHFw4p40kJ6Nmg6MJI1c8tLYFhzO696KN0WtJugMxNQ=; b=ks6paDLXx5CoICU1c5J9oIPtQ4iRuNJNJwxmAh6Jx/I7sMSbjTdHQk4odA/BOoXh/C sWYfGT7wUtZm0XAWjC0TT7GknPA/bO6sp1dCEa4O7fc8jdNqMvqy3NMLEShPw4C41O5c 8eQcIpa5J4yRKEEiCK09oQwJj1q5NjzR8jrPevJ4Yy+eVLqvGUs98zVH4JwbAd6H7yDW u3xuRGKEbgp/gNI/SGuiBND9J6ALDYP3+l3UOs4PqnzCkUXk0IODuOaFbBnanI8AxJgY cDZtWEsOlta2j/F1qg7+1CqefbITsjfdQ8YyGHbvuA4eP4MvNBQsfJrX/NBLDC1ZKjJh Nhrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OfzxJJ+e; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b6si1794675eje.118.2021.02.03.10.22.06; Wed, 03 Feb 2021 10:22:38 -0800 (PST) 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=@kernel.org header.s=k20201202 header.b=OfzxJJ+e; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232555AbhBCSTS (ORCPT + 99 others); Wed, 3 Feb 2021 13:19:18 -0500 Received: from mail.kernel.org ([198.145.29.99]:58274 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231941AbhBCSTN (ORCPT ); Wed, 3 Feb 2021 13:19:13 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id C86FD60295 for ; Wed, 3 Feb 2021 18:18:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1612376313; bh=zdQgjgjUk3KJOQ14/l2VAanex15NlCvPFgpWCWqM3u0=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=OfzxJJ+e2xedb2QhiyU0mb8Y1mjh6MNeSY57sFyd3QSHm0P++USv7ly9zcIfyWTRy nFbS3ZY876I20trgwrIGUDdQxoc0priWUCasdcyR0wPMgEYXMYnn0I+fcc/FRlDAok 9J+v7GUt4PKLFs3f+s3Ubj75Zi53eExcrdT7za5+5Z7zI8yCvwrQfT8RWJ4EFipU95 o4ok9melo2WJqnBfL4+JTCZFw0QBZxR7fnNN80xDGNV/WlAzre5apmYjfc65CszWr8 GW+6dzgF1xHq2rBjuk2Zs6ERB3mysDuQkOPn4hHCzsexlVo8i3nTCVOzASFww0kx1b ldDckC5zekrjA== Received: by mail-ed1-f54.google.com with SMTP id df22so803678edb.1 for ; Wed, 03 Feb 2021 10:18:32 -0800 (PST) X-Gm-Message-State: AOAM532Bc9hoZlZf0RiJ/octgCZc0AmwZ3tdjA1IY42iv4b4FcyQmPLP S9I8vZO1Jg5Xjnq1j7ecDN9mwuwyBELvn2bpN3U/Cg== X-Received: by 2002:aa7:d4d2:: with SMTP id t18mr4504548edr.238.1612376311167; Wed, 03 Feb 2021 10:18:31 -0800 (PST) MIME-Version: 1.0 References: <87h7mtc9pr.fsf_-_@collabora.com> In-Reply-To: From: Andy Lutomirski Date: Wed, 3 Feb 2021 10:18:11 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] entry: Fix missed trap after single-step on system call return To: Linus Torvalds Cc: Gabriel Krisman Bertazi , Kyle Huey , Thomas Gleixner , Andy Lutomirski , open list , "Robert O'Callahan" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 3, 2021 at 10:10 AM Linus Torvalds wrote: > > On Wed, Feb 3, 2021 at 10:00 AM Gabriel Krisman Bertazi > wrote: > > > > Does the patch below follows your suggestion? I'm setting the > > SYSCALL_WORK shadowing TIF_SINGLESTEP every time, instead of only when > > the child is inside a system call. Is this acceptable? > > Looks sane to me. > > My main worry would be about "what about the next system call"? It's > not what Kyle's case cares about, but let me just give an example: > > - task A traces task B, and starts single-stepping. Task B was *not* > in a system call at this point. > > - task B happily executes one instruction at a time, takes a TF > fault, everything is good > > - task B now does a system call. That will disable single-stepping > while in the kernel > > - task B returns from the system call. TF will be set in eflags, but > the first instruction *after* the system call will execute unless we > go through the system call exit path > > So I think the tracer basically misses one instruction when single-stepping. I was hoping you wouldn't ask this :) The x86 architecture is fundamentally a bit busted here. If we return from a system call with SYSRET and TF is set in R11, then SYSRET traps, which means that #DB is delivered before executing a user instruction. I have been asking Intel for quite a while to document this, and they said they did, but I still can't find it. IRET is the opposite: if we return from a system call with IRET and TF is set on the stack, we execute one user instruction and then trap. So if we want to reliably single-step a system call and trap after the system call, we just need to synthesize a trap on the way out. Doing this and getting all the nasty corners (e.g. sigreturn setting TF, sigreturn *clearing* TF, signal delivery as part of the syscall, ptrace mucking with TF) etc right might be nontrivial. I suspect the behavior back in the bad old asm-entry-path days was at best inconsistent. --Andy