From patchwork Tue Jul 26 11:25:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Jones X-Patchwork-Id: 1660757 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=O7+puQSK; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ventanamicro.com header.i=@ventanamicro.com header.a=rsa-sha256 header.s=google header.b=MW2c4kJh; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LsZNP69bWz9sB4 for ; Tue, 26 Jul 2022 21:25:56 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=tb6LaudnIZzUOAyxhxcgDJCzXqYl3+lKVKqWWacx/cA=; b=O7+puQSK9wyyLs EblpMnwztdvkOxC8+7BKsosM74y+v1i648gD781ULoyrNyAEXtY2hl9lOQDJKRrC2AngJMGmiTqRF TFyA32DqeyOYfZtsrM+c6XTyS8ZEPZfhkhzE64y7vsmuIjn0wj2Ai7f8zjfUmj9BiLwzvsVrcfRLg bXqQC7S8hSBvzLht4GjedQAxWPaJD2GFD5EodwOinvYTGggWP31o1/0CHQduA3aI2oeavKz4I8nzD RsL599qSlG7y6592Td+QhGgRKKSQrk6Zy/bEY6bQoHIbuBFAdrRupS7VIP9L6t5ZlSKyspi7psShB noCA9HWVXZJ2AVQU5Fyw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGIgx-00Fj0Y-QT; Tue, 26 Jul 2022 11:25:35 +0000 Received: from mail-ej1-x633.google.com ([2a00:1450:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGIgv-00Fila-JN for opensbi@lists.infradead.org; Tue, 26 Jul 2022 11:25:34 +0000 Received: by mail-ej1-x633.google.com with SMTP id j22so25598365ejs.2 for ; Tue, 26 Jul 2022 04:25:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=zDPd2sUfSHeQ9qjZk2HtEUtmDh/U1W3uk/pbV9d8LHM=; b=MW2c4kJhYZB4+rADvUuvLMPbH9+CJOo/fxBdBl0h/KWmAHTQSpUey1Heveo8wI56bq 0hb/1g8abtzRVQowE1axNu6mveoEUpbMEHz5aHsJQKTTZhEyn/4fZ/B0+CnXeV/3k7rT yFhuhr5pYyLQ69TJrWKuwwjF0/OWvEShQnikWmwEx24yflAzs92wQpdRtEgAIawlctp+ 0uQbvZrzlIJCiXwffvWCkLP55A43GstfetrdAb0rHpodN3fnl3hft8afSwd0alAaFnwH mm6SjEQi5djrP+rAbfL4yriDlP6xtMS0kllGfXdFo3wnvlkYU3C3ldUqvaiABfmk5AGh qDuw== 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:mime-version :content-transfer-encoding; bh=zDPd2sUfSHeQ9qjZk2HtEUtmDh/U1W3uk/pbV9d8LHM=; b=LhGoUIDetZmWMOuc66KU1mc6moNg7QO63P8Hf/ZjrjeMlbnWiw7EtgK2hdI2EIDOBa HpjcIHEox63vZ9+bt18+WIV9AmmivgqrpSjfpz94WA4plimkQIYZWjXAk4FN7M205QgE EMP4wXfn5A5pnzq8PjtEyevzIP7aCdEFwBrUhGqkneYo8yoU45/sEDcSjYzX6gvwgJ/M hMS65nf1UyfEJ6PF0AW8K/uv5EK31mYVss3QcPXRLRH1wTIDDIPKMBuANWUGbRJBIC8q yaBT81jRudGEHM0zcZ2bWLjfs5isSf8wcAXQ1ie1cTtk4IV9cOFkJpqV8YIaN+5oJgZs Sf9A== X-Gm-Message-State: AJIora/u16wZQWwdBAFtUGjFKmz/E668/rJpXhfij+L5LSKiLQp6/x/f cLSAeaJNhxZQ7rOFwR7YFJSHbtFD/KtzLw== X-Google-Smtp-Source: AGRyM1vCvxR98lsHYEKoNnZpmFns5f/dIdB5Dj76gRcSRxYpSEm7MB3S0VlVsKYwAUpqMPUSM6Yq7Q== X-Received: by 2002:a17:907:2cef:b0:72b:5b3e:3d7a with SMTP id hz15-20020a1709072cef00b0072b5b3e3d7amr13867167ejc.293.1658834725147; Tue, 26 Jul 2022 04:25:25 -0700 (PDT) Received: from localhost (90-176-226-43.rcj.o2.cz. [90.176.226.43]) by smtp.gmail.com with ESMTPSA id fu3-20020a170907b00300b0072f47838640sm6348197ejc.71.2022.07.26.04.25.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Jul 2022 04:25:24 -0700 (PDT) From: Andrew Jones To: opensbi@lists.infradead.org Cc: Anup Patel Subject: [PATCH] lib: sbi: Fix sbi_snprintf Date: Tue, 26 Jul 2022 13:25:22 +0200 Message-Id: <20220726112522.5070-1-ajones@ventanamicro.com> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_042533_663433_8B5B4FBC X-CRM114-Status: GOOD ( 12.55 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: printc would happily write to 'out' even when 'out_len' was zero, potentially overflowing buffers. Rework printc to not do that and also ensure the null byte is written at the last position when neces [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:633 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org printc would happily write to 'out' even when 'out_len' was zero, potentially overflowing buffers. Rework printc to not do that and also ensure the null byte is written at the last position when necessary, as stated in the snprintf man page. Finally, ensure all writes to 'out' go through printc and rename a goto label which clashed with it. Fixes: 9e8ff05cb61f ("Initial commit.") Signed-off-by: Andrew Jones --- lib/sbi/sbi_console.c | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c index 34c843d3f9e3..47f361705fc7 100644 --- a/lib/sbi/sbi_console.c +++ b/lib/sbi/sbi_console.c @@ -76,20 +76,24 @@ typedef __builtin_va_list va_list; static void printc(char **out, u32 *out_len, char ch) { - if (out) { - if (*out) { - if (out_len && (0 < *out_len)) { - **out = ch; - ++(*out); - (*out_len)--; - } else { - **out = ch; - ++(*out); - } - } - } else { + if (!out) { sbi_putc(ch); + return; } + + if (!*out) + return; + + if (!out_len || *out_len > 1) + **out = ch; + else if (*out_len == 1) + **out = '\0'; + + if (out_len && *out_len > 0) + --(*out_len); + + if (!out_len || *out_len > 0) + ++(*out); } static int prints(char **out, u32 *out_len, const char *string, int width, @@ -193,7 +197,7 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) if (*format == '\0') break; if (*format == '%') - goto out; + goto literal; /* Get flags */ if (*format == '-') { ++format; @@ -332,13 +336,14 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) continue; } } else { - out: +literal: printc(out, out_len, *format); ++pc; } } + if (out) - **out = '\0'; + printc(out, out_len, '\0'); return pc; }