From patchwork Fri Jan 18 21:30:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 156057 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp3732953jaa; Fri, 18 Jan 2019 13:39:50 -0800 (PST) X-Google-Smtp-Source: ALg8bN7BG4qQ1pvVic0pHlQrZDOOm2aKMjmMqyK1RNUcsvW7BhD7UkV7kso3Rs2TPEZKiaEJn8ca X-Received: by 2002:a1c:70b:: with SMTP id 11mr17593210wmh.74.1547847590350; Fri, 18 Jan 2019 13:39:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547847590; cv=none; d=google.com; s=arc-20160816; b=xI4aBd2FLnDRmN1aJFjzi0mUz/aXyCw9OOcBFJrtlXBdn0XJYpdvQUyVKNt1OWzgnh 7dMaXE92S5RKomX557m6o+XeB7darIkBdSzVi+x8UDJGPlFlUL/mP7ff61xF3Gy6ppi+ hG+VPgjKn8Zp4Uh1eN1jHMncb8UhCZGnbEKYJx2lbIhBAfifxY+AnXlyEv3kUmnVnStA zXpihqjrCatw8jx8h3lnEjcag2OB6jjFn/IxFnN7MKzBKMWqTZ1ON1nn2GAeKkQkqgBk sli9Keui45Rny+NmfIGOtLPcixCb1IyODl1HZWqzejnsefW430DtmLoATaL2tFdz+05n LyPQ== 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; bh=0FyNzZUX6dDuZ4RmFXTfDNUyXGiazdUeQzMHZNrjzJM=; b=c5RD61gzldN5fmuhrcJRhUcIuQNkpqs2QTuEnuVAEb8t1c701hdLOgxZrztm8YY9FB bSOaQSLQfyWK68YNOoxU58qzMkH03nuZaml3bhFUhSCxcqAwAjs6JTbrzaHOm4uwLuZb T34dpQOXpW9RKODvqGdiV8HMRejtdK/eoqU+dDyHv5phkjWBVgLr6b6GAfIo0FkbJcHY QHOctLcF8DWRrZJMWnIuPhCvLSxWAXKUeRH4c6tmYDsOpRjpn7Eutt1CAmzoaiqOIMdZ Udrx06C5aIGMlqEZj+xYfIzPi3moh8WRmXrSrnO0XeDu8dBbFCJSnEwqZFwX6hagArAk dUlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=Zu2qZHMX; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 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. [209.51.188.17]) by mx.google.com with ESMTPS id g205si29977200wme.36.2019.01.18.13.39.50 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 18 Jan 2019 13:39:50 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=Zu2qZHMX; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 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 ([127.0.0.1]:47478 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gkbrh-0001iD-5j for patch@linaro.org; Fri, 18 Jan 2019 16:39:49 -0500 Received: from eggs.gnu.org ([209.51.188.92]:55735) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gkbjv-0004i0-70 for qemu-devel@nongnu.org; Fri, 18 Jan 2019 16:31:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gkbjt-0003xT-HD for qemu-devel@nongnu.org; Fri, 18 Jan 2019 16:31:47 -0500 Received: from mail-pg1-x544.google.com ([2607:f8b0:4864:20::544]:33666) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gkbjt-0003ss-9V for qemu-devel@nongnu.org; Fri, 18 Jan 2019 16:31:45 -0500 Received: by mail-pg1-x544.google.com with SMTP id z11so6660047pgu.0 for ; Fri, 18 Jan 2019 13:31:43 -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=0FyNzZUX6dDuZ4RmFXTfDNUyXGiazdUeQzMHZNrjzJM=; b=Zu2qZHMX9pG+XkGDq4AFNgb87YQP3l9FPEj+L5//1ilncCwa5sIVk3+FuwhjO5461M tJGiFVcwayJGPNq2fgJOec8EOK5L2PrLtZi9R3ybuq/hCPk9GuWPoT+UAjeMCaMG2/p5 ZbEHweTSNrvmvAJHV2S9GnyHgnBoz8kPe9vQY= 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=0FyNzZUX6dDuZ4RmFXTfDNUyXGiazdUeQzMHZNrjzJM=; b=AjLWyGj5adOW/84fNMugAX3/tUembRXEwp+2LJCBSHGTWhgWjvrWfWwHbKmKO3YPos wCiUD/wK/eMsoMnxerHftlH3XIKjZv9NwxVSd+3tkyMlUxNKmuQfEPsdCkfI0NHcGJ1/ wqxB4tFgPpdJrBLduMBB57hq/r/y2qYkj6ApCQsuaFDt99rr8mg3b5bLxabHNlWdTdGW 6/rUxLCGHxA4v6ktZOI9fYmWPKCVHD05uLsOWogt5arPfJ37Nwd9gniTClo1JxRFIahr z+aDY2WiUivMy6Nuqm3SiLfxiQ9SEiQUh1sG445OA9VhCvWIx1+SrUh7sJa50GIYqGPG F7AQ== X-Gm-Message-State: AJcUukeWpiyE9/LDlF4EQlZZWdJp2Pz5nF/dKDYG7AzviOTuaHpdGIqx O6rYdg3IiUci5LkYdrYrlSA/OUMPndQ= X-Received: by 2002:a62:1709:: with SMTP id 9mr20735392pfx.249.1547847101607; Fri, 18 Jan 2019 13:31:41 -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.31.39 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 18 Jan 2019 13:31:40 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Date: Sat, 19 Jan 2019 08:30:39 +1100 Message-Id: <20190118213122.22865-6-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::544 Subject: [Qemu-devel] [PATCH v6 06/49] linux-user: Split out readlink, readlinkat 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" Split out a shared implementation for both of these; unify the best parts of /proc/self/exe checking. Remove the temporary forward declaration for is_proc_self. Signed-off-by: Richard Henderson --- linux-user/syscall-defs.h | 6 ++++ linux-user/strace.c | 29 ------------------ linux-user/syscall-file.inc.c | 45 ++++++++++++++++++++++++++++ linux-user/syscall.c | 55 ----------------------------------- linux-user/strace.list | 6 ---- 5 files changed, 51 insertions(+), 90 deletions(-) -- 2.17.2 diff --git a/linux-user/syscall-defs.h b/linux-user/syscall-defs.h index 1f3a9c47ab..d1a6c6fa3c 100644 --- a/linux-user/syscall-defs.h +++ b/linux-user/syscall-defs.h @@ -20,3 +20,9 @@ SYSCALL_DEF(open, ARG_STR, ARG_OPENFLAG, ARG_MODEFLAG); #endif SYSCALL_DEF(openat, ARG_ATDIRFD, ARG_STR, ARG_OPENFLAG, ARG_MODEFLAG); +#ifdef TARGET_NR_readlink +SYSCALL_DEF(readlink, ARG_STR, ARG_PTR, ARG_DEC); +#endif +#ifdef TARGET_NR_readlinkat +SYSCALL_DEF(readlinkat, ARG_ATDIRFD, ARG_STR, ARG_PTR, ARG_DEC); +#endif diff --git a/linux-user/strace.c b/linux-user/strace.c index 1e4f6c9b53..4faeb8c031 100644 --- a/linux-user/strace.c +++ b/linux-user/strace.c @@ -2244,35 +2244,6 @@ print_fstatat64(const struct syscallname *name, #define print_newfstatat print_fstatat64 #endif -#ifdef TARGET_NR_readlink -static void -print_readlink(const struct syscallname *name, - abi_long arg0, abi_long arg1, abi_long arg2, - abi_long arg3, abi_long arg4, abi_long arg5) -{ - print_syscall_prologue(name); - print_string(arg0, 0); - print_pointer(arg1, 0); - print_raw_param("%u", arg2, 1); - print_syscall_epilogue(name); -} -#endif - -#ifdef TARGET_NR_readlinkat -static void -print_readlinkat(const struct syscallname *name, - abi_long arg0, abi_long arg1, abi_long arg2, - abi_long arg3, abi_long arg4, abi_long arg5) -{ - print_syscall_prologue(name); - print_at_dirfd(arg0, 0); - print_string(arg1, 0); - print_pointer(arg2, 0); - print_raw_param("%u", arg3, 1); - print_syscall_epilogue(name); -} -#endif - #ifdef TARGET_NR_rename static void print_rename(const struct syscallname *name, diff --git a/linux-user/syscall-file.inc.c b/linux-user/syscall-file.inc.c index f202a4c8f4..841795f8aa 100644 --- a/linux-user/syscall-file.inc.c +++ b/linux-user/syscall-file.inc.c @@ -308,3 +308,48 @@ SYSCALL_IMPL(openat) { return do_openat(cpu_env, arg1, arg2, arg3, arg4); } + +static abi_long do_readlinkat(int dirfd, abi_ulong target_path, + abi_ulong target_buf, abi_ulong bufsiz) +{ + char *p = lock_user_string(target_path); + void *buf = lock_user(VERIFY_WRITE, target_buf, bufsiz, 0); + abi_long ret; + + if (!p || !buf) { + ret = -TARGET_EFAULT; + } else if (!bufsiz) { + /* Short circuit this for the magic exe check. */ + ret = -TARGET_EINVAL; + } else if (is_proc_myself((const char *)p, "exe")) { + char real[PATH_MAX]; + char *temp = realpath(exec_path, real); + + if (temp == NULL) { + ret = -host_to_target_errno(errno); + } else { + ret = MIN(strlen(real), bufsiz); + /* We cannot NUL terminate the string. */ + memcpy(buf, real, ret); + } + } else { + ret = get_errno(readlinkat(dirfd, path(p), buf, bufsiz)); + } + unlock_user(buf, target_buf, ret); + unlock_user(p, target_path, 0); + return ret; +} + +#ifdef TARGET_NR_readlink +SYSCALL_IMPL(readlink) +{ + return do_readlinkat(AT_FDCWD, arg1, arg2, arg3); +} +#endif + +#ifdef TARGET_NR_readlinkat +SYSCALL_IMPL(readlinkat) +{ + return do_readlinkat(arg1, arg2, arg3, arg4); +} +#endif diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 32d1a285eb..035f17c0d9 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -6577,8 +6577,6 @@ int host_to_target_waitstatus(int status) return status; } -static int is_proc_myself(const char *filename, const char *entry); - #define TIMER_MAGIC 0x0caf0000 #define TIMER_MAGIC_MASK 0xffff0000 @@ -8052,59 +8050,6 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, } return ret; #endif -#ifdef TARGET_NR_readlink - case TARGET_NR_readlink: - { - void *p2; - p = lock_user_string(arg1); - p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0); - if (!p || !p2) { - ret = -TARGET_EFAULT; - } else if (!arg3) { - /* Short circuit this for the magic exe check. */ - ret = -TARGET_EINVAL; - } else if (is_proc_myself((const char *)p, "exe")) { - char real[PATH_MAX], *temp; - temp = realpath(exec_path, real); - /* Return value is # of bytes that we wrote to the buffer. */ - if (temp == NULL) { - ret = get_errno(-1); - } else { - /* Don't worry about sign mismatch as earlier mapping - * logic would have thrown a bad address error. */ - ret = MIN(strlen(real), arg3); - /* We cannot NUL terminate the string. */ - memcpy(p2, real, ret); - } - } else { - ret = get_errno(readlink(path(p), p2, arg3)); - } - unlock_user(p2, arg2, ret); - unlock_user(p, arg1, 0); - } - return ret; -#endif -#if defined(TARGET_NR_readlinkat) - case TARGET_NR_readlinkat: - { - void *p2; - p = lock_user_string(arg2); - p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0); - if (!p || !p2) { - ret = -TARGET_EFAULT; - } else if (is_proc_myself((const char *)p, "exe")) { - char real[PATH_MAX], *temp; - temp = realpath(exec_path, real); - ret = temp == NULL ? get_errno(-1) : strlen(real) ; - snprintf((char *)p2, arg4, "%s", real); - } else { - ret = get_errno(readlinkat(arg1, path(p), p2, arg4)); - } - unlock_user(p2, arg3, ret); - unlock_user(p, arg2, 0); - } - return ret; -#endif #ifdef TARGET_NR_swapon case TARGET_NR_swapon: if (!(p = lock_user_string(arg1))) diff --git a/linux-user/strace.list b/linux-user/strace.list index b2d3e99df7..2f466e5699 100644 --- a/linux-user/strace.list +++ b/linux-user/strace.list @@ -1079,12 +1079,6 @@ #ifdef TARGET_NR_readdir { TARGET_NR_readdir, "readdir" , NULL, NULL, NULL }, #endif -#ifdef TARGET_NR_readlink -{ TARGET_NR_readlink, "readlink" , NULL, print_readlink, NULL }, -#endif -#ifdef TARGET_NR_readlinkat -{ TARGET_NR_readlinkat, "readlinkat" , NULL, print_readlinkat, NULL }, -#endif #ifdef TARGET_NR_readv { TARGET_NR_readv, "readv" , NULL, NULL, NULL }, #endif