2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 The FreeBSD Foundation.
6 * This software was developed by Bora Ozarslan under sponsorship from
7 * the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <sys/elf_common.h>
33 #include <sys/endian.h>
49 __FBSDID("$FreeBSD$");
51 static bool convert_to_feature_val(char *, uint32_t *);
52 static bool edit_file_features(Elf *, int, int, char *);
53 static bool get_file_features(Elf *, int, int, uint32_t *, uint64_t *);
54 static void print_features(void);
55 static bool print_file_features(Elf *, int, int, char *);
56 static void usage(void);
58 struct ControlFeatures {
64 static struct ControlFeatures featurelist[] = {
65 { "aslr", NT_FREEBSD_FCTL_ASLR_DISABLE, "Disable ASLR" },
66 { "protmax", NT_FREEBSD_FCTL_PROTMAX_DISABLE,
67 "Disable implicit PROT_MAX" },
68 { "stackgap", NT_FREEBSD_FCTL_STKGAP_DISABLE, "Disable stack gap" },
69 { "wxneeded", NT_FREEBSD_FCTL_WXNEEDED, "Requires W+X mappings" },
70 { "la48", NT_FREEBSD_FCTL_LA48, "amd64: Limit user VA to 48bit" },
71 { "aslrstkgap", NT_FREEBSD_FCTL_ASG_DISABLE, "Disable ASLR stack gap" },
74 static struct option long_opts[] = {
75 { "help", no_argument, NULL, 'h' },
79 #if BYTE_ORDER == LITTLE_ENDIAN
80 #define SUPPORTED_ENDIAN ELFDATA2LSB
82 #define SUPPORTED_ENDIAN ELFDATA2MSB
86 main(int argc, char **argv)
93 bool editfeatures, lflag;
100 if (elf_version(EV_CURRENT) == EV_NONE)
101 errx(EXIT_FAILURE, "elf_version error");
103 while ((ch = getopt_long(argc, argv, "hle:", long_opts, NULL)) != -1) {
124 warnx("no file(s) specified");
132 if ((fd = open(argv[0],
133 editfeatures ? O_RDWR : O_RDONLY, 0)) < 0) {
134 warn("error opening file %s", argv[0]);
139 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
140 warnx("elf_begin failed: %s", elf_errmsg(-1));
145 if ((kind = elf_kind(elf)) != ELF_K_ELF) {
146 if (kind == ELF_K_AR)
147 warnx("file '%s' is an archive", argv[0]);
149 warnx("file '%s' is not an ELF file", argv[0]);
154 if (gelf_getehdr(elf, &ehdr) == NULL) {
155 warnx("gelf_getehdr: %s", elf_errmsg(-1));
160 * XXX need to support cross-endian operation, but for now
161 * exit on error rather than misbehaving.
163 if (ehdr.e_ident[EI_DATA] != SUPPORTED_ENDIAN) {
164 warnx("file endianness must match host");
170 if (!print_file_features(elf, ehdr.e_phnum, fd,
175 } else if (!edit_file_features(elf, ehdr.e_phnum, fd,
194 #define USAGE_MESSAGE \
196 Usage: %s [options] file...\n\
197 Set or display the control features for an ELF object.\n\n\
198 Supported options are:\n\
199 -l List known control features.\n\
200 -e [+-=]feature,list Edit features from a comma separated list.\n\
201 -h | --help Print a usage message and exit.\n"
207 fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
212 convert_to_feature_val(char *feature_str, uint32_t *feature_val)
220 operation = *feature_str;
222 len = nitems(featurelist);
223 while ((feature = strsep(&feature_str, ",")) != NULL) {
224 for (i = 0; i < len; ++i) {
225 if (strcmp(featurelist[i].alias, feature) == 0) {
226 input |= featurelist[i].value;
231 warnx("%s is not a valid feature", feature);
236 if (operation == '+') {
237 *feature_val |= input;
238 } else if (operation == '=') {
239 *feature_val = input;
240 } else if (operation == '-') {
241 *feature_val &= ~input;
243 warnx("'%c' not an operator - use '+', '-', '='",
251 edit_file_features(Elf *elf, int phcount, int fd, char *val)
256 if (!get_file_features(elf, phcount, fd, &features, &off)) {
257 warnx("NT_FREEBSD_FEATURE_CTL note not found");
261 if (!convert_to_feature_val(val, &features))
264 if (lseek(fd, off, SEEK_SET) == -1 ||
265 write(fd, &features, sizeof(features)) <
266 (ssize_t)sizeof(features)) {
267 warnx("error writing feature value");
278 printf("Known features are:\n");
279 for (i = 0; i < nitems(featurelist); ++i)
280 printf("%-16s%s\n", featurelist[i].alias,
281 featurelist[i].desc);
285 print_file_features(Elf *elf, int phcount, int fd, char *filename)
290 if (!get_file_features(elf, phcount, fd, &features, NULL)) {
294 printf("File '%s' features:\n", filename);
295 for (i = 0; i < nitems(featurelist); ++i) {
296 printf("%-16s'%s' is ", featurelist[i].alias,
297 featurelist[i].desc);
299 if ((featurelist[i].value & features) == 0)
308 get_file_features(Elf *elf, int phcount, int fd, uint32_t *features,
313 unsigned long read_total;
314 int namesz, descsz, i;
318 * Go through each program header to find one that is of type PT_NOTE
319 * and has a note for feature control.
321 for (i = 0; i < phcount; ++i) {
322 if (gelf_getphdr(elf, i, &phdr) == NULL) {
323 warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
327 if (phdr.p_type != PT_NOTE)
330 if (lseek(fd, phdr.p_offset, SEEK_SET) < 0) {
331 warn("lseek() failed:");
336 while (read_total < phdr.p_filesz) {
337 if (read(fd, ¬e, sizeof(note)) <
338 (ssize_t)sizeof(note)) {
339 warnx("elf note header too short");
342 read_total += sizeof(note);
345 * XXX: Name and descriptor are 4 byte aligned, however,
346 * the size given doesn't include the padding.
348 namesz = roundup2(note.n_namesz, 4);
349 name = malloc(namesz);
351 warn("malloc() failed.");
354 descsz = roundup2(note.n_descsz, 4);
355 if (read(fd, name, namesz) < namesz) {
356 warnx("elf note name too short");
360 read_total += namesz;
362 if (note.n_namesz != 8 ||
363 strncmp("FreeBSD", name, 7) != 0 ||
364 note.n_type != NT_FREEBSD_FEATURE_CTL) {
365 /* Not the right note. Skip the description */
366 if (lseek(fd, descsz, SEEK_CUR) < 0) {
367 warn("lseek() failed.");
371 read_total += descsz;
376 if (note.n_descsz < sizeof(uint32_t)) {
377 warnx("Feature descriptor can't "
378 "be less than 4 bytes");
384 * XXX: For now we look at only 4 bytes of the
385 * descriptor. This should respect descsz.
387 if (note.n_descsz > sizeof(uint32_t))
388 warnx("Feature note is bigger than expected");
389 if (read(fd, features, sizeof(uint32_t)) <
390 (ssize_t)sizeof(uint32_t)) {
391 warnx("feature note data too short");
396 *off = phdr.p_offset + read_total;
402 warnx("NT_FREEBSD_FEATURE_CTL note not found");