From patchwork Sun Jul 28 19:02: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: 1965743 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=gHVtrZCq; 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 4WX9s86XF8z1ybY for ; Mon, 29 Jul 2024 05:04:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2E809385AC29 for ; Sun, 28 Jul 2024 19:04:27 +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 3D3083857022 for ; Sun, 28 Jul 2024 19:02:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3D3083857022 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 3D3083857022 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=1722193369; cv=none; b=b2AhuHwzuAkIyheRIUlqivibikK2cpieIcRKS/eF15rhQ067902UaOKUeTs8tjLjw1UPgYBvE1AQwujrZwl2te9xc+pjalpZBe5giC0MOa1NDiwd2tOBbYRzXSqvdGqLEOi4DCl3dRm7btsI636RjcvmXqnGJDaWsLhlWBMMu/I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722193369; c=relaxed/simple; bh=3NXOXpiCFgRmtjoBBUw8AEE75jRjNkqLFRgsWXw11xw=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=vhFc8k6TKVB9brkirVZIFK1dY0B8ATyOd+azZb7mGPUByUkrt36GvzZRp8E56qvifP6Ye1uvOQ8SexE7JtHHzCPyebu/qX6mXZFdEbDM4pgW/ZGok/SCp8WMhs4Mf+USjqe2i45F+nVUUTZvROs90VsRM894MRTJRplfvW8mCvo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722193355; 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=cnD5Bjnrt/De1Em2DJlNQb8sXf3HK0LwosPRBU8d5wo=; b=gHVtrZCqq3R6dSToImCFBIyL8jQ91Ff9y+P0mQWyo/BpuJKu/bFAaES8F9Ne1L/qWnzcKb lywa8jsLbYSkTbnQ18Q757SusbllQxvbQmzTKm0y1rylsNx+TyOAUOTL8uB8oe1g6KC1KL jt5i4rjjWdtwU7z20Avx+0eTGr2BEAI= Received: from mx-prod-mc-02.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-641-3O0twUd-MAWY6JohPN_wnw-1; Sun, 28 Jul 2024 15:02:33 -0400 X-MC-Unique: 3O0twUd-MAWY6JohPN_wnw-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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 229C71955F0D for ; Sun, 28 Jul 2024 19:02:33 +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 048BC300018D for ; Sun, 28 Jul 2024 19:02:31 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 03/13] Linux: Introduce __do_fexecve in fexecve In-Reply-To: Message-ID: <49be319a6cc60341a437ba186ef701b9095e00d7.1722193092.git.fweimer@redhat.com> References: X-From-Line: 49be319a6cc60341a437ba186ef701b9095e00d7 Mon Sep 17 00:00:00 2001 Date: Sun, 28 Jul 2024 21:02:28 +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, 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 Call the execve system call directly, in preparation of wrapping environ access in the *execve* family of function. Make handling of errno more explicit on the fallback path by using INTERNAL_SYSCALL_CALL. --- sysdeps/unix/sysv/linux/fexecve.c | 53 ++++++++++++++++--------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/sysdeps/unix/sysv/linux/fexecve.c b/sysdeps/unix/sysv/linux/fexecve.c index e0e5566fd7..46c8170092 100644 --- a/sysdeps/unix/sysv/linux/fexecve.c +++ b/sysdeps/unix/sysv/linux/fexecve.c @@ -27,6 +27,33 @@ #include #include +static int +__do_fexecve (int fd, char *const argv[], char *const envp[]) +{ + /* Avoid implicit array coercion in syscall macros. */ + int err = -INTERNAL_SYSCALL_CALL (execveat, fd, "", &argv[0], &envp[0], + AT_EMPTY_PATH); + +#ifndef __ASSUME_EXECVEAT + if (err == ENOSYS) + { + /* We use the /proc filesystem to get the information. If it is not + mounted we fail. We do not need the return value. */ + struct fd_to_filename filename; + const char *path = __fd_to_filename (fd, &filename); + err = -INTERNAL_SYSCALL_CALL (execve, path, &argv[0], &envp[0]); + + /* We come here only if the 'execve' call fails. Determine whether + /proc is mounted. If not we return ENOSYS. */ + if (INTERNAL_SYSCALL_CALL (faccessat, AT_FDCWD, "/proc/self/fd", 0) + == -ENOENT) + err = ENOSYS; + } +#endif /* __ASSUME_EXECVEAT */ + + __set_errno (err); + 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'. */ @@ -39,29 +66,5 @@ fexecve (int fd, char *const argv[], char *const envp[]) return -1; } - /* Avoid implicit array coercion in syscall macros. */ - INLINE_SYSCALL (execveat, 5, fd, "", &argv[0], &envp[0], AT_EMPTY_PATH); -#ifndef __ASSUME_EXECVEAT - if (errno != ENOSYS) - return -1; -#endif - -#ifndef __ASSUME_EXECVEAT - /* We use the /proc filesystem to get the information. If it is not - mounted we fail. We do not need the return value. */ - struct fd_to_filename filename; - __execve (__fd_to_filename (fd, &filename), argv, envp); - - int save = errno; - - /* We come here only if the 'execve' call fails. Determine whether - /proc is mounted. If not we return ENOSYS. */ - struct __stat64_t64 st; - if (__stat64_time64 ("/proc/self/fd", &st) != 0 && errno == ENOENT) - save = ENOSYS; - - __set_errno (save); -#endif - - return -1; + return __do_fexecve (fd, argv, envp); }