Changeset View
Changeset View
Standalone View
Standalone View
contrib/elftoolchain/strings/strings.c
Show All 19 Lines | |||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||||
* SUCH DAMAGE. | * SUCH DAMAGE. | ||||
*/ | */ | ||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#include <sys/capsicum.h> | |||||
#include <sys/stat.h> | #include <sys/stat.h> | ||||
#include <capsicum_helpers.h> | |||||
#include <ctype.h> | #include <ctype.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#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 <unistd.h> | #include <unistd.h> | ||||
#include <libelf.h> | #include <libelf.h> | ||||
#include <libelftc.h> | #include <libelftc.h> | ||||
#include <gelf.h> | #include <gelf.h> | ||||
#include <libcasper.h> | |||||
#include <casper/cap_fileargs.h> | |||||
#include "_elftc.h" | #include "_elftc.h" | ||||
ELFTC_VCSID("$Id: strings.c 3571 2017-09-14 02:04:50Z emaste $"); | ELFTC_VCSID("$Id: strings.c 3571 2017-09-14 02:04:50Z emaste $"); | ||||
enum radix_style { | enum radix_style { | ||||
RADIX_DECIMAL, | RADIX_DECIMAL, | ||||
RADIX_HEX, | RADIX_HEX, | ||||
RADIX_OCTAL | RADIX_OCTAL | ||||
Show All 25 Lines | static struct option strings_longopts[] = { | ||||
{ "help", no_argument, NULL, 'h'}, | { "help", no_argument, NULL, 'h'}, | ||||
{ "print-file-name", no_argument, NULL, 'f'}, | { "print-file-name", no_argument, NULL, 'f'}, | ||||
{ "radix", required_argument, NULL, 't'}, | { "radix", required_argument, NULL, 't'}, | ||||
{ "version", no_argument, NULL, 'v'}, | { "version", no_argument, NULL, 'v'}, | ||||
{ NULL, 0, NULL, 0 } | { NULL, 0, NULL, 0 } | ||||
}; | }; | ||||
long getcharacter(FILE *); | long getcharacter(FILE *); | ||||
int handle_file(const char *); | int handle_file(fileargs_t *fa, const char *); | ||||
int handle_elf(const char *, FILE *); | int handle_elf(const char *, FILE *); | ||||
int handle_binary(const char *, FILE *, size_t); | int handle_binary(const char *, FILE *, size_t); | ||||
int find_strings(const char *, FILE *, off_t, off_t); | int find_strings(const char *, FILE *, off_t, off_t); | ||||
void show_version(void); | void show_version(void); | ||||
void usage(void); | void usage(void); | ||||
/* | /* | ||||
* strings(1) extracts text(contiguous printable characters) | * strings(1) extracts text(contiguous printable characters) | ||||
* from elf and binary files. | * from elf and binary files. | ||||
*/ | */ | ||||
int | int | ||||
main(int argc, char **argv) | main(int argc, char **argv) | ||||
{ | { | ||||
fileargs_t *fa; | |||||
cap_rights_t rights; | |||||
int ch, rc; | int ch, rc; | ||||
rc = 0; | rc = 0; | ||||
min_len = 0; | min_len = 0; | ||||
encoding_size = 1; | encoding_size = 1; | ||||
if (elf_version(EV_CURRENT) == EV_NONE) | if (elf_version(EV_CURRENT) == EV_NONE) | ||||
errx(EXIT_FAILURE, "ELF library initialization failed: %s", | errx(EXIT_FAILURE, "ELF library initialization failed: %s", | ||||
elf_errmsg(-1)); | elf_errmsg(-1)); | ||||
▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | while ((ch = getopt_long(argc, argv, "1234567890ae:fhn:ot:Vv", | ||||
default: | default: | ||||
usage(); | usage(); | ||||
/* NOTREACHED */ | /* NOTREACHED */ | ||||
} | } | ||||
} | } | ||||
argc -= optind; | argc -= optind; | ||||
argv += optind; | argv += optind; | ||||
cap_rights_init(&rights, CAP_READ, CAP_SEEK, CAP_FSTAT, CAP_FCNTL); | |||||
fa = fileargs_init(argc, argv, O_RDONLY, 0, &rights); | |||||
if (fa == NULL) | |||||
err(1, "Unable to enter capability mode"); | |||||
cem: I would maybe print "unable to initialize casper fileargs."
I expect emaste will say, "what do… | |||||
emasteUnsubmitted Not Done Inline ActionsThat is what I might say, but I don't feel strongly either. emaste: That is what I might say, but I don't feel strongly either. | |||||
oshogboAuthorUnsubmitted Done Inline ActionsOk I will address that. oshogbo: Ok I will address that. | |||||
caph_cache_catpages(); | |||||
if (caph_limit_stdio() < 0 && caph_enter_casper() < 0) { | |||||
emasteUnsubmitted Not Done Inline Actions@jkoshy_users.sourceforge.net this is the "sandbox setup & enter" entry; for OpenBSD this would be a pledge() call. The fileargs_init() and fileargs_fopen() are the magic bits that perform the privilege separated file opening and fd shuffling that do not have an equivalent on !FreeBSD. emaste: @jkoshy_users.sourceforge.net this is the "sandbox setup & enter" entry; for OpenBSD this would… | |||||
fileargs_free(fa); | |||||
cemUnsubmitted Not Done Inline ActionsWhy free before err() exit? cem: Why free before err() exit? | |||||
oshogboAuthorUnsubmitted Done Inline ActionsIt's just nice to clean up after yourself, and I wanted to do another good example just to fallow. oshogbo: It's just nice to clean up after yourself, and I wanted to do another good example just to… | |||||
err(1, "Unable to enter capability mode"); | |||||
} | |||||
if (min_len == 0) | if (min_len == 0) | ||||
min_len = 4; | min_len = 4; | ||||
if (*argv == NULL) | if (*argv == NULL) | ||||
rc = find_strings("{standard input}", stdin, 0, 0); | rc = find_strings("{standard input}", stdin, 0, 0); | ||||
else while (*argv != NULL) { | else while (*argv != NULL) { | ||||
if (handle_file(*argv) != 0) | if (handle_file(fa, *argv) != 0) | ||||
rc = 1; | rc = 1; | ||||
argv++; | argv++; | ||||
} | } | ||||
fileargs_free(fa); | |||||
cemUnsubmitted Not Done Inline ActionsWhy free before return exit? :-) cem: Why free before return exit? :-) | |||||
oshogboAuthorUnsubmitted Done Inline ActionsIt's just nice to clean up after yourself, and I wanted to do another good example just to fallow. oshogbo: It's just nice to clean up after yourself, and I wanted to do another good example just to… | |||||
return (rc); | return (rc); | ||||
} | } | ||||
int | int | ||||
handle_file(const char *name) | handle_file(fileargs_t *fa, const char *name) | ||||
{ | { | ||||
FILE *pfile; | FILE *pfile; | ||||
int rt; | int rt; | ||||
if (name == NULL) | if (name == NULL) | ||||
return (1); | return (1); | ||||
pfile = fopen(name, "rb"); | pfile = fileargs_fopen(fa, name, "rb"); | ||||
if (pfile == NULL) { | if (pfile == NULL) { | ||||
warnx("'%s': %s", name, strerror(errno)); | warnx("'%s': %s", name, strerror(errno)); | ||||
return (1); | return (1); | ||||
} | } | ||||
rt = handle_elf(name, pfile); | rt = handle_elf(name, pfile); | ||||
fclose(pfile); | fclose(pfile); | ||||
return (rt); | return (rt); | ||||
▲ Show 20 Lines • Show All 236 Lines • Show Last 20 Lines |
I would maybe print "unable to initialize casper fileargs."
I expect emaste will say, "what do we expect a user to do differently?", and from that perspective, sure, the two failures are essentially the same problem. But as a developer who may occasionally debug capsicumized programs, I greatly prefer that error messages are unique in code, so it is clear where the fault occurred.
I do not feel strongly about it.