From patchwork Sun Jul 28 19:03:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1965748 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ZL0gcd7T; 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 4WX9v36VjYz1ybX for ; Mon, 29 Jul 2024 05:06:07 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1C795385DC1B for ; Sun, 28 Jul 2024 19:06: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.133.124]) by sourceware.org (Postfix) with ESMTP id 5B9FB385842A for ; Sun, 28 Jul 2024 19:03:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5B9FB385842A 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 5B9FB385842A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722193417; cv=none; b=piZaiiv3u8A7p/b2dTQazy6VLWK9x/uaIYB2XTnOvhtuqfjI4/BA+UQscf2J/9hSYqWXSzLIHw5kHt88ATLohycMMAzSzTwUmeh1A5v9JbF0dote01bdCRN1biKNGGQGS7KQaqAi76YJ44ovKqTRkaiJ8RztPFMsBTPb0BDL6r0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722193417; c=relaxed/simple; bh=1HSA3xATEnNNS402giZmTNYscXmDzmEHF1P2vhuxji4=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=sGlt2xoPZAqJmghdvWuGxnK9ez3mljCbtQERwvuC7oH4vp4pRHse7LHQb1LevgCNvA/f/Sk1Dt7LcZmNjS2DMuvFaAEGXK4JJePaXD4DPK+DDDr5caTm/IbhYXbi2iKtoPvAHlt4qaZfA+ADUxJqMjTGPacv2ZrxRYD848qhRNM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722193415; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=D/SARHqkLmt7r/W0RZUy0k9xjdht7+Q20DZTyStvT6c=; b=ZL0gcd7Tjs/gnB+vfJ8rCgHqM5My9LIIxyIokUX7dShHJjphYVNLZLhy6RlwZiJzU6iBm8 RclERXIArTF/G8qhtmWaVv1JUHyiwj1eAv/6PRKHwwUJTc4hdjoXWuDPVlVfQHNndye5jU LD3kF1QiL1s1icMImvTDonNxi8zPZ/4= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-638-hgQ3-VadMFWS-uuZKl1U1w-1; Sun, 28 Jul 2024 15:03:33 -0400 X-MC-Unique: hgQ3-VadMFWS-uuZKl1U1w-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 942EB19560A2 for ; Sun, 28 Jul 2024 19:03:32 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.224.24]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 32DB71955F40 for ; Sun, 28 Jul 2024 19:03:30 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 12/13] posix: Acquire the environment lock during fork In-Reply-To: Message-ID: <4896ad435ba63221db22c6430fbe99405e7753de.1722193092.git.fweimer@redhat.com> References: X-From-Line: 4896ad435ba63221db22c6430fbe99405e7753de Mon Sep 17 00:00:00 2001 Date: Sun, 28 Jul 2024 21:03:28 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.9 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_H4, 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~incoming=patchwork.ozlabs.org@sourceware.org This makes setenv, unsetenv etc. usable after fork with the builtin-malloc (avoid deadlocks). Deadlocks may still occur with replacement mallocs. If this turns out to be a problem, a bit of refactoring should allow calling malloc without the environment lock in unsetenv. --- posix/fork.c | 9 +++++++++ stdlib/Makefile | 2 ++ stdlib/getenv.c | 2 ++ stdlib/setenv.c | 7 ++----- stdlib/setenv.h | 4 ++++ stdlib/tst-environ-fork.c | 38 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 stdlib/tst-environ-fork.c diff --git a/posix/fork.c b/posix/fork.c index 298765a1ff..75d0910a94 100644 --- a/posix/fork.c +++ b/posix/fork.c @@ -25,6 +25,7 @@ #include #include #include +#include static void fresetlockfiles (void) @@ -64,6 +65,10 @@ __libc_fork (void) _IO_list_lock (); + /* The setenv function acquires this lock, and then the malloc + locks. Use the same lock order here. */ + __libc_lock_lock (__environ_lock); + /* Acquire malloc locks. This needs to come last because fork handlers may use malloc, and the libio list lock has an indirect malloc dependency as well (via the getdelim @@ -87,6 +92,8 @@ __libc_fork (void) /* Release malloc locks. */ call_function_static_weak (__malloc_fork_unlock_child); + __libc_lock_unlock (__environ_lock); + /* Reset the file list. These are recursive mutexes. */ fresetlockfiles (); @@ -119,6 +126,8 @@ __libc_fork (void) /* Release malloc locks, parent process variant. */ call_function_static_weak (__malloc_fork_unlock_parent); + __libc_lock_unlock (__environ_lock); + /* We execute this even if the 'fork' call failed. */ _IO_list_unlock (); } diff --git a/stdlib/Makefile b/stdlib/Makefile index 5934b683b1..5613c34bcb 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -277,6 +277,7 @@ tests := \ tst-canon-bz26341 \ tst-cxa_atexit \ tst-environ \ + tst-environ-fork \ tst-getenv-signal \ tst-getenv-thread \ tst-getenv-unsetenv \ @@ -639,6 +640,7 @@ $(objpfx)tst-setcontext3.out: tst-setcontext3.sh $(objpfx)tst-setcontext3 $(objpfx)tst-qsort5: $(libm) +$(objpfx)tst-environ-fork: $(shared-thread-library) $(objpfx)tst-getenv-signal: $(shared-thread-library) $(objpfx)tst-getenv-thread: $(shared-thread-library) $(objpfx)tst-getenv-unsetenv: $(shared-thread-library) diff --git a/stdlib/getenv.c b/stdlib/getenv.c index ad7f0d00c8..c581ecf951 100644 --- a/stdlib/getenv.c +++ b/stdlib/getenv.c @@ -19,7 +19,9 @@ #include #include #include +#include +__libc_lock_define_initialized (, __environ_lock); struct environ_array *__environ_array_list; environ_counter __environ_counter; diff --git a/stdlib/setenv.c b/stdlib/setenv.c index 6bd0596b79..a97f6de4d4 100644 --- a/stdlib/setenv.c +++ b/stdlib/setenv.c @@ -55,11 +55,8 @@ extern char **environ; #endif #if _LIBC -/* This lock protects against simultaneous modifications of `environ'. */ -# include -__libc_lock_define_initialized (static, envlock) -# define LOCK __libc_lock_lock (envlock) -# define UNLOCK __libc_lock_unlock (envlock) +# define LOCK __libc_lock_lock (__environ_lock) +# define UNLOCK __libc_lock_unlock (__environ_lock) #else # define LOCK # define UNLOCK diff --git a/stdlib/setenv.h b/stdlib/setenv.h index 3e530f0bc0..1a184594ce 100644 --- a/stdlib/setenv.h +++ b/stdlib/setenv.h @@ -22,8 +22,12 @@ /* Not actually usable in rtld, but it is required for symbol discovery. */ #if IS_IN (libc) || IS_IN (rtld) # include +# include # include +/* This lock protects against simultaneous modifications of `environ'. */ +__libc_lock_define (extern, __environ_lock attribute_hidden); + /* We use an exponential sizing policy for environment arrays. The arrays are not deallocating during the lifetime of the process. This adds between one and two additional pointers per active diff --git a/stdlib/tst-environ-fork.c b/stdlib/tst-environ-fork.c new file mode 100644 index 0000000000..0e25b324e7 --- /dev/null +++ b/stdlib/tst-environ-fork.c @@ -0,0 +1,38 @@ +/* Test environment snapshots with fork. + 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 + . */ + +/* This test uses the process-creation skeleton, but does not performa + re-exec, so the use of PROGRAM is a bit misleading here. */ + +#define PROGRAM "tst-environ-fork" +#include "tst-environ-snapshot-skeleton.c" + +static pid_t +create_process (void) +{ + pid_t pid = xfork (); + if (pid == 0) + { + verify_environ (); + /* Verify that it is possible to obtain the environment lock. */ + TEST_COMPARE (setenv ("unrelated", "unrelated-set", 1), 0); + TEST_COMPARE_STRING (getenv ("unrelated"), "unrelated-set"); + _exit (0); + } + return pid; +}