From: Vasileios Almpanis <vasilisalmpanis@gmail.com>
To: David Laight <david.laight.linux@gmail.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Andrey Smirnov <andrew.smirnov@gmail.com>,
David Woodhouse <dwmw2@infradead.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
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()
Date: Wed, 2 Sep 2026 18:35:28 +0200 [thread overview]
Message-ID: <3552bfc7-ac6e-4133-95f9-69b4e96a5005@gmail.com> (raw)
In-Reply-To: <20260902163151.59ffe47f@pumpkin>
On 9/2/26 5:31 PM, David Laight wrote:
> On Wed, 02 Sep 2026 12:21:19 +0200
> Vasileios Almpanis <vasilisalmpanis@gmail.com> 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:
>> <TASK>
>> 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 <vasilisalmpanis@gmail.com>
>> ---
>> 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.
Do you think it makes sense to send a v3 to fix the commit message?
Kind regards,
Vasileios Almpanis <vasilisalmpanis@gmail.com>
next prev parent reply other threads:[~2026-09-02 16:35 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-02 10:21 Vasileios Almpanis
2026-09-02 11:06 ` Greg Kroah-Hartman
2026-09-02 12:45 ` Vasileios Almpanis
2026-09-02 12:56 ` Greg Kroah-Hartman
2026-09-02 13:46 ` Vasileios Almpanis
2026-09-02 15:31 ` David Laight
2026-09-02 16:35 ` Vasileios Almpanis [this message]
2026-09-02 18:23 ` David Laight
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3552bfc7-ac6e-4133-95f9-69b4e96a5005@gmail.com \
--to=vasilisalmpanis@gmail.com \
--cc=andrew.smirnov@gmail.com \
--cc=david.laight.linux@gmail.com \
--cc=dwmw2@infradead.org \
--cc=gregkh@linuxfoundation.org \
--cc=gustavoars@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®