From patchwork Thu Dec 7 21:05:39 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: 81711 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 57079384DEE6 for ; Thu, 7 Dec 2023 21:05:58 +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 AE5163858D3C for ; Thu, 7 Dec 2023 21:05:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AE5163858D3C 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 AE5163858D3C 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=1701983146; cv=none; b=QFhFjQVrCLOH659yElfu4bnuVgoRMZXXVsxS8uPV8tdFnmjEWVdTE2bp1MvaqXNSR/zqx28BR+CFxG5B6NcP7vTKZcmN5MzHDGWSo1We9OxCwKZ0BkCmsDT94U8P4lMbJvXGxW3fJ5HN3c63Qg9WTF7ANGmQQN2eRYcvIeTzqNk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701983146; c=relaxed/simple; bh=dULd9GzM1XgCXwIkb/YLe/rpOcT+ioN+FmEXyduUYiY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=kwwOtyRilS7/n0ij11NqeVxRPnJxu1paE+/IIlTHbDNXPq8ZneJWvLhZKPYn4XzXX75HBwp1Uf73MiXk8XhRAPDOciRc/VaAf9iCG7Qlc2sPIE4mqqOBOjbIqNxqXw/Vi3XZl2IBAd6Ih6DIw9N0ecU6n/e1agaXvWjnJW7tFag= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-6ceba6c4b8dso666318b3a.1 for ; Thu, 07 Dec 2023 13:05:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701983142; x=1702587942; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=2vjVugOhYDfsulJ/fZg2RiEMmfP1r78K2xJH/vIdDs0=; b=lxrsT+XAZUUqcJQ6ep/uy+gwmlMzlpHmH/onnTYB1CXUJ6n8tuIfn8fbq2rxYa1+aE rENwMr+x8WQAVsDbxGKd6o/IXS9GwigGGhfxQ6ZtRh+fvwgdoYi25EWbOtthKkc1dElN 0e2GS2OGABm9I/yMQbqohK2WJ+vdYrOOSUS38wZpCkkTL5rPJe332pKfFIBFBRb19W7r B+x7hOGtIwiujjOVl7fCSzqm7GiArjRaWcWWm7K+/iGWV3T0ZyoZWDfOgjuzTRws4rE0 oW4BmOpyRydxEjPTjadKc7HkbInyV6xmyww1pj9gdP3lKil7TtBo/qCouUkYFakSNkJm cf3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701983142; x=1702587942; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2vjVugOhYDfsulJ/fZg2RiEMmfP1r78K2xJH/vIdDs0=; b=YNlGzLA4oE7oS2u/24hh4IZgXsanY1svXOi+bbal8Mh3Pg/BwykO3/zDK3AIhbetgd jvYWhOM0p8/Lf0oLoYO5lcnvn1iDti4gykheAWB1pAxNdWQxcAD1SXY1RTsPA/OtEDHy zbpPbWURW+gaFhjQa9q0dQ/eWhld/1CTq6LLx4aMNHJh3BLh4eMQc9HUC8usJyF7J+Kw TBJjWbyoma6mBMEf/mx4WF31so8M5F1RwkP5gYlUGmB4w18KC3eBSqGsM1n+dSO6rzTR BIytTPyp/UkzgcFDvWChTyWTohJlbIDGp73ixRDdT+dubDJrPzrpZ8huISrghCCIVOAk 0q5A== X-Gm-Message-State: AOJu0YxFQDCfG/n70t3GBAmMnEvRu5SXa9TvDerFp6zliCczfSfqo3qo I9RdJMM/gU2gm7v8D/mSX0jYZlvgg7w= X-Google-Smtp-Source: AGHT+IHmzyO80YYbqSB4aD/oG1wXyDgr6mfg0omV7UBeGrELh9xot/l0SYEfnx1FofJl2xODg1hL3Q== X-Received: by 2002:a05:6a00:158d:b0:6ce:2732:577 with SMTP id u13-20020a056a00158d00b006ce27320577mr4104295pfk.40.1701983141666; Thu, 07 Dec 2023 13:05:41 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.129.147]) by smtp.gmail.com with ESMTPSA id cm6-20020a056a020a0600b005b92d3cb4c2sm178151pgb.58.2023.12.07.13.05.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 13:05:40 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 8DA5B7402D7; Thu, 7 Dec 2023 13:05:39 -0800 (PST) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: Noah Goldstein Subject: [PATCH] x86: Check PT_GNU_PROPERTY early Date: Thu, 7 Dec 2023 13:05:39 -0800 Message-ID: <20231207210539.2268470-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.43.0 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+patchwork=sourceware.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. Reviewed-by: Noah Goldstein --- 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 */