diff --git a/contrib/elftoolchain/readelf/readelf.c b/contrib/elftoolchain/readelf/readelf.c index 2cfc25cadd19..b03798eabebd 100644 --- a/contrib/elftoolchain/readelf/readelf.c +++ b/contrib/elftoolchain/readelf/readelf.c @@ -26,8 +26,10 @@ #include #include + #include #include +#include #include #include #include @@ -45,6 +47,9 @@ #include #include +#include +#include + #include "_elftc.h" ELFTC_VCSID("$Id: readelf.c 3649 2018-11-24 03:26:23Z emaste $"); @@ -7214,15 +7219,8 @@ process_members: } static void -dump_object(struct readelf *re) +dump_object(struct readelf *re, int fd) { - int fd; - - if ((fd = open(re->filename, O_RDONLY)) == -1) { - warn("open %s failed", re->filename); - return; - } - if ((re->flags & DISPLAY_FILENAME) != 0) printf("\nFile: %s\n", re->filename); @@ -7589,9 +7587,11 @@ readelf_usage(int status) int main(int argc, char **argv) { + cap_rights_t rights; + fileargs_t *fa; struct readelf *re, re_storage; unsigned long si; - int opt, i; + int fd, opt, i; char *ep; re = &re_storage; @@ -7714,9 +7714,28 @@ main(int argc, char **argv) errx(EXIT_FAILURE, "ELF library initialization failed: %s", elf_errmsg(-1)); + cap_rights_init(&rights, CAP_FCNTL, CAP_FSTAT, CAP_MMAP_R, CAP_SEEK); + fa = fileargs_init(argc, argv, O_RDONLY, 0, &rights, FA_OPEN); + if (fa == NULL) + err(1, "Unable to initialize casper fileargs"); + + caph_cache_catpages(); + if (caph_limit_stdio() < 0) { + fileargs_free(fa); + err(1, "Unable to limit stdio rights"); + } + if (caph_enter_casper() < 0) { + fileargs_free(fa); + err(1, "Unable to enter capability mode"); + } + for (i = 0; i < argc; i++) { re->filename = argv[i]; - dump_object(re); + fd = fileargs_open(fa, re->filename); + if (fd < 0) + warn("open %s failed", re->filename); + else + dump_object(re, fd); } exit(EXIT_SUCCESS); diff --git a/usr.bin/readelf/Makefile b/usr.bin/readelf/Makefile index 238a901197d7..ba44aac32d67 100644 --- a/usr.bin/readelf/Makefile +++ b/usr.bin/readelf/Makefile @@ -1,5 +1,7 @@ # $FreeBSD$ +.include + ELFTCDIR= ${SRCTOP}/contrib/elftoolchain READELFDIR= ${ELFTCDIR}/readelf @@ -10,6 +12,12 @@ SRCS= readelf.c LIBADD= dwarf elftc elf +.if ${MK_CASPER} != "no" +LIBADD+= casper +LIBADD+= cap_fileargs +CFLAGS+= -DWITH_CASPER +.endif + CFLAGS+=-I${ELFTCDIR}/libelftc -I${ELFTCDIR}/common # This same hack is in lib/libelf/Makefile and lib/libdwarf/Makefile