From patchwork Fri Jun 1 07:30:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 137466 Delivered-To: patch@linaro.org Received: by 2002:a2e:9706:0:0:0:0:0 with SMTP id r6-v6csp606270lji; Fri, 1 Jun 2018 00:35:08 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKMOkaNPkMhzWTbM7TTRIHrOfm7R75BWsvRKo88djgS0eVHFPBdleVOefB3kDY3lg9UIBox X-Received: by 2002:a37:c31d:: with SMTP id a29-v6mr9059885qkj.213.1527838508344; Fri, 01 Jun 2018 00:35:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527838508; cv=none; d=google.com; s=arc-20160816; b=VuzElaiMtbJ2ni3bYsLCoImcDdmfIT3ngG/mcVyGw8mXxvmM70MS0hpgY+fSNnWjpo 1TwS03Gqfm471BTxOJQbDxXuG59w2ThPrQBwPtSqXQ+lNDYma0aApImdtS/ARkbEmuBW mpBI0Qu2MbbDNEEuaa02kpUydrupHv5VMhWbtW+onHJvQRhT3tiMD07AaT2ER+TOu13/ iN07gk8jMCk30nxoLKEci6509U4biW1kPN4cgXWzgq9SIlD3MvwD5qJAqw1BUkM7Z6sU +M7dl+ID8OWhP0iqOYSeUcyH7WkDtDF+ikr9ENk6s+9cPPbWk59jhZqvzKjBZcBGP86g Yl8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature:arc-authentication-results; bh=RPrIDkYfAKFWJaaTRA6onq1Qh07sa6uq89O2i6nWx8M=; b=t567WJ2isJaYvWVDRI7mqsF1Q2Qwms6l+uALmfHdZLbsfYn8DFueIM5CpVr5v1u/XO qiBo9H6Rk/1oSWPy/JJq82ixBmsaL7Y1ykEa7mo9VqlUe4KsWvgOvilN9RtOzPNMjucK nZ72/ZBmE45LtiI/oCyLkz2YFUkJguSWYGeVuJNTf7fmbupnPZhRpVsO8AcNKlZhipMP 6ij7YNqOATTH8MZKUaYaFJG6T3U7e0gxeAGtRJ/AI0p5V4g0fVW0nytE4DrS88KQPNcM TTNfK8SdR9TnWlOGoUdPDus0/uh8QPEfpQUO76JYLs4j16eZzKTE8q2MIlfv8J/Dvhn/ NEdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=SHCLakwS; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id q11-v6si6285575qtq.38.2018.06.01.00.35.08 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 01 Jun 2018 00:35:08 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=SHCLakwS; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:47824 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fOeaW-0007vw-Gl for patch@linaro.org; Fri, 01 Jun 2018 03:35:04 -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) 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-Received: by 2002:a17:902:4b:: with SMTP id 69-v6mr10292077pla.178.1527838269282; Fri, 01 Jun 2018 00:31:09 -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) 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+patch=linaro.org@nongnu.org Sender: "Qemu-devel" 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(-) -- 2.17.0 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)); } +IMPL(execve) +{ + abi_ulong *guest_ptrs; + char **host_ptrs; + int argc, envc, alloc, i; + abi_ulong gp; + abi_ulong guest_argp = arg2; + abi_ulong guest_envp = arg3; + char *filename; + abi_long ret; + + /* Initial estimate of number of guest pointers required. */ + alloc = 32; + guest_ptrs = g_new(abi_ulong, alloc); + + /* Iterate through argp and envp, counting entries, and + * reading guest addresses from the arrays. + */ + for (gp = guest_argp, argc = 0; gp; gp += sizeof(abi_ulong)) { + abi_ulong addr; + if (get_user_ual(addr, gp)) { + return -TARGET_EFAULT; + } + if (!addr) { + break; + } + if (argc >= alloc) { + alloc *= 2; + guest_ptrs = g_renew(abi_ulong, guest_ptrs, alloc); + } + guest_ptrs[argc++] = addr; + } + for (gp = guest_envp, envc = 0; gp; gp += sizeof(abi_ulong)) { + abi_ulong addr; + if (get_user_ual(addr, gp)) { + return -TARGET_EFAULT; + } + if (!addr) { + break; + } + if (argc + envc >= alloc) { + alloc *= 2; + guest_ptrs = g_renew(abi_ulong, guest_ptrs, alloc); + } + guest_ptrs[argc + envc++] = addr; + } + + /* Exact number of host pointers required. */ + host_ptrs = 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 = -TARGET_EFAULT; + for (i = 0; i < argc; ++i) { + char *p = lock_user_string(guest_ptrs[i]); + if (!p) { + goto fini; + } + host_ptrs[i] = p; + } + for (i = 0; i < envc; ++i) { + char *p = lock_user_string(guest_ptrs[argc + i]); + if (!p) { + goto fini; + } + host_ptrs[argc + 1 + i] = p; + } + + /* Read the executable filename. */ + filename = 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 = get_errno(safe_execve(filename, host_ptrs, host_ptrs + argc + 1)); + unlock_user(filename, arg1, 0); + + fini: + /* Deallocate everything we allocated above. */ + for (i = 0; i < argc; ++i) { + if (host_ptrs[i]) { + unlock_user(host_ptrs[i], guest_ptrs[i], 0); + } + } + for (i = 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 = 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 = 0; - - argc = 0; - guest_argp = arg2; - for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) { - if (get_user_ual(addr, gp)) - return -TARGET_EFAULT; - if (!addr) - break; - argc++; - } - envc = 0; - guest_envp = arg3; - for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) { - if (get_user_ual(addr, gp)) - return -TARGET_EFAULT; - if (!addr) - break; - envc++; - } - - argp = g_new0(char *, argc + 1); - envp = g_new0(char *, envc + 1); - - for (gp = guest_argp, q = argp; gp; - gp += sizeof(abi_ulong), q++) { - if (get_user_ual(addr, gp)) - goto execve_efault; - if (!addr) - break; - if (!(*q = lock_user_string(addr))) - goto execve_efault; - total_size += strlen(*q) + 1; - } - *q = NULL; - - for (gp = guest_envp, q = envp; gp; - gp += sizeof(abi_ulong), q++) { - if (get_user_ual(addr, gp)) - goto execve_efault; - if (!addr) - break; - if (!(*q = lock_user_string(addr))) - goto execve_efault; - total_size += strlen(*q) + 1; - } - *q = NULL; - - if (!(p = 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 = get_errno(safe_execve(p, argp, envp)); - unlock_user(p, arg1, 0); - - goto execve_end; - - execve_efault: - ret = -TARGET_EFAULT; - - execve_end: - for (gp = guest_argp, q = argp; *q; - gp += sizeof(abi_ulong), q++) { - if (get_user_ual(addr, gp) - || !addr) - break; - unlock_user(*q, addr, 0); - } - for (gp = guest_envp, q = envp; *q; - gp += 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 = lock_user_string(arg1))) return -TARGET_EFAULT; @@ -12916,6 +12924,7 @@ IMPL(everything_else) static impl_fn * const syscall_table[] = { [TARGET_NR_brk] = impl_brk, [TARGET_NR_close] = impl_close, + [TARGET_NR_execve] = impl_execve, [TARGET_NR_exit] = impl_exit, [TARGET_NR_read] = impl_read, [TARGET_NR_write] = impl_write,