From nobody Wed Apr 16 08:11:26 2025 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=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1613050512; cv=none; d=zohomail.com; s=zohoarc; b=GtL8VRQ4+2wzGSjI4yBxrKP3wR2P7BxdoIfgvFd1rFP3O8B06/ximYoGSDnhhZvV61Gz3IH55lxXH02ze4cP/2vASvw1VshTaSQS3IknY0cg2gX7KFb+6ze6sXqgld/9Wsk28TlYPJUqYvO/Y7Cf7W+uN308IQtEXFNWmCIGIJ8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1613050512; h=Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=u7gQJdYfeB2TD4ECturIjPYtiu4xSyaQm8fEdsXqpVI=; b=YMRNVCQw4rIgCe4hS9T8UJ5ZHD87n1Cblyqx8gE9oLzIT2qdO5OvS9aVJKLLNWYy/AVCGtQ3CZ9LkZFQW6um7TiyhDN6iSAupNg1T4jM9xkonFrfcpH4+plom2rEYoRSuWtKAlmf958/3uF4tKu6LALUXQcdr+7vSp3rHGtpSuU= 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 1613050511676920.9876547370994; Thu, 11 Feb 2021 05:35:11 -0800 (PST) Received: from localhost ([::1]:49788 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lAC7i-0008W2-EQ for importer@patchew.org; Thu, 11 Feb 2021 08:35:10 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33708) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lABZG-0008L8-6s for qemu-devel@nongnu.org; Thu, 11 Feb 2021 07:59:35 -0500 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]:34189) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lABZ8-00004t-9g for qemu-devel@nongnu.org; Thu, 11 Feb 2021 07:59:33 -0500 Received: by mail-wm1-x32f.google.com with SMTP id o10so5932301wmc.1 for ; Thu, 11 Feb 2021 04:59:24 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id g15sm4955716wrx.1.2021.02.11.04.59.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Feb 2021 04:59:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=u7gQJdYfeB2TD4ECturIjPYtiu4xSyaQm8fEdsXqpVI=; b=WeV9fQflPVrwParK0E94Nf7FuHgOA3FB8Zme+PcF6ujHDVQbqcv5/yeOB+2YIur+gJ symK+GN7HtrkGB3VfKGvZqKMIXyzkamfA06D9B0wD5erHXKIqu4RiPiETAjUkJPjN9+m dlgmnqJF3Rc+tR+wa6TpVkBqD4FJ1refMH1jaDLJFeEFpiRh8Fr5IRTSVBun3t4jHL5r HC6mlhQ3Dthqno/IEr+sEKyQ9et/J7aGjAR1WerLw3jjuMNqwMAxGWz4o7k5V8fTbxBo V0Ikc0zeiHI3ALenFKn61T7NSLKs7WOGadJ4XR7Eemt7JJK+B/i/8ljEsSv6eJJNMASO 3Y3Q== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=u7gQJdYfeB2TD4ECturIjPYtiu4xSyaQm8fEdsXqpVI=; b=CnU4Wro1AbFDzmZ79+/Fzf2J1NfgMLZNgndWQW9Xe8ZnSDAGb4W9639Pu8kqlQq68U LfP16Mw84z2VZX+BnOZh8j0EDAL8iinjO0HcVb6OptZIO7/An/npj5XjyFbwPl0M9Prq r9Ehe2aQ9Xhj9MLTaBScsV7tPkOx2DlE6c45u9ahv0pxIcMxI8NjYwaCArlUghrkS4I/ Q2tr+VDvtYO4eO/TjQkF02T5dHzic81h4NzkOZXgU0F/De0GEzNtNoLVF9ZtjNVYYmH3 0HKOUcnINgshmvNq3X76DdfvFkw9JLnE6TZqNQy25PFKQZvkdzswfpgIjV5SoJBWxX1c eRpQ== X-Gm-Message-State: AOAM533EtXpCW3VyFe+7aHGV3dDAt+F6zCAAHyCKR9pbrpnKc9lvbDoI Jjzzuk7HEfS9/z3EENVUmLJ5nAwfQwo7MQ== X-Google-Smtp-Source: ABdhPJzwG39nNvn7B6xQUvMEoQCXxHHymIkZLdNbDfCXlgHAf9nJOohs4wJOEgmuO2551TlqTkQPkA== X-Received: by 2002:a7b:c95a:: with SMTP id i26mr5014145wml.164.1613048363284; Thu, 11 Feb 2021 04:59:23 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PULL 27/45] linux-user: Fix types in uaccess.c Date: Thu, 11 Feb 2021 12:58:42 +0000 Message-Id: <20210211125900.22777-28-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210211125900.22777-1-peter.maydell@linaro.org> References: <20210211125900.22777-1-peter.maydell@linaro.org> 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::32f; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x32f.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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) Content-Type: text/plain; charset="utf-8" From: Richard Henderson 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 Message-id: 20210210000223.884088-19-richard.henderson@linaro.org Signed-off-by: Peter Maydell --- linux-user/qemu.h | 14 ++++++-------- linux-user/uaccess.c | 45 ++++++++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 36b58bd840f..d25a5dafc0f 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -7,8 +7,6 @@ #include "exec/cpu_ldst.h" =20 #undef DEBUG_REMAP -#ifdef DEBUG_REMAP -#endif /* DEBUG_REMAP */ =20 #include "exec/user/abitypes.h" =20 @@ -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); =20 /* Functions for accessing guest memory. The tget and tput functions read/write single values, byteswapping as necessary. The lock_user fun= ction @@ -640,13 +638,13 @@ abi_long copy_to_user(abi_ulong gaddr, void *hptr, si= ze_t len); =20 /* 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); =20 /* Unlock an area of guest memory. The first LEN bytes must be flushed back to guest memory. host_ptr =3D 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); =20 /* 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); =20 /* 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 bba012ed159..76af6a92b11 100644 --- a/linux-user/uaccess.c +++ b/linux-user/uaccess.c @@ -4,7 +4,7 @@ =20 #include "qemu.h" =20 -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) } =20 #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, lo= ng len); if (host_ptr =3D=3D g2h_untagged(guest_addr)) { return; } - if (len > 0) { + if (len !=3D 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); =20 void *lock_user_string(abi_ulong guest_addr) { - abi_long len =3D target_strlen(guest_addr); + ssize_t len =3D 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); } =20 /* 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 =3D 0; - void *ghptr; + int ret =3D 0; + void *ghptr =3D lock_user(VERIFY_READ, gaddr, len, 1); =20 - if ((ghptr =3D lock_user(VERIFY_READ, gaddr, len, 1))) { + if (ghptr) { memcpy(hptr, ghptr, len); unlock_user(ghptr, gaddr, 0); - } else + } else { ret =3D -TARGET_EFAULT; - + } return ret; } =20 - -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 =3D 0; - void *ghptr; + int ret =3D 0; + void *ghptr =3D lock_user(VERIFY_WRITE, gaddr, len, 0); =20 - if ((ghptr =3D lock_user(VERIFY_WRITE, gaddr, len, 0))) { + if (ghptr) { memcpy(ghptr, hptr, len); unlock_user(ghptr, gaddr, len); - } else + } else { ret =3D -TARGET_EFAULT; + } =20 return ret; } =20 /* 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; =20 guest_addr =3D guest_addr1; for(;;) { @@ -101,11 +101,12 @@ abi_long target_strlen(abi_ulong guest_addr1) unlock_user(ptr, guest_addr, 0); guest_addr +=3D len; /* we don't allow wrapping or integer overflow */ - if (guest_addr =3D=3D 0 ||=20 - (guest_addr - guest_addr1) > 0x7fffffff) + if (guest_addr =3D=3D 0 || (guest_addr - guest_addr1) > 0x7fffffff= ) { return -TARGET_EFAULT; - if (len !=3D max_len) + } + if (len !=3D max_len) { break; + } } return guest_addr - guest_addr1; } --=20 2.20.1