From patchwork Mon Sep 9 21:46:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 97352 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 B1D513858282 for ; Mon, 9 Sep 2024 21:48:06 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id E213C3858D39 for ; Mon, 9 Sep 2024 21:47:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E213C3858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E213C3858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725918465; cv=none; b=eaIUGH75O+ffi410alyXNkiltba2wFkkIzoe0fkGRHP9qJT70q59S4uKjrS8rilxixXm4v9Gh+4PhpqThzn4bco9ikKwo+B+gL1JGcroPz2plfpVB8ztroq9gaj2YX/vu06H9BGcrQPOCHYiljTc98IEAOLvuqGKqAQFJ67BoLo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725918465; c=relaxed/simple; bh=otxzZOtAeaSK/Lm8NBlS8RFq87DwmobkowNefatXTEw=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Z7BGwMtb+xBRVmcPG36z7gkzP4bNbjDK/7qOuHhZsvlotxqXGMpfr8R3lyfuzjYuSLBCwlfxoDFWNOITrJLF/IP1+KLxzxkFBDLa566eAs+EDhV4DqL7ljhJjnjoBjqXNdAjj/bHfjofz9z4XeSBaLscK+rlbRejT5NEP5HcdYs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725918462; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=X0hdbage524pwC4ouR5MyCfGsOFjnEUu3PP5SeRbx9Q=; b=GcaOEnfIPTjc1sF4HY/d3/kuYIor8M9WgOdLXHIbm/VU7FNDkBIC8NN4BEoURR5bxvukaB CmfgJjaXnsiGFNZsoWzkeR5OjE+7Pvb0Ht9JZTBMfTnUjqtHpg5BRgj2GXC5IfOyghjdFM nCRJi0UfTjcLkb0S1UoIXafz9bck9V0= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-113-F50YnOUgP9qwHMPueG7Knw-1; Mon, 09 Sep 2024 17:47:40 -0400 X-MC-Unique: F50YnOUgP9qwHMPueG7Knw-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-42cb080ab53so16233515e9.0 for ; Mon, 09 Sep 2024 14:47:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725918459; x=1726523259; h=mime-version:message-id:subject:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=X0hdbage524pwC4ouR5MyCfGsOFjnEUu3PP5SeRbx9Q=; b=IGEZ0iR0OR9S1/udU2qllBnqpCNOTSFELsXY0RktVfq8YAygooScuBkJya3bBZzrE0 IrqnnIcSyC1vYAGvBu69umVvJ3+eoVxBQaPjgmgJubkYiUwSFDWHKQMt5MkCwA2YiJq6 t+ZDX7lqVXWw307MCaTxEPjSv3t03V7+oNx+MdwCBgJl5K3hZQOL/K7cQxwayn+6ZxEr tO7GTOcsbWnRKHn//B6gr7I65cWXk2EHJ0uu1h/ZNBaUPm4z5XIDbUCUkuQfXkt+1c9N UX2ZDsgMaacW/4a9F91+LxfTVhu/caeWcpkqMIbO+bjWdkXS5aMtnd5VFg8e0hTWQ21t LuMg== X-Gm-Message-State: AOJu0YzfIw73Fw6HM9LurLlppP3sc3PtFsSlTupnCYtxGvqpCsV5XcP/ hw9jfyVN9/iUPBjhrGN304nMhHIaptRIZOfoahCXaW03NmcMmuYjIFsADXpB+ZrcfSsJuMMfjp8 VTsj/kJ/KtRZO/+g5Li5ANzd0ujuwummBkuwE2FhMPlOVm7JXZwhmf799/UJujz1v5X0ItR+j17 eOnCkLRwIEIl/bG8SYg2tRsw4VN1BjscNn/IH8r0GkZw== X-Received: by 2002:a5d:5d84:0:b0:374:c84d:1cfe with SMTP id ffacd0b85a97d-3789f86f674mr3197680f8f.21.1725918459014; Mon, 09 Sep 2024 14:47:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGQc0+8/8I4fcZgQb3+t1WUO5JPFfWQPeQ3Qe3SkqAmqVIm5Fcc5vIEGNWz5zjNh/s2UTyvUg== X-Received: by 2002:a5d:5d84:0:b0:374:c84d:1cfe with SMTP id ffacd0b85a97d-3789f86f674mr3197659f8f.21.1725918457970; Mon, 09 Sep 2024 14:47:37 -0700 (PDT) Received: from digraph.polyomino.org.uk (digraph.polyomino.org.uk. [2001:8b0:bf73:93f7::51bb:e332]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-378956650fdsm7022092f8f.25.2024.09.09.14.47.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2024 14:47:37 -0700 (PDT) Received: from jsm28 (helo=localhost) by digraph.polyomino.org.uk with local-esmtp (Exim 4.95) (envelope-from ) id 1snmDr-00G4Gp-Lz for libc-alpha@sourceware.org; Mon, 09 Sep 2024 21:46:59 +0000 Date: Mon, 9 Sep 2024 21:46:59 +0000 (UTC) From: Joseph Myers To: libc-alpha@sourceware.org Subject: Add freopen special-case tests: chroot, EFBIG Message-ID: <7a39f01b-c931-8b25-d08a-74247185a197@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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~patchwork=sourceware.org@sourceware.org Add tests of special cases for freopen that were omitted from the more general tests of different modes and similar issues. The special cases in the two tests here are logically unconnected, it was simply convenient to put both tests in one patch. * Test freopen with a NULL path to the new file, in a chroot. Rather than asserting that this fails (logically, failure in this case is an implementation detail; it's not required for freopen to rely on /proc), verify that either it fails (without memory leaks) or that it succeeds and behaves as expected on success. There is no check for file descriptor leaks because the machinery for that also depends on /proc, so can't be used in a chroot. * Test that freopen and freopen64 are genuinely different in configurations with 32-bit off_t by checking for an EFBIG trying to write past 2GB in a file opened with freopen in such a configuration but no error with 64-bit off_t or when opening with freopen64. Tested for x86_64 and x86. diff --git a/stdio-common/Makefile b/stdio-common/Makefile index ce7f7cdd3b..9d0ba0763a 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -219,8 +219,11 @@ tests := \ tst-fphex-wide \ tst-freopen2 \ tst-freopen3 \ + tst-freopen4 \ + tst-freopen5 \ tst-freopen64-2 \ tst-freopen64-3 \ + tst-freopen64-4 \ tst-fseek \ tst-fwrite \ tst-fwrite-memstrm \ @@ -324,8 +327,11 @@ ifneq ($(PERL),no) tests-special += \ $(objpfx)tst-freopen2-mem.out \ $(objpfx)tst-freopen3-mem.out \ + $(objpfx)tst-freopen4-mem.out \ + $(objpfx)tst-freopen5-mem.out \ $(objpfx)tst-freopen64-2-mem.out \ $(objpfx)tst-freopen64-3-mem.out \ + $(objpfx)tst-freopen64-4-mem.out \ $(objpfx)tst-getline-enomem-mem.out \ $(objpfx)tst-getline-mem.out \ $(objpfx)tst-printf-bz18872-mem.out \ @@ -341,10 +347,16 @@ generated += \ tst-freopen2.mtrace \ tst-freopen3-mem.out \ tst-freopen3.mtrace \ + tst-freopen4-mem.out \ + tst-freopen4.mtrace \ + tst-freopen5-mem.out \ + tst-freopen5.mtrace \ tst-freopen64-2-mem.out \ tst-freopen64-2.mtrace \ tst-freopen64-3-mem.out \ tst-freopen64-3.mtrace \ + tst-freopen64-4-mem.out \ + tst-freopen64-4.mtrace \ tst-getline-enomem-mem.out \ tst-getline-enomem.mtrace \ tst-getline-mem.out \ @@ -476,6 +488,15 @@ tst-freopen3-ENV = \ tst-freopen64-3-ENV = \ MALLOC_TRACE=$(objpfx)tst-freopen64-3.mtrace \ LD_PRELOAD=$(common-objpfx)malloc/libc_malloc_debug.so +tst-freopen4-ENV = \ + MALLOC_TRACE=$(objpfx)tst-freopen4.mtrace \ + LD_PRELOAD=$(common-objpfx)malloc/libc_malloc_debug.so +tst-freopen64-4-ENV = \ + MALLOC_TRACE=$(objpfx)tst-freopen64-4.mtrace \ + LD_PRELOAD=$(common-objpfx)malloc/libc_malloc_debug.so +tst-freopen5-ENV = \ + MALLOC_TRACE=$(objpfx)tst-freopen5.mtrace \ + LD_PRELOAD=$(common-objpfx)malloc/libc_malloc_debug.so $(objpfx)tst-unbputc.out: tst-unbputc.sh $(objpfx)tst-unbputc $(SHELL) $< $(common-objpfx) '$(test-program-prefix)'; \ diff --git a/stdio-common/tst-freopen4-main.c b/stdio-common/tst-freopen4-main.c new file mode 100644 index 0000000000..e169442cf4 --- /dev/null +++ b/stdio-common/tst-freopen4-main.c @@ -0,0 +1,100 @@ +/* Test freopen in chroot. + 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 +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +int +do_test (void) +{ + mtrace (); + char *temp_dir = support_create_temp_directory ("tst-freopen4"); + FILE *fp; + int ret; + + /* These chroot tests verify that either reopening a renamed or + deleted file works even in the absence of /proc, or that it fails + (without memory leaks); thus, for example, such reopening does + not crash in the absence of /proc. */ + + support_become_root (); + if (!support_can_chroot ()) + return EXIT_UNSUPPORTED; + xchroot (temp_dir); + + /* Test freopen with NULL, renamed file. This verifies that + reopening succeeds (and resets the file position indicator to + start of file) even when the original path could no longer be + opened, or fails without a memory leak. (It is not possible to + use to test for file descriptor leaks + here, because that also depends on /proc.) */ + + verbose_printf ("testing freopen with NULL, renamed file\n"); + fp = xfopen ("/file1", "w+"); + ret = fputs ("file has been renamed", fp); + TEST_VERIFY (ret >= 0); + ret = rename ("/file1", "/file1a"); + TEST_COMPARE (ret, 0); + fp = FREOPEN (NULL, "r+", fp); + if (fp != NULL) + { + puts ("freopen of renamed file succeeded"); + TEST_COMPARE_FILE_STRING (fp, "file has been renamed"); + xfclose (fp); + } + else + puts ("freopen of renamed file failed (OK)"); + ret = rename ("/file1a", "/file1"); + TEST_COMPARE (ret, 0); + + /* Test freopen with NULL, deleted file. This verifies that + reopening succeeds (and resets the file position indicator to + start of file) even when the original path could no longer be + opened, or fails without a memory leak. */ + + verbose_printf ("testing freopen with NULL, deleted file\n"); + fp = xfopen ("/file1", "r+"); + ret = fputs ("file has now been deleted", fp); + TEST_VERIFY (ret >= 0); + ret = remove ("/file1"); + TEST_COMPARE (ret, 0); + fp = FREOPEN (NULL, "r+", fp); + if (fp != NULL) + { + puts ("freopen of deleted file succeeded"); + TEST_COMPARE_FILE_STRING (fp, "file has now been deleted"); + xfclose (fp); + } + else + puts ("freopen of deleted file failed (OK)"); + + free (temp_dir); + return 0; +} + +#include diff --git a/stdio-common/tst-freopen4.c b/stdio-common/tst-freopen4.c new file mode 100644 index 0000000000..24c5cc5f85 --- /dev/null +++ b/stdio-common/tst-freopen4.c @@ -0,0 +1,3 @@ +#define FREOPEN freopen +#define OTHER_FREOPEN freopen64 +#include diff --git a/stdio-common/tst-freopen5.c b/stdio-common/tst-freopen5.c new file mode 100644 index 0000000000..f32626bccf --- /dev/null +++ b/stdio-common/tst-freopen5.c @@ -0,0 +1,144 @@ +/* Test freopen and freopen64 with large offsets. + 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 +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#define START_TEST(DESC) \ + do \ + { \ + fds = support_descriptors_list (); \ + verbose_printf (DESC); \ + } \ + while (0) + +#define END_TEST \ + do \ + { \ + support_descriptors_check (fds); \ + support_descriptors_free (fds); \ + } \ + while (0) + +int +do_test (void) +{ + mtrace (); + struct support_descriptors *fds; + FILE *fp; + int ret; + + char *temp_dir = support_create_temp_directory ("tst-freopen5"); + /* This file is removed at the end of each test rather than left + around between tests to avoid problems with subsequent tests + reopening it as a large (2GB + 1 byte) file. */ + char *file1 = xasprintf ("%s/file1", temp_dir); + + /* fopen with freopen64: large offsets OK. */ + START_TEST ("testing fopen with freopen64\n"); + fp = fopen ("/dev/null", "r"); + TEST_VERIFY_EXIT (fp != NULL); + fp = freopen64 (file1, "w", fp); + TEST_VERIFY_EXIT (fp != NULL); + setbuf (fp, NULL); + ret = fseeko64 (fp, 1LL << 32, SEEK_SET); + TEST_COMPARE (ret, 0); + ret = fputc ('x', fp); + TEST_COMPARE (ret, 'x'); + xfclose (fp); + ret = remove (file1); + TEST_COMPARE (ret, 0); + END_TEST; + + /* fopen64 with freopen64: large offsets OK. */ + START_TEST ("testing fopen64 with freopen64\n"); + fp = fopen64 ("/dev/null", "r"); + TEST_VERIFY_EXIT (fp != NULL); + fp = freopen64 (file1, "w", fp); + TEST_VERIFY_EXIT (fp != NULL); + setbuf (fp, NULL); + ret = fseeko64 (fp, 1LL << 32, SEEK_SET); + TEST_COMPARE (ret, 0); + ret = fputc ('x', fp); + TEST_COMPARE (ret, 'x'); + xfclose (fp); + ret = remove (file1); + TEST_COMPARE (ret, 0); + END_TEST; + + /* fopen with freopen: large offsets not OK on 32-bit systems. */ + START_TEST ("testing fopen with freopen\n"); + fp = fopen ("/dev/null", "r"); + TEST_VERIFY_EXIT (fp != NULL); + fp = freopen (file1, "w", fp); + TEST_VERIFY_EXIT (fp != NULL); + setbuf (fp, NULL); + ret = fseeko64 (fp, 1LL << 32, SEEK_SET); + TEST_COMPARE (ret, 0); + errno = 0; + ret = fputc ('x', fp); + if (sizeof (off_t) == 4) + { + TEST_COMPARE (ret, EOF); + TEST_COMPARE (errno, EFBIG); + } + else + TEST_COMPARE (ret, 'x'); + fclose (fp); + ret = remove (file1); + TEST_COMPARE (ret, 0); + END_TEST; + + /* fopen64 with freopen: large offsets not OK on 32-bit systems. */ + START_TEST ("testing fopen64 with freopen\n"); + fp = fopen64 ("/dev/null", "r"); + TEST_VERIFY_EXIT (fp != NULL); + fp = freopen (file1, "w", fp); + TEST_VERIFY_EXIT (fp != NULL); + setbuf (fp, NULL); + ret = fseeko64 (fp, 1LL << 32, SEEK_SET); + TEST_COMPARE (ret, 0); + errno = 0; + ret = fputc ('x', fp); + if (sizeof (off_t) == 4) + { + TEST_COMPARE (ret, EOF); + TEST_COMPARE (errno, EFBIG); + } + else + TEST_COMPARE (ret, 'x'); + fclose (fp); + ret = remove (file1); + TEST_COMPARE (ret, 0); + END_TEST; + + free (temp_dir); + free (file1); + return 0; +} + +#include diff --git a/stdio-common/tst-freopen64-4.c b/stdio-common/tst-freopen64-4.c new file mode 100644 index 0000000000..be985f7618 --- /dev/null +++ b/stdio-common/tst-freopen64-4.c @@ -0,0 +1,3 @@ +#define FREOPEN freopen64 +#define OTHER_FREOPEN freopen +#include