From patchwork Wed Jan 10 16:48:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 83767 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 26C1C38582B3 for ; Wed, 10 Jan 2024 16:49:17 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x22a.google.com (mail-oi1-x22a.google.com [IPv6:2607:f8b0:4864:20::22a]) by sourceware.org (Postfix) with ESMTPS id E53D63858D38 for ; Wed, 10 Jan 2024 16:48:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E53D63858D38 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 E53D63858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::22a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704905332; cv=none; b=dLKv+nBlPhe2H7AyMPZuLtO/jU5aeFXHM7wGbwqv30XqpcRyoHPgG/FScIC9VskXXbm4sdN2qNrfiSA59DELENk+DonIaF8t1glQXoGL9jPUYk+u9wWmkWZXX7uE0JRwcb0RbVqqNHlJhI444Sg+A8cwJV7aDt7CEqZoNF7G/sM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704905332; c=relaxed/simple; bh=umXB0muWz0zsrQNGend3uHj+L0mi2Rmj4QShv/IQwjU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Kv0sVv9igZrBpTvPpBBDOVTM44MAXIhjhZj6Ch/KS9Af7HrfIc3V6xpV62CU3hC674GQXrUp2qc/Ye5AEXVIguZOkba0nVvbaVhX2yrfp850epDQbxHdcTU8AbefcK21M+OJEd4ljFKhnsq0tI1reIJAJ+QAIEEMtmoPofO7Qgs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oi1-x22a.google.com with SMTP id 5614622812f47-3bb53e20a43so3514125b6e.1 for ; Wed, 10 Jan 2024 08:48:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704905329; x=1705510129; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=qWNWN3R6cIr1PDMU45hQ16JHVExO1rnyIi8h20v04AY=; b=BgpGIZ2kN9M38Qk4RgmLJB162ZtMaGcNNuDye8Ccm09xUzonXEoSuc1lOoMeGaZOsR bzUxTZSHFAKQlrG2ZzFLoO7RlhnlblBaUORAjZ9P0TTONdjPENRqK0IdhHpwazf5FYuO LpG60J0rFxuUE86B8Am79wLbxjMtr9kn1aa9AIxvcVH5mVs2/Q5X1bs3Nf+kh/WAELBs rwRFdjPh/9LCCyJKKAR05XJ6yvnNb+oa8qTcv5X4hBrIIym5wSgxC8ulwfbp7wuEQQEw fpjIGIIhDNIDcodtzvrhf7feQMNY8OQ1d0MW4WLhyQ8x47kEQfO/JILztCsfIAEwfRYO uhfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704905329; x=1705510129; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qWNWN3R6cIr1PDMU45hQ16JHVExO1rnyIi8h20v04AY=; b=XKQECPj7HE2LfYZOIZhsbXnwJqr0/z3ZufGsevgrzjDmzEdNjAOZVxhoHEABoMAS6K 8gkkel+48Eea64HULttcxb9hViLYUJSRQxxtzZqtJfOKGYJU8pp128QP9PaH/M+owu5r /clplFruhKvoTDzU72wzgR9yb2YAvabSlBCBcggAZKDJT092HAaYWUu8wu7slZCDoaAx 9EhXB8DriK7YaszVKGoN4qU0+n+lNt3ebrfDFscNps4qKqP1fAi5MRQ1Z+9w08kmYDml W2zhgEIZRxqa8WeO3tdxt6N0vXTtJZBBgYqyOHv/TNrxMmBsRYtwCPgeXM8Jq7g7tGJL BQrA== X-Gm-Message-State: AOJu0Yyja4rmv7kJtfrgNCrhSxveN29Eetnvw1Oqnr1d6H3cX5NPyTC8 xnZEte2dmLNu/Tz8NU+/OmBj0kKAK3Y= X-Google-Smtp-Source: AGHT+IH6clP/m3QIluUIY+sSk9LgCDotC/GQXLddkyD2//FJxuwTQlYAJAg03mLPeG6kQ9/k+uoZVw== X-Received: by 2002:a05:6358:1c:b0:172:dd11:bfa8 with SMTP id 28-20020a056358001c00b00172dd11bfa8mr1000851rww.30.1704905328941; Wed, 10 Jan 2024 08:48:48 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.56.169.119]) by smtp.gmail.com with ESMTPSA id e9-20020aa798c9000000b006d9a13b491csm3709416pfm.212.2024.01.10.08.48.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 08:48:48 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 733EF740178; Wed, 10 Jan 2024 08:48:47 -0800 (PST) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: josmyers@redhat.com Subject: [PATCH] x86-64/cet: Make CET feature check specific to Linux/x86 Date: Wed, 10 Jan 2024 08:48:47 -0800 Message-ID: <20240110164847.3304707-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-3024.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_NUMSUBJECT, 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 CET feature bits in TCB, which are Linux specific, are used to check if CET features are active. Move CET feature check to Linux/x86 directory. Reviewed-by: Noah Goldstein --- .../sysv/linux/x86/bits/platform/features.h | 48 +++++++++++++++++++ sysdeps/unix/sysv/linux/x86_64/dl-cet.h | 8 ++++ .../linux/x86_64/get-cpuid-feature-leaf.c | 24 ++++++++++ sysdeps/x86/Makefile | 6 ++- sysdeps/x86/bits/platform/features.h | 27 +++++++++++ sysdeps/x86/bits/platform/x86.h | 8 ---- sysdeps/x86/get-cpuid-feature-leaf.c | 8 ---- sysdeps/x86/sys/platform/x86.h | 19 ++------ sysdeps/x86_64/dl-machine.h | 9 ++-- 9 files changed, 120 insertions(+), 37 deletions(-) create mode 100644 sysdeps/unix/sysv/linux/x86/bits/platform/features.h create mode 100644 sysdeps/unix/sysv/linux/x86_64/get-cpuid-feature-leaf.c create mode 100644 sysdeps/x86/bits/platform/features.h diff --git a/sysdeps/unix/sysv/linux/x86/bits/platform/features.h b/sysdeps/unix/sysv/linux/x86/bits/platform/features.h new file mode 100644 index 0000000000..7704febe92 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86/bits/platform/features.h @@ -0,0 +1,48 @@ +/* Inline functions for x86 CPU features. + This file is part of the GNU C Library. + Copyright (C) 2024 Free Software Foundation, Inc. + + 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 + . */ + +#ifndef _SYS_PLATFORM_X86_H +# error "Never include directly; use instead." +#endif + +/* Bits in the feature_1 field in TCB. */ + +enum +{ + x86_feature_1_ibt = 1U << 0, + x86_feature_1_shstk = 1U << 1 +}; + +static __inline__ _Bool +x86_cpu_cet_active (unsigned int __index) +{ +#ifdef __x86_64__ + unsigned int __feature_1; +# ifdef __LP64__ + __asm__ ("mov %%fs:72, %0" : "=r" (__feature_1)); +# else + __asm__ ("mov %%fs:40, %0" : "=r" (__feature_1)); +# endif + if (__index == x86_cpu_IBT) + return __feature_1 & x86_feature_1_ibt; + else + return __feature_1 & x86_feature_1_shstk; +#else + return false; +#endif +} diff --git a/sysdeps/unix/sysv/linux/x86_64/dl-cet.h b/sysdeps/unix/sysv/linux/x86_64/dl-cet.h index a00c7ee662..1fe3133406 100644 --- a/sysdeps/unix/sysv/linux/x86_64/dl-cet.h +++ b/sysdeps/unix/sysv/linux/x86_64/dl-cet.h @@ -54,6 +54,14 @@ dl_cet_get_cet_status (void) return status; } +static __always_inline bool +dl_cet_ibt_enabled (void) +{ + unsigned int feature_1 = THREAD_GETMEM (THREAD_SELF, + header.feature_1); + return (feature_1 & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0; +} + /* Enable shadow stack with a macro to avoid shadow stack underflow. */ #define ENABLE_X86_CET(cet_feature) \ if ((cet_feature & GNU_PROPERTY_X86_FEATURE_1_SHSTK)) \ diff --git a/sysdeps/unix/sysv/linux/x86_64/get-cpuid-feature-leaf.c b/sysdeps/unix/sysv/linux/x86_64/get-cpuid-feature-leaf.c new file mode 100644 index 0000000000..39b342463c --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86_64/get-cpuid-feature-leaf.c @@ -0,0 +1,24 @@ +/* Get CPUID feature leaf. Linux/x86-64 version. + Copyright (C) 2024 Free Software Foundation, Inc. + + 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 + +#ifdef __LP64__ +_Static_assert (FEATURE_1_OFFSET == 72, "FEATURE_1_OFFSET != 72"); +#else +_Static_assert (FEATURE_1_OFFSET == 40, "FEATURE_1_OFFSET != 40"); +#endif diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile index bf913594c1..4d50b327b5 100644 --- a/sysdeps/x86/Makefile +++ b/sysdeps/x86/Makefile @@ -5,7 +5,11 @@ endif ifeq ($(subdir),elf) sysdep_routines += get-cpuid-feature-leaf sysdep-dl-routines += dl-get-cpu-features -sysdep_headers += sys/platform/x86.h bits/platform/x86.h +sysdep_headers += \ + bits/platform/features.h \ + bits/platform/x86.h \ + sys/platform/x86.h \ +# sysdep_headers CFLAGS-dl-get-cpu-features.os += $(rtld-early-cflags) CFLAGS-get-cpuid-feature-leaf.o += $(no-stack-protector) diff --git a/sysdeps/x86/bits/platform/features.h b/sysdeps/x86/bits/platform/features.h new file mode 100644 index 0000000000..f02489266e --- /dev/null +++ b/sysdeps/x86/bits/platform/features.h @@ -0,0 +1,27 @@ +/* Inline functions for x86 CPU features. + This file is part of the GNU C Library. + Copyright (C) 2024 Free Software Foundation, Inc. + + 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 + . */ + +#ifndef _SYS_PLATFORM_X86_H +# error "Never include directly; use instead." +#endif + +static __inline__ _Bool +x86_cpu_cet_active (unsigned int __index) +{ + return false; +} diff --git a/sysdeps/x86/bits/platform/x86.h b/sysdeps/x86/bits/platform/x86.h index c31d086194..8b157d99b3 100644 --- a/sysdeps/x86/bits/platform/x86.h +++ b/sysdeps/x86/bits/platform/x86.h @@ -337,11 +337,3 @@ enum x86_cpu_AVX10_YMM = x86_cpu_index_24_ecx_0_ebx + 17, x86_cpu_AVX10_ZMM = x86_cpu_index_24_ecx_0_ebx + 18, }; - -/* Bits in the feature_1 field in TCB. */ - -enum -{ - x86_feature_1_ibt = 1U << 0, - x86_feature_1_shstk = 1U << 1 -}; diff --git a/sysdeps/x86/get-cpuid-feature-leaf.c b/sysdeps/x86/get-cpuid-feature-leaf.c index e6a41cc79c..6f67346081 100644 --- a/sysdeps/x86/get-cpuid-feature-leaf.c +++ b/sysdeps/x86/get-cpuid-feature-leaf.c @@ -19,14 +19,6 @@ #include #include -#ifdef __x86_64__ -# ifdef __LP64__ -_Static_assert (FEATURE_1_OFFSET == 72, "FEATURE_1_OFFSET != 72"); -# else -_Static_assert (FEATURE_1_OFFSET == 40, "FEATURE_1_OFFSET != 40"); -# endif -#endif - const struct cpuid_feature * __x86_get_cpuid_feature_leaf (unsigned int leaf) { diff --git a/sysdeps/x86/sys/platform/x86.h b/sysdeps/x86/sys/platform/x86.h index 4d77c0991d..b8066ccc9f 100644 --- a/sysdeps/x86/sys/platform/x86.h +++ b/sysdeps/x86/sys/platform/x86.h @@ -22,6 +22,7 @@ #include #include #include +#include __BEGIN_DECLS @@ -46,22 +47,8 @@ static __inline__ _Bool x86_cpu_active (unsigned int __index) { if (__index == x86_cpu_IBT || __index == x86_cpu_SHSTK) - { -#ifdef __x86_64__ - unsigned int __feature_1; -# ifdef __LP64__ - __asm__ ("mov %%fs:72, %0" : "=r" (__feature_1)); -# else - __asm__ ("mov %%fs:40, %0" : "=r" (__feature_1)); -# endif - if (__index == x86_cpu_IBT) - return __feature_1 & x86_feature_1_ibt; - else - return __feature_1 & x86_feature_1_shstk; -#else - return false; -#endif - } + return x86_cpu_cet_active (__index); + const struct cpuid_feature *__ptr = __x86_get_cpuid_feature_leaf (__index / (8 * sizeof (unsigned int) * 4)); unsigned int __reg diff --git a/sysdeps/x86_64/dl-machine.h b/sysdeps/x86_64/dl-machine.h index 2d2ce503a5..6d605d0d32 100644 --- a/sysdeps/x86_64/dl-machine.h +++ b/sysdeps/x86_64/dl-machine.h @@ -587,10 +587,11 @@ x86_64_rewrite_plt (struct link_map *map, ElfW(Addr) plt_rewrite) const ElfW(Rela) *reloc = (const void *) start; const ElfW(Rela) *reloc_end = (const void *) (start + size); - unsigned int feature_1 = THREAD_GETMEM (THREAD_SELF, - header.feature_1); - bool ibt_enabled_p - = (feature_1 & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0; +# ifdef __CET__ + bool ibt_enabled_p = dl_cet_ibt_enabled (); +# else + bool ibt_enabled_p = false; +# endif if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)) _dl_debug_printf ("\nchanging PLT in '%s' to direct branch\n",