From patchwork Mon Nov 6 11:39:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 741368 Delivered-To: patch@linaro.org Received: by 2002:adf:fd90:0:b0:32d:baff:b0ca with SMTP id d16csp1035595wrr; Mon, 6 Nov 2023 03:40:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IGcxuyhj9y3lOVYLpoN+ur8WihIj5FVaoWyvfzU5wb2W/lakACthdfiXFnNhqzRH8rhL0FH X-Received: by 2002:a50:8a9c:0:b0:543:5116:b55c with SMTP id j28-20020a508a9c000000b005435116b55cmr16995561edj.16.1699270837389; Mon, 06 Nov 2023 03:40:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699270837; cv=none; d=google.com; s=arc-20160816; b=I3FIj3OtAgNTWAjTDnXvXL4W0/VscHAPawy0NQqFNhp5ngpHWELXa8hFhgl4gsJ+9b 0RSKPk5ckDhJTd0dJ9I9y+TJbfoJ9avVokwfV9tXfEGGBO0eIxrlAIW303Kp+ZAIr1f0 iVOhYiT0YmVTOtw+zgoJJFJYj4dMGOXRYrwQCDXwHEgH7SurNTXROSdJ18joCR/gFLK9 Bu4dFUHCGUwF+T2VNiJxkr1C5agWQp82DAWYr1Bq7cSIO2GbFstJ50LefYN9i3hJKYP8 vmD1OvNdPnrse7GXayoLcIVRy8u5ZtEd2pKaDM6NJywN142Ird73N5P4ADfmk48yFo8D 5XJA== 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=AqNZNjqQYhADk2f9LcxrecaAQ0zvQd04y6QhLiDYaOs=; fh=eBItVz4B+jX5FuRNHVjUaldxTsf8xOQKLrDTtjpPbrk=; b=TH5NSl0WgtdqavzX2Xt70vLIVsZ2LqoJ7DX+2al0C0r9o45W/eUB60Mht9AZaymJu1 Alyai7v/k7KYp7x9+lA8sizvOn29Z3mLu0D+zerlOZamiuaKZ3nvms0KVMYCLdLVa+Q4 h76jWqEanpS9/uV4vcfqhagCU7BkYYBS8MWCWcxgX/NKX9iWb0jD4cesEz25NWbm3ggp qHWVdQO6UOl3PLqTeyEwh2bbvNagQT3aYPe9y6x++MWe6m7g9VHoM5o393oul5GXok5K o5CYPmFM+thbENFaEmxwYsaPsrfomChegNAqqcZf6/ia1plAcLiQEF7VRUHaiuCgdMyS iAbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=wqV2mED6; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id u24-20020a50a418000000b0053daf001684si4494766edb.311.2023.11.06.03.40.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 03:40:37 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=wqV2mED6; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3C38786BA9; Mon, 6 Nov 2023 12:40:36 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.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=linaro.org header.i=@linaro.org header.b="wqV2mED6"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8759A86C33; Mon, 6 Nov 2023 12:40:35 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) (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 A511086E40 for ; Mon, 6 Nov 2023 12:40:29 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=masahisa.kojima@linaro.org Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1cc53d0030fso34813975ad.0 for ; Mon, 06 Nov 2023 03:40:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1699270827; x=1699875627; darn=lists.denx.de; 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=AqNZNjqQYhADk2f9LcxrecaAQ0zvQd04y6QhLiDYaOs=; b=wqV2mED6QcTF/lhs9g6o+xBbpdeLqE8z8ZMX5aJGp/o7FTinqlnEKmixEZEgnSUmrH PY4T8X7QlRMKJhu5WSvG73zz2ynxB9/qKB/n6AuhXBJc/fE/0vy6GKbn7/jVWEIgX8ED b+2zwZheMSwAtQnh/uDNWu6QBByLf5/+vgxI1Pq6MtjCc53WEWAJRVPgvm4vgCbfRgHv U2EFZtHcgxPtPRB+EYEmTG6optuaLbhmbBW+Nfcl/SVAmJRLqoDrWldpwdu3N49cTT0b KDmnOBuNhTvkLovpzT3c43niaqpuugLnprFbcs7N7t8kkYU6EPsYZ1yBKPREch0rSIMI krOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699270827; x=1699875627; 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=AqNZNjqQYhADk2f9LcxrecaAQ0zvQd04y6QhLiDYaOs=; b=QDHcyjQ51ZGKfeD7ttYWX424wmTxd3HNsiF0F1VXYX6h2Ek/tpNrvT4oKVG03vhJqd FLRYlaDPKejZvrxBtUsv104ni/Kuu7zaSuJzDbpdNJL6mrSWhtu43I0FX81R1RxaAieN L4Uvj8AWCBO6WJGPGtKJNeMGXytC6ZxwOKIqBEJsee+u+7p1Ic5gXiaShEACmnEojSMj FCgvp1eep4BEU7liuUGBATxYhuks76eQIuHm/t2CMPoX1tRSH6Nc2pczem3FPTngN3pD 84bnLHyVeIfYXy3RThkZGQvGXRAUbSq+TAqdMWoCTFrkA86eZJxDmuuskQdc3gwC7AXL TWpw== X-Gm-Message-State: AOJu0YzMzKrJht6EjJGAZU2dnFI2hggirTUSDfDu48qJHcEMHcBc7H4l XZHdywVMuXpkAD/NF6O4AWwnDxgymFKqGQ9U/XA= X-Received: by 2002:a17:903:120c:b0:1cc:467a:338f with SMTP id l12-20020a170903120c00b001cc467a338fmr15655173plh.4.1699270827119; Mon, 06 Nov 2023 03:40:27 -0800 (PST) Received: from localhost ([164.70.16.189]) by smtp.gmail.com with ESMTPSA id b2-20020a170902d50200b001c60c8d6b4asm5730856plg.149.2023.11.06.03.40.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 03:40:26 -0800 (PST) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Simon Glass , Takahiro Akashi , Michal Simek , Masahisa Kojima , Ramon Fried , Joe Hershberger Subject: [RESEND PATCH v10 1/9] net: wget: prevent overwriting reserved memory Date: Mon, 6 Nov 2023 20:39:12 +0900 Message-Id: <20231106113920.3631591-2-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231106113920.3631591-1-masahisa.kojima@linaro.org> References: <20231106113920.3631591-1-masahisa.kojima@linaro.org> MIME-Version: 1.0 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.8 at phobos.denx.de X-Virus-Status: Clean This introduces the valid range check to store the received blocks using lmb. The same logic is implemented in tftp. Signed-off-by: Masahisa Kojima Acked-by: Ilias Apalodimas Reviewed-by: Simon Glass Reviewed-by: Ramon Fried --- net/wget.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/net/wget.c b/net/wget.c index 8bb4d72db1..6f97eb1d12 100644 --- a/net/wget.c +++ b/net/wget.c @@ -4,16 +4,20 @@ * Copyright Duncan Hare 2017 */ +#include #include #include #include #include #include +#include #include #include #include #include +DECLARE_GLOBAL_DATA_PTR; + static const char bootfile1[] = "GET "; static const char bootfile3[] = " HTTP/1.0\r\n\r\n"; static const char http_eom[] = "\r\n\r\n"; @@ -56,6 +60,29 @@ static unsigned int retry_tcp_ack_num; /* TCP retry acknowledge number*/ static unsigned int retry_tcp_seq_num; /* TCP retry sequence number */ static int retry_len; /* TCP retry length */ +static ulong wget_load_size; + +/** + * wget_init_max_size() - initialize maximum load size + * + * Return: 0 if success, -1 if fails + */ +static int wget_init_load_size(void) +{ + struct lmb lmb; + phys_size_t max_size; + + lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob); + + max_size = lmb_get_free_size(&lmb, image_load_addr); + if (!max_size) + return -1; + + wget_load_size = max_size; + + return 0; +} + /** * store_block() - store block in memory * @src: source of data @@ -64,10 +91,25 @@ static int retry_len; /* TCP retry length */ */ static inline int store_block(uchar *src, unsigned int offset, unsigned int len) { + ulong store_addr = image_load_addr + offset; ulong newsize = offset + len; uchar *ptr; - ptr = map_sysmem(image_load_addr + offset, len); + if (IS_ENABLED(CONFIG_LMB)) { + ulong end_addr = image_load_addr + wget_load_size; + + if (!end_addr) + end_addr = ULONG_MAX; + + if (store_addr < image_load_addr || + store_addr + len > end_addr) { + printf("\nwget error: "); + printf("trying to overwrite reserved memory...\n"); + return -1; + } + } + + ptr = map_sysmem(store_addr, len); memcpy(ptr, src, len); unmap_sysmem(ptr); @@ -248,25 +290,39 @@ static void wget_connected(uchar *pkt, unsigned int tcp_seq_num, net_boot_file_size = 0; - if (len > hlen) - store_block(pkt + hlen, 0, len - hlen); + if (len > hlen) { + if (store_block(pkt + hlen, 0, len - hlen) != 0) { + wget_loop_state = NETLOOP_FAIL; + wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action); + net_set_state(NETLOOP_FAIL); + return; + } + } debug_cond(DEBUG_WGET, "wget: Connected Pkt %p hlen %x\n", pkt, hlen); for (i = 0; i < pkt_q_idx; i++) { + int err; + ptr1 = map_sysmem( (phys_addr_t)(pkt_q[i].pkt), pkt_q[i].len); - store_block(ptr1, - pkt_q[i].tcp_seq_num - - initial_data_seq_num, - pkt_q[i].len); + err = store_block(ptr1, + pkt_q[i].tcp_seq_num - + initial_data_seq_num, + pkt_q[i].len); unmap_sysmem(ptr1); debug_cond(DEBUG_WGET, "wget: Connctd pkt Q %p len %x\n", pkt_q[i].pkt, pkt_q[i].len); + if (err) { + wget_loop_state = NETLOOP_FAIL; + wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action); + net_set_state(NETLOOP_FAIL); + return; + } } } } @@ -338,6 +394,7 @@ static void wget_handler(uchar *pkt, u16 dport, len) != 0) { wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action); + net_set_state(NETLOOP_FAIL); return; } @@ -428,6 +485,15 @@ void wget_start(void) debug_cond(DEBUG_WGET, "\nwget:Load address: 0x%lx\nLoading: *\b", image_load_addr); + if (IS_ENABLED(CONFIG_LMB)) { + if (wget_init_load_size()) { + printf("\nwget error: "); + printf("trying to overwrite reserved memory...\n"); + net_set_state(NETLOOP_FAIL); + return; + } + } + net_set_timeout_handler(wget_timeout, wget_timeout_handler); tcp_set_tcp_handler(wget_handler);