From cdd34c657193205e8680b74d7c2524cb81a091a7 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 24 Apr 2015 16:47:32 +0000 Subject: [PATCH] Trying to drop privileges means we can't handle ARG_MAX arguments, so remove it for now. --- usr.bin/file/file.c | 40 ++++++++++++++-------------------------- usr.bin/file/file.h | 5 +---- 2 files changed, 15 insertions(+), 30 deletions(-) diff --git a/usr.bin/file/file.c b/usr.bin/file/file.c index a115c4caaa9..7a5989a4aa7 100644 --- a/usr.bin/file/file.c +++ b/usr.bin/file/file.c @@ -1,4 +1,4 @@ -/* $OpenBSD: file.c,v 1.29 2015/04/24 16:30:06 nicm Exp $ */ +/* $OpenBSD: file.c,v 1.30 2015/04/24 16:47:32 nicm Exp $ */ /* * Copyright (c) 2015 Nicholas Marriott @@ -91,8 +91,8 @@ usage(void) int main(int argc, char **argv) { - struct input_file *files = NULL; - int nfiles, opt, i, width = 0; + struct input_file inf; + int opt, i, width = 0; FILE *f; struct magic *m; char *home, *path; @@ -136,13 +136,6 @@ main(int argc, char **argv) } else if (argc == 0) usage(); - nfiles = argc; - if (nfiles != 0) { - files = xcalloc(nfiles, sizeof *files); - for (i = 0; i < argc; i++) - open_file(&files[i], argv[i], &width); - } - home = getenv("HOME"); if (home == NULL || *home == '\0') { pw = getpwuid(getuid()); @@ -167,26 +160,17 @@ main(int argc, char **argv) if (f == NULL) err(1, "%s", path); - if (geteuid() == 0) { - pw = getpwnam(FILE_USER); - if (pw == NULL) - errx(1, "unknown user %s", FILE_USER); - if (setgroups(1, &pw->pw_gid) != 0) - err(1, "setgroups"); - if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) - err(1, "setresgid"); - if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) - err(1, "setresuid"); - } - m = magic_load(f, path, cflag || Wflag); if (cflag) { magic_dump(m); exit(0); } - for (i = 0; i < nfiles; i++) - test_file(m, &files[i], width); + for (i = 0; i < argc; i++) { + memset(&inf, 0, sizeof inf); + open_file(&inf, argv[i], &width); + test_file(m, &inf, width); + } exit(0); } @@ -282,7 +266,7 @@ fill_buffer(struct input_file *inf) } if (got == 0) break; - next = (char*)next + got; + next = (char *)next + got; left -= got; } @@ -508,10 +492,14 @@ test_file(struct magic *m, struct input_file *inf, int width) printf("%s\n", inf->result); else printf("%-*s %s\n", width, inf->label, inf->result); + free(inf->result); if (inf->mapped && inf->base != NULL) munmap(inf->base, inf->size); inf->base = NULL; - free(inf->result); + if (inf->fd != -1) + close(inf->fd); + free((void *)inf->label); + free((void *)inf->path); } diff --git a/usr.bin/file/file.h b/usr.bin/file/file.h index 77b6e85da3c..8b433a4712d 100644 --- a/usr.bin/file/file.h +++ b/usr.bin/file/file.h @@ -1,4 +1,4 @@ -/* $OpenBSD: file.h,v 1.25 2015/04/24 16:24:11 nicm Exp $ */ +/* $OpenBSD: file.h,v 1.26 2015/04/24 16:47:32 nicm Exp $ */ /* * Copyright (c) 2015 Nicholas Marriott @@ -22,9 +22,6 @@ /* Bytes to read if can't use the whole file. */ #define FILE_READ_SIZE (256 * 1024) -/* User to drop to if run as root. */ -#define FILE_USER "nobody" - /* text.c */ const char *text_get_type(const void *, size_t); const char *text_try_words(const void *, size_t, int); -- 2.20.1