From patchwork Mon Oct 16 06:45:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 734021 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1045:b0:32d:baff:b0ca with SMTP id c5csp69698wrx; Sun, 15 Oct 2023 23:46:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHE8Qn3+qoIJwFu2gg/ehNGcVK3MFANOA2CTCIERWqESYyOt8Ls1g+tTngy33BTDVk4Dff7 X-Received: by 2002:a5d:4487:0:b0:32d:87c8:b548 with SMTP id j7-20020a5d4487000000b0032d87c8b548mr9630708wrq.21.1697438809858; Sun, 15 Oct 2023 23:46:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697438809; cv=none; d=google.com; s=arc-20160816; b=WQ4MgUxdC84g7IgyAZjGTggPqc0340Onw0y59xm2N43FBjYUgik7oxqHygZugwl2RI sUfQf0sDkPztIyaEAcWC6JtXb2t65RO8uOsq97XPOgK0DBL2vIkfjB/+D0iV2vbgQuGW 48Kuod65X2kKs+htv3fbhIIlm/TuH8sutbVqPOi21bpQbnPS7z1jn1FxOAIlE/4AeVgT Pbz8QTVufZu71knUEt2k9CQfQbXh8MaGQJR/FwtcdgtW2cYPKt2CgN+Y/cSsXvVpndbR CYDZCwt+rtrnUsuL5aYpMqCRnP5LnY5u3BS8o6/JrZ4Y0ztuwyDT4FrnsdKoYUQgj+NC zkUQ== 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=rJgraZNtSTqiRDbKGXGRoX89DnV2i0OwHl6eUvMwsd+C6K2/0NcWt7+gkLvGDAL3iv WDeTVgZ2ezNMWCP/trV2RlucpiyMGXVMJuYyAgB16iE0dM3/RBXYGxRku3gzMsDwTtD3 4hYEi8Xt+2OCKzrkUPvomSHD4cJyaRUE5RiwL0et/aTRkt+MKK5hauSRNTvCcI64eSMd +24qcoUDnR8VU+ggTs4bCNOxOyJ1kNmuKIh/sHB75/ZsQe7esEx5knMKshp6hxMq5S0D 7KP6GI5lCHqOyJF968GxuX6epjQ8BT4R0dLgau/vYkhvwojOMlt9OEghgJhTh45QrqcU sLfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SFNTNslc; 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 d16-20020a056000115000b0031ffe1596e3si10980745wrx.702.2023.10.15.23.46.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 23:46:49 -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=SFNTNslc; 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 529F086B79; Mon, 16 Oct 2023 08:46:48 +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="SFNTNslc"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id BDC68869AC; Mon, 16 Oct 2023 08:46:46 +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-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) (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 DF660868C0 for ; Mon, 16 Oct 2023 08:46:35 +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-x429.google.com with SMTP id d2e1a72fcca58-6b3c2607d9bso1821401b3a.1 for ; Sun, 15 Oct 2023 23:46:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1697438793; x=1698043593; 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=SFNTNslc9eAvZACHOHg12R1rh+Rs0OEtk0ia7pWnWJmIPKrBfuD27Uuk3AVXZCxJ7s mmehnlQnzId9UE5GdF29KUXGhwcVF3Dd/7FJuGZuqe78I8fUfUZStj54+mqdiwT/KY8n cZnYgiIwg8a9qPRT9s0r3NbSeU4olLrp3E5crNMqKwtkwOMPnYLbWXtwxV5T+4+ve0u9 dXnIE3lpq9YVpEy5CIV3QMSeicKOnW4PiIlVCZVl2A65hSB/xW3YiEsC2PR1ILgQOdjK RgaWt/2IOu8r1OXrHz1jnGIk+8r2Y9A1PGOjNagl8oL+Qe40/dgPoNReqUcrzpvOsYUA xglA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697438793; x=1698043593; 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=atooYxvPHfB6/8dKbUga8uLOewpcAjSm2defT844Pp7IQarhE3RAYgVgeWuC+PTUYR 1FTo4XRRKigOG1GqiR+r/z2KP357X5nl5Ovo50Yj3S6KAJNClegXx5kumYfxFAnEdbgm JZMB71ZbYRzxCSfHWOlKoa2xMbBcfhhijov3PvAQdpvUB6LoOFfPJa/7JtNgyl6SVZrP UnnxmIww51fl5KPEt1iLzlUNLfuqdfoJL9EnrbaduL+tv107PDhwILsO3pZDrYPi9rj9 F0OI1aeL1x9eyQ7FpZi/fUQWfMuFfVNBBlykL5pcQWqdImC71dqHQ8tvDGnBRprXUjuB 2vCA== X-Gm-Message-State: AOJu0YyejDJ6SOD2QitwK/SrZRuaN7M3EBcnAakkW3ejCnr76KocL3aL zWDP8q+Ad4+p9r3skKGwAVcEliRpMqDXhhfLYLc= X-Received: by 2002:a05:6a20:258d:b0:14d:e075:fc5d with SMTP id k13-20020a056a20258d00b0014de075fc5dmr33112772pzd.40.1697438793512; Sun, 15 Oct 2023 23:46:33 -0700 (PDT) Received: from localhost ([164.70.16.189]) by smtp.gmail.com with ESMTPSA id b4-20020a170902ed0400b001c60d0a6d84sm7744481pld.127.2023.10.15.23.46.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 23:46:32 -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 v7 1/9] net: wget: prevent overwriting reserved memory Date: Mon, 16 Oct 2023 15:45:18 +0900 Message-Id: <20231016064526.2410856-2-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231016064526.2410856-1-masahisa.kojima@linaro.org> References: <20231016064526.2410856-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);