From patchwork Fri Sep 15 10:16: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: 723104 Delivered-To: patch@linaro.org Received: by 2002:adf:f0d1:0:b0:31d:da82:a3b4 with SMTP id x17csp285510wro; Fri, 15 Sep 2023 03:17:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG7khArdAj+il1E0BW5QnVNcf3r7n0BC9rt3y5Z1/kNfDPlS3fYVrCjIMWKErWoPbqNa/uR X-Received: by 2002:adf:e807:0:b0:314:3e77:f210 with SMTP id o7-20020adfe807000000b003143e77f210mr1236282wrm.59.1694773061165; Fri, 15 Sep 2023 03:17:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694773061; cv=none; d=google.com; s=arc-20160816; b=ljE5lXhssSoZAkqQNZ8OU/EVClfwVp4g9Jaoccgdcf3cfpyHgQ/cGgYBNyY+7p10aP rwuRXpoYP+DErrSBkokHirGQOIpbnySq5gCbnL2wgosNTjVEbmkExm87uScBpO7KIu4x /wNoZRVZHqgMyaKKKj2yXPJrrWHk5iIE3JNJzWrdkeWj1AW4FQL798Eu6NdtIfiVly53 UZrLVJ8djWYk2CPF9WhGHQeLDjiDr9/DsN5qxki523AQdDHdJA0M0lMvXfmRltfamrXj bGgywfpk51iM/yQ2nbxUCRhlgU7XFY76NN2wyaauhcy9HdQvuu/5Fo8dRTu+lnmYf/ip UPKA== 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=uXypzoBjnA7EBhpmHWSKfXof0kGA/ra0Hb3wmn7+bGs=; fh=O5Tcc7OxYmMQkVi+jNNm3q1qDsnUZdKiJar3RqhYUSs=; b=MEF7UE07n686kVMdfz1V1B579kcvYxeM7RMXdvtoIh+dA1G5Vi1KkwN3sT/7LTur1F UMliBZ8+NpmeEqQOu8LacAEfP5YKWQTC0PgwL7qc8ZBrWw7ZlUl6OBhMiRJeZwJhrd4q hB5QR/ZXRnj4ALhWIOeYuM5uwtSdXvHRnUJMnWwL9YP9wwarlLjhZIOJkLskU/5YN250 JToKuB6JLtq1VVPuNAwfftR0ZcKkF5zgb2ui4aRwn+Y1vw9CFolHcpWklSDAtLBj8nOV QU2Qyv21kR29gYsb3hjCwqnh4QCm4EpU8HrSB5uUqs8kLy+JUjE5+rhv62V8Om6ggeiu tWeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zJS77mR1; 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 o4-20020a5d4744000000b0031facb6dcf0si1453458wrs.1042.2023.09.15.03.17.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 03:17:41 -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=zJS77mR1; 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 9DD7286529; Fri, 15 Sep 2023 12:17:37 +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="zJS77mR1"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0F3B38654B; Fri, 15 Sep 2023 12:17:36 +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-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) (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 CEB2286503 for ; Fri, 15 Sep 2023 12:17:33 +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-x631.google.com with SMTP id d9443c01a7336-1bd9b4f8e0eso15357235ad.1 for ; Fri, 15 Sep 2023 03:17:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1694773052; x=1695377852; 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=uXypzoBjnA7EBhpmHWSKfXof0kGA/ra0Hb3wmn7+bGs=; b=zJS77mR1ruMIrDdzH2qijfQUY8WhHYYCs0xTlkhb/Igv2nj29m3kXFtmNkqgYOdhsy m1lu3E33dnElPnF6BmCpT+uqat5K1A4L0eNMLNZv4UExYXH3lHpX3Zih4No09cbdjPAx bkQIlNnHAxjH3aZaTiMYYPh0APzSIfyYv9KW2VsRc4oJvXYVy7NblcNzjm5TQEcOsx8P jTXHDgJo84Zb0RWZ9DVfsXY72hnr52Uq7I2Jmdxh5h1VuU+H24fFNCv727tOPj51wnba v2Wj+CCU928v9pAn40ovTRywz4o+tIbtAZ5KCb4ef1REEllUH4+Ql7KRnwQ+p6cWM7HM LO6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694773052; x=1695377852; 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=uXypzoBjnA7EBhpmHWSKfXof0kGA/ra0Hb3wmn7+bGs=; b=gQmW6AEc3ZEtC0Y/bHKjqL7Ohc1a8zlAHVzs3cNzPpslwALBDtOQrwdNnp74mSBGJF bpQlwD2anvfqdZpzTPv1foGtxM9P2sjSaESONEKDvdJY0KchhF7SmTbFcmt8WIr5h6Ax iKApPm1V9VChUuv6rJNSxdnP+3gw9kqV/ESG3jxEHk3Sf8Ts4K5q55JPoITTVZne143+ I7AJbWb0J+h/fCn7CwdEwKd0jB9eSM/9M94Q43Z+b9O8u6mLgK4ssy9xf3vdpf7VugkV 4HJnqUXLAja3BtSTZphAUL2nC4lITsa85cpHBwvA4qAEwyOh9CwvanAJdc6PgncWpvu1 fRVw== X-Gm-Message-State: AOJu0Yzp7N62mt+JQUHnMNenB0YIBFjwtdAKVbH4VFKfLJaB2yefkXBr ucGXUEpgxtswZjSkOQGpfFAPS/i69XUNsJIU5Ig= X-Received: by 2002:a17:902:9303:b0:1c3:a1a8:969a with SMTP id bc3-20020a170902930300b001c3a1a8969amr1133987plb.8.1694773051832; Fri, 15 Sep 2023 03:17:31 -0700 (PDT) Received: from localhost ([164.70.16.189]) by smtp.gmail.com with ESMTPSA id 12-20020a170902ee4c00b001bf5c12e9fesm3119761plo.125.2023.09.15.03.17.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 03:17:31 -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 v3 1/7] net: wget: prevent overwriting reserved memory Date: Fri, 15 Sep 2023 19:16:34 +0900 Message-Id: <20230915101640.1166332-2-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230915101640.1166332-1-masahisa.kojima@linaro.org> References: <20230915101640.1166332-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 Reviewed-by: Simon Glass --- net/wget.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/net/wget.c b/net/wget.c index 2dbfeb1a1d..9690e4539c 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) { + puts("\nwget error: "); + puts("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);