From patchwork Wed Sep 27 09:36:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 726692 Delivered-To: patch@linaro.org Received: by 2002:adf:ea87:0:b0:31d:da82:a3b4 with SMTP id s7csp3430524wrm; Wed, 27 Sep 2023 02:37:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEQf4RgCHhm//dIesGGzgokPVsVZBTEnJE8bRm0o/8guQ/fau+OtaOUa4F9SXUwIVFSwz6s X-Received: by 2002:a1c:720a:0:b0:405:39b4:3145 with SMTP id n10-20020a1c720a000000b0040539b43145mr1538650wmc.2.1695807457335; Wed, 27 Sep 2023 02:37:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695807457; cv=none; d=google.com; s=arc-20160816; b=DBd9ebo21R5oqYOhGI+umLNUq6QvleUIaIMsXtZcZP59uYRZwBO2jpmM2NQY2HiH+x zVp2Pg2kALMBhLUozUjwwV4aFv6x6SW+fKgHCAc9r1bTx+8CXP/UqcwGCVhNGNC+tx2y Dm6Eo9D0+ZTmDo2DKJimdIMevixZVES55zAYVNLrPu+nXz0zkKkqMkjvOjPis9SxKPOj 9SpuiXeqWRr3xnvxND9FkvZQKm+ItjRCpo2IJG2nZ4thkVhlW6wrzvoNNJHIqdSRzT/s x9InY0N/qsL7hYU/dkJfts4MSJvv3/WutjRNGOJk+SPI+JrO9O8BXOHFYaB5O3FLCHkK RmLQ== 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=DPpoO3E3wKSKmirTYAQ6I9w0N2Cw2YOKQMy00nQW3fw=; fh=O5Tcc7OxYmMQkVi+jNNm3q1qDsnUZdKiJar3RqhYUSs=; b=pm4L+RzOHyfUtQdzkn5y7Ecfa06m/iZYdMjxDcQaeEQkgd5xGnsRag6u3ry0U1U6lK wKBlvKBXAWkhVETTOKN9JXRZ75wW0Sk15OoBLBg9Lu+EySBaMSm/7YzyGrslFWoFEMqD +AKeekf2qNmSZJ2aoA142JpU0dwSKlHkspvZA0XcgnCe1d74UadHBTmHSpCA2vROOAtk eroXmOCRl43kwfPnJIG1Zwqg/XyyptYYlZqTk88F0dPBwU5lRYT2Q7sgQt81VnX0AICl 7wlyPwyM+iVnKx4ZdYfkBUKQhDd5h9iVd/BxPOpIThvd89gMbdSWl130MW8Vz1SdX09Z 7WzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=G5lDVLdP; 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 b10-20020a5d550a000000b0032326716ba4si3778977wrv.520.2023.09.27.02.37.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 02:37:37 -0700 (PDT) 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=G5lDVLdP; 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 9ABDF86D52; Wed, 27 Sep 2023 11:37:30 +0200 (CEST) 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="G5lDVLdP"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CD8AD86D82; Wed, 27 Sep 2023 11:37:29 +0200 (CEST) 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 autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) (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 8780C86D4F for ; Wed, 27 Sep 2023 11:37:27 +0200 (CEST) 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-x42c.google.com with SMTP id d2e1a72fcca58-690bd59322dso8082465b3a.3 for ; Wed, 27 Sep 2023 02:37:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1695807445; x=1696412245; 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=DPpoO3E3wKSKmirTYAQ6I9w0N2Cw2YOKQMy00nQW3fw=; b=G5lDVLdPQ/S16Gh0kHVv/fhVLNJh02CPa435CcrdMmlRPLvUmW78wwqpvRMUUOq71W /YnRNBUCXCEJr+Zu7TQIbaZaVEdIKkGBof4Amh+IXUlojr/Rsl1PpW9DrLv0g6BJhxtG OXZk6633w5bX8xig+4AfYAFqV6E4y4I8jCJrp+HxSuwWOTTGAjz53TwNIsdpZSEjsdAS 0r4Hz0igRe3V9cLZAN+FGmkoBPNr3xVtlYJJXZZ7vXyUSQeNDYJ6DJbh0ahijPtvFnTl Al+kbS5/K8MgYYsHsWMlTFKddQ09HFMdINAk3BhlJx14HcaXgRc1/MjJfjgAi4gwR8nD myjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695807445; x=1696412245; 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=DPpoO3E3wKSKmirTYAQ6I9w0N2Cw2YOKQMy00nQW3fw=; b=O5P5gFditBVNEFdj3DHmKYqIvhsskboKVMlqZ89BI1Y95Y961TBWO9u7EH7AfuMx9O IV0c7TYmmu+7vr5pu4JdcGHVtDiES4I/3O+ro12HGrNeeQX0swvAZD6gM5ZjqroTSANK EvDrJ/hX5f/AWm79gmB3e/Ynl2C8D3yXBcPt0Ly7IkCIl+70rt6CMdBAme7d64SJ6jWX hoYLBBdCg+XcC+YNQvbY7w3VNv3o9RbI/uMBzGeZWjgtty0LxxB/KzvTag9z6HXtl5Dw BNL9VY6yYrNNm5fx6RBvBT2bjNy5LS9mQjjV1kA9/Zd3icIMY08kXh76GqIxH6Diintp Mv2w== X-Gm-Message-State: AOJu0YwOaJ4jNd0MWrkrMBHafabMPaWJJae9Y8rGV3i9SaLDKrpmSnQZ /2G/ItP8ntgeQaoA6BNM+sd8m91lsYmr6EocQoU= X-Received: by 2002:a05:6a21:a5aa:b0:13a:ccb9:d5b7 with SMTP id gd42-20020a056a21a5aa00b0013accb9d5b7mr1925664pzc.41.1695807445421; Wed, 27 Sep 2023 02:37:25 -0700 (PDT) Received: from localhost ([164.70.16.189]) by smtp.gmail.com with ESMTPSA id jd3-20020a170903260300b001c3bc7b880csm12650173plb.256.2023.09.27.02.37.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 02:37:24 -0700 (PDT) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Simon Glass , Takahiro Akashi , Masahisa Kojima , Joe Hershberger , Ramon Fried Subject: [PATCH v5 1/7] net: wget: prevent overwriting reserved memory Date: Wed, 27 Sep 2023 18:36:25 +0900 Message-Id: <20230927093631.1595494-2-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230927093631.1595494-1-masahisa.kojima@linaro.org> References: <20230927093631.1595494-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 2dbfeb1a1d..a48a8cb624 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"; @@ -55,6 +59,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 @@ -63,10 +90,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); @@ -240,25 +282,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; + } } } } @@ -330,6 +386,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; } @@ -420,6 +477,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);