mtd: physmap: physmap-bt1-rom: Fix unintentional stack access
PATCH 1/1 mtd: physmap: physmap-bt1-rom: Fix unintentional stack access
Cast &data to (char *) in order to avoid unintentionally accessing the stack.
Notice that data is of type u32, so any increment to &data will be in the order of 4-byte chunks, and this piece of code is actually intended to be a byte offset.
Addresses-Coverity-ID: 1497765 ("Out-of-bounds access")
--- drivers/mtd/maps/physmap-bt1-rom.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/maps/physmap-bt1-rom.c +1 -1
@@ -79,7 +79,7 @@ static void __xipram bt1_rom_map_copy_from(struct map_info *map,if (shift) { chunk =min_t (ssize_t,4 - shift, len); data =readl_relaxed (src - shift); -memcpy (to, &data + shift, chunk); +memcpy (to, (char *)&data + shift, chunk); src += chunk; to += chunk; len -= chunk;
Signature · 6 lines
2.27.0 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
Miquel Raynal
maintainerHi Gustavo,
"Gustavo A. R. Silva" <[email protected]> wrote on Fri, 12 Feb 2021 04:40:22 -0600:
Quoted 6 lines
Cast &data to (char *) in order to avoid unintentionally accessing the stack.
Notice that data is of type u32, so any increment to &data will be in the order of 4-byte chunks, and this piece of code is actually intended to be a byte offset.
I don't have the same reading. I don't say that Coverity report is wrong, but let's discuss this a bit further.
Given that &data is of type u32 *, you say that "&data + shift" produces increments of 4-bytes, ie. we would access "&data + 4 * shift"? Because I don't think this is the case (again, I may be wrong).
I'm sure this would be the case if we dereferenced data like data[shift] though, but in this case I don't see what this cast is fixing. Can you enlighten me?
Could the out-of-bounds warning come from the fact that shift might be bigger than the data array spread?
Quoted 13 lines
Fixes b3e79e7682e0 ("mtd: physmap: Add Baikal-T1 physically mapped ROM support")Addresses-Coverity-ID: 1497765 ("Out-of-bounds access")
Signed-off-by Gustavo A. R. Silva <[email protected]>--- drivers/mtd/maps/physmap-bt1-rom.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/mtd/maps/physmap-bt1-rom.c +0
-0
drivers/mtd/maps/physmap-bt1-rom.c static void __xipram bt1_rom_map_copy_from(struct map_info *map, · line 85
Quoted 8 lines
drivers/mtd/maps/physmap-bt1-rom.c +1
-1@@ -79,7 +79,7 @@ static void __xipram bt1_rom_map_copy_from(struct map_info *map,if (shift) { chunk =min_t (ssize_t,4 - shift, len); data =readl_relaxed (src - shift); -memcpy (to, &data + shift, chunk); +memcpy (to, (char *)&data + shift, chunk); src += chunk; to += chunk; len -= chunk;
Thanks,
Miquèl
Quoted 12 lines · Miquel Raynal
Hi Gustavo,
"Gustavo A. R. Silva" <[email protected]> wrote on Fri, 12 Feb 2021 04:40:22 -0600:
Quoted 6 lines
Cast &data to (char *) in order to avoid unintentionally accessing the stack.
Notice that data is of type u32, so any increment to &data will be in the order of 4-byte chunks, and this piece of code is actually intended to be a byte offset.
Quoted 6 lines
I don't have the same reading. I don't say that Coverity report is wrong, but let's discuss this a bit further.
Given that &data is of type u32 *, you say that "&data + shift" produces increments of 4-bytes, ie. we would access "&data + 4 * shift"? Because I don't think this is the case (again, I may be wrong).
Yep; this is pointer arithmetic. If you have an object ptr of type u32 *:
u32 *ptr;
and let's say it points to address 100. If you increment it by one:
ptr++
ptr will now point to address 104, not to 101.
Now, if instead, you first cast ptr to 'char *' and increment it by 1, then it will point to address 101.
Quoted 7 lines
I'm sure this would be the case if we dereferenced data like data[shift] though, but in this case I don't see what this cast is fixing. Can you enlighten me?
Could the out-of-bounds warning come from the fact that shift might be bigger than the data array spread?
Whats Coverity wants to say here is basically that using &data as an array might corrupt adjacent memory.
Thanks
--
Gustavo
Quoted 9 lines
Quoted 9 lines
Fixes b3e79e7682e0 ("mtd: physmap: Add Baikal-T1 physically mapped ROM support")Addresses-Coverity-ID: 1497765 ("Out-of-bounds access")
Signed-off-by Gustavo A. R. Silva <[email protected]>--- drivers/mtd/maps/physmap-bt1-rom.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 17 lines
drivers/mtd/maps/physmap-bt1-rom.c static void __xipram bt1_rom_map_copy_from(struct map_info *map, · line 85
Quoted 13 lines
drivers/mtd/maps/physmap-bt1-rom.c +1
-1@@ -79,7 +79,7 @@ static void __xipram bt1_rom_map_copy_from(struct map_info *map,if (shift) { chunk =min_t (ssize_t,4 - shift, len); data =readl_relaxed (src - shift); -memcpy (to, &data + shift, chunk); +memcpy (to, (char *)&data + shift, chunk); src += chunk; to += chunk; len -= chunk;Thanks,
Miquèl
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
Miquel Raynal
maintainerHi Gustavo,
"Gustavo A. R. Silva" <[email protected]> wrote on Fri, 12 Feb 2021 08:45:33 -0600:
Quoted 24 lines · Miquel Raynal
Quoted 12 lines
Hi Gustavo,
"Gustavo A. R. Silva" <[email protected]> wrote on Fri, 12 Feb 2021 04:40:22 -0600:
Quoted 6 lines
Cast &data to (char *) in order to avoid unintentionally accessing the stack.
Notice that data is of type u32, so any increment to &data will be in the order of 4-byte chunks, and this piece of code is actually intended to be a byte offset.
Quoted 6 lines
I don't have the same reading. I don't say that Coverity report is wrong, but let's discuss this a bit further.
Given that &data is of type u32 *, you say that "&data + shift" produces increments of 4-bytes, ie. we would access "&data + 4 * shift"? Because I don't think this is the case (again, I may be wrong).
Yep; this is pointer arithmetic. If you have an object ptr of type u32 *:
u32 *ptr;
Quoted 8 lines
and let's say it points to address 100. If you increment it by one:
ptr++
ptr will now point to address 104, not to 101.
Now, if instead, you first cast ptr to 'char *' and increment it by 1, then it will point to address 101.
Yep, I got confused with the proper addition compared to dereferencing.
Patch looks legitimate.
Thanks,
Miquèl
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
Quoted 4 lines · Miquel Raynal
Quoted 2 lines
Now, if instead, you first cast ptr to 'char *' and increment it by 1, then it will point to address 101.
Yep, I got confused with the proper addition compared to dereferencing.
No problem.
Quoted 1 line
Patch looks legitimate.
Great. :)
Thanks
--
Gustavo
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
Quoted 6 lines · Gustavo A. R. Silva
Cast &data to (char *) in order to avoid unintentionally accessing the stack.
Notice that data is of type u32, so any increment to &data will be in the order of 4-byte chunks, and this piece of code is actually intended to be a byte offset.
Thanks, one more time.)
Quoted 25 lines
Fixes b3e79e7682e0 ("mtd: physmap: Add Baikal-T1 physically mapped ROM support")Addresses-Coverity-ID: 1497765 ("Out-of-bounds access")
Signed-off-by Gustavo A. R. Silva <[email protected]>--- drivers/mtd/maps/physmap-bt1-rom.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/mtd/maps/physmap-bt1-rom.c +1
-1@@ -79,7 +79,7 @@ static void __xipram bt1_rom_map_copy_from(struct map_info *map,if (shift) { chunk =min_t (ssize_t,4 - shift, len); data =readl_relaxed (src - shift); -memcpy (to, &data + shift, chunk); +memcpy (to, (char *)&data + shift, chunk); src += chunk; to += chunk; len -= chunk;Signature · 1 line
2.27.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
Quoted 7 lines · Gustavo A. R. Silva
Cast &data to (char *) in order to avoid unintentionally accessing the stack.
Notice that data is of type u32, so any increment to &data will be in the order of 4-byte chunks, and this piece of code is actually intended to be a byte offset.
Quoted 5 lines
Fixes b3e79e7682e0 ("mtd: physmap: Add Baikal-T1 physically mapped ROM support")Addresses-Coverity-ID: 1497765 ("Out-of-bounds access")
Signed-off-by Gustavo A. R. Silva <[email protected]>Acked-by Serge Semin <[email protected]>
Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next, thanks.
Miquel