From patchwork Tue Apr 15 19:34:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 881241 Delivered-To: patch@linaro.org Received: by 2002:adf:f902:0:b0:38f:210b:807b with SMTP id b2csp626208wrr; Tue, 15 Apr 2025 12:35:34 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVkU2K7uLeD4JNrd/owOkgbf1eUJB9xtEayJUlVjMq8e0ygB6nJx0ugoOAAwhZZmApbzaAsyw==@linaro.org X-Google-Smtp-Source: AGHT+IGO4LdQIf3nEBoZ4rDfE0Fr5Qcw7pkk2WOm5SUP6KuoMNJE798Zia7K/bXKx79gx9q+sszr X-Received: by 2002:a05:6122:3d16:b0:50b:e9a5:cd7b with SMTP id 71dfb90a1353d-529092ff545mr434713e0c.9.1744745733765; Tue, 15 Apr 2025 12:35:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1744745733; cv=none; d=google.com; s=arc-20240605; b=Oj58VPYB3HKhDEJtBYlMAZ09Qy7zqYi7YP/vXiKyO0aVFxvkZHiil9OiTPo62CZsui fq9g+5PyXGWly/jZhpxLUngueZofFjqRpu2QKc3FevPinChhPBpQBdPuinSe+IFlUQZK tnvFYEHhy9zxg0Yd0/pqyvUICEKUjrn9SP+htdIz3QJRYYTXgjwE+66n+ADa2oBlJhNB KSyAdLp4nh6IQPNOlFGktdOP2gxKuFM9mI2zaTUZKieVpKgLm2H1mqHymubT1avKo7Ok JCf2UPQtYxzdkxPicjZoyywZrpPIow1p6t/YMEvnD9wAyC5PTnHWIFLaVjvtc10XBdjb hHKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=g48XLGI3oYJiC+FEguq2qZRcjoaBxDTm/BABIiCSeQY=; fh=PQg1uaxoFkVPH7GvYt2sPX/rmqpadkXu1gxBQgKczic=; b=kkOQvCRFFE49Ule7mEaROl0EGeDlAQurYOQ6obbBuPBiRg5n5lni0OzihcNXn2Vq7Z IfydNNpuJAAwBlhtpcvP+39CoS6wZyzVjz7subsl9ZQ/nlPzAXK2wHcabX31Pu8HIYeb RGmwDatPkbPsWLD86XJ2JlmbV4TbOlWcfxDjZTGtFhlcyCuflJcLTJTbEYa+empUqzkA UbH9GmGn8gbVDvP5fllPjRgn/KiVSsw7XifEhBaVFrEUsymOe9Cva24wpBI0dWDLV33d yZ8sl9RBR0smNgIy7z8EgCy84fL82/nYE/k0AvMNIAfYgIER8Y8ihEU8hpvPbT2P+UST cRBw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=jzJTSAxx; 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; dara=neutral header.i=@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 71dfb90a1353d-527abd48967si4900268e0c.13.2025.04.15.12.35.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Apr 2025 12:35:33 -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=jzJTSAxx; 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; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id AFC2982BA7; Tue, 15 Apr 2025 21:34:58 +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="jzJTSAxx"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B218682B92; Tue, 15 Apr 2025 21:34:56 +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,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450: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 63E5182B4A for ; Tue, 15 Apr 2025 21:34:54 +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=jerome.forissier@linaro.org Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-39c0e0bc733so5168484f8f.1 for ; Tue, 15 Apr 2025 12:34:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1744745693; x=1745350493; 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=g48XLGI3oYJiC+FEguq2qZRcjoaBxDTm/BABIiCSeQY=; b=jzJTSAxxVDpg5ndWE3PxZPn5f5SiMI8exzQr7h3oCJcbQu33AuL8NhvNmqmHhv/f67 vs5emf/5rjem/LnzCoHM9tuwY6y8sk5ahKxbwXR9hVWSWJw3p6j4UX9Eh/1YkrglSHzO hJSG4sqakslomfD1C2KMQuvrcoUXUR0k0SSoUcjXPQ0npbk4bw3cj4EySmrM371BKJh0 9AEkUp34Y2XBrzYvKfBXxQNLBmE+zWpFHgIYqXNNQJeW+ThDjH+5LifiorZ9EiG/kl9c uMbeKo+zsYAnAJyrXFft76x7sDlP4h0ihUUZ1hIgb1yK2KfA6vi95clO6FTzdGT3PTV0 bFwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744745693; x=1745350493; 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=g48XLGI3oYJiC+FEguq2qZRcjoaBxDTm/BABIiCSeQY=; b=tkJAP4ifmwitoIEsNg5fGMHWkbnSHTjAoVfdbkxJHZ86I+qcm4Orv6101wnrPd0bLe /FTPEbTCEJRQ0V1YybhXWQxmtMF6KnDa601t0cST1J88YRhIlPoXueo4f10Np1u+Ir84 3RXFoZgcF18/TKyIoIak/H7CqnK/VAMFagWXe1/uugFg0irWb8CxL0v+KBdO6Gt1mup3 ZdRopD4jbWKGbnutIgqzChL7IJY2nSTkWBxF62Dxsmugnv+lxOFZgfMNOmQKBqiJi3gD Ec1ZWOVA8wyJBbWJr8xbssm5q24Xi/IlRJCnotsQNWcPrbrJL45yeXDFSeFkVfc/RNv5 p6JA== X-Gm-Message-State: AOJu0YxJc5jyqlcNacXsvzVWgdqmNHRnNqYj2lsWH9b+0qK+SgoTNXMn 2waL6C+QzjN7mgvuA5b6hjTRcZJfCOQ/smDdLfvPRL8TQk2XgLcmJeefqidQMT6+6+S+O6noTjE 3E0k= X-Gm-Gg: ASbGncsa1mSv6C3pmVkV1IygpEjr4eNuEYA4V/fS41+R5h+xxK+87sqwpvRvykXeniB QU7o0Eb8s2aw+O73mMNygBqP87f4yp6waQ/WSnbrDIEx5f59sAap4becg2kWqFcBkmfKF6txiWs K6u9xnGNvXr8MgdvoQQYJZS5GlgiVWUBG8m/dKuPpmbmdOGk3VAV1TXkmfH6xup6hD/YoCZQ4s6 m69286mQ2wTDY5grrXOcSW7DdAgCqRctyo88D9RSH15EjjrsfKW2C7cNo0oD3Z7vO4ZWvd2rUnF iMwsC2SVLiry6Ee6Ya7D4BngMzw8AHzcmKb66vOTsFZz+GNr X-Received: by 2002:a05:6000:22c2:b0:39e:cbde:8889 with SMTP id ffacd0b85a97d-39ee27295fdmr652202f8f.6.1744745693582; Tue, 15 Apr 2025 12:34:53 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:6ebc:6ce:770e:95f9]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39eaf43d046sm15520484f8f.63.2025.04.15.12.34.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Apr 2025 12:34:53 -0700 (PDT) From: Jerome Forissier To: U-Boot mailing list Cc: Jerome Forissier , Sughosh Ganu , Ilias Apalodimas , Joe Hershberger , Ramon Fried , Tom Rini Subject: [PATCH v4 4/4] net-lwip: tftp: add LMB and buffer checks Date: Tue, 15 Apr 2025 21:34:29 +0200 Message-ID: <20250415193436.1240804-5-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250415193436.1240804-1-jerome.forissier@linaro.org> References: <20250415193436.1240804-1-jerome.forissier@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 Legacy NET tftp invokes a store_block() function which performs buffer validation (LMB, address wrapping). Do the same with NET_LWIP. Signed-off-by: Jerome Forissier Suggested-by: Sughosh Ganu --- (no changes since v3) Changes in v3: - store_block(): add Sphinx-like documentation net/lwip/tftp.c | 56 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/net/lwip/tftp.c b/net/lwip/tftp.c index 123d66b5dba..c7f1420ca0f 100644 --- a/net/lwip/tftp.c +++ b/net/lwip/tftp.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,47 @@ struct tftp_ctx { enum done_state done; }; +/** + * store_block() - copy received data + * + * This function is called by the receive callback to copy a block of data + * into its final location (ctx->daddr). Before doing so, it checks if the copy + * is allowed. + * + * @ctx: the context for the current transfer + * @src: the data received from the TCP stack + * @len: the length of the data + */ +static int store_block(struct tftp_ctx *ctx, void *src, u16_t len) +{ + ulong store_addr = ctx->daddr; + void *ptr; + + if (CONFIG_IS_ENABLED(LMB)) { + if (store_addr + len < store_addr || + lmb_read_check(store_addr, len)) { + puts("\nTFTP error: "); + puts("trying to overwrite reserved memory...\n"); + return -1; + } + } + + ptr = map_sysmem(store_addr, len); + memcpy(ptr, src, len); + unmap_sysmem(ptr); + + ctx->daddr += len; + ctx->size += len; + ctx->block_count++; + if (ctx->block_count % 10 == 0) { + putc('#'); + if (ctx->block_count % (65 * 10) == 0) + puts("\n\t "); + } + + return 0; +} + static void *tftp_open(const char *fname, const char *mode, u8_t is_write) { return NULL; @@ -71,17 +113,9 @@ static int tftp_write(void *handle, struct pbuf *p) struct tftp_ctx *ctx = handle; struct pbuf *q; - for (q = p; q; q = q->next) { - memcpy((void *)ctx->daddr, q->payload, q->len); - ctx->daddr += q->len; - ctx->size += q->len; - ctx->block_count++; - if (ctx->block_count % 10 == 0) { - putc('#'); - if (ctx->block_count % (65 * 10) == 0) - puts("\n\t "); - } - } + for (q = p; q; q = q->next) + if (store_block(ctx, q->payload, q->len) < 0) + return -1; return 0; }