From patchwork Mon Nov 6 11:14:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 741337 Delivered-To: patch@linaro.org Received: by 2002:adf:fd90:0:b0:32d:baff:b0ca with SMTP id d16csp1026781wrr; Mon, 6 Nov 2023 03:16:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IFmTbqp0d040tehjgTS7/6f/BM0tlejfe1at11X+iTjiAIfUQCy7aFq1HXvopa8xBzRAelH X-Received: by 2002:a17:906:794d:b0:9be:705:d7d0 with SMTP id l13-20020a170906794d00b009be0705d7d0mr12528367ejo.0.1699269417501; Mon, 06 Nov 2023 03:16:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699269417; cv=none; d=google.com; s=arc-20160816; b=MnleEhTgLextfC5wL2x5iwKODqxrT0O4OyzlzaueA4kpwOjGRQ05Vmx6IAGxdbPYXF OTHhOvwvdxMFEd53462fguub+KnrgDYjmSFMyzDRA8GepyQ0IUeJwRLJEy0xT1iazZWZ arrwv5NefWM0p8vKQ/3qnqAcUwWy18xiuq6flZpU+j4Ya0l8IdHRjF1UtiF9j8zosuOB syH/xz04qLk57wMuFFtc1kAQ3IYfwqhn3EUQhbfD5mntzKCu4BPspbbLBoy/4ZR3Jg9R ukCmACKVNSrdluYHEu/tUyQOcTPkuk6jPrftwZ88UzMfFAimF5opjWmZuLlhpw9Yg/hA Y53w== 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=rQDMNQ1jZZ6tFPltarfkz/2HIruR2mVBL3kfLv7fj6kqd2Prei5KI8V8MGoERp77Eu A82h73OgTbEoRu9UDLQcNhpT1yn0O/L1EqRA/ZCFE45bWTU9V0pMnqGANDl34E0IPjJx 3S4l6NB2XSqg8bzsRmbii33qTVfozRzX7shUKbUe48dNLkEFDJr0h48zHxhSvdCEMFPM siRCIcWiXp0q+8pncflN9lqfa4VFoLflzxvPxxAzGoH0nweBn8MiKZX7jxVUgWzhagUE 0puNLsZkAhK64uvOAWwBKuWE86vm0S/85M28yk9dzc3wFbfCk212AXvx6ZxEEtOI0ipB 7yeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=D7ljuDqm; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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. [85.214.62.61]) by mx.google.com with ESMTPS id vg18-20020a170907d31200b009ad833b57e1si4514472ejc.195.2023.11.06.03.16.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 03:16:57 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=D7ljuDqm; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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 7892F86F91; Mon, 6 Nov 2023 12:16:45 +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="D7ljuDqm"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1E62C86C33; Mon, 6 Nov 2023 12:16:44 +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-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) (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 8348686F42 for ; Mon, 6 Nov 2023 12:16:40 +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-pf1-x429.google.com with SMTP id d2e1a72fcca58-6c311ca94b4so3645531b3a.3 for ; Mon, 06 Nov 2023 03:16:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1699269398; x=1699874198; 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=D7ljuDqmFDJF+PQT4hgzAUbpfhvnDnX53tiJqnuidozxZaC3oclXBltv5a/9cxCPeW fkt8/SBESMWCUTDHmOyfxiO93FO6drAu6wG41aZSLGeL5vmuGdM8DV6vsugcVP8DWacC vZ2T0/13Gm98BlQMwbu6oC8Rab8nA5I+dZ/pwaYoq++liaXJe43DSYhYVymaFwrmZ8uQ 1tu2ZfobJ7xv1nxdWvlqIQmXiOWp6w3bJYndMJ0GlC0f0dPVZ9I6mWLPDCuDxbLv5mhR ACzr/Z7AwWIrGXUMq5msmjH4NwzmJVWcpMev09gY4NIkx7ZdLGLeyzvZ8J12YO262RWf LpxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699269398; x=1699874198; 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=QqNjC4s/YltwLUjlSuux5uAx1T3Qdon+J+jn8bq8i0Ie2PwI/ITLWy+qVmic4u6N/R QnHOiyI77hedO1p3SliBN3BCIelfVhSoBfcZ2g0OssCfmi1JQ+uAz7wi56C2zuMzaSDJ xx6zvCDUPmrp7n8F3SW0RM1WwUpxt8OFxBaC8XEUfrqTYISFI4wpXAxVWgeFMi2LHPYb yalXiBAvU2z12MFF1EyUL+Rsy+beZWFhfBy3ozFPn6iX7qC6o3xDDYe0PSDKGZceceCS AArIIiL4ivDOMVF+pXmI9syZhXEeUrNLACA5ehUhnhgekhIB3T2qIkLTnDADa2tPtPfb HZUA== X-Gm-Message-State: AOJu0YytytpCkjuJiDq1aGCK+HZU7uFx1zlPVcPxzo7HqMrxguPMFDSB Vt4LBn7Cj79q4tsvij1+1QO0xxQL9Dv43xEOhgo= X-Received: by 2002:a05:6a00:1143:b0:693:3851:bd3e with SMTP id b3-20020a056a00114300b006933851bd3emr26639258pfm.3.1699269398675; Mon, 06 Nov 2023 03:16:38 -0800 (PST) Received: from localhost ([164.70.16.189]) by smtp.gmail.com with ESMTPSA id n1-20020aa78a41000000b0068fbaea118esm5467580pfa.45.2023.11.06.03.16.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 03:16:38 -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 v10 1/9] net: wget: prevent overwriting reserved memory Date: Mon, 6 Nov 2023 20:14:56 +0900 Message-Id: <20231106111504.3631058-2-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231106111504.3631058-1-masahisa.kojima@linaro.org> References: <20231106111504.3631058-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);