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/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/capsicum.h>
35 #include <sys/elf_common.h>
36 #include <sys/errno.h>
38 #include <capsicum_helpers.h>
47 #include <libcasper.h>
48 #include <casper/cap_fileargs.h>
50 static int elftype(const char *);
51 static const char *iselftype(int);
52 static void printelftypes(void);
53 static void usage(void) __dead2;
59 /* XXX - any more types? */
60 static struct ELFtypes elftypes[] = {
61 { "FreeBSD", ELFOSABI_FREEBSD },
62 { "Linux", ELFOSABI_LINUX },
63 { "Solaris", ELFOSABI_SOLARIS },
64 { "SVR4", ELFOSABI_SYSV }
68 main(int argc, char **argv)
71 const char *strtype = "FreeBSD";
72 int ch, flags, retval, type;
73 bool change, force, listed;
77 type = ELFOSABI_FREEBSD;
83 while ((ch = getopt(argc, argv, "f:lt:v")) != -1)
87 errx(1, "f option incompatible with t option");
90 if (errno == ERANGE || type < 0 || type > 255) {
91 warnx("invalid argument to option f: %s",
105 errx(1, "t option incompatible with f option");
118 warnx("no file(s) specified");
123 if (!force && (type = elftype(strtype)) == -1) {
124 warnx("invalid ELF type '%s'", strtype);
129 flags = change || force ? O_RDWR : O_RDONLY;
130 cap_rights_init(&rights, CAP_READ, CAP_SEEK);
132 cap_rights_set(&rights, CAP_WRITE);
134 fa = fileargs_init(argc, argv, flags, 0, &rights, FA_OPEN);
136 err(1, "unable to init casper");
138 caph_cache_catpages();
139 if (caph_limit_stdio() < 0 || caph_enter_casper() < 0)
140 err(1, "unable to enter capability mode");
144 char buffer[EI_NIDENT];
146 if ((fd = fileargs_open(fa, argv[0])) < 0) {
147 warn("error opening file %s", argv[0]);
151 if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) {
152 warnx("file '%s' too short", argv[0]);
156 if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 ||
157 buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) {
158 warnx("file '%s' is not ELF format", argv[0]);
162 if (!change && !force) {
164 "File '%s' is of brand '%s' (%u).\n",
165 argv[0], iselftype(buffer[EI_OSABI]),
167 if (!iselftype(type)) {
168 warnx("ELF ABI Brand '%u' is unknown",
174 buffer[EI_OSABI] = type;
175 lseek(fd, 0, SEEK_SET);
176 if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) {
177 warn("error writing %s %d", argv[0], fd);
195 (void)fprintf(stderr,
196 "usage: brandelf [-lv] [-f ELF_ABI_number] [-t string] file ...\n");
205 for (elfwalk = 0; elfwalk < nitems(elftypes); elfwalk++)
206 if (etype == elftypes[elfwalk].value)
207 return (elftypes[elfwalk].str);
212 elftype(const char *elfstrtype)
216 for (elfwalk = 0; elfwalk < nitems(elftypes); elfwalk++)
217 if (strcasecmp(elfstrtype, elftypes[elfwalk].str) == 0)
218 return (elftypes[elfwalk].value);
227 fprintf(stderr, "known ELF types are: ");
228 for (elfwalk = 0; elfwalk < nitems(elftypes); elfwalk++)
229 fprintf(stderr, "%s(%u) ", elftypes[elfwalk].str,
230 elftypes[elfwalk].value);
231 fprintf(stderr, "\n");