common/board_f: Respect original FDT size while relocating
PATCH 1/1 common/board_f: Respect original FDT size while relocating
From: Oleksandr Andrushchenko <[email protected]>
While relocating FDT we reserve some memory for the new FDT and set the size of the FDT with that respect. But FDT may be placed at the end of the RAM leading to memory access beyond it. Fix this by copying exact FDT size bytes, not the reserved size.
--- common/board_f.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
common/board_f.c +1 -1
@@ -670,7 +670,7 @@ static int reloc_fdt(void)if (gd->flags &GD_FLG_SKIP_RELOC )return 0 ;if (gd->new_fdt) { -memcpy (gd->new_fdt, gd->fdt_blob, gd->fdt_size); +memcpy (gd->new_fdt, gd->fdt_blob,fdt_totalsize (gd->fdt_blob)); gd->fdt_blob = gd->new_fdt; }#endif
Signature · 1 line
2.17.1
PATCH 1/1 common/board_f: Respect original FDT size while relocating
Quoted 17 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
While relocating FDT we reserve some memory for the new FDT and set the size of the FDT with that respect. But FDT may be placed at the end of the RAM leading to memory access beyond it. Fix this by copying exact FDT size bytes, not the reserved size.
Signed-off-by Oleksandr Andrushchenko <[email protected]>--- common/board_f.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)common/board_f.c +0
-0
common/board_f.c static int reloc_fdt(void) · line 676
Quoted 8 lines
common/board_f.c +1
-1@@ -670,7 +670,7 @@ static int reloc_fdt(void)if (gd->flags &GD_FLG_SKIP_RELOC )return 0 ;if (gd->new_fdt) { -memcpy (gd->new_fdt, gd->fdt_blob, gd->fdt_size); +memcpy (gd->new_fdt, gd->fdt_blob,fdt_totalsize (gd->fdt_blob)); gd->fdt_blob = gd->new_fdt; }#endif
So, I think the problem is placing the fdt so close to the end of memory and we need to fix that. With the above change, we won't copy past the end of memory but gd->fdt_blob + gd->fdt_size will still point past it, yes? Thanks!
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 659 bytes Desc: not available URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200619/502930e6/attachment.sig>
PATCH 1/1 common/board_f: Respect original FDT size while relocating
Quoted 14 lines · Tom Rini
Quoted 12 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
While relocating FDT we reserve some memory for the new FDT and set the size of the FDT with that respect. But FDT may be placed at the end of the RAM leading to memory access beyond it. Fix this by copying exact FDT size bytes, not the reserved size.
Signed-off-by Oleksandr Andrushchenko <[email protected]>--- common/board_f.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines
common/board_f.c static int reloc_fdt(void) · line 676
Quoted 13 lines
common/board_f.c +1
-1@@ -670,7 +670,7 @@ static int reloc_fdt(void)if (gd->flags &GD_FLG_SKIP_RELOC )return 0 ;if (gd->new_fdt) { -memcpy (gd->new_fdt, gd->fdt_blob, gd->fdt_size); +memcpy (gd->new_fdt, gd->fdt_blob,fdt_totalsize (gd->fdt_blob)); gd->fdt_blob = gd->new_fdt; }#endif So, I think the problem is placing the fdt so close to the end of memory and we need to fix that.
Exactly
Quoted 2 lines
With the above change, we won't copy past the
end of memory
yes
Quoted 2 lines
but
gd->fdt_blob+gd->fdt_sizewill still point past it,
yes?
Not really as the next op after the memcpy will set fdt_blob to the new location
and it is safe to access the memory in [gd->fdt_blob; gd->fdt_blob + gd->fdt_size) range.
We only ensure we are copying the fdt itself, not also the reserved memory which
doesn't exist past the original fdt addresses
Quoted 2 lines
Thanks!
PATCH 1/1 common/board_f: Respect original FDT size while relocating
Quoted 37 lines · Oleksandr Andrushchenko
Quoted 14 lines · Tom Rini
Quoted 12 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
While relocating FDT we reserve some memory for the new FDT and set the size of the FDT with that respect. But FDT may be placed at the end of the RAM leading to memory access beyond it. Fix this by copying exact FDT size bytes, not the reserved size.
Signed-off-by Oleksandr Andrushchenko <[email protected]>--- common/board_f.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Quoted 15 lines
common/board_f.c static int reloc_fdt(void) · line 676
Quoted 13 lines
common/board_f.c +1
-1@@ -670,7 +670,7 @@ static int reloc_fdt(void)if (gd->flags &GD_FLG_SKIP_RELOC )return 0 ;if (gd->new_fdt) { -memcpy (gd->new_fdt, gd->fdt_blob, gd->fdt_size); +memcpy (gd->new_fdt, gd->fdt_blob,fdt_totalsize (gd->fdt_blob)); gd->fdt_blob = gd->new_fdt; }#endif So, I think the problem is placing the fdt so close to the end of memory and we need to fix that.
Exactly
Quoted 2 lines
With the above change, we won't copy past the
end of memoryyes
Quoted 2 lines
but
gd->fdt_blob+gd->fdt_sizewill still point past it,
yes?
Quoted 7 lines
Not really as the next op after the memcpy will set
fdt_blobto the new locationand it is safe to access the memory in [
gd->fdt_blob;gd->fdt_blob+gd->fdt_size) range.We only ensure we are copying the fdt itself, not also the reserved memory which
doesn't exist past the original fdt addresses
Ah, so I had something backwards then. We're fine because gd->new_fdt + gd->fdt_size is fine. Thanks!
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 659 bytes Desc: not available URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200619/badca31b/attachment.sig>
PATCH 1/1 common/board_f: Respect original FDT size while relocating
Quoted 39 lines · Tom Rini
Quoted 38 lines · Oleksandr Andrushchenko
Quoted 14 lines · Tom Rini
Quoted 12 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
While relocating FDT we reserve some memory for the new FDT and set the size of the FDT with that respect. But FDT may be placed at the end of the RAM leading to memory access beyond it. Fix this by copying exact FDT size bytes, not the reserved size.
Signed-off-by Oleksandr Andrushchenko <[email protected]>--- common/board_f.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Quoted 15 lines
common/board_f.c static int reloc_fdt(void) · line 676
Quoted 13 lines
common/board_f.c +1
-1@@ -670,7 +670,7 @@ static int reloc_fdt(void)if (gd->flags &GD_FLG_SKIP_RELOC )return 0 ;if (gd->new_fdt) { -memcpy (gd->new_fdt, gd->fdt_blob, gd->fdt_size); +memcpy (gd->new_fdt, gd->fdt_blob,fdt_totalsize (gd->fdt_blob)); gd->fdt_blob = gd->new_fdt; }#endif So, I think the problem is placing the fdt so close to the end of memory and we need to fix that.
Exactly
Quoted 2 lines
With the above change, we won't copy past the
end of memoryyes
Quoted 2 lines
but
gd->fdt_blob+gd->fdt_sizewill still point past it,
yes?Not really as the next op after the memcpy will set
fdt_blobto the new location
Quoted 8 lines
Quoted 5 lines
and it is safe to access the memory in [
gd->fdt_blob;gd->fdt_blob+gd->fdt_size) range.We only ensure we are copying the fdt itself, not also the reserved memory which
doesn't exist past the original fdt addresses
Ah, so I had something backwards then. We're fine because
gd->new_fdt+gd->fdt_sizeis fine. Thanks!
Yes, that's right
PATCH 1/1 common/board_f: Respect original FDT size while relocating
In reply to Oleksandr AndrushchenkoQuoted 8 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
While relocating FDT we reserve some memory for the new FDT and set the size of the FDT with that respect. But FDT may be placed at the end of the RAM leading to memory access beyond it. Fix this by copying exact FDT size bytes, not the reserved size.
Quoted 5 lines
Signed-off-by Oleksandr Andrushchenko <[email protected]>--- common/board_f.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
PATCH 1/1 common/board_f: Respect original FDT size while relocating
In reply to Oleksandr AndrushchenkoQuoted 9 lines · Oleksandr Andrushchenko
From: Oleksandr Andrushchenko <[email protected]>
While relocating FDT we reserve some memory for the new FDT and set the size of the FDT with that respect. But FDT may be placed at the end of the RAM leading to memory access beyond it. Fix this by copying exact FDT size bytes, not the reserved size.
Signed-off-by Oleksandr Andrushchenko <[email protected]>Reviewed-by Simon Glass <[email protected]>
Applied to u-boot/master, thanks!
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 659 bytes Desc: not available URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200707/6afaf5e7/attachment.sig>