From patchwork Fri Feb 12 18:48:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 381872 Delivered-To: patch@linaro.org Received: by 2002:a02:b18a:0:0:0:0:0 with SMTP id t10csp3197688jah; Fri, 12 Feb 2021 11:22:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJzRUWoXTEefqAdYzGRqmVDjfIWQVnwxmpjlVyURDtoUBl5Fp7azzRHxuexPNLZ8TQouHLdt X-Received: by 2002:a25:3b92:: with SMTP id i140mr6346016yba.187.1613157755123; Fri, 12 Feb 2021 11:22:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613157755; cv=none; d=google.com; s=arc-20160816; b=S6B01jvzzdFYT6aGCFSfMBx+xz7yR+zI/WI9/E44YO4KBs75js8GsQMrcK95LeH7D1 F28FkvFSzVT0SAwlidQjSvsB6tQ8L42NL6MsLNizdPzydoMtIwIjrLdGkGQ+a17JRgQN N0j16uoMAg04ZVQKgG2FKIDp6wkjLphCqs1nGpVFh7I4Ph46bvkbopP6vZIENFCLMOUs XibOzbvmyqcmJiDZTVud5OdbW974q+Nn1EtnqX244yQxZ6vcEVxAHiqRBcDjhDDnYtZC 8cdraT5M/ByF3cj44Fr3krui4nzvRTklhuN47WTBnLNqYOdnd6jElFn2GxEnvcr0yY4+ DzQQ== 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:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=rmu2PU4rJNhYRjC32Fq6chUgbDJkHTIm3xnv7XFmDE4=; b=j7WwSNH3wDgPhhyEb4EQSTbnUcu4N41tpPswl2nhAahzH5asgc7lYCMrb7GkjxHAq1 t+zkKR1DSgmE2jMwzStxxwYkQXzrBiy8jjPzpd/q0CCwavEEL1u2gxRDG1btq0PzpXg2 T76xVQyPIrgSQotIZQSN1a7XXAlAO+D1I+oNBxE6f7+mu8m0ScaKpFptBRvNdLLfPtCg 31dq9qvgpxEBBZJvbTZISAbsAJplt5B0iIsrmoGpdV9FFGRGbtGxpml5W9RIvJ59F2cB NbLZPCnGJPKlQhDKP/WmGraUBzYNLu7JEL/wkdyM1cyk/5NdPAHUqQ/6HZ/FQQ8RsQIl Gj8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zSJXWIQa; 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=pass (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 w6si8971027ybq.238.2021.02.12.11.22.35 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 12 Feb 2021 11:22:35 -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=pass header.i=@linaro.org header.s=google header.b=zSJXWIQa; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:50184 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lAe1S-0006xj-Je for patch@linaro.org; Fri, 12 Feb 2021 14:22:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39566) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lAdVY-0001oF-Fz for qemu-devel@nongnu.org; Fri, 12 Feb 2021 13:49:36 -0500 Received: from mail-pg1-x531.google.com ([2607:f8b0:4864:20::531]:39053) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lAdVT-0007q9-FT for qemu-devel@nongnu.org; Fri, 12 Feb 2021 13:49:36 -0500 Received: by mail-pg1-x531.google.com with SMTP id o63so245561pgo.6 for ; Fri, 12 Feb 2021 10:49:27 -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 :mime-version:content-transfer-encoding; bh=rmu2PU4rJNhYRjC32Fq6chUgbDJkHTIm3xnv7XFmDE4=; b=zSJXWIQaXGfP5Qck+HSWS4lMOHNwAZ8McYIMM+fNo2TxDMxKSY3h2ILRjmNZ/snR// 9MQaGCLCnUDjnyrXBbHzKetHnDZFCgXRgZ1ibXtt3uvEAPTi5YIZVkZMtLQLYEThke7T Oo+2aGIRzuv+UgHaSxwN+h/mQC0iKkd8fWRd1vB9LEa2h8kn5ItPGcp3HsZZMCrRv6AW iknUvVztbI1ADXbyz+IK15/fOo7wIU0OUv44bimQVUJM5TUJ1fOm6ZojQLgzWxHoKAzr gcKnfU4kT6Qk304kkWG8QCDV0I+vfQ3r6bDXUJ3m+5hwuYdHVcjP+CaaqIpqFjm53ttk E0iQ== 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:mime-version:content-transfer-encoding; bh=rmu2PU4rJNhYRjC32Fq6chUgbDJkHTIm3xnv7XFmDE4=; b=qqhV5lYwVQ5tL6hLNkjV7zo/bJrp9hn11eViWBqEDJ/hhWVfOFBNAZ54TH8IYgDLkh buOOBq6VYITmvxg7no6m1TpZZz/NMkjmNLMWNcox5Il1nyR+Xp5oSfGoDpCZC5JfgCXS 46XwnvcgHRquQW6G2TKeLi2Bnzx5UxAv5PNEuLJ7FaZkrEpzgSGdysH6bfCklCwmjfEO tIA8MqWF+fQIWpsAUocbAoFEeiGvgQsqiJfXWrBlYZYmLjp0IP4WV1hmtf702o/VBIea eY/C2OLPGaATrnwSrcAfyx4SOT9/2xFNmqV/h114N03IC3Ux7T30A2AltThjjSbB69bu lAFA== X-Gm-Message-State: AOAM531ntm1KGrRSxpHbwQy/x8O7i0TVrPNzIduTP7WkbDMrl773uMs/ H5OdMCKT/hTly/I2SsXrGNuNH9IOOj917g== X-Received: by 2002:a62:fb0b:0:b029:1d8:fdca:60f2 with SMTP id x11-20020a62fb0b0000b02901d8fdca60f2mr4039312pfm.70.1613155766234; Fri, 12 Feb 2021 10:49:26 -0800 (PST) Received: from localhost.localdomain (174-21-150-71.tukw.qwest.net. [174.21.150.71]) by smtp.gmail.com with ESMTPSA id k5sm9427618pfi.31.2021.02.12.10.49.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Feb 2021 10:49:25 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v7 18/31] linux-user: Fix types in uaccess.c Date: Fri, 12 Feb 2021 10:48:49 -0800 Message-Id: <20210212184902.1251044-19-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210212184902.1251044-1-richard.henderson@linaro.org> References: <20210212184902.1251044-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::531; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x531.google.com 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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-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: , Cc: peter.maydell@linaro.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" For copy_*_user, only 0 and -TARGET_EFAULT are returned; no need to involve abi_long. Use size_t for lengths. Use bool for the lock_user copy argument. Use ssize_t for target_strlen, because we can't overflow the host memory space. Reviewed-by: Peter Maydell Signed-off-by: Richard Henderson --- linux-user/qemu.h | 14 ++++++-------- linux-user/uaccess.c | 45 ++++++++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 30 deletions(-) -- 2.25.1 Reviewed-by: Philippe Mathieu-Daudé diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 36b58bd840..d25a5dafc0 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -7,8 +7,6 @@ #include "exec/cpu_ldst.h" #undef DEBUG_REMAP -#ifdef DEBUG_REMAP -#endif /* DEBUG_REMAP */ #include "exec/user/abitypes.h" @@ -629,8 +627,8 @@ static inline bool access_ok(CPUState *cpu, int type, * buffers between the target and host. These internally perform * locking/unlocking of the memory. */ -abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len); -abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len); +int copy_from_user(void *hptr, abi_ulong gaddr, size_t len); +int copy_to_user(abi_ulong gaddr, void *hptr, size_t len); /* Functions for accessing guest memory. The tget and tput functions read/write single values, byteswapping as necessary. The lock_user function @@ -640,13 +638,13 @@ abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len); /* Lock an area of guest memory into the host. If copy is true then the host area will have the same contents as the guest. */ -void *lock_user(int type, abi_ulong guest_addr, long len, int copy); +void *lock_user(int type, abi_ulong guest_addr, size_t len, bool copy); /* Unlock an area of guest memory. The first LEN bytes must be flushed back to guest memory. host_ptr = NULL is explicitly allowed and does nothing. */ -#ifdef DEBUG_REMAP -static inline void unlock_user(void *host_ptr, abi_ulong guest_addr, long len) +#ifndef DEBUG_REMAP +static inline void unlock_user(void *host_ptr, abi_ulong guest_addr, size_t len) { } #else void unlock_user(void *host_ptr, abi_ulong guest_addr, long len); @@ -654,7 +652,7 @@ void unlock_user(void *host_ptr, abi_ulong guest_addr, long len); /* Return the length of a string in target memory or -TARGET_EFAULT if access error. */ -abi_long target_strlen(abi_ulong gaddr); +ssize_t target_strlen(abi_ulong gaddr); /* Like lock_user but for null terminated strings. */ void *lock_user_string(abi_ulong guest_addr); diff --git a/linux-user/uaccess.c b/linux-user/uaccess.c index bba012ed15..76af6a92b1 100644 --- a/linux-user/uaccess.c +++ b/linux-user/uaccess.c @@ -4,7 +4,7 @@ #include "qemu.h" -void *lock_user(int type, abi_ulong guest_addr, long len, int copy) +void *lock_user(int type, abi_ulong guest_addr, size_t len, bool copy) { if (!access_ok_untagged(type, guest_addr, len)) { return NULL; @@ -26,7 +26,7 @@ void *lock_user(int type, abi_ulong guest_addr, long len, int copy) } #ifdef DEBUG_REMAP -void unlock_user(void *host_ptr, abi_ulong guest_addr, long len); +void unlock_user(void *host_ptr, abi_ulong guest_addr, size_t len); { if (!host_ptr) { return; @@ -34,7 +34,7 @@ void unlock_user(void *host_ptr, abi_ulong guest_addr, long len); if (host_ptr == g2h_untagged(guest_addr)) { return; } - if (len > 0) { + if (len != 0) { memcpy(g2h_untagged(guest_addr), host_ptr, len); } g_free(host_ptr); @@ -43,53 +43,53 @@ void unlock_user(void *host_ptr, abi_ulong guest_addr, long len); void *lock_user_string(abi_ulong guest_addr) { - abi_long len = target_strlen(guest_addr); + ssize_t len = target_strlen(guest_addr); if (len < 0) { return NULL; } - return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1); + return lock_user(VERIFY_READ, guest_addr, (size_t)len + 1, 1); } /* copy_from_user() and copy_to_user() are usually used to copy data * buffers between the target and host. These internally perform * locking/unlocking of the memory. */ -abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len) +int copy_from_user(void *hptr, abi_ulong gaddr, size_t len) { - abi_long ret = 0; - void *ghptr; + int ret = 0; + void *ghptr = lock_user(VERIFY_READ, gaddr, len, 1); - if ((ghptr = lock_user(VERIFY_READ, gaddr, len, 1))) { + if (ghptr) { memcpy(hptr, ghptr, len); unlock_user(ghptr, gaddr, 0); - } else + } else { ret = -TARGET_EFAULT; - + } return ret; } - -abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len) +int copy_to_user(abi_ulong gaddr, void *hptr, size_t len) { - abi_long ret = 0; - void *ghptr; + int ret = 0; + void *ghptr = lock_user(VERIFY_WRITE, gaddr, len, 0); - if ((ghptr = lock_user(VERIFY_WRITE, gaddr, len, 0))) { + if (ghptr) { memcpy(ghptr, hptr, len); unlock_user(ghptr, gaddr, len); - } else + } else { ret = -TARGET_EFAULT; + } return ret; } /* Return the length of a string in target memory or -TARGET_EFAULT if access error */ -abi_long target_strlen(abi_ulong guest_addr1) +ssize_t target_strlen(abi_ulong guest_addr1) { uint8_t *ptr; abi_ulong guest_addr; - int max_len, len; + size_t max_len, len; guest_addr = guest_addr1; for(;;) { @@ -101,11 +101,12 @@ abi_long target_strlen(abi_ulong guest_addr1) unlock_user(ptr, guest_addr, 0); guest_addr += len; /* we don't allow wrapping or integer overflow */ - if (guest_addr == 0 || - (guest_addr - guest_addr1) > 0x7fffffff) + if (guest_addr == 0 || (guest_addr - guest_addr1) > 0x7fffffff) { return -TARGET_EFAULT; - if (len != max_len) + } + if (len != max_len) { break; + } } return guest_addr - guest_addr1; }