From nobody Sun Nov 24 07:21:32 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; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1725067335; cv=none; d=zohomail.com; s=zohoarc; b=mcQdEUM0+yisaAS+cAve2FQ3BfugHJmvotdKefKIJvi3BIQixutn/t2sqCXx3XyQRxUjdZxrB0/zzLgEMBP6eCRUYlyQRWcTxthykO974ZvIYn7YBWH7hf23SHXEMyI6pLyrfgedZ34YeNUhW2sqZXgrz+M4BzpEocQhZrhAR4I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1725067335; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=rMDFVMvjJEOudw8bnlcbNGMt/5UtjU5WLRqGq7dj+Bw=; b=m5aL2k2XzsqD1n78W1hrKhOBwkLmCgPvX/lxXUUS4cUDK4jOFkvuTxKO/rgqiCVatjPnW1q4K/3rrxMjgRjjpEzAoBSfbiO1TImJewXcT0fkIRxKp/1W0JhSWrrE/vqp8e62asxO/7dYayAph7zsOCji3kMl+7twpKC+5CKOm28= 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1725067335249802.95283626488; Fri, 30 Aug 2024 18:22:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1skCnt-0006Ey-65; Fri, 30 Aug 2024 21:21:25 -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 1skADw-0000bC-4R for qemu-devel@nongnu.org; Fri, 30 Aug 2024 18:36:08 -0400 Received: from mail-yb1-xb32.google.com ([2607:f8b0:4864:20::b32]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1skADu-00045w-CK for qemu-devel@nongnu.org; Fri, 30 Aug 2024 18:36:07 -0400 Received: by mail-yb1-xb32.google.com with SMTP id 3f1490d57ef6-e1651f48c31so2499140276.0 for ; Fri, 30 Aug 2024 15:36:05 -0700 (PDT) Received: from noahgold-desk.sc.intel.com ([192.55.54.47]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c340bfa76bsm18816856d6.29.2024.08.30.15.36.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 15:36:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725057365; x=1725662165; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=rMDFVMvjJEOudw8bnlcbNGMt/5UtjU5WLRqGq7dj+Bw=; b=TMdBHMqziexjw/cCNcV5Kg85BKWbqVW6xhBGQZtVHDGq4Y1iwFJVzGtOJFFnenh16I 4BofsQ78oMWJTFlFR0JWkjqI7+IkeTThwpjFBs9H7IvfoyuR0EWawk1EeWhYzN5Q5c/V PDFDzcu5zDIaJ9lOiHYZA2TC7480Ekptrao4DcAP1iHk8DCgK8VMTNBKnuG3ZvxJE0jH sv67bHAohpKOUpU2WcqrK12KRSlcBwGv+ohrzLZ/mqj4qAue/Pe0gjA/w463ZNp3Mp5x v1yWLMMT2gGcn0fk8mCF1m16iopkDuncQpRbOnUdtawcU5qjn6lPc7fqzQ1G8EQ7xDFL cNig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725057365; x=1725662165; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=rMDFVMvjJEOudw8bnlcbNGMt/5UtjU5WLRqGq7dj+Bw=; b=jfpHjtOZFv/zLNtcLEnJD2Vhx1cDN/UhaKd0a1qyI9VdM0bYeEB2mwGN7K/c8EqbGp teJpoH34xH0xtcvRg3rzFdfhxwVAZQo1Jqe6qSHmP8ExO3rj0R7wPdlLbjRoQiKBBMX0 Jqw27ScWD9nllnBO8joN7OEzNRwsLYZq6kZ4PMPnQOXcLE9ENQrmlBoKdgQzxo8F/a6L PMW2+HEnbP/5zt9YchFNUG4wi2P7MFi/zjDpvmhu3uEPHGCoY2AkcnIjPVDuXamdaRYS 9FXVd6MUolcPHcbdQiBOGFMO6cg+6lunwSCysmX2KEvvAlLHqqPkhOeGaAbC/iBh4FGs k/6Q== X-Gm-Message-State: AOJu0YzvBi1H5koopDyh3nW5hm67WnmJ/IhY+OjJdoj73UV27VGWl/mE a/wZGPnpSY3L6ueG+Ax15ox1gvGWA3vuSy4lAI5kbZtIDF3qghmn67dZvw== X-Google-Smtp-Source: AGHT+IEHZTuOGdhve44IJbM4iJ6V0ZOW6D1Gzd10ab+Ds2adEIxC8HCsT2S5SZK6sUXzoMJamowh6w== X-Received: by 2002:a05:6902:2b0b:b0:e1a:41ef:4a1a with SMTP id 3f1490d57ef6-e1a79fe7e63mr3942150276.12.1725057364655; Fri, 30 Aug 2024 15:36:04 -0700 (PDT) From: Noah Goldstein To: qemu-devel@nongnu.org Cc: goldstein.w.n@gmail.com, laurent@vivier.eu Subject: [PATCH v1] linux-user: Add option to run `execve`d programs through QEMU Date: Fri, 30 Aug 2024 15:36:01 -0700 Message-Id: <20240830223601.2796327-1-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 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: pass client-ip=2607:f8b0:4864:20::b32; envelope-from=goldstein.w.n@gmail.com; helo=mail-yb1-xb32.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Fri, 30 Aug 2024 21:21:16 -0400 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 @gmail.com) X-ZM-MESSAGEID: 1725067335994116600 Content-Type: text/plain; charset="utf-8" The new option '-qemu-children' makes it so that on `execve` the child process will be launch by the same `qemu` executable that is currently running along with its current commandline arguments. The motivation for the change is to make it so that plugins running through `qemu` can continue to run on children. Why not just `binfmt`?: Plugins can be desirable regardless of system/architecture emulation, and can sometimes be useful for elf files that can run natively. Enabling `binfmt` for all natively runnable elf files may not be desirable. Signed-off-by: Noah Goldstein --- linux-user/main.c | 22 ++++++++++++++++++++++ linux-user/syscall.c | 20 +++++++++++++++----- linux-user/user-internals.h | 4 ++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/linux-user/main.c b/linux-user/main.c index 8143a0d4b0..dfb303a1f2 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -81,6 +81,10 @@ unsigned long mmap_min_addr; uintptr_t guest_base; bool have_guest_base; =20 +bool qemu_dup_for_children; +int qemu_argc; +char ** qemu_argv; + /* * Used to implement backwards-compatibility for the `-strace`, and * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by @@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg) perf_enable_jitdump(); } =20 +static void handle_arg_qemu_children(const char *arg) +{ + qemu_dup_for_children =3D true; +} + static QemuPluginList plugins =3D QTAILQ_HEAD_INITIALIZER(plugins); =20 #ifdef CONFIG_PLUGIN @@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] =3D { "", "Generate a /tmp/perf-${pid}.map file for perf"}, {"jitdump", "QEMU_JITDUMP", false, handle_arg_jitdump, "", "Generate a jit-${pid}.dump file for perf"}, + {"qemu-children", + "QEMU_CHILDREN", false, handle_arg_qemu_children, + "", "Run child processes (created with execve) with qemu " + "(as instantiated for the parent)"}, {NULL, NULL, false, NULL, NULL, NULL} }; =20 @@ -729,6 +742,15 @@ int main(int argc, char **argv, char **envp) =20 optind =3D parse_args(argc, argv); =20 + if (qemu_dup_for_children) { + int i; + qemu_argc =3D optind; + qemu_argv =3D g_new0(char *, qemu_argc); + for (i =3D 0; i < optind; ++i) { + qemu_argv[i] =3D strdup(argv[i]); + } + } + qemu_set_log_filename_flags(last_log_filename, last_log_mask | (enable_strace * LOG_STRAC= E), &error_fatal); diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 9d5415674d..732ef89054 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -8459,13 +8459,14 @@ static int do_execv(CPUArchState *cpu_env, int dirf= d, abi_long pathname, abi_long guest_argp, abi_long guest_envp, int flags, bool is_execveat) { - int ret; + int ret, argp_offset; char **argp, **envp; int argc, envc; abi_ulong gp; abi_ulong addr; char **q; void *p; + bool through_qemu =3D !is_execveat && qemu_dup_for_children; =20 argc =3D 0; =20 @@ -8489,10 +8490,11 @@ static int do_execv(CPUArchState *cpu_env, int dirf= d, envc++; } =20 - argp =3D g_new0(char *, argc + 1); + argp_offset =3D through_qemu ? qemu_argc : 0; + argp =3D g_new0(char *, argc + argp_offset + 1); envp =3D g_new0(char *, envc + 1); =20 - for (gp =3D guest_argp, q =3D argp; gp; gp +=3D sizeof(abi_ulong), q++= ) { + for (gp =3D guest_argp, q =3D argp + argp_offset; gp; gp +=3D sizeof(a= bi_ulong), q++) { if (get_user_ual(addr, gp)) { goto execve_efault; } @@ -8537,9 +8539,17 @@ static int do_execv(CPUArchState *cpu_env, int dirfd, } =20 const char *exe =3D p; - if (is_proc_myself(p, "exe")) { + if (through_qemu) { + int i; + for (i =3D 0; i < argp_offset; ++i) { + argp[i] =3D qemu_argv[i]; + } + exe =3D qemu_argv[0]; + } + else if (is_proc_myself(p, "exe")) { exe =3D exec_path; } + ret =3D is_execveat ? safe_execveat(dirfd, exe, argp, envp, flags) : safe_execve(exe, argp, envp); @@ -8553,7 +8563,7 @@ execve_efault: ret =3D -TARGET_EFAULT; =20 execve_end: - for (gp =3D guest_argp, q =3D argp; *q; gp +=3D sizeof(abi_ulong), q++= ) { + for (gp =3D guest_argp, q =3D argp + argp_offset; *q; gp +=3D sizeof(a= bi_ulong), q++) { if (get_user_ual(addr, gp) || !addr) { break; } diff --git a/linux-user/user-internals.h b/linux-user/user-internals.h index 5c7f173ceb..0719e65ff4 100644 --- a/linux-user/user-internals.h +++ b/linux-user/user-internals.h @@ -30,6 +30,10 @@ void stop_all_tasks(void); extern const char *qemu_uname_release; extern unsigned long mmap_min_addr; =20 +extern bool qemu_dup_for_children; +extern int qemu_argc; +extern char ** qemu_argv; + typedef struct IOCTLEntry IOCTLEntry; =20 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp, --=20 2.34.1