From patchwork Mon Mar 27 21:18:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 667409 Delivered-To: patch@linaro.org Received: by 2002:a5d:4d08:0:0:0:0:0 with SMTP id z8csp1314635wrt; Mon, 27 Mar 2023 14:19:53 -0700 (PDT) X-Google-Smtp-Source: AKy350btjtcbMF+uvKSVUJ3d6iBvyfFf1I3OggY3aZh05mV/b8z1KNrM2COEdWQJgfTTYpFmbCIL X-Received: by 2002:a05:6214:e67:b0:56e:b91f:aec4 with SMTP id jz7-20020a0562140e6700b0056eb91faec4mr18894655qvb.11.1679951993096; Mon, 27 Mar 2023 14:19:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679951993; cv=none; d=google.com; s=arc-20160816; b=X8EnKoNSGwHM9G4w09FcoJg41iK2BQkNXpt5g7i/anwjCTWg+RGicA4TSvFYAqMT2h pxXSBnVSdEdUZ9mWOimvoJXWcmybC9yDFLo8khlya3m6zuco4a+Ejc0UcITH2qcpQX5M WJiEMPE+xxFazH0mKA4Ag9h4peiOP1isHUQRGLcRgwr6MglCIL4qaMsM0Ie6C091n+6E /weuUlwwCMmymM10IZ9bVnqkT7BluxaRUfO3OgiT+slSVv6iPMLF19rp6hnNa46SHHW/ PBSMr3vbPOEyt3WGuzeujE1hK39ZtaxNDSHIPQghlaYN5H63b3yl2lxpFF6aW8vGFrwP zPSA== 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:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=c1VLo2WxkBtbuWkw96A50eULRoFliCIvVpHxdRWcTHM=; b=XertjUYm3lH+tKHP9rxUzctk+rk42/LazwUs1OXuuwzVNoWdgqus0AunBeWnq4W8ZL Gckc1TgZouX6KEWDvX1CgAQ52qO89QTuqww1MuukceHxhcHRDAGdVUFkGn0rFid5wBYD lT/j34YWZ4oMFLS+3tlGGvoIhSSOn+9o8poWF/8SRD3bDjXvLuvunpY3TWRqoQJq81gT wYWECkMpQ1kbP14NqnrjHFIxIQX/Rp39Mqgrz2IfXG1WxLRaiu/yX7p8GMIZZxFt/dqw hh0Oqe8wYGOReKkiSt0h6cPqHGONqNn4Tj73/w6L5mFvbbUp6QNSL8w7/LsT0+mdJqfL f70w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=OjIqF5Sa; 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 w3-20020ac857c3000000b003bbd8bdb935si8086353qta.793.2023.03.27.14.19.52 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 27 Mar 2023 14:19:53 -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=OjIqF5Sa; 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] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pguEi-00046S-8I; Mon, 27 Mar 2023 17:18:40 -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 1pguEg-00045v-Fe for qemu-devel@nongnu.org; Mon, 27 Mar 2023 17:18:38 -0400 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pguEe-0005xd-HA for qemu-devel@nongnu.org; Mon, 27 Mar 2023 17:18:38 -0400 Received: by mail-pj1-x1035.google.com with SMTP id fy10-20020a17090b020a00b0023b4bcf0727so10433834pjb.0 for ; Mon, 27 Mar 2023 14:18:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1679951915; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=c1VLo2WxkBtbuWkw96A50eULRoFliCIvVpHxdRWcTHM=; b=OjIqF5SaAC+hrGl83V/7f9kYjAyUBx1aX9XkS9HcVLDClZQqHsQmtQoOcv8b1/a1id 28xGbaKas51S4eqsjv3cDh5rZA31PT0X8K1mxTabCDaXZfbfFaRYA1TmoHbYSQsy6Lx1 st+mn9jvufw/G9Tx2PHUS/a+6133PIL/WvmiJd+SFh7u1P3N/R/HUiUhiKueEZH6mWGu 804i8iypkhBWzh9LtKHvvOxWod+qRqc3sMmdJLcaIKEn5ZGd3+9qLD5RmHGK2WlIC3mS 5zHE0KRFJ1/iEiZH99W+WDVn+DRmzgb8y1gqwjLFG9hnWrAHv/Mj3WNaNpphS12egQ5x b/kQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679951915; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=c1VLo2WxkBtbuWkw96A50eULRoFliCIvVpHxdRWcTHM=; b=lUB+12eQr6pHtQiMavVUTYx6woii8aCzQoGtwNgqY38eOMHhdn+zA0Fsif10vYTZ63 +Qe8oBpbje0YmloFYNUgxHgSI1+7sxjsT3KM6efYT1QfUTnDmB7g82ANxcxZiutqfn1G BhmUIDioATrO57M1PHQ+QiiiSOLLfnvfCHgn3SGhg0MBZpNrB+N1OV6uj1gGA4wK5HL/ KXcnqJC1FGZJ0iyMsDcetuNr/PSfTmI2VplJK/QF6nKKuXQ1n4aJNmigCWKUXu7Sfkhg QCp/9vPykXzIoa4RTJsgV6Rmj+KggyzE2mNf34btWCEstwWdXeQe2NO1LRLiG9UWEZoH 5AXg== X-Gm-Message-State: AAQBX9fCHC4rCfK33YTiFEhfq56+GNPgsFW1rgf15P0tKNXrCKQph5NO 25McnHMVRZGV5BH3dl1TbQb6t17PWVP5DXtznA8= X-Received: by 2002:a17:90a:be0c:b0:23f:5a76:506 with SMTP id a12-20020a17090abe0c00b0023f5a760506mr12936821pjs.46.1679951915335; Mon, 27 Mar 2023 14:18:35 -0700 (PDT) Received: from stoup.. ([2602:ae:1544:6601:6705:aa17:3641:e634]) by smtp.gmail.com with ESMTPSA id d2-20020a17090a6f0200b0023440af7aafsm7902708pjk.9.2023.03.27.14.18.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Mar 2023 14:18:34 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org, philmd@linaro.org Subject: [PATCH for-8.0 v2 10/12] linux-user: Pass last not end to probe_guest_base Date: Mon, 27 Mar 2023 14:18:22 -0700 Message-Id: <20230327211824.1785547-11-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230327211824.1785547-1-richard.henderson@linaro.org> References: <20230327211824.1785547-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1035; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1035.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.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 Pass the address of the last byte of the image, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved. Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé --- linux-user/elfload.c | 24 ++++++++++++------------ linux-user/flatload.c | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index fa4cc41567..dfae967908 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2504,7 +2504,7 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr, if (guest_hiaddr > reserved_va) { error_report("%s: requires more than reserved virtual " "address space (0x%" PRIx64 " > 0x%lx)", - image_name, (uint64_t)guest_hiaddr, reserved_va); + image_name, (uint64_t)guest_hiaddr + 1, reserved_va); exit(EXIT_FAILURE); } } else { @@ -2512,7 +2512,7 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr, if ((guest_hiaddr - guest_base) > ~(uintptr_t)0) { error_report("%s: requires more virtual address space " "than the host can provide (0x%" PRIx64 ")", - image_name, (uint64_t)guest_hiaddr - guest_base); + image_name, (uint64_t)guest_hiaddr + 1 - guest_base); exit(EXIT_FAILURE); } #endif @@ -2525,18 +2525,18 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr, if (reserved_va) { guest_loaddr = (guest_base >= mmap_min_addr ? 0 : mmap_min_addr - guest_base); - guest_hiaddr = reserved_va; + guest_hiaddr = reserved_va - 1; } /* Reserve the address space for the binary, or reserved_va. */ test = g2h_untagged(guest_loaddr); - addr = mmap(test, guest_hiaddr - guest_loaddr, PROT_NONE, flags, -1, 0); + addr = mmap(test, guest_hiaddr - guest_loaddr + 1, PROT_NONE, flags, -1, 0); if (test != addr) { pgb_fail_in_use(image_name); } qemu_log_mask(CPU_LOG_PAGE, - "%s: base @ %p for " TARGET_ABI_FMT_ld " bytes\n", - __func__, addr, guest_hiaddr - guest_loaddr); + "%s: base @ %p for %" PRIu64 " bytes\n", + __func__, addr, (uint64_t)guest_hiaddr - guest_loaddr + 1); } /** @@ -2680,7 +2680,7 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr, if (hiaddr != orig_hiaddr) { error_report("%s: requires virtual address space that the " "host cannot provide (0x%" PRIx64 ")", - image_name, (uint64_t)orig_hiaddr); + image_name, (uint64_t)orig_hiaddr + 1); exit(EXIT_FAILURE); } @@ -2694,7 +2694,7 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr, * arithmetic wraps around. */ if (sizeof(uintptr_t) == 8 || loaddr >= 0x80000000u) { - hiaddr = (uintptr_t) 4 << 30; + hiaddr = UINT32_MAX; } else { offset = -(HI_COMMPAGE & -align); } @@ -2702,7 +2702,7 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr, loaddr = MIN(loaddr, LO_COMMPAGE & -align); } - addr = pgb_find_hole(loaddr, hiaddr - loaddr, align, offset); + addr = pgb_find_hole(loaddr, hiaddr - loaddr + 1, align, offset); if (addr == -1) { /* * If HI_COMMPAGE, there *might* be a non-consecutive allocation @@ -2755,7 +2755,7 @@ static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr, if (guest_hiaddr > reserved_va) { error_report("%s: requires more than reserved virtual " "address space (0x%" PRIx64 " > 0x%lx)", - image_name, (uint64_t)guest_hiaddr, reserved_va); + image_name, (uint64_t)guest_hiaddr + 1, reserved_va); exit(EXIT_FAILURE); } @@ -3021,7 +3021,7 @@ static void load_elf_image(const char *image_name, int image_fd, if (a < loaddr) { loaddr = a; } - a = eppnt->p_vaddr + eppnt->p_memsz; + a = eppnt->p_vaddr + eppnt->p_memsz - 1; if (a > hiaddr) { hiaddr = a; } @@ -3112,7 +3112,7 @@ static void load_elf_image(const char *image_name, int image_fd, * In both cases, we will overwrite pages in this range with mappings * from the executable. */ - load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE, + load_addr = target_mmap(loaddr, (size_t)hiaddr - loaddr + 1, PROT_NONE, MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | (ehdr->e_type == ET_EXEC ? MAP_FIXED : 0), -1, 0); diff --git a/linux-user/flatload.c b/linux-user/flatload.c index e99570ca18..5efec2630e 100644 --- a/linux-user/flatload.c +++ b/linux-user/flatload.c @@ -448,7 +448,7 @@ static int load_flat_file(struct linux_binprm * bprm, * Allocate the address space. */ probe_guest_base(bprm->filename, 0, - text_len + data_len + extra + indx_len); + text_len + data_len + extra + indx_len - 1); /* * there are a couple of cases here, the separate code/data