Changeset View
Changeset View
Standalone View
Standalone View
contrib/elftoolchain/strings/strings.c
Show All 32 Lines | |||||
#include <fcntl.h> | #include <fcntl.h> | ||||
#include <getopt.h> | #include <getopt.h> | ||||
#include <inttypes.h> | #include <inttypes.h> | ||||
#include <stdint.h> | #include <stdint.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <sysexits.h> | #include <sysexits.h> | ||||
#include <termios.h> | |||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <libelf.h> | #include <libelf.h> | ||||
#include <libelftc.h> | #include <libelftc.h> | ||||
#include <gelf.h> | #include <gelf.h> | ||||
#include "_elftc.h" | #include "_elftc.h" | ||||
#if ELFTC_HAVE_CAPSICUM | |||||
#include <sys/capsicum.h> | |||||
#endif | |||||
ELFTC_VCSID("$Id: strings.c 3446 2016-05-03 01:31:17Z emaste $"); | ELFTC_VCSID("$Id: strings.c 3446 2016-05-03 01:31:17Z emaste $"); | ||||
enum return_code { | enum return_code { | ||||
RETURN_OK, | RETURN_OK, | ||||
RETURN_NOINPUT, | RETURN_NOINPUT, | ||||
RETURN_SOFTWARE | RETURN_SOFTWARE | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 141 Lines • ▼ Show 20 Lines | if (!*argv) | ||||
rc = handle_file("{standard input}"); | rc = handle_file("{standard input}"); | ||||
else while (*argv) { | else while (*argv) { | ||||
rc = handle_file(*argv); | rc = handle_file(*argv); | ||||
argv++; | argv++; | ||||
} | } | ||||
return (rc); | return (rc); | ||||
} | } | ||||
#if ELFTC_HAVE_CAPSICUM | |||||
static void | |||||
enter_capsicum(int fd, bool use_mmap) | |||||
{ | |||||
cap_rights_t rights; | |||||
unsigned long cmd; | |||||
/* | |||||
* We may mmap the file when we've already opened it, allow this. | |||||
*/ | |||||
if (use_mmap) | |||||
cap_rights_init(&rights, CAP_READ, CAP_FSTAT, CAP_SEEK, | |||||
CAP_MMAP); | |||||
else | |||||
cap_rights_init(&rights, CAP_READ, CAP_FSTAT, CAP_SEEK); | |||||
if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS) | |||||
errx(1, "Unable to limit rights for stdin"); | |||||
cap_rights_init(&rights, CAP_WRITE, CAP_FSTAT, CAP_IOCTL); | |||||
if (cap_rights_limit(STDOUT_FILENO, &rights) < 0 && errno != ENOSYS) | |||||
errx(1, "Unable to limit rights for stdout"); | |||||
if (cap_rights_limit(STDERR_FILENO, &rights) < 0 && errno != ENOSYS) | |||||
errx(1, "Unable to limit rights for stderr"); | |||||
cmd = TIOCGETA; | |||||
if (cap_ioctls_limit(STDOUT_FILENO, &cmd, 1) < 0 && errno != ENOSYS) | |||||
errx(1, "unable to limit ioctls for stdout"); | |||||
if (cap_enter() != 0) | |||||
errx(1, "unable to enter capability mode"); | |||||
} | |||||
#endif | |||||
int | int | ||||
handle_file(const char *name) | handle_file(const char *name) | ||||
{ | { | ||||
int fd, rt; | int fd, rt; | ||||
if (name == NULL) | if (name == NULL) | ||||
return (RETURN_NOINPUT); | return (RETURN_NOINPUT); | ||||
if (strcmp("{standard input}", name) != 0) { | if (strcmp("{standard input}", name) != 0) { | ||||
if (freopen(name, "rb", stdin) == NULL) { | if (freopen(name, "rb", stdin) == NULL) { | ||||
warnx("'%s': %s", name, strerror(errno)); | warnx("'%s': %s", name, strerror(errno)); | ||||
return (RETURN_NOINPUT); | return (RETURN_NOINPUT); | ||||
} | } | ||||
} else { | } else { | ||||
#if ELFTC_HAVE_CAPSICUM | |||||
enter_capsicum(STDIN_FILENO, false); | |||||
#endif | |||||
return (find_strings(name, (off_t)0, (off_t)0)); | return (find_strings(name, (off_t)0, (off_t)0)); | ||||
} | } | ||||
fd = fileno(stdin); | fd = fileno(stdin); | ||||
if (fd < 0) | if (fd < 0) | ||||
return (RETURN_NOINPUT); | return (RETURN_NOINPUT); | ||||
#if ELFTC_HAVE_CAPSICUM | |||||
enter_capsicum(fd, true); | |||||
#endif | |||||
rt = handle_elf(name, fd); | rt = handle_elf(name, fd); | ||||
return (rt); | return (rt); | ||||
} | } | ||||
/* | /* | ||||
* Files not understood by handle_elf, will be passed off here and will | * Files not understood by handle_elf, will be passed off here and will | ||||
* treated as a binary file. This would include text file, core dumps ... | * treated as a binary file. This would include text file, core dumps ... | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 225 Lines • Show Last 20 Lines |