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: 77433 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 A0A10388214C for ; Tue, 10 Oct 2023 18:02:39 +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-Google-Smtp-Source: AGHT+IFnI8SuGQaZIeTJXfAR27xqNlN3twMR+HvpkRcjFf4475toyGWUj6v/ROoq1fPQRBqZvXPifg== 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+patchwork=sourceware.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