Skip to contentSkip to conversation

efi_loader: correct parameter size in efi_allocate_pool

PATCH New Heinrich Schuchardt 2 participants 5 messages Mar 18, 2019 – Mar 20, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 efi_loader: correct parameter size in efi_allocate_pool

efi_allocate_pages() expects a (uint64_t *) pointer to pass the address of the assigned memory. If we pass the address of a pointer here, an illegal memory access occurs on 32bit systems.

Fixes 282a06cbcae8 ("efi_loader: Expose U-Boot addresses in memory map

for sandbox")

---
 lib/efi_loader/efi_memory.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/efi_loader/efi_memory.c +3 -2
index ebd2b36c03..55622d2fb4 100644
@@ -440,6 +440,7 @@ efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
 efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
 {
 	efi_status_t r;
+	u64 addr;
 	struct efi_pool_allocation *alloc;
 	u64 num_pages = efi_size_in_pages(size +
 					  sizeof(struct efi_pool_allocation));
@@ -453,9 +454,9 @@ efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
 	}
 
 	r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
-			       (uint64_t *)&alloc);
-
+			       &addr);
 	if (r == EFI_SUCCESS) {
+		alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
 		alloc->num_pages = num_pages;
 		*buffer = alloc->data;
 	}
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/1 efi_loader: correct parameter size in efi_allocate_pool

Quoted 24 lines · Heinrich Schuchardt

efi_allocate_pages() expects a (uint64_t *) pointer to pass the address of the assigned memory. If we pass the address of a pointer here, an illegal memory access occurs on 32bit systems.

Fixes 282a06cbcae8 ("efi_loader: Expose U-Boot addresses in memory map

for sandbox")

---
 lib/efi_loader/efi_memory.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/efi_loader/efi_memory.c +1 -0
index ebd2b36c03..55622d2fb4 100644
@@ -440,6 +440,7 @@ efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
 efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
 {
 	efi_status_t r;
+	u64 addr;
 	struct efi_pool_allocation *alloc;
 	u64 num_pages = efi_size_in_pages(size +
 					  sizeof(struct efi_pool_allocation));

lib/efi_loader/efi_memory.c efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer) · line 456

Quoted 4 lines
lib/efi_loader/efi_memory.c +0 -1
@@ -453,9 +454,9 @@ efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
 	}
 
 	r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
-			       (uint64_t *)&alloc);

I wonder why efi_allocate_pages() doesn't expect (void **) for the fourth argument.
If this is because the type of the argument is a pointer to "physical address,"

lib/efi_loader/efi_memory.c efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer) · line 459

Quoted 4 lines
lib/efi_loader/efi_memory.c +2 -1
-
+			       &addr);
 	if (r == EFI_SUCCESS) {
+		alloc = (struct efi_pool_allocation *)(uintptr_t)addr;

we should use map_sysmem() here.

Thanks,
-Takahiro Akashi

Quoted 6 lines
lib/efi_loader/efi_memory.c +0 -0
 		alloc->num_pages = num_pages;
 		*buffer = alloc->data;
 	}
Signature · 1 line
2.20.1
#2 lore raw

PATCH 1/1 efi_loader: correct parameter size in efi_allocate_pool

Quoted 27 lines · Takahiro Akashi
Quoted 26 lines · Heinrich Schuchardt

efi_allocate_pages() expects a (uint64_t *) pointer to pass the address of the assigned memory. If we pass the address of a pointer here, an illegal memory access occurs on 32bit systems.

Fixes 282a06cbcae8 ("efi_loader: Expose U-Boot addresses in memory map

for sandbox")

---
 lib/efi_loader/efi_memory.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/efi_loader/efi_memory.c +1 -0
index ebd2b36c03..55622d2fb4 100644
@@ -440,6 +440,7 @@ efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
 efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
 {
 	efi_status_t r;
+	u64 addr;
 	struct efi_pool_allocation *alloc;
 	u64 num_pages = efi_size_in_pages(size +
 					  sizeof(struct efi_pool_allocation));
@@ -453,9 +454,9 @@ efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
 	}
 
Quoted 5 lines

lib/efi_loader/efi_memory.c efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer) · line 456

