From patchwork Wed Nov 8 11:06:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 742222 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:110f:b0:32d:baff:b0ca with SMTP id z15csp227386wrw; Wed, 8 Nov 2023 03:07:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IGlaM3r3zeK0Dt7I6NWhk17tj9xbJDblFCZaXsvb9U/JOn8NKdp2JYs2aD8vSPm3FAqCfux X-Received: by 2002:a17:906:f74c:b0:9ae:5a56:be32 with SMTP id jp12-20020a170906f74c00b009ae5a56be32mr4294576ejb.38.1699441678178; Wed, 08 Nov 2023 03:07:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699441678; cv=none; d=google.com; s=arc-20160816; b=oPCT6xN9HpNHdHvyQihx5ZfU4G7eRysOg/WgbZhlu0i0b+1IfwZbA1eIod977HStoh xzYM3fKUK9302RZSKV0nBNmZu/Jq6KRttcbqNPvka+sHLGQC+ylG4Q13DqE7XPsZ4quY yObKso9qIcwnBlDvs7FlLh6PoxF9E2+wpg425dcEa75e5VudTasQOQtTEri7b2E7cb5a IB78TLlK27OGVWTGxz2ZL1HF98GkftCoeMhiB1oK3qoU8Qs+iVQD33KJU3vErs/thfOV P6znDauz5HJuNeOLYJV1dHakExzVCa4Z6s2MPLoOSBEX2EdQmVE8NT8XTdl/9jk0E2if OuCw== 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=pkhzyYQRk5QmF55uLbwM6zHdjbg6egBMDjfWYvvujav2W0W7IShgEw3tXp0lW1asDe CB4BsjnPH1WPFopcSkmKZKHTbtadJRcddQjoqjvthoZYq1f5i11DP4g+JofM7+Urf3iS A4BJUsE/3uHcv7RIWr0BrUiqQcjqnwCNlfUua+43yi7I9Nin7l0rHehB2MQEe8O6/tH3 vG1MMKYaLiAiaCqNJcey/SZKwkAtJRPhaXUjaea2qWLazWWDRVYOMIV/OTOSnWVXrCmd p4g6AnlEy3zKLk/wF4ObsGmSvlXoBbYtIt+J++pC9Y0hH6GCUD50otl/o46kX1mXw4ml +87g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=PgYPJipZ; 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 ga41-20020a1709070c2900b009be5c079196si2016989ejc.461.2023.11.08.03.07.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:07:58 -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=PgYPJipZ; 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 9E9FF8746A; Wed, 8 Nov 2023 12:07:50 +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="PgYPJipZ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1937A8752D; Wed, 8 Nov 2023 12:07:49 +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-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) (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 66CA987525 for ; Wed, 8 Nov 2023 12:07:46 +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-x62a.google.com with SMTP id d9443c01a7336-1cc5916d578so60939285ad.2 for ; Wed, 08 Nov 2023 03:07:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1699441664; x=1700046464; 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=PgYPJipZ0WDFzwYQcdqvG7afUVGquvlDQzbkPyuHCmajlP1VoRXGXZFSaoUapSQ7XS aJuTo/iEBf87tpri5spqNKkB6OGK/5HOXW558Dj3E+b/0w6m+wlaKy56O2D71Hw6+ULl xZNH0rnc5WyH7QKG7SjH9I/XpQBL78qKFxkz6tKwjdS8Meazrby6TbkPsLMQvlvp2r1N 5RgRW7UBsexx6qVxebFccFQAWIhZfMM1evfHQ7pQ/ROKql4NqdPDsuNYtb6sTU6O0aba WSAhct4Pg9ygaJNPmBolZRN32H00hKjldwnJUbJRzEoEKZxTk3guf/T0UfgCUtyNJnig 5iuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699441664; x=1700046464; 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=VA0OQDiD/vKwB02MH8MVBFRx2L/BGsbl6IscUab+8nxnjTiZPQmYiAqbNJDr9c2RAU bzcdkFLGJ8Iopudg8pEqIjq37bbAzTSI0KkDC2uTw1eQtZG55dS41gr29wWYoKjZl0xj 9IT1uYd6E4/soGj+iVoxviCF4E3YhFVqK9jFz4lroZ2nAMf+o2XmF+/K4ycF0sClhZVa NlAj3qRf5BNENNDksL20DD8cb9qAPSvoJ8zkpRSdUVW+l4pICLjfrHDJM6xro2/u/ke8 QI2ILvxWQtGPTUxkY8qD8DxjNLi9J0ibJAgojSFf1PCsNzswwhpknoxOYG0ZK/3V4hsw /qDA== X-Gm-Message-State: AOJu0YxAYOXI2+RFk4Y0psuhioKl+WBJxv/w8H/ENZTmFxx3cl7Ooz/D uIuY0GgLo1CNPwpVR2Zh1G4bFmRUTuUwtLC3M3c= X-Received: by 2002:a17:903:244e:b0:1cc:4e81:36a4 with SMTP id l14-20020a170903244e00b001cc4e8136a4mr1907881pls.5.1699441664401; Wed, 08 Nov 2023 03:07:44 -0800 (PST) Received: from localhost ([164.70.16.189]) by smtp.gmail.com with ESMTPSA id d3-20020a170902cec300b001c57aac6e5esm1496956plg.23.2023.11.08.03.07.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:07:43 -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: [PATCH v11 1/9] net: wget: prevent overwriting reserved memory Date: Wed, 8 Nov 2023 20:06:28 +0900 Message-Id: <20231108110637.3691441-2-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231108110637.3691441-1-masahisa.kojima@linaro.org> References: <20231108110637.3691441-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);