1996-10-16 18:16:22 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2001-06-22 21:43:51 +00:00
|
|
|
* Copyright (c) 2000, 2001 David O'Brien
|
2011-12-30 10:59:15 +00:00
|
|
|
* Copyright (c) 1996 Søren Schmidt
|
1996-10-16 18:16:22 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer
|
|
|
|
* in this position and unchanged.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
2002-06-02 20:05:59 +00:00
|
|
|
* derived from this software without specific prior written permission
|
1996-10-16 18:16:22 +00:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 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 SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-12-03 00:00:22 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
2001-05-02 23:57:49 +00:00
|
|
|
|
2018-11-18 12:03:12 +00:00
|
|
|
#include <sys/param.h>
|
2018-11-18 12:23:03 +00:00
|
|
|
#include <sys/capsicum.h>
|
2001-05-02 23:56:21 +00:00
|
|
|
#include <sys/elf_common.h>
|
2001-12-03 00:00:22 +00:00
|
|
|
#include <sys/errno.h>
|
2018-11-18 12:03:12 +00:00
|
|
|
|
2018-11-18 12:23:03 +00:00
|
|
|
#include <capsicum_helpers.h>
|
2001-12-03 00:00:22 +00:00
|
|
|
#include <err.h>
|
1997-05-21 23:07:31 +00:00
|
|
|
#include <fcntl.h>
|
2018-11-18 12:03:12 +00:00
|
|
|
#include <stdbool.h>
|
1996-10-16 18:16:22 +00:00
|
|
|
#include <stdio.h>
|
1997-05-21 23:07:31 +00:00
|
|
|
#include <stdlib.h>
|
1997-02-09 20:35:54 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1996-10-16 18:16:22 +00:00
|
|
|
|
2018-11-18 12:23:03 +00:00
|
|
|
#include <libcasper.h>
|
|
|
|
#include <casper/cap_fileargs.h>
|
|
|
|
|
2000-04-18 02:39:26 +00:00
|
|
|
static int elftype(const char *);
|
|
|
|
static const char *iselftype(int);
|
2000-01-04 02:33:54 +00:00
|
|
|
static void printelftypes(void);
|
2001-12-03 00:00:22 +00:00
|
|
|
static void usage(void);
|
1996-10-16 18:16:22 +00:00
|
|
|
|
2000-04-18 02:39:26 +00:00
|
|
|
struct ELFtypes {
|
|
|
|
const char *str;
|
|
|
|
int value;
|
|
|
|
};
|
|
|
|
/* XXX - any more types? */
|
|
|
|
static struct ELFtypes elftypes[] = {
|
|
|
|
{ "FreeBSD", ELFOSABI_FREEBSD },
|
2000-07-01 05:48:33 +00:00
|
|
|
{ "Linux", ELFOSABI_LINUX },
|
|
|
|
{ "Solaris", ELFOSABI_SOLARIS },
|
|
|
|
{ "SVR4", ELFOSABI_SYSV }
|
2000-04-18 02:39:26 +00:00
|
|
|
};
|
|
|
|
|
1997-02-09 20:35:54 +00:00
|
|
|
int
|
1996-10-16 18:16:22 +00:00
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
|
2000-04-18 02:39:26 +00:00
|
|
|
const char *strtype = "FreeBSD";
|
2018-11-18 12:23:03 +00:00
|
|
|
int ch, flags, retval, type;
|
2018-11-18 12:03:12 +00:00
|
|
|
bool change, force, listed;
|
2018-11-18 12:23:03 +00:00
|
|
|
fileargs_t *fa;
|
|
|
|
cap_rights_t rights;
|
2018-11-18 12:03:12 +00:00
|
|
|
|
|
|
|
type = ELFOSABI_FREEBSD;
|
|
|
|
retval = 0;
|
|
|
|
change = false;
|
|
|
|
force = false;
|
|
|
|
listed = false;
|
1996-10-16 18:16:22 +00:00
|
|
|
|
2000-04-18 02:39:26 +00:00
|
|
|
while ((ch = getopt(argc, argv, "f:lt:v")) != -1)
|
1996-10-16 18:16:22 +00:00
|
|
|
switch (ch) {
|
1998-04-21 02:31:09 +00:00
|
|
|
case 'f':
|
2000-04-18 02:39:26 +00:00
|
|
|
if (change)
|
2001-02-06 12:05:58 +00:00
|
|
|
errx(1, "f option incompatible with t option");
|
2018-11-18 12:03:12 +00:00
|
|
|
force = true;
|
2000-04-18 02:39:26 +00:00
|
|
|
type = atoi(optarg);
|
|
|
|
if (errno == ERANGE || type < 0 || type > 255) {
|
|
|
|
warnx("invalid argument to option f: %s",
|
|
|
|
optarg);
|
|
|
|
usage();
|
|
|
|
}
|
1998-04-21 02:31:09 +00:00
|
|
|
break;
|
2000-01-04 02:33:54 +00:00
|
|
|
case 'l':
|
|
|
|
printelftypes();
|
2018-11-18 12:03:12 +00:00
|
|
|
listed = true;
|
2000-01-04 02:33:54 +00:00
|
|
|
break;
|
1996-10-16 18:16:22 +00:00
|
|
|
case 'v':
|
2012-01-10 02:58:29 +00:00
|
|
|
/* does nothing */
|
1996-10-16 18:16:22 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
2000-04-18 02:39:26 +00:00
|
|
|
if (force)
|
2001-02-06 12:05:58 +00:00
|
|
|
errx(1, "t option incompatible with f option");
|
2018-11-18 12:03:12 +00:00
|
|
|
change = true;
|
2000-04-18 02:39:26 +00:00
|
|
|
strtype = optarg;
|
1996-10-16 18:16:22 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2018-11-18 12:03:12 +00:00
|
|
|
if (argc == 0) {
|
2000-01-04 02:33:54 +00:00
|
|
|
if (listed)
|
|
|
|
exit(0);
|
|
|
|
else {
|
|
|
|
warnx("no file(s) specified");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
1998-04-21 02:31:09 +00:00
|
|
|
|
2000-04-18 02:39:26 +00:00
|
|
|
if (!force && (type = elftype(strtype)) == -1) {
|
|
|
|
warnx("invalid ELF type '%s'", strtype);
|
2000-01-04 02:33:54 +00:00
|
|
|
printelftypes();
|
|
|
|
usage();
|
|
|
|
}
|
1998-04-21 02:31:09 +00:00
|
|
|
|
2018-11-18 12:23:03 +00:00
|
|
|
flags = change || force ? O_RDWR : O_RDONLY;
|
|
|
|
cap_rights_init(&rights, CAP_READ, CAP_SEEK);
|
|
|
|
if (flags == O_RDWR)
|
|
|
|
cap_rights_set(&rights, CAP_WRITE);
|
|
|
|
|
2019-04-17 16:18:14 +00:00
|
|
|
fa = fileargs_init(argc, argv, flags, 0, &rights, FA_OPEN);
|
2018-11-18 12:23:03 +00:00
|
|
|
if (fa == NULL)
|
2019-11-06 01:42:30 +00:00
|
|
|
err(1, "unable to init casper");
|
2018-11-18 12:23:03 +00:00
|
|
|
|
|
|
|
caph_cache_catpages();
|
|
|
|
if (caph_limit_stdio() < 0 || caph_enter_casper() < 0)
|
|
|
|
err(1, "unable to enter capability mode");
|
|
|
|
|
2018-11-18 12:03:12 +00:00
|
|
|
while (argc != 0) {
|
1996-10-16 18:16:22 +00:00
|
|
|
int fd;
|
1997-05-21 23:07:31 +00:00
|
|
|
char buffer[EI_NIDENT];
|
1996-10-16 18:16:22 +00:00
|
|
|
|
2018-11-18 12:23:03 +00:00
|
|
|
if ((fd = fileargs_open(fa, argv[0])) < 0) {
|
1997-08-23 15:51:14 +00:00
|
|
|
warn("error opening file %s", argv[0]);
|
1997-02-09 20:35:54 +00:00
|
|
|
retval = 1;
|
1996-10-16 18:16:22 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
1997-05-21 23:07:31 +00:00
|
|
|
if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) {
|
1997-06-23 06:47:12 +00:00
|
|
|
warnx("file '%s' too short", argv[0]);
|
1997-02-09 20:35:54 +00:00
|
|
|
retval = 1;
|
1996-10-16 18:16:22 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 ||
|
|
|
|
buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) {
|
1997-06-23 06:47:12 +00:00
|
|
|
warnx("file '%s' is not ELF format", argv[0]);
|
1997-02-09 20:35:54 +00:00
|
|
|
retval = 1;
|
1996-10-16 18:16:22 +00:00
|
|
|
goto fail;
|
1998-04-21 02:31:09 +00:00
|
|
|
}
|
2000-04-18 02:39:26 +00:00
|
|
|
if (!change && !force) {
|
|
|
|
fprintf(stdout,
|
|
|
|
"File '%s' is of brand '%s' (%u).\n",
|
|
|
|
argv[0], iselftype(buffer[EI_OSABI]),
|
|
|
|
buffer[EI_OSABI]);
|
|
|
|
if (!iselftype(type)) {
|
|
|
|
warnx("ELF ABI Brand '%u' is unknown",
|
|
|
|
type);
|
|
|
|
printelftypes();
|
1996-10-16 18:16:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2000-04-18 02:39:26 +00:00
|
|
|
buffer[EI_OSABI] = type;
|
1996-10-16 18:16:22 +00:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
1997-05-21 23:07:31 +00:00
|
|
|
if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) {
|
2000-04-18 02:39:26 +00:00
|
|
|
warn("error writing %s %d", argv[0], fd);
|
1997-06-23 06:47:12 +00:00
|
|
|
retval = 1;
|
1996-10-16 18:16:22 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fail:
|
2000-07-02 03:34:08 +00:00
|
|
|
close(fd);
|
1996-10-16 18:16:22 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
1997-02-09 20:35:54 +00:00
|
|
|
|
2018-11-18 12:23:03 +00:00
|
|
|
fileargs_free(fa);
|
2018-11-18 12:03:12 +00:00
|
|
|
return (retval);
|
1996-10-16 18:16:22 +00:00
|
|
|
}
|
|
|
|
|
1997-06-23 06:47:12 +00:00
|
|
|
static void
|
2001-12-03 00:00:22 +00:00
|
|
|
usage(void)
|
1996-10-16 18:16:22 +00:00
|
|
|
{
|
2005-05-21 09:55:10 +00:00
|
|
|
(void)fprintf(stderr,
|
|
|
|
"usage: brandelf [-lv] [-f ELF_ABI_number] [-t string] file ...\n");
|
1997-02-09 20:35:54 +00:00
|
|
|
exit(1);
|
1996-10-16 18:16:22 +00:00
|
|
|
}
|
1998-04-21 02:31:09 +00:00
|
|
|
|
2000-04-18 02:39:26 +00:00
|
|
|
static const char *
|
2001-06-22 21:43:51 +00:00
|
|
|
iselftype(int etype)
|
2000-04-18 02:39:26 +00:00
|
|
|
{
|
2001-06-22 21:43:51 +00:00
|
|
|
size_t elfwalk;
|
2000-04-18 02:39:26 +00:00
|
|
|
|
2018-11-18 12:03:12 +00:00
|
|
|
for (elfwalk = 0; elfwalk < nitems(elftypes); elfwalk++)
|
2001-06-22 21:43:51 +00:00
|
|
|
if (etype == elftypes[elfwalk].value)
|
2018-11-18 12:03:12 +00:00
|
|
|
return (elftypes[elfwalk].str);
|
|
|
|
return (0);
|
2000-04-18 02:39:26 +00:00
|
|
|
}
|
2000-01-04 02:33:54 +00:00
|
|
|
|
1998-04-21 02:44:12 +00:00
|
|
|
static int
|
2000-04-18 02:39:26 +00:00
|
|
|
elftype(const char *elfstrtype)
|
2000-01-04 02:33:54 +00:00
|
|
|
{
|
2001-06-22 21:43:51 +00:00
|
|
|
size_t elfwalk;
|
1998-04-21 02:31:09 +00:00
|
|
|
|
2018-11-18 12:03:12 +00:00
|
|
|
for (elfwalk = 0; elfwalk < nitems(elftypes); elfwalk++)
|
2005-05-02 17:19:33 +00:00
|
|
|
if (strcasecmp(elfstrtype, elftypes[elfwalk].str) == 0)
|
2018-11-18 12:03:12 +00:00
|
|
|
return (elftypes[elfwalk].value);
|
|
|
|
return (-1);
|
1998-04-21 02:31:09 +00:00
|
|
|
}
|
2000-01-04 02:33:54 +00:00
|
|
|
|
|
|
|
static void
|
2001-12-03 00:00:22 +00:00
|
|
|
printelftypes(void)
|
2000-01-04 02:33:54 +00:00
|
|
|
{
|
2001-06-22 21:43:51 +00:00
|
|
|
size_t elfwalk;
|
2000-01-04 02:33:54 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "known ELF types are: ");
|
2018-11-18 12:03:12 +00:00
|
|
|
for (elfwalk = 0; elfwalk < nitems(elftypes); elfwalk++)
|
|
|
|
fprintf(stderr, "%s(%u) ", elftypes[elfwalk].str,
|
|
|
|
elftypes[elfwalk].value);
|
2000-01-04 02:33:54 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|