* [PATCH v2] fpga: microchip-spi: add bounds checks in mpf_ops_parse_header() [not found] <20260402125446.3776153-3-sebasjosue84@gmail.com> @ 2026-04-02 15:37 ` Sebastian Alba Vives 2026-04-02 16:03 ` Greg KH ` (2 more replies) 2026-04-02 16:23 ` [PATCH v3] " Sebastian Alba Vives 1 sibling, 3 replies; 6+ messages in thread From: Sebastian Alba Vives @ 2026-04-02 15:37 UTC (permalink / raw) To: linux-fpga Cc: yilun.xu, conor.dooley, mdf, linux-kernel, Sebastian Josue Alba Vives, stable From: Sebastian Josue Alba Vives <sebasjosue84@gmail.com> mpf_ops_parse_header() reads several fields from the bitstream file and uses them as offsets and sizes without validating them against the buffer size, leading to multiple out-of-bounds read vulnerabilities: 1. There is no check that count is large enough to read header_size at MPF_HEADER_SIZE_OFFSET (24). Add a minimum count check. 2. When header_size (u8 from file) is 0, the expression *(buf + header_size - 1) reads one byte before the buffer. Return -EINVAL since retrying with a larger buffer cannot fix a zero header_size. 3. In the block lookup loop, block_id_offset and block_start_offset advance by MPF_LOOKUP_TABLE_RECORD_SIZE (9) each iteration with blocks_num (u8) controlling the count. With a small buffer, these offsets exceed count, causing OOB reads via get_unaligned_le32(). Return -EAGAIN since a larger buffer may resolve the issue. 4. components_size_start (from file) and component_size_byte_num (derived from components_num, u16 from file) are used as offsets into buf without validation, allowing arbitrary OOB reads. Add bounds checks for all four cases. Fixes: 5f8d4a9008307 ("fpga: microchip-spi: add Microchip MPF FPGA manager") Cc: stable@vger.kernel.org Signed-off-by: Sebastian Alba Vives <sebasjosue84@gmail.com> --- drivers/fpga/microchip-spi.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/drivers/fpga/microchip-spi.c b/drivers/fpga/microchip-spi.c index 6134cea..00fa2d6 100644 --- a/drivers/fpga/microchip-spi.c +++ b/drivers/fpga/microchip-spi.c @@ -115,7 +115,13 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, return -EINVAL; } + if (count < MPF_HEADER_SIZE_OFFSET + 1) + return -EINVAL; + header_size = *(buf + MPF_HEADER_SIZE_OFFSET); + if (!header_size) + return -EINVAL; + if (header_size > count) { info->header_size = header_size; return -EAGAIN; @@ -139,6 +145,10 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, bitstream_start = 0; while (blocks_num--) { + if (block_id_offset >= count || + block_start_offset + sizeof(u32) > count) + return -EAGAIN; + block_id = *(buf + block_id_offset); block_start = get_unaligned_le32(buf + block_start_offset); @@ -183,6 +193,10 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, component_size_byte_off = (i * MPF_BITS_PER_COMPONENT_SIZE) % BITS_PER_BYTE; + if (components_size_start + component_size_byte_num + + sizeof(u32) > count) + return -EINVAL; + component_size = get_unaligned_le32(buf + components_size_start + component_size_byte_num); -- 2.43.0 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2] fpga: microchip-spi: add bounds checks in mpf_ops_parse_header() 2026-04-02 15:37 ` [PATCH v2] fpga: microchip-spi: add bounds checks in mpf_ops_parse_header() Sebastian Alba Vives @ 2026-04-02 16:03 ` Greg KH 2026-04-02 16:06 ` Greg KH 2026-04-02 16:16 ` Conor Dooley 2 siblings, 0 replies; 6+ messages in thread From: Greg KH @ 2026-04-02 16:03 UTC (permalink / raw) To: Sebastian Alba Vives Cc: linux-fpga, yilun.xu, conor.dooley, mdf, linux-kernel, stable On Thu, Apr 02, 2026 at 09:37:52AM -0600, Sebastian Alba Vives wrote: > From: Sebastian Josue Alba Vives <sebasjosue84@gmail.com> > > mpf_ops_parse_header() reads several fields from the bitstream file > and uses them as offsets and sizes without validating them against the > buffer size, leading to multiple out-of-bounds read vulnerabilities: > > 1. There is no check that count is large enough to read header_size > at MPF_HEADER_SIZE_OFFSET (24). Add a minimum count check. > > 2. When header_size (u8 from file) is 0, the expression > *(buf + header_size - 1) reads one byte before the buffer. > Return -EINVAL since retrying with a larger buffer cannot fix > a zero header_size. > > 3. In the block lookup loop, block_id_offset and block_start_offset > advance by MPF_LOOKUP_TABLE_RECORD_SIZE (9) each iteration with > blocks_num (u8) controlling the count. With a small buffer, these > offsets exceed count, causing OOB reads via get_unaligned_le32(). > Return -EAGAIN since a larger buffer may resolve the issue. > > 4. components_size_start (from file) and component_size_byte_num > (derived from components_num, u16 from file) are used as offsets > into buf without validation, allowing arbitrary OOB reads. > > Add bounds checks for all four cases. > > Fixes: 5f8d4a9008307 ("fpga: microchip-spi: add Microchip MPF FPGA manager") > Cc: stable@vger.kernel.org > Signed-off-by: Sebastian Alba Vives <sebasjosue84@gmail.com> > --- > drivers/fpga/microchip-spi.c | 14 ++++++++++++++ > 1 file changed, 14 insertions(+) > > diff --git a/drivers/fpga/microchip-spi.c b/drivers/fpga/microchip-spi.c > index 6134cea..00fa2d6 100644 > --- a/drivers/fpga/microchip-spi.c > +++ b/drivers/fpga/microchip-spi.c > @@ -115,7 +115,13 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, > return -EINVAL; > } > > + if (count < MPF_HEADER_SIZE_OFFSET + 1) > + return -EINVAL; > + > header_size = *(buf + MPF_HEADER_SIZE_OFFSET); > + if (!header_size) > + return -EINVAL; > + > if (header_size > count) { > info->header_size = header_size; > return -EAGAIN; > @@ -139,6 +145,10 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, > bitstream_start = 0; > > while (blocks_num--) { > + if (block_id_offset >= count || > + block_start_offset + sizeof(u32) > count) > + return -EAGAIN; > + > block_id = *(buf + block_id_offset); > block_start = get_unaligned_le32(buf + block_start_offset); > > @@ -183,6 +193,10 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, > component_size_byte_off = > (i * MPF_BITS_PER_COMPONENT_SIZE) % BITS_PER_BYTE; > > + if (components_size_start + component_size_byte_num + > + sizeof(u32) > count) > + return -EINVAL; > + > component_size = get_unaligned_le32(buf + > components_size_start + > component_size_byte_num); > -- > 2.43.0 > > Hi, This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree. You are receiving this message because of the following common error(s) as indicated below: - This looks like a new version of a previously submitted patch, but you did not list below the --- line any changes from the previous version. Please read the section entitled "The canonical patch format" in the kernel file, Documentation/process/submitting-patches.rst for what needs to be done here to properly describe this. If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers. thanks, greg k-h's patch email bot ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] fpga: microchip-spi: add bounds checks in mpf_ops_parse_header() 2026-04-02 15:37 ` [PATCH v2] fpga: microchip-spi: add bounds checks in mpf_ops_parse_header() Sebastian Alba Vives 2026-04-02 16:03 ` Greg KH @ 2026-04-02 16:06 ` Greg KH 2026-04-02 16:16 ` Conor Dooley 2 siblings, 0 replies; 6+ messages in thread From: Greg KH @ 2026-04-02 16:06 UTC (permalink / raw) To: Sebastian Alba Vives Cc: linux-fpga, yilun.xu, conor.dooley, mdf, linux-kernel, stable On Thu, Apr 02, 2026 at 09:37:52AM -0600, Sebastian Alba Vives wrote: > From: Sebastian Josue Alba Vives <sebasjosue84@gmail.com> > > mpf_ops_parse_header() reads several fields from the bitstream file > and uses them as offsets and sizes without validating them against the > buffer size, leading to multiple out-of-bounds read vulnerabilities: Note, bitstream files are "trusted" so this really isn't a "vulnerability" just a "someone with hardware permissions sent a stupid file to the kernel" issue :) > 1. There is no check that count is large enough to read header_size > at MPF_HEADER_SIZE_OFFSET (24). Add a minimum count check. > > 2. When header_size (u8 from file) is 0, the expression > *(buf + header_size - 1) reads one byte before the buffer. > Return -EINVAL since retrying with a larger buffer cannot fix > a zero header_size. > > 3. In the block lookup loop, block_id_offset and block_start_offset > advance by MPF_LOOKUP_TABLE_RECORD_SIZE (9) each iteration with > blocks_num (u8) controlling the count. With a small buffer, these > offsets exceed count, causing OOB reads via get_unaligned_le32(). > Return -EAGAIN since a larger buffer may resolve the issue. > > 4. components_size_start (from file) and component_size_byte_num > (derived from components_num, u16 from file) are used as offsets > into buf without validation, allowing arbitrary OOB reads. > > Add bounds checks for all four cases. > > Fixes: 5f8d4a9008307 ("fpga: microchip-spi: add Microchip MPF FPGA manager") > Cc: stable@vger.kernel.org > Signed-off-by: Sebastian Alba Vives <sebasjosue84@gmail.com> > --- > drivers/fpga/microchip-spi.c | 14 ++++++++++++++ > 1 file changed, 14 insertions(+) Please look at these review comments: https://sashiko.dev/#/patchset/20260402153752.3793055-1-sebasjosue84%40gmail.com this driver is "odd", but some of those comments seem correct. thansk, greg k-h ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] fpga: microchip-spi: add bounds checks in mpf_ops_parse_header() 2026-04-02 15:37 ` [PATCH v2] fpga: microchip-spi: add bounds checks in mpf_ops_parse_header() Sebastian Alba Vives 2026-04-02 16:03 ` Greg KH 2026-04-02 16:06 ` Greg KH @ 2026-04-02 16:16 ` Conor Dooley 2 siblings, 0 replies; 6+ messages in thread From: Conor Dooley @ 2026-04-02 16:16 UTC (permalink / raw) To: Sebastian Alba Vives Cc: linux-fpga, yilun.xu, conor.dooley, mdf, linux-kernel, stable [-- Attachment #1: Type: text/plain, Size: 3126 bytes --] On Thu, Apr 02, 2026 at 09:37:52AM -0600, Sebastian Alba Vives wrote: > From: Sebastian Josue Alba Vives <sebasjosue84@gmail.com> > > mpf_ops_parse_header() reads several fields from the bitstream file > and uses them as offsets and sizes without validating them against the > buffer size, leading to multiple out-of-bounds read vulnerabilities: > > 1. There is no check that count is large enough to read header_size > at MPF_HEADER_SIZE_OFFSET (24). Add a minimum count check. > > 2. When header_size (u8 from file) is 0, the expression > *(buf + header_size - 1) reads one byte before the buffer. > Return -EINVAL since retrying with a larger buffer cannot fix > a zero header_size. > > 3. In the block lookup loop, block_id_offset and block_start_offset > advance by MPF_LOOKUP_TABLE_RECORD_SIZE (9) each iteration with > blocks_num (u8) controlling the count. With a small buffer, these > offsets exceed count, causing OOB reads via get_unaligned_le32(). > Return -EAGAIN since a larger buffer may resolve the issue. > > 4. components_size_start (from file) and component_size_byte_num > (derived from components_num, u16 from file) are used as offsets > into buf without validation, allowing arbitrary OOB reads. > > Add bounds checks for all four cases. > > Fixes: 5f8d4a9008307 ("fpga: microchip-spi: add Microchip MPF FPGA manager") > Cc: stable@vger.kernel.org > Signed-off-by: Sebastian Alba Vives <sebasjosue84@gmail.com> > --- > drivers/fpga/microchip-spi.c | 14 ++++++++++++++ > 1 file changed, 14 insertions(+) > > diff --git a/drivers/fpga/microchip-spi.c b/drivers/fpga/microchip-spi.c > index 6134cea..00fa2d6 100644 > --- a/drivers/fpga/microchip-spi.c > +++ b/drivers/fpga/microchip-spi.c > @@ -115,7 +115,13 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, > return -EINVAL; > } > > + if (count < MPF_HEADER_SIZE_OFFSET + 1) > + return -EINVAL; > + > header_size = *(buf + MPF_HEADER_SIZE_OFFSET); > + if (!header_size) > + return -EINVAL; > + > if (header_size > count) { > info->header_size = header_size; > return -EAGAIN; > @@ -139,6 +145,10 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, > bitstream_start = 0; > > while (blocks_num--) { > + if (block_id_offset >= count || > + block_start_offset + sizeof(u32) > count) > + return -EAGAIN; > + > block_id = *(buf + block_id_offset); > block_start = get_unaligned_le32(buf + block_start_offset); > > @@ -183,6 +193,10 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, > component_size_byte_off = > (i * MPF_BITS_PER_COMPONENT_SIZE) % BITS_PER_BYTE; > > + if (components_size_start + component_size_byte_num + > + sizeof(u32) > count) > + return -EINVAL; I didn't mention it explicitly, but it kinda follows from the other comment, do we not just want to ask for a bigger buffer here too? > + > component_size = get_unaligned_le32(buf + > components_size_start + > component_size_byte_num); > -- > 2.43.0 > [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 228 bytes --] ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v3] fpga: microchip-spi: add bounds checks in mpf_ops_parse_header() [not found] <20260402125446.3776153-3-sebasjosue84@gmail.com> 2026-04-02 15:37 ` [PATCH v2] fpga: microchip-spi: add bounds checks in mpf_ops_parse_header() Sebastian Alba Vives @ 2026-04-02 16:23 ` Sebastian Alba Vives 2026-04-07 10:44 ` Xu Yilun 1 sibling, 1 reply; 6+ messages in thread From: Sebastian Alba Vives @ 2026-04-02 16:23 UTC (permalink / raw) To: linux-fpga Cc: yilun.xu, conor.dooley, mdf, linux-kernel, Sebastian Josue Alba Vives, stable From: Sebastian Josue Alba Vives <sebasjosue84@gmail.com> mpf_ops_parse_header() reads several fields from the bitstream file and uses them as offsets and sizes without validating them against the buffer size, leading to multiple out-of-bounds read vulnerabilities: 1. There is no check that count is large enough to read header_size at MPF_HEADER_SIZE_OFFSET (24). Add a minimum count check. 2. When header_size (u8 from file) is 0, the expression *(buf + header_size - 1) reads one byte before the buffer. Return -EINVAL since retrying with a larger buffer cannot fix a zero header_size. 3. In the block lookup loop, block_id_offset and block_start_offset advance by MPF_LOOKUP_TABLE_RECORD_SIZE (9) each iteration with blocks_num (u8) controlling the count. With a small buffer, these offsets exceed count, causing OOB reads via get_unaligned_le32(). Return -EAGAIN with updated header_size so the retry mechanism can request a larger buffer. 4. components_num is read from MPF_DATA_SIZE_OFFSET without checking that the offset is within bounds. Add a bounds check. 5. components_size_start (from file) and component_size_byte_num (derived from components_num, u16 from file) are used as offsets into buf without validation. On 32-bit architectures, their sum could overflow, bypassing the bounds check. Add an overflow check before the bounds comparison. Add bounds checks for all five cases. Fixes: 5f8d4a9008307 ("fpga: microchip-spi: add Microchip MPF FPGA manager") Cc: stable@vger.kernel.org Signed-off-by: Sebastian Alba Vives <sebasjosue84@gmail.com> --- Changes in v3: - Add overflow check for 32-bit architectures in component size loop - Add bounds check for MPF_DATA_SIZE_OFFSET read - Update info->header_size before returning -EAGAIN in block loop so the retry mechanism can request a larger buffer Changes in v2: - Return -EINVAL instead of -EAGAIN for header_size == 0 - Return -EAGAIN instead of -EINVAL in block lookup loop - Add count check before reading at MPF_HEADER_SIZE_OFFSET drivers/fpga/microchip-spi.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/drivers/fpga/microchip-spi.c b/drivers/fpga/microchip-spi.c index 6134cea..10be986 100644 --- a/drivers/fpga/microchip-spi.c +++ b/drivers/fpga/microchip-spi.c @@ -115,7 +115,13 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, return -EINVAL; } + if (count < MPF_HEADER_SIZE_OFFSET + 1) + return -EINVAL; + header_size = *(buf + MPF_HEADER_SIZE_OFFSET); + if (!header_size) + return -EINVAL; + if (header_size > count) { info->header_size = header_size; return -EAGAIN; @@ -139,6 +145,12 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, bitstream_start = 0; while (blocks_num--) { + if (block_id_offset >= count || + block_start_offset + sizeof(u32) > count) { + info->header_size = block_start_offset + sizeof(u32); + return -EAGAIN; + } + block_id = *(buf + block_id_offset); block_start = get_unaligned_le32(buf + block_start_offset); @@ -175,6 +187,9 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, * to each other. Image header should be extended by now up to where * actual bitstream starts, so no need for overflow check anymore. */ + if (MPF_DATA_SIZE_OFFSET + sizeof(u16) > count) + return -EINVAL; + components_num = get_unaligned_le16(buf + MPF_DATA_SIZE_OFFSET); for (i = 0; i < components_num; i++) { @@ -183,6 +198,11 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, component_size_byte_off = (i * MPF_BITS_PER_COMPONENT_SIZE) % BITS_PER_BYTE; + if (components_size_start + component_size_byte_num < components_size_start || + components_size_start + component_size_byte_num + + sizeof(u32) > count) + return -EINVAL; + component_size = get_unaligned_le32(buf + components_size_start + component_size_byte_num); -- 2.43.0 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v3] fpga: microchip-spi: add bounds checks in mpf_ops_parse_header() 2026-04-02 16:23 ` [PATCH v3] " Sebastian Alba Vives @ 2026-04-07 10:44 ` Xu Yilun 0 siblings, 0 replies; 6+ messages in thread From: Xu Yilun @ 2026-04-07 10:44 UTC (permalink / raw) To: Sebastian Alba Vives; +Cc: linux-fpga, conor.dooley, mdf, linux-kernel, stable > @@ -139,6 +145,12 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, > bitstream_start = 0; > > while (blocks_num--) { > + if (block_id_offset >= count || > + block_start_offset + sizeof(u32) > count) { > + info->header_size = block_start_offset + sizeof(u32); > + return -EAGAIN; > + } > + The image header has already been extended up to all look-up table for blocks, is it? header_size += blocks_num * MPF_LOOKUP_TABLE_RECORD_SIZE; if (header_size > count) { info->header_size = header_size; return -EAGAIN; } > block_id = *(buf + block_id_offset); > block_start = get_unaligned_le32(buf + block_start_offset); > > @@ -175,6 +187,9 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, > * to each other. Image header should be extended by now up to where > * actual bitstream starts, so no need for overflow check anymore. > */ > + if (MPF_DATA_SIZE_OFFSET + sizeof(u16) > count) > + return -EINVAL; > + Do you notice the comments above? IIUC it says all these header info should be before actual bitstream starts, if we could ensure this, we don't need to check the addresses inside the header byte by byte. I think it is important we understand the structure of the image file first then meaningfully check the boundaries chunk by chunk, rather than byte by byte, which makes code unreadable. Thanks, Yilun > components_num = get_unaligned_le16(buf + MPF_DATA_SIZE_OFFSET); > > for (i = 0; i < components_num; i++) { > @@ -183,6 +198,11 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, > component_size_byte_off = > (i * MPF_BITS_PER_COMPONENT_SIZE) % BITS_PER_BYTE; > > + if (components_size_start + component_size_byte_num < components_size_start || > + components_size_start + component_size_byte_num + > + sizeof(u32) > count) > + return -EINVAL; > + > component_size = get_unaligned_le32(buf + > components_size_start + > component_size_byte_num); > -- > 2.43.0 > ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-04-07 11:05 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20260402125446.3776153-3-sebasjosue84@gmail.com>
2026-04-02 15:37 ` [PATCH v2] fpga: microchip-spi: add bounds checks in mpf_ops_parse_header() Sebastian Alba Vives
2026-04-02 16:03 ` Greg KH
2026-04-02 16:06 ` Greg KH
2026-04-02 16:16 ` Conor Dooley
2026-04-02 16:23 ` [PATCH v3] " Sebastian Alba Vives
2026-04-07 10:44 ` Xu Yilun
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).