Skip to contentSkip to conversation

common/board_f: Respect original FDT size while relocating

PATCH New Oleksandr Andrushchenko 4 participants 7 messages Jun 19, 2020 – Jul 7, 2020

lore
Share & tools
lore
#0 lore raw

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
index 01194eaa0e4d..aa1285e94999 100644
@@ -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
#1 lore raw

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.

---
 common/board_f.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/board_f.c +0 -0
index 01194eaa0e4d..aa1285e94999 100644

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>
#2 lore raw

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.

---
  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
index 01194eaa0e4d..aa1285e94999 100644
@@ -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_size will 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!

#3 lore raw

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.

---
  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
index 01194eaa0e4d..aa1285e94999 100644
@@ -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_size will still point past it,
yes?

Quoted 7 lines

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

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>
#4 lore raw

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.

---
   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
index 01194eaa0e4d..aa1285e94999 100644
@@ -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_size will still point past it,
yes?

Not really as the next op after the memcpy will set fdt_blob to 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_size is fine. Thanks!

Yes, that's right

#5 lore raw

PATCH 1/1 common/board_f: Respect original FDT size while relocating

In reply to Oleksandr Andrushchenko
Quoted 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
---
 common/board_f.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#6 lore raw

PATCH 1/1 common/board_f: Respect original FDT size while relocating

In reply to Oleksandr Andrushchenko
Quoted 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.

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>