From patchwork Tue Jun 7 20:45:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 579402 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5806:0:0:0:0 with SMTP id j6csp5556745max; Tue, 7 Jun 2022 15:12:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxtGeokyDFgpY2Vv1WzqoEQhKcq3y0/xmnUa5YFT8sgGJgLb1VKh5uUeNViBPkY7CFsNm1F X-Received: by 2002:a05:622a:1a27:b0:304:f1cb:e8c4 with SMTP id f39-20020a05622a1a2700b00304f1cbe8c4mr7896463qtb.194.1654639941953; Tue, 07 Jun 2022 15:12:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654639941; cv=none; d=google.com; s=arc-20160816; b=WqOtz9ZpBZDLvrD010U1MutyBrskorUM0ApZ32uMTA9wnyAoZ/hm8PH1bNQvQ+hc27 KnVHc6lUcWerj2doW4Fah1qltMb9pvDPDRsiD0jZKusGQ2hPawHx8knkZgeD4aqEB+qX RVbV8YDUIqPBQH+Xa2czPzvX7GtOFXZaHJ/EW7s+GzX7yGAgsUasGmR5MJIzGIckk5q1 WIOTH1beEmEo9BvJlE9jEuFLzuZjybB0i22HavwM91rf4/m2/UEBnR99lTZD84vBgAp0 jC/ZJ/Di7uBlT9qxh64fwPsKLdgPK70ErYx2wl1mmbL93OrvknqOPsoJRJ97BD+qjPyo SvRA== 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:to:from :dkim-signature; bh=aBrl+ytbZVErgWrNLoalp3tVxQVgRG0j/fJKDA/VkdQ=; b=JS2AKGK/2KebYoSO5kZR/0fm+Q1FKfqViag9WYNHxilg1JLVM4+ykdOmn+34dpvfl0 OzAMIiNX5KYA1EHlvPUii11eTxwdYK6w8bqgf8vBVUaLEhBUPyEJkcgbXOy1aEAKVdOL ti+FcycCPGpjHizui4V+bTiuT18kb6EONvfVnw8cE2DacuDZ3glxw2uob6ZkAm5bFstM PRPd/4HSlMNJBiCFvU0U/PftjBRp4LxgItPggmsDlD8oUwmcUS81MEpTSbQz0NZV6Yyp eFfUK9QRQAMIywq3ALTo51Et7qfaDIAOT+NQxwWXE6bd+3qB1Pq9bSyOWgtIv0IQThx5 UAaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=cSxRTCXl; 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=fail (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 z22-20020a05620a261600b006a6c21e653bsi3549448qko.602.2022.06.07.15.12.21 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 07 Jun 2022 15:12:21 -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=fail header.i=@linaro.org header.s=google header.b=cSxRTCXl; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:46894 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nyhQy-0007n4-Hv for patch@linaro.org; Tue, 07 Jun 2022 18:12:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39172) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nyg5t-0007VD-V4 for qemu-devel@nongnu.org; Tue, 07 Jun 2022 16:46:29 -0400 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]:33404) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nyg5s-0001Mg-6y for qemu-devel@nongnu.org; Tue, 07 Jun 2022 16:46:29 -0400 Received: by mail-pg1-x533.google.com with SMTP id r71so17015993pgr.0 for ; Tue, 07 Jun 2022 13:46:27 -0700 (PDT) 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=aBrl+ytbZVErgWrNLoalp3tVxQVgRG0j/fJKDA/VkdQ=; b=cSxRTCXl+SWRUws1CpeT2W1ErcHDYkmVZMc9QB8W30nmAM1cKxs1Dp1FfOnJag9Rh7 JgkLlVJW7Y46OBTK6Xt3stZCHkUzZPhMXAVnpyNE9eh/05zqpNgmvJyBT+iD1jPt21hD +N510fzQufntPm5cY//R9cvSwsiJ/pomQH060SA1UXDMTy5bj6tff3xnbMeU/j/C/2fq NUEMLxbiNzwxqvO5HfzNRW9VRVwY0dtKiiZeHJ/rgYDfhSOLr5yvkWwk/h+WFPI4XWXW MM9pDD/U5P8uTONV+8pb5XAviqYDVd+Gr757icWb8UTeHznAYVuW1qa4bewKUAMyq0jZ 7srg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aBrl+ytbZVErgWrNLoalp3tVxQVgRG0j/fJKDA/VkdQ=; b=dcSJgVS5TaSWPDBzEqLd7T1mR+pSfKYAqOei2IKRiERUnTHpKK8hbXL4LHKXIJOsw/ qG2ajNOjXU9/NNP4TfIQfWKF8VqHNnkxgu4pTyg6uJRTHWiUpWDP5Zicm/eq00e4GsdM OaXtT5ol/15L5ftZOO2E2899g7NxQn+w0VdOih4TJCHWIL0nQF18NFSoASTJesWGQlSB y0UylrA/GKTctzv4SPg1A5OYaVPO6VPSeb18wBCEv7/JzRhRIsiRaAmwvenq/BY6BAHR XgmCvVQ4HlnuLDj4evc9xVc8NEqqybwmN67OFXwNCjBExjO/wN6xsW/l8rs+NM0buOdj jQVQ== X-Gm-Message-State: AOAM531heAKvkt/CEmpiZ0LjwRRxpEGf0b3Z9IhuszLAcc9jZawoI+99 d09nYEzNJc1YxdJ0Vo1OpHQDSFZecA37Fw== X-Received: by 2002:a63:d013:0:b0:3fc:e50f:8e2a with SMTP id z19-20020a63d013000000b003fce50f8e2amr25171067pgf.283.1654634787409; Tue, 07 Jun 2022 13:46:27 -0700 (PDT) Received: from stoup.. ([2602:ae:1547:e101:3cf2:c634:5b19:25f6]) by smtp.gmail.com with ESMTPSA id r6-20020a170902be0600b00162037fbacdsm12971279pls.187.2022.06.07.13.46.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 13:46:26 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v4 31/53] semihosting: Bound length for semihost_sys_{read, write} Date: Tue, 7 Jun 2022 13:45:35 -0700 Message-Id: <20220607204557.658541-32-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220607204557.658541-1-richard.henderson@linaro.org> References: <20220607204557.658541-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::533; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x533.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" Fixes a minor bug in which a 64-bit guest on a 32-bit host could truncate the length. This would only ever cause a problem if there were no bits set in the low 32, so that it truncates to 0. Signed-off-by: Richard Henderson Reviewed-by: Luc Michel --- semihosting/syscalls.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/semihosting/syscalls.c b/semihosting/syscalls.c index 5cb12d6adc..eefbae74f1 100644 --- a/semihosting/syscalls.c +++ b/semihosting/syscalls.c @@ -283,6 +283,14 @@ void semihost_sys_close(CPUState *cs, gdb_syscall_complete_cb complete, int fd) void semihost_sys_read_gf(CPUState *cs, gdb_syscall_complete_cb complete, GuestFD *gf, target_ulong buf, target_ulong len) { + /* + * Bound length for 64-bit guests on 32-bit hosts, not overlowing ssize_t. + * Note the Linux kernel does this with MAX_RW_COUNT, so it's not a bad + * idea to do this unconditionally. + */ + if (len > INT32_MAX) { + len = INT32_MAX; + } switch (gf->type) { case GuestFDGDB: gdb_read(cs, complete, gf, buf, len); @@ -313,6 +321,14 @@ void semihost_sys_read(CPUState *cs, gdb_syscall_complete_cb complete, void semihost_sys_write_gf(CPUState *cs, gdb_syscall_complete_cb complete, GuestFD *gf, target_ulong buf, target_ulong len) { + /* + * Bound length for 64-bit guests on 32-bit hosts, not overlowing ssize_t. + * Note the Linux kernel does this with MAX_RW_COUNT, so it's not a bad + * idea to do this unconditionally. + */ + if (len > INT32_MAX) { + len = INT32_MAX; + } switch (gf->type) { case GuestFDGDB: gdb_write(cs, complete, gf, buf, len);