From patchwork Thu Apr 10 15:14:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 879757 Delivered-To: patch@linaro.org Received: by 2002:a5d:4882:0:b0:38f:210b:807b with SMTP id g2csp305486wrq; Thu, 10 Apr 2025 08:16:46 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVIPJgubqzTVT41goI403wrZB/OQxu7kg9aP2C4MIn82Z8qMQhGMTQqf9c+dwWPkqGJ9qGZnQ==@linaro.org X-Google-Smtp-Source: AGHT+IGxzmZob9wqYvs4/jhHxQQlgk0oyQjgBFJLm2i1yOmO38dSayuCumq/R2rlf8Kn0saRME3k X-Received: by 2002:a05:6102:3a10:b0:4af:deaf:f891 with SMTP id ada2fe7eead31-4c9d347d9c5mr2665692137.4.1744298205852; Thu, 10 Apr 2025 08:16:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1744298205; cv=none; d=google.com; s=arc-20240605; b=AVbxETHBVWQSplTBjcUIt0IPA2r6hX/xUKYnUtu4FudNY9IQqhQUaRBkfB929UhL7v yG9nsZUAt7iXN9OJoL+CtR/E8VdEVU6fR1sBHYl0aXc/PVvuAyY4GtPD5rPMQ9+gfLZ4 L5YX4sSJa4bgYgFqVbtR4x57kuRxDLmZIiPkWuvO96q7/n+TCh5mcSazFuv78kSk53yn tYzc8EWw2Nc7PASNjLwKD3Zd38+lxwb35E3lQhd/H5elwFGmxmYRCLZi4ctvAZhEBrEk R0NDO60GCvz3RH6I0VNFCM2dhuyv9o7evQ78kbEbd+JzdZsxmWTuBSAclXnV1FXPC+m1 WKUQ== 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=GiQhEdzxFC/SizZUKx6GlyeI6XqT1jcQhvAy1drEgA8=; fh=PQg1uaxoFkVPH7GvYt2sPX/rmqpadkXu1gxBQgKczic=; b=DSZadR8CX60K6pZd9cKG4Qoza6mGQu6O+/6huOANa7wuitQ4U5Pb8deGe3oLEAk6zG imoN9l6elnUrMBbLpWN+sv+1lGqihlx449w59O25qrflr8/REgmP9e+MfnF8GhTL9/yt 7ja09/vf8Hdtx79ceznVvsU3obdTd3wFBYWJ0k3wrtnB96sS9gOQfvmjrcB3XQkW9tPr P5b64DNNm0VtrWplTaNzyADop0WbYWL+yTlT2zpwaYoX41/ZffY621T5gBV53Pgef8wj cQ7fxAiro703KuO8zYXsU3ORLL9IzFeGtQV57R3kC5rvMgCJy4JVXH/zhNvyc/Rp0lez rU8g==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=qI47O1w4; 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 ada2fe7eead31-4c9c988f020si1107928137.313.2025.04.10.08.16.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Apr 2025 08:16:45 -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=qI47O1w4; 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 09F1083D3C; Thu, 10 Apr 2025 17:16:21 +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="qI47O1w4"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 102918192A; Thu, 10 Apr 2025 17:16:19 +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-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) (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 DDEBC83B8F for ; Thu, 10 Apr 2025 17:16:16 +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-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-43edecbfb94so10460105e9.1 for ; Thu, 10 Apr 2025 08:16:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1744298176; x=1744902976; 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=GiQhEdzxFC/SizZUKx6GlyeI6XqT1jcQhvAy1drEgA8=; b=qI47O1w4IXxfzshbZ32bylguhhTu9auFbqsTm9CNhcYeSVWcFVK6JX0psyLpvimBky BiqjzghFuZByh0K0BcbXPG1WfHxnzyQqMjZ3UFUj71NRgq8lYWgfsaBlV7s7SFaS69CR Z9A0YF8uc0iEx4KKwn5/IgdRiBT9Uo3J1T3o2N1er+f/6D/qoeicPoefDgvHdQj7BGjm ZxcLdiP1QC2xxj0Nlak5V3JfWGzlSVFC3uSQc8gZKA/wFJJfKWlAFt5y0TnwCltVjNfo 7zSwwrUJQWpXb4dMhUA++NqFkA/8l3pLJ0ysyejYNLfx0Z58Z9M1mC+xkjlNgbjYkbn7 /w+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744298176; x=1744902976; 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=GiQhEdzxFC/SizZUKx6GlyeI6XqT1jcQhvAy1drEgA8=; b=u/AEvrOxdZnrikmy8eF7KrFnTy8uc22ZspSMTCFeYDkJZXepjz2qHbveCwZ4QkeeFx 3/4jZPfSUXNd7U/eJAVuApOXULHet5mBI6VtYi/cKVXHVEpZHAEl3tmQAZsU7LLaiVIo Pwt1Hj+SjTzD4elFnZr5g8yaLbPzmutnHDRK8BqIrOycBv4dJ56/4g0JMJHOhePvrDoN rxFng37nuvwaQL+jV9laFUDgEHLF1zr6Cw/cVc2WgeGvCPZx4qS5Zq7bxwGdNshhyVpq qt4ENVqHsYxy/51YEAXGNbWdfUI8V1/VnGNfKE9l7/o/JE3YN/a3l6/2MwfWPFfAHV8R xa5Q== X-Gm-Message-State: AOJu0YyEIojvhvoYdp7XaI9o/hn+KE9fztu6FmdFMCCFwMudhryrBCgc brnK+jfAMME/k4eZ7VDtgA+/3WnlJtdPt3mkmxaxdhZyoZSHDizvuhM473+wDbQazM3BuNogJt7 BvAE= X-Gm-Gg: ASbGncuZRhjG6MQT3cZwMN1DxeGF4W1BPknuPS3GjqEZxJq/VJHfQierMm6tqXaVDHk 5dd3HMZsayprJETrKONh/sK4AB5sUNupgIoQEQ1lDDC79teTjbbUjQDLD3LcZNF0QiRnrSsf8bj dB1hXDlCFrmLk0W5gfeu+3e1bknN6yE38lw4AKC+c9etgzMm2drTnXG+BY6RKgrDEwl3YA/KZWp hJG+mLviZqkjQ5b3STLO5bY+jHPDRDIZWPhpYAEyTmAFIh6bmJDaSUh4Immc7DIHZF0lrooEnJ8 DWa3gvgr89/cPiR9TUSB+aGE0zo1GoMj4rdWNE8xJF+ujx397g== X-Received: by 2002:a5d:5f4b:0:b0:399:6d26:7752 with SMTP id ffacd0b85a97d-39d8fd8b4bamr2633423f8f.38.1744298176113; Thu, 10 Apr 2025 08:16:16 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:7edb:df05:a425:37cd]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43f233c824bsm54873225e9.24.2025.04.10.08.16.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Apr 2025 08:16:15 -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 v3 3/3] net-lwip: tftp: add LMB and buffer checks Date: Thu, 10 Apr 2025 17:14:54 +0200 Message-ID: <20250410151600.3370247-4-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250410151600.3370247-1-jerome.forissier@linaro.org> References: <20250410151600.3370247-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 --- Changes in v3: - store_block() [tftp]: 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; }