Skip to content

Commit 2a038e3

Browse files
author
Henry Wilson
committed
fatelf-exec: Execute FatElf binaries
Add the ability to execute fatelf binaries from a memfd. OpenBSD support may be possible with shm_mkstemp. A future improvement may be to read into a mmap of the memfd. Use systemd-binfmt to tell the kernel how to execute FatElf binaries.
1 parent ed748c4 commit 2a038e3

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
cmake_minimum_required(VERSION 3.0.0)
22
project(FatELF)
33

4+
include(CheckSymbolExists)
5+
include(GNUInstallDirs)
6+
47
execute_process(
58
COMMAND git rev-list HEAD~..
69
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
@@ -37,5 +40,18 @@ add_fatelf_executable(fatelf-verify)
3740
add_fatelf_executable(fatelf-split)
3841
add_fatelf_executable(fatelf-validate)
3942

43+
set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
44+
check_symbol_exists(memfd_create sys/mman.h MEMFD)
45+
if(MEMFD)
46+
add_fatelf_executable(fatelf-exec)
47+
48+
find_program(BINFMT systemd-binfmt PATHS /usr/lib/systemd)
49+
if(BINFMT)
50+
configure_file(fatelf.conf.in fatelf.conf)
51+
install(DIRECTORY DESTINATION ${CMAKE_INSTALL_LIBDIR}/binfmt.d)
52+
install(FILES ${CMAKE_BINARY_DIR}/fatelf.conf DESTINATION ${CMAKE_INSTALL_LIBDIR}/binfmt.d/)
53+
endif()
54+
endif()
55+
4056
# end of CMakeLists.txt ...
4157

fatelf.conf.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:fatelf-v1:M::\xFA\x70\x0E\x1F\x01\x00::${CMAKE_INSTALL_FULL_BINDIR}/fatelf-exec:

utils/fatelf-exec.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* FatELF; support multiple ELF binaries in one file.
3+
*
4+
* Please see the file LICENSE.txt in the source's root directory.
5+
*
6+
* This file written by Henry W. Wilson.
7+
*/
8+
9+
#define _GNU_SOURCE
10+
#define FATELF_UTILS 1
11+
#include "fatelf-utils.h"
12+
#include <sys/mman.h>
13+
#include <sys/utsname.h>
14+
#include <unistd.h>
15+
16+
int main(int argc, char *argv[], char *envp[])
17+
{
18+
xfatelf_init(argc, (char const**)argv);
19+
if (argc < 2)
20+
xfail("USAGE: %s <in> [args...]", argv[0]);
21+
22+
const int fd = xopen(argv[1], O_RDONLY | O_CLOEXEC, 0755);
23+
FATELF_header *header = xread_fatelf_header(argv[1], fd);
24+
struct utsname utsn;
25+
uname(&utsn);
26+
const int recidx = xfind_fatelf_record(header, utsn.machine);
27+
const int outfd = memfd_create("ELF", MFD_CLOEXEC);
28+
29+
if (outfd < 0)
30+
xfail("memfd_create");
31+
32+
const FATELF_record *rec = &header->records[recidx];
33+
34+
xcopyfile_range(argv[1], fd, "memfd:ELF", outfd, rec->offset, rec->size);
35+
xappend_junk(argv[1], fd, "memfd:ELF", outfd, header);
36+
37+
fexecve(outfd, (argv + 1), envp);
38+
xfail("fexecve");
39+
return EXIT_FAILURE;
40+
} // main
41+
42+
// end of fatelf-exec.c ...

0 commit comments

Comments
 (0)