From patchwork Fri Jan 18 21:30:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 1027800 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="WrdM/qGh"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43hFSM5t4hz9sDL for ; Sat, 19 Jan 2019 09:08:03 +1100 (AEDT) Received: from localhost ([127.0.0.1]:47911 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gkcIz-0000EH-NX for incoming@patchwork.ozlabs.org; Fri, 18 Jan 2019 17:08:01 -0500 Received: from eggs.gnu.org ([209.51.188.92]:56082) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gkbkf-0005Mq-93 for qemu-devel@nongnu.org; Fri, 18 Jan 2019 16:32:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gkbkd-0004td-Tg for qemu-devel@nongnu.org; Fri, 18 Jan 2019 16:32:33 -0500 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]:39398) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gkbkd-0004mg-KQ for qemu-devel@nongnu.org; Fri, 18 Jan 2019 16:32:31 -0500 Received: by mail-pf1-x441.google.com with SMTP id r136so7201503pfc.6 for ; Fri, 18 Jan 2019 13:32:25 -0800 (PST) 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=+fTBAf0C8A6A480A95zgMw1/4VjmqrTZEt0WUaFU6OY=; b=WrdM/qGhgkwRaH/ZDgr1cBia7AJE7mcAIf9Mr3+99xP0rDUiBGU9PxrhqvYcISISzd mn96+X7KraZOFFcZLVKa+H3obNcMY3/9UHVPZhL3aSbhha+4GNU6ezgNN0QpzSxoxI50 eK19z5d2jQdJJDXywx5wuff9tpJjfiKipntXw= 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=+fTBAf0C8A6A480A95zgMw1/4VjmqrTZEt0WUaFU6OY=; b=jzurltqRSQXQ4VKavn2pIOpNZcejXWcjLpFtLVMcImaKfH/jXq09DqYU2f4bTjojvw TKq28j/t78Sovu5+HotOxwNNDiwnQOaITOenrLD6KX5rR3IZvGHocjk+bdbUKfexJHPH RxK+vxbxFoEj957+J4PKc4c7dTXWq2ns8AMihMgNyXywQuvKzrzPQQZD5gmKdxf8ND8l d77tG+kN30cIfEdj709tL9HDATI9mmcG8JxykhrYt2caMXiTy/7MV6gOHwiCdiPb2h8y yGMYYdipIUr/2FKIdujWiuyv8V4tBZEpPfsb21DwVS3/EZRCDda/SPbBI+i+YlU+2mTg SThQ== X-Gm-Message-State: AJcUukc2tx5W6Rrx8bPctx3gWnFjnIX5MfZVRBNdT0Dfr+zy4hwZ1p7A 8xg+rNviJ68heuI0GNX6Nm8CHz9DkFA= X-Google-Smtp-Source: ALg8bN7TkeV/XImL5Nny6UiCs2XPVNHq3f7HhioF+AZmmiwHge22QnUBs2RecUiYI3zvul/GmpW7HQ== X-Received: by 2002:a65:50c1:: with SMTP id s1mr18917189pgp.350.1547847144103; Fri, 18 Jan 2019 13:32:24 -0800 (PST) Received: from cloudburst.twiddle.net ([2001:8000:10e0:a000:c673:a6b6:fdef:1933]) by smtp.gmail.com with ESMTPSA id v12sm5833667pgg.41.2019.01.18.13.32.22 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 18 Jan 2019 13:32:23 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Date: Sat, 19 Jan 2019 08:30:57 +1100 Message-Id: <20190118213122.22865-24-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20190118213122.22865-1-richard.henderson@linaro.org> References: <20190118213122.22865-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::441 Subject: [Qemu-devel] [PATCH v6 24/49] 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: Richard Henderson --- linux-user/syscall-defs.h | 1 + linux-user/strace.c | 32 ---------- linux-user/syscall-proc.inc.c | 110 ++++++++++++++++++++++++++++++++++ linux-user/syscall.c | 97 ------------------------------ linux-user/strace.list | 3 - 5 files changed, 111 insertions(+), 132 deletions(-) diff --git a/linux-user/syscall-defs.h b/linux-user/syscall-defs.h index 78d3f600eb..58fef48666 100644 --- a/linux-user/syscall-defs.h +++ b/linux-user/syscall-defs.h @@ -25,6 +25,7 @@ SYSCALL_DEF(close, ARG_DEC); SYSCALL_DEF(creat, ARG_STR, ARG_MODEFLAG); #endif SYSCALL_DEF(exit, ARG_DEC); +SYSCALL_DEF(execve, ARG_STR, ARG_PTR, ARG_PTR); #ifdef TARGET_NR_fork SYSCALL_DEF(fork); #endif diff --git a/linux-user/strace.c b/linux-user/strace.c index a41b6dcfe7..43865e9df9 100644 --- a/linux-user/strace.c +++ b/linux-user/strace.c @@ -568,38 +568,6 @@ print_newselect(const struct syscallname *name, } #endif -static void -print_execve(const struct syscallname *name, - abi_long arg1, abi_long arg2, abi_long arg3, - abi_long arg4, abi_long arg5, abi_long arg6) -{ - abi_ulong arg_ptr_addr; - char *s; - - if (!(s = lock_user_string(arg1))) - return; - gemu_log("%s(\"%s\",{", name->name, s); - unlock_user(s, arg1, 0); - - for (arg_ptr_addr = arg2; ; arg_ptr_addr += sizeof(abi_ulong)) { - abi_ulong *arg_ptr, arg_addr; - - arg_ptr = lock_user(VERIFY_READ, arg_ptr_addr, sizeof(abi_ulong), 1); - if (!arg_ptr) - return; - arg_addr = tswapal(*arg_ptr); - unlock_user(arg_ptr, arg_ptr_addr, 0); - if (!arg_addr) - break; - if ((s = lock_user_string(arg_addr))) { - gemu_log("\"%s\",", s); - unlock_user(s, arg_addr, 0); - } - } - - gemu_log("NULL})"); -} - /* * Variants for the return value output function */ diff --git a/linux-user/syscall-proc.inc.c b/linux-user/syscall-proc.inc.c index de4efed9f4..552aea60ae 100644 --- a/linux-user/syscall-proc.inc.c +++ b/linux-user/syscall-proc.inc.c @@ -269,6 +269,116 @@ SYSCALL_IMPL(clone) return do_clone(cpu_env, arg1, arg2, arg3, arg4, arg5); } +SYSCALL_IMPL(execve) +{ + char **argp, **envp; + int argc, envc; + abi_ulong gp; + abi_ulong guest_path = arg1; + abi_ulong guest_argp = arg2; + abi_ulong guest_envp = arg3; + abi_ulong addr; + char **q, *p; + int total_size = 0; + abi_long ret = -TARGET_EFAULT; + + argc = 0; + for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) { + if (get_user_ual(addr, gp)) { + goto execve_nofree; + } + if (!addr) { + break; + } + argc++; + } + envc = 0; + for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) { + if (get_user_ual(addr, gp)) { + goto execve_nofree; + } + 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++) { + char *this_q; + + if (get_user_ual(addr, gp)) { + goto execve_free; + } + if (!addr) { + break; + } + this_q = lock_user_string(addr); + if (!this_q) { + goto execve_free; + } + *q = this_q; + total_size += strlen(this_q) + 1; + } + + for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) { + char *this_q; + + if (get_user_ual(addr, gp)) { + goto execve_free; + } + if (!addr) { + break; + } + this_q = lock_user_string(addr); + if (!this_q) { + goto execve_free; + } + *q = this_q; + total_size += strlen(this_q) + 1; + } + + p = lock_user_string(guest_path); + if (!p) { + goto execve_free; + } + + /* + * 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, guest_path, 0); + + execve_free: + 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); + + execve_nofree: + return ret; +} + SYSCALL_IMPL(exit) { CPUState *cpu = ENV_GET_CPU(cpu_env); diff --git a/linux-user/syscall.c b/linux-user/syscall.c index eda199d46f..9b5b2eb7f1 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -5330,103 +5330,6 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, void *p; switch(num) { - 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; diff --git a/linux-user/strace.list b/linux-user/strace.list index faa0a9012d..8bc3fe6088 100644 --- a/linux-user/strace.list +++ b/linux-user/strace.list @@ -139,9 +139,6 @@ #ifdef TARGET_NR_execv { TARGET_NR_execv, "execv" , NULL, print_execv, NULL }, #endif -#ifdef TARGET_NR_execve -{ TARGET_NR_execve, "execve" , NULL, print_execve, NULL }, -#endif #ifdef TARGET_NR_execveat { TARGET_NR_execveat, "execveat" , NULL, NULL, NULL }, #endif