From patchwork Sun Oct 15 23:48:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 733891 Delivered-To: patch@linaro.org Received: by 2002:a5d:54d1:0:b0:31d:da82:a3b4 with SMTP id x17csp2465624wrv; Sun, 15 Oct 2023 16:51:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEOG3s87+06FiSNjMZ20VWcnODyVgUUPmackAqQO7rOw0nPBXk7ZmR4LsHAFZ7IIH5TBfuV X-Received: by 2002:a5d:5052:0:b0:31f:fc6c:8b75 with SMTP id h18-20020a5d5052000000b0031ffc6c8b75mr26886291wrt.7.1697413877042; Sun, 15 Oct 2023 16:51:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697413877; cv=none; d=google.com; s=arc-20160816; b=V4c2sukp0RTdb5KSfn7P/S5OVTWgSJ5EF7anBbKgafuoJ7MXAeFuKksaf00iWQi3x0 6/glJDyRyq+INpY9Wx6nKaLq4XtYVQFAF1siKtNQIb6zVCpJjklAmOGJwVoFZfZ8FZYL hzoQGikDAhs/JiQWMBvBvDKJcuem1J7IQ9eEH2WdbvknCO61LWKqxYv6QnF4NIDRaZa+ MBCtZExOOU9LcycYj+kb2wBd+c/f9hxeXScT5Ekxu3t71ySfj3dEnwjafoJwwDWZs5rI Gs2Pr6imCb6ieZTZJiQ8lXQKiVUrxJRk5CJn//sI9Aj0PLNURXsBxZ/cuOSIme/ufwrZ vGHQ== 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=R58nKEFyB3l4t3Fwl5S1VRMsRdJHBYigotfbrE3kHL/Mb6AenS6IuqASClzOm6E4if qGarmFxxGrgtWjnB+6pWzzbFF73DB5eT7uGmOZtzjwfrTdMI70PcwD4uZsZe2TQox0PF w023cDW29gROFXqq16JVIMVF83C+VEQiHff/SzRN3EP51rkPVD0Yct06/CLnJucCvWb+ WJTPZD1AcBJ75Aijo2MgSrfU3sj4X+SBkJQanUtT80lrUuIIQFiMIy9DmG8dPSsTo9bF btpp32pe3f6pM77nU36CQN572gsdO4MJrz/QHAPglT2rB1nkkxtQqoRyw908EqPMRoQw 3KVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Kywo59Xk; 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 bp12-20020a5d5a8c000000b0032c82d0c7bcsi6983852wrb.866.2023.10.15.16.51.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 16:51:17 -0700 (PDT) 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=Kywo59Xk; 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 64CAB86BB3; Mon, 16 Oct 2023 01:50:45 +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="Kywo59Xk"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CCC84869D1; Mon, 16 Oct 2023 01:50:30 +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-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) (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 7914F864AA for ; Mon, 16 Oct 2023 01:50:25 +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-pl1-x633.google.com with SMTP id d9443c01a7336-1c9daca2b85so28348975ad.1 for ; Sun, 15 Oct 2023 16:50:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1697413822; x=1698018622; 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=Kywo59XkoQawqB0zqsC+iQN9L7+/9jWTWWGxZ9dZ7vE3lFFFZo0QZ3al3k2Th7mm9z kib0cDme/qi8pZaq0d1aMQuUDaB5PdgpkBKryHIyvNU/iV9O23I7OvOygoLmA3v3nAYv ZrtRHxALTTSZ83qCr7zhEY5kqc7W93Kt236oLjdmW8sWFix0u0cn2wjwQRbyisQXSW8Q TMoAnmHS1u1mr6kFNuQSLrkSxZE4Au7EXEDPXv3VWTONteGGygK7BX+c/vwLARLM3n/4 wLfEmLwNsCCfGY9DSTy+9aGXPs5etM8BvCoAXDRDsFDDVLHfnMaRHGYBfm2pkodhUf0/ EdBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697413822; x=1698018622; 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=XIs8qaQuXJF9o7a1nM8v8PKSpPx7pbflxvqsAl778bEa+qWbmoLsHJZQuwQz3j32z7 FHiijaGQ5OdyTXh95K3CRXci4koKjQlE5Gd82e3uDCEO3J1dsQdc7fCuIZ/AUL5gFeit KWPx6ScY7uPaPxvBB8B5D4wy0jZe7+a6HHDL6XFh79MLAOtuDBJ8lg9cRyl46mCLi/Ah BlLpBUauK+oedkwFsn9yBWboufqFuZI2AmxxJepCPvbRxaKpfUs2WLqeXJikbKydlU4B Nf0UX6DAVWcLdL4PHwRjoNcHK25ukTEMGtDx5OLavxjf9Pg0PnL76UsDrWK5LuZorPd3 xVlA== X-Gm-Message-State: AOJu0YykhhYxNkZX1az8hbIr5OZEoSvn+dEkUtdaCj7FhG5N1yghvKG2 ZZdJvdt07+Lht1+Ha/KbLmdqzqeu492e8Dc+qZI= X-Received: by 2002:a17:903:2347:b0:1bc:edd:e891 with SMTP id c7-20020a170903234700b001bc0edde891mr9063971plh.1.1697413821941; Sun, 15 Oct 2023 16:50:21 -0700 (PDT) Received: from localhost ([164.70.16.189]) by smtp.gmail.com with ESMTPSA id y10-20020a170902b48a00b001c60e7bf5besm7208228plr.281.2023.10.15.16.50.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 16:50:21 -0700 (PDT) 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 v6 1/9] net: wget: prevent overwriting reserved memory Date: Mon, 16 Oct 2023 08:48:59 +0900 Message-Id: <20231015234907.2362717-2-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231015234907.2362717-1-masahisa.kojima@linaro.org> References: <20231015234907.2362717-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);