From patchwork Fri Dec 10 02:40:54 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: 48744 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 060573857C43 for ; Fri, 10 Dec 2021 02:41:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 060573857C43 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1639104083; bh=xWi/CvkAhiMNY/CcgzzIG3WxbIkuI5atLHWmXP64ZSU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=K0Id+G/s7ldI4FVTUf/nF0/LD+3GS0PQTpEjXflE4JtzLJmC4//XbSJ807GD77bJE rafwzhJ8qutvQBZGkEPQpJSY8qKZqv7flOy3RiBfsPbf1j036rKClGTjBkqHeJY3VC hQTX3y0SJp7uIfu29jinEwZCy10fDw4T3zWoLLKE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id 83C6E3858403 for ; Fri, 10 Dec 2021 02:41:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 83C6E3858403 Received: by mail-pl1-x62d.google.com with SMTP id o14so5324638plg.5 for ; Thu, 09 Dec 2021 18:41:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=xWi/CvkAhiMNY/CcgzzIG3WxbIkuI5atLHWmXP64ZSU=; b=g6pUOAxd0QaG4yjYe6UnBtMxasCOTouDsI74hsg9iAFUPdRm6V2dx/3869Pte6XkTD oqY1f1OyR//7muQS0DZu3Zs9ajOsYFPu3buLALnWELRj/Je+zGXbfcKofF5t+u+v2OTq aoxT8o5QmC4gEZwVkLdC7PpZM8JsoGp1r3Iz/x/yBwBbum0R7LfGiDLH/oZUIyCO8gw+ yq3zw3L4I5VSo410hAWWXTpz8kz6Bf63McKEr/X3brztCIMoHJWdRlF6YgOu20Ndi3UP YcwTutplZV3Xp6IaERzVy1D7sXraP8ufY0WElTGj+rUVukYl5fCKlTDKG3zKplzyha67 KdrQ== X-Gm-Message-State: AOAM53305iMGhR0NtTFSQacORGE8RE4qzTVfiFlS6beHMtNOpfOW+SV3 DJ3kme9vVYfGyPU73YLYR6MFqwut0CA= X-Google-Smtp-Source: ABdhPJwy3YvoCznVq/2WQimzkdvvPUOBmV+DHc0WzNhsMIfDW6hNN8RKUjfdh7jZvXAQ39qEkgTjzw== X-Received: by 2002:a17:902:e88f:b0:141:f982:777 with SMTP id w15-20020a170902e88f00b00141f9820777mr73668838plg.68.1639104059209; Thu, 09 Dec 2021 18:40:59 -0800 (PST) Received: from gnu-cfl-2.localdomain ([172.58.35.133]) by smtp.gmail.com with ESMTPSA id o17sm842746pgb.42.2021.12.09.18.40.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Dec 2021 18:40:58 -0800 (PST) Received: from gnu-tgl-2.localdomain (gnu-tgl-2 [192.168.1.34]) by gnu-cfl-2.localdomain (Postfix) with ESMTPS id B980C42096C for ; Thu, 9 Dec 2021 18:40:57 -0800 (PST) Received: from gnu-tgl-2.. (localhost [IPv6:::1]) by gnu-tgl-2.localdomain (Postfix) with ESMTP id 162823003D5 for ; Thu, 9 Dec 2021 18:40:54 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2] elf: Stop with zero entry point value [BZ #28453] Date: Thu, 9 Dec 2021 18:40:54 -0800 Message-Id: <20211210024054.3570891-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.33.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3029.4 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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" Changes in the v2 patch: 1. Use rtld_progname in the error message. Stop with zero entry point value unless we are tracing shared objects since a zero entry point value in the ELF header indicates there is no associated entry point. Now we get $ ./elf/ld.so /lib64/libstdc++.so.6.0.29 ./elf/ld.so: cannot execute '/lib64/libstdc++.so.6.0.29' without entry point $ instead of $ /lib64/ld-linux-x86-64.so.2 /lib64/libstdc++.so.6.0.29 Segmentation fault (core dumped) $ This fixes [BZ #28453]. Reviewed-by: Siddhesh Poyarekar --- elf/Makefile | 10 ++++++++++ elf/rtld.c | 8 ++++++++ elf/tst-rtld-run-dso.sh | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100755 elf/tst-rtld-run-dso.sh diff --git a/elf/Makefile b/elf/Makefile index ef36008673..1832dfa537 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -50,6 +50,10 @@ ifeq (yesyes,$(build-shared)$(run-built-tests)) tests-special += $(objpfx)list-tunables.out endif +ifeq (yes,$(build-shared)) +tests-special += $(objpfx)tst-rtld-run-dso.out +endif + # Make sure that the compiler does not insert any library calls in tunables # code paths. ifeq (yes,$(have-loop-to-function)) @@ -1877,6 +1881,12 @@ $(objpfx)list-tunables.out: tst-rtld-list-tunables.sh $(objpfx)ld.so $(objpfx)/tst-rtld-list-tunables.out > $@; \ $(evaluate-test) +$(objpfx)tst-rtld-run-dso.out: tst-rtld-run-dso.sh $(objpfx)ld.so \ + $(objpfx)testobj1.so + $(SHELL) tst-rtld-run-dso.sh $(objpfx)ld.so $(objpfx)testobj1.so \ + '$(test-wrapper-env)' '$(run_program_env)' > $@ + $(evaluate-test) + tst-dst-static-ENV = LD_LIBRARY_PATH='$$ORIGIN' $(objpfx)tst-rtld-help.out: $(objpfx)ld.so diff --git a/elf/rtld.c b/elf/rtld.c index 6ce1e07dc0..b4373ba193 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -1424,6 +1424,14 @@ dl_main (const ElfW(Phdr) *phdr, implementations which has no real free() function it does not makes sense to free the old string first. */ main_map->l_name = (char *) ""; + + /* Stop if there is no associated entry point and we are not + tracing shared objects. */ + if (main_map->l_entry == main_map->l_addr + && state.mode != rtld_mode_trace) + _dl_fatal_printf("%s: cannot execute '%s' without entry point\n", + ld_so_name, rtld_progname); + *user_entry = main_map->l_entry; /* Set bit indicating this is the main program map. */ diff --git a/elf/tst-rtld-run-dso.sh b/elf/tst-rtld-run-dso.sh new file mode 100755 index 0000000000..5192f64210 --- /dev/null +++ b/elf/tst-rtld-run-dso.sh @@ -0,0 +1,33 @@ +#!/bin/sh +# Test for ld.so on a shared library with no associated entry point. +# 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 +# . + +set -e + +rtld=$1 +dso=$2 +test_wrapper_env=$3 +run_program_env=$4 + +LC_ALL=C +export LC_ALL + +${test_wrapper_env} \ +${run_program_env} \ +$rtld $dso 2>&1 \ +| grep "cannot execute"