From nobody Thu Sep 19 01:14:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1721758414; cv=none; d=zohomail.com; s=zohoarc; b=LZ09LUuXpflSx132bs7cfjTTpaTdQrOJaezuX29a6uSdY80RztDibYwRBKjTbPxrZoWgVOl8HEIw6nDnPIZnDNq9XmePJv2Sj/+RuTtGUQFD4QUxkZ9SbUh7GlEOnGdv8iKdTrVpdhciBAh1AYScGy8mRrNjUB9/1CJiw2KyGgk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1721758414; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=7Whe4XPbca7lrsBGH9KDOzbYV/caOsn/K1GjqT+CBVQ=; b=GVRVKUhOB9yDmWeo8v814bQnb8YNV16gRUGj4U/UQ97eX/mbXGClHqfUSOFJk01BJIP9x2Y+ElyDyl5uLRmAirvZa4W7rHZ69JxsLUpOnaWe6UYt6BYYnWjrl2REy6cChpN0O8/ZvIDH/52Y32xiRI18KyrsQeO9l27A41vAv3E= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1721758414782579.8766655131975; Tue, 23 Jul 2024 11:13:34 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWJxX-0002ng-89; Tue, 23 Jul 2024 14:09:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sWJwx-0001JL-IW for qemu-devel@nongnu.org; Tue, 23 Jul 2024 14:09:25 -0400 Received: from mail-il1-x12d.google.com ([2607:f8b0:4864:20::12d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWJwr-0001D8-PE for qemu-devel@nongnu.org; Tue, 23 Jul 2024 14:09:23 -0400 Received: by mail-il1-x12d.google.com with SMTP id e9e14a558f8ab-39948831566so11381695ab.3 for ; Tue, 23 Jul 2024 11:09:07 -0700 (PDT) Received: from dune.bsdimp.com ([50.253.99.174]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-397f7a2827csm33361775ab.53.2024.07.23.11.09.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 11:09:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20230601.gappssmtp.com; s=20230601; t=1721758146; x=1722362946; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7Whe4XPbca7lrsBGH9KDOzbYV/caOsn/K1GjqT+CBVQ=; b=AHm70aKqq9olJXcLfcOzSXgVjD/0QCrFL7zES63TZof2USPSNiP1VtvfmfzYnKtedu TkY5u/0yPYCfL1mxqcGp2mObP3oMddhA08JTZKr8SoS/DVJvXTBNPe2u51aax0eX+5uo Him/vIxHNFvyfYYWKa4Pyfsajh/qYaor6+rUJ9nAajfPGR/FOljtmLt+9kWZCtqGu5K+ /VuStATNh/yjCBDQ2IAaPqLiWLGNDPAwtP/uWRLe62NjLnyvyKPgYrx5sFN8OUjsR8WU eOu+Bx+3qJilgWeUyVDc6UBZUGkXb3Jq/a/cU0Z2Jx3MzhBolc2jqdyJsAYo/Aj1eK0J tNOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721758146; x=1722362946; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7Whe4XPbca7lrsBGH9KDOzbYV/caOsn/K1GjqT+CBVQ=; b=BHQFSbW4zR3BblwoWdnPKdWAhVsweXURcG2JDOCY5EJGIwFavArZfuJCu+kMOHXn/I 1Y5aF6uAbrKqrpc0zSSOgNgKZ91UEeIFzwBTFdRjVyPONJ4aJUXPBFAp1eaicyqNpTOA YyM5r+qmVXrHD8/0iqZBnLrGOqsCERQoYORUvKfHrLwuQ8iOixxJNorEjNqgwn2eh0GD VcOHDK7pQfo6LQKCaqw7aUS71/aAe4YkP2jQccwrBAzMHEyPOjXo9XQgSdJIegONU+PB hPgfAHhMfpIyqaPEF0PLRJuD9SAMlc2GFLXSXBqBpUG2pEUCrX2QFB8GV22u3Js3/tFQ wY3A== X-Gm-Message-State: AOJu0YwxtTRodaXuHhNFfNRVNgNZvvQUI1Vwle9Styc/2NUuW9HREYyD EF0iX/nz78igl1xuJuLRvuLa5cGWS+b55oFgYo0/LsLMdD701I8FT9Kosr1Ptstu7TlSWC2859Z zT0c= X-Google-Smtp-Source: AGHT+IHMUen2Xdb3r8qPCmkW11eiNdEHwu/n5pw+WlgkqGQToeFwtRvz6wG2KQKvcystj13B4J0dYg== X-Received: by 2002:a05:6e02:18c7:b0:383:4a12:b77c with SMTP id e9e14a558f8ab-3993ff8615bmr127566505ab.8.1721758146426; Tue, 23 Jul 2024 11:09:06 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: Warner Losh , Peter Maydell , Kyle Evans , qemu-arm@nongnu.org, Doug Rabson , Richard Henderson Subject: [PULL 09/14] bsd-user: Simplify the implementation of execve Date: Tue, 23 Jul 2024 12:07:20 -0600 Message-ID: <20240723180725.99114-10-imp@bsdimp.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240723180725.99114-1-imp@bsdimp.com> References: <20240723180725.99114-1-imp@bsdimp.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2607:f8b0:4864:20::12d; envelope-from=imp@bsdimp.com; helo=mail-il1-x12d.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bsdimp-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1721758415772116600 Content-Type: text/plain; charset="utf-8" From: Doug Rabson This removes the logic which prepends the emulator to each call to execve and fexecve. This is not necessary with the existing imgact_binmisc support and it avoids the need to install the emulator binary into jail environments when using 'binmiscctl --pre-open'. Signed-off-by: Doug Rabson Reviewed-by: Warner Losh Signed-off-by: Warner Losh Acked-by: Richard Henderson --- bsd-user/freebsd/os-proc.c | 118 +------------------------------------ bsd-user/main.c | 18 ------ 2 files changed, 3 insertions(+), 133 deletions(-) diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c index e0203e259b0..bf993f1b662 100644 --- a/bsd-user/freebsd/os-proc.c +++ b/bsd-user/freebsd/os-proc.c @@ -26,65 +26,13 @@ struct kinfo_proc; =20 #include "qemu.h" =20 -/* - * Get the filename for the given file descriptor. - * Note that this may return NULL (fail) if no longer cached in the kernel. - */ -static char * -get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len) -{ - char *ret =3D NULL; - unsigned int cnt; - struct procstat *procstat =3D NULL; - struct kinfo_proc *kp =3D NULL; - struct filestat_list *head =3D NULL; - struct filestat *fst; - - procstat =3D procstat_open_sysctl(); - if (procstat =3D=3D NULL) { - goto out; - } - - kp =3D procstat_getprocs(procstat, KERN_PROC_PID, pid, &cnt); - if (kp =3D=3D NULL) { - goto out; - } - - head =3D procstat_getfiles(procstat, kp, 0); - if (head =3D=3D NULL) { - goto out; - } - - STAILQ_FOREACH(fst, head, next) { - if (fd =3D=3D fst->fs_fd) { - if (fst->fs_path !=3D NULL) { - (void)strlcpy(filename, fst->fs_path, len); - ret =3D filename; - } - break; - } - } - -out: - if (head !=3D NULL) { - procstat_freefiles(procstat, head); - } - if (kp !=3D NULL) { - procstat_freeprocs(procstat, kp); - } - if (procstat !=3D NULL) { - procstat_close(procstat); - } - return ret; -} - /* * execve/fexecve */ abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp, abi_ulong guest_envp, int do_fexec) { - char **argp, **envp, **qargp, **qarg1, **qarg0, **qargend; + char **argp, **envp, **qarg0; int argc, envc; abi_ulong gp; abi_ulong addr; @@ -117,9 +65,7 @@ abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_u= long guest_argp, qarg0 =3D argp =3D g_new0(char *, argc + 9); /* save the first argument for the emulator */ *argp++ =3D (char *)getprogname(); - qargp =3D argp; *argp++ =3D (char *)getprogname(); - qarg1 =3D argp; envp =3D g_new0(char *, envc + 1); for (gp =3D guest_argp, q =3D argp; gp; gp +=3D sizeof(abi_ulong), q++= ) { if (get_user_ual(addr, gp)) { @@ -137,7 +83,6 @@ abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_u= long guest_argp, total_size +=3D strlen(*q) + 1; } *q++ =3D NULL; - qargend =3D q; =20 for (gp =3D guest_envp, q =3D envp; gp; gp +=3D sizeof(abi_ulong), q++= ) { if (get_user_ual(addr, gp)) { @@ -166,71 +111,14 @@ abi_long freebsd_exec_common(abi_ulong path_or_fd, ab= i_ulong guest_argp, } =20 if (do_fexec) { - if (((int)path_or_fd > 0 && - is_target_elf_binary((int)path_or_fd)) =3D=3D 1) { - char execpath[PATH_MAX]; - - /* - * The executable is an elf binary for the target - * arch. execve() it using the emulator if we can - * determine the filename path from the fd. - */ - if (get_filename_from_fd(getpid(), (int)path_or_fd, execpath, - sizeof(execpath)) !=3D NULL) { - memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg= 1)); - qarg1[1] =3D qarg1[0]; - qarg1[0] =3D (char *)"-0"; - qarg1 +=3D 2; - qargend +=3D 2; - *qarg1 =3D execpath; -#ifndef DONT_INHERIT_INTERP_PREFIX - memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg= 1)); - *qarg1++ =3D (char *)"-L"; - *qarg1++ =3D (char *)interp_prefix; -#endif - ret =3D get_errno(execve(qemu_proc_pathname, qargp, envp)); - } else { - /* Getting the filename path failed. */ - ret =3D -TARGET_EBADF; - goto execve_end; - } - } else { - ret =3D get_errno(fexecve((int)path_or_fd, argp, envp)); - } + ret =3D get_errno(fexecve((int)path_or_fd, argp, envp)); } else { - int fd; - p =3D lock_user_string(path_or_fd); if (p =3D=3D NULL) { ret =3D -TARGET_EFAULT; goto execve_end; } - - /* - * Check the header and see if it a target elf binary. If so - * then execute using qemu user mode emulator. - */ - fd =3D open(p, O_RDONLY | O_CLOEXEC); - if (fd > 0 && is_target_elf_binary(fd) =3D=3D 1) { - close(fd); - /* execve() as a target binary using emulator. */ - memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg1)); - qarg1[1] =3D qarg1[0]; - qarg1[0] =3D (char *)"-0"; - qarg1 +=3D 2; - qargend +=3D 2; - *qarg1 =3D (char *)p; -#ifndef DONT_INHERIT_INTERP_PREFIX - memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg1)); - *qarg1++ =3D (char *)"-L"; - *qarg1++ =3D (char *)interp_prefix; -#endif - ret =3D get_errno(execve(qemu_proc_pathname, qargp, envp)); - } else { - close(fd); - /* Execve() as a host native binary. */ - ret =3D get_errno(execve(p, argp, envp)); - } + ret =3D get_errno(execve(p, argp, envp)); unlock_user(p, path_or_fd, 0); } =20 diff --git a/bsd-user/main.c b/bsd-user/main.c index dcad266c2c9..82e94a03160 100644 --- a/bsd-user/main.c +++ b/bsd-user/main.c @@ -90,7 +90,6 @@ unsigned long reserved_va; =20 const char *interp_prefix =3D CONFIG_QEMU_INTERP_PREFIX; const char *qemu_uname_release; -char qemu_proc_pathname[PATH_MAX]; /* full path to exeutable */ =20 unsigned long target_maxtsiz =3D TARGET_MAXTSIZ; /* max text size */ unsigned long target_dfldsiz =3D TARGET_DFLDSIZ; /* initial data size li= mit */ @@ -247,22 +246,6 @@ adjust_ssize(void) setrlimit(RLIMIT_STACK, &rl); } =20 -static void save_proc_pathname(char *argv0) -{ - int mib[4]; - size_t len; - - mib[0] =3D CTL_KERN; - mib[1] =3D KERN_PROC; - mib[2] =3D KERN_PROC_PATHNAME; - mib[3] =3D -1; - - len =3D sizeof(qemu_proc_pathname); - if (sysctl(mib, 4, qemu_proc_pathname, &len, NULL, 0)) { - perror("sysctl"); - } -} - int main(int argc, char **argv) { const char *filename; @@ -292,7 +275,6 @@ int main(int argc, char **argv) usage(); } =20 - save_proc_pathname(argv[0]); =20 error_init(argv[0]); module_call_init(MODULE_INIT_TRACE); --=20 2.45.1