From patchwork Sun Jul 28 19:03:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1965752 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=KVXbNA6e; 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 4WX9wc2qXKz1ybX for ; Mon, 29 Jul 2024 05:07:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 931BB385840B for ; Sun, 28 Jul 2024 19:07:26 +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 B7FBE385C6CA for ; Sun, 28 Jul 2024 19:03:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B7FBE385C6CA 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 B7FBE385C6CA 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=1722193414; cv=none; b=CgkL5R399xd6EREvquk8Vi/8JUsXPhkNF9chx5xlp0aby7v0VsROniTF42C0MjJzjqTTz/3bdX90z6GneqJds1CkDsck41fmKTYBRDNGLG36W7rtJsCqB+mJWHPv/EhX7ZBaRz3nt4k5khBPVbGOXVsOwYYBnlh1k0cvfV5QCfU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722193414; c=relaxed/simple; bh=5chBUYmcr+mJBOn9KElLZsJZpZJDKbl82bb4UG1YLJk=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=GEL0J1Ec+mOtGRY+wDyJXGp8AnOhZrlkatrQ/L3Bzni8tzpehdLJRzM+mEp0aM4NHZWTq0Ec5hPYlJ9lx4jVMI+WxswwA+a7stag3Mb7GGzZ87oVBVFTj6ieWFdrk/NIKVpJXfJGtmCzyiqWODWSSEUg2Xy8WC+jVhfpFg/pQAk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722193409; 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=xsmVM/rsoJrmjcYWayPIXtyiZTqzjg5scmSIRp8jNFI=; b=KVXbNA6eMWkQVzTKY1TqN0GHI4V2yZbTCgRPgmLfiHBjGbV12r0SNvVUFHR6gLynDbN9HT 2/VxZ1XqC9joTY43YRNaGjCkyeBkHh90v31NQkeNSbROiws6vcZIyH6Mvvk0zSQ1rVOCVy 1/vIkaPiwXefAkj4FgXXqd7GtYAzh/k= Received: from mx-prod-mc-01.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-657-fUOeHMyCMx6XkEyRukpPgg-1; Sun, 28 Jul 2024 15:03:26 -0400 X-MC-Unique: fUOeHMyCMx6XkEyRukpPgg-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2AB201955F3B for ; Sun, 28 Jul 2024 19:03:26 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.224.24]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0F5D1300018D for ; Sun, 28 Jul 2024 19:03:24 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 11/13] posix: Implement environ snapshotting in __spawni In-Reply-To: Message-ID: References: X-From-Line: b5927afe9408f9118035c45202b83e3a70e47f4a Mon Sep 17 00:00:00 2001 Date: Sun, 28 Jul 2024 21:03:21 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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_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~incoming=patchwork.ozlabs.org@sourceware.org This keeps stack usage bounded if environ is used with the functions in the posix_spawn family. --- posix/spawni.c | 94 +++++++++++++++++++++++++++++++- stdlib/Makefile | 3 + stdlib/tst-environ-posix_spawn.c | 31 +++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 stdlib/tst-environ-posix_spawn.c diff --git a/posix/spawni.c b/posix/spawni.c index ef47dd9ea4..02e3ad0c8d 100644 --- a/posix/spawni.c +++ b/posix/spawni.c @@ -20,6 +20,29 @@ #include #include "spawn_int.h" #include +#include +#include +#include + +/* Used to keep track of the memory mapping for the environment + snapshot. */ +struct spawni_environ_snapshot +{ + char **data; + size_t size; /* In bytes. */ +}; + +static void +__spawni_environ_snapshot_unmap (void *arg) +{ + struct spawni_environ_snapshot *ses = arg; + if (ses->data != NULL) + { + int save_errno = errno; + __munmap (ses->data, ses->size); + __set_errno (save_errno); + } +} /* Spawn a new process executing PATH with the attributes describes in *ATTRP. Before running the process perform the actions described in FILE-ACTIONS. */ @@ -29,5 +52,74 @@ __spawni (pid_t *pid, const char *file, const posix_spawnattr_t *attrp, char *const argv[], char *const envp[], int xflags) { - return __arch_spawni (pid, file, file_actions, attrp, argv, envp, xflags); + if (envp == NULL || envp != __environ + || !__environ_is_from_array_list ((char **) envp) + || __environ_single_threaded_no_snapshot (envp)) + return __arch_spawni (pid, file, file_actions, attrp, argv, envp, xflags); + + /* Perform the environment snapshot. It is beneficial to do this + here, and not in execveat, because it is possible to use an + mmap-backed array to avoid a huge stack allocation. Memory + management is easier because the allocation happens before vfork + and can be undone completely before the function returns. */ + + size_t env_size; + { + char *on_stack[ENVIRON_STACK_SNAPSHOT_SIZE]; + env_size = __getenvarray (on_stack, ENVIRON_STACK_SNAPSHOT_SIZE); + if (env_size < ENVIRON_STACK_SNAPSHOT_SIZE) + return __arch_spawni (pid, file, file_actions, attrp, argv, + on_stack, xflags); + } + + int ret; + + /* Use mmap for async-signal-safety. Async-cancel-safety has + historically not been provided, so do not disable asynchronous + cancellation. As a result, there are memory leaks. */ + struct spawni_environ_snapshot ses = { NULL, 0 }; + __libc_cleanup_push (__spawni_environ_snapshot_unmap, &ses); + + while (true) + { + /* Make room for the null terminator. */ + ++env_size; + + /* Create the allocation. */ + { + size_t size = env_size * sizeof (char *); + void *ptr = __mmap (NULL, size, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (ptr == MAP_FAILED) + { + ret = -1; + break; + } + ses.data = ptr; + ses.size = size; + } + + /* Try to get a snapshot. */ + size_t env_count = __getenvarray (ses.data, env_size); + if (env_count < env_size) + { + ret = __arch_spawni (pid, file, file_actions, attrp, argv, + ses.data, xflags); + break; + } + + /* The environment grew. Unmap the allocation that turned out + to be too small. Make a copy to avoid spurious unmaps with + asynchronous cancellation. */ + struct spawni_environ_snapshot copy = ses; + ses.data = NULL; + __spawni_environ_snapshot_unmap (©); + + /* Try again with the updated size. */ + env_size = env_count; + } + + __libc_cleanup_pop (1); + return ret; } diff --git a/stdlib/Makefile b/stdlib/Makefile index 498ec15e91..5934b683b1 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -379,6 +379,7 @@ tests-container := \ # statically, or use harcoded paths. tests-with-reexec := \ tst-environ-execl \ + tst-environ-posix_spawn \ # tests-with-reexec tests += $(tests-with-reexec) @@ -644,6 +645,8 @@ $(objpfx)tst-getenv-unsetenv: $(shared-thread-library) # See above for $(tests-with-reexec). ifeq ($(build-hardcoded-path-in-tests),yes) $(objpfx)tst-environ-execl: $(shared-thread-library) +$(objpfx)tst-environ-posix_spawn: $(shared-thread-library) else $(objpfx)tst-environ-execl: $(static-thread-library) +$(objpfx)tst-environ-posix_spawn: $(static-thread-library) endif diff --git a/stdlib/tst-environ-posix_spawn.c b/stdlib/tst-environ-posix_spawn.c new file mode 100644 index 0000000000..4791e19f1f --- /dev/null +++ b/stdlib/tst-environ-posix_spawn.c @@ -0,0 +1,31 @@ +/* Test environment snapshots with posix_spawn. + 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 + . */ + +#define PROGRAM "tst-environ-posix_spawn" +#include "tst-environ-snapshot-skeleton.c" + +#include + +static pid_t +create_process (void) +{ + char *argv[] = { self_path, (char *) "verify", NULL }; + pid_t pid; + return posix_spawn (&pid, self_path, NULL, NULL, argv, environ); + return pid; +}