From patchwork Tue Oct 10 18:01:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 731465 Delivered-To: patch@linaro.org Received: by 2002:ab3:7922:0:b0:23f:8cfb:5ad7 with SMTP id j2csp2263359ltl; Tue, 10 Oct 2023 11:02:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IELBAU34vgsIeYGSC0gc/WcjoE8US4v65yFiHUYL4W4Oo43wAwRZiZ5UDMDnMBZEO8WNH6X X-Received: by 2002:a17:907:b1b:b0:9b9:ff16:344c with SMTP id h27-20020a1709070b1b00b009b9ff16344cmr12370468ejl.50.1696960957466; Tue, 10 Oct 2023 11:02:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696960957; cv=none; d=google.com; s=arc-20160816; b=MRc/u4iqjz4h5P+vLFoC21HLaj3DgpvprPiYf5VaCRtN4BlrIkw/8vJQ7CbnOfcweZ 1w9Ez08zxetVd0VJ+Ml8hL6fJqQqjiBy1mPB3J6+/Cu4zVLaKSuCiHQNQEgA9Dlf5ik3 2nzRxfc/UALrHnlRu6dNeI0+sMAjpTf1B9vPGZKDyEi6Ad2UcsMCPCqnYHzcY6DWy27c FXTfPPG48+fym9aqUgK2dM6Xz6qIHAnblxj4zcDk9Qx04JCACkWrpUV/mkjhajSLooYS SdyEd88Zz2wauxRZkLwMm7ia1AksfehNvXnUwIkfn45TFs2OvzVYUsa1nl9ILlbQ7nkK +KaQ== ARC-Message-Signature: i=1; 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:dmarc-filter:delivered-to; bh=se9yeywrMEpcYKTZwOHIBRmngujNH3SnMnvIe1n7jvI=; fh=+FUb54tScwW7D3lvWhZcQBi30wyNNn2DusdH7ahfqKk=; b=bnYVTYu98E/75ZCsQ7UZ36DpMpnfEtCy85JsIkyMekR+U5n59/BQPnUN8yBLYa/IFS GgK3jwUUePv7p2l+FYHChMGOid1oWIcTnKp6QrArypZmLQ5ps9hdVM4GX/XDQ4zBq2RI sDlZSuqTPsK4TQvSCtCejNT+UuWb5epdU/wnzetsiG6grHCpBnEKhDpBvIqbcXkXePqC 1dTOP8TJHkmPhfC40zifpwpjyDdPPa+DRfsCQhYpGoVI4j++RSok9EFOX6Z26Tacy+A6 dMBzDms7oMhzl2T0GJEkoAtISSgCrzLeK7rXOWMPPL6e3dj9Ge2G+CirSA2UxcynchpK 7XEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=oReW9NiU; 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 a14-20020a1709065f8e00b0099bd56894d8si5677948eju.71.2023.10.10.11.02.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 11:02:37 -0700 (PDT) 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=oReW9NiU; 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 95928385C422 for ; Tue, 10 Oct 2023 18:02:31 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by sourceware.org (Postfix) with ESMTPS id 20522385B522 for ; Tue, 10 Oct 2023 18:01:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 20522385B522 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1c87a85332bso49453575ad.2 for ; Tue, 10 Oct 2023 11:01:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696960892; x=1697565692; 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=se9yeywrMEpcYKTZwOHIBRmngujNH3SnMnvIe1n7jvI=; b=oReW9NiUqXL1pcN7+6Dph1WrK61yF4LOg3KYPOFYM7DFJYn4vZWpc2W5/6qlhQ7U28 Agrs4vE8cLom8AARm+HL/0wmBX5UWbE9Y3Cd6+1guSs47TvEOjdut6ZRN5xkbXLbEzKQ 0Xx1zA8rzPmJvvGXHFah8wJ8kS1WVv7FoE0CZl8BteutakKjt88/KhewpfrKNyzkCmvi 3aYam2N+16bhzoh+YnU36jzOzHFz+f3pTyR0LjEyLHrunsvJUkrlxdDhfpNFzfrcF86k 1/Uw4cCs/HhfMs36JNDwdctyBarCYlEufP5y14CeQqKL9cTgBkFH8qXuOuP0T2Sgz3FS unRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696960892; x=1697565692; 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=se9yeywrMEpcYKTZwOHIBRmngujNH3SnMnvIe1n7jvI=; b=WfOCMieuHh5vZB+c3755q4ZRzZZE/q3dufptbqJ/8NFRP1jsWoi1X1CJRttLMuulls OFSW6VUR8RLnag4ncwYEd5iiMMh1Nxs+MN76eoJezh7vxGYC6jWh7O5Z0qhGmplcSBjK xTNzGtwWHdZ2C93ZA/MzZiIaKvnpP+rpzk0XGMZXKclFjtvn3jotKtTdz2HxmsYSu7/j auKamuMS9DPTliKxh2XgAvxPXyW8oYm9bM0Wk5xRrz3FZS/bvRT80at4QWWw1vjnN9nb Rhmxe/7csrkRt1Ho4FETJGlApbaBG4cGY+e0ordZJ//kOKGL+KMdc3J4kyockNCTltd3 Ba+w== X-Gm-Message-State: AOJu0YwXA36njdb+567MVIMsqAv5qKw2ej6q0p9Im61aDM8SbQSfVAVQ mUTk5nIib7I30+4Wj2YWwtJNTiZ3Dz2e82pYUiyA1w== X-Received: by 2002:a17:903:120b:b0:1bf:2e5c:7367 with SMTP id l11-20020a170903120b00b001bf2e5c7367mr21623418plh.42.1696960892581; Tue, 10 Oct 2023 11:01:32 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:d09b:ef2e:7c42:5ecf:a4ef]) by smtp.gmail.com with ESMTPSA id 5-20020a170902c24500b001bb9d6b1baasm12088022plg.198.2023.10.10.11.01.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 11:01:31 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Siddhesh Poyarekar Subject: [PATCH 07/11] elf: Fix _dl_debug_vdprintf to work before self-relocation Date: Tue, 10 Oct 2023 15:01:07 -0300 Message-Id: <20231010180111.561793-8-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231010180111.561793-1-adhemerval.zanella@linaro.org> References: <20231010180111.561793-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 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 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). Checked on x86_64-linux-gnu. --- elf/dl-printf.c | 16 ++++++++++++++-- stdio-common/Makefile | 5 +++++ stdio-common/_itoa.c | 5 +++++ 3 files changed, 24 insertions(+), 2 deletions(-) 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