From nobody Tue Feb 10 11:15:12 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1527838613953382.90818418881634; Fri, 1 Jun 2018 00:36:53 -0700 (PDT) Received: from localhost ([::1]:47834 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fOecH-0000jb-4T for importer@patchew.org; Fri, 01 Jun 2018 03:36:53 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57442) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fOeWm-0005VL-0N for qemu-devel@nongnu.org; Fri, 01 Jun 2018 03:31:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fOeWk-0000Q9-OT for qemu-devel@nongnu.org; Fri, 01 Jun 2018 03:31:12 -0400 Received: from mail-pl0-x244.google.com ([2607:f8b0:400e:c01::244]:41500) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fOeWk-0000PO-Gw for qemu-devel@nongnu.org; Fri, 01 Jun 2018 03:31:10 -0400 Received: by mail-pl0-x244.google.com with SMTP id az12-v6so14819013plb.8 for ; Fri, 01 Jun 2018 00:31:10 -0700 (PDT) Received: from cloudburst.twiddle.net (97-126-112-211.tukw.qwest.net. [97.126.112.211]) by smtp.gmail.com with ESMTPSA id c4-v6sm4431782pgv.86.2018.06.01.00.31.07 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 01 Jun 2018 00:31:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=RPrIDkYfAKFWJaaTRA6onq1Qh07sa6uq89O2i6nWx8M=; b=SHCLakwSSQp8naUpcWTUPqSPxdWq468yH7aKimOUznHZqcV7ZYiI6qHdVJZ1W1tA1i /3m12q4OjE0WDRYFP1a2Z/mCdKxHtw0nqIaQrJ6Idp91m+MWkpO4mJGv1j8ZVQVGCwgB qS91yexo4GQPIuotUTGAsWnVpS6rQEiwHbqfY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=RPrIDkYfAKFWJaaTRA6onq1Qh07sa6uq89O2i6nWx8M=; b=XHs9vv+QcTtTuP1+t8iLXZPDcC78bXNlITMXAyuR/34IxW7ibj+JfHe+F30PbHfRTa +KcRdEvKQ7SGZGy0h4CZo55NmIlXOKqJVUFfJ085SkeKTdMeFYernL6mTGXKNXZujNw+ fgCcb1o5NBgVRERQSx94jXGe07Kt4+eVupFPPQWi6Ec0e02XU4H9AD1icmm/EGX63dW2 V2qojmtv54BNK/236mLKV0QG05O6GS/o2ALfq9RFM6MoA7obOwtut+drSH0AoLm+aLua mAheIx83zzOzxuNBGEVAPXrGU8v/QB/iM9JBQaWjR5B+9J/4us+zvhPyE1unQpqeR/AM vCuA== X-Gm-Message-State: ALKqPwf8NKP73OwFqiGm3x/6ERq1QJgvPgqmum1bVdm5qg8AOxdNy61w /QRDM9AJ2imK6Q12zw7TPT/NK31YorI= X-Google-Smtp-Source: ADUXVKJmWPKoxI6ZbF0oebZfeuzoHdEzSlNySSSxuX6tqbsFRq8d9/sOUsWlBBMYDAvF9s4MiGjppw== X-Received: by 2002:a17:902:4b:: with SMTP id 69-v6mr10292077pla.178.1527838269282; Fri, 01 Jun 2018 00:31:09 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Fri, 1 Jun 2018 00:30:28 -0700 Message-Id: <20180601073050.8054-12-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180601073050.8054-1-richard.henderson@linaro.org> References: <20180601073050.8054-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c01::244 Subject: [Qemu-devel] [PATCH 11/33] linux-user: Split out execve X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: laurent@vivier.eu Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" At the same time, fix the repeated re-reading of the argv and env arrays from guest memory. Instead read into a unified array once. Signed-off-by: Richard Henderson --- linux-user/syscall.c | 203 ++++++++++++++++++++++--------------------- 1 file changed, 106 insertions(+), 97 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index b0d268dab7..a9b59a8658 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -7998,6 +7998,111 @@ IMPL(close) return get_errno(close(arg1)); } =20 +IMPL(execve) +{ + abi_ulong *guest_ptrs; + char **host_ptrs; + int argc, envc, alloc, i; + abi_ulong gp; + abi_ulong guest_argp =3D arg2; + abi_ulong guest_envp =3D arg3; + char *filename; + abi_long ret; + + /* Initial estimate of number of guest pointers required. */ + alloc =3D 32; + guest_ptrs =3D g_new(abi_ulong, alloc); + + /* Iterate through argp and envp, counting entries, and + * reading guest addresses from the arrays. + */ + for (gp =3D guest_argp, argc =3D 0; gp; gp +=3D sizeof(abi_ulong)) { + abi_ulong addr; + if (get_user_ual(addr, gp)) { + return -TARGET_EFAULT; + } + if (!addr) { + break; + } + if (argc >=3D alloc) { + alloc *=3D 2; + guest_ptrs =3D g_renew(abi_ulong, guest_ptrs, alloc); + } + guest_ptrs[argc++] =3D addr; + } + for (gp =3D guest_envp, envc =3D 0; gp; gp +=3D sizeof(abi_ulong)) { + abi_ulong addr; + if (get_user_ual(addr, gp)) { + return -TARGET_EFAULT; + } + if (!addr) { + break; + } + if (argc + envc >=3D alloc) { + alloc *=3D 2; + guest_ptrs =3D g_renew(abi_ulong, guest_ptrs, alloc); + } + guest_ptrs[argc + envc++] =3D addr; + } + + /* Exact number of host pointers required. */ + host_ptrs =3D g_new0(char *, argc + envc + 2); + + /* Iterate through the argp and envp that we already read + * and convert the guest pointers to host pointers. + */ + ret =3D -TARGET_EFAULT; + for (i =3D 0; i < argc; ++i) { + char *p =3D lock_user_string(guest_ptrs[i]); + if (!p) { + goto fini; + } + host_ptrs[i] =3D p; + } + for (i =3D 0; i < envc; ++i) { + char *p =3D lock_user_string(guest_ptrs[argc + i]); + if (!p) { + goto fini; + } + host_ptrs[argc + 1 + i] =3D p; + } + + /* Read the executable filename. */ + filename =3D lock_user_string(arg1); + if (!filename) { + goto fini; + } + + /* Although execve() is not an interruptible syscall it is + * a special case where we must use the safe_syscall wrapper: + * if we allow a signal to happen before we make the host + * syscall then we will 'lose' it, because at the point of + * execve the process leaves QEMU's control. So we use the + * safe syscall wrapper to ensure that we either take the + * signal as a guest signal, or else it does not happen + * before the execve completes and makes it the other + * program's problem. + */ + ret =3D get_errno(safe_execve(filename, host_ptrs, host_ptrs + argc + = 1)); + unlock_user(filename, arg1, 0); + + fini: + /* Deallocate everything we allocated above. */ + for (i =3D 0; i < argc; ++i) { + if (host_ptrs[i]) { + unlock_user(host_ptrs[i], guest_ptrs[i], 0); + } + } + for (i =3D 0; i < envc; ++i) { + if (host_ptrs[argc + 1 + i]) { + unlock_user(host_ptrs[argc + 1 + i], guest_ptrs[argc + i], 0); + } + } + g_free(host_ptrs); + g_free(guest_ptrs); + return ret; +} + IMPL(exit) { CPUState *cpu =3D ENV_GET_CPU(cpu_env); @@ -8237,103 +8342,6 @@ IMPL(everything_else) unlock_user(p, arg2, 0); return ret; #endif - case TARGET_NR_execve: - { - char **argp, **envp; - int argc, envc; - abi_ulong gp; - abi_ulong guest_argp; - abi_ulong guest_envp; - abi_ulong addr; - char **q; - int total_size =3D 0; - - argc =3D 0; - guest_argp =3D arg2; - for (gp =3D guest_argp; gp; gp +=3D sizeof(abi_ulong)) { - if (get_user_ual(addr, gp)) - return -TARGET_EFAULT; - if (!addr) - break; - argc++; - } - envc =3D 0; - guest_envp =3D arg3; - for (gp =3D guest_envp; gp; gp +=3D sizeof(abi_ulong)) { - if (get_user_ual(addr, gp)) - return -TARGET_EFAULT; - if (!addr) - break; - envc++; - } - - argp =3D g_new0(char *, argc + 1); - 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)) - goto execve_efault; - if (!addr) - break; - if (!(*q =3D lock_user_string(addr))) - goto execve_efault; - total_size +=3D strlen(*q) + 1; - } - *q =3D NULL; - - for (gp =3D guest_envp, q =3D envp; gp; - gp +=3D sizeof(abi_ulong), q++) { - if (get_user_ual(addr, gp)) - goto execve_efault; - if (!addr) - break; - if (!(*q =3D lock_user_string(addr))) - goto execve_efault; - total_size +=3D strlen(*q) + 1; - } - *q =3D NULL; - - if (!(p =3D lock_user_string(arg1))) - goto execve_efault; - /* Although execve() is not an interruptible syscall it is - * a special case where we must use the safe_syscall wrapper: - * if we allow a signal to happen before we make the host - * syscall then we will 'lose' it, because at the point of - * execve the process leaves QEMU's control. So we use the - * safe syscall wrapper to ensure that we either take the - * signal as a guest signal, or else it does not happen - * before the execve completes and makes it the other - * program's problem. - */ - ret =3D get_errno(safe_execve(p, argp, envp)); - unlock_user(p, arg1, 0); - - goto execve_end; - - execve_efault: - ret =3D -TARGET_EFAULT; - - execve_end: - for (gp =3D guest_argp, q =3D argp; *q; - gp +=3D sizeof(abi_ulong), q++) { - if (get_user_ual(addr, gp) - || !addr) - break; - unlock_user(*q, addr, 0); - } - for (gp =3D guest_envp, q =3D envp; *q; - gp +=3D sizeof(abi_ulong), q++) { - if (get_user_ual(addr, gp) - || !addr) - break; - unlock_user(*q, addr, 0); - } - - g_free(argp); - g_free(envp); - } - return ret; case TARGET_NR_chdir: if (!(p =3D lock_user_string(arg1))) return -TARGET_EFAULT; @@ -12916,6 +12924,7 @@ IMPL(everything_else) static impl_fn * const syscall_table[] =3D { [TARGET_NR_brk] =3D impl_brk, [TARGET_NR_close] =3D impl_close, + [TARGET_NR_execve] =3D impl_execve, [TARGET_NR_exit] =3D impl_exit, [TARGET_NR_read] =3D impl_read, [TARGET_NR_write] =3D impl_write, --=20 2.17.0