2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2000, 2001 David O'Brien
5 * Copyright (c) 1996 Søren Schmidt
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/param.h>
33 #include <sys/capsicum.h>
34 #include <sys/elf_common.h>
35 #include <sys/errno.h>
37 #include <capsicum_helpers.h>
46 #include <libcasper.h>
47 #include <casper/cap_fileargs.h>
49 static int elftype(const char *);
50 static const char *iselftype(int);
51 static void printelftypes(void);
52 static void usage(void) __dead2;
58 /* XXX - any more types? */
59 static struct ELFtypes elftypes[] = {
60 { "FreeBSD", ELFOSABI_FREEBSD },
61 { "Linux", ELFOSABI_LINUX },
62 { "Solaris", ELFOSABI_SOLARIS },
63 { "SVR4", ELFOSABI_SYSV }
67 main(int argc, char **argv)
70 const char *strtype = "FreeBSD";
71 int ch, flags, retval, type;
72 bool change, force, listed;
76 type = ELFOSABI_FREEBSD;
82 while ((ch = getopt(argc, argv, "f:lt:v")) != -1)
86 errx(1, "f option incompatible with t option");
89 if (errno == ERANGE || type < 0 || type > 255) {
90 warnx("invalid argument to option f: %s",
104 errx(1, "t option incompatible with f option");
117 warnx("no file(s) specified");
122 if (!force && (type = elftype(strtype)) == -1) {
123 warnx("invalid ELF type '%s'", strtype);
128 flags = change || force ? O_RDWR : O_RDONLY;
129 cap_rights_init(&rights, CAP_READ, CAP_SEEK);
131 cap_rights_set(&rights, CAP_WRITE);
133 fa = fileargs_init(argc, argv, flags, 0, &rights, FA_OPEN);
135 err(1, "unable to init casper");
137 caph_cache_catpages();
138 if (caph_limit_stdio() < 0 || caph_enter_casper() < 0)
139 err(1, "unable to enter capability mode");
143 char buffer[EI_NIDENT];
145 if ((fd = fileargs_open(fa, argv[0])) < 0) {
146 warn("error opening file %s", argv[0]);
150 if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) {
151 warnx("file '%s' too short", argv[0]);
155 if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 ||
156 buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) {
157 warnx("file '%s' is not ELF format", argv[0]);
161 if (!change && !force) {
163 "File '%s' is of brand '%s' (%u).\n",
164 argv[0], iselftype(buffer[EI_OSABI]),
166 if (!iselftype(type)) {
167 warnx("ELF ABI Brand '%u' is unknown",
173 buffer[EI_OSABI] = type;
174 lseek(fd, 0, SEEK_SET);
175 if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) {
176 warn("error writing %s %d", argv[0], fd);
194 (void)fprintf(stderr,
195 "usage: brandelf [-lv] [-f ELF_ABI_number] [-t string] file ...\n");
204 for (elfwalk = 0; elfwalk < nitems(elftypes); elfwalk++)
205 if (etype == elftypes[elfwalk].value)
206 return (elftypes[elfwalk].str);
211 elftype(const char *elfstrtype)
215 for (elfwalk = 0; elfwalk < nitems(elftypes); elfwalk++)
216 if (strcasecmp(elfstrtype, elftypes[elfwalk].str) == 0)
217 return (elftypes[elfwalk].value);
226 fprintf(stderr, "known ELF types are: ");
227 for (elfwalk = 0; elfwalk < nitems(elftypes); elfwalk++)
228 fprintf(stderr, "%s(%u) ", elftypes[elfwalk].str,
229 elftypes[elfwalk].value);
230 fprintf(stderr, "\n");