From nobody Fri May 3 19:55:50 2024 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 Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1512449414663551.0325735942961; Mon, 4 Dec 2017 20:50:14 -0800 (PST) Received: from localhost ([::1]:46385 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eM5BB-0001is-8b for importer@patchew.org; Mon, 04 Dec 2017 23:50:01 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46173) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eM5A4-0001Pe-Uq for qemu-devel@nongnu.org; Mon, 04 Dec 2017 23:48:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eM5A1-00026C-OH for qemu-devel@nongnu.org; Mon, 04 Dec 2017 23:48:53 -0500 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]:37617) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eM5A1-00025V-Eu for qemu-devel@nongnu.org; Mon, 04 Dec 2017 23:48:49 -0500 Received: by mail-pf0-x244.google.com with SMTP id n6so10338947pfa.4 for ; Mon, 04 Dec 2017 20:48:49 -0800 (PST) Received: from cloudburst.twiddle.net (97-126-111-33.tukw.qwest.net. [97.126.111.33]) by smtp.gmail.com with ESMTPSA id p10sm27269119pfl.32.2017.12.04.20.48.45 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 04 Dec 2017 20:48:45 -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; bh=JY+dEpHz+8jZnxXDBly8S6X9BayFUCJcns5i6z/VVeU=; b=ZmVUuOO/S9syc4tSxUVIdBT3SpJDMUZiLanLIVDSPrMGuAia6dsZ/mhMgehAxj8NaD NJTqbyrLKqb1CeSfz4Gy1h3S/4c+AMKsBPjHKDIEQo7akcy/7/lspKgSaPs7pOci/HB0 JC34CcQSP7z308dRdLLNwhUIuPxALnj7LNO/U= 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; bh=JY+dEpHz+8jZnxXDBly8S6X9BayFUCJcns5i6z/VVeU=; b=bEO6UndzWRf/cP6w4B3mND+Rx0TUTXcDUiJq3n+eEGP7oc/T2Z+Tv2Vyo4rMRT3mfx dj/SaWQ+d2kILq/0YMeAHjoKndM+Q7aiw86aK2LfTqMkAHrzn2N2C1uFedEyQP5oTumB P8jUja01in4LbykYiLyYS/qvmZEppCmsz1H97Vka5EZx4cr2gWSZbLdZDMKJP3pxraCn RGh0MlmBPMcn5q/JwTtnVoE+9HeXp5vZtFPuMrFD5okaLajfGR6apent2pAP3DvTT7hp Q351JYESf9HZ7Y9e/uXTYHa+jMSx51+MtZolrSyD/Utr46xCSWP1rXJoUBYf5M/EH9JW 6zsA== X-Gm-Message-State: AJaThX6yXqt/htO1LwTtkkP5lzW0OdLEpG+MMx20wUBQ7fbYVv7/VDy0 Al1geyB3jfJHAiMhstlD0882Xe9F5ts= X-Google-Smtp-Source: AGs4zMYSzWY2H1aPVKGekQT2pGm2sayuQR6b1dFNY317ItrprOJklATF9yIV6Qr5fNTFGiHCWj3BWQ== X-Received: by 10.159.252.1 with SMTP id n1mr16817987pls.415.1512449327047; Mon, 04 Dec 2017 20:48:47 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Date: Mon, 4 Dec 2017 20:48:44 -0800 Message-Id: <20171205044844.3597-1-richard.henderson@linaro.org> X-Mailer: git-send-email 2.13.6 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::244 Subject: [Qemu-devel] [PATCH v2] linux-user: Use *at functions instead of caching interp_prefix contents 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: peter.maydell@linaro.org, Richard Henderson 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" From: Richard Henderson If the interp_prefix is a complete chroot, it may have a *lot* of files. Setting up the cache for this is quite expensive. Instead, use the *at versions of various syscalls to attempt the operation in the prefix. Signed-off-by: Richard Henderson --- Changes since v1 (Nov 2016): * Require interp_dirfd set before trying the *at path. r~ --- linux-user/qemu.h | 1 + linux-user/elfload.c | 12 ++- linux-user/main.c | 3 +- linux-user/syscall.c | 236 ++++++++++++++++++++++++++++++++++++++++++-----= ---- 4 files changed, 208 insertions(+), 44 deletions(-) diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 4edd7d0c08..d04924fd2e 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -437,6 +437,7 @@ void mmap_fork_start(void); void mmap_fork_end(int child); =20 /* main.c */ +extern int interp_dirfd; extern unsigned long guest_stack_size; =20 /* user access */ diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 20f3d8c2c3..5d3f7aa11d 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2203,7 +2203,17 @@ static void load_elf_interp(const char *filename, st= ruct image_info *info, { int fd, retval; =20 - fd =3D open(path(filename), O_RDONLY); + switch (interp_dirfd > 0 && filename[0] =3D=3D '/') { + case true: + fd =3D openat(interp_dirfd, filename + 1, O_RDONLY); + if (fd >=3D 0 || errno !=3D ENOENT) { + break; + } + /* fallthru */ + default: + fd =3D open(filename, O_RDONLY); + break; + } if (fd < 0) { goto exit_perror; } diff --git a/linux-user/main.c b/linux-user/main.c index 6286661bd3..06edc2c6bc 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -99,6 +99,7 @@ unsigned long reserved_va; static void usage(int exitcode); =20 static const char *interp_prefix =3D CONFIG_QEMU_INTERP_PREFIX; +int interp_dirfd; const char *qemu_uname_release; =20 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <=3D address + 32, so @@ -4297,7 +4298,7 @@ int main(int argc, char **argv, char **envp) memset(&bprm, 0, sizeof (bprm)); =20 /* Scan interp_prefix dir for replacement files. */ - init_paths(interp_prefix); + interp_dirfd =3D open(interp_prefix, O_CLOEXEC | O_DIRECTORY | O_PATH); =20 init_qemu_uname_release(); =20 diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 11c9116c4a..dc4387e3e6 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -7228,7 +7228,18 @@ static abi_long do_name_to_handle_at(abi_long dirfd,= abi_long pathname, fh =3D g_malloc0(total_size); fh->handle_bytes =3D size; =20 - ret =3D get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags= )); + switch (interp_dirfd > 0 && name[0] =3D=3D '/') { + case true: + ret =3D name_to_handle_at(interp_dirfd, name + 1, fh, &mid, flags); + if (ret =3D=3D 0 || errno !=3D ENOENT) { + break; + } + /* fallthru */ + default: + ret =3D name_to_handle_at(dirfd, name, fh, &mid, flags); + break; + } + ret =3D get_errno(ret); unlock_user(name, pathname, 0); =20 /* man name_to_handle_at(2): @@ -7604,6 +7615,7 @@ static int do_openat(void *cpu_env, int dirfd, const = char *pathname, int flags, #endif { NULL, NULL, NULL } }; + int ret; =20 if (is_proc_myself(pathname, "exe")) { int execfd =3D qemu_getauxval(AT_EXECFD); @@ -7643,7 +7655,18 @@ static int do_openat(void *cpu_env, int dirfd, const= char *pathname, int flags, return fd; } =20 - return safe_openat(dirfd, path(pathname), flags, mode); + switch (interp_dirfd > 0 && pathname[0] =3D=3D '/') { + case true: + ret =3D safe_openat(interp_dirfd, pathname + 1, flags, mode); + if (ret >=3D 0 || errno !=3D ENOENT) { + break; + } + /* fallthru */ + default: + ret =3D safe_openat(dirfd, pathname, flags, mode); + break; + } + return ret; } =20 #define TIMER_MAGIC 0x0caf0000 @@ -7729,6 +7752,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long = arg1, struct stat st; struct statfs stfs; void *p; + char *fn; =20 #if defined(DEBUG_ERESTARTSYS) /* Debug-only code for exercising the syscall-restart code paths @@ -8260,10 +8284,21 @@ abi_long do_syscall(void *cpu_env, int num, abi_lon= g arg1, } else { tvp =3D NULL; } - if (!(p =3D lock_user_string(arg2))) + if (!(fn =3D lock_user_string(arg2))) goto efault; - ret =3D get_errno(futimesat(arg1, path(p), tvp)); - unlock_user(p, arg2, 0); + switch (interp_dirfd > 0 && fn[0] =3D=3D '/') { + case true: + ret =3D futimesat(interp_dirfd, fn + 1, tvp); + if (ret =3D=3D 0 || errno !=3D ENOENT) { + break; + } + /* fallthru */ + default: + ret =3D futimesat(arg1, fn, tvp); + break; + } + ret =3D get_errno(ret); + unlock_user(fn, arg2, 0); } break; #endif @@ -8277,18 +8312,42 @@ abi_long do_syscall(void *cpu_env, int num, abi_lon= g arg1, #endif #ifdef TARGET_NR_access case TARGET_NR_access: - if (!(p =3D lock_user_string(arg1))) + if (!(fn =3D lock_user_string(arg1))) { goto efault; - ret =3D get_errno(access(path(p), arg2)); - unlock_user(p, arg1, 0); + } + switch (interp_dirfd > 0 && fn[0] =3D=3D '/') { + case true: + ret =3D faccessat(interp_dirfd, fn + 1, arg2, 0); + if (ret =3D=3D 0 || errno !=3D ENOENT) { + break; + } + /* fallthru */ + default: + ret =3D access(fn, arg2); + break; + } + ret =3D get_errno(ret); + unlock_user(fn, arg1, 0); break; #endif #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat) case TARGET_NR_faccessat: - if (!(p =3D lock_user_string(arg2))) + if (!(fn =3D lock_user_string(arg2))) { goto efault; - ret =3D get_errno(faccessat(arg1, p, arg3, 0)); - unlock_user(p, arg2, 0); + } + switch (interp_dirfd > 0 && fn[0] =3D=3D '/') { + case true: + ret =3D faccessat(interp_dirfd, fn + 1, arg3, 0); + if (ret =3D=3D 0 || errno !=3D ENOENT) { + break; + } + /* fallthru */ + default: + ret =3D faccessat(arg1, fn, arg3, 0); + break; + } + ret =3D get_errno(ret); + unlock_user(fn, arg2, 0); break; #endif #ifdef TARGET_NR_nice /* not on alpha */ @@ -8415,7 +8474,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long = arg1, } else { if (!(p =3D lock_user_string(arg1))) goto efault; - ret =3D get_errno(acct(path(p))); + ret =3D get_errno(acct(p)); unlock_user(p, arg1, 0); } break; @@ -9181,14 +9240,14 @@ abi_long do_syscall(void *cpu_env, int num, abi_lon= g arg1, case TARGET_NR_readlink: { void *p2; - p =3D lock_user_string(arg1); + fn =3D lock_user_string(arg1); p2 =3D lock_user(VERIFY_WRITE, arg2, arg3, 0); - if (!p || !p2) { + if (!fn || !p2) { ret =3D -TARGET_EFAULT; } else if (!arg3) { /* Short circuit this for the magic exe check. */ ret =3D -TARGET_EINVAL; - } else if (is_proc_myself((const char *)p, "exe")) { + } else if (is_proc_myself(fn, "exe")) { char real[PATH_MAX], *temp; temp =3D realpath(exec_path, real); /* Return value is # of bytes that we wrote to the buffer.= */ @@ -9202,10 +9261,21 @@ abi_long do_syscall(void *cpu_env, int num, abi_lon= g arg1, memcpy(p2, real, ret); } } else { - ret =3D get_errno(readlink(path(p), p2, arg3)); + switch (interp_dirfd > 0 && fn[0] =3D=3D '/') { + case true: + ret =3D readlinkat(interp_dirfd, fn + 1, p2, arg3); + if (ret =3D=3D 0 || errno !=3D ENOENT) { + break; + } + /* fallthru */ + default: + ret =3D readlink(fn, p2, arg3); + break; + } + ret =3D get_errno(ret); } unlock_user(p2, arg2, ret); - unlock_user(p, arg1, 0); + unlock_user(fn, arg1, 0); } break; #endif @@ -9213,20 +9283,31 @@ abi_long do_syscall(void *cpu_env, int num, abi_lon= g arg1, case TARGET_NR_readlinkat: { void *p2; - p =3D lock_user_string(arg2); + fn =3D lock_user_string(arg2); p2 =3D lock_user(VERIFY_WRITE, arg3, arg4, 0); - if (!p || !p2) { + if (!fn || !p2) { ret =3D -TARGET_EFAULT; - } else if (is_proc_myself((const char *)p, "exe")) { + } else if (is_proc_myself(fn, "exe")) { char real[PATH_MAX], *temp; temp =3D realpath(exec_path, real); ret =3D temp =3D=3D NULL ? get_errno(-1) : strlen(real) ; snprintf((char *)p2, arg4, "%s", real); } else { - ret =3D get_errno(readlinkat(arg1, path(p), p2, arg4)); + switch (interp_dirfd && fn[0] =3D=3D '/') { + case true: + ret =3D readlinkat(interp_dirfd, fn + 1, p2, arg4); + if (ret =3D=3D 0 || errno !=3D ENOENT) { + break; + } + /* fallthru */ + default: + ret =3D readlinkat(arg1, fn, p2, arg4); + break; + } + ret =3D get_errno(ret); } unlock_user(p2, arg3, ret); - unlock_user(p, arg2, 0); + unlock_user(fn, arg2, 0); } break; #endif @@ -9395,7 +9476,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long = arg1, case TARGET_NR_statfs: if (!(p =3D lock_user_string(arg1))) goto efault; - ret =3D get_errno(statfs(path(p), &stfs)); + ret =3D get_errno(statfs(p, &stfs)); unlock_user(p, arg1, 0); convert_statfs: if (!is_error(ret)) { @@ -9425,7 +9506,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long = arg1, case TARGET_NR_statfs64: if (!(p =3D lock_user_string(arg1))) goto efault; - ret =3D get_errno(statfs(path(p), &stfs)); + ret =3D get_errno(statfs(p, &stfs)); unlock_user(p, arg1, 0); convert_statfs64: if (!is_error(ret)) { @@ -9654,18 +9735,42 @@ abi_long do_syscall(void *cpu_env, int num, abi_lon= g arg1, break; #ifdef TARGET_NR_stat case TARGET_NR_stat: - if (!(p =3D lock_user_string(arg1))) + if (!(fn =3D lock_user_string(arg1))) { goto efault; - ret =3D get_errno(stat(path(p), &st)); - unlock_user(p, arg1, 0); + } + switch (interp_dirfd > 0 && fn[0] =3D=3D '/') { + case true: + ret =3D fstatat(interp_dirfd, fn + 1, &st, 0); + if (ret =3D=3D 0 || errno !=3D ENOENT) { + break; + } + /* fallthru */ + default: + ret =3D stat(fn, &st); + break; + } + ret =3D get_errno(ret); + unlock_user(fn, arg1, 0); goto do_stat; #endif #ifdef TARGET_NR_lstat case TARGET_NR_lstat: - if (!(p =3D lock_user_string(arg1))) + if (!(fn =3D lock_user_string(arg1))) { goto efault; - ret =3D get_errno(lstat(path(p), &st)); - unlock_user(p, arg1, 0); + } + switch (interp_dirfd > 0 && fn[0] =3D=3D '/') { + case true: + ret =3D fstatat(interp_dirfd, fn + 1, &st, AT_SYMLINK_NOFOLLOW= ); + if (ret =3D=3D 0 || errno !=3D ENOENT) { + break; + } + /* fallthru */ + default: + ret =3D lstat(fn, &st); + break; + } + ret =3D get_errno(ret); + unlock_user(fn, arg1, 0); goto do_stat; #endif case TARGET_NR_fstat: @@ -10745,20 +10850,44 @@ abi_long do_syscall(void *cpu_env, int num, abi_l= ong arg1, #endif #ifdef TARGET_NR_stat64 case TARGET_NR_stat64: - if (!(p =3D lock_user_string(arg1))) + if (!(fn =3D lock_user_string(arg1))) { goto efault; - ret =3D get_errno(stat(path(p), &st)); - unlock_user(p, arg1, 0); + } + switch (interp_dirfd > 0 && fn[0] =3D=3D '/') { + case true: + ret =3D fstatat(interp_dirfd, fn + 1, &st, 0); + if (ret =3D=3D 0 || errno !=3D ENOENT) { + break; + } + /* fallthru */ + default: + ret =3D stat(fn, &st); + break; + } + ret =3D get_errno(ret); + unlock_user(fn, arg1, 0); if (!is_error(ret)) ret =3D host_to_target_stat64(cpu_env, arg2, &st); break; #endif #ifdef TARGET_NR_lstat64 case TARGET_NR_lstat64: - if (!(p =3D lock_user_string(arg1))) + if (!(fn =3D lock_user_string(arg1))) { goto efault; - ret =3D get_errno(lstat(path(p), &st)); - unlock_user(p, arg1, 0); + } + switch (interp_dirfd > 0 && fn[0] =3D=3D '/') { + case true: + ret =3D fstatat(interp_dirfd, fn + 1, &st, AT_SYMLINK_NOFOLLOW= ); + if (ret =3D=3D 0 || errno !=3D ENOENT) { + break; + } + /* fallthru */ + default: + ret =3D lstat(fn, &st); + break; + } + ret =3D get_errno(ret); + unlock_user(fn, arg1, 0); if (!is_error(ret)) ret =3D host_to_target_stat64(cpu_env, arg2, &st); break; @@ -10777,9 +10906,21 @@ abi_long do_syscall(void *cpu_env, int num, abi_lo= ng arg1, #ifdef TARGET_NR_newfstatat case TARGET_NR_newfstatat: #endif - if (!(p =3D lock_user_string(arg2))) + if (!(fn =3D lock_user_string(arg2))) goto efault; - ret =3D get_errno(fstatat(arg1, path(p), &st, arg4)); + switch (interp_dirfd > 0 && fn[0] =3D=3D '/') { + case true: + ret =3D fstatat(interp_dirfd, fn + 1, &st, arg4); + if (ret =3D=3D 0 || errno !=3D ENOENT) { + break; + } + /* fallthru */ + default: + ret =3D fstatat(arg1, fn, &st, arg4); + break; + } + ret =3D get_errno(ret); + unlock_user(fn, arg2, 0); if (!is_error(ret)) ret =3D host_to_target_stat64(cpu_env, arg3, &st); break; @@ -11776,12 +11917,23 @@ abi_long do_syscall(void *cpu_env, int num, abi_l= ong arg1, if (!arg2) ret =3D get_errno(sys_utimensat(arg1, NULL, tsp, arg4)); else { - if (!(p =3D lock_user_string(arg2))) { + if (!(fn =3D lock_user_string(arg2))) { ret =3D -TARGET_EFAULT; goto fail; } - ret =3D get_errno(sys_utimensat(arg1, path(p), tsp, arg4)); - unlock_user(p, arg2, 0); + switch (interp_dirfd > 0 && fn[0] =3D=3D '/') { + case true: + ret =3D sys_utimensat(interp_dirfd, fn + 1, tsp, arg4); + if (ret =3D=3D 0 || errno !=3D ENOENT) { + break; + } + /* fallthru */ + default: + ret =3D sys_utimensat(arg1, fn, tsp, arg4); + break; + } + ret =3D get_errno(ret); + unlock_user(fn, arg2, 0); } } break; @@ -11811,7 +11963,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_lon= g arg1, #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch) case TARGET_NR_inotify_add_watch: p =3D lock_user_string(arg2); - ret =3D get_errno(sys_inotify_add_watch(arg1, path(p), arg3)); + ret =3D get_errno(sys_inotify_add_watch(arg1, p, arg3)); unlock_user(p, arg2, 0); break; #endif --=20 2.13.6