From patchwork Tue Jul 19 12:11:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 591654 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5817:0:0:0:0 with SMTP id j23csp3898718max; Tue, 19 Jul 2022 05:12:26 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sd4UsRXKoNGtcnjmvmm0hEVNrsyNnZIj/NeTT5c9UsxgnMoo6S0A2IF/tup6X7LaAjjCN8 X-Received: by 2002:a0c:8ec5:0:b0:473:f142:118b with SMTP id y5-20020a0c8ec5000000b00473f142118bmr2267956qvb.97.1658232745982; Tue, 19 Jul 2022 05:12:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658232745; cv=none; d=google.com; s=arc-20160816; b=FU2QVWX2EZzUQHOpxLekL5tZH/Wp/l8NlYWUlc+H4EioMKafMabR1ehnZQbZo2eCh4 6ndyz9bCiwo6KCbxETDKXM/YsannGTO7WCtUHSxzSIVz8t199D4ZkG8MqDU0zFfMS1Jr nsNI4dW3YHw6UQZcIVKiwJVRb5zT04HXGx3ISF2YP0BJqExrZaKpUu9ZrFnln34NL1IV lB+jcgIwSTV/qVwYLXTJu0OEJ9b4EURorhWwM1vbGddzx+I7VWvYnOqx3v4LVuJabY9C cNEzI3Y3Bbs+2hJZP2wB+4ocADnMDX5XswCzXavywjnRoEzpRJRe+GkMPQ24YO/nRSgN O/wQ== 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=0ETA/mDbYhq/UKc8mSgAXpRi3qw9ZajpFxGpuDQmECA=; b=XW4HjCCyTyOJE8NekDtDl30I4ABgvUbL4gZAGlDl8oVYM2qIFu+ivA5KDQiweqKKrW rbAY09TsSGTKb1R9OT+xr6Jrr3rjQB3WX9mJjjjXI210AloJBGMEn2AnjSAGksvKRLri 3U9a/okNAuUd1YlUunTxe677N2OOepFeE5pPQoShVH61hP9gSFAbbVZbe7X5qDTypfIa J4sbqKd3NVFZgvPjvADVneu78b9m+RM7SEHkxI3Wu5JG0f5rl6meCQuitjie+vM+6C6Q KSSZtpw0rzpZ8UCtusk5Lb2Gp18W1VinMqGHUvq30Y0+PpZbhgpFE7rh8TdaWX4BBtKA NM0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=pjRScg81; 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 do11-20020a05620a2b0b00b006b5e8ab439bsi2564622qkb.726.2022.07.19.05.12.25 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 19 Jul 2022 05:12:25 -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=pjRScg81; 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]:48678 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oDm5R-0004yJ-Iw for patch@linaro.org; Tue, 19 Jul 2022 08:12:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51882) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oDm4N-0004vw-P9 for qemu-devel@nongnu.org; Tue, 19 Jul 2022 08:11:19 -0400 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]:44782) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oDm4M-0001lg-0N for qemu-devel@nongnu.org; Tue, 19 Jul 2022 08:11:19 -0400 Received: by mail-wr1-x429.google.com with SMTP id bk26so21274754wrb.11 for ; Tue, 19 Jul 2022 05:11:17 -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=0ETA/mDbYhq/UKc8mSgAXpRi3qw9ZajpFxGpuDQmECA=; b=pjRScg81sOpxeHDV2gj/WY7amhwmdAz5lOXZIUSO4maz9uNprEfztim7cIpDBO+kBx ndbF3vCip/DqGky9Ls/Vobba8pdIOr9BHpYTun66eDSVmAiVMUmGN+GLSUF1tjntdNfW NcbOYWCbdiZ/Ye9SKHzI4ZuKxbCn3mRBqI3IyqD+JGf2ECA1TpWRdssGh9hcsiwIKPX/ R3aAVmeejcuI128+ioJadeWfu3S2i77Dt3wkoA+jy9Dd0eK0Dy+j9CKAW38Jdj795w82 2NER3XIbAFA+2pvHpDH8pLQop7GgnaBObIF4v54HtC24qZLsBuyYG8IrIb61kN0HWti6 BqxA== 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=0ETA/mDbYhq/UKc8mSgAXpRi3qw9ZajpFxGpuDQmECA=; b=Vl3McLHG7sI6C2wg0z24csKPyKocx1u+dPrM6Pbp2glTq1+V+Nrqhv+xzu3jGIMMSo TSSh7gUs/XGvZpbmNwIgaoI2ckU0GAjOOzCpmNq752+vG98TSxcKjx2EW1Rlh5NRQGyN EtgeVdB5ZulWgMp9oSoIHuBUai6e5OqiB001aHW8vLr0UIoGJX1quqPoxhaQNJ7u+4Kr RlLA94xSPeNJtsFZ+2HugjfHhRsZG6PV3G5FuX+0QYZjwj9dqzICb56fyQh8Bzv8Ltq7 JjBpayw6t5SyV0mk4mHeFbMdjnURDVBny3jQWl5wSNWEH0r/UCdfMU2Q0FH1m25mPlbs 6Igw== X-Gm-Message-State: AJIora+LvVEWwzTveip/khqeaJxSMl2pCqmoLpJeFa3iUWaoB/rc4Wnl M62fgq17aOpJoUYqibDGaxBcXUVHVuFZzg== X-Received: by 2002:adf:cf0a:0:b0:21e:2970:bade with SMTP id o10-20020adfcf0a000000b0021e2970bademr4583652wrj.455.1658232676729; Tue, 19 Jul 2022 05:11:16 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id m21-20020a05600c4f5500b0039c5ab7167dsm22111287wmq.48.2022.07.19.05.11.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Jul 2022 05:11:16 -0700 (PDT) From: Peter Maydell To: qemu-devel@nongnu.org Cc: =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PATCH 4/4] semihosting: Fix handling of buffer in TARGET_SYS_TMPNAM Date: Tue, 19 Jul 2022 13:11:10 +0100 Message-Id: <20220719121110.225657-5-peter.maydell@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220719121110.225657-1-peter.maydell@linaro.org> References: <20220719121110.225657-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x429.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" 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 --- semihosting/arm-compat-semi.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/semihosting/arm-compat-semi.c b/semihosting/arm-compat-semi.c index d12288fc806..e741674238f 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);