From cd3f69c290c76312e7ae568f8a1b9f2b13e87700 Mon Sep 17 00:00:00 2001 From: John Reiser Date: Thu, 31 Aug 2023 13:25:53 -0700 Subject: [PATCH] Explicit write() can subsume ftruncate() modified: stub/src/amd64-linux.elf-so_main.c modified: stub/src/i386-linux.elf-so_main.c --- src/stub/src/amd64-linux.elf-so_main.c | 4 ---- src/stub/src/i386-linux.elf-so_main.c | 3 --- 2 files changed, 7 deletions(-) diff --git a/src/stub/src/amd64-linux.elf-so_main.c b/src/stub/src/amd64-linux.elf-so_main.c index 2fb99266..260bba6c 100644 --- a/src/stub/src/amd64-linux.elf-so_main.c +++ b/src/stub/src/amd64-linux.elf-so_main.c @@ -263,7 +263,6 @@ make_hatch_x86_64( } else { // Does not fit at hi end of .text, so must use a new page "permanently" int mfd = memfd_create(addr_string("upx"), 0); // the directory entry - ftruncate(mfd, 6); Pwrite(mfd, addr_string("\x0f\x05\x5f\x5e\x5a\xc3"), 6); hatch = Pmap(0, 6, PROT_READ|PROT_EXEC, MAP_SHARED, mfd, 0); close(mfd); @@ -306,7 +305,6 @@ make_hatch_ppc64( } else { // Does not fit at hi end of .text, so must use a new page "permanently" int mfd = memfd_create(addr_string("upx"), 0); // the directory entry - ftruncate(mfd,sizeof(code)); Pwrite(mfd, code, sizeof(code)); hatch = Pmap(0, sizeof(code), PROT_READ|PROT_EXEC, MAP_SHARED, mfd, 0); close(mfd); @@ -345,7 +343,6 @@ make_hatch_arm64( } else { // Does not fit at hi end of .text, so must use a new page "permanently" int mfd = memfd_create(addr_string("upx"), 0); // the directory entry - ftruncate(mfd, sizeof(code)); Pwrite(mfd, code, sizeof(code)); void *mfd_addr = Pmap(0, sizeof(code), PROT_READ|PROT_EXEC, MAP_SHARED, mfd, 0); close(mfd); @@ -498,7 +495,6 @@ upx_so_main( // returns &escape_hatch if (phdr->p_flags & PF_X) { int mfd = memfd_create(addr_string("upx"), 0); unsigned mfd_len = 0ul - PAGE_MASK; - ftruncate(mfd, mfd_len); Pwrite(mfd, elf_tmp, binfo->sz_unc); // de-compressed Elf_Ehdr and Elf_Phdrs Pwrite(mfd, binfo->sz_unc + va_load, mfd_len - binfo->sz_unc); // rest of 1st page diff --git a/src/stub/src/i386-linux.elf-so_main.c b/src/stub/src/i386-linux.elf-so_main.c index a53f682d..bc5d6d91 100644 --- a/src/stub/src/i386-linux.elf-so_main.c +++ b/src/stub/src/i386-linux.elf-so_main.c @@ -287,7 +287,6 @@ make_hatch_i386( } else { // Does not fit at hi end of .text, so must use a new page "permanently" int mfd = memfd_create(addr_string("upx"), 0); // the directory entry - //ftruncate(mfd, 4); write(mfd, &escape, 4); hatch = mmap(0, 4, PROT_READ|PROT_EXEC, MAP_SHARED, mfd, 0); close(mfd); @@ -325,7 +324,6 @@ make_hatch_arm32( } else { // Does not fit at hi end of .text, so must use a new page "permanently" int mfd = memfd_create(addr_string("upx"), 0); // the directory entry - //ftruncate(mfd, 2*4); write(mfd, &code, 2*4); hatch = Pmap(0, 2*4, PROT_READ|PROT_EXEC, MAP_SHARED, mfd, 0); close(mfd); @@ -555,7 +553,6 @@ upx_so_main( // returns &escape_hatch if (phdr->p_flags & PF_X) { int mfd = memfd_create(addr_string("upx"), 0); unsigned mfd_len = 0ul - page_mask; - ftruncate(mfd, mfd_len); Pwrite(mfd, elf_tmp, binfo->sz_unc); // de-compressed Elf_Ehdr and Elf_Phdrs Pwrite(mfd, binfo->sz_unc + va_load, mfd_len - binfo->sz_unc); // rest of 1st page