Skip to content

Commit 9a33442

Browse files
mogasergiurazvand
authored andcommitted
Handle empty file size loadable segments
In cases such as those if the application has no .data but has .bss, the .bss may end up being alone in an empty filesz loadable segment. Handle such situation by only `mmap`ing file contents if the segment has actual file contents (filesz != 0). Signed-off-by: Sergiu Moga <[email protected]> Approved-by: Razvan Deaconescu <[email protected]> Reviewed-by: Razvan Deaconescu <[email protected]> GitHub-Closes: #85
1 parent 8b55bff commit 9a33442

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

elf_load.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -519,19 +519,27 @@ static int do_elf_load_fdphdr_not0(struct elf_prog *elf_prog,
519519
/* mmap with all flags. If protections are enabled, these will
520520
* be manually re-adjusted later.
521521
*/
522-
vastart = (uintptr_t)mmap(addr, phdr->p_filesz + delta_p_offset,
523-
get_phdr_mmap_prot(phdr),
524-
MAP_FIXED | MAP_PRIVATE,
525-
fd, phdr->p_offset - delta_p_offset);
526-
if (unlikely(vastart == (uintptr_t)MAP_FAILED)) {
527-
uk_pr_err("Failed to mmap the phdr at offset %lu\n",
528-
phdr->p_offset);
529-
return (int)vastart;
522+
if (phdr->p_filesz) {
523+
vastart = (uintptr_t)mmap(addr, phdr->p_filesz + delta_p_offset,
524+
get_phdr_mmap_prot(phdr),
525+
MAP_FIXED | MAP_PRIVATE,
526+
fd, phdr->p_offset - delta_p_offset);
527+
if (unlikely(vastart == (uintptr_t)MAP_FAILED)) {
528+
uk_pr_err("Failed to mmap the phdr at offset %lu\n",
529+
phdr->p_offset);
530+
return (int)vastart;
531+
}
532+
533+
vastart += phdr->p_filesz + delta_p_offset;
534+
vaend = PAGE_ALIGN_UP(vastart +
535+
(phdr->p_memsz - phdr->p_filesz));
536+
} else {
537+
vastart = (uintptr_t)addr;
538+
vaend = PAGE_ALIGN_UP(vastart + phdr->p_memsz +
539+
delta_p_offset);
530540
}
531541

532542
/* mmap anonymously what we are left if memsz > filesz */
533-
vastart += phdr->p_filesz + delta_p_offset;
534-
vaend = PAGE_ALIGN_UP(vastart + (phdr->p_memsz - phdr->p_filesz));
535543
if (vaend > vastart) {
536544
rc = elf_load_mmap_filesz_memsz_diff(elf_prog, phdr,
537545
vastart, vaend);

0 commit comments

Comments
 (0)