From patchwork Mon Oct 16 04:23:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 734006 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1045:b0:32d:baff:b0ca with SMTP id c5csp33793wrx; Sun, 15 Oct 2023 21:24:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHWh+/0KfBsjv6vPp9qChrGM8NlueSJSD8Yocc5XK8uDbydstC/J76fMHS8r1/GUPr5YaqO X-Received: by 2002:a05:600c:1c1d:b0:406:5a14:5c1e with SMTP id j29-20020a05600c1c1d00b004065a145c1emr4803074wms.1.1697430290695; Sun, 15 Oct 2023 21:24:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697430290; cv=none; d=google.com; s=arc-20160816; b=GvvujCTXS0Ot/jbvDrVMZTJnjXu+/rKY5fdtsujWJ77iCQIUXThx/MMgfi7urtHunk DmJdEAZOSB8IVTxJTlL7G1TD5PeffvpcOoIQ16e/eduEkGNejv7bmUFar5TIj+YX/sIf 2XcVf6yAfDyI4fM7Nij0UJ51ltkSjBcXEhfh8Cmlt1ugS21FBQUQsR43ttdLyRTOG3Fn 78xyyTWctGBolkcnsbWTLJT1b5HTlQJ6926tvCe4hsuWELvOiArkeBgboPXJ0xxcxQcU RI67zZ/bg5MSdc/cODs62ydXqomQZD4jZKwadmb0vLKuGgRfa5OcHP1wMuDhknNewT3/ GjMw== 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=XjA5VBV2Z7ueS0coj4rZo1LlbuWXRcWZpPbSDpdfmDuZFFEeIkcrSswFTSf3yQJOKY vcWD3oDFd4ElYyekogDugkSErBWIJ30pj8cmVBf4y8Vl7nmWUxBc9qY9xL4pEfm8q+x0 8UL+LaD7W/UJQeRRIfRSZbYUt7T7ZzSnWx/2vk2gmBeXI/s3Q59qPqNODqIFCxwdYmMI xceHcxk24VCU7hiLHjvqhuQxL8Q7uvQAkWxQetERxf5FRptyP/RpP4oFftFv3U5RaIEC Fu4Ox2wTRUeLCjEPd6xCaSyh1G8mJ3PILPuSKhR7onNIYO8fdZ7qeZhKFzaodomzlJ3H QkRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=OceMo3n8; 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 d17-20020a05600c4c1100b004050f26342dsi2217312wmp.181.2023.10.15.21.24.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 21:24:50 -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=OceMo3n8; 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 1B08A86BB4; Mon, 16 Oct 2023 06:24:50 +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="OceMo3n8"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 6203E85423; Mon, 16 Oct 2023 06:24:35 +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-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) (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 9E21986524 for ; Mon, 16 Oct 2023 06:24:18 +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-oi1-x235.google.com with SMTP id 5614622812f47-3af5fd13004so2766908b6e.0 for ; Sun, 15 Oct 2023 21:24:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1697430256; x=1698035056; 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=OceMo3n8g8sKe+GgZN/6677bOVQnLnkZCt6HnomzD8jZnsb44RJzwrlWr8uboXThW+ 4PnFOH37ZjPDlqqFq27+NJO1D/CqCEV+wJd39RILf9WatX+t+e/8r2ABp0xmCx8xDPxP 2VGebPHI8YXCrCAa8t4AFrYXW2JE4PWoP0gJ6VzTdUnBI0aKt5tf5NIZ1wy0rs+1pJRY yb7Yc6gfQAkZoslP+SNo8NVo+hlQKV3Rqk349sG/azCtWSwB0D6lDgaQlApNBf/mdvxU m1pRSUfh2eoqjKAwuTX7SZ50l/y1zHtaUy/yxzjogUGG9gXCmU9pQ860p0xiZ8Pq2K7x J18Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697430256; x=1698035056; 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=SG1AeDZeUXkLRYJHlItVi1aoOJrfCYelzf/wiX3/Agqf8dVS/ltlP7a0BkpmBSj+l8 HPF2IFTF0OuyyS8YuWKFthlh47LbuLYw4NTFBiLfddFuQr1iLM0bnQL6wUzzWTSdQBwO poriU8X3qiAhFhHouSHHhzoLHug+wTzo/jkJn1u5p7rFUP0Q1USV/9HaoNua0uZzEbmI 6EcUuuqb2W/YRpdVi5rR+pCtWGCOQmioC09WpQbPjgWe4xwFifDS5ZwN7qx1AQhbsGrw 3MQEMfQmbseqvJusDzyhHsT0dLVGb08SciDIZcyly/1YIkbDDDlukYlu/iMzLu6DLTjf dm/Q== X-Gm-Message-State: AOJu0YxPTr29rh9Di3IyiHVLP5bmhZ2ScTH2wfbMaSlKzsqKbU+Oo/5p 2eVexGE/CP6zoPNzrV8blhkL+2G1EDImhzIFFi0= X-Received: by 2002:a05:6808:1b1f:b0:3ae:2877:9b31 with SMTP id bx31-20020a0568081b1f00b003ae28779b31mr38513671oib.20.1697430256688; Sun, 15 Oct 2023 21:24:16 -0700 (PDT) Received: from localhost ([164.70.16.189]) by smtp.gmail.com with ESMTPSA id u69-20020a638548000000b005b0b7ede830sm3254326pgd.50.2023.10.15.21.24.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 21:24:16 -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: [RESEND PATCH v6 1/9] net: wget: prevent overwriting reserved memory Date: Mon, 16 Oct 2023 13:23:00 +0900 Message-Id: <20231016042309.2386387-2-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231016042309.2386387-1-masahisa.kojima@linaro.org> References: <20231016042309.2386387-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);