From patchwork Wed Dec 6 17:19:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 81536 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AE925385AE5E for ; Wed, 6 Dec 2023 17:21:05 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id 8CA5A3858C35 for ; Wed, 6 Dec 2023 17:20:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8CA5A3858C35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8CA5A3858C35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::429 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883216; cv=none; b=Dl1yJ66kAgMedyrJF4CXAmqe/zfCoekwOSxXvdMlptkJy+85X1XCRrWctdGuP19eNL2Hy7tt02diRoqycriXUOPzfz762MTfvG2AcrD0+H0e4cgN3eYhLhwGrjhW+2VhLzY2kqVeeNldx7c028glNXQ7Km30JqHYXGwql6a8fg4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883216; c=relaxed/simple; bh=jd0sxgsxyNF0H8fSJqlHVH+XCMDLRXBOmtJbjeU7zNQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=VArsRAPD5CoEfipKX4yt/PvKvfWgqdmF3U/xMgv+KjVpotDL8WLHEIoPYSIeJQPMi78XUP7DrsOQqiUDW9BQr+xZgmeZzwpz7Qx0EAWR6ikX+mKV4qVFGlj0GPTDzCorUwaHgEdjoelyggwjRgijW5+vU47d/lilAro05ihdp00= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-6ce26a03d9eso71259b3a.0 for ; Wed, 06 Dec 2023 09:20:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883213; x=1702488013; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=AYFCl5ZK3Y14DV2DRLY3TXM1FXTruCQqP3PS25gMOmU=; b=kpCslpPrp+u1ZV8BI+3vLKjc6H9t0+aTUng+dP4/isthFtz62zi4oupJEp+2f17ots hrb63D5BaZ6sEf6IKf1fSdLQNiOhTIh4P3LblTTD0YFVy2qwEiDFaajJJEp3xTHDG5iU CkLwZu8nFtExZ+W75O2d56+Gvv1GGCcpJ6YwUYjIYtT2aZMKE4UcEt6qMef0Cqh9K+fB 9Z3cFS5VQSvWSkszlda3hvg+PfXP5J3KtWaSOO40b4keK28qPk3tojnFgxCYn8R5zKaM nvL2ti5+KBxi9FTguaYfZHV532b/HqdeEdvwceAf+HHMMhg3eR6V1/OJlHbgpwx0N7TN kp6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883213; x=1702488013; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AYFCl5ZK3Y14DV2DRLY3TXM1FXTruCQqP3PS25gMOmU=; b=HBVvQ5P8kp/87tWbZofOrMChUqU4RfZs6d/aATglfkLWrVh7/P1A6MYIfJWWIm0OMm Q7Fq6bwnpQ2FhQF+qpQKnZvIIHsj0zdFueD9c1l/GKIToxDc9vU3ZdVHarvPAVqua1tA YcAphBIDz5jqw6bXgP5xz+wiGEoxv9eKQWtP9+LoMOZHB6iEXKhJFoS8x1yU2eOeyztq KiL9+icnZ1W9Vllh+jilcAGOZ7We0rXSqeTAxVxvJk1Zr02oGliRnwFlN4zbofvsozBp Lodh12dlQCTiIK4qsS3vjU/NRqBa+lMU7LvHpp/lntPvlLfD/ROzgloM1vX1RtO4bSt4 wxTw== X-Gm-Message-State: AOJu0YyBMh9cMdicIPUJ/BK/CEXFmpHIdJX15D9gXP2zCFHWuwlYDUff spsZgcdDVp3l03zLrH4e+YefvppxcjM= X-Google-Smtp-Source: AGHT+IG8AGMlyOZRQuWGWTgsiJYMqDDUF1Esg7Tnkc383GE2Le0EV2iRO0crR2M/A834oUzuLxDtqA== X-Received: by 2002:a05:6a20:8e0f:b0:186:ae16:103 with SMTP id y15-20020a056a208e0f00b00186ae160103mr1198878pzj.30.1701883213398; Wed, 06 Dec 2023 09:20:13 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id e12-20020a62aa0c000000b006ce4f01f769sm239644pff.45.2023.12.06.09.20.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:11 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 7484174062D; Wed, 6 Dec 2023 09:20:10 -0800 (PST) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH 04/17] x86/cet: Check legacy shadow stack applications Date: Wed, 6 Dec 2023 09:19:57 -0800 Message-ID: <20231206172010.1023415-5-hjl.tools@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231206172010.1023415-1-hjl.tools@gmail.com> References: <20231206172010.1023415-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3024.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Add tests to verify that legacy shadow stack applications run properly when shadow stack is enabled in Linux kernel. --- sysdeps/x86/Makefile | 23 ++++++++++++++ sysdeps/x86/tst-shstk-legacy-1-extra.S | 35 ++++++++++++++++++++++ sysdeps/x86/tst-shstk-legacy-1a-static.c | 1 + sysdeps/x86/tst-shstk-legacy-1a.c | 32 ++++++++++++++++++++ sysdeps/x86/tst-shstk-legacy-1b-static.c | 1 + sysdeps/x86/tst-shstk-legacy-1b.c | 38 ++++++++++++++++++++++++ 6 files changed, 130 insertions(+) create mode 100644 sysdeps/x86/tst-shstk-legacy-1-extra.S create mode 100644 sysdeps/x86/tst-shstk-legacy-1a-static.c create mode 100644 sysdeps/x86/tst-shstk-legacy-1a.c create mode 100644 sysdeps/x86/tst-shstk-legacy-1b-static.c create mode 100644 sysdeps/x86/tst-shstk-legacy-1b.c diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile index ea45aad34c..dea14e343c 100644 --- a/sysdeps/x86/Makefile +++ b/sysdeps/x86/Makefile @@ -129,6 +129,21 @@ tests += tst-cet-legacy-1 tst-cet-legacy-1a tst-cet-legacy-2 \ tst-cet-legacy-10 tst-cet-legacy-10-static tests-static += tst-cet-legacy-9-static tst-cet-legacy-10-static tst-cet-legacy-1a-ARGS = -- $(host-test-program-cmd) + +tests += \ + tst-shstk-legacy-1a \ + tst-shstk-legacy-1a-static \ + tst-shstk-legacy-1b \ + tst-shstk-legacy-1b-static \ +# tests +tests-static += \ + tst-shstk-legacy-1a-static \ + tst-shstk-legacy-1b-static \ +# tests-static +extra-objs += \ + tst-shstk-legacy-1-extra.o \ +# extra-objs + tests += tst-cet-legacy-4a tst-cet-legacy-4b tst-cet-legacy-4c \ tst-cet-legacy-5b tst-cet-legacy-6b modules-names += tst-cet-legacy-mod-1 tst-cet-legacy-mod-2 \ @@ -168,6 +183,9 @@ CFLAGS-tst-cet-legacy-7.c += -fcf-protection=none CFLAGS-tst-cet-legacy-10.c += -mshstk CFLAGS-tst-cet-legacy-10-static.c += -mshstk +CFLAGS-tst-shstk-legacy-1a.c += -fcf-protection=none +CFLAGS-tst-shstk-legacy-1a-static.c += -fcf-protection=none + $(objpfx)tst-cet-legacy-1: $(objpfx)tst-cet-legacy-mod-1.so \ $(objpfx)tst-cet-legacy-mod-2.so $(objpfx)tst-cet-legacy-1a: $(objpfx)tst-cet-legacy-mod-1.so \ @@ -200,6 +218,11 @@ $(objpfx)tst-cet-legacy-6b.out: $(objpfx)tst-cet-legacy-mod-6a.so \ tst-cet-legacy-6b-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=-IBT,-SHSTK tst-cet-legacy-9-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=-IBT,-SHSTK tst-cet-legacy-9-static-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=-IBT,-SHSTK + +$(objpfx)tst-shstk-legacy-1a: $(objpfx)tst-shstk-legacy-1-extra.o +$(objpfx)tst-shstk-legacy-1a-static: $(objpfx)tst-shstk-legacy-1-extra.o +$(objpfx)tst-shstk-legacy-1b: $(objpfx)tst-shstk-legacy-1-extra.o +$(objpfx)tst-shstk-legacy-1b-static: $(objpfx)tst-shstk-legacy-1-extra.o endif # Add -fcf-protection to CFLAGS when CET is enabled. diff --git a/sysdeps/x86/tst-shstk-legacy-1-extra.S b/sysdeps/x86/tst-shstk-legacy-1-extra.S new file mode 100644 index 0000000000..f3adb9f639 --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1-extra.S @@ -0,0 +1,35 @@ +/* Legacy shadow stack code. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + + .text + .globl legacy + .type legacy, @function +legacy: + .cfi_startproc +#ifdef __x86_64__ + movq (%rsp), %rax + addq $8, %rsp + jmp *%rax +#else + movl (%esp), %eax + addl $4, %esp + jmp *%eax +#endif + .cfi_endproc + .size legacy, .-legacy + .section .note.GNU-stack,"",@progbits diff --git a/sysdeps/x86/tst-shstk-legacy-1a-static.c b/sysdeps/x86/tst-shstk-legacy-1a-static.c new file mode 100644 index 0000000000..dd549890a0 --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1a-static.c @@ -0,0 +1 @@ +#include "tst-shstk-legacy-1a.c" diff --git a/sysdeps/x86/tst-shstk-legacy-1a.c b/sysdeps/x86/tst-shstk-legacy-1a.c new file mode 100644 index 0000000000..c6f5810838 --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1a.c @@ -0,0 +1,32 @@ +/* Check that legacy shadow stack code won't trigger segfault. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +/* Check that legacy shadow stack code won't trigger segfault. */ +extern void legacy (void); + +static int +do_test (void) +{ + legacy (); + return EXIT_SUCCESS; +} + +#include diff --git a/sysdeps/x86/tst-shstk-legacy-1b-static.c b/sysdeps/x86/tst-shstk-legacy-1b-static.c new file mode 100644 index 0000000000..4945344675 --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1b-static.c @@ -0,0 +1 @@ +#include "tst-shstk-legacy-1b.c" diff --git a/sysdeps/x86/tst-shstk-legacy-1b.c b/sysdeps/x86/tst-shstk-legacy-1b.c new file mode 100644 index 0000000000..05231e60ae --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1b.c @@ -0,0 +1,38 @@ +/* Check that legacy shadow stack code will trigger segfault. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +/* Check that legacy shadow stack code will trigger segfault. */ +extern void legacy (void); + +static int +do_test (void) +{ + if (!CPU_FEATURE_ACTIVE (SHSTK)) + return EXIT_UNSUPPORTED; + + legacy (); + return EXIT_FAILURE; +} + +#define EXPECTED_SIGNAL (CPU_FEATURE_ACTIVE (SHSTK) ? SIGSEGV : 0) +#include