From nobody Sun Nov 24 09:00:27 2024 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=1723757615; cv=none; d=zohomail.com; s=zohoarc; b=LESGh9K5DAJKesv4TUUSl3UNdySpmVo9zRpdtQNS1zLnCTzHU5wvFbwtjR5xrhdbnhHM43fXG/ye0PxQAbjhq/NSD4xYTj6B1bvkG/f866LnmRD80lgKbuQIoSWvtPd/4aidSINWYV9Guc6TMbVYUQS/kzGBLE44wE9Hz724Hbw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1723757615; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=LhIEls3+aao5NH/WyIPh/wrRQxUecE2kgRFP7AbP+eo=; b=Rnc/xinWaIy2zlgeGRAZ5+Bv6LlthcvjhaL5xZPutoi/faKpeo37t0eOhHQ57nokB3boSpjoEcFU7iGAs0X01wGPwbjV27IS+rVZSwUz6tGfBw0lAgxhE/1LitDgOHX1jULlWI5umJ3UAMdS8pUq9VBIx1m+oO+BA1Ixj28B2VA= 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) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1723757615593275.3495961764652; Thu, 15 Aug 2024 14:33:35 -0700 (PDT) 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) 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) 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-Google-Smtp-Source: AGHT+IF4mI08MsW8UTtDzL20PadbTFhUj++SXMrg10T9n+Ru4x/pULLkzc1JlhxMr2LYm3AD0eVP1g== 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) 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 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=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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1723757617836116600 Content-Type: text/plain; charset="utf-8" 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: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Song Gao Tested-by: Song Gao --- 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_ulon= g len, } =20 if (misaligned_offset) { - /* TODO: The read could be short. */ - if (pread(fd, p, host_len, offset + real_start - start) !=3D host_= len) { - do_munmap(p, host_len); - return -1; + size_t o =3D 0; + while (1) { + ssize_t r =3D pread(fd, p + o, host_len - o, + o + offset + real_start - start); + if (likely(r =3D=3D host_len - o) || r =3D=3D 0) { + /* Complete or EOF */ + break; + } + if (unlikely(r =3D=3D -1)) { + /* Error */ + do_munmap(p, host_len); + return -1; + } + /* Short read -- iterate */ + o +=3D r; } if (!(host_prot & PROT_WRITE)) { mprotect(p, host_len, host_prot); --=20 2.43.0