From patchwork Fri Oct 7 19:11:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tadeusz Struk X-Patchwork-Id: 613219 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4C870C433FE for ; Fri, 7 Oct 2022 19:11:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229648AbiJGTLr (ORCPT ); Fri, 7 Oct 2022 15:11:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229620AbiJGTLq (ORCPT ); Fri, 7 Oct 2022 15:11:46 -0400 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA278DEB0 for ; Fri, 7 Oct 2022 12:11:45 -0700 (PDT) Received: by mail-pf1-x42d.google.com with SMTP id i6so5722767pfb.2 for ; Fri, 07 Oct 2022 12:11:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=X33KFnbOehc32ARH7cR80ekLDQpypni50VNhotuRRFg=; b=IvqidsxBiaz+vky6OVpa9iKOLZmMUiMGeo2lVRQhLdlV9DUkgGp4RcoGELOaUz/nnK Mji3h65SYUswQ7N6fFyZIQPjXHA5IsN8Omdj5TV4q8yGPJZpc2hvPT+9W2Z9NaHTDQze EtMAFP+7rVL5yrd7GlIH8vPETfaGzHNuLzE7zXO9f5964dqx9CIObapSsccp6SuPWO03 DzwVzfPEgizmR8GGPNU9CmES5OD3BKCxAw4Y+TCNZQq1jJvRaG1G//9JS+wof5HgY2XP i6z3jFnsbmJ5G4O43L92WhwEqpQQwjx/hsOoKu7UfDaceuUyFgMVq5pRuouXhzsSX939 pLNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=X33KFnbOehc32ARH7cR80ekLDQpypni50VNhotuRRFg=; b=4Pww0tCrkQrgpqAXy4pkaoltSldnXRRVhL0nMcvw2Ps8nUixW9J2KTXg2/Wcp5qNhq 5CH2UYcN/xCEo0D/QHo+573j/DBwzSkJiGAK409UUKU9nY+oS/MxGc/k4FZ64QVOt6up y7iKxzWQGRnmeXFZt2LY6Ej8mhQFG1gG0cc9K4vhWVV/WMfJePhdwv90Y07WFLJecw4t OQxXLpiNygRyhy/CncTGAjNowAeYYnwylKP82o/jObAMVyCCFLlzctHa+2nfH71gtg/p pav71Ad1+eQ6lonPci4nP5tIl5wlci6Kugk6IduTV0zSj6gA6udKg10W1XTnqCmkuUbT j7Iw== X-Gm-Message-State: ACrzQf3sQjcD9ZLG2rqHe25JL8xBBgErKwzigpW9O0o7hGM5F+HY1Po1 jhdMMiEoBsgZnGPhYGWxTo8uaQ== X-Google-Smtp-Source: AMsMyM780EJ8vUByNvj1Fyl6j04b/TM9vitn4lnAs1BvlFQNSgrhMX6tFf8sZgiVtfGTRdSXmo8wOA== X-Received: by 2002:a62:164d:0:b0:562:bc4e:253 with SMTP id 74-20020a62164d000000b00562bc4e0253mr4416576pfw.26.1665169905284; Fri, 07 Oct 2022 12:11:45 -0700 (PDT) Received: from desktop.hsd1.or.comcast.net ([2601:1c0:4c81:c480:feaa:14ff:fe3a:b225]) by smtp.gmail.com with ESMTPSA id s1-20020a6550c1000000b0043a09d5c32bsm1971571pgp.74.2022.10.07.12.11.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Oct 2022 12:11:44 -0700 (PDT) From: Tadeusz Struk To: David Gibson , Rob Herring Cc: devicetree@vger.kernel.org, devicetree-compiler@vger.kernel.org, Tadeusz Struk Subject: [PATCH 1/2] libfdt: add fdt_get_property_by_offset_w helper Date: Fri, 7 Oct 2022 12:11:15 -0700 Message-Id: <20221007191116.161426-1-tadeusz.struk@linaro.org> X-Mailer: git-send-email 2.37.3 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Add a new fdt_get_property_by_offset_w helper function. It is a wrapper on the fdt_get_property_by_offset() cuntion that returns a writable pointer to a property at a given offset. Signed-off-by: Tadeusz Struk --- libfdt/libfdt.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libfdt/libfdt.h b/libfdt/libfdt.h index a7f432c..cddc2d6 100644 --- a/libfdt/libfdt.h +++ b/libfdt/libfdt.h @@ -660,6 +660,13 @@ int fdt_next_property_offset(const void *fdt, int offset); const struct fdt_property *fdt_get_property_by_offset(const void *fdt, int offset, int *lenp); +static inline struct fdt_property *fdt_get_property_by_offset_w(const void *fdt, + int offset, + int *lenp) +{ + return (struct fdt_property *)(uintptr_t) + fdt_get_property_by_offset(fdt, offset, lenp); +} /** * fdt_get_property_namelen - find a property based on substring From patchwork Fri Oct 7 19:11:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tadeusz Struk X-Patchwork-Id: 613566 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0CEDC433FE for ; Fri, 7 Oct 2022 19:11:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229638AbiJGTL6 (ORCPT ); Fri, 7 Oct 2022 15:11:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229620AbiJGTL5 (ORCPT ); Fri, 7 Oct 2022 15:11:57 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44B45DEB0 for ; Fri, 7 Oct 2022 12:11:56 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id lx7so5234283pjb.0 for ; Fri, 07 Oct 2022 12:11:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DdvE2IDZlB1Y3HbmvUMs04k2VVXo+MuYagZupHijn6I=; b=rww/nAJx/y+K/abCIaSBro5ui3XZ4A3Cv5MgZVCnElylsxOwEwHvnvkhZLfblD1O+D YHNYaJu8VEEQgR8VYFPR7SNxWnL50AvPgrEunHGZVkFVGxVesEn+5Yk49hKFy5D2+POI g+ipzxK92aVoPQTdRhb5yI6CVbjAnGGOBBc5JS9zAmHe0eyLsh3au95Wa2eBP+cjfSFF Sft0HLw8ND7oZY0tdUhBZVuAYU2bEKQp3BbCd91B1XI32lCZg6i7ZmFCT3qOIE2RKLTc SfRIO3SVkPk+DvufUVoM9qCemRPeDiTUzMYiKERlw3HujRsWvl1mszHSIMyD92/s+eXC GMuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DdvE2IDZlB1Y3HbmvUMs04k2VVXo+MuYagZupHijn6I=; b=VkXmx2LtaIy29z8DHYK3lkupXsY9rfrVFfowZxoLwODsKcuJmyMks3dIiPyVO1iWA4 l9h7FB4XhdnVosPyX9PTu8yJJBbOLTRl936S7RmKOmmOwx9TiJHZkeH0xAVMoRBU2wui OxRohWpFH/14ZEJjRShIHj46YBN+jZjR57dPCDNiGhqbcBLHLeITMjO2BDqG4/0AZMny 2BNXer0X7igilhJG9qA0Zr4SmqM/Ey85xHoybQjJwJtsh7Hyj2eNGr/PbUbEwFl7ciTa 1kd6wBYC9pHVP0O52e5QP08MbTYz85TmTawl/8Ya0LTMNV/al4LFf3pE1482r0YTrIhy KEDA== X-Gm-Message-State: ACrzQf2joM4I/U2s/EqAXsBGYiHem1Zwwtvbne/KxMKUS/r3vx/fJMcu 00lTbpYAe1ppt/CP1B4wXAIbDw== X-Google-Smtp-Source: AMsMyM4tzwFLRag/lpLMgdsijk8eSRKA4j2EjO7/q/UGv314yzDw8uVz4y7kZvwACtxz3sB32hsGhA== X-Received: by 2002:a17:903:20d3:b0:17c:8197:c4db with SMTP id i19-20020a17090320d300b0017c8197c4dbmr6094542plb.43.1665169915705; Fri, 07 Oct 2022 12:11:55 -0700 (PDT) Received: from desktop.hsd1.or.comcast.net ([2601:1c0:4c81:c480:feaa:14ff:fe3a:b225]) by smtp.gmail.com with ESMTPSA id s1-20020a6550c1000000b0043a09d5c32bsm1971571pgp.74.2022.10.07.12.11.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Oct 2022 12:11:54 -0700 (PDT) From: Tadeusz Struk To: David Gibson , Rob Herring Cc: devicetree@vger.kernel.org, devicetree-compiler@vger.kernel.org, Tadeusz Struk Subject: [PATCH v5 2/2] libfdt: tests: add get_next_tag_invalid_prop_len Date: Fri, 7 Oct 2022 12:11:16 -0700 Message-Id: <20221007191116.161426-2-tadeusz.struk@linaro.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221007191116.161426-1-tadeusz.struk@linaro.org> References: <20221007191116.161426-1-tadeusz.struk@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Add a new test get_next_tag_invalid_prop_len, which covers fdt_next_tag(), when it is passed an corrupted blob, with invalid property len values. The test runs twice, on a blob in sw and finished state. Signed-off-by: Tadeusz Struk --- v5: * Add back fdt_finish_reservemap() instead of directly calling fdt_set_off_dt_strings() * Call the test twice, on the blob in sw, and finished state * Use the new fdt_get_property_by_offset_w() helper to get the pointer to the property at a given address. v4: * I didn't keep track of the changes in the test code, but this version should have all the comments addressed. --- tests/.gitignore | 1 + tests/Makefile.tests | 2 +- tests/get_next_tag_invalid_prop_len.c | 106 ++++++++++++++++++++++++++ tests/meson.build | 1 + tests/run_tests.sh | 1 + 5 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 tests/get_next_tag_invalid_prop_len.c diff --git a/tests/.gitignore b/tests/.gitignore index 03bdde2..3376ed9 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -74,3 +74,4 @@ tmp.* /truncated_memrsv /utilfdt_test /value-labels +/get_next_tag_invalid_prop_len diff --git a/tests/Makefile.tests b/tests/Makefile.tests index 2d36c5d..2c5b4c9 100644 --- a/tests/Makefile.tests +++ b/tests/Makefile.tests @@ -4,7 +4,7 @@ LIB_TESTS_L = get_mem_rsv \ get_path supernode_atdepth_offset parent_offset \ node_offset_by_prop_value node_offset_by_phandle \ node_check_compatible node_offset_by_compatible \ - get_alias \ + get_alias get_next_tag_invalid_prop_len \ char_literal \ sized_cells \ notfound \ diff --git a/tests/get_next_tag_invalid_prop_len.c b/tests/get_next_tag_invalid_prop_len.c new file mode 100644 index 0000000..dc42412 --- /dev/null +++ b/tests/get_next_tag_invalid_prop_len.c @@ -0,0 +1,106 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/* + * libfdt - Flat Device Tree manipulation + * Testcase for fdt_next_tag() + */ +#include +#include +#include +#include + +#include +#include "tests.h" +#include "testdata.h" + +#define FDT_SIZE 65536 +#define RET_FAIL(fmt, ...) \ +({ \ + printf("FAIL\t"fmt "\n", ##__VA_ARGS__); \ + return -1; \ +}) +#define CHECK_ERR(err) \ +({ if (err) \ + FAIL("%s: %d: %s", __FILE__, __LINE__, fdt_strerror(err)); \ +}) + +static int fdt_next_tag_test(bool fdt_finished) +{ + struct fdt_property *prp; + void *fdt; + int nextoff = 0, offset, err; + uint32_t tag; + + fdt = malloc(FDT_SIZE); + if (!fdt) + FAIL("Can't allocate memory"); + err = fdt_create(fdt, FDT_SIZE); + CHECK_ERR(err); + err = fdt_finish_reservemap(fdt); + CHECK_ERR(err); + /* Create a root node and add two properties */ + err = fdt_begin_node(fdt, ""); + CHECK_ERR(err); + err = fdt_property_u32(fdt, "prop-int-32", 0x1234); + CHECK_ERR(err); + err = fdt_property_u32(fdt, "prop2-int-32", 0x4321); + CHECK_ERR(err); + err = fdt_end_node(fdt); + CHECK_ERR(err); + if (fdt_finished) { + /* Call ftd_finish to set the correct ftd state. */ + err = fdt_finish(fdt); + CHECK_ERR(err); + } + + offset = fdt_first_property_offset(fdt, 0); + if (offset <= 0) + RET_FAIL("Invalid offset %x, expected value greater than 0\n", offset); + + /* Normal case */ + tag = fdt_next_tag(fdt, offset, &nextoff); + if (tag != FDT_PROP) + RET_FAIL("Invalid tag %x, expected FDT_PROP\n", tag); + if (nextoff <= 0) + RET_FAIL("Invalid nextoff %d, expected value greater than 0", nextoff); + + /* Get a writable ptr to the first property and corrupt the lenght */ + prp = fdt_get_property_by_offset_w(fdt, offset, NULL); + if (!prp) + RET_FAIL("Bad property pointer"); + + /* int overflow case */ + prp->len = cpu_to_fdt32(0xFFFFFFFA); + tag = fdt_next_tag(fdt, offset, &nextoff); + if (tag != FDT_END) + RET_FAIL("Invalid tag %x, expected premature FDT_END", tag); + if (nextoff != -FDT_ERR_BADSTRUCTURE) + RET_FAIL("Invalid nextoff, expected error -FDT_ERR_BADSTRUCTURE"); + + /* negative offset case */ + prp->len = cpu_to_fdt32(0x7FFFFFFA); + tag = fdt_next_tag(fdt, offset, &nextoff); + if (tag != FDT_END) + RET_FAIL("Invalid tag %x, expected premature FDT_END", tag); + if (nextoff != -FDT_ERR_BADSTRUCTURE) + RET_FAIL("Invalid nextoff, expected error -FDT_ERR_BADSTRUCTURE"); + + free(fdt); + return 0; +} + +int main(int argc, char *argv[]) +{ + int res; + + test_init(argc, argv); + + res = fdt_next_tag_test(false); + if (res) + FAIL("Failed test ftd in sw state"); + + res = fdt_next_tag_test(true); + if (res) + FAIL("Failed test ftd in finished state"); + + PASS(); +} diff --git a/tests/meson.build b/tests/meson.build index 4ac154a..29a42dd 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -47,6 +47,7 @@ tests = [ 'get_path', 'get_phandle', 'get_prop_offset', + 'get_next_tag_invalid_prop_len', 'getprop', 'incbin', 'integer-expressions', diff --git a/tests/run_tests.sh b/tests/run_tests.sh index 244df8a..46678cb 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -513,6 +513,7 @@ libfdt_tests () { run_dtc_test -I fs -O dts -o fs.test_tree1.test.dts $FSBASE/test_tree1 run_dtc_test -I fs -O dtb -o fs.test_tree1.test.dtb $FSBASE/test_tree1 run_test dtbs_equal_unordered -m fs.test_tree1.test.dtb test_tree1.dtb + run_test get_next_tag_invalid_prop_len ## https://github.com/dgibson/dtc/issues/64 check_tests "$SRCDIR/phandle-args-overflow.dts" clocks_property