From patchwork Tue Oct 17 13:05:14 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: 734385 Delivered-To: patch@linaro.org Received: by 2002:adf:f0cd:0:b0:32d:baff:b0ca with SMTP id x13csp468835wro; Tue, 17 Oct 2023 06:07:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEsXHuATTQUbOsI+2rZzRFDFQz7iWvPifMN9EsK8OVFMvQhc11Wzo1vGfhueDm/QMDXFzhN X-Received: by 2002:a05:6102:3107:b0:457:ced4:414f with SMTP id e7-20020a056102310700b00457ced4414fmr2011521vsh.25.1697548029612; Tue, 17 Oct 2023 06:07:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697548029; cv=pass; d=google.com; s=arc-20160816; b=KWvTnR66NULTz+6r7ykccT/KdZ3Z2ebdx27axT+dEj9ErKeRy1IvqoeDy7ljZ6zGX9 bmtcAUAWO/k5hksKEfJoZU9xhhhiYnb+EbjZhWbahXdnfXktU0t561rJ1SCEx2QAP9b5 cuUk7Xk7IgD9c14WAwRqZpagY3wHhpTLjSW0xajA0QKTJcsvHAa6XYjmQA0hf9l6SnlD 32uHFuIfHJkDrgZsCirlR3iZiCsx0Nx/1q1701sWD5CTNcllNctH+bdzJruHZ2J/ggA4 IpwmBf2A8eSw79FGfTZiRGBz/OCN2Blu+sdMUtpnUI5/xB1M7OnRf7vZdxHJP6ub4V6Y l4uQ== 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=se9yeywrMEpcYKTZwOHIBRmngujNH3SnMnvIe1n7jvI=; fh=+FUb54tScwW7D3lvWhZcQBi30wyNNn2DusdH7ahfqKk=; b=DJRZbwChFddY1VyUiz3EWo9L157oK1N9+tyad2AvuRyMQ9+1RBSoo5NrTXasGIGMwN 9jVXH1kJ69WtwPqjxdltI+MAN2OpJEB8taNruNq/HcSDd51aenkB0o8fltpDvNnBAJea InG4X9et1kNQ4Y/OCNT519WdyW+EfoepNsS1DaoOQDjOv7Ac+rsRjWBX8r77DcSnWnDc 31OC1XwGqdsTEcWRO6dKGC/tma14eR4eeUhxYrx/YL3xhGKmJKSmpVRwEOaGHa+Pfhs4 ehfqBzwf6xl3iXFYmDE9k+ihyfKrWZ5BqvqFezTWq/8uVb8cfvbJKDO4yxKC7TSsTSAc QhEg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=DGb4iaLy; 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 gt23-20020a0561022a5700b0044d50589e8asi111483vsb.224.2023.10.17.06.07.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 06:07:09 -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=DGb4iaLy; 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 2B964385DC35 for ; Tue, 17 Oct 2023 13:07:06 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by sourceware.org (Postfix) with ESMTPS id 9B39A3856973 for ; Tue, 17 Oct 2023 13:06:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9B39A3856973 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 9B39A3856973 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::535 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697547992; cv=none; b=F3p0eAoi26D0ZU1Oy/aJLC/V7/ABJsM8hpYoL+Cutw3gsYjWiCemsiP6Qzm6X/WowtmIgBFJgBCBNqpIID5VZ1TOJuuBPY6+Lnp6LBJ9orhoEEiO0538XUDNTwqXfydwV3yJo8Q5VjABqybI8qARg6rP8qOfnmSCvUG3AhxgMPY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697547992; c=relaxed/simple; bh=vctkq8ApNE0MisixeiIabODyKumsSuPIHQcT8a8+LP0=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=SL/6qSG7vBdD/kawzAebHP9XhYjfle7JCfdTTH2pPFZabQNj/PaQI7Gvx6aOd+v/76OAaI6cyg+bJA2T1dL63dH65xxLYgoAvcScEWozBAhh4MO97vncVRPEBafuarMfIujosFHo7a1sjaRP9qitnEOtKmhtwOVwHsLwqVSa+Ag= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x535.google.com with SMTP id 41be03b00d2f7-577e62e2adfso3557417a12.2 for ; Tue, 17 Oct 2023 06:06:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1697547989; x=1698152789; 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=DGb4iaLyrRbQbsa9m7echGOMsAUdngAlsSA12AR6Yl9bfKmITzGwfFkVbJ5t+xbuhu EgUgRNBnifxvX5vz6ZUE3lV2KE4iGiX0zFogQsqUoT5LmxLOM/HDOo0NnAZ+ZCoSbOC9 5O7Siqo0YTD2hE/Marg8lmewX2O0gdBt0MUQ/hadUGysl7Ajy2E1VKUNCWaIIht2sZgL X8fV7Xk4ynQ9UfUcwpJuLSu/seYaANsgms5QE7ei4HGcWOpATtUktTD1L9ZKb12syvp2 X9/bIUDEU4ZJRXEccBArD5B44eORq1hkrHi9+Wl+bpjDTdttR6f47ziVX13a1kJFqk0o kRpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697547989; x=1698152789; 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=wEQ2U2JbPQu4Sor2HPBEoxJ8jvkGj4KCMH6gaCIt+q6RnU42OtGnvFfY2OoiCAQRHv Eu/Z2rUq5iM36kjfqXMVXA1fgJoFcH0IfSAgxZjfq9g0ELXccPFdC2+MObcwTNTsS/GA A3AR+5JhQ16dmnZrpqnn4Yr5NGXc/pbBYWWmeabybrMEaoCbBDFPw6uXfHrlWiWbozs9 UNMLnBCMv7RrFoTdcKEtTYuQ78+l4Dc376Qb8UcRsXz6H/UQAPn4UD//MQuIQQCytSUm Jpbc6I186RmqyNPl8luPniny9tzf0fwe4LOre8fNUTpFnffYJEqYJjX6SvdtOXjbBbYI qxlQ== X-Gm-Message-State: AOJu0YwoIvGhCfI1BrOaGRGTzUz+DdIOqfCyuRQvE4wlg39iajvHzngK 6ZNEadqPhrutPFNxn087Mh43emMR2JpIOhbmFRy2ew== X-Received: by 2002:a05:6a20:7346:b0:172:15c8:1fa2 with SMTP id v6-20020a056a20734600b0017215c81fa2mr2045323pzc.53.1697547988751; Tue, 17 Oct 2023 06:06:28 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:7f2e:11d:92b4:4d78:4197]) by smtp.gmail.com with ESMTPSA id l28-20020a635b5c000000b0056b6d1ac949sm1309788pgm.13.2023.10.17.06.06.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 06:06:28 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Siddhesh Poyarekar Subject: [PATCH v2 07/19] elf: Fix _dl_debug_vdprintf to work before self-relocation Date: Tue, 17 Oct 2023 10:05:14 -0300 Message-Id: <20231017130526.2216827-8-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231017130526.2216827-1-adhemerval.zanella@linaro.org> References: <20231017130526.2216827-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 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. Reviewed-by: Siddhesh Poyarekar --- 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