From patchwork Fri Nov 10 04:25:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 742837 Delivered-To: patch@linaro.org Received: by 2002:a5d:67cf:0:b0:32d:baff:b0ca with SMTP id n15csp12359wrw; Thu, 9 Nov 2023 20:27:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IHfLtOURinTM/EvT7rfaF0BD5cWhZH1GNhxEMN2td+f45qfzFtvGL9XRa3rI+vHgif18tUF X-Received: by 2002:a17:907:1ca3:b0:9dd:30c8:6f33 with SMTP id nb35-20020a1709071ca300b009dd30c86f33mr6133692ejc.57.1699590467737; Thu, 09 Nov 2023 20:27:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699590467; cv=none; d=google.com; s=arc-20160816; b=eFyPDyDByy/5mUaNWi40Zxf5Jx85Oa2OXJ25xScutsQl36C7k8++cGVyVv/FVD9lTB zg1mSyW1+Q0MUL5qwzcwuzp74Mclm00iwKGUYnOSWZ+JWAYVr7NDteIriv71cH0sRH+V MRoh28B9q/CV5B+ep4QcCDeD170UFNWCW8//QOgAHX+6XM2WUXfo8Tu0gDMXgirqyxpU c8fY64vM4EENvr4DVYsrszKxJt61AIa7HuF58mOjnAqwrHJ5yupX4ifMysDAcCGWYF8Y YVXTTix5+ePYN+kR5Q+MZfQDr+Symz8z5wr9uKM1Lh11A9f5B2F/1aKGF+/r6wtiO4zu qp4Q== 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=FpU7UIKzl9U1NPYKYifRIinn8xXs3wKRuf2re5x6OfL9/gNpK4MMh6NHMIwif0WZjh mYFJPd+ezcUzoU+3Wa/C5bwMlUkSn5cIjlTpN62ITvQz5EuprW4fHhx0/rPRpYmXEWQo 7njaJiX2NO80n/5enZB5u6qmNx+s4qZGd6W+7CHzGJ69/0C59crWpBCWZS59KFxhEePV tdAG+KMFLC6ADQZlsclzvsKb7PVfR8CFnlpZydFueqd0YImY9A/pgqxZwE39KT/sOLWU C+skdpqhRLiKQIg0KeSQLwbNrfpalb/e4mSiVzL+wdH5hmAs/yWW2L2317rDWyXxPdJz eoRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=eeB+hhrh; 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 qk19-20020a1709077f9300b0099cd34c0f5esi4189187ejc.466.2023.11.09.20.27.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 20:27:47 -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=eeB+hhrh; 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 9F5D387786; Fri, 10 Nov 2023 05:27:20 +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="eeB+hhrh"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3D49E87788; Fri, 10 Nov 2023 05:27:14 +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-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) (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 41E3287785 for ; Fri, 10 Nov 2023 05:27:10 +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-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-280260db156so1421848a91.2 for ; Thu, 09 Nov 2023 20:27:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1699590427; x=1700195227; 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=eeB+hhrhPoHTRVMxFVPacb1iEVr89I5AHqorFdhfQgU2riI/NbmKi4eEqVNIK12YMD 2mRCzmZfkpUzH8FDtrYUCTiY8hWmYd+jzhQ2fWecPONln4uuwqxRMMy9civygBkmxwIO YlQXQtehvzcRpnfep/82g+qpmrv21WTuBDNB+rzHBEWaGIdAbzTXpPIMrrmJ0W+AYhqY SJtiQkYWSsiXChcCfbMb3/JvF1EBHxmWr5GnuCCQzNlkRILMtHJfAQq27IsiwVHVLoHE aaWz77DFZX7PiBHnCDh8bdcX2sWoeF3aSXF4JRSx7gZOubejayCTkEfd7h051H6ufI7c R5Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699590427; x=1700195227; 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=xA1+60OT4dz3b5oONTbPlra4TBp+tGpN+BpGldWLTNsaLPrJB51ZC2fTIm7Ynjk/Su rCNQF3cT2VnAWLWOTKGYhF+ZLpuHcgJcPczzEqYyM8V6QwOic9redGMZtbKiQMJr8ghd LCznY+wfg0EI0TzqipvcDj/unZNTmyXLsta+DJgwU822rz6iyBO25QdAHbXW1aA9OPfZ JQBBdzEJfTW+LOrkvsDkBohrd5Vgnid3t3mm+3ggRfTSXOZtofDgvKCSmql4k+HdcyNW SYqZvNKv6T9L+EY7l34mgk3PzXmCZ/SZkDxPwP5H6I7kmeX9rGr3qjcD9TmgrgtjMzIl LT+Q== X-Gm-Message-State: AOJu0YzeCmbcWSXDilRozha7toFrLa8taje1UalhbWU78NgCeMjuOMC6 aQawc7Sx52yXJppKSpQA7YTDN+7lnkCcZGwq2yE= X-Received: by 2002:a17:90b:1b47:b0:280:1dfc:1302 with SMTP id nv7-20020a17090b1b4700b002801dfc1302mr3920329pjb.17.1699590427586; Thu, 09 Nov 2023 20:27:07 -0800 (PST) Received: from localhost ([164.70.16.189]) by smtp.gmail.com with ESMTPSA id a17-20020a17090abe1100b002775281b9easm612121pjs.50.2023.11.09.20.27.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 20:27:06 -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 v12 1/9] net: wget: prevent overwriting reserved memory Date: Fri, 10 Nov 2023 13:25:34 +0900 Message-Id: <20231110042542.3797301-2-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231110042542.3797301-1-masahisa.kojima@linaro.org> References: <20231110042542.3797301-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);