Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp3225474pxb; Tue, 12 Jan 2021 09:16:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJzQriRrB8FmNO5SWVTDyi0zGyNplyNlqEXyhVjT7jqp7/9EbbxCPqqQbkEONzpOE9zODMxX X-Received: by 2002:a50:8e19:: with SMTP id 25mr109426edw.263.1610471768784; Tue, 12 Jan 2021 09:16:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610471768; cv=none; d=google.com; s=arc-20160816; b=F/7xhFyQRQZlNMZaAt+Z6Foy6DqejiWaduuCO8N8OXRf7SZ34gAWnUmQYJDx4j7uF4 FbOIQh2SzQJugvcmpd0Mjy2dhEoupj2+ADMRV2JfdLLPbSxSHgkJ5fvdd+fA9iY28TPG 2TIGXuEy/H0CGRNk8cX0KVTcdlECT8fOnvEH2rcBX6dNZkZnBzhtERjP60q1FjDwH20A fqIKeV9IZD+wUJi/cmAmRbrkDmEWx7EAasAFoJ3gHiX7jOLcK9IZbuyqUze9YguLry8r 6vN/EJl8kfEoEWsJ+ylITO98C4RRg26M1V1zxG5l2q5Iy4FhugqzuZ1K9TvZxVJso7r5 yhaw== 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=J5c4zTB41x2l2Ko1l1MSLInY6XtbwmsR3FTCUm0jzNo=; b=LXuWmc2fnC/qvd4QY6qm/gmb781/mCwZRW/vKHLHYQDrG0zqs8nkmRyVRpsQWpgd5C wIp408xJ2XiWDRM3+Gu3L0YI2wlaeTREhEQmT4gTngXcNDgcJ7Y398MFIbULOR5Y+7AE gmwiJm+xC6aLRTz6KxIsqeFbZDoVkn45ZJR2eeocSZtN6aSe62UekL0IPBOzz4OJu5MM mF2nbaoN5xIRwF9ZSJ5b+zjezuYsAD9V3IFXbNsOxak8Cyf98iWmUXOSf1c4mMaH1d8Q 5bS7mtTuR0KOxmYdRcJKgB55kuVcBOL4tc8ghQNsvN4oTJU8A9zCZYcl5AaWA4EWNX5n QymQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=vCkxH3Of; 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 k20si1545316edo.493.2021.01.12.09.15.45; Tue, 12 Jan 2021 09:16:08 -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=vCkxH3Of; 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 S1730780AbhALROJ (ORCPT + 99 others); Tue, 12 Jan 2021 12:14:09 -0500 Received: from mail.kernel.org ([198.145.29.99]:51180 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727203AbhALROJ (ORCPT ); Tue, 12 Jan 2021 12:14:09 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5C9452311F for ; Tue, 12 Jan 2021 17:13:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1610471608; bh=twHLJ9CGnv0NIwQsAyiyk3+GTyCQ6W+V9TFfln36PjQ=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=vCkxH3OfI4Fl/Ss7xMwzgak+vnn4/yGqSRA6WKo0q004QMbEdEWZeS91BHhTtZzHR q/TVi/X8cjfDe6Rir2s1HyecUgYfWtPf7yQ6AHcwi165cn8SCTpzyimuktbUTofEvy Wl8nySJUuyinfWWq+/+aOXmmR4fpLaUBQqgWd+dU7r0pCCyvRVfc7ncJ3OO9Z5+QfS OHmkNhou8dJahzhBrnww1e1uK1igmlttHzo7QasDeLJbQu4mR9AFTW28L5GWrIb9NJ gyy7sp2FPn7P39np+FRwND5z3I0aGIzDQ5/1Cmv+5IpP6EOz18xcqClHrVDgfwoElu 8pnviJAkVcsyA== Received: by mail-ej1-f42.google.com with SMTP id d17so4559273ejy.9 for ; Tue, 12 Jan 2021 09:13:28 -0800 (PST) X-Gm-Message-State: AOAM532BDncMwviv3OTlYzZUwdzwUIFeA/vCKNgEGwfY7BVdNE3v7cwi 41q/zeNDpgLldcbOFakUS/nztNbxMhkrB2mVa+EHwg== X-Received: by 2002:a17:906:ae55:: with SMTP id lf21mr3837689ejb.101.1610471606875; Tue, 12 Jan 2021 09:13:26 -0800 (PST) MIME-Version: 1.0 References: <20210111200027.GH25645@zn.tnic> <5B5C1F0A-9780-4E42-BC65-742BAEE920BF@intel.com> In-Reply-To: From: Andy Lutomirski Date: Tue, 12 Jan 2021 09:13:15 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: gdbserver + fsgsbase kaputt To: "Metzger, Markus T" Cc: Andy Lutomirski , "Bae, Chang Seok" , Borislav Petkov , "tdevries@suse.com" , x86-ml , lkml Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 12, 2021 at 9:02 AM Metzger, Markus T wrote: > > > [ 26.990644] getreg: gs_base = 0xf7f8e000 > > [ 26.991694] getreg: GS=0x63, GSBASE=0xf7f8e000 > > [ 26.993117] PTRACE_SETREGS > > [ 26.993813] putreg: change gsbase from 0xf7f8e000 to 0x0 > > [ 26.995134] putreg: write GS=0x63; old GSBASE=0x0 > > [ 26.996235] PTRACE_SETREGS done > > > > That's gdbserver reading GS and GSBASE and then telling the kernel to > > set GS to the same value and GSBASE to 0. > > > > I can come up with horrible kernel hacks to try to work around this, > > but gdbserver is really giving the kernel bad instructions here. > > I agree that this looks like a GDB bug rather than a kernel bug. GDB > should preserve the GS_BASE value if it doesn't intend to change it. Indeed. But we have this pesky no-userspace-regressions policy in the kernel. So the question I have is: is this enough of a regression that we need to hack around it in the kernel? The specific broken use case seems quite niche: 64-bit gdbserver targeting 32-bit userspace. It's taken two-and-a-half kernel releases for anyone to notice, because sensible people use plain gdb for local debugging and gdbserver for debugging VMs, embedded targets, and such. If we do decide we need to fix this in the kernel, I think we need to simultaneously add ptrace GPR accessors that work better than the current mess. We should have explicit 32-bit GPR access and 64-bit GPR access, and they should do precisely what is requested without regard for caller's ABI or the target's CS.L. And we should add some API that can be used to look up a segment descriptor so that ptrace() users can emulate segment writes. At the same time, we can make SETREGS so the descriptor lookup when writing GS and totally ignore the GSBASE value if CS.L == 0, and we backport the entire mess back to 5.9 and ask all debugger maintainers to update their code to use the new APIs that are kludge-free in future releases. Everyone wins except for anyone who wants to understand exactly what the legacy API does and the suckers who have to write test cases for all of this.