From patchwork Wed Dec 6 17:19:54 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: 1872895 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=HEZPEWb/; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SlkgY5lKpz23nD for ; Thu, 7 Dec 2023 04:20:25 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D20BE385800F for ; Wed, 6 Dec 2023 17:20:23 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by sourceware.org (Postfix) with ESMTPS id D51103858C60 for ; Wed, 6 Dec 2023 17:20:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D51103858C60 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 D51103858C60 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::629 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883216; cv=none; b=AmJm10N65+NfIVnMC0C/x0PETWZcjH3ZwZ1z4aIaTgNyN4MDLdSI2LC2fNBxcFjQsX6/j5x2mlI13YlWbymUdMZakMjnBtINR560mobUB1c89yDOfMipWw4RUt7I66D/2yNAMMugKnRX5Ho5KGLwFWcPtWzDKhIh70kWAFwD8RA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883216; c=relaxed/simple; bh=H4KfFPd84bQXsI8GrhD6soEt0+Vey8+mtaA3pb8NwX0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=AwnTCWr+MlsX21wD2nOhd99gbRsO1oI9ZZdrJ+boP6VlPvkhuuJctgNXgK5UsAF2Ig9/bGxzu5JniQkRoomj4QSjtH8rZRslZ5/LceiNOit8L5CZvEZJdP2AD2gSoLjYwInwhMrJqWrP1ffwzpaf63gYelJ0G4a1u1OSPV5LB24= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1d05e4a94c3so49503585ad.1 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=1701883214; x=1702488014; 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=qU8FFPMXeWsyvUwHHn3NNVq2DBg1l1VNH0MbIVzlRaY=; b=HEZPEWb/4lxRBlySFarW9bAkbHJCdTCKtVDppQAXY1N0UoIcFjN3cpHbusO6LhgoXe m2OfbzS+cQ/7wtgGHFctCu0R18h4uht+5wr/u7nyrc7zMEqQszXrxY3w/1IcjAg6huOH OpxNPPPJMKhpjpj2NsPFcR2/x77ClR4JFwUqbzNxJFZVONzBhjWzDct5477h+tNmv3bq R2s6HdqxqRR5MTzzfk9SFiMnOzBJhEi4V6wvvtPFXArvFx7S6IoCy7JwxKp1hou2FUKO DyJmJsdI9t/PAjPIyUeKBbjqBUBJT2fuBIou5MvAYuQKdqzNqMoP56zeHb9pfdRyusCe s2kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883214; x=1702488014; 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=qU8FFPMXeWsyvUwHHn3NNVq2DBg1l1VNH0MbIVzlRaY=; b=xKiZ1Ik/IIKwNb+NPFSZGVAeYM5D4RvyKjWMU9FuwoQnmVqKxoFJx1LTiaFAiZozw5 TT9CpfgoWqjUR+QIVicUA0fpOwwU4DtQMHMo2wwonX15XtRnuSmX0UKRdMw0G/z7x6Zs XC3aa1sNpnmxRTW+WvjhENztmWxO6T8n9ffZqoGkUpBrHxEo67R8oHhBGa9lFxDPp4kx m4bG0e1Htu5gjKWbuGHWI/bZ/EiM9doaZTOOzEP4NoCcS+I7UsozXKp8y1bdJxifNR78 oH74/q9UW52Csvv9HItQKY0xHSgHZxeaxESY98lVp3wuta0yIibCAywij1KjNo06/TaU RXCA== X-Gm-Message-State: AOJu0Yyy9JbygU9tH2kiA1ie32WR++Xf/I6xDjrBqjkLZoKREorHU4xc KmpmtMwkADDvJFdscQEyc2M5zMkAxcY= X-Google-Smtp-Source: AGHT+IHWx2DChGD812PDx3nzsLKAef7PaB8pO4+DzL0L8T0dQsGSjjhzxo1+x+ESK3OjgiqyGUYPSw== X-Received: by 2002:a17:903:11c9:b0:1d0:b503:1ea5 with SMTP id q9-20020a17090311c900b001d0b5031ea5mr1482251plh.20.1701883213799; 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 j18-20020a170902c3d200b001cfb6bef8fesm67644plj.186.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 53C077404C5; 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 01/17] x86/cet: Check user_shstk in /proc/cpuinfo Date: Wed, 6 Dec 2023 09:19:54 -0800 Message-ID: <20231206172010.1023415-2-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=-3025.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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+incoming=patchwork.ozlabs.org@sourceware.org Linux kernel reports CPU shadow stack feature in /proc/cpuinfo as user_shstk, instead of shstk. --- sysdeps/x86/tst-cpu-features-cpuinfo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sysdeps/x86/tst-cpu-features-cpuinfo.c b/sysdeps/x86/tst-cpu-features-cpuinfo.c index 18d64375ca..1d6c647b70 100644 --- a/sysdeps/x86/tst-cpu-features-cpuinfo.c +++ b/sysdeps/x86/tst-cpu-features-cpuinfo.c @@ -246,7 +246,7 @@ do_test (int argc, char **argv) fails += CHECK_PROC (sgx, SGX); fails += CHECK_PROC (sgx_lc, SGX_LC); fails += CHECK_PROC (sha_ni, SHA); - fails += CHECK_PROC (shstk, SHSTK); + fails += CHECK_PROC (user_shstk, SHSTK); fails += CHECK_PROC (smap, SMAP); fails += CHECK_PROC (smep, SMEP); fails += CHECK_PROC (smx, SMX); From patchwork Wed Dec 6 17:19:55 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: 1872907 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=H+RvKufx; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SlkjM2dv8z23nD for ; Thu, 7 Dec 2023 04:21:59 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ABB7E3861840 for ; Wed, 6 Dec 2023 17:21:45 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id 12BC73858D20 for ; Wed, 6 Dec 2023 17:20:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 12BC73858D20 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 12BC73858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883220; cv=none; b=mxBP43uL1Y2UdWJHuTHAgplhSDoq6gw3nBUnIaFgNyYwqpKntxEsFmw3DSzu/dMIP0C+Nj09+RrYW+cm/TdS7uOjeAob971EFvvv1xRgfvErHHdfYeTfLtIjBe5SjMwUO9Xu7qNZiOivByNksN4eLUjQ2uedqAaqW8Oq6zRq0Ss= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883220; c=relaxed/simple; bh=jxW/SkrVn18D8mOS3FpGySqq7q/7HGgXbFvR43enobU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Z9xVkhi747P0uzW29DNgITygWsBPjsd5tZ8AJgaPfW7SvGx4JAZXbB0c7X7Mst4r1wiQprz3lUu0SXo5TVy2cQK8YIMyk1RjkK3UP46zOrNdZ+s31qbd7DNwRqeYw09aZPi3OzJKniENd/JlTQC0YiEGIK23+EWEblgQlo2jANs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1d075392ff6so341605ad.1 for ; Wed, 06 Dec 2023 09:20:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883214; x=1702488014; 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=qMvtHogobq6JlLL5ALFhdAK3OmuMdJlleYsUArs3zhg=; b=H+RvKufxryNuoAD0ZCi+LhydyrSHBeI5qnrOY/B4W1rhZAMveOnGY4Ff4h24lgOaz4 JUEmbsaQaq9zckHHi52IEghHSHg9CeoQAFFCK8HH+v95XG8Phw0hZJHjci3aw0aKGRbW ARTEGFyrsiMdk/4CiazBwTIice2AJDjK1AupGKhtABWTlE2DVbswYWd2ldubefye/X90 6nm8b7oMCnHc3D1QjN0GzMRgVc2gjrCXFJmcsNcsEtOZ77Kav2u1vEwL8twH4rWJozsP EWn/JntxNXEiA6hB720U5YF4w8yahFFg0oBfyZao5W65xo1B39buyzjCU//Uad+r6CQz Kf9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883214; x=1702488014; 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=qMvtHogobq6JlLL5ALFhdAK3OmuMdJlleYsUArs3zhg=; b=t81mywD5noET6+ELNV3/+29AIiWoa7/kMJtBFgL/HO4Ogsg8tDb4m+jGNnhMOcvwf4 KOzNR2NUwxzcIQFoCpoeEVtmpRrBDJRCNdasTrk+RjHJbkn1+3cyzHFbgtBa30W3k4Ci 1PBBRO0bwsjWyiUUgIxadfdgcfagcOZR8wlqpqRYZNqQey2KCbySCU1kKrraSkhIkZeF Zse1hnfMhNsmBlWnbV9qCO2F6h9jOrlr7eGjNtVMHzKC/37YG65jQhzghYBkZbvPwido JxQTS+lNfrKQHB75NMINd80XsSNPvRrSbYFz8hb1Uw/HAmsnczmHpoNcLeTKawzMFjRa q2Dw== X-Gm-Message-State: AOJu0YwDLi1m3bsFnTG3HZerNNqV7Jpf5Bfx82osw/ku7nRVJt16sxpL 5hsoj5O1eLW40diwmva16GFqlEXidKY= X-Google-Smtp-Source: AGHT+IFgNb10eI+PxW97YsnOnM85/2wnibfZF0QeIsYgRRHu57xcda5maeav2AeWnd8TyfKXxMEVwg== X-Received: by 2002:a17:902:bb8c:b0:1d0:700b:3f8a with SMTP id m12-20020a170902bb8c00b001d0700b3f8amr3536552pls.68.1701883214001; Wed, 06 Dec 2023 09:20:14 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id 6-20020a170902c24600b001d0695106c4sm82280plg.105.2023.12.06.09.20.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:12 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 5ED5874062B; 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 02/17] x86/cet: Update tst-cet-vfork-1 Date: Wed, 6 Dec 2023 09:19:55 -0800 Message-ID: <20231206172010.1023415-3-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.7 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+incoming=patchwork.ozlabs.org@sourceware.org Change tst-cet-vfork-1.c to verify that vfork child return triggers SIGSEGV due to shadow stack mismatch. --- sysdeps/unix/sysv/linux/x86/tst-cet-vfork-1.c | 43 ++++++++----------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/sysdeps/unix/sysv/linux/x86/tst-cet-vfork-1.c b/sysdeps/unix/sysv/linux/x86/tst-cet-vfork-1.c index 9c4b6f4d42..c92ed9e737 100644 --- a/sysdeps/unix/sysv/linux/x86/tst-cet-vfork-1.c +++ b/sysdeps/unix/sysv/linux/x86/tst-cet-vfork-1.c @@ -18,34 +18,26 @@ . */ #include -#include #include #include #include #include #include #include -#include -#include __attribute__ ((noclone, noinline)) static void do_test_1 (void) { pid_t p1; - int fd[2]; - if (pipe (fd) == -1) - { - puts ("pipe failed"); - _exit (EXIT_FAILURE); - } + /* NB: Since child return pops shadow stack which is shared with + parent, child must not return after vfork. */ if ((p1 = vfork ()) == 0) { - pid_t p = getpid (); - TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))); - /* Child return should trigger SIGSEGV. */ + /* Child return should trigger SIGSEGV due to shadow stack + mismatch. */ return; } else if (p1 == -1) @@ -54,22 +46,22 @@ do_test_1 (void) _exit (EXIT_FAILURE); } - pid_t p2 = 0; - if (TEMP_FAILURE_RETRY (read (fd[0], &p2, sizeof (pid_t))) - != sizeof (pid_t)) - puts ("pipd read failed"); - else + int r; + if (TEMP_FAILURE_RETRY (waitpid (p1, &r, 0)) != p1) { - int r; - if (TEMP_FAILURE_RETRY (waitpid (p1, &r, 0)) != p1) - puts ("waitpid failed"); - else if (r != 0) - puts ("pip write in child failed"); + puts ("waitpid failed"); + _exit (EXIT_FAILURE); + } + + if (!WIFSIGNALED (r) || WTERMSIG (r) != SIGSEGV) + { + puts ("Child not terminated with SIGSEGV"); + _exit (EXIT_FAILURE); } /* Parent exits immediately so that parent returns without triggering - SIGSEGV when shadow stack isn't in use. */ - _exit (EXIT_FAILURE); + SIGSEGV when shadow stack is in use. */ + _exit (EXIT_SUCCESS); } static int @@ -80,9 +72,8 @@ do_test (void) return EXIT_UNSUPPORTED; do_test_1 (); /* Child exits immediately so that child returns without triggering - SIGSEGV when shadow stack isn't in use. */ + SIGSEGV when shadow stack is in use. */ _exit (EXIT_FAILURE); } -#define EXPECTED_SIGNAL (_get_ssp () == 0 ? 0 : SIGSEGV) #include From patchwork Wed Dec 6 17:19:56 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: 1872904 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=elEkEtex; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Slkhv65GZz23nD for ; Thu, 7 Dec 2023 04:21:35 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 39AC2386100C for ; Wed, 6 Dec 2023 17:21:27 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by sourceware.org (Postfix) with ESMTPS id 66F45385840C for ; Wed, 6 Dec 2023 17:20:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 66F45385840C 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 66F45385840C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883217; cv=none; b=eMDuEgRcaIxFRf4kL1Wn1i5Hxvt0b+M5Dlci/WmK79jIsInuse7re9hYXdwluffPvtZehgSiFIaKarDsKCgHfzBjiTPKDBSMVXSepw/1ZDFBvvr5kVsnFAwtK/FGMCbyr/X7ULvtp7wcDjy+odS1kZbsceq9ZnbCBIoXOmeIqv8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883217; c=relaxed/simple; bh=deFYwnx7KfgvCTJ+z6zcdr1XwG/yWR4L/YEp8Qg84A8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=LlIBpmLK7U2OtWRlJrx3ak4l+JYq2vOOeStaD/GHA2/Vs/TgESPE5dfi05iMHD/T2e44r1iYllSpIg2pzr7Jga80fyV9yyfSRgXqV4gy2rC51eIuNoMpeU2t/2M+1bdYT+swkaFuXktVN6fijH1yPwcQgCbx3rp22CzvYRkgrzQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-6ce972ac39dso47717b3a.3 for ; Wed, 06 Dec 2023 09:20:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883212; x=1702488012; 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=P3qExcyLMx22FpOGhd0HGApIlxD0z1QRLqYTfzf8yyI=; b=elEkEtexcTqWG4VL3BXiI9wqq1YPG95jjgu+0Z0xXYQzAbljcqfTC0r6ASCpkCP5Sj yjlj7XdOxUihmdNYKngtTcsyMjQLE1/39iOzYwsOPF8dCFY+no9N55msmbbRGLHQXFer CZGSq67k1ZzfF5bCBjNaPP3/vV/cfYtbwPzy5Xfocd1be+xCkh2UlnglPX8oss1j9Wvg Qt+0vLOwSv+9X8MQ/zN36WtSISpskhjtrxFjLPxVRuwyO+y8v7dAEWr+++QDRDuMMayx ySGdTca6+wMRLRWnQrZY42Ib8GdjIf0Yhx0AtkbxlU5jnRiAtboBeMPXX1dnqqsQTEfM u2vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883212; x=1702488012; 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=P3qExcyLMx22FpOGhd0HGApIlxD0z1QRLqYTfzf8yyI=; b=Xd+TL9yyx6FypVPTwQv6w6iVsBUv21bzxwa3r5r+Md0qu33LZuvhJOQDsg04OV17Tj LbCvPSwBrYQ6UcFuoB32iKqe5pxcG3dYbHtJqYeEHAXBMJHDDwtXlV0HOjsa162lJkOg 3/ryVGUPgZeOJaUXRuf1jusdyCYmwsMJ06TX5SlF7ulCVpNkRbS7PupbVBY/jNfjzHYv A+naFlSeDuJf5Da2aS7vFvVz+H9FhZ6d3ROd93oLwDN9vICiagBEffccfFReBjlYCtKk W6ZnFsMl71IRUwzgW52KFEs5PkEfL7nqTeqkiJOPZYlZPCgbFP34bAvceycKlUJIOY/A Ar2g== X-Gm-Message-State: AOJu0YxpD3mFMlAdgOnR+V7/YvKrbN91NT/CVvYj6kaokJIBR84P0QI+ iHNEIeDJAbP5gVm1/XUn8aPPM/GCXsg= X-Google-Smtp-Source: AGHT+IGEg/2d/x+BTsSwly/O5O7X0J4wzmSheNUV1C6s2AIWTLaTFQouwR9oFdHcv1uPeUbzNS9fKQ== X-Received: by 2002:a05:6a20:3d27:b0:18b:d823:5362 with SMTP id y39-20020a056a203d2700b0018bd8235362mr1093286pzi.48.1701883212039; Wed, 06 Dec 2023 09:20:12 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id p21-20020a056a000b5500b006cea1e12564sm234388pfo.81.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 69C5174062C; 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 03/17] x86/cet: Don't assume that SHSTK implies IBT Date: Wed, 6 Dec 2023 09:19:56 -0800 Message-ID: <20231206172010.1023415-4-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.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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+incoming=patchwork.ozlabs.org@sourceware.org Since shadow stack (SHSTK) is enabled in the Linux kernel without enabling indirect branch tracking (IBT), don't assume that SHSTK implies IBT. Use "CPU_FEATURE_ACTIVE (IBT)" to check if IBT is active and "CPU_FEATURE_ACTIVE (SHSTK)" to check if SHSTK is active. --- sysdeps/x86/Makefile | 1 - sysdeps/x86/tst-cet-legacy-10.c | 6 +++--- sysdeps/x86/tst-cet-legacy-8.c | 15 ++++++++------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile index 917c26f116..ea45aad34c 100644 --- a/sysdeps/x86/Makefile +++ b/sysdeps/x86/Makefile @@ -165,7 +165,6 @@ CFLAGS-tst-cet-legacy-mod-6a.c += -fcf-protection=branch CFLAGS-tst-cet-legacy-mod-6b.c += -fcf-protection CFLAGS-tst-cet-legacy-mod-6c.c += -fcf-protection CFLAGS-tst-cet-legacy-7.c += -fcf-protection=none -CFLAGS-tst-cet-legacy-8.c += -mshstk CFLAGS-tst-cet-legacy-10.c += -mshstk CFLAGS-tst-cet-legacy-10-static.c += -mshstk diff --git a/sysdeps/x86/tst-cet-legacy-10.c b/sysdeps/x86/tst-cet-legacy-10.c index a85cdc3171..ae2c34de3e 100644 --- a/sysdeps/x86/tst-cet-legacy-10.c +++ b/sysdeps/x86/tst-cet-legacy-10.c @@ -21,19 +21,19 @@ #include #include -/* Check that CPU_FEATURE_ACTIVE on IBT and SHSTK matches _get_ssp. */ +/* Check that CPU_FEATURE_ACTIVE on SHSTK matches _get_ssp. */ static int do_test (void) { if (_get_ssp () != 0) { - if (CPU_FEATURE_ACTIVE (IBT) && CPU_FEATURE_ACTIVE (SHSTK)) + if (CPU_FEATURE_ACTIVE (SHSTK)) return EXIT_SUCCESS; } else { - if (!CPU_FEATURE_ACTIVE (IBT) && !CPU_FEATURE_ACTIVE (SHSTK)) + if (!CPU_FEATURE_ACTIVE (SHSTK)) return EXIT_SUCCESS; } diff --git a/sysdeps/x86/tst-cet-legacy-8.c b/sysdeps/x86/tst-cet-legacy-8.c index 5d8d9ba7dc..77d77a5408 100644 --- a/sysdeps/x86/tst-cet-legacy-8.c +++ b/sysdeps/x86/tst-cet-legacy-8.c @@ -18,7 +18,7 @@ #include #include -#include +#include #include #include #include @@ -29,11 +29,6 @@ static int do_test (void) { - /* NB: This test should trigger SIGSEGV on CET platforms. If SHSTK - is disabled, assuming IBT is also disabled. */ - if (_get_ssp () == 0) - return EXIT_UNSUPPORTED; - void (*funcp) (void); funcp = xmmap (NULL, 0x1000, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1); @@ -41,8 +36,14 @@ do_test (void) /* Write RET instruction. */ *(char *) funcp = 0xc3; funcp (); + + /* NB: This test should trigger SIGSEGV when IBT is active. We should + reach here if IBT isn't active. */ + if (!CPU_FEATURE_ACTIVE (IBT)) + return EXIT_UNSUPPORTED; + return EXIT_FAILURE; } -#define EXPECTED_SIGNAL (_get_ssp () == 0 ? 0 : SIGSEGV) +#define EXPECTED_SIGNAL (CPU_FEATURE_ACTIVE (IBT) ? SIGSEGV : 0) #include 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: 1872900 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=kpCslpPr; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SlkhH2p5dz23nD for ; Thu, 7 Dec 2023 04:21:03 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ACDA13861881 for ; Wed, 6 Dec 2023 17:20:59 +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+incoming=patchwork.ozlabs.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 From patchwork Wed Dec 6 17:19:58 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: 1872897 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Rk3TxPql; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Slkgz6Zb8z23nD for ; Thu, 7 Dec 2023 04:20:47 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E9312385AE4F for ; Wed, 6 Dec 2023 17:20:45 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by sourceware.org (Postfix) with ESMTPS id 5EB68385842A for ; Wed, 6 Dec 2023 17:20:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5EB68385842A 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 5EB68385842A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883217; cv=none; b=BeprAR2wV9hlRbD6+jW/DuunOS39J2HiZ5nQbwd9o7VOEeJFuNnXiooiXvPT6pMLV/2hWYXo02SB2TYSnGnvqTOZd2FvnzWXt5vqYa17eDQTJF2uHWLw+3i3qjbzLf7cGrh+9R1VT8dTZSvBuromgRJ/81sfecJ3DgKjH6Ok2SU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883217; c=relaxed/simple; bh=o8x5M5AVfOtzugcvvDnZ60H12xeU9uueCjSjpH2ftZM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=JFxiZm/6Qf1ovgcp9jWDTUhvtYD2N4cUQ9OC96CeYjSUtOxVdBpfSenamqdbUcq25KJPWd5Igq04tGpDbYXOSBNTGVPjQnOwy0jxP+GLIX0bnJcBpoO5K4ZTe4/Px5Fo4O3qluiXhB/myW7dtw2Qfyc+DQCEboshe2zC0ZRfDjU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1d0897e99e0so26628315ad.3 for ; Wed, 06 Dec 2023 09:20:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883215; x=1702488015; 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=Yor7DQPNWfZlkzKfxkVaOBO/MSsJWWmMQkcNtvrZLTM=; b=Rk3TxPqlRkTTEr/xghW/uTfgfoJ5pOTHh1UCWbX21MaOdlcEReni3UmiOvmko4tR99 IfxZiXC1ZXbL95NtCg7gaRYHiNxWrmjoD7jwaIhZ47fajDDORB0j5JN+q/PGy5lqu5d7 KpQtiR6lQVk3FjDtiOVCaboLfy/3VXZwdgztj9bBIxyxfNgblqcgNEo/oSTqyxo9oLgG oXefwFEh73xFWg7Kk2/eWLu5rBZw9izLiXukeSO92jKDUUp+RcZ968dBC6u/gCtfGTcP OhHJWGq71tu0blxwaax/ddTAG1KjOiVZJojlttLPWW9ecqhrFU2L5KKHbjLGA4fvzGs0 9Hjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883215; x=1702488015; 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=Yor7DQPNWfZlkzKfxkVaOBO/MSsJWWmMQkcNtvrZLTM=; b=rJ5ipS1l2RhjSMaQesv1LqneRdY+BiJhmgYhevLSjfevWJlzDbDV+S1PNRKg0lskTr y4Ukboe46/Qq/6E384D1RD/f4skKHvHZxcsB8oEhuZeOnxf8F5uE1pt2LB9CDqvp8biK SkxISXn+5hbATw5KpiwKPpLPzRezOIeGFWP+yL0ho4WACuAFgBi3QKwLgM+uqf8q9DWj dVm9cBdImuJ+MA9/1DN6TvFGzT+Nj2/+nvrWhYbWi20V27il0FHOWKbiw3EMXpVGN12m 4h8mVFfk2vxkP8WpOyDc15+h+aG1SDIUlgRN5u9XFoLP+Cnj+CCv+HV+kW5k28Flc13O qK4w== X-Gm-Message-State: AOJu0YzL6GFQ8Q2H4FCUmUYSfRojOHKYcBYdcQ+q6WWiUrbw7UF7Dds0 UucbweWY0xIjXzRdFtVyNNvkegM3TEM= X-Google-Smtp-Source: AGHT+IFO3abV2EKQZrN2ckdnjoVnBcR3PzCNfDjTrhhaLi32+0w0l960zPJ8eSGzuKxXMcSFfRqHtw== X-Received: by 2002:a17:902:820b:b0:1d0:7407:355d with SMTP id x11-20020a170902820b00b001d07407355dmr956482pln.50.1701883215300; Wed, 06 Dec 2023 09:20:15 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id iz4-20020a170902ef8400b001cfd049528esm81810plb.110.2023.12.06.09.20.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:14 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 7F61F74062E; 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 05/17] x86/cet: Check CPU_FEATURE_ACTIVE when CET is disabled Date: Wed, 6 Dec 2023 09:19:58 -0800 Message-ID: <20231206172010.1023415-6-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, 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+incoming=patchwork.ozlabs.org@sourceware.org Verify that CPU_FEATURE_ACTIVE (SHSTK) works properly when CET is disabled. --- sysdeps/x86/Makefile | 8 ++++++-- sysdeps/x86/tst-cet-legacy-10a-static.c | 2 ++ sysdeps/x86/tst-cet-legacy-10a.c | 2 ++ 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 sysdeps/x86/tst-cet-legacy-10a-static.c create mode 100644 sysdeps/x86/tst-cet-legacy-10a.c diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile index dea14e343c..580c3ecdc5 100644 --- a/sysdeps/x86/Makefile +++ b/sysdeps/x86/Makefile @@ -126,8 +126,10 @@ tests += tst-cet-legacy-1 tst-cet-legacy-1a tst-cet-legacy-2 \ tst-cet-legacy-2a tst-cet-legacy-3 tst-cet-legacy-4 \ tst-cet-legacy-5a tst-cet-legacy-6a tst-cet-legacy-7 \ tst-cet-legacy-8 tst-cet-legacy-9 tst-cet-legacy-9-static \ - tst-cet-legacy-10 tst-cet-legacy-10-static -tests-static += tst-cet-legacy-9-static tst-cet-legacy-10-static + tst-cet-legacy-10 tst-cet-legacy-10-static \ + tst-cet-legacy-10a tst-cet-legacy-10a-static +tests-static += tst-cet-legacy-9-static tst-cet-legacy-10-static \ + tst-cet-legacy-10a-static tst-cet-legacy-1a-ARGS = -- $(host-test-program-cmd) tests += \ @@ -182,6 +184,8 @@ CFLAGS-tst-cet-legacy-mod-6c.c += -fcf-protection 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-cet-legacy-10a.c += -fcf-protection=none +CFLAGS-tst-cet-legacy-10a-static.c += -fcf-protection=none CFLAGS-tst-shstk-legacy-1a.c += -fcf-protection=none CFLAGS-tst-shstk-legacy-1a-static.c += -fcf-protection=none diff --git a/sysdeps/x86/tst-cet-legacy-10a-static.c b/sysdeps/x86/tst-cet-legacy-10a-static.c new file mode 100644 index 0000000000..05073a5d1e --- /dev/null +++ b/sysdeps/x86/tst-cet-legacy-10a-static.c @@ -0,0 +1,2 @@ +#pragma GCC target ("shstk") +#include "tst-cet-legacy-10.c" diff --git a/sysdeps/x86/tst-cet-legacy-10a.c b/sysdeps/x86/tst-cet-legacy-10a.c new file mode 100644 index 0000000000..05073a5d1e --- /dev/null +++ b/sysdeps/x86/tst-cet-legacy-10a.c @@ -0,0 +1,2 @@ +#pragma GCC target ("shstk") +#include "tst-cet-legacy-10.c" From patchwork Wed Dec 6 17:19:59 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: 1872902 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=cOXJzpfG; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Slkhj1bgsz23nD for ; Thu, 7 Dec 2023 04:21:25 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ABB02384773C for ; Wed, 6 Dec 2023 17:21:18 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by sourceware.org (Postfix) with ESMTPS id E60983857C69 for ; Wed, 6 Dec 2023 17:20:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E60983857C69 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 E60983857C69 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::102a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883219; cv=none; b=GcpIyBkyjo4jeUi1Z8/VvoBvDoy1GZefx/CfeVHN+MxMsTtRHavCqZZvywbrzZ7/zuRT/bWuxfE+CAT4MDt+EibLW1pELqlrF/bCO9aaTd4UEsVmPelfEqDM49B30x5tywD2sRl6/Gnr3C8lK4ZzhmnRwc0nnPuiRK8+wDK8ZKk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883219; c=relaxed/simple; bh=wV7BjJu894nE4AYl90sNAjHFW+N7Ska8uveZsjZHLHw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ugqf2Wswn4u1b/kd6NRF98oC+Uc6vKd43IyIQv6DPR03+U0x2vpvTCvLk7uY+9CBG6F1Y0unzQp+U1wxlFZ0QLlcIvXwWStz0/87057TkQ9wzeotUGhPYfjtK83FTsiUg4IiT8mIrMCdmMc166uKXlSQC081RT4zG20FmoOUrjM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-28647f4ebd9so38436a91.3 for ; Wed, 06 Dec 2023 09:20:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883216; x=1702488016; 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=GOSE4t1msgsYWOi+j3WV+bBpkEvf1QHzZChc7NU2YRM=; b=cOXJzpfGKhsTTOPc9P349wfG1K4dpDW3dneN8vwDX7GgEXHB9Ud3UbL6N8ZOlIQ+fZ mJzzpOFFKfnonRU2HzbpIvJlAt57ji/e4E2QMwHke1lXmGyLAPMa1g3uqrVhv/PUpcvC mE+sdw4kx+yoW+O53EpD0bgkn32h7LwQ8mTBj8VClXx6Qxmj4WGIYdDZejJeS6TT8c0g sOge3v8ZHmdZNv8lk1IRD0c2ryB5TIJ3gKnKobGmI1PwlTxp3TFd/KqFr58ixU565dWt xWd9+pFNgJU3ROFW05CtwY/8R5OgKCF9ZWEasbtTEhzpNcD8Vx1/7fH0ODfWtPCFtEsu +czQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883216; x=1702488016; 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=GOSE4t1msgsYWOi+j3WV+bBpkEvf1QHzZChc7NU2YRM=; b=C/RIe4BvHKmdixxvlCSz2nsAtUlOIPYsEI6YkkiWElJRcl494/aJ9o65Rh9MxdkLVW iMLRlrgey/XBk7Dxs4zVaSiuLOk03s412y+A/OogD2rbq095wV0gVii/GjOLGrQ5lJPF M89AtGdjXMdRKTHyubbJy29WhfuV4zMrHrb066xAnZ56XCY0HS5xqtYIrQOhj7pfnhsj Ug/8e31t71lpGjXoXa9gT84q5vHKKG4Mce36liMH042WIFBh8/qb7j0EtE9bixjzF+Ja Jol1nofvcjvKkAFBDSc2km1oJnghDPHn3p37Y3+IrIWjdXwrImu6Newj6eME4ycSFTYt pe3A== X-Gm-Message-State: AOJu0Ywy0wr7tcbMq9YPNR9qNq3LraP+EskSPWVXy8lp9p7fgyp9+P5S wX/44kZ9s00jSjQ7LmXn2ex4Aeijh30= X-Google-Smtp-Source: AGHT+IGZA/048B8tlvq6RCB7nInTffi5+2h/QYfwqt+klfFpWhhuXC9Qw9pPGPN1X9xLFYfCRFYyOQ== X-Received: by 2002:a17:90b:1241:b0:286:6cc1:5fce with SMTP id gx1-20020a17090b124100b002866cc15fcemr1031711pjb.81.1701883215842; Wed, 06 Dec 2023 09:20:15 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id d7-20020a17090b004700b002864c14063fsm80973pjt.20.2023.12.06.09.20.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:14 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 8A1F174062F; 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 06/17] x86/cet: Add tests for GLIBC_TUNABLES=glibc.cpu.hwcaps=-SHSTK Date: Wed, 6 Dec 2023 09:19:59 -0800 Message-ID: <20231206172010.1023415-7-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=-3025.0 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+incoming=patchwork.ozlabs.org@sourceware.org Verify that GLIBC_TUNABLES=glibc.cpu.hwcaps=-SHSTK turns off shadow stack properly. --- sysdeps/x86/Makefile | 7 +++++++ sysdeps/x86/tst-shstk-legacy-1c-static.c | 1 + sysdeps/x86/tst-shstk-legacy-1c.c | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 sysdeps/x86/tst-shstk-legacy-1c-static.c create mode 100644 sysdeps/x86/tst-shstk-legacy-1c.c diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile index 580c3ecdc5..5f1414fba3 100644 --- a/sysdeps/x86/Makefile +++ b/sysdeps/x86/Makefile @@ -137,10 +137,13 @@ tests += \ tst-shstk-legacy-1a-static \ tst-shstk-legacy-1b \ tst-shstk-legacy-1b-static \ + tst-shstk-legacy-1c \ + tst-shstk-legacy-1c-static \ # tests tests-static += \ tst-shstk-legacy-1a-static \ tst-shstk-legacy-1b-static \ + tst-shstk-legacy-1c-static \ # tests-static extra-objs += \ tst-shstk-legacy-1-extra.o \ @@ -227,6 +230,10 @@ $(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 +tst-shstk-legacy-1c-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=-SHSTK +tst-shstk-legacy-1c-static-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=-SHSTK +$(objpfx)tst-shstk-legacy-1c: $(objpfx)tst-shstk-legacy-1-extra.o +$(objpfx)tst-shstk-legacy-1c-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-1c-static.c b/sysdeps/x86/tst-shstk-legacy-1c-static.c new file mode 100644 index 0000000000..91ea346aaf --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1c-static.c @@ -0,0 +1 @@ +#include "tst-shstk-legacy-1c.c" diff --git a/sysdeps/x86/tst-shstk-legacy-1c.c b/sysdeps/x86/tst-shstk-legacy-1c.c new file mode 100644 index 0000000000..eb218c6c70 --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1c.c @@ -0,0 +1,20 @@ +/* Check that legacy shadow stack code won't trigger segfault with + GLIBC_TUNABLES=glibc.cpu.hwcaps=-SHSTK + 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 "tst-shstk-legacy-1a.c" From patchwork Wed Dec 6 17:20:00 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: 1872908 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=dRMEvlDK; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SlkjY0VBRz23nD for ; Thu, 7 Dec 2023 04:22:09 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D85313875DE4 for ; Wed, 6 Dec 2023 17:21:54 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by sourceware.org (Postfix) with ESMTPS id 71F723858002 for ; Wed, 6 Dec 2023 17:20:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 71F723858002 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 71F723858002 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1033 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883222; cv=none; b=hFBSy9mJxrK25FLmKYggqoz4pPk4z1LLsVYoGFMaikNm/T9H4H4tbpClEu4VrdM4nNJI5Ri7s7FUVt6KRxm0G0CVChPD7QBQt1IldBJiRbIYXz0guvqM+1teGQ4vwoZsoZUAg+MPq3MDzGQwMe9cYk3S2gDoC5pRSsF7qKG+uv8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883222; c=relaxed/simple; bh=VJlKekKsFPa86ZKwlrttJyotssr7E0k1EOF+o9P9sn8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=C1S6x28uOHG4hjHbSZW7Zfm9wV6qSzIwhMhz+qf+rVnxc0VWXYIB2df+ECx6iHCJlzUH4frNkegoOt2SSxjUu1ySczY62z5MhUpF8z95oVrJE04v4rEsbGFBCaqhksE3UJULmYIwfwgdzm4A4YMo/vWpIhluofAI2aJdXcIDczI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pj1-x1033.google.com with SMTP id 98e67ed59e1d1-2886579d59fso55578a91.1 for ; Wed, 06 Dec 2023 09:20:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883217; x=1702488017; 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=0d8006v/xxHx85NWEJQnBj70U5Sbx8p61PZOj9Rqv0Q=; b=dRMEvlDKYn0/JDOBvQ7LzmGZSgsGrRLoY24bYuRhbq04W9g4QgMezxdiDKyY2euXEB crm/+5bZxUqqR5bkqlNUrSv90xU0jbpdcKPf2Wo6zxd2t033kZ46YMdJ3Sbmd63I5vF6 Fp0qoqehZpVWnSOZepI518RiL6oRPko5ZCVu0KiOZy+VD111FTfDXNvaDHbIUbAU9lHW zBVqbP27RO9WZqT7vO1yJqX1C2EjuHwnQQlm6dv15QRmYzp1hMc2GPu/RWXPA327JYmE 94pAJGAuU1Ai1es4UgdwGFQfhRwp9mBpsESS0rXlmlvKmRpFzypuMPk5pSnd/AvxF5Qr O5Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883217; x=1702488017; 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=0d8006v/xxHx85NWEJQnBj70U5Sbx8p61PZOj9Rqv0Q=; b=xJW0LJaFKsp/6DnB+LlA8kAhxfJyIraQBu7+ojiV+PrB2DS/7FDjhY9lvzoaqVekGP Fc8Rx89E/VwNZoUZewPgMP5mj3e95UQrrsEnoID33XgqF8Cv5Ky01O7Z14pMU8ECHJua uNXlb0wXNxltVZA6EcNyzRyg1bgcCVlxhkWJt8evgOGLUktO+XpZaOjkWH9kOJn93OxR ebiKB4LP+pU/W2gPl7RoTjOBNjvY9JhNx0zJoh99fRqJe0Kf5FVBPqrrDSaO3UnGSgI9 nJ8yL0QbKR7kmq63lTAC8d6rwIfanC2xPHozp85DyyQs4QiDtjSPnm3fP2sDUW1Xbtr+ Ss9Q== X-Gm-Message-State: AOJu0Yy+7tJyGBaz23hHBC1HepXoCtu6eqh/VTNXPk7+3MH6u/i0Zgww dvpDNXDHSQwNfNi+YLEpAD7NueBkW3w= X-Google-Smtp-Source: AGHT+IE4erKzPW1wudht/A1xPq/IG+eriY5ExSwi/ZucsxyUcb53s3ttGUWtx2PAp6kjtbQkfyix3w== X-Received: by 2002:a17:90a:df82:b0:286:6cc0:cace with SMTP id p2-20020a17090adf8200b002866cc0cacemr1323409pjv.69.1701883217296; Wed, 06 Dec 2023 09:20:17 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id sr12-20020a17090b4e8c00b002868d090c27sm76728pjb.25.2023.12.06.09.20.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:16 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 95E91740630; 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 07/17] x86/cet: Check legacy shadow stack code in .init_array section Date: Wed, 6 Dec 2023 09:20:00 -0800 Message-ID: <20231206172010.1023415-8-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=-3025.0 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+incoming=patchwork.ozlabs.org@sourceware.org Verify that legacy shadow stack code in .init_array section in application and shared library, which are marked as shadow stack enabled, will trigger segfault. --- sysdeps/x86/Makefile | 36 +++++++++++++++ sysdeps/x86/tst-shstk-legacy-1d-static.c | 1 + sysdeps/x86/tst-shstk-legacy-1d.c | 47 ++++++++++++++++++++ sysdeps/x86/tst-shstk-legacy-1e-static.c | 1 + sysdeps/x86/tst-shstk-legacy-1e-static.sh | 32 ++++++++++++++ sysdeps/x86/tst-shstk-legacy-1e.c | 53 +++++++++++++++++++++++ sysdeps/x86/tst-shstk-legacy-1e.sh | 34 +++++++++++++++ sysdeps/x86/tst-shstk-legacy-1f.c | 29 +++++++++++++ sysdeps/x86/tst-shstk-legacy-1g.c | 35 +++++++++++++++ sysdeps/x86/tst-shstk-legacy-1g.sh | 34 +++++++++++++++ sysdeps/x86/tst-shstk-legacy-mod-1.c | 28 ++++++++++++ 11 files changed, 330 insertions(+) create mode 100644 sysdeps/x86/tst-shstk-legacy-1d-static.c create mode 100644 sysdeps/x86/tst-shstk-legacy-1d.c create mode 100644 sysdeps/x86/tst-shstk-legacy-1e-static.c create mode 100755 sysdeps/x86/tst-shstk-legacy-1e-static.sh create mode 100644 sysdeps/x86/tst-shstk-legacy-1e.c create mode 100755 sysdeps/x86/tst-shstk-legacy-1e.sh create mode 100644 sysdeps/x86/tst-shstk-legacy-1f.c create mode 100644 sysdeps/x86/tst-shstk-legacy-1g.c create mode 100755 sysdeps/x86/tst-shstk-legacy-1g.sh create mode 100644 sysdeps/x86/tst-shstk-legacy-mod-1.c diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile index 5f1414fba3..b8b98177e0 100644 --- a/sysdeps/x86/Makefile +++ b/sysdeps/x86/Makefile @@ -139,11 +139,22 @@ tests += \ tst-shstk-legacy-1b-static \ tst-shstk-legacy-1c \ tst-shstk-legacy-1c-static \ + tst-shstk-legacy-1d \ + tst-shstk-legacy-1d-static \ + tst-shstk-legacy-1e \ + tst-shstk-legacy-1e-static \ + tst-shstk-legacy-1f \ + tst-shstk-legacy-1g \ # tests +modules-names += \ + tst-shstk-legacy-mod-1 \ +# modules-names tests-static += \ tst-shstk-legacy-1a-static \ tst-shstk-legacy-1b-static \ tst-shstk-legacy-1c-static \ + tst-shstk-legacy-1d-static \ + tst-shstk-legacy-1e-static \ # tests-static extra-objs += \ tst-shstk-legacy-1-extra.o \ @@ -192,6 +203,9 @@ CFLAGS-tst-cet-legacy-10a-static.c += -fcf-protection=none CFLAGS-tst-shstk-legacy-1a.c += -fcf-protection=none CFLAGS-tst-shstk-legacy-1a-static.c += -fcf-protection=none +CFLAGS-tst-shstk-legacy-1d.c += -fcf-protection=none +CFLAGS-tst-shstk-legacy-1d-static.c += -fcf-protection=none +CFLAGS-tst-shstk-legacy-1f.c += -fcf-protection=none $(objpfx)tst-cet-legacy-1: $(objpfx)tst-cet-legacy-mod-1.so \ $(objpfx)tst-cet-legacy-mod-2.so @@ -234,6 +248,28 @@ tst-shstk-legacy-1c-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=-SHSTK tst-shstk-legacy-1c-static-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=-SHSTK $(objpfx)tst-shstk-legacy-1c: $(objpfx)tst-shstk-legacy-1-extra.o $(objpfx)tst-shstk-legacy-1c-static: $(objpfx)tst-shstk-legacy-1-extra.o +$(objpfx)tst-shstk-legacy-1d: $(objpfx)tst-shstk-legacy-1-extra.o +$(objpfx)tst-shstk-legacy-1d-static: $(objpfx)tst-shstk-legacy-1-extra.o +$(objpfx)tst-shstk-legacy-1e: $(objpfx)tst-shstk-legacy-1-extra.o +$(objpfx)tst-shstk-legacy-1e-static: $(objpfx)tst-shstk-legacy-1-extra.o +$(objpfx)tst-shstk-legacy-1e.out: \ + $(..)/sysdeps/x86/tst-shstk-legacy-1e.sh $(objpfx)tst-shstk-legacy-1e + $(SHELL) $< $(common-objpfx) '$(test-program-prefix)' 2> $@; \ + $(evaluate-test) +$(objpfx)tst-shstk-legacy-1e-static.out: \ + $(..)/sysdeps/x86/tst-shstk-legacy-1e-static.sh \ + $(objpfx)tst-shstk-legacy-1e-static + $(SHELL) $< $(common-objpfx) 2> $@; \ + $(evaluate-test) +$(objpfx)tst-shstk-legacy-1f: $(objpfx)tst-shstk-legacy-mod-1.so +$(objpfx)tst-shstk-legacy-mod-1.so: \ + $(objpfx)tst-shstk-legacy-mod-1.os \ + $(objpfx)tst-shstk-legacy-1-extra.os +$(objpfx)tst-shstk-legacy-1g: $(objpfx)tst-shstk-legacy-mod-1.so +$(objpfx)tst-shstk-legacy-1g.out: \ + $(..)/sysdeps/x86/tst-shstk-legacy-1g.sh $(objpfx)tst-shstk-legacy-1g + $(SHELL) $< $(common-objpfx) '$(test-program-prefix)' 2> $@; \ + $(evaluate-test) endif # Add -fcf-protection to CFLAGS when CET is enabled. diff --git a/sysdeps/x86/tst-shstk-legacy-1d-static.c b/sysdeps/x86/tst-shstk-legacy-1d-static.c new file mode 100644 index 0000000000..dca27a5482 --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1d-static.c @@ -0,0 +1 @@ +#include "tst-shstk-legacy-1d.c" diff --git a/sysdeps/x86/tst-shstk-legacy-1d.c b/sysdeps/x86/tst-shstk-legacy-1d.c new file mode 100644 index 0000000000..465cfab1db --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1d.c @@ -0,0 +1,47 @@ +/* Check that legacy shadow stack code in init_array 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 in init_array won't trigger + segfault. */ +extern void legacy (void); +int done; + +void +legacy_1 (void) +{ + legacy (); + done = 1; +} + +void (*init_array []) (void) + __attribute__ ((section (".init_array"), aligned (sizeof (void *)))) = +{ + &legacy_1 +}; + +static int +do_test (void) +{ + return EXIT_SUCCESS; +} + +#include diff --git a/sysdeps/x86/tst-shstk-legacy-1e-static.c b/sysdeps/x86/tst-shstk-legacy-1e-static.c new file mode 100644 index 0000000000..cb6ce0de00 --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1e-static.c @@ -0,0 +1 @@ +#include "tst-shstk-legacy-1e.c" diff --git a/sysdeps/x86/tst-shstk-legacy-1e-static.sh b/sysdeps/x86/tst-shstk-legacy-1e-static.sh new file mode 100755 index 0000000000..e943aec70e --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1e-static.sh @@ -0,0 +1,32 @@ +#!/bin/sh +# Check that legacy shadow stack code in init_array 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 +# . + +common_objpfx=$1; shift + +${common_objpfx}elf/tst-shstk-legacy-1e-static +# The exit status should only be unsupported (77) or segfault (139). +status=$? +if test $status -eq 77; then + exit 77 +elif test $status == 139; then + exit 0 +else + exit 1 +fi diff --git a/sysdeps/x86/tst-shstk-legacy-1e.c b/sysdeps/x86/tst-shstk-legacy-1e.c new file mode 100644 index 0000000000..e78a4b776e --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1e.c @@ -0,0 +1,53 @@ +/* Check that legacy shadow stack code in init_array 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 in init_array will trigger + segfault. */ +extern void legacy (void); +int done; + +void +legacy_1 (void) +{ + legacy (); + done = 1; +} + +void (*init_array []) (void) + __attribute__ ((section (".init_array"), aligned (sizeof (void *)))) = +{ + &legacy_1 +}; + +static int +do_test (void) +{ + if (!CPU_FEATURE_ACTIVE (SHSTK)) + return EXIT_UNSUPPORTED; + + return EXIT_FAILURE; +} + +#define EXPECTED_SIGNAL (CPU_FEATURE_ACTIVE (SHSTK) ? SIGSEGV : 0) +#include diff --git a/sysdeps/x86/tst-shstk-legacy-1e.sh b/sysdeps/x86/tst-shstk-legacy-1e.sh new file mode 100755 index 0000000000..b0467aa899 --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1e.sh @@ -0,0 +1,34 @@ +#!/bin/sh +# Check that legacy shadow stack code in init_array 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 +# . + +common_objpfx=$1; shift +test_program_prefix=$1; shift + +${test_program_prefix} \ + ${common_objpfx}elf/tst-shstk-legacy-1e +# The exit status should only be unsupported (77) or segfault (139). +status=$? +if test $status -eq 77; then + exit 77 +elif test $status == 139; then + exit 0 +else + exit 1 +fi diff --git a/sysdeps/x86/tst-shstk-legacy-1f.c b/sysdeps/x86/tst-shstk-legacy-1f.c new file mode 100644 index 0000000000..27e01a229e --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1f.c @@ -0,0 +1,29 @@ +/* Check that legacy shadow stack code in init_array 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 + +static int +do_test (void) +{ + return EXIT_SUCCESS; +} + +#include diff --git a/sysdeps/x86/tst-shstk-legacy-1g.c b/sysdeps/x86/tst-shstk-legacy-1g.c new file mode 100644 index 0000000000..a1f3d242e9 --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1g.c @@ -0,0 +1,35 @@ +/* Check that legacy shadow stack code in init_array 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 + +static int +do_test (void) +{ + if (!CPU_FEATURE_ACTIVE (SHSTK)) + return EXIT_UNSUPPORTED; + + return EXIT_FAILURE; +} + +#define EXPECTED_SIGNAL (CPU_FEATURE_ACTIVE (SHSTK) ? SIGSEGV : 0) +#include diff --git a/sysdeps/x86/tst-shstk-legacy-1g.sh b/sysdeps/x86/tst-shstk-legacy-1g.sh new file mode 100755 index 0000000000..c112bf6d8d --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-1g.sh @@ -0,0 +1,34 @@ +#!/bin/sh +# Check that legacy shadow stack code in init_array 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 +# . + +common_objpfx=$1; shift +test_program_prefix=$1; shift + +${test_program_prefix} \ + ${common_objpfx}elf/tst-shstk-legacy-1g +# The exit status should only be unsupported (77) or segfault (139). +status=$? +if test $status -eq 77; then + exit 77 +elif test $status == 139; then + exit 0 +else + exit 1 +fi diff --git a/sysdeps/x86/tst-shstk-legacy-mod-1.c b/sysdeps/x86/tst-shstk-legacy-mod-1.c new file mode 100644 index 0000000000..b75b5484d9 --- /dev/null +++ b/sysdeps/x86/tst-shstk-legacy-mod-1.c @@ -0,0 +1,28 @@ +/* Check legacy shadow stack code in init_array. + 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 + +/* Check legacy shadow stack code in init_array. */ +extern void legacy (void) __attribute__ ((visibility ("hidden"))); + +void (*init_array []) (void) + __attribute__ ((section (".init_array"), aligned (sizeof (void *)))) = +{ + &legacy +}; From patchwork Wed Dec 6 17:20:01 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: 1872898 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=KcE7NCaf; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Slkh14s12z23nD for ; Thu, 7 Dec 2023 04:20:49 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8A01E385BADA for ; Wed, 6 Dec 2023 17:20:47 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id 389823857BBC for ; Wed, 6 Dec 2023 17:20:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 389823857BBC 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 389823857BBC Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::632 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883219; cv=none; b=sNmp3HPlBCTNG6NzFJUUB0uazz1SH6t1ZidG80/liFAglmrN9lTH+y2k6ukczUde2N5BxQ/sE7axDPLQC4T9EkjoQIdCKoCDbwkJ/4sdG/nqGIgu0Q16aL/yuMp1blX4oqkD3IOi189g5I8k5dEC/2WaREHpBRtAWto2f9dmk8I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883219; c=relaxed/simple; bh=pNkCZXNIQh2xn6rz/UuD8LwErH0OJ5y7R5CbnmgwFZE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=oPt2/tcR3/eu1Zds8LwwIn4Yg4SzNy/UDzeUSHE+wxU1HtnHV7bJHL30lj0HKLZQBnITL4UJf8XbEx/qVJMOf+rxBw+w17SqggcTdubBhTLb2EOEolYftqtkSCwy7i9C1phhvDRnbDiTfqoAwQkFKbn7OMQw/oIwWo0xaqGmFms= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1d045097b4cso19945ad.0 for ; Wed, 06 Dec 2023 09:20:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883217; x=1702488017; 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=TMtBtpENyKJSljFp/XcjpQD/4uOEW3HiikpGCnYffbw=; b=KcE7NCaf1yVHbyfUCWePgtFajQp3kdQk/YM55uLl9g6Nfk2ixIQ3t6BagCuFey5h7M 126XRGK6UjOYSE/dxdebdmz87kBpQte+n2deOa10sOzj8xGOoD6zpDPKIeybdMpOU2mi Sl7CIGxyZjFquGY7AdsNOMQOSBGfiqtl2R5BxASj0zS5losyVkZenVeOb/vXLngP3tA3 YR0mLWZZHbUU2Y1YAnVdmg00pLjlCJpRTB8tqj+mw+ZlpPjXv/OP1QiPZs44u+IJ77cw O5W/H6kTeLalOFLzKzzJiE7bbClj15lAqpfz4enV2mNMYIonyO9+iA79uATOylPCri3+ xgPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883217; x=1702488017; 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=TMtBtpENyKJSljFp/XcjpQD/4uOEW3HiikpGCnYffbw=; b=T9gi8+GwUM5uAwmkZsU8Zwv60ed5WR4BNfCfL1fw9QsRqa6CIDNluq+jn+vcLRWFAk EjDLCIKCdiu4LHtTmNbOuwSeskJHMq+xB38NBEC9ihA/pWFDNeaLMM6kcNGOe2RV5i3c adLQY0g4Xg1HDsotNnpE0efxp4d3DAN6PCZQQQfybv+/mBdzzY5H3VESztBVekpF43bV E/0mTZAZwtp2sKjy45INQLxZWZuObq8V6nKlSX+JrBdbvLf8Wxm/KB3JH5G9XZ33gRl2 dZZ63QYLNVMxqMn68Lch9rITuWiRupDTQ4cqpuGzlKMxcv2eL0xIfdNuLaeQT2fwcQ4s wauA== X-Gm-Message-State: AOJu0Yyy8+pxkRckig18yBTGLbktO2cTzLZyWwSrVKjE3OyYVM7e2zIM pKIhMTDRfl9PpXg8xDNfrV4Blg0UF2w= X-Google-Smtp-Source: AGHT+IHYzX2qKSiWLzDtKPok7RLNcaakfSt2P8daJcSMmi3fl046qL7XOmHeAp3V6/xF1HP3m5zYxw== X-Received: by 2002:a17:902:da8b:b0:1d0:8988:cf93 with SMTP id j11-20020a170902da8b00b001d08988cf93mr1019760plx.28.1701883217159; Wed, 06 Dec 2023 09:20:17 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id s17-20020a170902ea1100b001cfb41589ddsm61152plg.293.2023.12.06.09.20.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:16 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id A21FF740631; 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 08/17] x86/cet: Check CPU_FEATURE_ACTIVE in permissive mode Date: Wed, 6 Dec 2023 09:20:01 -0800 Message-ID: <20231206172010.1023415-9-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.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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+incoming=patchwork.ozlabs.org@sourceware.org Verify that CPU_FEATURE_ACTIVE works properly in permissive mode. --- sysdeps/x86/Makefile | 1 + sysdeps/x86/tst-cet-legacy-4.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile index b8b98177e0..6ceefe16c7 100644 --- a/sysdeps/x86/Makefile +++ b/sysdeps/x86/Makefile @@ -174,6 +174,7 @@ CFLAGS-tst-cet-legacy-mod-1.c += -fcf-protection=none CFLAGS-tst-cet-legacy-mod-2.c += -fcf-protection=none CFLAGS-tst-cet-legacy-3.c += -fcf-protection=none CFLAGS-tst-cet-legacy-4.c += -fcf-protection=branch +CPPFLAGS-tst-cet-legacy-4a.c += -DCET_IS_PERMISSIVE=1 CFLAGS-tst-cet-legacy-4a.c += -fcf-protection CFLAGS-tst-cet-legacy-4b.c += -fcf-protection CFLAGS-tst-cet-legacy-mod-4.c += -fcf-protection=none diff --git a/sysdeps/x86/tst-cet-legacy-4.c b/sysdeps/x86/tst-cet-legacy-4.c index d75fb0e61c..c098120253 100644 --- a/sysdeps/x86/tst-cet-legacy-4.c +++ b/sysdeps/x86/tst-cet-legacy-4.c @@ -21,6 +21,7 @@ #include #include #include +#include #include @@ -40,6 +41,10 @@ do_test (void) return 0; } +#ifdef CET_IS_PERMISSIVE + TEST_VERIFY (!CPU_FEATURE_ACTIVE (IBT) && !CPU_FEATURE_ACTIVE (SHSTK)); +#endif + fp = dlsym (h, "test"); if (fp == NULL) FAIL_EXIT1 ("cannot get symbol 'test': %s\n", dlerror ()); From patchwork Wed Dec 6 17:20:02 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: 1872899 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=e4KmlDX+; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SlkhB3Nklz23nD for ; Thu, 7 Dec 2023 04:20:58 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5F144385AC21 for ; Wed, 6 Dec 2023 17:20:54 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by sourceware.org (Postfix) with ESMTPS id 107F33858C31 for ; Wed, 6 Dec 2023 17:20:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 107F33858C31 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 107F33858C31 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883219; cv=none; b=oFWoRW5Snhub/vrKB6ZcT93s6zFiEGqohsbxoJOHiRZXJ5N8IW006VVes5hgeb4+WO28AeJUTFpTcBtu2/GFhFcIvcBxnyh6oPyyEzwpWbaGOIuPpSfCLZDZHvPaBVcmkky+br8TUIErh1HV+SJ1RsUazV2YuJfxquXf5/B5S3Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883219; c=relaxed/simple; bh=dULd9GzM1XgCXwIkb/YLe/rpOcT+ioN+FmEXyduUYiY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=mdacF1vCVvoREwAm7se8TOAd4qIyRGYrPMRsv+4sTLVZL2MpZtg2uspYhZcWxowB3yqSQNF10rISVOqkmgdFN0ply+niMjp/DjSPVB5Gzsk92dn3/T5wAM58IPEv9fbMSJQSVytJsKQO1O0vJ8YrZ5g/mqzBjQjNLzRzNa3xS7I= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1d04dba2781so37911455ad.3 for ; Wed, 06 Dec 2023 09:20:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883217; x=1702488017; 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=2vjVugOhYDfsulJ/fZg2RiEMmfP1r78K2xJH/vIdDs0=; b=e4KmlDX+xYfxxw4W8dZGxKbZGUHhdvfvi7QFBwHibXa9USiLz3+nK0HltJMsuKY/JY xs6UkNyaGEfu//laLfIJ+NSZn5IEMeZwvPhbjaqNItyh6AOWS35vvACJARFoWbU37Vdh JZbKRO1z3Uy96i+65S1FXCtLmbu6PauC00WdwZzArvm/8k2K53FvQzMv0Ld/CaPpPHWv fk/W0XauO0HfeqHi8KfWX4MUZUw+I/s5caI3sZPEkz0daXhPSNWgEhbv2i9YjjWSo9M3 Lod87Ol4GDdvM1J+2BVnpUDlgpA3130USiZLcunLwFNSZqnoieLTDYNLMAjfwWptxqrJ ZJIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883217; x=1702488017; 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=2vjVugOhYDfsulJ/fZg2RiEMmfP1r78K2xJH/vIdDs0=; b=WdPjS7Kffpv6J0/TPOEZbILthcT/su0nXGmsxFF+WpDOhVl/ZL0hsBWl9pUCz7UZMG Lv/d2RQ5sLI/hQlNwRIbSFE4oZ2hBFAr/BLkInOUP3fI5+0inmsgM0V19SrO12uAVbgf 9MITC5K0x7S0uxDLfnM5gTHDOoPDToAZmjp6Ux6jwc1srnJLU0EJ4wZOs59J3GwgJaAX p2sV8RilcRoGrJpG+t68njgGol1sWtxbEYwWTOFoqsLXFv4PgBnvsQ+I/VdUiU2bA7VJ E1hEMhDbFt34BSgLm9HT4Qib0dO/Px+XxH7khbBJERtV2km3NV3SNhHTi6W17mEBo4hn tjDg== X-Gm-Message-State: AOJu0YyyuttiREhg47ZG1cN7/Vm77OjhVubyKIgrv1rFzM7aQ8ZfVx5e /Sg9weZLZXN1VKhZnxvm1Tk= X-Google-Smtp-Source: AGHT+IFTlxIfKDDaatY4CS0z33126TiTyY6zFYR/NL3huiLUxCB0bAIQJ9rt/Wn4NRSweXPNz9OwOA== X-Received: by 2002:a17:902:d488:b0:1d0:9e59:35f1 with SMTP id c8-20020a170902d48800b001d09e5935f1mr918892plg.114.1701883216981; Wed, 06 Dec 2023 09:20:16 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id jk23-20020a170903331700b001d0ca40158dsm64894plb.280.2023.12.06.09.20.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:16 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id AF420740632; 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 09/17] x86: Check PT_GNU_PROPERTY early Date: Wed, 6 Dec 2023 09:20:02 -0800 Message-ID: <20231206172010.1023415-10-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.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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+incoming=patchwork.ozlabs.org@sourceware.org The PT_GNU_PROPERTY segment is scanned before PT_NOTE. For binaries with the PT_GNU_PROPERTY segment, we can check it to avoid scan of the PT_NOTE segment. --- sysdeps/x86/dl-prop.h | 120 ++++++++++++++++++++++++++++-------------- 1 file changed, 80 insertions(+), 40 deletions(-) diff --git a/sysdeps/x86/dl-prop.h b/sysdeps/x86/dl-prop.h index b2836f3009..d2c53c2182 100644 --- a/sysdeps/x86/dl-prop.h +++ b/sysdeps/x86/dl-prop.h @@ -81,6 +81,60 @@ _dl_open_check (struct link_map *m) #endif } +/* Check the GNU property and return its value. It returns: + -1: Skip this note. + 0: Stop checking. + 1: Continue to check. + */ +static inline int +_dl_check_gnu_property (unsigned int type, unsigned int datasz, + void *ptr, unsigned int *feature_1_and, + unsigned int *needed_1, + unsigned int *isa_1_needed) +{ + if (type == GNU_PROPERTY_X86_FEATURE_1_AND + || type == GNU_PROPERTY_X86_ISA_1_NEEDED + || type == GNU_PROPERTY_1_NEEDED) + { + /* The sizes of types which we are searching for are + 4 bytes. There is no point to continue if this + note is ill-formed. */ + if (datasz != 4) + return -1; + + /* NB: Stop the scan only after seeing all types which + we are searching for. */ + _Static_assert (((GNU_PROPERTY_X86_ISA_1_NEEDED + > GNU_PROPERTY_X86_FEATURE_1_AND) + && (GNU_PROPERTY_X86_FEATURE_1_AND + > GNU_PROPERTY_1_NEEDED)), + "GNU_PROPERTY_X86_ISA_1_NEEDED > " + "GNU_PROPERTY_X86_FEATURE_1_AND && " + "GNU_PROPERTY_X86_FEATURE_1_AND > " + "GNU_PROPERTY_1_NEEDED"); + if (type == GNU_PROPERTY_X86_FEATURE_1_AND) + *feature_1_and = *(unsigned int *) ptr; + else if (type == GNU_PROPERTY_1_NEEDED) + *needed_1 = *(unsigned int *) ptr; + else + { + *isa_1_needed = *(unsigned int *) ptr; + + /* Keep searching for the next GNU property note + generated by the older linker. */ + return 0; + } + } + else if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) + { + /* Stop the scan since property type is in ascending + order. */ + return 0; + } + + return 1; +} + static inline void __attribute__ ((unused)) _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, const ElfW(Addr) size, const ElfW(Addr) align) @@ -141,45 +195,14 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, last_type = type; - if (type == GNU_PROPERTY_X86_FEATURE_1_AND - || type == GNU_PROPERTY_X86_ISA_1_NEEDED - || type == GNU_PROPERTY_1_NEEDED) - { - /* The sizes of types which we are searching for are - 4 bytes. There is no point to continue if this - note is ill-formed. */ - if (datasz != 4) - return; - - /* NB: Stop the scan only after seeing all types which - we are searching for. */ - _Static_assert (((GNU_PROPERTY_X86_ISA_1_NEEDED - > GNU_PROPERTY_X86_FEATURE_1_AND) - && (GNU_PROPERTY_X86_FEATURE_1_AND - > GNU_PROPERTY_1_NEEDED)), - "GNU_PROPERTY_X86_ISA_1_NEEDED > " - "GNU_PROPERTY_X86_FEATURE_1_AND && " - "GNU_PROPERTY_X86_FEATURE_1_AND > " - "GNU_PROPERTY_1_NEEDED"); - if (type == GNU_PROPERTY_X86_FEATURE_1_AND) - feature_1_and = *(unsigned int *) ptr; - else if (type == GNU_PROPERTY_1_NEEDED) - needed_1 = *(unsigned int *) ptr; - else - { - isa_1_needed = *(unsigned int *) ptr; - - /* Keep searching for the next GNU property note - generated by the older linker. */ - break; - } - } - else if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) - { - /* Stop the scan since property type is in ascending - order. */ - break; - } + int result = _dl_check_gnu_property (type, datasz, ptr, + &feature_1_and, + &needed_1, + &isa_1_needed); + if (result == -1) + return; /* Skip this note. */ + else if (result == 0) + break; /* Stop checking. */ /* Check the next property item. */ ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr))); @@ -217,7 +240,24 @@ static inline int __attribute__ ((always_inline)) _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type, uint32_t datasz, void *data) { - return 0; + /* This is called on each GNU property. */ + unsigned int needed_1 = 0; + unsigned int feature_1_and = 0; + unsigned int isa_1_needed = 0; + int result = _dl_check_gnu_property (type, datasz, data, + &feature_1_and, &needed_1, + &isa_1_needed); + if (needed_1 != 0) + l->l_1_needed = needed_1; + if (isa_1_needed != 0) + l->l_x86_isa_1_needed = isa_1_needed; + if (feature_1_and != 0) + l->l_x86_feature_1_and = feature_1_and; + if ((needed_1 | isa_1_needed | feature_1_and) != 0) + l->l_property = lc_property_valid; + else if (l->l_property == lc_property_unknown) + l->l_property = lc_property_none; + return result <= 0 ? 0 : result; } #endif /* _DL_PROP_H */ From patchwork Wed Dec 6 17:20:03 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: 1872901 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Lep+32rP; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SlkhW4bbMz23nD for ; Thu, 7 Dec 2023 04:21:15 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E6023384F00C for ; Wed, 6 Dec 2023 17:21:10 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by sourceware.org (Postfix) with ESMTPS id 91E843858005 for ; Wed, 6 Dec 2023 17:20:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 91E843858005 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 91E843858005 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::530 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883221; cv=none; b=i9vy0AgiLaV6oUwwAKQrVbyX27eKP4rpCZBuiehsEBGr+thsfVoXfZdZAFj8EE8oJzLtbWWkfti4WB77Dpu5UHzoxJVGXZRwq0ksFGsKXMJ09CQcTfHVFcjDEr7EhCmYdZM43AFUHcgMHw6xVat3odyd1oV9EUO43rBPyscV2hU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883221; c=relaxed/simple; bh=U7KtRUd4W+8d58U0hiS7FViS0HH/Rp0YE4Is1fguNT0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=inh/C+C9lJ7zGILul4s78cWUfBqsDZRMvqEQGYmXnppnXNSUduh7Hv+PTgQZ/ZUk1uyqvphPHN8dhRbr2xr2wE+7RNweaF+Yg8mUZpyr0LITVbdtICQmg+t9q8L/T2+HHEdGRdiVuc5zxZ+yMC7QIswBBz5Il59dJhOXNmT071s= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x530.google.com with SMTP id 41be03b00d2f7-5c690c3d113so2857903a12.1 for ; Wed, 06 Dec 2023 09:20:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883217; x=1702488017; 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=WokAJGlqrtoizPOZj4+PiZkzpP7EhDImCNe72omnP0M=; b=Lep+32rPeAU1PBfpkmKlhRyWmoIL0PwApc9KdGU3iSgZzBM3ldQjcUIFJDbexcu4sB oDYjnpE8+NSTwz08oZ6ebExLUazz/YSo+KsQG/resKvYDCmN0h8OdhiBdMnA6mnMPiat YlrwHmk7YjO5b/twAxmD8hie02j8A5AbWfyW9t2v8V4pbVj4etRy5y1ox+ZJV5olR8Ua BIbQXMBcUBqFog6+yzg0GbgzoLO+QORfxcDE1JdQzeU/t7XobnfvkYAtieeyjzJk/oa/ otVqkxXrcU/G4YkgB6PStDGRkSRcL5EXV2YGU3chyaXr7MeMnH86zHah5T8EzzPk3KDs Gf1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883217; x=1702488017; 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=WokAJGlqrtoizPOZj4+PiZkzpP7EhDImCNe72omnP0M=; b=ccENnoLtZ1EEOxe9ndhJM6bOZNR/xltRmih1C9nJ389bW2YGeaOHJdc8leibCF4xfa Jrprt7JO6xKvG1nT3Cun4OMmUVJHqbrGI7gvfYaPSiMvMWRcWPscSCPBqfdWEfGhjZBF 1zD0U+WZgtgr6vpCjSa57QkBmn+rOc3B+PfG7YLONSg3IyM1DRSCrYZ9IRILIVPqZN80 TMPDxx+o2Q0Plybw8cJSlhx2FW2e2pCuCRgnFoarzFkA1Jnjc1XdLMZeu38oDrxD/t/C Ah6UPAGeoEH3VeDmi5io+OteeFcZCCnJgiC2aRyu2RXsKwBxTWLUhVN+6p0B/G0z0iwQ uNZA== X-Gm-Message-State: AOJu0YyliPSILGOK8Asz9+LGXOUTZ4U73M+fr5njk3oUK4tcul0YVP1Q /I5Y9n41363d6aTDI5PiNugPFLCxeW8= X-Google-Smtp-Source: AGHT+IHo/IyWWYLJLequpoDC2FJU2xdxoTMA72TvcIMEy4Un00JIhSazGq0tjLx+b7hwP7oe6STcmQ== X-Received: by 2002:a05:6a20:c1a6:b0:187:604a:3add with SMTP id bg38-20020a056a20c1a600b00187604a3addmr1466443pzb.24.1701883217499; Wed, 06 Dec 2023 09:20:17 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id a1-20020a637f01000000b005ac384b71cbsm150150pgd.60.2023.12.06.09.20.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:16 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id BCF5C740633; 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 10/17] x86: Modularize sysdeps/x86/dl-cet.c Date: Wed, 6 Dec 2023 09:20:03 -0800 Message-ID: <20231206172010.1023415-11-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=-3025.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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+incoming=patchwork.ozlabs.org@sourceware.org Improve readability and make maintenance easier for dl-feature.c by modularizing sysdeps/x86/dl-cet.c: 1. Support processors with: a. Only IBT. Or b. Only SHSTK. Or c. Both IBT and SHSTK. 2. Lock CET features only if IBT or SHSTK are enabled and are not enabled permissively. --- sysdeps/x86/dl-cet.c | 456 ++++++++++++++++++++++++++----------------- 1 file changed, 280 insertions(+), 176 deletions(-) diff --git a/sysdeps/x86/dl-cet.c b/sysdeps/x86/dl-cet.c index 60ea1cb558..67c51ee8c2 100644 --- a/sysdeps/x86/dl-cet.c +++ b/sysdeps/x86/dl-cet.c @@ -32,206 +32,310 @@ # error GNU_PROPERTY_X86_FEATURE_1_SHSTK != X86_FEATURE_1_SHSTK #endif -/* Check if object M is compatible with CET. */ +struct dl_cet_info +{ + const char *program; + + /* Check how IBT and SHSTK should be enabled. */ + enum dl_x86_cet_control enable_ibt_type; + enum dl_x86_cet_control enable_shstk_type; + + /* If IBT and SHSTK were previously enabled. */ + unsigned int feature_1_enabled; + + /* If IBT and SHSTK should be enabled. */ + unsigned int enable_feature_1; + + /* If there are any legacy shared object. */ + unsigned int feature_1_legacy; + + /* Which shared object is the first legacy shared object. */ + unsigned int feature_1_legacy_ibt; + unsigned int feature_1_legacy_shstk; +}; + +/* Check if the object M and its dependencies are legacy object. */ static void -dl_cet_check (struct link_map *m, const char *program) +dl_check_legacy_object (struct link_map *m, + struct dl_cet_info *info) { - /* Check how IBT should be enabled. */ - enum dl_x86_cet_control enable_ibt_type - = GL(dl_x86_feature_control).ibt; - /* Check how SHSTK should be enabled. */ - enum dl_x86_cet_control enable_shstk_type - = GL(dl_x86_feature_control).shstk; - - /* No legacy object check if both IBT and SHSTK are always on. */ - if (enable_ibt_type == cet_always_on - && enable_shstk_type == cet_always_on) + unsigned int i; + struct link_map *l = NULL; + + i = m->l_searchlist.r_nlist; + while (i-- > 0) { - THREAD_SETMEM (THREAD_SELF, header.feature_1, GL(dl_x86_feature_1)); - return; - } + /* Check each shared object to see if IBT and SHSTK are enabled. */ + l = m->l_initfini[i]; - /* Check if IBT is enabled by kernel. */ - bool ibt_enabled - = (GL(dl_x86_feature_1) & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0; - /* Check if SHSTK is enabled by kernel. */ - bool shstk_enabled - = (GL(dl_x86_feature_1) & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0; + if (l->l_init_called) + continue; - if (ibt_enabled || shstk_enabled) - { - struct link_map *l = NULL; - unsigned int ibt_legacy = 0, shstk_legacy = 0; - bool found_ibt_legacy = false, found_shstk_legacy = false; - - /* Check if IBT and SHSTK are enabled in object. */ - bool enable_ibt = (ibt_enabled - && enable_ibt_type != cet_always_off); - bool enable_shstk = (shstk_enabled - && enable_shstk_type != cet_always_off); - if (program) +#ifdef SHARED + /* Skip check for ld.so since it has the features enabled. The + features will be disabled later if they are not enabled in + executable. */ + if (l == &GL(dl_rtld_map) + || l->l_real == &GL(dl_rtld_map) + || (info->program != NULL && l == m)) + continue; +#endif + + /* IBT and SHSTK set only if enabled in executable and all DSOs. + NB: cet_always_on is handled outside of the loop. */ + info->enable_feature_1 &= ((l->l_x86_feature_1_and + & (GNU_PROPERTY_X86_FEATURE_1_IBT + | GNU_PROPERTY_X86_FEATURE_1_SHSTK)) + | ~(GNU_PROPERTY_X86_FEATURE_1_IBT + | GNU_PROPERTY_X86_FEATURE_1_SHSTK)); + if ((info->feature_1_legacy + & GNU_PROPERTY_X86_FEATURE_1_IBT) == 0 + && ((info->enable_feature_1 + & GNU_PROPERTY_X86_FEATURE_1_IBT) + != (info->feature_1_enabled + & GNU_PROPERTY_X86_FEATURE_1_IBT))) { - /* Enable IBT and SHSTK only if they are enabled in executable. - NB: IBT and SHSTK may be disabled by environment variable: - - GLIBC_TUNABLES=glibc.cpu.hwcaps=-IBT,-SHSTK - */ - enable_ibt &= (CPU_FEATURE_USABLE (IBT) - && (enable_ibt_type == cet_always_on - || (m->l_x86_feature_1_and - & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0)); - enable_shstk &= (CPU_FEATURE_USABLE (SHSTK) - && (enable_shstk_type == cet_always_on - || (m->l_x86_feature_1_and - & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0)); + info->feature_1_legacy_ibt = i; + info->feature_1_legacy |= GNU_PROPERTY_X86_FEATURE_1_IBT; } - /* ld.so is CET-enabled by kernel. But shared objects may not - support IBT nor SHSTK. */ - if (enable_ibt || enable_shstk) - { - unsigned int i; + if ((info->feature_1_legacy + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) == 0 + && ((info->enable_feature_1 + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) + != (info->feature_1_enabled + & GNU_PROPERTY_X86_FEATURE_1_SHSTK))) + { + info->feature_1_legacy_shstk = i; + info->feature_1_legacy |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; + } + } - i = m->l_searchlist.r_nlist; - while (i-- > 0) - { - /* Check each shared object to see if IBT and SHSTK are - enabled. */ - l = m->l_initfini[i]; + /* Handle cet_always_on. */ + if ((info->feature_1_enabled + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0 + && info->enable_ibt_type == cet_always_on) + { + info->feature_1_legacy &= ~GNU_PROPERTY_X86_FEATURE_1_IBT; + info->enable_feature_1 |= GNU_PROPERTY_X86_FEATURE_1_IBT; + } - if (l->l_init_called) - continue; + if ((info->feature_1_enabled + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0 + && info->enable_shstk_type == cet_always_on) + { + info->feature_1_legacy &= ~GNU_PROPERTY_X86_FEATURE_1_SHSTK; + info->enable_feature_1 |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; + } +} #ifdef SHARED - /* Skip CET check for ld.so since ld.so is CET-enabled. - CET will be disabled later if CET isn't enabled in - executable. */ - if (l == &GL(dl_rtld_map) - || l->l_real == &GL(dl_rtld_map) - || (program && l == m)) - continue; +/* Enable IBT and SHSTK only if they are enabled in executable. Set + feature bits properly at the start of the program. */ + +static void +dl_cet_check_startup (struct link_map *m, struct dl_cet_info *info) +{ + /* NB: IBT and SHSTK may be disabled by environment variable: + + GLIBC_TUNABLES=glibc.cpu.hwcaps=-IBT,-SHSTK. + */ + if (CPU_FEATURE_USABLE (IBT)) + { + if (info->enable_ibt_type == cet_always_on) + info->enable_feature_1 |= GNU_PROPERTY_X86_FEATURE_1_IBT; + else + info->enable_feature_1 &= ((m->l_x86_feature_1_and + & GNU_PROPERTY_X86_FEATURE_1_IBT) + | ~GNU_PROPERTY_X86_FEATURE_1_IBT); + } + else + info->enable_feature_1 &= ~GNU_PROPERTY_X86_FEATURE_1_IBT; + + if (CPU_FEATURE_USABLE (SHSTK)) + { + if (info->enable_shstk_type == cet_always_on) + info->enable_feature_1 |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; + else + info->enable_feature_1 &= ((m->l_x86_feature_1_and + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) + | ~GNU_PROPERTY_X86_FEATURE_1_SHSTK); + } + else + info->enable_feature_1 &= ~GNU_PROPERTY_X86_FEATURE_1_SHSTK; + + if (info->enable_feature_1 != 0) + dl_check_legacy_object (m, info); + + unsigned int disable_feature_1 + = info->enable_feature_1 ^ info->feature_1_enabled; + if (disable_feature_1 != 0) + { + /* Disable features in the kernel because of legacy objects or + cet_always_off. */ + if (dl_cet_disable_cet (disable_feature_1) != 0) + _dl_fatal_printf ("%s: can't disable x86 Features\n", + info->program); + + /* Clear the disabled bits. Sync dl_x86_feature_1 and + info->feature_1_enabled with info->enable_feature_1. */ + info->feature_1_enabled = info->enable_feature_1; + GL(dl_x86_feature_1) = info->enable_feature_1; + } + + if (HAS_CPU_FEATURE (IBT) || HAS_CPU_FEATURE (SHSTK)) + { + /* Lock CET features only if IBT or SHSTK are enabled and are not + enabled permissively. */ + unsigned int feature_1_lock = 0; + + if (((info->feature_1_enabled & GNU_PROPERTY_X86_FEATURE_1_IBT) + != 0) + && info->enable_ibt_type != cet_permissive) + feature_1_lock |= GNU_PROPERTY_X86_FEATURE_1_IBT; + + if (((info->feature_1_enabled & GNU_PROPERTY_X86_FEATURE_1_SHSTK) + != 0) + && info->enable_shstk_type != cet_permissive) + feature_1_lock |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; + + if (feature_1_lock != 0 + && dl_cet_lock_cet () != 0) + _dl_fatal_printf ("%s: can't lock CET\n", info->program); + } + + THREAD_SETMEM (THREAD_SELF, header.feature_1, GL(dl_x86_feature_1)); +} #endif - /* IBT is enabled only if it is enabled in executable as - well as all shared objects. */ - enable_ibt &= (enable_ibt_type == cet_always_on - || (l->l_x86_feature_1_and - & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0); - if (!found_ibt_legacy && enable_ibt != ibt_enabled) - { - found_ibt_legacy = true; - ibt_legacy = i; - } - - /* SHSTK is enabled only if it is enabled in executable as - well as all shared objects. */ - enable_shstk &= (enable_shstk_type == cet_always_on - || (l->l_x86_feature_1_and - & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0); - if (enable_shstk != shstk_enabled) - { - found_shstk_legacy = true; - shstk_legacy = i; - } - } - } +/* Check feature bits when dlopening the shared object M. */ + +static void +dl_cet_check_dlopen (struct link_map *m, struct dl_cet_info *info) +{ + /* Check if there are any legacy objects loaded. */ + if (info->enable_feature_1 != 0) + { + dl_check_legacy_object (m, info); - bool cet_feature_changed = false; + /* Skip if there are no legacy shared objects loaded. */ + if (info->feature_1_legacy == 0) + return; + } - if (enable_ibt != ibt_enabled || enable_shstk != shstk_enabled) - { - if (!program) - { - if (enable_ibt_type != cet_permissive) - { - /* When IBT is enabled, we cannot dlopen a shared - object without IBT. */ - if (found_ibt_legacy) - _dl_signal_error (0, - m->l_initfini[ibt_legacy]->l_name, - "dlopen", - N_("rebuild shared object with IBT support enabled")); - } - - if (enable_shstk_type != cet_permissive) - { - /* When SHSTK is enabled, we cannot dlopen a shared - object without SHSTK. */ - if (found_shstk_legacy) - _dl_signal_error (0, - m->l_initfini[shstk_legacy]->l_name, - "dlopen", - N_("rebuild shared object with SHSTK support enabled")); - } - - if (enable_ibt_type != cet_permissive - && enable_shstk_type != cet_permissive) - return; - } - - /* Disable IBT and/or SHSTK if they are enabled by kernel, but - disabled in executable or shared objects. */ - unsigned int cet_feature = 0; - - if (!enable_ibt) - cet_feature |= GNU_PROPERTY_X86_FEATURE_1_IBT; - if (!enable_shstk) - cet_feature |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; - - int res = dl_cet_disable_cet (cet_feature); - if (res != 0) - { - if (program) - _dl_fatal_printf ("%s: can't disable CET\n", program); - else - { - if (found_ibt_legacy) - l = m->l_initfini[ibt_legacy]; - else - l = m->l_initfini[shstk_legacy]; - _dl_signal_error (-res, l->l_name, "dlopen", - N_("can't disable CET")); - } - } - - /* Clear the disabled bits in dl_x86_feature_1. */ - GL(dl_x86_feature_1) &= ~cet_feature; - - cet_feature_changed = true; - } + unsigned int disable_feature_1 = 0; + unsigned int legacy_obj = 0; + const char *msg = NULL; -#ifdef SHARED - if (program && (ibt_enabled || shstk_enabled)) + if ((info->feature_1_enabled + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0 + && (info->feature_1_legacy + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0) + { + if (info->enable_ibt_type != cet_permissive) { - if ((!ibt_enabled - || enable_ibt_type != cet_permissive) - && (!shstk_enabled - || enable_shstk_type != cet_permissive)) - { - /* Lock CET if IBT or SHSTK is enabled in executable unless - IBT or SHSTK is enabled permissively. */ - int res = dl_cet_lock_cet (); - if (res != 0) - _dl_fatal_printf ("%s: can't lock CET\n", program); - } - - /* Set feature_1 if IBT or SHSTK is enabled in executable. */ - cet_feature_changed = true; + legacy_obj = info->feature_1_legacy_ibt; + msg = N_("rebuild shared object with IBT support enabled"); } -#endif + else + disable_feature_1 |= GNU_PROPERTY_X86_FEATURE_1_IBT; + } - if (cet_feature_changed) + /* Check the next feature only if there is no error. */ + if (msg == NULL + && (info->feature_1_enabled + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0 + && (info->feature_1_legacy + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0) + { + if (info->enable_shstk_type != cet_permissive) { - unsigned int feature_1 = 0; - if (enable_ibt) - feature_1 |= GNU_PROPERTY_X86_FEATURE_1_IBT; - if (enable_shstk) - feature_1 |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; - struct pthread *self = THREAD_SELF; - THREAD_SETMEM (self, header.feature_1, feature_1); + legacy_obj = info->feature_1_legacy_shstk; + msg = N_("rebuild shared object with SHSTK support enabled"); } + else + disable_feature_1 |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; + } + + /* If there is an error, long jump back to the caller. */ + if (msg != NULL) + _dl_signal_error (0, m->l_initfini[legacy_obj]->l_name, "dlopen", + msg); + + if (disable_feature_1 != 0) + { + int res = dl_cet_disable_cet (disable_feature_1); + if (res) + { + if ((disable_feature_1 + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0) + msg = N_("can't disable IBT"); + else + msg = N_("can't disable SHSTK"); + /* Long jump back to the caller on error. */ + _dl_signal_error (-res, m->l_initfini[legacy_obj]->l_name, + "dlopen", msg); + } + + /* Clear the disabled bits in dl_x86_feature_1. */ + GL(dl_x86_feature_1) &= ~disable_feature_1; + + THREAD_SETMEM (THREAD_SELF, header.feature_1, + GL(dl_x86_feature_1)); + } +} + +static void +dl_cet_check (struct link_map *m, const char *program) +{ + struct dl_cet_info info; + + /* Check how IBT and SHSTK should be enabled. */ + info.enable_ibt_type = GL(dl_x86_feature_control).ibt; + info.enable_shstk_type = GL(dl_x86_feature_control).shstk; + + info.feature_1_enabled = GL(dl_x86_feature_1); + + /* No legacy object check if IBT and SHSTK are always on. */ + if (info.enable_ibt_type == cet_always_on + && info.enable_shstk_type == cet_always_on) + { +#ifdef SHARED + /* Set it only during startup. */ + if (program != NULL) + THREAD_SETMEM (THREAD_SELF, header.feature_1, + info.feature_1_enabled); +#endif + return; } + + /* Check if IBT and SHSTK were enabled by kernel. */ + if (info.feature_1_enabled == 0) + return; + + info.program = program; + + /* Check which features should be enabled. */ + info.enable_feature_1 = 0; + if (info.enable_ibt_type != cet_always_off) + info.enable_feature_1 |= (info.feature_1_enabled + & GNU_PROPERTY_X86_FEATURE_1_IBT); + if (info.enable_shstk_type != cet_always_off) + info.enable_feature_1 |= (info.feature_1_enabled + & GNU_PROPERTY_X86_FEATURE_1_SHSTK); + + /* Start with no legacy objects. */ + info.feature_1_legacy = 0; + info.feature_1_legacy_ibt = 0; + info.feature_1_legacy_shstk = 0; + +#ifdef SHARED + if (program) + dl_cet_check_startup (m, &info); + else +#endif + dl_cet_check_dlopen (m, &info); } void From patchwork Wed Dec 6 17:20:04 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: 1872912 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=eIjVESMd; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SlkkK28Z6z23nD for ; Thu, 7 Dec 2023 04:22:48 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8386A38618F2 for ; Wed, 6 Dec 2023 17:22:42 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id 617E83857348 for ; Wed, 6 Dec 2023 17:20:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 617E83857348 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 617E83857348 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::434 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883230; cv=none; b=QUhdazVVzxdAgm2CywM1qRhs0Jv2wcrqrxYhlg1l0fLNgihYfTYv20ENAA99nj2KxZqBRvMCGqGd/37O7DCwnk/6DMUoH31h+nolqmmI+SA2gHZ6smyPxqm3kWx44ZQFXO9XUV8IAXSgjvFrIwvwhxD6bnl/JgkW7/JP0E6cWcE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883230; c=relaxed/simple; bh=Prrvjb8kmW2A51gy1dFAv2MKCBcmd0YJWNL1BqGNMwg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=NL2Y5P20rdXO29pvkh+7zC/2lpZixsv3IxWxHjA2Leym8+8siksgxMufXvhHqP6Dt5O7q7/bOb1NB6q7IlV8uErfHPmnEtr2TgaG46FgvD4GaY7fenTrZJ0bikE3NYL8mBDueFHw5nt11mwNeovmq5jjcR0lHORJL75h4pVaRkQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-6ce95e387e5so50663b3a.2 for ; Wed, 06 Dec 2023 09:20:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883219; x=1702488019; 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=e+OrhT6SkonBv0/abUz9lMci6rDAzaBPDOdKiNyLCIY=; b=eIjVESMdQrncteh/NM4kbMSfJem40ksE/6urcW6oGH+R+/rjvBWQEjHhAjcPKcU/43 uBMYAi2nz7Zt+HzDUqOxzqJC/wU1GXRuK42YpW48e1qj8NqImke868A7amb9rLZBcMR+ 6ISQ9FRPVvEW+ggld65m5USyZaQQ1e/GeHIdzzbgnJJOrdBOPHXfWX1jrF7guGV+xvlJ z8bnDS7wQ9pnD4Gz30C1GgPdcVoZBpBISBdH9kEpLyCd4yt3opAerH/WNQ7/z2gaL9Es fwD/rAFU8fbAquIgMkNHmWB1ScXDYN/+V7LNmEWo6z804rrRMmAfFS15vxS7aMBbUx23 Q4wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883219; x=1702488019; 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=e+OrhT6SkonBv0/abUz9lMci6rDAzaBPDOdKiNyLCIY=; b=e8MlI2xBoDhjo74wErA769kZSxSbXH32IzPJxgqwtdG01ByzfbnDGkZTlNFCg8qRrI 9l0+g7jy8iPNkTCzFHSUawb9aW+VguJaIpvlJLvDW7W0Zj4fch/+QCFRJlhlAQyAgEVg vKJG8SWgI8Ll1IJ5vtypjsliOwy/l4OHJp9+DNJ3fCo+XM31bDETms9CTRq/reRF8x7J 1+S2Sb+uzJuGR52DVVtXZJnYp1pcrYO5O/s8ZOIuW4TitFK5hCJYQBtIgo6rjX3z8s7d 9WKUUs9gVbBFgszuZKHZxBwcnr9BdynKMdOX17LogrQFttD8G9PXsnuLb/cQ72QE8y0R fWhA== X-Gm-Message-State: AOJu0YzskTgzw6lc4RgEu1XTU4ebwL+9SOvbS1zoKVPpaT6ITdAv6fuC P5+U/yQjB6XCi6y6IrYr+PiY/hbTARc= X-Google-Smtp-Source: AGHT+IHzvBW6TceVSI56m1Qwqf7QpMI8Ava6g4OmOxgYJ3vqcEof4DSopW0R4X1Z1qj+Xh4jmJuIUw== X-Received: by 2002:a05:6a20:1448:b0:18f:97c:926d with SMTP id a8-20020a056a20144800b0018f097c926dmr1079812pzi.82.1701883219217; Wed, 06 Dec 2023 09:20:19 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id l187-20020a633ec4000000b005c67bb1585csm151271pga.68.2023.12.06.09.20.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:16 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id C97B4740634; 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 11/17] x86/cet: Sync with Linux kernel 6.6 shadow stack interface Date: Wed, 6 Dec 2023 09:20:04 -0800 Message-ID: <20231206172010.1023415-12-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=-3025.0 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+incoming=patchwork.ozlabs.org@sourceware.org Sync with Linux kernel 6.6 shadow stack interface. Since only x86-64 is supported, i386 shadow stack codes are unchanged and CET shouldn't be enabled for i386. 1. When the shadow stack base in TCB is unset, the default shadow stack is in use. Use the current shadow stack pointer as the marker for the default shadow stack. 2. Allocate shadow stack with the map_shadow_stack syscall. 3. Rename arch_prctl CET commands to ARCH_SHSTK_XXX. 4. Rewrite the CET control functions with the current kernel shadow stack interface. Since CET is no longer enabled by kernel, a separate patch will enable shadow stack during startup. --- sysdeps/unix/sysv/linux/x86/Makefile | 1 + .../sysv/linux/x86/allocate-shadow-stack.c | 54 +++++++++++++++++++ .../sysv/linux/x86/allocate-shadow-stack.h | 27 ++++++++++ sysdeps/unix/sysv/linux/x86/bits/mman.h | 5 ++ sysdeps/unix/sysv/linux/x86/cpu-features.c | 13 +++-- sysdeps/unix/sysv/linux/x86/dl-cet.h | 16 ++++-- .../unix/sysv/linux/x86/include/asm/prctl.h | 37 ++++++------- .../sysv/linux/x86/tst-cet-setcontext-1.c | 17 +++--- .../unix/sysv/linux/x86_64/__start_context.S | 38 +++---------- sysdeps/unix/sysv/linux/x86_64/getcontext.S | 30 ++--------- sysdeps/unix/sysv/linux/x86_64/makecontext.c | 29 +++++----- sysdeps/unix/sysv/linux/x86_64/swapcontext.S | 22 ++------ sysdeps/x86/cpu-features.c | 15 ++++-- sysdeps/x86/dl-cet.c | 2 +- sysdeps/x86_64/nptl/tls.h | 2 +- 15 files changed, 176 insertions(+), 132 deletions(-) create mode 100644 sysdeps/unix/sysv/linux/x86/allocate-shadow-stack.c create mode 100644 sysdeps/unix/sysv/linux/x86/allocate-shadow-stack.h diff --git a/sysdeps/unix/sysv/linux/x86/Makefile b/sysdeps/unix/sysv/linux/x86/Makefile index 9dfdd689a9..ed0d6500b9 100644 --- a/sysdeps/unix/sysv/linux/x86/Makefile +++ b/sysdeps/unix/sysv/linux/x86/Makefile @@ -44,6 +44,7 @@ CFLAGS-tst-cet-vfork-1.c += -mshstk endif ifeq ($(subdir),stdlib) +sysdep_routines += allocate-shadow-stack tests += tst-cet-setcontext-1 CFLAGS-tst-cet-setcontext-1.c += -mshstk endif diff --git a/sysdeps/unix/sysv/linux/x86/allocate-shadow-stack.c b/sysdeps/unix/sysv/linux/x86/allocate-shadow-stack.c new file mode 100644 index 0000000000..3a76db1a60 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86/allocate-shadow-stack.c @@ -0,0 +1,54 @@ +/* Helper function to allocate shadow stack. + 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 +#include +#include + +/* NB: This can be treated as a syscall by caller. */ + +#ifndef __x86_64__ +__attribute__ ((regparm (2))) +#endif +long int +__allocate_shadow_stack (size_t stack_size, + shadow_stack_size_t *child_stack) +{ +#ifdef __NR_map_shadow_stack + size_t shadow_stack_size + = stack_size >> STACK_SIZE_TO_SHADOW_STACK_SIZE_SHIFT; + /* Align shadow stack to 8 bytes. */ + shadow_stack_size = ALIGN_UP (shadow_stack_size, 8); + void *shadow_stack = (void *)INLINE_SYSCALL_CALL + (map_shadow_stack, NULL, shadow_stack_size, SHADOW_STACK_SET_TOKEN); + /* Report the map_shadow_stack error. */ + if (shadow_stack == MAP_FAILED) + return -errno; + + /* Save the shadow stack base and size on child stack. */ + child_stack[0] = (uintptr_t) shadow_stack; + child_stack[1] = shadow_stack_size; + + return 0; +#else + return -ENOSYS; +#endif +} diff --git a/sysdeps/unix/sysv/linux/x86/allocate-shadow-stack.h b/sysdeps/unix/sysv/linux/x86/allocate-shadow-stack.h new file mode 100644 index 0000000000..834373e0d3 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86/allocate-shadow-stack.h @@ -0,0 +1,27 @@ +/* Helper function to allocate shadow stack. + 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 + +typedef __typeof (((ucontext_t *) 0)->__ssp[0]) shadow_stack_size_t; + +extern long int __allocate_shadow_stack (size_t, shadow_stack_size_t *) +#ifndef __x86_64__ + __attribute__ ((regparm (2))) +#endif + attribute_hidden; diff --git a/sysdeps/unix/sysv/linux/x86/bits/mman.h b/sysdeps/unix/sysv/linux/x86/bits/mman.h index 3d356e86a0..221f7c82bd 100644 --- a/sysdeps/unix/sysv/linux/x86/bits/mman.h +++ b/sysdeps/unix/sysv/linux/x86/bits/mman.h @@ -27,6 +27,11 @@ #define MAP_32BIT 0x40 /* Only give out 32-bit addresses. */ #define MAP_ABOVE4G 0x80 /* Only map above 4GB. */ +#ifdef __USE_MISC +/* Set up a restore token in the newly allocatd shadow stack */ +# define SHADOW_STACK_SET_TOKEN 0x1 +#endif + #include /* Include generic Linux declarations. */ diff --git a/sysdeps/unix/sysv/linux/x86/cpu-features.c b/sysdeps/unix/sysv/linux/x86/cpu-features.c index 41e7600668..0e6e2bf855 100644 --- a/sysdeps/unix/sysv/linux/x86/cpu-features.c +++ b/sysdeps/unix/sysv/linux/x86/cpu-features.c @@ -23,10 +23,15 @@ static inline int __attribute__ ((always_inline)) get_cet_status (void) { - unsigned long long cet_status[3]; - if (INTERNAL_SYSCALL_CALL (arch_prctl, ARCH_CET_STATUS, cet_status) == 0) - return cet_status[0]; - return 0; + unsigned long long kernel_feature; + unsigned int status = 0; + if (INTERNAL_SYSCALL_CALL (arch_prctl, ARCH_SHSTK_STATUS, + &kernel_feature) == 0) + { + if ((kernel_feature & ARCH_SHSTK_SHSTK) != 0) + status = GNU_PROPERTY_X86_FEATURE_1_SHSTK; + } + return status; } # ifndef SHARED diff --git a/sysdeps/unix/sysv/linux/x86/dl-cet.h b/sysdeps/unix/sysv/linux/x86/dl-cet.h index c885bf1323..da220ac627 100644 --- a/sysdeps/unix/sysv/linux/x86/dl-cet.h +++ b/sysdeps/unix/sysv/linux/x86/dl-cet.h @@ -21,12 +21,20 @@ static inline int __attribute__ ((always_inline)) dl_cet_disable_cet (unsigned int cet_feature) { - return (int) INTERNAL_SYSCALL_CALL (arch_prctl, ARCH_CET_DISABLE, - cet_feature); + if (cet_feature != GNU_PROPERTY_X86_FEATURE_1_SHSTK) + return -1; + long long int kernel_feature = ARCH_SHSTK_SHSTK; + return (int) INTERNAL_SYSCALL_CALL (arch_prctl, ARCH_SHSTK_DISABLE, + kernel_feature); } static inline int __attribute__ ((always_inline)) -dl_cet_lock_cet (void) +dl_cet_lock_cet (unsigned int cet_feature) { - return (int) INTERNAL_SYSCALL_CALL (arch_prctl, ARCH_CET_LOCK, 0); + if (cet_feature != GNU_PROPERTY_X86_FEATURE_1_SHSTK) + return -1; + /* Lock all SHSTK features. */ + long long int kernel_feature = -1; + return (int) INTERNAL_SYSCALL_CALL (arch_prctl, ARCH_SHSTK_LOCK, + kernel_feature); } diff --git a/sysdeps/unix/sysv/linux/x86/include/asm/prctl.h b/sysdeps/unix/sysv/linux/x86/include/asm/prctl.h index 45ad0b052f..2f511321ad 100644 --- a/sysdeps/unix/sysv/linux/x86/include/asm/prctl.h +++ b/sysdeps/unix/sysv/linux/x86/include/asm/prctl.h @@ -4,24 +4,19 @@ #include_next -#ifndef ARCH_CET_STATUS -/* CET features: - IBT: GNU_PROPERTY_X86_FEATURE_1_IBT - SHSTK: GNU_PROPERTY_X86_FEATURE_1_SHSTK - */ -/* Return CET features in unsigned long long *addr: - features: addr[0]. - shadow stack base address: addr[1]. - shadow stack size: addr[2]. - */ -# define ARCH_CET_STATUS 0x3001 -/* Disable CET features in unsigned int features. */ -# define ARCH_CET_DISABLE 0x3002 -/* Lock all CET features. */ -# define ARCH_CET_LOCK 0x3003 -/* Allocate a new shadow stack with unsigned long long *addr: - IN: requested shadow stack size: *addr. - OUT: allocated shadow stack address: *addr. - */ -# define ARCH_CET_ALLOC_SHSTK 0x3004 -#endif /* ARCH_CET_STATUS */ +#ifndef ARCH_SHSTK_ENABLE +/* Enable SHSTK features in unsigned long int features. */ +# define ARCH_SHSTK_ENABLE 0x5001 +/* Disable SHSTK features in unsigned long int features. */ +# define ARCH_SHSTK_DISABLE 0x5002 +/* Lock SHSTK features in unsigned long int features. */ +# define ARCH_SHSTK_LOCK 0x5003 +/* Unlock SHSTK features in unsigned long int features. */ +# define ARCH_SHSTK_UNLOCK 0x5004 +/* Return SHSTK features in unsigned long int features. */ +# define ARCH_SHSTK_STATUS 0x5005 + +/* ARCH_SHSTK_ features bits */ +# define ARCH_SHSTK_SHSTK 0x1 +# define ARCH_SHSTK_WRSS 0x2 +#endif diff --git a/sysdeps/unix/sysv/linux/x86/tst-cet-setcontext-1.c b/sysdeps/unix/sysv/linux/x86/tst-cet-setcontext-1.c index 837a9fd0eb..2ea66c803b 100644 --- a/sysdeps/unix/sysv/linux/x86/tst-cet-setcontext-1.c +++ b/sysdeps/unix/sysv/linux/x86/tst-cet-setcontext-1.c @@ -87,15 +87,14 @@ do_test (void) ctx[4].uc_link = &ctx[0]; makecontext (&ctx[4], (void (*) (void)) f1, 0); - /* NB: When shadow stack is enabled, makecontext calls arch_prctl - with ARCH_CET_ALLOC_SHSTK to allocate a new shadow stack which - can be unmapped. The base address and size of the new shadow - stack are returned in __ssp[1] and __ssp[2]. makecontext is - called for CTX1, CTX3 and CTX4. But only CTX1 is used. New - shadow stacks are allocated in the order of CTX3, CTX1, CTX4. - It is very likely that CTX1's shadow stack is placed between - CTX3 and CTX4. We munmap CTX3's and CTX4's shadow stacks to - create gaps above and below CTX1's shadow stack. We check + /* NB: When shadow stack is enabled, makecontext calls map_shadow_stack + to allocate a new shadow stack which can be unmapped. The base + address and size of the new shadow stack are returned in __ssp[1] + and __ssp[2]. makecontext is called for CTX1, CTX3 and CTX4. But + only CTX1 is used. New shadow stacks are allocated in the order + of CTX3, CTX1, CTX4. It is very likely that CTX1's shadow stack is + placed between CTX3 and CTX4. We munmap CTX3's and CTX4's shadow + stacks to create gaps above and below CTX1's shadow stack. We check that setcontext CTX1 works correctly in this case. */ if (_get_ssp () != 0) { diff --git a/sysdeps/unix/sysv/linux/x86_64/__start_context.S b/sysdeps/unix/sysv/linux/x86_64/__start_context.S index f6436dd6bb..ae04203c90 100644 --- a/sysdeps/unix/sysv/linux/x86_64/__start_context.S +++ b/sysdeps/unix/sysv/linux/x86_64/__start_context.S @@ -24,20 +24,14 @@ /* Use CALL to push __start_context onto the new stack as well as the new shadow stack. RDI points to ucontext: Incoming: - __ssp[0]: The original caller's shadow stack pointer. - __ssp[1]: The size of the new shadow stack. - __ssp[2]: The size of the new shadow stack. - Outgoing: __ssp[0]: The new shadow stack pointer. __ssp[1]: The base address of the new shadow stack. __ssp[2]: The size of the new shadow stack. */ ENTRY(__push___start_context) - /* Save the pointer to ucontext. */ - movq %rdi, %r9 /* Get the original shadow stack pointer. */ - rdsspq %r8 + rdsspq %rcx /* Save the original stack pointer. */ movq %rsp, %rdx /* Load the top of the new stack into RSI. */ @@ -45,24 +39,12 @@ ENTRY(__push___start_context) /* Add 8 bytes to RSI since CALL will push the 8-byte return address onto stack. */ leaq 8(%rsi), %rsp - /* Allocate the new shadow stack. The size of the new shadow - stack is passed in __ssp[1]. */ - lea (oSSP + 8)(%rdi), %RSI_LP - movl $ARCH_CET_ALLOC_SHSTK, %edi - movl $__NR_arch_prctl, %eax - /* The new shadow stack base is returned in __ssp[1]. */ - syscall - testq %rax, %rax - jne L(hlt) /* This should never happen. */ - - /* Get the size of the new shadow stack. */ - movq 8(%rsi), %rdi - - /* Get the base address of the new shadow stack. */ - movq (%rsi), %rsi - + /* The size of the new shadow stack is stored in __ssp[2]. */ + mov (oSSP + 16)(%rdi), %RSI_LP + /* The new shadow stack base is stored in __ssp[1]. */ + mov (oSSP + 8)(%rdi), %RAX_LP /* Use the restore stoken to restore the new shadow stack. */ - rstorssp -8(%rsi, %rdi) + rstorssp -8(%rax, %rsi) /* Save the restore token on the original shadow stack. */ saveprevssp @@ -73,18 +55,12 @@ ENTRY(__push___start_context) jmp __start_context 1: - /* Get the new shadow stack pointer. */ - rdsspq %rdi - /* Use the restore stoken to restore the original shadow stack. */ - rstorssp -8(%r8) + rstorssp -8(%rcx) /* Save the restore token on the new shadow stack. */ saveprevssp - /* Store the new shadow stack pointer in __ssp[0]. */ - movq %rdi, oSSP(%r9) - /* Restore the original stack. */ mov %rdx, %rsp ret diff --git a/sysdeps/unix/sysv/linux/x86_64/getcontext.S b/sysdeps/unix/sysv/linux/x86_64/getcontext.S index a00e2f6290..71f3802dca 100644 --- a/sysdeps/unix/sysv/linux/x86_64/getcontext.S +++ b/sysdeps/unix/sysv/linux/x86_64/getcontext.S @@ -58,35 +58,15 @@ ENTRY(__getcontext) testl $X86_FEATURE_1_SHSTK, %fs:FEATURE_1_OFFSET jz L(no_shstk) - /* Save RDI in RDX which won't be clobbered by syscall. */ - movq %rdi, %rdx - xorl %eax, %eax cmpq %fs:SSP_BASE_OFFSET, %rax jnz L(shadow_stack_bound_recorded) - /* Get the base address and size of the default shadow stack - which must be the current shadow stack since nothing has - been recorded yet. */ - sub $24, %RSP_LP - mov %RSP_LP, %RSI_LP - movl $ARCH_CET_STATUS, %edi - movl $__NR_arch_prctl, %eax - syscall - testq %rax, %rax - jz L(continue_no_err) - - /* This should never happen. */ - hlt - -L(continue_no_err): - /* Record the base of the current shadow stack. */ - movq 8(%rsp), %rax + /* When the shadow stack base is unset, the default shadow + stack is in use. Use the current shadow stack pointer + as the marker for the default shadow stack. */ + rdsspq %rax movq %rax, %fs:SSP_BASE_OFFSET - add $24, %RSP_LP - - /* Restore RDI. */ - movq %rdx, %rdi L(shadow_stack_bound_recorded): /* Get the current shadow stack pointer. */ @@ -94,7 +74,7 @@ L(shadow_stack_bound_recorded): /* NB: Save the caller's shadow stack so that we can jump back to the caller directly. */ addq $8, %rax - movq %rax, oSSP(%rdx) + movq %rax, oSSP(%rdi) /* Save the current shadow stack base in ucontext. */ movq %fs:SSP_BASE_OFFSET, %rax diff --git a/sysdeps/unix/sysv/linux/x86_64/makecontext.c b/sysdeps/unix/sysv/linux/x86_64/makecontext.c index de9e03eb81..788b730132 100644 --- a/sysdeps/unix/sysv/linux/x86_64/makecontext.c +++ b/sysdeps/unix/sysv/linux/x86_64/makecontext.c @@ -24,6 +24,8 @@ # include # include # include +# include +# include #endif #include "ucontext_i.h" @@ -88,23 +90,24 @@ __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...) if ((feature_1 & X86_FEATURE_1_SHSTK) != 0) { /* Shadow stack is enabled. We need to allocate a new shadow - stack. */ - unsigned long ssp_size = (((uintptr_t) sp - - (uintptr_t) ucp->uc_stack.ss_sp) - >> STACK_SIZE_TO_SHADOW_STACK_SIZE_SHIFT); - /* Align shadow stack to 8 bytes. */ - ssp_size = ALIGN_UP (ssp_size, 8); - - ucp->__ssp[1] = ssp_size; - ucp->__ssp[2] = ssp_size; - - /* Call __push___start_context to allocate a new shadow stack, - push __start_context onto the new stack as well as the new - shadow stack. NB: After __push___start_context returns, + stack. NB: ucp->__ssp[0]: The new shadow stack pointer. ucp->__ssp[1]: The base address of the new shadow stack. ucp->__ssp[2]: The size of the new shadow stack. */ + long int ret + = __allocate_shadow_stack (((uintptr_t) sp + - (uintptr_t) ucp->uc_stack.ss_sp), + &ucp->__ssp[1]); + if (ret != 0) + { + /* FIXME: What should we do? */ + abort (); + } + + ucp->__ssp[0] = ucp->__ssp[1] + ucp->__ssp[2] - 8; + /* Call __push___start_context to push __start_context onto the new + stack as well as the new shadow stack. */ __push___start_context (ucp); } else diff --git a/sysdeps/unix/sysv/linux/x86_64/swapcontext.S b/sysdeps/unix/sysv/linux/x86_64/swapcontext.S index 5925752164..2f2fe9875b 100644 --- a/sysdeps/unix/sysv/linux/x86_64/swapcontext.S +++ b/sysdeps/unix/sysv/linux/x86_64/swapcontext.S @@ -109,25 +109,11 @@ ENTRY(__swapcontext) cmpq %fs:SSP_BASE_OFFSET, %rax jnz L(shadow_stack_bound_recorded) - /* Get the base address and size of the default shadow stack - which must be the current shadow stack since nothing has - been recorded yet. */ - sub $24, %RSP_LP - mov %RSP_LP, %RSI_LP - movl $ARCH_CET_STATUS, %edi - movl $__NR_arch_prctl, %eax - syscall - testq %rax, %rax - jz L(continue_no_err) - - /* This should never happen. */ - hlt - -L(continue_no_err): - /* Record the base of the current shadow stack. */ - movq 8(%rsp), %rax + /* When the shadow stack base is unset, the default shadow + stack is in use. Use the current shadow stack pointer + as the marker for the default shadow stack. */ + rdsspq %rax movq %rax, %fs:SSP_BASE_OFFSET - add $24, %RSP_LP L(shadow_stack_bound_recorded): /* If we unwind the stack, we can't undo stack unwinding. Just diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c index 0bf923d48b..f180f0d9a4 100644 --- a/sysdeps/x86/cpu-features.c +++ b/sysdeps/x86/cpu-features.c @@ -1121,8 +1121,9 @@ no_cpuid: # ifndef SHARED /* Check if IBT and SHSTK are enabled by kernel. */ - if ((cet_status & GNU_PROPERTY_X86_FEATURE_1_IBT) - || (cet_status & GNU_PROPERTY_X86_FEATURE_1_SHSTK)) + if ((cet_status + & (GNU_PROPERTY_X86_FEATURE_1_IBT + | GNU_PROPERTY_X86_FEATURE_1_SHSTK))) { /* Disable IBT and/or SHSTK if they are enabled by kernel, but disabled by environment variable: @@ -1131,9 +1132,11 @@ no_cpuid: */ unsigned int cet_feature = 0; if (!CPU_FEATURE_USABLE (IBT)) - cet_feature |= GNU_PROPERTY_X86_FEATURE_1_IBT; + cet_feature |= (cet_status + & GNU_PROPERTY_X86_FEATURE_1_IBT); if (!CPU_FEATURE_USABLE (SHSTK)) - cet_feature |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; + cet_feature |= (cet_status + & GNU_PROPERTY_X86_FEATURE_1_SHSTK); if (cet_feature) { @@ -1148,7 +1151,9 @@ no_cpuid: lock CET if IBT or SHSTK is enabled permissively. */ if (GL(dl_x86_feature_control).ibt != cet_permissive && GL(dl_x86_feature_control).shstk != cet_permissive) - dl_cet_lock_cet (); + dl_cet_lock_cet (GL(dl_x86_feature_1) + & (GNU_PROPERTY_X86_FEATURE_1_IBT + | GNU_PROPERTY_X86_FEATURE_1_SHSTK)); } # endif } diff --git a/sysdeps/x86/dl-cet.c b/sysdeps/x86/dl-cet.c index 67c51ee8c2..8b911fd931 100644 --- a/sysdeps/x86/dl-cet.c +++ b/sysdeps/x86/dl-cet.c @@ -201,7 +201,7 @@ dl_cet_check_startup (struct link_map *m, struct dl_cet_info *info) feature_1_lock |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; if (feature_1_lock != 0 - && dl_cet_lock_cet () != 0) + && dl_cet_lock_cet (feature_1_lock) != 0) _dl_fatal_printf ("%s: can't lock CET\n", info->program); } diff --git a/sysdeps/x86_64/nptl/tls.h b/sysdeps/x86_64/nptl/tls.h index 1403f939f7..4bcc2552a1 100644 --- a/sysdeps/x86_64/nptl/tls.h +++ b/sysdeps/x86_64/nptl/tls.h @@ -60,7 +60,7 @@ typedef struct void *__private_tm[4]; /* GCC split stack support. */ void *__private_ss; - /* The lowest address of shadow stack, */ + /* The marker for the current shadow stack. */ unsigned long long int ssp_base; /* Must be kept even if it is no longer used by glibc since programs, like AddressSanitizer, depend on the size of tcbhead_t. */ From patchwork Wed Dec 6 17:20:05 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: 1872903 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=K/OSHa2L; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Slkhm3Qm7z23nD for ; Thu, 7 Dec 2023 04:21:28 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 07343386185C for ; Wed, 6 Dec 2023 17:21:21 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id 4F5583858023 for ; Wed, 6 Dec 2023 17:20:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4F5583858023 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 4F5583858023 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::632 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883221; cv=none; b=EGagewBZyy3s60uxxB8QdqHeCUes5Osnuhmy5WsdUmBxoyrYcykHF5l84QAITOJYyMt0BZrYgdonlGSkn7/xN+h7iRsPKcSpiCbvfxX9KH2FDFKV/pxZ1YYivOdYWv3be9FP0EaAsEit93ocsN87JC7xMFU0rWbqw7+6WFEWTUk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883221; c=relaxed/simple; bh=A1+fHusyoFA5MDej/XYdek+uGkHZyKKGCEBcDtlbUlM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lcLrBFJUwbWl9YT17jxQRM2M57ijW5JBqS8WQB22ZxMOlYaDtL0Asv+m5ndWZshDadDLOrh+g7wZGk/2oA1pATrxHW3Q6YwbZDfsw2gcCYXFWEoxYYBXI/IZD01gNuHrd9ZvrpHQITE5vG5lX0nXkw+I73pBgyoCs8Aw5hiJP2s= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1cfc9c4acb6so27745ad.0 for ; Wed, 06 Dec 2023 09:20:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883219; x=1702488019; 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=VuRsd3PBVfXGnOWm4frtoz3pLiPQWXDOpUpz4C5moqM=; b=K/OSHa2LsJNn4XW89lrMweIm7DmcZDpEUVU74iR1T+v+WuQW98JXly2o8SHpMMux2s IMVxmczMmsuPgFHsg7o1CEW95AtU9qal3irW7Z23QxHwXwcHQdmGZ6gnEJ+7lquJvDat qqRjOyrMdHyb9jt7A6tzqu7JUDedXB5BS4wQeYhKRIh79QcidR174MgJ2x+c73N+ERJd BgTosaBDW0VikJy9n4AcmqZDrJF6aEmK6wfYHws+GZ6PJOBatS1B0cECuGWYvaBpzx6E PxNAU1JB0wo1ekG4qJeloLazJvFxnVCWkvYvFv64sNdqXYk75s+cUs+Ln+oyu7qAxDmL l6OA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883219; x=1702488019; 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=VuRsd3PBVfXGnOWm4frtoz3pLiPQWXDOpUpz4C5moqM=; b=byPC21Mu9LiJ/ZDjv15YmHT0pf6D4NIKJvARaYAShKdVYMcS622bOnIRPje8+hzhrO tEa1m+fmr9cGvXVz1+1EaDW3WMkXmxqzHaD1JidQq992BPOQZy3qXDxi7KtOX8QDjucd Iy6XmGWFBja4uZ/CE7ONCUmaTuwoobIMGIyAcugIHYdyCWgWqdzKqRVwgFyG27kilqGn TOTVQ3ZVrMF6cq/6HEc1BIXljOk8CKT7TkhsrCpf8P38U4+NwlmcpbMpP9I/QYRG7tSh +dZeB6XOhDDE4fClh3uIu3z4AAg7XIeGV1g6hIGtTr/1ege3giTrojio2dKybG7cTwn9 nt5w== X-Gm-Message-State: AOJu0Yx3murTUlEzRw3p2fYmZX3wMIxtnrLUAwISeby1lX5W/0wzaEA0 18NAR36ykNKbrtdd7UMoPt4OsNghduM= X-Google-Smtp-Source: AGHT+IHv/vsCzmo5avri3+QVh7rS6bMCQ7xqYOqIHYo+ZW4S5k9hs3bLqRIYBis7cpxHbDHfOaOs4A== X-Received: by 2002:a17:902:ec82:b0:1d0:9c9d:dcdc with SMTP id x2-20020a170902ec8200b001d09c9ddcdcmr905515plg.85.1701883218992; Wed, 06 Dec 2023 09:20:18 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id k16-20020a170902ba9000b001bc6e6069a6sm70795pls.122.2023.12.06.09.20.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:16 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id D864A740635; 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 12/17] elf: Always provide _dl_get_dl_main_map in libc.a Date: Wed, 6 Dec 2023 09:20:05 -0800 Message-ID: <20231206172010.1023415-13-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=-3025.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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+incoming=patchwork.ozlabs.org@sourceware.org Always provide _dl_get_dl_main_map in libc.a. It will be used by x86 to process PT_GNU_PROPERTY segment. --- elf/dl-support.c | 2 -- sysdeps/generic/ldsodefs.h | 8 ++++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/elf/dl-support.c b/elf/dl-support.c index 837fa1c836..70c5b3599a 100644 --- a/elf/dl-support.c +++ b/elf/dl-support.c @@ -344,7 +344,6 @@ _dl_non_dynamic_init (void) DL_SYSINFO_IMPLEMENTATION #endif -#if ENABLE_STATIC_PIE /* Since relocation to hidden _dl_main_map causes relocation overflow on aarch64, a function is used to get the address of _dl_main_map. */ @@ -353,7 +352,6 @@ _dl_get_dl_main_map (void) { return &_dl_main_map; } -#endif /* This is used by _dl_runtime_profile, not used on static code. */ void diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index 9b50ddd09f..0e8a008a49 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -1172,10 +1172,6 @@ void __libc_setup_tls (void); # if ENABLE_STATIC_PIE /* Relocate static executable with PIE. */ extern void _dl_relocate_static_pie (void) attribute_hidden; - -/* Get a pointer to _dl_main_map. */ -extern struct link_map * _dl_get_dl_main_map (void) - __attribute__ ((visibility ("hidden"))); # else # define _dl_relocate_static_pie() # endif @@ -1217,6 +1213,10 @@ rtld_hidden_proto (_dl_deallocate_tls) extern void _dl_nothread_init_static_tls (struct link_map *) attribute_hidden; +/* Get a pointer to _dl_main_map. */ +extern struct link_map * _dl_get_dl_main_map (void) + __attribute__ ((visibility ("hidden"))); + /* Find origin of the executable. */ extern const char *_dl_get_origin (void) attribute_hidden; From patchwork Wed Dec 6 17:20:06 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: 1872910 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=KLGEwf25; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Slkjn4j6pz23nD for ; Thu, 7 Dec 2023 04:22:21 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F107E3836E80 for ; Wed, 6 Dec 2023 17:22:04 +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 C2E5A385843B for ; Wed, 6 Dec 2023 17:20:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C2E5A385843B 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 C2E5A385843B 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=1701883228; cv=none; b=WVgH6CgFPFw1TXnYLU8vBPMxm5ToE13pElnmvCuTfgd3okgBB7J1QmfuIwXpYwGG/TbIirEOBTJk0gRUrEsb2Z+LkrkSg8C64e/pMyZC0u0+Lxotmla1m3gQmSEuwuzeZM34oCxVa6EmBWJ5E+QkbYFdn5txa5zl52qaJSpojeo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883228; c=relaxed/simple; bh=XDysHeSeKRFd3knkiuJqgqtIg1iJ7jmhqsr9y/M6nbg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=wQh12px3TDwRvXYxZpmCw7ihkIvI/j5hTNoLqSgVlQfqaDhXLFYvcw3OR4Uc1fEQRAgMooF5cRXRBIanHy39Kpu0QpbxZAb1JmpcBJOis1iHME0nOrwwNi57rr0jteFdTu+DItazIW4U2wC8QqlNep1uuXEMwpsYwWM3qaIqz1U= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-6ce76f0748fso45825b3a.2 for ; Wed, 06 Dec 2023 09:20:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883222; x=1702488022; 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=qydojsTe8hI1JAkIJDXFjPrTLc121cq1MY2fBdIlLXM=; b=KLGEwf25e4L5lLfBpspenHHGciJg/392uG2DhbvPgXVUz/XW1CkH6pbxgB0amAp2tj lXT/zSwzEaAe6nynF4MfSVboNlVBMLngHp40YfJNiCYLB9mgehFt1MjbKpcL3iywBKW5 qCFi2e3nXkUaCCUKzLEnh1ZF+tBO/Wl1wM/bYg9JiwxxlSe7Tx3kcpewk132pCbwE2D5 iYqL0VBiEOdpH2+WSyaQIrLJieWHcIQCn8P+dsAXPPD16MT85K2DMXS1S7c0FpVksUZh lsf5KpOS4dXTEllQyRIdUUY6AN4+QKl77yvPYzoJGXX1qgTEzEl10tfhSZxKUSpRxy4T VE0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883222; x=1702488022; 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=qydojsTe8hI1JAkIJDXFjPrTLc121cq1MY2fBdIlLXM=; b=nME+JFLeCJS+d8Q3SIE8c4b1XAcOE3C7gwyMg8u8/AVcUlsM0mURBj+qK3ln42QAUq mGPstPf9RtP+aBLrFASBDgZCv6a9DLP6DDDJrYETyJ2AnZqRB9/2lbX73n16/8iyWQZn IYOZuIcrJf5IBvSOg1AwQIC0yvEC0Onw2zzZ+qMwkhJJk13aARd+mJJCdo5l8jyYTT0K 1xq8qYILV2z4DJzOyyD6d6WwuvCKA9GduKoXUbGqQJLLMCLSiiAKcIdVmSrzC2wukFTh LKI5GO9vc5VW5R2tavG1imAvaJZnJ2l4ZuOMzZ2MNhVn1PP2fL/bEh6ABOCRH19wXsds m6bQ== X-Gm-Message-State: AOJu0YxmhroM4E9yZ79yZWP0tJdCtoOnUdhLSxMmpLvGuxyuXdocTuJD tXQZOAN3TAE99lDMHjMQE6pD6bMxdUQ= X-Google-Smtp-Source: AGHT+IE1xdduXfZ/qOJ5d3gfGcXXeOFe/LMEisIE8zRYm0zZNzZxRGbXOeng5RK8oYlP9EgoPVRxTw== X-Received: by 2002:a05:6a20:8e17:b0:18f:dde5:7211 with SMTP id y23-20020a056a208e1700b0018fdde57211mr1071061pzj.7.1701883221624; Wed, 06 Dec 2023 09:20:21 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id e19-20020a62aa13000000b006ce41b15613sm220925pff.112.2023.12.06.09.20.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:16 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id E656C740636; 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 13/17] x86/cet: Enable shadow stack during startup Date: Wed, 6 Dec 2023 09:20:06 -0800 Message-ID: <20231206172010.1023415-14-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=-3025.0 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+incoming=patchwork.ozlabs.org@sourceware.org Previously, CET was enabled by kernel before passing control to user space and the startup code must disable CET if applications or shared libraries aren't CET enabled. Since the current kernel only supports shadow stack and won't enable shadow stack before passing control to user space, we need to enable shadow stack during startup if the application and all shared library are shadow stack enabled. There is no need to disable shadow stack at startup. Shadow stack can only be enabled in a function which will never return. Otherwise, shadow stack will underflow at the function return. 1. GL(dl_x86_feature_1) is set to the CET features which are supported by the processor and are not disabled by the tunable. Only non-zero features in GL(dl_x86_feature_1) should be enabled. After enabling shadow stack with ARCH_SHSTK_ENABLE, ARCH_SHSTK_STATUS is used to check if shadow stack is really enabled. 2. Use ARCH_SHSTK_ENABLE in RTLD_START in dynamic executable. It is safe since RTLD_START never returns. 3. Call arch_prctl (ARCH_SHSTK_ENABLE) from ARCH_SETUP_TLS in static executable. Since the start function using ARCH_SETUP_TLS never returns, it is safe to enable shadow stack in ARCH_SETUP_TLS. --- sysdeps/unix/sysv/linux/x86/cpu-features.c | 49 -------------- sysdeps/unix/sysv/linux/x86/dl-cet.h | 23 +++++++ sysdeps/unix/sysv/linux/x86_64/dl-cet.h | 47 +++++++++++++ sysdeps/x86/cpu-features-offsets.sym | 1 + sysdeps/x86/cpu-features.c | 51 -------------- sysdeps/x86/dl-cet.c | 77 +++++++++++----------- sysdeps/x86/get-cpuid-feature-leaf.c | 2 +- sysdeps/x86/include/cpu-features.h | 3 + sysdeps/x86/libc-start.h | 54 ++++++++++++++- sysdeps/x86_64/dl-machine.h | 12 +++- 10 files changed, 175 insertions(+), 144 deletions(-) delete mode 100644 sysdeps/unix/sysv/linux/x86/cpu-features.c create mode 100644 sysdeps/unix/sysv/linux/x86_64/dl-cet.h diff --git a/sysdeps/unix/sysv/linux/x86/cpu-features.c b/sysdeps/unix/sysv/linux/x86/cpu-features.c deleted file mode 100644 index 0e6e2bf855..0000000000 --- a/sysdeps/unix/sysv/linux/x86/cpu-features.c +++ /dev/null @@ -1,49 +0,0 @@ -/* Initialize CPU feature data for Linux/x86. - This file is part of the GNU C Library. - Copyright (C) 2018-2023 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 - . */ - -#if CET_ENABLED -# include -# include - -static inline int __attribute__ ((always_inline)) -get_cet_status (void) -{ - unsigned long long kernel_feature; - unsigned int status = 0; - if (INTERNAL_SYSCALL_CALL (arch_prctl, ARCH_SHSTK_STATUS, - &kernel_feature) == 0) - { - if ((kernel_feature & ARCH_SHSTK_SHSTK) != 0) - status = GNU_PROPERTY_X86_FEATURE_1_SHSTK; - } - return status; -} - -# ifndef SHARED -static inline void -x86_setup_tls (void) -{ - __libc_setup_tls (); - THREAD_SETMEM (THREAD_SELF, header.feature_1, GL(dl_x86_feature_1)); -} - -# define ARCH_SETUP_TLS() x86_setup_tls () -# endif -#endif - -#include diff --git a/sysdeps/unix/sysv/linux/x86/dl-cet.h b/sysdeps/unix/sysv/linux/x86/dl-cet.h index da220ac627..634c885d33 100644 --- a/sysdeps/unix/sysv/linux/x86/dl-cet.h +++ b/sysdeps/unix/sysv/linux/x86/dl-cet.h @@ -38,3 +38,26 @@ dl_cet_lock_cet (unsigned int cet_feature) return (int) INTERNAL_SYSCALL_CALL (arch_prctl, ARCH_SHSTK_LOCK, kernel_feature); } + +static inline unsigned int __attribute__ ((always_inline)) +dl_cet_get_cet_status (void) +{ + unsigned long long kernel_feature; + unsigned int status = 0; + if (INTERNAL_SYSCALL_CALL (arch_prctl, ARCH_SHSTK_STATUS, + &kernel_feature) == 0) + { + if ((kernel_feature & ARCH_SHSTK_SHSTK) != 0) + status = GNU_PROPERTY_X86_FEATURE_1_SHSTK; + } + return status; +} + +/* 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)) \ + { \ + long long int kernel_feature = ARCH_SHSTK_SHSTK; \ + INTERNAL_SYSCALL_CALL (arch_prctl, ARCH_SHSTK_ENABLE, \ + kernel_feature); \ + } diff --git a/sysdeps/unix/sysv/linux/x86_64/dl-cet.h b/sysdeps/unix/sysv/linux/x86_64/dl-cet.h new file mode 100644 index 0000000000..e23e05c6b8 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86_64/dl-cet.h @@ -0,0 +1,47 @@ +/* Linux/x86-64 CET initializers function. + Copyright (C) 2023 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 +#include_next + +#define X86_STRINGIFY_1(x) #x +#define X86_STRINGIFY(x) X86_STRINGIFY_1 (x) + +/* Enable shadow stack before calling _dl_init if it is enabled in + GL(dl_x86_feature_1). Call _dl_setup_x86_features to setup shadow + stack. */ +#define RTLD_START_ENABLE_X86_FEATURES \ +"\ + # Check if shadow stack is enabled in GL(dl_x86_feature_1).\n\ + movl _rtld_local+" X86_STRINGIFY (RTLD_GLOBAL_DL_X86_FEATURE_1_OFFSET) "(%rip), %edx\n\ + testl $" X86_STRINGIFY (X86_FEATURE_1_SHSTK) ", %edx\n\ + jz 1f\n\ + # Enable shadow stack if enabled in GL(dl_x86_feature_1).\n\ + movl $" X86_STRINGIFY (ARCH_SHSTK_SHSTK) ", %esi\n\ + movl $" X86_STRINGIFY (ARCH_SHSTK_ENABLE) ", %edi\n\ + movl $" X86_STRINGIFY (__NR_arch_prctl) ", %eax\n\ + syscall\n\ +1:\n\ + # Pass GL(dl_x86_feature_1) to _dl_cet_setup_features.\n\ + movl %edx, %edi\n\ + # Align stack for the _dl_cet_setup_features call.\n\ + andq $-16, %rsp\n\ + call _dl_cet_setup_features\n\ + # Restore %rax and %rsp from %r12 and %r13.\n\ + movq %r12, %rax\n\ + movq %r13, %rsp\n\ +" diff --git a/sysdeps/x86/cpu-features-offsets.sym b/sysdeps/x86/cpu-features-offsets.sym index 6d03cea8e8..5429f60632 100644 --- a/sysdeps/x86/cpu-features-offsets.sym +++ b/sysdeps/x86/cpu-features-offsets.sym @@ -4,3 +4,4 @@ RTLD_GLOBAL_RO_DL_X86_CPU_FEATURES_OFFSET offsetof (struct rtld_global_ro, _dl_x86_cpu_features) XSAVE_STATE_SIZE_OFFSET offsetof (struct cpu_features, xsave_state_size) +RTLD_GLOBAL_DL_X86_FEATURE_1_OFFSET offsetof (struct rtld_global, _dl_x86_feature_1) diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c index f180f0d9a4..097868c1d9 100644 --- a/sysdeps/x86/cpu-features.c +++ b/sysdeps/x86/cpu-features.c @@ -1106,57 +1106,6 @@ no_cpuid: TUNABLE_CALLBACK (set_x86_ibt)); TUNABLE_GET (x86_shstk, tunable_val_t *, TUNABLE_CALLBACK (set_x86_shstk)); - - /* Check CET status. */ - unsigned int cet_status = get_cet_status (); - - if ((cet_status & GNU_PROPERTY_X86_FEATURE_1_IBT) == 0) - CPU_FEATURE_UNSET (cpu_features, IBT) - if ((cet_status & GNU_PROPERTY_X86_FEATURE_1_SHSTK) == 0) - CPU_FEATURE_UNSET (cpu_features, SHSTK) - - if (cet_status) - { - GL(dl_x86_feature_1) = cet_status; - -# ifndef SHARED - /* Check if IBT and SHSTK are enabled by kernel. */ - if ((cet_status - & (GNU_PROPERTY_X86_FEATURE_1_IBT - | GNU_PROPERTY_X86_FEATURE_1_SHSTK))) - { - /* Disable IBT and/or SHSTK if they are enabled by kernel, but - disabled by environment variable: - - GLIBC_TUNABLES=glibc.cpu.hwcaps=-IBT,-SHSTK - */ - unsigned int cet_feature = 0; - if (!CPU_FEATURE_USABLE (IBT)) - cet_feature |= (cet_status - & GNU_PROPERTY_X86_FEATURE_1_IBT); - if (!CPU_FEATURE_USABLE (SHSTK)) - cet_feature |= (cet_status - & GNU_PROPERTY_X86_FEATURE_1_SHSTK); - - if (cet_feature) - { - int res = dl_cet_disable_cet (cet_feature); - - /* Clear the disabled bits in dl_x86_feature_1. */ - if (res == 0) - GL(dl_x86_feature_1) &= ~cet_feature; - } - - /* Lock CET if IBT or SHSTK is enabled in executable. Don't - lock CET if IBT or SHSTK is enabled permissively. */ - if (GL(dl_x86_feature_control).ibt != cet_permissive - && GL(dl_x86_feature_control).shstk != cet_permissive) - dl_cet_lock_cet (GL(dl_x86_feature_1) - & (GNU_PROPERTY_X86_FEATURE_1_IBT - | GNU_PROPERTY_X86_FEATURE_1_SHSTK)); - } -# endif - } #endif #ifndef SHARED diff --git a/sysdeps/x86/dl-cet.c b/sysdeps/x86/dl-cet.c index 8b911fd931..7f37244d37 100644 --- a/sysdeps/x86/dl-cet.c +++ b/sysdeps/x86/dl-cet.c @@ -172,40 +172,11 @@ dl_cet_check_startup (struct link_map *m, struct dl_cet_info *info) = info->enable_feature_1 ^ info->feature_1_enabled; if (disable_feature_1 != 0) { - /* Disable features in the kernel because of legacy objects or - cet_always_off. */ - if (dl_cet_disable_cet (disable_feature_1) != 0) - _dl_fatal_printf ("%s: can't disable x86 Features\n", - info->program); - /* Clear the disabled bits. Sync dl_x86_feature_1 and info->feature_1_enabled with info->enable_feature_1. */ info->feature_1_enabled = info->enable_feature_1; GL(dl_x86_feature_1) = info->enable_feature_1; } - - if (HAS_CPU_FEATURE (IBT) || HAS_CPU_FEATURE (SHSTK)) - { - /* Lock CET features only if IBT or SHSTK are enabled and are not - enabled permissively. */ - unsigned int feature_1_lock = 0; - - if (((info->feature_1_enabled & GNU_PROPERTY_X86_FEATURE_1_IBT) - != 0) - && info->enable_ibt_type != cet_permissive) - feature_1_lock |= GNU_PROPERTY_X86_FEATURE_1_IBT; - - if (((info->feature_1_enabled & GNU_PROPERTY_X86_FEATURE_1_SHSTK) - != 0) - && info->enable_shstk_type != cet_permissive) - feature_1_lock |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; - - if (feature_1_lock != 0 - && dl_cet_lock_cet (feature_1_lock) != 0) - _dl_fatal_printf ("%s: can't lock CET\n", info->program); - } - - THREAD_SETMEM (THREAD_SELF, header.feature_1, GL(dl_x86_feature_1)); } #endif @@ -291,6 +262,15 @@ dl_cet_check (struct link_map *m, const char *program) { struct dl_cet_info info; + /* CET is enabled only if RTLD_START_ENABLE_X86_FEATURES is defined. */ +#if defined SHARED && defined RTLD_START_ENABLE_X86_FEATURES + /* Set dl_x86_feature_1 to features enabled in the executable. */ + if (program != NULL) + GL(dl_x86_feature_1) = (m->l_x86_feature_1_and + & (X86_FEATURE_1_IBT + | X86_FEATURE_1_SHSTK)); +#endif + /* Check how IBT and SHSTK should be enabled. */ info.enable_ibt_type = GL(dl_x86_feature_control).ibt; info.enable_shstk_type = GL(dl_x86_feature_control).shstk; @@ -300,17 +280,9 @@ dl_cet_check (struct link_map *m, const char *program) /* No legacy object check if IBT and SHSTK are always on. */ if (info.enable_ibt_type == cet_always_on && info.enable_shstk_type == cet_always_on) - { -#ifdef SHARED - /* Set it only during startup. */ - if (program != NULL) - THREAD_SETMEM (THREAD_SELF, header.feature_1, - info.feature_1_enabled); -#endif - return; - } + return; - /* Check if IBT and SHSTK were enabled by kernel. */ + /* Check if IBT and SHSTK were enabled. */ if (info.feature_1_enabled == 0) return; @@ -344,6 +316,33 @@ _dl_cet_open_check (struct link_map *l) dl_cet_check (l, NULL); } +/* Set GL(dl_x86_feature_1) to the enabled features and clear the + active bits of the disabled features. */ + +attribute_hidden +void +_dl_cet_setup_features (unsigned int cet_feature) +{ + /* NB: cet_feature == GL(dl_x86_feature_1) which is set to features + enabled from executable, not necessarily supported by kernel. */ + if (cet_feature) + { + cet_feature = dl_cet_get_cet_status (); + /* Sync GL(dl_x86_feature_1) with kernel. */ + GL(dl_x86_feature_1) = cet_feature; + if (cet_feature) + { + THREAD_SETMEM (THREAD_SELF, header.feature_1, cet_feature); + + /* Lock CET if IBT or SHSTK is enabled in executable. Don't + lock CET if IBT or SHSTK is enabled permissively. */ + if (GL(dl_x86_feature_control).ibt != cet_permissive + && (GL(dl_x86_feature_control).shstk != cet_permissive)) + dl_cet_lock_cet (cet_feature); + } + } +} + #ifdef SHARED # ifndef LINKAGE diff --git a/sysdeps/x86/get-cpuid-feature-leaf.c b/sysdeps/x86/get-cpuid-feature-leaf.c index 40a46cc79c..9317a6b494 100644 --- a/sysdeps/x86/get-cpuid-feature-leaf.c +++ b/sysdeps/x86/get-cpuid-feature-leaf.c @@ -24,7 +24,7 @@ __x86_get_cpuid_feature_leaf (unsigned int leaf) static const struct cpuid_feature feature = {}; if (leaf < CPUID_INDEX_MAX) return ((const struct cpuid_feature *) - &GLRO(dl_x86_cpu_features).features[leaf]); + &GLRO(dl_x86_cpu_features).features[leaf]); else return &feature; } diff --git a/sysdeps/x86/include/cpu-features.h b/sysdeps/x86/include/cpu-features.h index 2d7427a6c0..23bd8146a2 100644 --- a/sysdeps/x86/include/cpu-features.h +++ b/sysdeps/x86/include/cpu-features.h @@ -990,6 +990,9 @@ extern const struct cpu_features *_dl_x86_get_cpu_features (void) # define INIT_ARCH() # define _dl_x86_get_cpu_features() (&GLRO(dl_x86_cpu_features)) extern void _dl_x86_init_cpu_features (void) attribute_hidden; + +extern void _dl_cet_setup_features (unsigned int) + attribute_hidden; #endif #ifdef __x86_64__ diff --git a/sysdeps/x86/libc-start.h b/sysdeps/x86/libc-start.h index e93da6ef3d..856230daeb 100644 --- a/sysdeps/x86/libc-start.h +++ b/sysdeps/x86/libc-start.h @@ -19,7 +19,57 @@ #ifndef SHARED # define ARCH_SETUP_IREL() apply_irel () # define ARCH_APPLY_IREL() -# ifndef ARCH_SETUP_TLS -# define ARCH_SETUP_TLS() __libc_setup_tls () +# ifdef __CET__ +/* Get CET features enabled in the static executable. */ + +static inline unsigned int +get_cet_feature (void) +{ + /* Check if CET is supported and not disabled by tunables. */ + struct cpu_features *cpu_features + = (struct cpu_features *) __get_cpu_features (); + unsigned int cet_feature = 0; + if (CPU_FEATURE_USABLE_P (cpu_features, IBT)) + cet_feature |= GNU_PROPERTY_X86_FEATURE_1_IBT; + if (CPU_FEATURE_USABLE_P (cpu_features, SHSTK)) + cet_feature |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; + if (!cet_feature) + return cet_feature; + + struct link_map *main_map = _dl_get_dl_main_map (); + + /* Scan program headers backward to check PT_GNU_PROPERTY early for + x86 feature bits on static executable. */ + const ElfW(Phdr) *phdr = GL(dl_phdr); + const ElfW(Phdr) *ph; + for (ph = phdr + GL(dl_phnum); ph != phdr; ph--) + if (ph[-1].p_type == PT_GNU_PROPERTY) + { + _dl_process_pt_gnu_property (main_map, -1, &ph[-1]); + /* Enable IBT and SHSTK only if they are enabled on static + executable. */ + cet_feature &= (main_map->l_x86_feature_1_and + & (GNU_PROPERTY_X86_FEATURE_1_IBT + | GNU_PROPERTY_X86_FEATURE_1_SHSTK)); + /* Set GL(dl_x86_feature_1) to the enabled CET features. */ + GL(dl_x86_feature_1) = cet_feature; + break; + } + + return cet_feature; +} + +/* The function using this macro to enable shadow stack must not return + to avoid shadow stack underflow. */ +# define ARCH_SETUP_TLS() \ + { \ + __libc_setup_tls (); \ + \ + unsigned int cet_feature = get_cet_feature (); \ + ENABLE_X86_CET (cet_feature); \ + _dl_cet_setup_features (cet_feature); \ + } +# else +# define ARCH_SETUP_TLS() __libc_setup_tls () # endif #endif /* !SHARED */ diff --git a/sysdeps/x86_64/dl-machine.h b/sysdeps/x86_64/dl-machine.h index 581a2f1a9e..faeae723cb 100644 --- a/sysdeps/x86_64/dl-machine.h +++ b/sysdeps/x86_64/dl-machine.h @@ -29,6 +29,11 @@ #include #include #include +#ifdef __CET__ +# include +#else +# define RTLD_START_ENABLE_X86_FEATURES +#endif /* Return nonzero iff ELF header is compatible with the running host. */ static inline int __attribute__ ((unused)) @@ -146,13 +151,16 @@ _start:\n\ _dl_start_user:\n\ # Save the user entry point address in %r12.\n\ movq %rax, %r12\n\ + # Save %rsp value in %r13.\n\ + movq %rsp, %r13\n\ +"\ + RTLD_START_ENABLE_X86_FEATURES \ +"\ # Read the original argument count.\n\ movq (%rsp), %rdx\n\ # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env)\n\ # argc -> rsi\n\ movq %rdx, %rsi\n\ - # Save %rsp value in %r13.\n\ - movq %rsp, %r13\n\ # And align stack for the _dl_init call. \n\ andq $-16, %rsp\n\ # _dl_loaded -> rdi\n\ From patchwork Wed Dec 6 17:20:07 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: 1872911 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=hPSQMcQN; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Slkk41Bblz23nD for ; Thu, 7 Dec 2023 04:22:36 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 97728383E708 for ; Wed, 6 Dec 2023 17:22:18 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id 40202385AC11 for ; Wed, 6 Dec 2023 17:20:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 40202385AC11 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 40202385AC11 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::434 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883230; cv=none; b=DVeuD6r/g0wK+VuduEGqogpWxzL46FoyqEQrtkKiEP0b/EaxQrNLspi8E5h4dRXXuolLrceF/ouAJrN1Wob8rNH6S+hYFdVQelVEjnYtd/0xVmIWd5r4fuNghdR2I2gZrmPKeGCw8ARQV4kShKlM9IBNhiKrEDqcrwqTkDhKLlc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883230; c=relaxed/simple; bh=TbbKXol4dplac/fN2Lhtbgcm6bquwYuTPcEpG6ZOLDk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=MTKoJiNOe3qA9c0iNUDTMKwQ2By6rfu55sVJV3C7Y8auqWm/zrOvNfWMKgsAwfFXmHfCuG8IJv6iuBYn3Bq59lUosKBE0uKQHcjghSlPB8x2Cqc3fxoOB87GleJGs8+vDU57cjWvT1aj8nBnJjk1mL4nHnaqhg4avBD3MGa80mg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-6cbd24d9557so39692b3a.1 for ; Wed, 06 Dec 2023 09:20:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883219; x=1702488019; 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=ANRVNjPijP3Z9eM3L0G9RkyDQHQgPqv1CKkr0Ocdpjs=; b=hPSQMcQNuqI1glhKil4yPryNyTRw3vPCSipVW+eBaAnGQgeJdRWTvXQSYsvSlnxG/O cGQA/f7qevE8vOPFDzbiMvOlRGNr9/vKTbYrkjWlmZdjArF+FHosl96s5nmfUNl/vyt7 uQgU6lgD2vrqu6mS47EyzBOn9y0IpQNLMR6rAc+TDePFgtfIfShAbfSZ8GHesg6Z6IxD j9ewE4L9mMnEvFjfou3YENiKNw/dAMTsJpmuHdRMm/qRzEPMtTIXJQNbH/jvzhO0PyEl 3t7ZkmdmYemCKtmuvY2hcHrWEoWO9OkWrOeoYG1MzqHo1A5pFasMVPcFe3eZENme5UoQ iGuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883219; x=1702488019; 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=ANRVNjPijP3Z9eM3L0G9RkyDQHQgPqv1CKkr0Ocdpjs=; b=jFYnwbEZEQSjA0ZhFFVGC28pPuKAZqB8qhUrVtAuIhl8LnY5onjQb2R2K38/b6N8DX 1yTPAaIhF+tSd+zfdSh3R9ckFaIg1a6/evhxFLWV8/2IdoiawfGtKUlGyut2RHHp0/aC z6lIwVM/Krva7foaOhGPBtwZaJndWwO2jpTMhKeErZeH79EafN/6CeziNa0WbvzfunOi i9VAyjAB2wLRunnrHwz5Qb7Fl6CGOCcnIYpVJrhsIMKa9gdvswmJ60xifbfqZUDB4neF 3PQy77apqy5N4E3HKv2H6QtvUWLF6c5Yrk/oIXxmTs6ohXOOB1ZQBo25mXmHTdjs1x9S ilqQ== X-Gm-Message-State: AOJu0YxWAssqRED9V0O1oauggCgQNmYd08OvxTh0olZP3+T07j0+r+io zVSR/mbOWBVNI75SJh6WgwtJu90M6bU= X-Google-Smtp-Source: AGHT+IGfhPhOyo6q+XUlNo/qMPDPZ3lnpVOlHe34QpYssZJVxK6YPD1kOxzFMTct+ojlKHpc6CmFTA== X-Received: by 2002:aa7:888a:0:b0:6c3:4bf2:7486 with SMTP id z10-20020aa7888a000000b006c34bf27486mr1844938pfe.7.1701883219079; Wed, 06 Dec 2023 09:20:19 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id q14-20020a62ae0e000000b006ce77ffcc75sm219510pff.165.2023.12.06.09.20.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:16 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 0456D740637; Wed, 6 Dec 2023 09:20:11 -0800 (PST) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH 14/17] x86/cet: Check feature_1 in TCB for active IBT and SHSTK Date: Wed, 6 Dec 2023 09:20:07 -0800 Message-ID: <20231206172010.1023415-15-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=-3025.0 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+incoming=patchwork.ozlabs.org@sourceware.org Initially, IBT and SHSTK are marked as active when CPU supports them and CET are enabled in glibc. They can be disabled early by tunables before relocation. Since after relocation, GLRO(dl_x86_cpu_features) becomes read-only, we can't update GLRO(dl_x86_cpu_features) to mark IBT and SHSTK as inactive. Instead, check the feature_1 field in TCB to decide if IBT and SHST are active. --- sysdeps/x86/bits/platform/x86.h | 8 ++++++++ sysdeps/x86/get-cpuid-feature-leaf.c | 11 ++++++++++- sysdeps/x86/sys/platform/x86.h | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/sysdeps/x86/bits/platform/x86.h b/sysdeps/x86/bits/platform/x86.h index 1e23d53ba2..1575ae53fb 100644 --- a/sysdeps/x86/bits/platform/x86.h +++ b/sysdeps/x86/bits/platform/x86.h @@ -337,3 +337,11 @@ 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 9317a6b494..f69936b31e 100644 --- a/sysdeps/x86/get-cpuid-feature-leaf.c +++ b/sysdeps/x86/get-cpuid-feature-leaf.c @@ -15,9 +15,18 @@ License along with the GNU C Library; if not, see . */ - +#include +#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 1ea2c5fc0b..89b1b16f22 100644 --- a/sysdeps/x86/sys/platform/x86.h +++ b/sysdeps/x86/sys/platform/x86.h @@ -45,6 +45,23 @@ x86_cpu_present (unsigned int __index) 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 + } const struct cpuid_feature *__ptr = __x86_get_cpuid_feature_leaf (__index / (8 * sizeof (unsigned int) * 4)); unsigned int __reg From patchwork Wed Dec 6 17:20:08 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: 1872905 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=b0TgyZN2; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Slkj71pLnz23nD for ; Thu, 7 Dec 2023 04:21:47 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9D4CD3870C33 for ; Wed, 6 Dec 2023 17:21:36 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by sourceware.org (Postfix) with ESMTPS id 7132D3857350 for ; Wed, 6 Dec 2023 17:20:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7132D3857350 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 7132D3857350 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::630 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883227; cv=none; b=nyqGHm//QMWDkbBNC+6kWgRMIBxMmfnh1VRAE4tLU5rR73aA+t5+7HIB75/DB31viiuJApvd/R+g5iR4hPYS5MztxiVs5HsaKgrmH8+4SQmQjLxF+coivmCj6vNlmYiJmnC6Bg4NefD7XEmaEUnIrttttZKp4dJvaY9ulpbpUsM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883227; c=relaxed/simple; bh=qCK11upKFGBB+La4ZXbMDWEYxvdwZcxl70V5K9orFRo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=gfZl/WUf9T5mBxuKEi5hnc5uiGdsWBIayMxvpdW/QnypAhNgaXJ0L4QkMoA+Agzmr41Y84BQeBTF7/cweyz+b2zKz8jd9Ec5g5r0UFHGkKsrlj0gsZ+PjUrc9UM5GQukMBwvJxmYVlhHWEa1jL635dzdOdUPGfMbYj5xdpOGOVs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1d075392ff6so342615ad.1 for ; Wed, 06 Dec 2023 09:20:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883220; x=1702488020; 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=8q1Ohnwjrg4tUBj3EdAv/9tjD2WX8CpF4KYlpGc8ifs=; b=b0TgyZN21c8K/5w+z2Wr3+7O5rYprE03Z1QJBflltU/ET+FM0zODw71ZWT+cKCIqmf zIpVp2eibyDBklz6NbMY8HUTJ+pGFkbxMPCmbczpCBYShO7SJAtRAwLIgePykYTkRGL7 Epcn4nppRss1ZrzfiHkHp6P+1Vnz+dYgKFbv4byro3TD0d2D/heOtofCxvjmIm+fS1tq oudI/xNl4gTeT/bTgVADIoFF3CWMLSKAf6EB/6qxNK7bv/OrFOTslmmosG3X101+Dqwd mR+S2J/0SmBGpoEZfZFUJWXhfaqAkIGZgGIl0ye6/1hMBdtJGeUP5vyHg10k8Xhd+KYT +fDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883220; x=1702488020; 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=8q1Ohnwjrg4tUBj3EdAv/9tjD2WX8CpF4KYlpGc8ifs=; b=KSRcy/c39re3h0arHVbK5sCW1FBVxuvh3wdiYS5Iw83IcxqAG2hMt8fibhIv+0sJHn haOwOqdpBw6YCC+OaG974mKeYbupux3gh7o+nL6DwwxZjJddhaaEvGI4fMclU4gp8rMP g/WOmpkiZnCmsdscH3EVm8ZDlqJcycBdaLsT8g33tSHRwMzXH+SEQ4b0tQAZVCgoZ1Vq EQDj4mQHzOsNB3xHtyIVB6qhblAh0c2unRaXV0gobjv8kFyYieU8AemcYajd0e92gFIZ RGa9D3/ExkIvh9nM1LwuoemzUmS/NFkGRtJY0HxMs4yxzdPN0Rl4Zt1XzpGViv9v9oQg 0Zqg== X-Gm-Message-State: AOJu0YwcsQCsfDNh0FqbDkPJSnDtxLGyKHWOT8md/5AqiA4Av166y485 tugr0GT20G6sXQbro3Uk+RP37WGtF/s= X-Google-Smtp-Source: AGHT+IFpih9Z9vT8sUuio8AoHsHY7LgwDONLxWX6M3YGCun8GaPaSikkBP793EQKjYr8xMVWxOyfyg== X-Received: by 2002:a17:902:cece:b0:1d0:8e08:6a with SMTP id d14-20020a170902cece00b001d08e08006amr1909281plg.6.1701883220395; Wed, 06 Dec 2023 09:20:20 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id p2-20020a170902eac200b001d0c41b1d03sm84652pld.32.2023.12.06.09.20.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:19 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 10E9D740638; Wed, 6 Dec 2023 09:20:11 -0800 (PST) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH 15/17] x86/cet: Don't disable CET if not single threaded Date: Wed, 6 Dec 2023 09:20:08 -0800 Message-ID: <20231206172010.1023415-16-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=-3025.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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+incoming=patchwork.ozlabs.org@sourceware.org In permissive mode, don't disable IBT nor SHSTK when dlopening a legacy shared library if not single threaded since IBT and SHSTK may be still enabled in other threads. Other threads with IBT or SHSTK enabled will crash when calling functions in the legacy shared library. Instead, an error will be issued. --- sysdeps/x86/dl-cet.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/sysdeps/x86/dl-cet.c b/sysdeps/x86/dl-cet.c index 7f37244d37..c6c407d7f7 100644 --- a/sysdeps/x86/dl-cet.c +++ b/sysdeps/x86/dl-cet.c @@ -20,6 +20,7 @@ #include #include #include +#include /* GNU_PROPERTY_X86_FEATURE_1_IBT and GNU_PROPERTY_X86_FEATURE_1_SHSTK are defined in , which are only available for C sources. @@ -204,7 +205,10 @@ dl_cet_check_dlopen (struct link_map *m, struct dl_cet_info *info) && (info->feature_1_legacy & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0) { - if (info->enable_ibt_type != cet_permissive) + /* Don't disable IBT if not single threaded since IBT may be still + enabled in other threads. */ + if (info->enable_ibt_type != cet_permissive + || !SINGLE_THREAD_P) { legacy_obj = info->feature_1_legacy_ibt; msg = N_("rebuild shared object with IBT support enabled"); @@ -220,7 +224,10 @@ dl_cet_check_dlopen (struct link_map *m, struct dl_cet_info *info) && (info->feature_1_legacy & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0) { - if (info->enable_shstk_type != cet_permissive) + /* Don't disable SHSTK if not single threaded since SHSTK may be + still enabled in other threads. */ + if (info->enable_shstk_type != cet_permissive + || !SINGLE_THREAD_P) { legacy_obj = info->feature_1_legacy_shstk; msg = N_("rebuild shared object with SHSTK support enabled"); From patchwork Wed Dec 6 17:20:09 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: 1872909 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=QB/LWqFG; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Slkjj6PpLz23nD for ; Thu, 7 Dec 2023 04:22:17 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C76A8384DEC0 for ; Wed, 6 Dec 2023 17:22:01 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by sourceware.org (Postfix) with ESMTPS id 1B65F3857B86 for ; Wed, 6 Dec 2023 17:20:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1B65F3857B86 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 1B65F3857B86 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1029 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883231; cv=none; b=pZLr0SGfCL1C495ncOnlISw7wgToZLj1wRuQ+lICHGadwNV4SP7Eu5X9I+rUJzqeiXJ6rmD0K5p/c33Wc1n2RbTRcJxqehphEbRmwPmGvbyCeP8kcmauC+0V+XwcUJQOeMPUSi4KqPvyihWq/oYkmiqHb+unqfRgVwqwOIZSwiY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883231; c=relaxed/simple; bh=gR6QyUCQ5OMmT+W9sFsE9mMHpSS82h6yuYpb3Zra2wg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=TEHWj2lTcok1qAbGQeqBs3BVYG97vMJN/4fPswIuBOUFxdvFcvYLq6ltG2on+fTuotKRGL7yA+IGbSeZskZm2P1i4GWrcTUaP4Wjz+a+r0Mo0KYG9QCKhADxxsO47U3s2cnunnSdY59oH4p+zYyuEjIwAjVQ09P7Js3gLIMNpXU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-2865742e256so55179a91.0 for ; Wed, 06 Dec 2023 09:20:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883220; x=1702488020; 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=02wFt/hupQSOa0L6pMUGdQzAi2lHuhmlk5WjJ8O6HvU=; b=QB/LWqFGHFp6OnXal188yPCR1fAA6UJXUD4+op1fecj/AavC9fY12b5hDFNAWXjITx Y0S12u/TLv+Cvfw1BJZys3aARTKmENos541L3r3szAtibwT+/KyrJv/NQGVhBeTs8m+a B+mhhRgw3pQOF489J+goyP8cpSPPyuNxLgoJcEQuSg2chHccUZpWq2JNv9MOVPaX1sso dp2VNsYLyh5lelTkgYi4hIAqKMJuQEGKpSuW7/I3T6ugy78Xi2fk28p2iEB5iL9ayTln mAvMI9UoX4MiO/OEP1w4OZzoQZz0NhzwwQa1DIti2qi4MoM1Sr7qAlJaB4PcUAZgwpfi IFQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883220; x=1702488020; 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=02wFt/hupQSOa0L6pMUGdQzAi2lHuhmlk5WjJ8O6HvU=; b=jOvO21qPJYPKnyCLN9nVAH5KhNf8EcbS7YjOBc184Mgm2IhW2PeDVYOhgQYskoMLmr dTb+dxm62cr6+LeuWYNvbMcG70Jl7lvpD8AgoA98TxCgiKpYV87BVLw9WPq8oAYDvk75 FpQB2BFbhOSkBBOYxixiYK4IzBd2lNQE3Tb4qg4GUwpbqhdwPLfXQbRla9AfH3OdwPMO +/s0wh3HSJ2yXzY127o34SC+1ELpg2SLC6zuZgvm5vkOVct0t3669mB/KCHPBUTyWBsx Pt+JFXSe4XwfXJtoFFLgyPuQpGNix53YmtX9EChnpagDUJu2L0dRL06scSifxv2wJ890 bunw== X-Gm-Message-State: AOJu0YzQU+CWjH6GMEJ8sibTzjSw53VZ9wKc5YyXs7Y6/VBI8vi7c+Tb BWayru9NyKng8kqQ1r610Igr//VJut0= X-Google-Smtp-Source: AGHT+IHv7o/sb5wsLAJpfL6g5Kl1luFouIz41qUYzfZqRDI6s6qaTFH7kg6tHLeBTbSr1xHk5UHI+Q== X-Received: by 2002:a17:90a:d44f:b0:286:9ff9:ab9a with SMTP id cz15-20020a17090ad44f00b002869ff9ab9amr898995pjb.66.1701883220070; Wed, 06 Dec 2023 09:20:20 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id g14-20020a17090a300e00b002802a080d1dsm82842pjb.16.2023.12.06.09.20.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:19 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 1D574740639; Wed, 6 Dec 2023 09:20:11 -0800 (PST) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH 16/17] x86/cet: Don't set CET active by default Date: Wed, 6 Dec 2023 09:20:09 -0800 Message-ID: <20231206172010.1023415-17-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=-3025.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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+incoming=patchwork.ozlabs.org@sourceware.org Not all CET enabled applications and libraries have been properly tested in CET enabled environments. Some CET enabled applications or libraries will crash or misbehave when CET is enabled. Don't set CET active by default so that all applications and libraries will run normally regardless of whether CET is active or not. Shadow stack can be enabled by $ export GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK at run-time if shadow stack can be enabled by kernel. NB: This commit can be reverted if it is OK to enable CET by default for all applications and libraries. --- sysdeps/x86/cpu-features.c | 2 +- sysdeps/x86/cpu-tunables.c | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c index 097868c1d9..80a07ac589 100644 --- a/sysdeps/x86/cpu-features.c +++ b/sysdeps/x86/cpu-features.c @@ -110,7 +110,7 @@ update_active (struct cpu_features *cpu_features) if (!CPU_FEATURES_CPU_P (cpu_features, RTM_ALWAYS_ABORT)) CPU_FEATURE_SET_ACTIVE (cpu_features, RTM); -#if CET_ENABLED +#if CET_ENABLED && 0 CPU_FEATURE_SET_ACTIVE (cpu_features, IBT); CPU_FEATURE_SET_ACTIVE (cpu_features, SHSTK); #endif diff --git a/sysdeps/x86/cpu-tunables.c b/sysdeps/x86/cpu-tunables.c index 5697885226..8f4f25efb0 100644 --- a/sysdeps/x86/cpu-tunables.c +++ b/sysdeps/x86/cpu-tunables.c @@ -34,6 +34,18 @@ break; \ } +#define CHECK_GLIBC_IFUNC_CPU_BOTH(f, cpu_features, name, \ + disable, len) \ + _Static_assert (sizeof (#name) - 1 == len, #name " != " #len); \ + if (memcmp (f, #name, len) == 0) \ + { \ + if (disable) \ + CPU_FEATURE_UNSET (cpu_features, name) \ + else \ + CPU_FEATURE_SET_ACTIVE (cpu_features, name) \ + break; \ + } + /* Disable a preferred feature NAME. We don't enable a preferred feature which isn't available. */ #define CHECK_GLIBC_IFUNC_PREFERRED_OFF(f, cpu_features, name, len) \ @@ -149,11 +161,14 @@ TUNABLE_CALLBACK (set_hwcaps) (tunable_val_t *valp) } break; case 5: + { + CHECK_GLIBC_IFUNC_CPU_BOTH (n, cpu_features, SHSTK, disable, + 5); + } if (disable) { CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, LZCNT, 5); CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, MOVBE, 5); - CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, SHSTK, 5); CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, SSSE3, 5); CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, XSAVE, 5); } From patchwork Wed Dec 6 17:20:10 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: 1872906 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=UrO8pf/d; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SlkjB33mLz23nD for ; Thu, 7 Dec 2023 04:21:50 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A5590384577C for ; Wed, 6 Dec 2023 17:21:38 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id 343983857024 for ; Wed, 6 Dec 2023 17:20:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 343983857024 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 343983857024 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883225; cv=none; b=kWV79wcGSKrX08v0D8R7/AeNlcn6P2EvLVVOHfSdYehHpzA1ZCwGw/J0KC3rwXHXe2evwkPYnpLqgjWDWI+O6MgPCp5CFfXZ6X8M40GQLumYRM0L5Gxp8eJevxOvra0HdwnYiaBmh+7u+bFjuENOG0Qfm7sPOZkvkN/PGNurzzY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883225; c=relaxed/simple; bh=mU5KLRZoKhW6c5jDodB0m8or6wHsex8YRLmpPWOiOis=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=PsTmzkgacLzJHvQLSzVKUq1RvEgmZlDBmOkRtisyJIukzEykkY+J5mYj5cw96NHf47IjoW7blLnMKYw0DwyazLONsmT0z1HPhfnnGUSkwc7h4kUE6d4Xrv9whJIw/L/zGFrsWhKWtgXVL53WahhGzvn9XiiGicPOWZ4GwBN0yLI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1d0a7b72203so31661145ad.2 for ; Wed, 06 Dec 2023 09:20:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883221; x=1702488021; 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=skZ/+g/hO7te3JUAeA58eZbxee3M9jFlS6cpuh+IUJ0=; b=UrO8pf/dxNXFqoNphCTgoYoHMzbIi3e9GDC9A+rpu5KfvdumKvfmq2PiqmtliE6Zy5 hAOP43WsrpUpr4XZZOy5fcVT9WzyvH+S/T8C92/T0yb66E+4dpmPQM4QKjK+d+iVRQGt arvsDqcYxXdutVMTt6C/GhVcwbaT/hrZKMPVuHYp5SUHCE82NiZzxrJ0uPKySOK6clRj 4pmXTHPcdB8o803I5MYRPYgTVJTYYhmxXytKQ1XTRS/2uY/AZQR4TgJQjyd0k2b5zeDl fK3xT1jQDO510PTIhWi2W3gAD8h1Nagp91EJRuOOBjOZXjIBIGkMwaWq2CZ0Tl/E+rZr 8dYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883221; x=1702488021; 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=skZ/+g/hO7te3JUAeA58eZbxee3M9jFlS6cpuh+IUJ0=; b=hp67JS+iwLqoy5NhCRB7V5Eq2J8EORgflm5aSFDuNHbKdXLZlsCnvzYWQFQJyaUPyI nrmBgxfWTUi0sRVwZzoDvOMm4xQ0M2J5qYl9JZi5fP1ilcsexoWZhdID+QUw1hovDE1k 0Kc9e1i/rHhPVuKRLl4302wxokAOQ33rP+PEY/hyMYQ1Bg6GwQQ7Wl6sZO2iY6AVZJDO uDV/0VthoIdLA0g0SfbCkuUZ4UbFPOh0Qub/162Ow3WAbTu9CK1WU8poh6E9/tcwNj01 WxKTK31grytyYzietw91bywHb56kMahJ89Y0PA2AiJrIBtoeJytk5/7NK8NtLiZSBuA7 6ACQ== X-Gm-Message-State: AOJu0YyNw1orFBSHzhSnaVpWx0TtJSFadlR+hS2E1Q3MICo/wZqExVvb 6lp+wb0HH/XiMlCsEyw3VRL1/HovG3Y= X-Google-Smtp-Source: AGHT+IGcCG0pkrJ+B1z6Z6hkIluWElku9ObUBf42dXteI2qizswxPc9zoIri4u5gWaapQ5wfz+XKog== X-Received: by 2002:a17:903:41d1:b0:1d0:c1ea:d3ae with SMTP id u17-20020a17090341d100b001d0c1ead3aemr1284715ple.99.1701883221298; Wed, 06 Dec 2023 09:20:21 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id f11-20020a170902ce8b00b001d0855ce7c8sm64765plg.252.2023.12.06.09.20.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:19 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 2A22374063A; Wed, 6 Dec 2023 09:20:11 -0800 (PST) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH 17/17] x86/cet: Run some CET tests with shadow stack Date: Wed, 6 Dec 2023 09:20:10 -0800 Message-ID: <20231206172010.1023415-18-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=-3025.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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+incoming=patchwork.ozlabs.org@sourceware.org When CET is disabled by default, run some CET tests with shadow stack enabled using $ export GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK --- sysdeps/x86/Makefile | 14 ++++++++++++++ sysdeps/x86/tst-shstk-legacy-1e-static.sh | 1 + sysdeps/x86/tst-shstk-legacy-1e.sh | 1 + sysdeps/x86/tst-shstk-legacy-1g.sh | 1 + 4 files changed, 17 insertions(+) diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile index 6ceefe16c7..906f158d69 100644 --- a/sysdeps/x86/Makefile +++ b/sysdeps/x86/Makefile @@ -202,6 +202,13 @@ CFLAGS-tst-cet-legacy-10-static.c += -mshstk CFLAGS-tst-cet-legacy-10a.c += -fcf-protection=none CFLAGS-tst-cet-legacy-10a-static.c += -fcf-protection=none +tst-cet-legacy-4-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK +tst-cet-legacy-6-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK +tst-cet-legacy-10-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK +tst-cet-legacy-10-static-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK +tst-cet-legacy-10a-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK +tst-cet-legacy-10a-static-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK + CFLAGS-tst-shstk-legacy-1a.c += -fcf-protection=none CFLAGS-tst-shstk-legacy-1a-static.c += -fcf-protection=none CFLAGS-tst-shstk-legacy-1d.c += -fcf-protection=none @@ -241,14 +248,20 @@ 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 +tst-shstk-legacy-1a-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK +tst-shstk-legacy-1a-static-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=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 +tst-shstk-legacy-1b-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK +tst-shstk-legacy-1b-static-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK $(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 tst-shstk-legacy-1c-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=-SHSTK tst-shstk-legacy-1c-static-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=-SHSTK $(objpfx)tst-shstk-legacy-1c: $(objpfx)tst-shstk-legacy-1-extra.o $(objpfx)tst-shstk-legacy-1c-static: $(objpfx)tst-shstk-legacy-1-extra.o +tst-shstk-legacy-1d-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK +tst-shstk-legacy-1d-static-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK $(objpfx)tst-shstk-legacy-1d: $(objpfx)tst-shstk-legacy-1-extra.o $(objpfx)tst-shstk-legacy-1d-static: $(objpfx)tst-shstk-legacy-1-extra.o $(objpfx)tst-shstk-legacy-1e: $(objpfx)tst-shstk-legacy-1-extra.o @@ -262,6 +275,7 @@ $(objpfx)tst-shstk-legacy-1e-static.out: \ $(objpfx)tst-shstk-legacy-1e-static $(SHELL) $< $(common-objpfx) 2> $@; \ $(evaluate-test) +tst-shstk-legacy-1f-ENV = GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK $(objpfx)tst-shstk-legacy-1f: $(objpfx)tst-shstk-legacy-mod-1.so $(objpfx)tst-shstk-legacy-mod-1.so: \ $(objpfx)tst-shstk-legacy-mod-1.os \ diff --git a/sysdeps/x86/tst-shstk-legacy-1e-static.sh b/sysdeps/x86/tst-shstk-legacy-1e-static.sh index e943aec70e..008c50dae3 100755 --- a/sysdeps/x86/tst-shstk-legacy-1e-static.sh +++ b/sysdeps/x86/tst-shstk-legacy-1e-static.sh @@ -20,6 +20,7 @@ common_objpfx=$1; shift +GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK \ ${common_objpfx}elf/tst-shstk-legacy-1e-static # The exit status should only be unsupported (77) or segfault (139). status=$? diff --git a/sysdeps/x86/tst-shstk-legacy-1e.sh b/sysdeps/x86/tst-shstk-legacy-1e.sh index b0467aa899..82f2acbf75 100755 --- a/sysdeps/x86/tst-shstk-legacy-1e.sh +++ b/sysdeps/x86/tst-shstk-legacy-1e.sh @@ -21,6 +21,7 @@ common_objpfx=$1; shift test_program_prefix=$1; shift +GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK \ ${test_program_prefix} \ ${common_objpfx}elf/tst-shstk-legacy-1e # The exit status should only be unsupported (77) or segfault (139). diff --git a/sysdeps/x86/tst-shstk-legacy-1g.sh b/sysdeps/x86/tst-shstk-legacy-1g.sh index c112bf6d8d..261eef7cac 100755 --- a/sysdeps/x86/tst-shstk-legacy-1g.sh +++ b/sysdeps/x86/tst-shstk-legacy-1g.sh @@ -21,6 +21,7 @@ common_objpfx=$1; shift test_program_prefix=$1; shift +GLIBC_TUNABLES=glibc.cpu.hwcaps=SHSTK \ ${test_program_prefix} \ ${common_objpfx}elf/tst-shstk-legacy-1g # The exit status should only be unsupported (77) or segfault (139).