Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp828082pxb; Tue, 3 Nov 2020 13:42:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJwuonyqbVzCi8ZtL/PdfUYgDN+DfS1t8YhDpanyUMlPV6yLTW6yqlDuuMtb+21BKsKOpa77 X-Received: by 2002:a05:6402:1148:: with SMTP id g8mr24879542edw.271.1604439757068; Tue, 03 Nov 2020 13:42:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604439757; cv=none; d=google.com; s=arc-20160816; b=VsQ3gPw0QlJbYzE81qoFblqUUZfyj7MxYKcmCmRuhXqEGo6ILAcFfMXobpan8tGRar k9RXIzNjyYUkEy6LH6VBeV5CFjC/2nddxJb9PZhvhKzweZ7K2LQYmCZMpf9fNHIUnlNC 1skgjZfcjl3IHeqQHas7SyI5m7l273a0DfaJ5anmMJJJdEIPysVufYi/i7iMmNe+CdNY dy3s/VMBvrOiBIP6nPzENZ+l+akBrRME0+Mr9Oye+TnIoAlvBIW3dXs/n6cy9KIkrwzZ gmX6Z7iLrR+Pvp55YwlO+qshX2DEqSswSO2BRqK6RROykrWQOx2BxokkRQp/9RgbsMNo 45xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=eQaG+O7f7WRUgxe5JhbJasKN0L5Ni341+cXP7hqHsuI=; b=Dp+/To4BnUPwVQWE3gEdE5xpuOVZZALEvbQ+CNs6XCCdFo65HG/hXRplqg4X8HS5YS 0bdtY1LCT4Uv9Pla4GJywgwsUCRB4KDhHBqXdbR+X30HZiy7R45JbVel8XgxUicLA0Oq mYEgbvb0016So95ZkDt0EnR/ewDPCrJ335ZZB5+KAcItCj+2/BqQoUBQa4GcGulMRWJa vKv+mNuy8bMziaUnwijwpPMQRpgXLy8a1kLI/g6SIX7IqNdTBtzdaLrm0/vqiipFCOFN nt7e7NVFngTyyUrdnKJa5knzOH2dbSG99YVGChJ9rkdJYMqmAIb2rBqeki/tlP3PhDae Ek6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=hs+oWyex; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k16si5664eja.59.2020.11.03.13.42.13; Tue, 03 Nov 2020 13:42:37 -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=default header.b=hs+oWyex; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732176AbgKCVi1 (ORCPT + 99 others); Tue, 3 Nov 2020 16:38:27 -0500 Received: from mail.kernel.org ([198.145.29.99]:58432 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732736AbgKCU4f (ORCPT ); Tue, 3 Nov 2020 15:56:35 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7445B20732; Tue, 3 Nov 2020 20:56:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604436993; bh=cq4N67AXND+ehHmZ0wnQjczu3m/x2c89Jl5bmTbMYOc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hs+oWyexmWweRzbwgCSCKMTAMa9JqoTCrV70WuZxWkFp8WzNARIvPHsTX/P0yRXZY 9yW3WZ9mS46Fwx7WoovX3o/SDk2ZwMs4+Ahfrz4QB1C0/BO8hF1YwD5sx/40jUtiZx pk7lP7Nyh++o1VzKaVRguH5/+c/J7ZYvEYPz6Fbk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andy Lutomirski , Ingo Molnar Subject: [PATCH 5.4 100/214] selftests/x86/fsgsbase: Test PTRACE_PEEKUSER for GSBASE with invalid LDT GS Date: Tue, 3 Nov 2020 21:35:48 +0100 Message-Id: <20201103203259.903783924@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201103203249.448706377@linuxfoundation.org> References: <20201103203249.448706377@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andy Lutomirski commit 1b9abd1755ad947d7c9913e92e7837b533124c90 upstream. This tests commit: 8ab49526b53d ("x86/fsgsbase/64: Fix NULL deref in 86_fsgsbase_read_task") Unpatched kernels will OOPS. Signed-off-by: Andy Lutomirski Signed-off-by: Ingo Molnar Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/c618ae86d1f757e01b1a8e79869f553cb88acf9a.1598461151.git.luto@kernel.org Signed-off-by: Greg Kroah-Hartman --- tools/testing/selftests/x86/fsgsbase.c | 65 +++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) --- a/tools/testing/selftests/x86/fsgsbase.c +++ b/tools/testing/selftests/x86/fsgsbase.c @@ -442,6 +442,68 @@ static void test_unexpected_base(void) #define USER_REGS_OFFSET(r) offsetof(struct user_regs_struct, r) +static void test_ptrace_write_gs_read_base(void) +{ + int status; + pid_t child = fork(); + + if (child < 0) + err(1, "fork"); + + if (child == 0) { + printf("[RUN]\tPTRACE_POKE GS, read GSBASE back\n"); + + printf("[RUN]\tARCH_SET_GS to 1\n"); + if (syscall(SYS_arch_prctl, ARCH_SET_GS, 1) != 0) + err(1, "ARCH_SET_GS"); + + if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0) + err(1, "PTRACE_TRACEME"); + + raise(SIGTRAP); + _exit(0); + } + + wait(&status); + + if (WSTOPSIG(status) == SIGTRAP) { + unsigned long base; + unsigned long gs_offset = USER_REGS_OFFSET(gs); + unsigned long base_offset = USER_REGS_OFFSET(gs_base); + + /* Read the initial base. It should be 1. */ + base = ptrace(PTRACE_PEEKUSER, child, base_offset, NULL); + if (base == 1) { + printf("[OK]\tGSBASE started at 1\n"); + } else { + nerrs++; + printf("[FAIL]\tGSBASE started at 0x%lx\n", base); + } + + printf("[RUN]\tSet GS = 0x7, read GSBASE\n"); + + /* Poke an LDT selector into GS. */ + if (ptrace(PTRACE_POKEUSER, child, gs_offset, 0x7) != 0) + err(1, "PTRACE_POKEUSER"); + + /* And read the base. */ + base = ptrace(PTRACE_PEEKUSER, child, base_offset, NULL); + + if (base == 0 || base == 1) { + printf("[OK]\tGSBASE reads as 0x%lx with invalid GS\n", base); + } else { + nerrs++; + printf("[FAIL]\tGSBASE=0x%lx (should be 0 or 1)\n", base); + } + } + + ptrace(PTRACE_CONT, child, NULL, NULL); + + wait(&status); + if (!WIFEXITED(status)) + printf("[WARN]\tChild didn't exit cleanly.\n"); +} + static void test_ptrace_write_gsbase(void) { int status; @@ -511,6 +573,9 @@ int main() shared_scratch = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0); + /* Do these tests before we have an LDT. */ + test_ptrace_write_gs_read_base(); + /* Probe FSGSBASE */ sethandler(SIGILL, sigill, 0); if (sigsetjmp(jmpbuf, 1) == 0) {