From patchwork Fri Jul 29 09:19:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 594361 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:b345:0:0:0:0 with SMTP id w5csp1194714maz; Fri, 29 Jul 2022 02:55:50 -0700 (PDT) X-Google-Smtp-Source: AA6agR4ZnA9k7OHdF1tQSk2npjEppMAgeevOZx9tRL1VMw9dNoVIXiJ7k5rFA55w5eZjvYIoWMO0 X-Received: by 2002:a05:6214:20a2:b0:474:4adc:256d with SMTP id 2-20020a05621420a200b004744adc256dmr2528042qvd.31.1659088550020; Fri, 29 Jul 2022 02:55:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659088550; cv=none; d=google.com; s=arc-20160816; b=VBR5jLdsYseEZHN4QWOL0AHBXXi38GAPslZTcyALs6uReOFSkf1gB0CcCWsQjq3ksa 3S4hWDvWc2jrkXng1SWB6W9TD0kdkm/zmkvcQZrKlnYL7KWf1ZgJV5mLiPq4EDYJ2ZxM 0m1ACA3t356skkVC9NDNpSM8c4nUveyJiBeE3oBe4oimfFC7xacG//4Z0TTzKVQv7EFf 9s+BPAHofQNjdszEEhAC9FvihNa1DXESZdMVAGe5Rt1XVh34lw7pLV6XrJ7htyDpNR1O a5d5emGn0DSa67BIlh4Q0phkA00Iam6ytilKk4qj8gpzQK52dcgN+BYsxEZWxU7sn1FY TESA== 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=mbn6XiDKnJmRGG2YuNoOAPV2cZfoBYZK1OHleilmO84=; b=cuLT9qh0k6fKzd9PZYx4iZBgJ7ibDSUNJi7vjjn1rUZnvpX5GPJZpgNVybRE0/OU10 KBnHWycZG8NNzHCsDPqvPIxZqxdAt0EZVzg8bcR1E8raRIMJzA/94CTEZ8JS2Nrp/wB3 lk7D5dYfJ5+aUoIWKyeZsMaUIxCBbQMK9H4zxd4b+5RwfDnRD2Lt6gK6o69dQWcwGmhJ asiQpk0lzU+Wqxz5nae5y8QIXINkEa3vOdV/mLHdVCL8oSVAvlWtNnR21dUwk5FGCRIC metOjs0riR2ndtJsMeBEr6H3NS1cJ0FKN8O7189eV5529nWnBX1jvPPyQDXeiiDZTYNU yrIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=c9aoVxBv; 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 e26-20020ac8011a000000b0031ee3d828f7si1821226qtg.456.2022.07.29.02.55.49 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 29 Jul 2022 02:55:50 -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=c9aoVxBv; 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]:36126 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oHMij-0001QS-LX for patch@linaro.org; Fri, 29 Jul 2022 05:55:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45292) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oHMFE-0005Wx-Lp for qemu-devel@nongnu.org; Fri, 29 Jul 2022 05:25:20 -0400 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]:37508) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oHMFC-0006hk-V3 for qemu-devel@nongnu.org; Fri, 29 Jul 2022 05:25:20 -0400 Received: by mail-wr1-x42f.google.com with SMTP id z17so559871wrq.4 for ; Fri, 29 Jul 2022 02:25:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mbn6XiDKnJmRGG2YuNoOAPV2cZfoBYZK1OHleilmO84=; b=c9aoVxBvcweaE2Jfnzkf3ITZcceZf6BpIwZ4izg+CMiwn3MTAFb5KqpAMOedz3mooR bfaNS9am+X6GlHYsTr897A1SC52VTyxaAPCBBX2kL91nGcWY2Ilcm7pNetlCQOfFwIGr 1qW3lZ3hnkDMPP/S0wteABOGKPATRI5g4D3UdTzCrihj5qEd5qenuh0sxrJJuw1ptLu+ M11OxPUufSZMmH5lJ2cZNZh2uGJ9YEh5tBclEeHX6sYT1fHHGHzLbHswOTO93ScJfjGO XGMexpVuiJOtLbsx1CezNDUBdJ8m6/9rgx0EJqhdVPjqO0HOHtoVpnQr8VKC9o1m05EA pevA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mbn6XiDKnJmRGG2YuNoOAPV2cZfoBYZK1OHleilmO84=; b=ICNE6kc2sV82Fky3pPdtGHlnfMily11X8hYLpi27FKTRtMhU1q9ABPzYbbXWFOedKr zZDtGfHIvAGW+sKgYimZN9bCzyyk6AkbUO7sbScSFXDAglw9DsQw/JkEVyCD+/BvqkJJ XU90imkQZJe+5bllS8tK78321LwLili8iZPe3F7gPekB5LyvGqeTht73bSZMHqzoFA73 V0+aYvGa/QZIeOZHlPd1TjQCV1GOw6LFdqQhARt5k6pIBsmqA/YCSpc4bQNfCjVQl6bd Vq4kHgtVGGEsxwGOVap2oC5H+Ior1VZSe2DhTSPGVeo+gXl8LP3Dy4aj8nKW1xHiaWBA Q3Jg== X-Gm-Message-State: ACgBeo3uFF5DHxN5OHbziSu33vjP9i0buoHbXnBf2El9cdOU9iBWF2wi A2FE55ZrNditjuFtaVnYYIgheA== X-Received: by 2002:a05:6000:807:b0:21e:daca:7df2 with SMTP id bt7-20020a056000080700b0021edaca7df2mr1846828wrb.214.1659086717603; Fri, 29 Jul 2022 02:25:17 -0700 (PDT) Received: from zen.linaroharston ([185.81.254.11]) by smtp.gmail.com with ESMTPSA id q5-20020a1ce905000000b003a320e6f011sm3835992wmc.1.2022.07.29.02.25.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Jul 2022 02:25:14 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 965041FFC1; Fri, 29 Jul 2022 10:19:44 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: peter.maydell@linaro.org, richard.henderson@linaro.org Cc: qemu-devel@nongnu.org, =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PULL 09/13] semihosting: Fix handling of buffer in TARGET_SYS_TMPNAM Date: Fri, 29 Jul 2022 10:19:39 +0100 Message-Id: <20220729091943.2152410-10-alex.bennee@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220729091943.2152410-1-alex.bennee@linaro.org> References: <20220729091943.2152410-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42f; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x42f.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" From: Peter Maydell The TARGET_SYS_TMPNAM implementation has two bugs spotted by Coverity: * confusion about whether 'len' has the length of the string including or excluding the terminating NUL means we lock_user() len bytes of memory but memcpy() len + 1 bytes * In the error-exit cases we forget to free() the buffer that asprintf() returned to us Resolves: Coverity CID 1490285, 1490289 Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Message-Id: <20220719121110.225657-5-peter.maydell@linaro.org> Signed-off-by: Alex Bennée Message-Id: <20220725140520.515340-10-alex.bennee@linaro.org> diff --git a/semihosting/arm-compat-semi.c b/semihosting/arm-compat-semi.c index d12288fc80..e741674238 100644 --- a/semihosting/arm-compat-semi.c +++ b/semihosting/arm-compat-semi.c @@ -504,16 +504,25 @@ void do_common_semihosting(CPUState *cs) GET_ARG(1); GET_ARG(2); len = asprintf(&s, "/tmp/qemu-%x%02x", getpid(), (int)arg1 & 0xff); + if (len < 0) { + common_semi_set_ret(cs, -1); + break; + } + + /* Allow for trailing NUL */ + len++; /* Make sure there's enough space in the buffer */ - if (len < 0 || len >= arg2) { + if (len > arg2) { + free(s); common_semi_set_ret(cs, -1); break; } p = lock_user(VERIFY_WRITE, arg0, len, 0); if (!p) { + free(s); goto do_fault; } - memcpy(p, s, len + 1); + memcpy(p, s, len); unlock_user(p, arg0, len); free(s); common_semi_set_ret(cs, 0);