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 64E7DC4332F for ; Wed, 12 Oct 2022 16:28:35 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5D1AF84DB3; Wed, 12 Oct 2022 18:28:33 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com 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=gmail.com header.i=@gmail.com header.b="H1b4iydb"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 9F72584E4C; Wed, 12 Oct 2022 18:28:31 +0200 (CEST) Received: from mail-qv1-xf30.google.com (mail-qv1-xf30.google.com [IPv6:2607:f8b0:4864:20::f30]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 8DF3684C7B for ; Wed, 12 Oct 2022 18:28:28 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=seanga2@gmail.com Received: by mail-qv1-xf30.google.com with SMTP id mx8so11206822qvb.8 for ; Wed, 12 Oct 2022 09:28:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:subject:from:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=ivJcjpgK5BYxJ92IhF3P9uGJEaQ1Xe7UPH0uAsfi5+o=; b=H1b4iydbUqp2Pfa1eIr9/H/+fAhIQLAWYoG/RiCD+EVr9Q3EBvrm/tqkSQv+xn7k76 7dEcAGD8ziISY8QqIMYOxV7v5DKVNE546e45OfEaPmMCAQQjwC1+KDBtHsBW9MtCc2Lk iWdJ9e2wTZaLR+O3Q58laggRnaU94DtoiCf3ZovxpoPN2U5l9ETy/lKZ73d4kmSRweJ6 sgof6g+vumxsuL7PgNX76ZyuS+1BKZbj3/AhcM/5kcKt6KaueN0Hjj4jI+gZ6/2sfSCg DZ6NxST67KhSwD21x2CO9lBuUZgzpa6j1QXt/JItwrHH8rlf8+L1ui+VYl8V5Ot6z75N IiaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:subject:from:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ivJcjpgK5BYxJ92IhF3P9uGJEaQ1Xe7UPH0uAsfi5+o=; b=VdBuH8aAxB1Koc4X1af/+mTOoYO7YdWf+SFQPkD/EHxfMWEq930CguYlNGvHk5UHOh QWMOg5ziIjoAHF4aqfYWz1UFluOfj10T2Ef9zFYBABcw4Z5ZbvfeOKn4hai2Z2+dk9gX Hj5tUXLKyf+zQYYeuavRgbWQqQRA4Ueak2ekyFw+v0mzvfOaSb/Gytt9143jPjPtOjJo pLfQjtrY+pnjp1lsMuRAtV54WFAb0wFK5pIXzQ3NFaBXuFONjUVTwrKu1+Waxlxs1+jT gX4eTtLQbfzYay6QMTe0U89vEgMxnUSlQwhoIQYM8PB3w0zJNPkwG5h49m+NwQHnTNh3 YS8Q== X-Gm-Message-State: ACrzQf1M1EeMbNNcvaOcNxlBwUmwf8rJlgCC5eKORa/aF0cvHu58MsXl belIDvvEVNboqeivPSt7cyo= X-Google-Smtp-Source: AMsMyM5/gMb+xYn0Gx5c5x8FfT6jPC778y6Xe8NHWBDX7jj9TvZmzFZciiNvCnCkSOZfxjDROMqQtw== X-Received: by 2002:a0c:f412:0:b0:4b1:25f6:6c26 with SMTP id h18-20020a0cf412000000b004b125f66c26mr23578967qvl.69.1665592107345; Wed, 12 Oct 2022 09:28:27 -0700 (PDT) Received: from [192.168.1.201] (pool-173-73-95-180.washdc.fios.verizon.net. [173.73.95.180]) by smtp.gmail.com with ESMTPSA id t4-20020a05620a450400b006eeacec79a3sm953546qkp.104.2022.10.12.09.28.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 12 Oct 2022 09:28:26 -0700 (PDT) Message-ID: <042dcb34-f85f-351e-1b0e-513f89005fdd@gmail.com> Date: Wed, 12 Oct 2022 12:28:26 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 From: Sean Anderson Subject: Re: [PATCH] image: fit: Fix not verifying data configuration To: Simon Glass , Sean Anderson Cc: u-boot@lists.denx.de, Peng Fan References: <20221011232525.186166-1-sean.anderson@seco.com> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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 On 10/12/22 08:59, Simon Glass wrote: > Hi Sean, > > On Tue, 11 Oct 2022 at 17:25, Sean Anderson wrote: >> >> When reading data from a FIT image, we must verify the configuration we >> get it from. This is because when we have a key with required = "conf", >> the image does not need any particular signature or hash. The >> configuration is the only required verification, so we must verify it. >> >> Users of fit_get_data_node are liable to load unsigned data unless the >> user has set required = "image". Even then, they are vulnerable to >> mix-and-match attacks. This also affects other callers of >> fit_image_verify which don't first call fit_config_verify, such as >> source and imxtract. I don't think there is a backwards-compatible way >> to fix these interfaces. Fundamentally, selecting data by image when >> images are not required to be verified is unsafe. >> >> Fixes: 37feaf2f727 ("image: fit: Add some helpers for getting data") >> Signed-off-by: Sean Anderson >> --- >> >> boot/image-fit.c | 9 ++++++++- >> 1 file changed, 8 insertions(+), 1 deletion(-) >> >> diff --git a/boot/image-fit.c b/boot/image-fit.c >> index 9c04ff78a15..632fd405e29 100644 >> --- a/boot/image-fit.c >> +++ b/boot/image-fit.c >> @@ -1948,7 +1948,14 @@ int fit_get_data_node(const void *fit, const char *image_uname, >> int fit_get_data_conf_prop(const void *fit, const char *prop_name, >> const void **data, size_t *size) >> { >> - int noffset = fit_conf_get_node(fit, NULL); >> + int ret, noffset = fit_conf_get_node(fit, NULL); >> + >> + if (noffset < 0) >> + return noffset; >> + >> + ret = fit_config_verify(fit, noffset); >> + if (ret) >> + return ret; >> >> noffset = fit_conf_get_prop_node(fit, noffset, prop_name); >> return fit_get_data_tail(fit, noffset, data, size); >> -- >> 2.35.1.1320.gc452695387.dirty >> > > This is supposed to work by first verifying the configuration with > fit_config_verify(). After that, images in that configuration can be > freely loaded, verified by the hash that each image has. Well, this function was made to replaces several cases where code loaded a FIT image from somewhere, and then wanted to get data from an image based on the configuration. Typically they only want to extract one image, which is the common case for e.g. loading firmware. This idea of this function is to make the common case of "find me the image data from the default config and verify it" easier. If you look at the existing code which uses this function, they do not verify the configuration first. This is mainly because the original versions of this code which I replaced with this function did not verify the configuration either. So while the above process works for an integrated verification process, like what is done by bootm, it doesn't really work for one-off loading of image data. In particular, the requirements to make this secure (using required = "image" for your key), are not default. When I was trying to determine whether the source command would be OK to use to load some configuration, I looked at it and saw that it did fit_image_verify. I thought that was fine, but if you use required = "config", then all that does is verify the hash. Same thing for imxtract. Almost every instance of FIT loading outside of bootm has this issue, which you can easily see when grepping for fit_config_verify. The only other users are the SPL boot process, and fdt checksign. The latter isn't even that useful, since you then need to re-parse the fit in hush to determine the default configuration and determine the image names to use. Unfortunately, it's not trivial to determine whether any existing systems are vulnerable to this issue. If they set required = "image", then they can use source and imxtract (and any of the firmware loading methods) however they want. But if they don't (and there is no option to mkimage to do this, you have to use fdtset or something), then there is a problem. > So we need to make sure that first step is taken in all code paths, > rather than adding it willy nilly. > > If people don't add hashes (or perhaps signature if they want to use > more CPU time) for the images, then there is no protection. We could > add warnings or errors to mkimage for this case? Yes, there probably should be a warning if you skip cryptographic hashes on images with signed configs. But this is not really the root of the issue. --Sean