From patchwork Mon Nov 6 20:25:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 741457 Delivered-To: patch@linaro.org Received: by 2002:adf:fd90:0:b0:32d:baff:b0ca with SMTP id d16csp1294558wrr; Mon, 6 Nov 2023 12:26:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IEPF5Ks5CGzosfGwA0LwJCBaw7m943r6rz4FmLLleA6AfKqTKI6udMfP+GV6Am4QZQhcuvR X-Received: by 2002:a0d:e694:0:b0:589:e7c1:96f2 with SMTP id p142-20020a0de694000000b00589e7c196f2mr11653350ywe.46.1699302413964; Mon, 06 Nov 2023 12:26:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1699302413; cv=pass; d=google.com; s=arc-20160816; b=0UQ/dT64RjQVvQPLPvGjsnCtNiR3p9P1piWXvBdIL1tdHpEX0ADaM6HdqEs/v2L/dC RPY5K3qL1QIF9ggLxYEXAupLzV2Q1yf5in6G4qfjq7qkMAFDrgyrgRcHITpSk/S46Sq/ vPktuPWtdOwSeAAKz21qkuieNFXf5JMGobiP+tQyoFatLFAG7zGswHQf56atB6xIPZ5f 1BhSIBWRTK/9FC0/LffsVT+hUlnXNsSUFEdqEEFAqmOWJ/jOR/orkLHN3yjWgXD2fJ74 zXtx37OQ2LYXg/oFP2RXH2sHmkv+f1HF/YCnbZmu9zbifz/jcLuxrN6bmp/46jUbnNis G2GQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=m3Ws7f5PM27Oh7b/DP8x28r02VFmx16EapZknjfk7a8=; fh=+FUb54tScwW7D3lvWhZcQBi30wyNNn2DusdH7ahfqKk=; b=v2cJdIX9mL2G0ESkQr/F6L1yz4rVKqLVA2h2Jkq5WWU637v+HLNYKGzbDtcclDC7MT Ztxa+N98vanney/T9pxZvA6zcynKPFtPNvutWxxaz8vAT6i0YA09Zc7cvOEdpaupKaij +1xdWS/rx4acl/Ygz2Wwqn0A0hJNMNlg8K7NrnuVuf0faiF7hHYCTidIi1Ou38rH2/0k fM2Oj+P3hsIjQhERfSTTkyG/q4Osic0/NPuIcWtnUQAXrD42RS3M44n6B4qTL05MLhKj /OFnfQYrgZMmlIfCP5aZVUHXb+eF+v0Z6d6i/Kfq2G8mbc8P9IkKdWZkuKbX4u9DJV0q 423Q== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="Pt1/aa/L"; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id u17-20020a0cee91000000b0066d10e104cfsi5980491qvr.347.2023.11.06.12.26.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 12:26:53 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="Pt1/aa/L"; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3109B385C6E0 for ; Mon, 6 Nov 2023 20:26:53 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yw1-x1136.google.com (mail-yw1-x1136.google.com [IPv6:2607:f8b0:4864:20::1136]) by sourceware.org (Postfix) with ESMTPS id 04AA43857342 for ; Mon, 6 Nov 2023 20:26:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 04AA43857342 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 04AA43857342 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1136 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699302374; cv=none; b=txRwh+C17Ootd+XicXh9sOyFicz86I9fTOY7mNLr9b9P+Vx447GzFTfpJvpArprWMT6f/Aa957h26iSmQ3qLPXDGB3nQq6RXyXe1pRps2fBdHltoDgqIIXrXOJcd7AgfVMhhEagWHa2E3sINWTISfzSzE+LPmyHwG7TqRjP/agY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699302374; c=relaxed/simple; bh=GHAHXQGW8DqZSnhM9/SOgbbbUHiQk3AC8tGx9TXTpFg=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=GvGP2QCgbf+pPtJtrffXHPn5OmcdfR9jnGcR83NTRJBrqv7zHj6+UGn+tYQ1ZfRAY5Gqbeeih+jNFluO086NQHqvRVLblMEE1Y3CI9ocB5t7I81kysaASB6fOw/eubJ7l4oFlLpXUIRuRYuyM9dz7cOXSu2NvBX3ilJ0BLd0Wh0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yw1-x1136.google.com with SMTP id 00721157ae682-5a7af52ee31so57797167b3.2 for ; Mon, 06 Nov 2023 12:26:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1699302372; x=1699907172; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=m3Ws7f5PM27Oh7b/DP8x28r02VFmx16EapZknjfk7a8=; b=Pt1/aa/LgRnDEMKwJw/D7owkl1P/9NGQoVZfil9hvVlpL/ZkitSB2B1bplFEtF/d0v hht32IAtaGNpnRnrpWXfZCZsaT/MjXi5uF+vi+b5qd/uJd0iWV39bygUqg2FvDrMzNUt Pl/E2Ud/lcrj5R1+eok59NJNlpGzc/IJ0jZjifqOJky4X/vbZiWt2gnbsjI7+z+RCFcl XfCwiMh97yYNM4GMNTCu/J8Fi/tLYBqAOzOBdbpWpec8Og9hyMnJBbWRTz9jzU6jCiGg yIRCZTdxXyYmOLm946W98LhH/3sgVHDQKlXtL/YUtQ8ZU/Dg8gOxC8FEm/8SWauAONQZ 4oCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699302372; x=1699907172; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=m3Ws7f5PM27Oh7b/DP8x28r02VFmx16EapZknjfk7a8=; b=T4STwmFF1v+kWGwEQUlq8Dux+IlvrMmdk+W+/MjB2RhU8eJcC1eSh43eVtT062sa8/ BQI/ujq6RUT41Dddijlu10TaGA2G0ObarkGYtvapcZYLjpbJn1YJgwr59PE+nxero5HF INIp2KciTLeVe26QcVzp9cGNC1jxiHDbUMXpxOk1IP1q0ZRdTDKDFL/O7OGJcOyQFVKp VKXfe6slXhz8STlh8Y+dfSxTblC5+TSgzC9Rq55yRKtx9TH1r9SASrX+GOJhC+Y8wsKt UsaqXvIz2/1luT3BGk2W1d2doRlyakTo5l0ewWGPTKIWFUg/XkHnvU9a6/2OQi/7zMJf 7YlA== X-Gm-Message-State: AOJu0Yy6K2PJtBG9fjguAgmjaEA1KbX+G/7NgKc2TrzMRsRSajxONTtZ x2ggGi80pLdsYFqLzgcd7A6n1/TJf56xnqPdT6dR0Q== X-Received: by 2002:a81:570c:0:b0:5a7:b942:c3fe with SMTP id l12-20020a81570c000000b005a7b942c3femr11015788ywb.16.1699302371890; Mon, 06 Nov 2023 12:26:11 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:a715:c1a0:7281:6384:2ee9]) by smtp.gmail.com with ESMTPSA id ci7-20020a05690c0a8700b005a7b8fddfedsm4707154ywb.41.2023.11.06.12.26.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 12:26:11 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Siddhesh Poyarekar Subject: [PATCH v3 07/19] elf: Fix _dl_debug_vdprintf to work before self-relocation Date: Mon, 6 Nov 2023 17:25:40 -0300 Message-Id: <20231106202552.3404059-8-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231106202552.3404059-1-adhemerval.zanella@linaro.org> References: <20231106202552.3404059-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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+patch=linaro.org@sourceware.org The strlen might trigger and invalid GOT entry if it used before the process is self-relocated (for instance on dl-tunables if any error occurs). For i386, _dl_writev with PIE requires to use the old 'int $0x80' syscall mode because the calling the TLS register (gs) is not yet initialized. Checked on x86_64-linux-gnu. Reviewed-by: Siddhesh Poyarekar --- elf/dl-printf.c | 16 ++++++++++++++-- stdio-common/Makefile | 5 +++++ stdio-common/_itoa.c | 5 +++++ sysdeps/unix/sysv/linux/i386/dl-writev.h | 24 ++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 sysdeps/unix/sysv/linux/i386/dl-writev.h diff --git a/elf/dl-printf.c b/elf/dl-printf.c index 6efb4c019a..5e93208535 100644 --- a/elf/dl-printf.c +++ b/elf/dl-printf.c @@ -17,6 +17,10 @@ License along with the GNU C Library; if not, see . */ +#include +#if BUILD_PIE_DEFAULT +# pragma GCC visibility push(hidden) +#endif #include <_itoa.h> #include #include @@ -25,11 +29,19 @@ #include #include #include -#include #include #include #include +/* The function might be called before the process is self-relocated. */ +static size_t +_dl_debug_strlen (const char *s) +{ + const char *p = s; + for (; *s != '\0'; s++); + return s - p; +} + /* Bare-bones printf implementation. This function only knows about the formats and flags needed and can handle only up to 64 stripes in the output. */ @@ -193,7 +205,7 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg) case 's': /* Get the string argument. */ iov[niov].iov_base = va_arg (arg, char *); - iov[niov].iov_len = strlen (iov[niov].iov_base); + iov[niov].iov_len = _dl_debug_strlen (iov[niov].iov_base); if (prec != -1) iov[niov].iov_len = MIN ((size_t) prec, iov[niov].iov_len); ++niov; diff --git a/stdio-common/Makefile b/stdio-common/Makefile index bacb795fed..e88a9cea29 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -460,6 +460,11 @@ CFLAGS-isoc23_scanf.c += -fexceptions CFLAGS-dprintf.c += $(config-cflags-wno-ignored-attributes) +# Called during static library initialization, so turn stack-protection +# off for non-shared builds. +CFLAGS-_itoa.o = $(no-stack-protector) +CFLAGS-_itoa.op = $(no-stack-protector) + # scanf18.c and scanf19.c test a deprecated extension which is no # longer visible under most conformance levels; see the source files # for more detail. diff --git a/stdio-common/_itoa.c b/stdio-common/_itoa.c index 3037b0f529..48f2903ecb 100644 --- a/stdio-common/_itoa.c +++ b/stdio-common/_itoa.c @@ -16,6 +16,11 @@ License along with the GNU C Library; if not, see . */ +/* Mark symbols hidden in static PIE for early self relocation to work. + Note: string.h may have ifuncs which cannot be hidden on i686. */ +#if BUILD_PIE_DEFAULT +# pragma GCC visibility push(hidden) +#endif #include #include #include diff --git a/sysdeps/unix/sysv/linux/i386/dl-writev.h b/sysdeps/unix/sysv/linux/i386/dl-writev.h new file mode 100644 index 0000000000..624d0e46b0 --- /dev/null +++ b/sysdeps/unix/sysv/linux/i386/dl-writev.h @@ -0,0 +1,24 @@ +/* Message-writing for the dynamic linker. Linux/i386 version. + Copyright (C) 2013-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 + . */ + +#if BUILD_PIE_DEFAULT +/* Can't use "call *%gs:SYSINFO_OFFSET" during startup in static PIE. */ +# define I386_USE_SYSENTER 0 +#endif + +#include