From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DB32244A3EF for ; Wed, 2 Sep 2026 18:23:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788373386; cv=none; b=S9Yh9zVoi85K/DY55Fq5oS1NQehBfou9/IcC7FmdFPdXBmelF738bTzJ7fYd4mx4aEJVEJU0NCOiM6dZ8gDzZD/6c/PTiOdZBqjJB925hkO4PHSUJmLvzVLRAEJEEpx2heRRWuowUvENLmUM/W2HIK8jKre74QxZHHPqatz+0KQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788373386; c=relaxed/simple; bh=5M98h9LgBz5BhAzDN3vhK3FYBzMg0TL7AH+B/YkN7wg=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JDGmuOP4sf3pz+bivpJfBXaWVb9y6hjJx9T9/T54b9ZpiV6mf2/Vg5CUJ4vaTP5VyahkkEBz31WelIY0EZM2kd9TYW4ykeNkamYlo5/76ZdxeSIz22zv8ZAHniAS7RedNcEJHpAg1Vvz4cJtOYchnDH4N9yRNye9OFXWYUAzx/4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=en27mFQO; arc=none smtp.client-ip=209.85.128.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="en27mFQO" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-4921eed3fa2so11730895e9.0 for ; Wed, 02 Sep 2026 11:23:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788373383; x=1788978183; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=bghS+F6/3V7iNdu+BDTLXG9swQB37KksL6iykdp8MRk=; b=en27mFQOVSiqEAmWKqzBlDcoWC/E6utr/EHAk3XmBgZVp4kbmbnmL6fZS40AvVVAaZ +/XJacK8Tc8IUKxCa8v1KS31uTz4tUmSnL6sVZqlD/hTZC2+JjNmxSRe9QW5eyR4IuI8 N9FzV38Mrs7iV8XJkGdoe999n1ruSEl196L+2RRJ9FIqY3MMaLagkKK2L0hi36c4oap/ rwFn+Mi2JupjWJmXYUhu+8Q+6IIBNGiQb1OCzBCucaAQ5fd5nbV7NqlQOyRhJY9y+aFf FD1N5IGzg/kd6uxPN7x+hy6K5zHJpcVO/95dK30CTxVVNKlZmKsVvk+jRFW4I/m8x/5t xvNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788373383; x=1788978183; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=bghS+F6/3V7iNdu+BDTLXG9swQB37KksL6iykdp8MRk=; b=KCuxbpvTtnOh4IMeOZA/kBBSnRDkb/BRPa7NCEXKul6K5qi/hR8D14gA0IGQJLVavm JAUXQE6Oq1t4qW18OoGZOs5UzoxHaHAFGYYpEku5Sq15xcSGwKnX/n66k4NV/t0QPm6A 5t4v7hYGZ8ROzy04AvyITYqpTsS3kZIFF1bQG7zqBusiuq8hkz+TxzkalPsAEwna+Kfe ab0jBFgRy1eGElujTWGpArL6DiQcCPu8JSGT3VkzfcuF2/FvCwTkaXjA0f1AIDGtgaMY udtneHuAgFbHNDrdfWneQl6FRvPjOmYLgBVjDnR1Iam7CTOz+N6MMrqBNaiWU4g9Hth9 gMlg== X-Forwarded-Encrypted: i=1; AKwUvBzinlJiBCqJhsnqxwwoJLGvv2OJHh8r05C/EHUqvgcd6D6xEC2fk93w3WySltdZhZBAWj87F8pQ9VI=@vger.kernel.org X-Gm-Message-State: AFuF++mU9jI9f/dUbrchnn2O3882l3YsLvtMjpuk3cTrHPnogvjipx8g 7eM0ad0wGySN+Ss8CThekWpql0sAdhfuLqCXgecCNgQT4JSunEEusxKp X-Gm-Gg: AYBFou2t5DMzisKHlUjdodjklkNIklILKW5L2H5gWqMEPBIglDj8V1wFs3IctU0KfPV nDSoim32ax1HB3s3tUCSheDYdhz9b2YItdXfJRHNozt9yZoxzaCOyXtdk9bnDmbKqUt84/rvchQ ZFa7eN5FM/FR9l5lJTxSK87j0qbxg8w94WbDjsn+83562xDxYVlO6CjrJ09dqzP+hd3MX9XkDyG 2rlOAuzASr7MCUMnMsHqNftANbKgYPYB0p6ir4Y69gNXnUxoVfwe5S/miKTqQOskwPS0K4UcT+e sga/WcXJiV7nGHc2NHwsMqaIB40a6XdXMxWxb+OOGPLY/1JFbCjqOs+sQ5TBk8ry8geenZsxlUW BrbqyZxlNDJgA7gkALeezmJZX1D4sY2DTCSFmUKgGfrBUDhw2PAWSaEmNOTn9FdNqD1AdWHG+ly idgNuI4oXqAcei07s8PqkU3vAUTh0+/QKMB7Kv/PYZpU7kvpYsCv7zAoD4URELrkNBOskNGvaC9 iUwmk/zbB8vkDbXrTxr4W4UAg== X-Received: by 2002:a05:600c:8b05:b0:499:b65d:124f with SMTP id 5b1f17b1804b1-49ce5823d9dmr152107355e9.11.1788373382397; Wed, 02 Sep 2026 11:23:02 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49ce554d52esm47864385e9.3.2026.09.02.11.23.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Sep 2026 11:23:01 -0700 (PDT) Date: Wed, 2 Sep 2026 19:23:00 +0100 From: David Laight To: Vasileios Almpanis Cc: Greg Kroah-Hartman , Andrey Smirnov , David Woodhouse , "Gustavo A. R. Silva" , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH] ihex: Fix 16 bit truncation in ihex_binrec_size() Message-ID: <20260902192300.65202e38@pumpkin> In-Reply-To: <3552bfc7-ac6e-4133-95f9-69b4e96a5005@gmail.com> References: <20260902-ihex-v1-1-67fdbf8d97ff@gmail.com> <20260902163151.59ffe47f@pumpkin> <3552bfc7-ac6e-4133-95f9-69b4e96a5005@gmail.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 2 Sep 2026 18:35:28 +0200 Vasileios Almpanis wrote: > On 9/2/26 5:31 PM, David Laight wrote: > > On Wed, 02 Sep 2026 12:21:19 +0200 > > Vasileios Almpanis wrote: > > > >> ihex_binrec_size() returns uint16_t while computing be16_to_cpu(p->len) + > >> sizeof(struct ihex_binrec), so record lengths of 65530 and above wrap. > >> __ihex_next_binrec() uses the result as the offset to the next record. A > >> length of 65530 gives an advance of zero, so ihex_validate_fw() spins on > >> the same record forever. Lengths of 65531 to 65535 advance by 4 or 8 > >> instead of 65544, so a 14 byte image passes validation while its first > >> record claims 65535 bytes of payload. emi26_load_firmware() passes it to > >> emi26_writememory(), which kmemdup()s 65535 bytes out of a 14 byte buffer > >> producing the following splat: > >> > >> BUG: KASAN: vmalloc-out-of-bounds in kmemdup_noprof+0x3b/0x50 > >> Read of size 65535 at addr ffffc90000075006 by task kworker/11:1/174 > >> Workqueue: usb_hub_wq hub_event > >> Call Trace: > >> > >> kasan_check_range+0x10f/0x1e0 > >> __asan_memcpy+0x23/0x60 > >> kmemdup_noprof+0x3b/0x50 > >> emi26_writememory+0x29/0xd0 > >> emi26_probe+0x2d1/0xb64 > >> > >> Return size_t so neither the addition nor the following ALIGN() can wrap. > > The ALIGN() can't wrap, the u16 value is promoted to 'int' before anything > > is done with it. > > What it does save is the pointless '&= 0xffff' after the add. > > > > Since the result is added to a pointer it will need promoting to 'long'. > > But the compiler can assume that adding sizeof(*p) will zero the high > > bits and nothing extra is generated. > > (Not that this is a super-hot path...) > > > >> Fixes: 9fb4ab4d3dd6 ("ihex: Simplify next record offset calculation") > >> Cc: stable@vger.kernel.org > >> Signed-off-by: Vasileios Almpanis > >> --- > >> include/linux/ihex.h | 2 +- > >> 1 file changed, 1 insertion(+), 1 deletion(-) > >> > >> diff --git a/include/linux/ihex.h b/include/linux/ihex.h > >> index b824877e6d1b..0da1c4e3e693 100644 > >> --- a/include/linux/ihex.h > >> +++ b/include/linux/ihex.h > >> @@ -21,7 +21,7 @@ struct ihex_binrec { > >> uint8_t data[]; > >> } __attribute__((packed)); > >> > >> -static inline uint16_t ihex_binrec_size(const struct ihex_binrec *p) > >> +static inline size_t ihex_binrec_size(const struct ihex_binrec *p) > >> { > >> return be16_to_cpu(p->len) + sizeof(*p); > > I'd always put those in the other order - matching the memory contents. > > (But changing it would be churn.) > > > > David > Hi David, > > Thanks for taking the time to review my patch. > I had already posted a v2 before your mail arrived. > https://lore.kernel.org/all/20260902-ihex-v2-1-30bb117cfc77@gmail.com/T/#u > So the problem in this case is the fact that I misplace where the wrap > happens. The wrap happens in the function return - as you said. But there isn't a second wrap 'opportunity' in the ALIGN(). > Do you think it makes sense to send a v3 to fix the commit message? Probably doesn't matter that much. David > > Kind regards, > > Vasileios Almpanis >