Quoted 2 lines
lib/efi_loader/efi_memory.c +0 -1
 	r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
-			       (uint64_t *)&alloc);

I wonder why efi_allocate_pages() doesn't expect (void **) for the fourth argument.

efi_allocate_pages implements the AllocatePages() boot time service. The UEFI spec mandates that the parameter is of type EFI_PHYSICAL_ADDRESS * i.e. u64 *.

Quoted 8 lines

If this is because the type of the argument is a pointer to "physical address,"

lib/efi_loader/efi_memory.c efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer) · line 459

Quoted 4 lines
lib/efi_loader/efi_memory.c +2 -1
-
+			       &addr);
 	if (r == EFI_SUCCESS) {
+		alloc = (struct efi_pool_allocation *)(uintptr_t)addr;

we should use map_sysmem() here.

This would create a bug on the sandbox.

map_sysmem() converts an address from the sandbox virtual address space to the address space that can be used by EFI binaries.

AllocatePool() and AllocatePages() both refer to the same address space.

Cf. commit 49759743bf09 ("efi_loader: eliminate sandbox addresses")

Best regards

Heinrich

Quoted 11 lines

Thanks,
-Takahiro Akashi

Quoted 6 lines
lib/efi_loader/efi_memory.c +0 -0
 		alloc->num_pages = num_pages;
 		*buffer = alloc->data;
 	}
Signature · 1 line
2.20.1
#3 lore raw

PATCH 1/1 efi_loader: correct parameter size in efi_allocate_pool

Quoted 34 lines · Heinrich Schuchardt
Quoted 27 lines · Takahiro Akashi
Quoted 26 lines · Heinrich Schuchardt

efi_allocate_pages() expects a (uint64_t *) pointer to pass the address of the assigned memory. If we pass the address of a pointer here, an illegal memory access occurs on 32bit systems.

Fixes 282a06cbcae8 ("efi_loader: Expose U-Boot addresses in memory map

for sandbox")

---
 lib/efi_loader/efi_memory.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/efi_loader/efi_memory.c +1 -0
index ebd2b36c03..55622d2fb4 100644
@@ -440,6 +440,7 @@ efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
 efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
 {
 	efi_status_t r;
+	u64 addr;
 	struct efi_pool_allocation *alloc;
 	u64 num_pages = efi_size_in_pages(size +
 					  sizeof(struct efi_pool_allocation));
@@ -453,9 +454,9 @@ efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
 	}
 
Quoted 5 lines

lib/efi_loader/efi_memory.c efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer) · line 456

Quoted 2 lines
lib/efi_loader/efi_memory.c +0 -1
 	r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
-			       (uint64_t *)&alloc);

I wonder why efi_allocate_pages() doesn't expect (void **) for the fourth argument.

Quoted 3 lines

efi_allocate_pages implements the AllocatePages() boot time service. The UEFI spec mandates that the parameter is of type EFI_PHYSICAL_ADDRESS * i.e. u64 *.

But allocate_pool() takes "void **" for the third argument. Why?

Quoted 9 lines
Quoted 8 lines

If this is because the type of the argument is a pointer to "physical address,"

lib/efi_loader/efi_memory.c efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer) · line 459

Quoted 4 lines
lib/efi_loader/efi_memory.c +2 -1
-
+			       &addr);
 	if (r == EFI_SUCCESS) {
+		alloc = (struct efi_pool_allocation *)(uintptr_t)addr;

we should use map_sysmem() here.

Quoted 8 lines

This would create a bug on the sandbox.

map_sysmem() converts an address from the sandbox virtual address space to the address space that can be used by EFI binaries.

AllocatePool() and AllocatePages() both refer to the same address space.

Cf. commit 49759743bf09 ("efi_loader: eliminate sandbox addresses")

I don't know the case of sandbox, but What I have in mind is LPAE, that is, the system have >32-bit physical address space, but cpu can only access it via 32-bit pointer.

Thanks,
-Takahiro Akashi

Quoted 16 lines

Best regards

Heinrich

Quoted 11 lines

Thanks,
-Takahiro Akashi

Quoted 6 lines
lib/efi_loader/efi_memory.c +0 -0
 		alloc->num_pages = num_pages;
 		*buffer = alloc->data;
 	}
