From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EAF69C61DA3 for ; Tue, 21 Feb 2023 20:43:36 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id E076E85AB9; Tue, 21 Feb 2023 21:43:33 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="Q3FDQn0g"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3A31385A81; Tue, 21 Feb 2023 21:33:31 +0100 (CET) Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 8852585AC4 for ; Tue, 21 Feb 2023 21:22:42 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=pali@kernel.org Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 08AA3B81020; Tue, 21 Feb 2023 20:22:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6224FC4339B; Tue, 21 Feb 2023 20:22:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677010951; bh=hDZPhjbEC0S0tMXRKo/9XUOeOKmf9itELHI5Uu7YsCM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q3FDQn0gjFrx4fzba8+BU6fc5026QR/Tlda3N5lLkNbh4nwcsTcQZTJvdTdePbzcL cV1w0+IIzxcYOLk46vAskOD/c65sdPns3hiaIZD7+4JL6oSprqPA7R4IQRuovUmdab ccMJWQQzQuc7+CtbiRwKEW5rDsKEf6XHLM+HIl3KOziADZc4UuM0lgKbmUPgHR2SWz 9sIojIVVAwToZxYd6YEnOplca7/mjBuDtpGgOSkxydQrqu1dv1cgJgoSBVE6Ogy4t/ r4GIq5PkfVcmXYOY8YFn7bJc9wM6IGEUPT4tVsqM4Eu46OQVZGgyt2VPxzouZ0B7F3 WuPmPEhATnOqg== Received: by pali.im (Postfix) id E5449285B; Tue, 21 Feb 2023 21:22:29 +0100 (CET) From: =?UTF-8?q?Pali=20Roh=C3=A1r?= To: u-boot@lists.denx.de Cc: Stefan Roese , Tony Dinh , Josua Mayer Subject: [PATCH RFC u-boot-mvebu 16/59] tools: kwboot: Validate optional kwbimage v1 headers Date: Tue, 21 Feb 2023 21:18:42 +0100 Message-Id: <20230221201925.9644-17-pali@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230221201925.9644-1-pali@kernel.org> References: <20230221201925.9644-1-pali@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Before starting parsing of kwbimage, first validate that all optional v1 headers and correct. This prevents kwboot crashes on invalid input. Signed-off-by: Pali Rohár --- tools/kwboot.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/kwboot.c b/tools/kwboot.c index da840864b565..c8c7a8d24658 100644 --- a/tools/kwboot.c +++ b/tools/kwboot.c @@ -1939,6 +1939,7 @@ static int kwboot_img_patch(void *img, size_t *size, int baudrate) { struct main_hdr_v1 *hdr; + struct opt_hdr_v1 *ohdr; uint32_t srcaddr; uint8_t csum; size_t hdrsz; @@ -1990,6 +1991,13 @@ kwboot_img_patch(void *img, size_t *size, int baudrate) *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize)) goto err; + for_each_opt_hdr_v1 (ohdr, hdr) { + if (!opt_hdr_v1_valid_size(ohdr, (const uint8_t *)hdr + hdrsz)) { + fprintf(stderr, "Invalid optional image header\n"); + goto err; + } + } + /* * The 32-bit data checksum is optional for UART image. If it is not * present (checksum detected as invalid) then grow data part of the -- 2.20.1