Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp3040956pxa; Tue, 25 Aug 2020 09:50:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJza7e8TBbi3EYpKg3p5Svsek6AId8wUncuZ0VPVMKKSkRKv1SJcF/e6OrxKJKI7Y62/LqrP X-Received: by 2002:aa7:c60a:: with SMTP id h10mr7317685edq.140.1598374226736; Tue, 25 Aug 2020 09:50:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598374226; cv=none; d=google.com; s=arc-20160816; b=c4YWxuHN8ObZQqlU8zTkxfvkSx2fmZUQU/TlOKn5Mqov9by+DoSAx1BzFpiUGSQtfK LUGR+BtRQ0Zx8jHb3MkzOFcbR7ZjJ0sK+4wCsaqdsE4+1oyBQmwVQrSXvB/3rSY+YsVc DgCUah8Th6Z7c3VUfvo1edptL0RI6p5ICXKuVHvNz+rp/gC4CpNUwq4Vedoa38BIPCrk 1YuqPIfHqZFRxBIGJazGLjj0Wcax5kjBj8sHTghcyisMETEyU7fnBn7aYa+RfAJjfKjq OPUq1u06IqvfKwB9q5snty7YukvUBDbHEzgJZLuZonVffLm+wI5Cjp2LQAPKV1YLzAMK LlaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=9+gQw22k807KDyfkorM9fTQNEJunApDJJBt4bNeYNIk=; b=oHXqmvGty2HROIgdfg0cuDYBdWBvYbqoJPhNTrcw78gU8BJSE0pHMghA5ps0zIo2QE 7ppCwnaCND+evc29UeRLbIFhLeznHqjwaohed8t0l2P0geE3cUS+yQVYU8ZZb8z37oLr WD4x0M0H0CK4U91mLYSZ3CdjB7doLr/P3I8NbEvlPeJXq/A8z7jYV1SPT4ealBwmyGRy 6D0BeV5zn4R/1eWu9PpMmZZMEjGKYXOJsbkbUYu2O017M0OhmT8C6jFjWwl9X17wQywZ 0smaPXSN3J/D3uyXpkDySeOdQsQIiqdyXhTPdDTCs4WNqTvg+xuvWigQvxqL+VwaC5nV iM7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ixCNTeje; 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 w10si4470112edu.319.2020.08.25.09.50.03; Tue, 25 Aug 2020 09:50: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=@kernel.org header.s=default header.b=ixCNTeje; 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 S1726682AbgHYQqc (ORCPT + 99 others); Tue, 25 Aug 2020 12:46:32 -0400 Received: from mail.kernel.org ([198.145.29.99]:38810 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726356AbgHYQq0 (ORCPT ); Tue, 25 Aug 2020 12:46:26 -0400 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 35A3F20897 for ; Tue, 25 Aug 2020 16:46:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598373985; bh=h7VlFzTauWqMoJUomlXnnALAqMdX0hYV+PCoeAtM3rU=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=ixCNTejeXeS4PKewMPrRJz0TPr9F/qVGDl1NVmW5yLCnXY+CNkM1PeCIUztLTNI1J yhAfbnlEao6slE5NERq5TsP1mTb+Dm5wPk1oQLas3zK6gxiPkQseCT/9Yk0PjUeetv eiOxO5rEY6DiQrfVdAvuOPrz9UgKSZFqqYBcPWU8= Received: by mail-wr1-f53.google.com with SMTP id x7so7384256wro.3 for ; Tue, 25 Aug 2020 09:46:25 -0700 (PDT) X-Gm-Message-State: AOAM530MK1Tixq81K1bMpCUJF5viiGSgGL7YNbyQoeM82wL7b0uWhGFZ L+HktYY3+nvHg6nqK6Fv9tZQyxI7VoA/xG+YbQSIkQ== X-Received: by 2002:a5d:400e:: with SMTP id n14mr11308231wrp.75.1598373983617; Tue, 25 Aug 2020 09:46:23 -0700 (PDT) MIME-Version: 1.0 References: <7DF88F22-0310-40C9-9DA6-5EBCB4877933@amacapital.net> In-Reply-To: From: Andy Lutomirski Date: Tue, 25 Aug 2020 09:46:11 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [REGRESSION] x86/cpu fsgsbase breaks TLS in 32 bit rr tracees on a 64 bit system To: Kyle Huey Cc: Andy Lutomirski , "H. Peter Anvin" , "Robert O'Callahan" , "Bae, Chang Seok" , Thomas Gleixner , Ingo Molnar , Andi Kleen , "Shankar, Ravi V" , LKML , "Hansen, Dave" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 25, 2020 at 9:32 AM Kyle Huey wrote: > > On Tue, Aug 25, 2020 at 9:12 AM Andy Lutomirski wro= te: > > I don=E2=80=99t like this at all. Your behavior really shouldn=E2=80=99= t depend on > > whether the new instructions are available. Also, some day I would > > like to change Linux to have the new behavior even if FSGSBASE > > instructions are not available, and this will break rr again. (The > > current !FSGSBASE behavior is an ugly optimization of dubious value. > > I would not go so far as to describe it as correct.) > > Ok. > > > I would suggest you do one of the following things: > > > > 1. Use int $0x80 directly to load 32-bit regs into a child. This > > might dramatically simplify your code and should just do the right > > thing. > > I don't know what that means. This is untested, but what I mean is: static int ptrace32(int req, pid_t pid, int addr, int data) { int ret; /* new enough kernels won't clobber r8, etc. */ asm volatile ("int $0x80" : "=3Da" (ret) : "a" (26 /* ptrace */), "b" (req), "c" (pid), "d" (addr), "S" (data) : "flags", "r8", "r9", "r10", "r11"); return ret; } with a handful of caveats: - This won't compile with -fPIC, I think. Instead you'll need to write a little bit of asm to set up and restore ebx yourself. gcc is silly like this. - Note that addr is an int. You'll need to mmap(..., MAP_32BIT, ...) to get a buffer that can be pointed to with an int. The advantage is that this should work on all kernels that support 32-bit mode at all. > > > 2. Something like your patch but make it unconditional. > > > > 3. Ask for, and receive, real kernel support for setting FS and GS in > > the way that 32-bit code expects. > > I think the easiest way forward for us would be a PTRACE_GET/SETREGSET > like operation that operates on the regsets according to the > *tracee*'s bitness (rather than the tracer, as it works currently). > Does that sound workable? > Strictly speaking, on Linux, there is no unified concept of a task's bitness, so "set all these registers according to the target's bitness" is not well defined. We could easily give you a PTRACE_SETREGS_X86_32, etc, though.