Signature · 1 line
2.20.1
#4 lore raw

PATCH 1/1 efi_loader: correct parameter size in efi_allocate_pool

Quoted 35 lines · Takahiro Akashi
Quoted 34 lines · Heinrich Schuchardt
Quoted 32 lines · Takahiro Akashi
Quoted 24 lines · Heinrich Schuchardt

efi_allocate_pages() expects a (uint64_t *) pointer to pass the address of the assigned memory. If we pass the address of a pointer here, an illegal memory access occurs on 32bit systems.

Fixes 282a06cbcae8 ("efi_loader: Expose U-Boot addresses in memory map

for sandbox")

---
 lib/efi_loader/efi_memory.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/efi_loader/efi_memory.c +1 -0
index ebd2b36c03..55622d2fb4 100644
@@ -440,6 +440,7 @@ efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
 efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
 {
 	efi_status_t r;
+	u64 addr;
 	struct efi_pool_allocation *alloc;
 	u64 num_pages = efi_size_in_pages(size +
 					  sizeof(struct efi_pool_allocation));

lib/efi_loader/efi_memory.c efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer) · line 456

Quoted 4 lines
lib/efi_loader/efi_memory.c +0 -1
@@ -453,9 +454,9 @@ efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
 	}
 
 	r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
-			       (uint64_t *)&alloc);

I wonder why efi_allocate_pages() doesn't expect (void **) for the fourth argument.

Quoted 5 lines
Quoted 3 lines

efi_allocate_pages implements the AllocatePages() boot time service. The UEFI spec mandates that the parameter is of type EFI_PHYSICAL_ADDRESS * i.e. u64 *.

But allocate_pool() takes "void **" for the third argument. Why?

efi_allocate_pool() implements the AllocatePool() boot service which is defined in the UEFI spec this way.

Quoted 15 lines
Quoted 15 lines
Quoted 9 lines

If this is because the type of the argument is a pointer to "physical address,"

lib/efi_loader/efi_memory.c efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer) · line 459

Quoted 4 lines
lib/efi_loader/efi_memory.c +2 -1
-
+			       &addr);
 	if (r == EFI_SUCCESS) {
+		alloc = (struct efi_pool_allocation *)(uintptr_t)addr;

we should use map_sysmem() here.

This would create a bug on the sandbox.

map_sysmem() converts an address from the sandbox virtual address space to the address space that can be used by EFI binaries.

Quoted 7 lines
Quoted 3 lines

AllocatePool() and AllocatePages() both refer to the same address space.

Cf. commit 49759743bf09 ("efi_loader: eliminate sandbox addresses")

I don't know the case of sandbox, but What I have in mind is LPAE, that is, the system have >32-bit physical address space, but cpu can only access it via 32-bit pointer.

The UEFI spec requires "if paging is enabled and SetVirtualAddressMap() has not been called, any memory space defined by the UEFI memory map is identity mapped (virtual address equals physical address)".

It is only after calling SetVirtualAddressMap() that virtual addresses and physical ones may differ. But at that time boottime services like AllocatePool may not be called anymore.

I guess it is with LPAE in mind that AllocatePages() uses 64bit physical addresses while AllocatePool() uses virtual addresses with system dependent size.

Best regards

Heinrich

Quoted 21 lines

Thanks,
-Takahiro Akashi

Quoted 16 lines

Best regards

Heinrich

Quoted 11 lines

Thanks,
-Takahiro Akashi

Quoted 6 lines
lib/efi_loader/efi_memory.c +0 -1
 		alloc->num_pages = num_pages;
 		*buffer = alloc->data;
 	}
--

2.20.1