From patchwork Sun Jun 20 23:36:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 43923 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 41672385E83E for ; Sun, 20 Jun 2021 23:37:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 41672385E83E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1624232251; bh=wpEhIrpEMuU3z0Rmn8HpxmJIhbodh4q78kU7S211E84=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=V5K9twxZML6uNxUUYfeZcs0olDlQq62VqFGNWI/LPfydC6zUFX4DeYxXbyqYjD1qL TcAc7FVOFGoHf8+sLm1KfH0xrZJfJ7lmWFO/oIv9/0/jE4HB5Zn7u99cBBmxC1j7I0 0Qfbon9z7u4YWBfAtOf3O9Fl536Hk++K/ybSGmfg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by sourceware.org (Postfix) with ESMTPS id D4126385B804 for ; Sun, 20 Jun 2021 23:36:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D4126385B804 Received: by mail-pf1-x433.google.com with SMTP id y4so5269457pfi.9 for ; Sun, 20 Jun 2021 16:36:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wpEhIrpEMuU3z0Rmn8HpxmJIhbodh4q78kU7S211E84=; b=DBN7RAZB1KJbW0we0iWdPecSg8Hhcx4ziqBxS9APHItmQKNockpViB/tYdL+hqEzqD vdqG8bwaC41snVnKlvCbn3St6C31mVvUk1rqTDX4qPMNRqHpdLo74t2b6G4dmkIyVM9T +zIgaKrfFeDhvm5iHp56oWpn8yJ/0tM+PGBgonFIYHf8Lsb5ZglLXLNkR7+IC9UVXMTG WUq45XriywLGwaaVlsD250R68mrUXITB47D3ssoJ4MSM8YiCSmmAYaKzbyTSFnGdHIMU tQAJqg/GV6oGL9lbELeNyqI6zOPBWSU0Mwr5le1ZGnP7/GtTYmtWRBOqSO4VTpmlW4zs k9lA== X-Gm-Message-State: AOAM5301S4taYRcGGHslcY0ru+nSDv1bxyQaqfX7OYSnfR7qX4E7SudJ nvTeeObmtb3Av5nEBJ8BRtRiRkyIsMw= X-Google-Smtp-Source: ABdhPJzMOe+Rg0iQw1DPINZBp8Ghqk6as6dUieywAOqrTb/cNmenZMg5l4ztXf8PlpUK6307c6ymoQ== X-Received: by 2002:a63:1141:: with SMTP id 1mr14040247pgr.217.1624232182417; Sun, 20 Jun 2021 16:36:22 -0700 (PDT) Received: from gnu-cfl-2.localdomain ([172.56.39.115]) by smtp.gmail.com with ESMTPSA id x1sm1552969pfn.198.2021.06.20.16.36.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Jun 2021 16:36:22 -0700 (PDT) Received: from gnu-cfl-2.. (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id 4B94FC058F for ; Sun, 20 Jun 2021 16:36:20 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 3/4] Add run-time chesk for single global definition Date: Sun, 20 Jun 2021 16:36:19 -0700 Message-Id: <20210620233620.391576-4-hjl.tools@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210620233620.391576-1-hjl.tools@gmail.com> References: <20210620233620.391576-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3032.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_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" When performing symbol lookup for references in an object without single global definition: 1. Disallow copy relocations against protected data symbols in an object with single global definition. 2. Disallow non-zero symbol values of undefined function symbols, which are used as the function pointer, against protected function symbols in an object with single global definition. --- elf/dl-lookup.c | 5 ++++ sysdeps/generic/dl-protected.h | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 sysdeps/generic/dl-protected.h diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c index eea217eb28..430359af39 100644 --- a/elf/dl-lookup.c +++ b/elf/dl-lookup.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -527,6 +528,10 @@ do_lookup_x (const char *undef_name, uint_fast32_t new_hash, if (__glibc_unlikely (dl_symbol_visibility_binds_local_p (sym))) goto skip; + if (ELFW(ST_VISIBILITY) (sym->st_other) == STV_PROTECTED) + _dl_check_protected_symbol (undef_name, undef_map, ref, map, + type_class); + switch (ELFW(ST_BIND) (sym->st_info)) { case STB_WEAK: diff --git a/sysdeps/generic/dl-protected.h b/sysdeps/generic/dl-protected.h new file mode 100644 index 0000000000..a530eb3e51 --- /dev/null +++ b/sysdeps/generic/dl-protected.h @@ -0,0 +1,51 @@ +/* Support for STV_PROTECTED visibility. Generic version. + Copyright (C) 2021 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 + . */ + +#ifndef _DL_PROTECTED_H +#define _DL_PROTECTED_H + +static inline void __attribute__ ((always_inline)) +_dl_check_protected_symbol (const char *undef_name, + const struct link_map *undef_map, + const ElfW(Sym) *ref, + const struct link_map *map, + int type_class) +{ + if (undef_map != NULL + && !(undef_map->l_1_needed + & GNU_PROPERTY_1_NEEDED_SINGLE_GLOBAL_DEFINITION) + && (map->l_1_needed + & GNU_PROPERTY_1_NEEDED_SINGLE_GLOBAL_DEFINITION)) + { + if ((type_class & ELF_RTYPE_CLASS_COPY)) + /* Disallow copy relocations against protected data symbols in + an object with single global definition. */ + _dl_fatal_printf ("copy relocation against non-copyable protected symbol=%s in file=%s\n", + undef_name, DSO_FILENAME (map->l_name)); + else if (ref->st_value != 0 + && ref->st_shndx == SHN_UNDEF + && (type_class & ELF_RTYPE_CLASS_PLT)) + /* Disallow non-zero symbol values of undefined symbols, which + are used as the function pointer, against protected function + symbols in an object with single global definition. */ + _dl_fatal_printf ("non-canonical reference to canonical protected function symbol=%s in file=%s\n", + undef_name, DSO_FILENAME (map->l_name)); + } +} + +#endif /* _DL_PROTECTED_H */