From patchwork Sun Jul 28 19:02:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1965750 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=HbmvMZnl; 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 4WX9vW4bVPz1ybX for ; Mon, 29 Jul 2024 05:06:31 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CEE1C385828E for ; Sun, 28 Jul 2024 19:06:29 +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 DDCD0385828E for ; Sun, 28 Jul 2024 19:02:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DDCD0385828E 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 DDCD0385828E 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=1722193370; cv=none; b=Ne0/QwdsVuAcYJpSB+syM3GT26oHbicMwZe8Q1N4Ar3c1CVP7o7YGT+wuWfdgBJb5TNW4cNXvl67oMYjXh+cmSerI6zIhS3QsPHM7WpfSWJzFSvhi0N3MomksDod5nZPy5veWacwMljnZsc8e5o2vu3nE+qwPfmVODVoQovWfjo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722193370; c=relaxed/simple; bh=eZHfP82rgv6HHYJunK5WL9J3BoxIn43few3k30aax3E=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=SXMOpKJSEeqy3D56GPfHMo+YOZLri66DvB76E4QqFJSxeAnBRUYJSXci/4NtFIqcQ10al3ZkfN6ElBmgpWCJICheb5xAO4U9vuUF96/4r/AK9LvNSYNodvfwYzuEuEc0WObEU5TYbWo1ETb5WD2n4tXqAQGB3ysqgMwFxAEz0TA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722193367; 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=vOtKWt5ldCaNuPXDsjijTBUuTlC4GtEeTo7BbCXjP3o=; b=HbmvMZnlumExwicDCRAdwTMZsCGLsQhSlvMSpL72k7IOt4foi1lkOzhahAvuII68syAo8/ IxTB3VMaekPtCZvf4VMlEbEH2FWC8AVzqyO+M+0cr1ht3CiZjGROOzlcE3tbccaxcznfna QJoa+Q/xawaAKBKlZQSlYaRN+uKILjE= 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-164-MVzI0jELPn2-KYyd4wzJ_A-1; Sun, 28 Jul 2024 15:02:45 -0400 X-MC-Unique: MVzI0jELPn2-KYyd4wzJ_A-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 19AC41955F3B for ; Sun, 28 Jul 2024 19:02:45 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.224.24]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 19F4419560AE for ; Sun, 28 Jul 2024 19:02:43 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 05/13] posix: Consolidate execve family of functions In-Reply-To: Message-ID: References: X-From-Line: c73086c71d3deb6598029f3a72810c985a148ac7 Mon Sep 17 00:00:00 2001 Date: Sun, 28 Jul 2024 21:02:41 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 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 Ports only need to implement __arch_execveat going forward. Generic code implements execve, fexecve, execveat on top of it. The __arch_execveat/execveat split makes it easy to add generic code to the execveat implementation, e.g., for environ handling. The Linux implementation prefers the execve system call if available and keeps emulation for fexecve-style execveat, for maximum compatibility. Reviewed-by: Adhemerval Zanella --- posix/execve.c | 13 +---- posix/execveat.c | 12 ++--- posix/fexecve.c | 13 ++--- .../hurd/execve.c => generic/arch-execveat.h} | 21 +++------ .../mach/hurd/{execveat.c => arch-execveat.h} | 11 ++--- sysdeps/mach/hurd/fexecve.c | 47 ------------------- .../sysv/linux/{fexecve.c => arch-execveat.h} | 38 +++++++-------- sysdeps/unix/sysv/linux/execveat.c | 32 ------------- sysdeps/unix/sysv/linux/syscalls.list | 1 - 9 files changed, 40 insertions(+), 148 deletions(-) rename sysdeps/{mach/hurd/execve.c => generic/arch-execveat.h} (61%) rename sysdeps/mach/hurd/{execveat.c => arch-execveat.h} (90%) delete mode 100644 sysdeps/mach/hurd/fexecve.c rename sysdeps/unix/sysv/linux/{fexecve.c => arch-execveat.h} (73%) delete mode 100644 sysdeps/unix/sysv/linux/execveat.c diff --git a/posix/execve.c b/posix/execve.c index c8f08658cb..6d6cefaebe 100644 --- a/posix/execve.c +++ b/posix/execve.c @@ -16,24 +16,15 @@ . */ #include -#include +#include #include - /* Replace the current process, executing PATH with arguments ARGV and environment ENVP. ARGV and ENVP are terminated by NULL pointers. */ int __execve (const char *path, char *const argv[], char *const envp[]) { - if (path == NULL || argv == NULL || envp == NULL) - { - __set_errno (EINVAL); - return -1; - } - - __set_errno (ENOSYS); - return -1; + return __execveat (AT_FDCWD, path, argv, envp, 0); } -stub_warning (execve) weak_alias (__execve, execve) diff --git a/posix/execveat.c b/posix/execveat.c index bddc4d5fb3..110177ea54 100644 --- a/posix/execveat.c +++ b/posix/execveat.c @@ -20,6 +20,8 @@ #include #include +#include + /* Replace the current process, executing PATH relative to difrd with arguments argv and environment envp. argv and envp are terminated by NULL pointers. */ @@ -27,15 +29,7 @@ int __execveat (int dirfd, const char *path, char *const argv[], char *const envp[], int flags) { - if (path == NULL || argv == NULL || envp == NULL) - { - __set_errno (EINVAL); - return -1; - } - - __set_errno (ENOSYS); - return -1; + return __arch_execveat (dirfd, path, argv, envp, flags); } -stub_warning (execveat) weak_alias (__execveat, execveat) diff --git a/posix/fexecve.c b/posix/fexecve.c index 04e59f97ad..3f632b3ea3 100644 --- a/posix/fexecve.c +++ b/posix/fexecve.c @@ -16,23 +16,20 @@ . */ #include -#include +#include #include - /* Execute the file FD refers to, overlaying the running program image. ARGV and ENVP are passed to the new program, as for `execve'. */ int fexecve (int fd, char *const argv[], char *const envp[]) { - if (fd < 0 || argv == NULL || envp == NULL) + /* Avoid unexpected behavior of execveat with AT_FDCWD. */ + if (fd < 0) { - __set_errno (EINVAL); + __set_errno (EBADF); return -1; } - __set_errno (ENOSYS); - return -1; + return __execveat (fd, "", argv, envp, AT_EMPTY_PATH); } - -stub_warning (fexecve) diff --git a/sysdeps/mach/hurd/execve.c b/sysdeps/generic/arch-execveat.h similarity index 61% rename from sysdeps/mach/hurd/execve.c rename to sysdeps/generic/arch-execveat.h index 11e3f34b39..cd064df28f 100644 --- a/sysdeps/mach/hurd/execve.c +++ b/sysdeps/generic/arch-execveat.h @@ -1,4 +1,5 @@ -/* Copyright (C) 1991-2024 Free Software Foundation, Inc. +/* Architecture-specific execveat implementation. Stub version. + 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 @@ -15,18 +16,10 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include -#include -#include - -/* Replace the current process, executing FILE_NAME with arguments ARGV and - environment ENVP. ARGV and ENVP are terminated by NULL pointers. */ -int -__execve (const char *file_name, char *const argv[], char *const envp[]) +static int +__arch_execveat (int dirfd, const char *path, char *const argv, + char *const envp, int flags) { - return __execveat (AT_FDCWD, file_name, argv, envp, 0); + __set_errno (ENOSYS); + return -1; } - -weak_alias (__execve, execve) diff --git a/sysdeps/mach/hurd/execveat.c b/sysdeps/mach/hurd/arch-execveat.h similarity index 90% rename from sysdeps/mach/hurd/execveat.c rename to sysdeps/mach/hurd/arch-execveat.h index f24dd7b62f..999779cbd5 100644 --- a/sysdeps/mach/hurd/execveat.c +++ b/sysdeps/mach/hurd/arch-execveat.h @@ -1,4 +1,5 @@ -/* Copyright (C) 1991-2024 Free Software Foundation, Inc. +/* Execute program relative to a directory file descriptor. Hurd version. + Copyright (C) 1991-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 @@ -25,9 +26,9 @@ /* Replace the current process, executing FILE_NAME with arguments ARGV and environment ENVP. ARGV and ENVP are terminated by NULL pointers. */ -int -__execveat (int dirfd, const char *file_name, char *const argv[], - char *const envp[], int flags) +static int +__arch_execveat (int dirfd, const char *file_name, char *const argv[], + char *const envp[], int flags) { error_t err; char *concat_name = NULL; @@ -88,5 +89,3 @@ __execveat (int dirfd, const char *file_name, char *const argv[], return __hurd_fail (err); } - -weak_alias (__execveat, execveat) diff --git a/sysdeps/mach/hurd/fexecve.c b/sysdeps/mach/hurd/fexecve.c deleted file mode 100644 index 50472d5ca5..0000000000 --- a/sysdeps/mach/hurd/fexecve.c +++ /dev/null @@ -1,47 +0,0 @@ -/* Copyright (C) 1993-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 - -/* Execute the file FD refers to, overlaying the running program image. */ - -int -fexecve (int fd, char *const argv[], char *const envp[]) -{ - file_t file; - error_t err; - enum retry_type doretry; - char retryname[1024]; - - err = HURD_DPORT_USE (fd, - __dir_lookup (port, "", O_EXEC, 0, &doretry, retryname, &file)); - - if (! err && (doretry != FS_RETRY_NORMAL || retryname[0] != '\0')) - err = EGRATUITOUS; - if (err) - return __hurd_fail(err); - - err = _hurd_exec_paths (__mach_task_self (), file, NULL, NULL, argv, envp); - if (! err) - err = EGRATUITOUS; - - __mach_port_deallocate (__mach_task_self (), file); - return __hurd_fail (err); -} diff --git a/sysdeps/unix/sysv/linux/fexecve.c b/sysdeps/unix/sysv/linux/arch-execveat.h similarity index 73% rename from sysdeps/unix/sysv/linux/fexecve.c rename to sysdeps/unix/sysv/linux/arch-execveat.h index c2303735de..b6bc286829 100644 --- a/sysdeps/unix/sysv/linux/fexecve.c +++ b/sysdeps/unix/sysv/linux/arch-execveat.h @@ -1,4 +1,5 @@ -/* Copyright (C) 1994-2024 Free Software Foundation, Inc. +/* Execute program relative to a directory file descriptor. Linux version. + Copyright (C) 1994-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 @@ -15,17 +16,11 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include -#include #include -#include - #include -#include -#include #include +#include +#include static int __do_fexecve (int fd, char *const argv[], char *const envp[]) @@ -64,17 +59,20 @@ __do_fexecve (int fd, char *const argv[], char *const envp[]) return -1; } -/* Execute the file FD refers to, overlaying the running program image. - ARGV and ENVP are passed to the new program, as for `execve'. */ -int -fexecve (int fd, char *const argv[], char *const envp[]) +static int +__arch_execveat (int dirfd, const char *path, char *const *argv, + char *const *envp, int flags) { - /* Avoid unexpected behavior of execveat with AT_FDCWD. */ - if (fd < 0) - { - __set_errno (EBADF); - return -1; - } + /* Use execve if possible, to preserve the historic system call + profile. */ + if (dirfd == AT_FDCWD && flags == 0) + return INLINE_SYSCALL_CALL (execve, path, argv, envp, flags); + + /* Provide emulation for an fexecve-style execveat call. */ + if (dirfd >= 0 && flags == AT_EMPTY_PATH && path[0] == '\0') + return __do_fexecve (dirfd, argv, envp); - return __do_fexecve (fd, argv, envp); + /* Otherwise perform the system call without emulation. */ + return INLINE_SYSCALL_CALL (execveat, dirfd, path, &argv[0], &envp[0], + flags); } diff --git a/sysdeps/unix/sysv/linux/execveat.c b/sysdeps/unix/sysv/linux/execveat.c deleted file mode 100644 index e1d68b9043..0000000000 --- a/sysdeps/unix/sysv/linux/execveat.c +++ /dev/null @@ -1,32 +0,0 @@ -/* Execute program relative to a directory file descriptor. - Copyright (C) 2021-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 - -/* Execute the file FD refers to, overlaying the running program image. - ARGV and ENVP are passed to the new program, as for 'execve'. */ -int -execveat (int dirfd, const char *path, char *const argv[], char *const envp[], - int flags) -{ - /* Avoid implicit array coercion in syscall macros. */ - return INLINE_SYSCALL_CALL (execveat, dirfd, path, &argv[0], &envp[0], - flags); -} diff --git a/sysdeps/unix/sysv/linux/syscalls.list b/sysdeps/unix/sysv/linux/syscalls.list index 9ac42c3436..14bbb5fe6e 100644 --- a/sysdeps/unix/sysv/linux/syscalls.list +++ b/sysdeps/unix/sysv/linux/syscalls.list @@ -9,7 +9,6 @@ delete_module EXTRA delete_module 3 delete_module epoll_create1 EXTRA epoll_create1 i:i epoll_create1 epoll_ctl EXTRA epoll_ctl i:iiip epoll_ctl eventfd EXTRA eventfd2 i:ii eventfd -execve - execve i:spp __execve execve flock - flock i:ii __flock flock fsconfig EXTRA fsconfig i:iUsNi fsconfig fsmount EXTRA fsmount i:iUU fsmount