From patchwork Sat Sep 7 12:58:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1982145 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=j3RqVs6O; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X1Cph1lRDz1y1H for ; Sat, 7 Sep 2024 22:59:07 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 924D73857011 for ; Sat, 7 Sep 2024 12:59:05 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id 968553858D35 for ; Sat, 7 Sep 2024 12:58:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 968553858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 968553858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::435 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725713932; cv=none; b=Rud2uCFvO8p3/l+s8M1EeGuyVur1vIA2IhbE8WOQWF8dL1WsaMUf2Rk7TCft2ybNFHzbaaaRF20ZWWvBe4rhisMImAtIuF6m+9vQiknUF0gWZ2mH+YENElNEB65k1LGXPqAJ/FVrg+mGzXUUc57Bn8fUte/kbIg76W+uVhHbnaY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725713932; c=relaxed/simple; bh=1/TfcRkdJp60DMC2twgHReyhsolYculORdJhY14Mt50=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=WIpkxz26pBA5XHA7wV4+nKJSWqURiCtxZw8jnc3ez9JyJoL21jnw+afcTEaLWdz/x2UVU+795GZgZ7MO5DzBSiHvPzL7CU7xSMu8kC4YWhsUazxpIIxPszWnKgGp3JjDhcNIPyF1I3et31CCdiozRuxZF1cWdgLiftHe2xtgfCc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-718da0821cbso1254592b3a.0 for ; Sat, 07 Sep 2024 05:58:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725713929; x=1726318729; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ZTFcr3vngx1WP+rzpdbLGvIzuqezH1zWs5vaoJPySwo=; b=j3RqVs6O4e7zFBV0BPT/rFcleWuon4t/ASYXfH97lITjg7kwumPJyMcGxwsaUq18is omuP73LhuFrxqIZXtweVj0Ki2locr3hSqknDutIwGnqUuX1idA9g7iOX7qSKeqSaZ/4w /E707G7LKRWt9N3TcV7wtFM7jaJM0K0M3jkXWoBSV4FUYj+p0TqtULGRxJ5O3ECsT/+b 0+tnA7E03qZvQQv6qjAlUGrRLgHGcDGL63XXGJssyn0iXg7dHt1kCCRwtl5d4c6ybrFV dp9nKkUL+kwIwToYIcLMyPBd/oZwd0QSqAnICoMVK7uWeWgPEjfH2fLSBjWHxc1tSFl/ FaiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725713929; x=1726318729; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZTFcr3vngx1WP+rzpdbLGvIzuqezH1zWs5vaoJPySwo=; b=lgD1zX7vP6JUwjf2iGo4XSDFwwh/aoP2rukkZa3HGN+QMiG66zVRKkLwHjAEb0rQrt rccrW0BXCb3kRVjIL+4Sc5cxnDFnLtHGW9wBXDNoXm0pVw8nRNx0O9CotX//mnUgLPGm 9eYJbXxh9CH718EFXQDQdIixHP34ON3ElFLltEdFIOrgnyGGy68GeLDyLIAz7Tfy/0RK 3/bxbvaIg/+JVmgTt+1XClY+yDofVhMSzdvGo68iCr+nVlXQmSz7rLe7ZDRW4RQBVgBP Khd8WRgnqyfOuwQ+F48+28grRxXniZqi8EEttBpeRGbkhBZeqvO0tl5fAS3MWLzadAHi Vyng== X-Gm-Message-State: AOJu0YzGiGPmGJBPLg0Enb/n3j6ka5RPd0JsPjRQVO65NOyjXDE0okWA rpFT5CAfr0hzEHnNI3x1fnJNKrS11RDosZ8uSvEcmv8KujVPEHImpRX0LD3l X-Google-Smtp-Source: AGHT+IEsUxj29QO2VznNci70TJP9YKsdJZ1NihstukayoZxJXNUQw2rbd5x3BXiMBJl+yyZhKl94UQ== X-Received: by 2002:a05:6a21:4a41:b0:1cf:2513:8a01 with SMTP id adf61e73a8af0-1cf2aca2c9cmr3325495637.26.1725713929234; Sat, 07 Sep 2024 05:58:49 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.59.129.220]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-718e58c8937sm846305b3a.58.2024.09.07.05.58.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Sep 2024 05:58:48 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 38A12740298; Sat, 7 Sep 2024 05:58:47 -0700 (PDT) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: ben@decadent.org.uk Subject: [PATCH v2] libio: Set _vtable_offset before calling _IO_link_in [BZ #32148] Date: Sat, 7 Sep 2024 05:58:47 -0700 Message-ID: <20240907125847.273272-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 X-Spam-Status: No, score=-3020.8 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_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham 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~incoming=patchwork.ozlabs.org@sourceware.org Since _IO_vtable_offset is used to detect the old binaries, set it in _IO_old_file_init_internal before calling _IO_link_in which checks _IO_vtable_offset. Add a glibc 2.0 test with copy relocation on _IO_stderr_@GLIBC_2.0 to verify that fopen won't cause memory corruption. This fixes BZ #32148. Signed-off-by: H.J. Lu --- libio/Makefile | 7 ++++ libio/oldfileops.c | 4 +- libio/tst-fopen-compat.c | 84 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 libio/tst-fopen-compat.c diff --git a/libio/Makefile b/libio/Makefile index 59f3ee0b7c..c93c4c59b8 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -287,11 +287,18 @@ endif ifeq ($(build-shared),yes) aux += oldfileops oldstdfiles tests += \ + tst-fopen-compat \ tst-stderr-compat \ # tests tests-2.0 += \ + tst-fopen-compat \ tst-stderr-compat \ # tests-2.0 + +tst-fopen-compat-ARGS = tst-fopen-compat.c +# Disable PIE to trigger copy relocation. +CFLAGS-tst-fopen-compat.c += -fno-pie +tst-fopen-compat-no-pie = yes endif shared-only-routines = oldiofopen oldiofdopen oldiofclose oldfileops \ diff --git a/libio/oldfileops.c b/libio/oldfileops.c index 97148dba9b..8f775c9094 100644 --- a/libio/oldfileops.c +++ b/libio/oldfileops.c @@ -103,9 +103,11 @@ _IO_old_file_init_internal (struct _IO_FILE_plus *fp) fp->file._old_offset = _IO_pos_BAD; fp->file._flags |= CLOSED_FILEBUF_FLAGS; - _IO_link_in (fp); + /* NB: _vtable_offset must be set before calling _IO_link_in since + _IO_vtable_offset is used to detect the old binaries. */ fp->file._vtable_offset = ((int) sizeof (struct _IO_FILE) - (int) sizeof (struct _IO_FILE_complete)); + _IO_link_in (fp); fp->file._fileno = -1; if (&_IO_stdin_used != NULL || !_IO_legacy_file ((FILE *) fp)) diff --git a/libio/tst-fopen-compat.c b/libio/tst-fopen-compat.c new file mode 100644 index 0000000000..fd0c9cbe10 --- /dev/null +++ b/libio/tst-fopen-compat.c @@ -0,0 +1,84 @@ +/* Verify that fopen works with copy relocation on _IO_stderr_ in binaries + linked with glibc 2.0. + Copyright (C) 2024 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 + . */ + +#include + +#if TEST_COMPAT (libc, GLIBC_2_0, GLIBC_2_1) +# define _LIBC +# define _IO_USE_OLD_IO_FILE +# include +# include +# include +# include +# include + +struct _IO_jump_t; + +struct _IO_FILE_plus +{ + FILE file; + const struct _IO_jump_t *vtable; +}; + +extern struct _IO_FILE_plus _IO_stderr_; +compat_symbol_reference (libc, _IO_stderr_, _IO_stderr_, GLIBC_2_0); +compat_symbol_reference (libc, fopen, fopen, GLIBC_2_0); +compat_symbol_reference (libc, fclose, fclose, GLIBC_2_0); + +static int +do_test (int argc, char *argv[]) +{ + static char filename[PATH_MAX + 1]; + struct stat st; + char *name = NULL; + int i; + + /* Try to trigger copy relocation. */ + TEST_VERIFY_EXIT (_IO_stderr_.file._fileno >= 0); + + for (i = 1; i < argc; i++) + { + name = argv[i]; + if (stat (name, &st) == 0) + { + TEST_VERIFY_EXIT (strlen (name) <= PATH_MAX); + break; + } + } + TEST_VERIFY_EXIT (name != NULL); + + strcpy (filename, name); + FILE *fp = fopen (filename, "r"); + TEST_VERIFY_EXIT (strcmp (filename, name) == 0); + TEST_VERIFY_EXIT (fp != NULL); + TEST_VERIFY_EXIT (fclose (fp) == 0); + return 0; +} +#else +# include + +static int +do_test (int argc, char *argv[]) +{ + return EXIT_UNSUPPORTED; +} +#endif + +#define TEST_FUNCTION_ARGV do_test +#include