From patchwork Sun Jun 10 03:00:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 138089 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp2579485lji; Sat, 9 Jun 2018 20:12:55 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKwAr672qX9dJqD/qGx9pQ8zHXhDdEBHa5Xd1qbkNxm6HHQDfowJpfY60h3lC+nYDNFd6p3 X-Received: by 2002:ac8:2b46:: with SMTP id 6-v6mr6891508qtv.302.1528600375520; Sat, 09 Jun 2018 20:12:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528600375; cv=none; d=google.com; s=arc-20160816; b=nSeMbMJCM0nYaXhIeJzFrka0BKlOQFKvQySBDe0+Dq2Cya5ONXCRo2zcb6/enakqfE kWf34w0TUiARJvlUZiyYISFaG5ZyBfH66Bj7vypP30vynzHVhMNViYJ08atHCcgBs62Z x8kw2eb+rF7I9GybaOhMoHrnp/PksBx3YTotqv9JrGsLTxuqkskvJJfdMpLustdjb/tc GSiULBPfv9ooN1+ao/TfAz3bexXlUqYXOEHkZksnRH4vXWiJeRasVzwxBDH4KgpQtSSh m8X3b3AUuzzcykiPmkRCNmanvbQ3qoafLzQ7N16i0mMq7DOe7jRiqwGKBYK8QaUYhQOI 9z4Q== 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=3OazyDaQ98WLzwavAlvx2ZeLx9Uw8bApVXczaU8PD30=; b=DqxLLB56IRo1WO916LuU/TB4Onb2gMIjmy0a/bAFDpcy/79oRMqKS25qW4jJQadCGL 6MiDzRuykIHGEURNXZba/khB6068KP9oxc0UqJxCjNO2fuMWpZYf1LUtoce2yHvz98pt UlEUWN7sBlrlyqr/28UF2y055ryAoBaMSnnYtS3KFptdhZxg6pyiyJVd/jGHx2j9e+Q2 WQXpOcEgWxhI4ujiQ5OY0QN25aIfLa1FLkj21TwnnKZiSMaN0PbxvhO0XuIukXyNYZ2Q GNXhUNdgi8PY4dAa8U5VVJ1cqzAiXwmCe9iDe/t5GSwgkL7ED3kjJvRLBGH/+k2A0/Lk HOuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=c6CSBj9D; 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 t48-v6si384939qte.236.2018.06.09.20.12.55 for (version=TLS1 cipher=AES128-SHA bits=128/128); Sat, 09 Jun 2018 20:12:55 -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=c6CSBj9D; 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]:42226 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fRqmk-00025l-VO for patch@linaro.org; Sat, 09 Jun 2018 23:12:55 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40170) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fRqd2-0001m1-RX for qemu-devel@nongnu.org; Sat, 09 Jun 2018 23:02:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fRqd1-0003DW-GK for qemu-devel@nongnu.org; Sat, 09 Jun 2018 23:02:52 -0400 Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]:33387) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fRqd1-0003DC-8L for qemu-devel@nongnu.org; Sat, 09 Jun 2018 23:02:51 -0400 Received: by mail-pg0-x241.google.com with SMTP id e11-v6so8142360pgq.0 for ; Sat, 09 Jun 2018 20:02:51 -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=3OazyDaQ98WLzwavAlvx2ZeLx9Uw8bApVXczaU8PD30=; b=c6CSBj9DgVXYSjdvVGiq8inDEp6NCZaDIyG4g1xZVY2I0kZcY7M5k3e/fZiEwuoFQY Yp98oTSUoQpl8ZhTR/st6U+4FRzZCrrFVFePwUnd87fTjuC7bAa0/LYosmbc0nA8SroD +AhKMycG3On4xxr5NqacIt9/KBk7Ymk8kN7I0= 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=3OazyDaQ98WLzwavAlvx2ZeLx9Uw8bApVXczaU8PD30=; b=hDUpnofJPjPoERwXcuu5qTXWmo/GyFLKeSG/pvxNE5uqClS4RJ/BQ++mmPHvfM5pa5 Zm7zaZPmn8KFXxC+DnTS2Ol6+C6JrK/3HFm9LTmduWW1A4jhrJAXoninfRa5XEPs5vaI nu+nR5XWp4X/e4K64ABPhDYQdRQWkUXeP3UdwhOiUf0pDzzHuwF6u3L3QLc46sLS92/S T1kiaSczDK+8DdjrwAMXHCxJXOkfGiu8wgRFW83MrGQOkVQG/Ql3Lz5vM32YskzX8rBG pW3aM5Pit8p0fOJSfi6MneC8We3QOLtSBOrXjEi06aAk9noMivtc4sr/pOzRA90eBWPi r+PA== X-Gm-Message-State: APt69E2fSd1imZe9k5Zb53BuOgRVmavcAoMaVZdSzuM91IDvbvu/QydO fOlG4m0LMSH8hf/pa2euLy4YGjnOVjw= X-Received: by 2002:a65:4146:: with SMTP id x6-v6mr10420236pgp.221.1528599769977; Sat, 09 Jun 2018 20:02:49 -0700 (PDT) Received: from cloudburst.twiddle.net (rrcs-173-198-77-219.west.biz.rr.com. [173.198.77.219]) by smtp.gmail.com with ESMTPSA id y17-v6sm31712718pfe.33.2018.06.09.20.02.48 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 09 Jun 2018 20:02:49 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Sat, 9 Jun 2018 17:00:43 -1000 Message-Id: <20180610030220.3777-12-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180610030220.3777-1-richard.henderson@linaro.org> References: <20180610030220.3777-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:c05::241 Subject: [Qemu-devel] [PATCH v2 011/108] 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.1 diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 6d2f9915ec..aa6c4e1577 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -7969,6 +7969,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); @@ -8190,103 +8295,6 @@ static abi_long do_syscall1(void *cpu_env, unsigned num, abi_long arg1, 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; @@ -12467,6 +12475,7 @@ static impl_fn *syscall_table(unsigned num) */ SYSCALL(brk); SYSCALL(close); + SYSCALL(execve); SYSCALL(exit); SYSCALL(read); SYSCALL(write);