From patchwork Thu Aug 15 21:32:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 819436 Delivered-To: patch@linaro.org Received: by 2002:adf:a3c8:0:b0:367:895a:4699 with SMTP id m8csp16716wrb; Thu, 15 Aug 2024 14:33:35 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXBBEMXGlU6+/epVPeZgiTE/Gt1B5mH7/jhGj4NRjKZ9kAPl5tIO05XC6CTt1D8jkLLIHkAMtNhMIQUcDhkNB0H X-Google-Smtp-Source: AGHT+IEhAiGEPo8XJlY/Cwub5g4LZQ3AeTDel6IDJsFNdRFxDwssSHK+MVqK0bOu6/dFr2cd9dJl X-Received: by 2002:a05:6214:468c:b0:6b7:aaf5:7b82 with SMTP id 6a1803df08f44-6bf7cdc6286mr13875036d6.24.1723757615225; Thu, 15 Aug 2024 14:33:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1723757615; cv=none; d=google.com; s=arc-20160816; b=lZQj0XM00ouZKhJtg+I9OkkFjknlo+oIVOdzMJkfF5SxZqg329DnyMbo+kdr44u+b4 Wcz+SKj/q45zkM4WCO2vqWxu+CogIjtp2l3nH1+6liP8TrKWRgF0BipnVtwjvDsNwsTL c1gD211qRgvLVxLDfEmup6NUlL4IaK/q/wOIOD/VAT206lcMvOqVO6mPOwvVoE/wCBvt zAiaB7MaPw7CFeu2PsdyfZpEEuR4PweZqZ55cMT2vlkAFD4c/hWryydo7aWV3Qvdl+5I 7I1/EcJKFO+XE8yG/+DwsDhyT2naH+H3dL3fDk4INIIx2INtTozduT6dxg/EB0s1UzNo MdAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=LhIEls3+aao5NH/WyIPh/wrRQxUecE2kgRFP7AbP+eo=; fh=NNRhuLempws0vmWHqJc6ruuy+Pmto8dCanVHisTq9iA=; b=li2GMIMg1DZa2sn8Qhnx1V2yfrfiWvY6tt51eJBpzRsp6NiSVKx2uvcp1ZBxRdx2Oo du6CXey05nLECRpmasqZONiEs4b78d+BnfXEkv1OGcXLEaBgG247RAwD1FAHTPl8Tci0 mTbBzqFebzEN3PGDJOR3N+SGVO73Fgp+UwH9rbk78y3qpzf2zbIxvOyLbBbApyF4K+R4 wOwRyiYDq4NObcsHEFmkqCnpqlIgeXUSIzurTdjjDpIAc2BGmbsx47/ohcNv1k2zKlSn FIdIB3/G1QIDaDpIHBvt6d7RmmvhVO//LmbPYFbVQcpommUXBjP1stF0Y3JpX1SwrzZP vSyw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MMF1P3mf; 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; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id 6a1803df08f44-6bf6fec4004si26548606d6.253.2024.08.15.14.33.35 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 15 Aug 2024 14:33:35 -0700 (PDT) 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=MMF1P3mf; 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; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sei5M-00079Y-2B; Thu, 15 Aug 2024 17:32:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sei5K-00077e-C9 for qemu-devel@nongnu.org; Thu, 15 Aug 2024 17:32:42 -0400 Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sei5I-0002N9-Nb for qemu-devel@nongnu.org; Thu, 15 Aug 2024 17:32:42 -0400 Received: by mail-pg1-x52c.google.com with SMTP id 41be03b00d2f7-7c3d9a5e050so997247a12.2 for ; Thu, 15 Aug 2024 14:32:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1723757559; x=1724362359; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=LhIEls3+aao5NH/WyIPh/wrRQxUecE2kgRFP7AbP+eo=; b=MMF1P3mfslwF8BZcTqD6vtiABBhr/RXWMJ1iiF6/uYXFhYOItUVNbiYtPjGTI0611U EaVK2TSK35x0UAUUuaptoDD6tsr/WfhlVb4IH8hy7pCXS9LyGvqNtASK+4vqQGrEl/aK ENwb742kivX9KXyjsS+lbc38U8mLcAbayvCV2iAxYfl6N+V13nk98JkB9aEiQH6C7tbz 2GI/9shzwO49QLtntGzwzu39qimy9H+Jo5JiI8ovAg2m6eyqZl44ia3AvlsIGywUpTQk ODavMtxmb5naR8rfs0kN7BPr7LSchfbkNM+37zCyJRs2YBkJUbLGSoE1oLT72tuJEDYh tESQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723757559; x=1724362359; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=LhIEls3+aao5NH/WyIPh/wrRQxUecE2kgRFP7AbP+eo=; b=ABiLH1E30amaBzxHitEb/FqI9o980ucnONBEMR/D8RTfX4ieHzVwAwgt/Zl4Gk481/ VIC2ohtXHRhlKhK0beG63MM0zFEIO2eNrdJohrrDMkjH7WPSU6TId29nIU1BEisiINgr DuFDol3o26p7TQc8Y+KSmG9jOfxfUIPDy/mGy+FEYnEa83KDyDQW5IfHs54bbXu/OYpT 20gppovGdPj+4dMI69yaAnJ+1WjMCz+cmsuGplkBJCSoLb/jccJOFnj5X75hb3XM9iqy HUbEtKl6UlO5SYO0gstqwH+9Tv0gDK/ux+4LZSmC4mpwJYird2zgwZvs8j59zOFu6mvB XQmg== X-Gm-Message-State: AOJu0YxQcMzdeGngYCW6F16olhDGMMDUMIzN+4hApcGKLxxGHdvAv6W9 LxlXr6ieDsiqVX9CvpjtoApQkyiWcUc2rOJG2F9XEYBnaZBCChbxOAX6TkYg1SMBBjVs2nxke3e Sqng= X-Received: by 2002:a17:90a:d804:b0:2d3:cc7e:c418 with SMTP id 98e67ed59e1d1-2d3dffc6b6emr1010946a91.15.1723757558544; Thu, 15 Aug 2024 14:32:38 -0700 (PDT) Received: from stoup.. ([203.30.4.109]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d3e3d97f4dsm277648a91.54.2024.08.15.14.32.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Aug 2024 14:32:38 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: gaosong@loongson.cn, qemu-stable@nongnu.org Subject: [PATCH for-9.1] linux-user: Handle short reads in mmap_h_gt_g Date: Fri, 16 Aug 2024 07:32:31 +1000 Message-ID: <20240815213231.303424-1-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::52c; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x52c.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, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org In particular, if an image has a large bss, we can hit EOF before reading all host_len bytes of the mapping. Cc: qemu-stable@nongnu.org Fixes: eb5027ac618 ("linux-user: Split out mmap_h_gt_g") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2504 Signed-off-by: Richard Henderson Reviewed-by: Song Gao Tested-by: Song Gao Reviewed-by: Philippe Mathieu-Daudé --- linux-user/mmap.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 6418e811f6..de9ab13754 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -853,10 +853,21 @@ static abi_long mmap_h_gt_g(abi_ulong start, abi_ulong len, } if (misaligned_offset) { - /* TODO: The read could be short. */ - if (pread(fd, p, host_len, offset + real_start - start) != host_len) { - do_munmap(p, host_len); - return -1; + size_t o = 0; + while (1) { + ssize_t r = pread(fd, p + o, host_len - o, + o + offset + real_start - start); + if (likely(r == host_len - o) || r == 0) { + /* Complete or EOF */ + break; + } + if (unlikely(r == -1)) { + /* Error */ + do_munmap(p, host_len); + return -1; + } + /* Short read -- iterate */ + o += r; } if (!(host_prot & PROT_WRITE)) { mprotect(p, host_len, host_prot);