From nobody Tue Apr 16 13:26:23 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=1596125453; cv=none; d=zohomail.com; s=zohoarc; b=B1Ndj55CvpwLQy6SEhfgnKVZZMuH7rnonp6GIoYCf4Go7nVzvp0O42z9cLurAcIzr//guKA+Wh7Z2Iu33volejvtO/Os4VYmHT86yllBpbQ6nuc05um0QsV+wrmret64aOCB4uufUDCrNqf0DrnYapK6XdproVrrxi02AQQw7fY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596125453; h=Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=zqQAF9xKE/5F3X4KG56kpfJdotSVLh0P38cqHIhjICA=; b=L+5KCfq8V6apAU3hhjgojrBSmJ6Q8LwDIsYz4OXTroaVlt7p4v+dT94QfGRqcIr8MHTA1uiK6z4DdTvziB6irQEJy0yf3JRc7o/cxROTN1d8ZarCYutIYypx5ffiKJaeDWVXm0jWgCDWiL/LsRLJlyqcIxDaqmqYVR8IRazH5HU= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1596125453118565.2491770966677; Thu, 30 Jul 2020 09:10:53 -0700 (PDT) Received: from localhost ([::1]:48422 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k1B8t-0002sd-P6 for importer@patchew.org; Thu, 30 Jul 2020 12:10:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59290) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k1AzU-0005F3-Gi for qemu-devel@nongnu.org; Thu, 30 Jul 2020 12:01:08 -0400 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]:36038) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k1AzR-0005xm-Gb for qemu-devel@nongnu.org; Thu, 30 Jul 2020 12:01:07 -0400 Received: by mail-wm1-x344.google.com with SMTP id 3so6702246wmi.1 for ; Thu, 30 Jul 2020 09:01:04 -0700 (PDT) Received: from localhost ([193.136.93.218]) by smtp.gmail.com with ESMTPSA id w2sm12319861wre.5.2020.07.30.09.01.01 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 30 Jul 2020 09:01:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=zqQAF9xKE/5F3X4KG56kpfJdotSVLh0P38cqHIhjICA=; b=f+y8E1Z8zCvP411CgM/izmZKnE8vURoP2Dx4cjq3XYc6X6a/PTvO65/Hkm+BMoq254 LkmuoMQYNzQNegy0W77/HdcEoSujr+Ki/HAdBFgZRPhcBSUGrau+LIlNyFzv6GPOa2dS OedEf3cbgKij7/xRzi3WiaP+70RN1hMT5Gz8B8XHP+M7Mkz6MdwJJRJ4PgtfjAQN/BZr h/SKbkrB5xBkhgWrUAE8dwo+2azafj3m+uIyiZOaxhwVVBA8pJK+hr583NZI+IeEkwk4 8Lke1SQ0X5UVvnuhKdBHMfkEHg+7uteifKy30u2mlMEk0e0TDZ1MLvhYaFKpax0EvJff 306w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=zqQAF9xKE/5F3X4KG56kpfJdotSVLh0P38cqHIhjICA=; b=DKBRayEg0rwIBrkTCob2rFDdaZMlNBPpHDU+AVm4PTzETUwuY14LszenMaDZJLcc6K JnU/p7zVVMH6zZFTIGY4d9Wef9IUilbwu3mHRRdxh7kbRAOpTVEyo9UQfcL4tgzSNSrg uxqphJwI39LkjedOUz7X8m00e9ql51Tu8xuUiYi5GDueL1/k22PN3gwwUzACd/HOgj0H gH6u9Wowkk7jqwyrkH/K37mBWJmDof3vdTFeI6e7t1Xi1rud8Fakb4XcMnatMIZu/C1I vh4LW59rj/360yLQFbALgzGztkolFmcqYOVbWWdEGGdSpb4/CoYKdqnjLw0WAMYe9qSR 7+3g== X-Gm-Message-State: AOAM533OPoTMajnIuJWu6ULUPv3BhdCOj9D2F1nHmyY1Out3CPjZmkSk pI9A1fXTRgtMarE63Wdq5pasoROs/y8= X-Google-Smtp-Source: ABdhPJwsRVVaU6iyXzRZm4vflijzyRXLhrP5k9jMaX+B1zDA6g9awvvWSgMzyntS46IVzACB2HaTvg== X-Received: by 2002:a1c:4183:: with SMTP id o125mr13888093wma.101.1596124862869; Thu, 30 Jul 2020 09:01:02 -0700 (PDT) From: Ricardo Jesus To: qemu-devel@nongnu.org Subject: [PATCH] [v4] linux-user: add option to intercept execve() syscalls Date: Thu, 30 Jul 2020 17:01:06 +0100 Message-Id: <20200730160106.16613-1-rj.bcjesus@gmail.com> X-Mailer: git-send-email 2.27.0 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=2a00:1450:4864:20::344; envelope-from=rj.bcjesus@gmail.com; helo=mail-wm1-x344.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. 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, URIBL_BLOCKED=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 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" X-ZohoMail-DKIM: pass (identity @gmail.com) Content-Type: text/plain; charset="utf-8" This patch is based on the original work by Petros Angelatos [1], which I have updated to the current git master. The following commit message is largely based on Petros' original message. In order for one to use QEMU user mode emulation under a chroot, it is required to use binfmt_misc. This can be avoided by QEMU never doing a raw execve() to the host system. Introduce a new option, --execve, that uses the current QEMU interpreter to intercept execve(). qemu_execve() will prepend the interpreter path, similar to what binfmt_misc would do, and then pass the modified execve() to the host. [1] https://patchwork.ozlabs.org/patch/582756/ Signed-off-by: Ricardo Jesus --- linux-user/main.c | 8 +++ linux-user/qemu.h | 1 + linux-user/syscall.c | 138 +++++++++++++++++++++++++++++++++++++++---- 3 files changed, 136 insertions(+), 11 deletions(-) diff --git a/linux-user/main.c b/linux-user/main.c index 75c9785157..52f6860b45 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -107,6 +107,7 @@ static void usage(int exitcode); =20 static const char *interp_prefix =3D CONFIG_QEMU_INTERP_PREFIX; const char *qemu_uname_release; +const char *qemu_execve_path; =20 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <=3D address + 32, so we allocate a bigger stack. Need a better solution, for example @@ -337,6 +338,11 @@ static void handle_arg_guest_base(const char *arg) have_guest_base =3D true; } =20 +static void handle_arg_execve(const char *arg) +{ + qemu_execve_path =3D strdup(arg); +} + static void handle_arg_reserved_va(const char *arg) { char *p; @@ -441,6 +447,8 @@ static const struct qemu_argument arg_table[] =3D { "uname", "set qemu uname release string to 'uname'"}, {"B", "QEMU_GUEST_BASE", true, handle_arg_guest_base, "address", "set guest_base address to 'address'"}, + {"execve", "QEMU_EXECVE", true, handle_arg_execve, + "", "use this interpreter when a process calls execve()"}, {"R", "QEMU_RESERVED_VA", true, handle_arg_reserved_va, "size", "reserve 'size' bytes for guest virtual address space"}, {"d", "QEMU_LOG", true, handle_arg_log, diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 5c964389c1..d1b0d5716b 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -160,6 +160,7 @@ void init_task_state(TaskState *ts); void task_settid(TaskState *); void stop_all_tasks(void); extern const char *qemu_uname_release; +extern const char *qemu_execve_path; extern unsigned long mmap_min_addr; =20 /* ??? See if we can avoid exposing so much of the loader internals. */ diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 945fc25279..a266895a61 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -115,6 +115,7 @@ #ifdef HAVE_DRM_H #include #endif +#include #include "linux_loop.h" #include "uname.h" =20 @@ -7674,6 +7675,131 @@ static target_timer_t get_timer_id(abi_long arg) return timerid; } =20 +/* qemu_execve() Must return target values and target errnos. + * + * 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. + */ +static abi_long qemu_execve(char *filename, char *argv[], + char *envp[]) +{ + char *i_arg =3D NULL, *i_name =3D NULL; + char **new_argp; + int argc, fd, ret, i, offset =3D 5; + char *cp; + char buf[BINPRM_BUF_SIZE]; + + /* normal execve case */ + if (qemu_execve_path =3D=3D NULL || *qemu_execve_path =3D=3D 0) { + return get_errno(safe_execve(filename, argv, envp)); + } + + for (argc =3D 0; argv[argc] !=3D NULL; argc++) { + /* nothing */ ; + } + + fd =3D open(filename, O_RDONLY); + if (fd =3D=3D -1) { + return get_errno(fd); + } + + ret =3D read(fd, buf, BINPRM_BUF_SIZE); + if (ret =3D=3D -1) { + close(fd); + return get_errno(ret); + } + + /* if we have less than 2 bytes, we can guess it is not executable */ + if (ret < 2) { + close(fd); + return -host_to_target_errno(ENOEXEC); + } + + close(fd); + + /* adapted from the kernel + * https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tre= e/fs/binfmt_script.c + */ + if ((buf[0] =3D=3D '#') && (buf[1] =3D=3D '!')) { + /* + * This section does the #! interpretation. + * Sorta complicated, but hopefully it will work. -TYT + */ + + buf[BINPRM_BUF_SIZE - 1] =3D '\0'; + cp =3D strchr(buf, '\n'); + if (cp =3D=3D NULL) { + cp =3D buf + BINPRM_BUF_SIZE - 1; + } + *cp =3D '\0'; + while (cp > buf) { + cp--; + if ((*cp =3D=3D ' ') || (*cp =3D=3D '\t')) { + *cp =3D '\0'; + } else { + break; + } + } + for (cp =3D buf + 2; (*cp =3D=3D ' ') || (*cp =3D=3D '\t'); cp++) { + /* nothing */ ; + } + if (*cp =3D=3D '\0') { + return -ENOEXEC; /* No interpreter name found */ + } + i_name =3D cp; + i_arg =3D NULL; + for ( ; *cp && (*cp !=3D ' ') && (*cp !=3D '\t'); cp++) { + /* nothing */ ; + } + while ((*cp =3D=3D ' ') || (*cp =3D=3D '\t')) { + *cp++ =3D '\0'; + } + if (*cp) { + i_arg =3D cp; + } + + if (i_arg) { + offset +=3D 2; + } else { + offset +=3D 1; + } + } + + new_argp =3D alloca((argc + offset + 1) * sizeof(void *)); + + /* Copy the original arguments with offset */ + for (i =3D 0; i < argc; i++) { + new_argp[i + offset] =3D argv[i]; + } + + new_argp[0] =3D strdup(qemu_execve_path); + new_argp[1] =3D strdup("--execve"); + new_argp[2] =3D strdup(qemu_execve_path); + new_argp[3] =3D strdup("-0"); + new_argp[offset] =3D filename; + new_argp[argc + offset] =3D NULL; + + if (i_name) { + new_argp[4] =3D i_name; + new_argp[5] =3D i_name; + + if (i_arg) { + new_argp[6] =3D i_arg; + } + } else { + new_argp[4] =3D argv[0]; + } + + return get_errno(safe_execve(qemu_execve_path, new_argp, envp)); +} + static int target_to_host_cpu_mask(unsigned long *host_mask, size_t host_size, abi_ulong target_addr, @@ -8023,17 +8149,7 @@ static abi_long do_syscall1(void *cpu_env, int num, = abi_long arg1, =20 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)); + ret =3D qemu_execve(p, argp, envp); unlock_user(p, arg1, 0); =20 goto execve_end; --=20 2.27.0