2 * SPDX-License-Identifier: BSD-2-Clause
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>
51 static bool convert_to_feature_val(const char *, uint32_t *);
52 static bool edit_file_features(Elf *, int, int, char *, bool);
53 static bool get_file_features(Elf *, int, int, uint32_t *, uint64_t *, bool);
54 static void print_features(void);
55 static bool print_file_features(Elf *, int, int, char *, bool);
56 static void usage(void);
58 struct ControlFeatures {
64 static struct ControlFeatures featurelist[] = {
65 { "noaslr", NT_FREEBSD_FCTL_ASLR_DISABLE, "Disable ASLR" },
66 { "noprotmax", NT_FREEBSD_FCTL_PROTMAX_DISABLE,
67 "Disable implicit PROT_MAX" },
68 { "nostackgap", 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" },
73 static struct option long_opts[] = {
74 { "help", no_argument, NULL, 'h' },
78 #if BYTE_ORDER == LITTLE_ENDIAN
79 #define HOST_ENDIAN ELFDATA2LSB
80 #define SWAP_ENDIAN ELFDATA2MSB
82 #define HOST_ENDIAN ELFDATA2MSB
83 #define SWAP_ENDIAN ELFDATA2LSB
89 main(int argc, char **argv)
96 bool editfeatures, lflag, endian_swap;
103 if (elf_version(EV_CURRENT) == EV_NONE)
104 errx(EXIT_FAILURE, "elf_version error");
106 while ((ch = getopt_long(argc, argv, "hile:", long_opts, NULL)) != -1) {
116 if (features != NULL)
117 errx(1, "-e may be specified only once");
132 warnx("no file(s) specified");
140 if ((fd = open(argv[0],
141 editfeatures ? O_RDWR : O_RDONLY, 0)) < 0) {
142 warn("error opening file %s", argv[0]);
147 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
148 warnx("elf_begin failed: %s", elf_errmsg(-1));
153 if ((kind = elf_kind(elf)) != ELF_K_ELF) {
154 if (kind == ELF_K_AR)
155 warnx("file '%s' is an archive", argv[0]);
157 warnx("file '%s' is not an ELF file", argv[0]);
162 if (gelf_getehdr(elf, &ehdr) == NULL) {
163 warnx("gelf_getehdr: %s", elf_errmsg(-1));
168 if (ehdr.e_ident[EI_DATA] == HOST_ENDIAN) {
170 } else if (ehdr.e_ident[EI_DATA] == SWAP_ENDIAN) {
173 warnx("file endianness unknown");
179 if (!print_file_features(elf, ehdr.e_phnum, fd,
180 argv[0], endian_swap)) {
184 } else if (!edit_file_features(elf, ehdr.e_phnum, fd,
185 features, endian_swap)) {
203 #define USAGE_MESSAGE \
205 Usage: %s [options] file...\n\
206 Set or display the control features for an ELF object.\n\n\
207 Supported options are:\n\
208 -l List known control features.\n\
209 -i Ignore unknown features.\n\
210 -e [+-=]feature,list Edit features from a comma separated list.\n\
211 -h | --help Print a usage message and exit.\n"
217 fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
222 convert_to_feature_val(const char *feature_str, uint32_t *feature_val)
224 char *feature, *feature_tmp;
230 operation = *feature_str;
233 if (operation != '+' && operation != '-' && operation != '=')
234 errx(1, "'%c' not an operator - use '+', '-', '='", operation);
236 if ((feature_tmp = strdup(feature_str)) == NULL)
238 len = nitems(featurelist);
239 while ((feature = strsep(&feature_tmp, ",")) != NULL) {
240 for (i = 0; i < len; ++i) {
241 if (strcmp(featurelist[i].alias, feature) == 0) {
242 input |= featurelist[i].value;
245 /* XXX Backwards compatibility for "no"-prefix flags. */
246 if (strncmp(featurelist[i].alias, "no", 2) == 0 &&
247 strcmp(featurelist[i].alias + 2, feature) == 0) {
248 input |= featurelist[i].value;
250 "interpreting %s as %s; please specify %s",
251 feature, featurelist[i].alias,
252 featurelist[i].alias);
257 if (isdigit(feature[0])) {
259 unsigned long long val;
262 val = strtoll(feature, &eptr, 0);
263 if (eptr == feature || *eptr != '\0')
265 else if (val > UINT32_MAX)
268 warn("%s invalid", feature);
274 warnx("%s is not a valid feature", feature);
283 if (operation == '+') {
284 *feature_val |= input;
285 } else if (operation == '=') {
286 *feature_val = input;
287 } else if (operation == '-') {
288 *feature_val &= ~input;
295 edit_file_features(Elf *elf, int phcount, int fd, char *val, bool endian_swap)
297 uint32_t features, prev_features;
300 if (!get_file_features(elf, phcount, fd, &features, &off,
302 warnx("NT_FREEBSD_FEATURE_CTL note not found");
306 prev_features = features;
307 if (!convert_to_feature_val(val, &features))
309 /* Avoid touching file if no change. */
310 if (features == prev_features)
314 features = bswap32(features);
316 if (lseek(fd, off, SEEK_SET) == -1 ||
317 write(fd, &features, sizeof(features)) <
318 (ssize_t)sizeof(features)) {
319 warnx("error writing feature value");
330 printf("Known features are:\n");
331 for (i = 0; i < nitems(featurelist); ++i)
332 printf("%-16s%s\n", featurelist[i].alias,
333 featurelist[i].desc);
337 print_file_features(Elf *elf, int phcount, int fd, char *filename,
343 if (!get_file_features(elf, phcount, fd, &features, NULL,
348 printf("File '%s' features:\n", filename);
349 for (i = 0; i < nitems(featurelist); ++i) {
350 printf("%-16s'%s' is ", featurelist[i].alias,
351 featurelist[i].desc);
353 if ((featurelist[i].value & features) == 0)
362 get_file_features(Elf *elf, int phcount, int fd, uint32_t *features,
363 uint64_t *off, bool endian_swap)
367 unsigned long read_total;
368 int namesz, descsz, i;
372 * Go through each program header to find one that is of type PT_NOTE
373 * and has a note for feature control.
375 for (i = 0; i < phcount; ++i) {
376 if (gelf_getphdr(elf, i, &phdr) == NULL) {
377 warnx("gelf_getphdr failed: %s", elf_errmsg(-1));
381 if (phdr.p_type != PT_NOTE)
384 if (lseek(fd, phdr.p_offset, SEEK_SET) < 0) {
385 warn("lseek() failed:");
390 while (read_total < phdr.p_filesz) {
391 if (read(fd, ¬e, sizeof(note)) <
392 (ssize_t)sizeof(note)) {
393 warnx("elf note header too short");
396 read_total += sizeof(note);
399 note.n_namesz = bswap32(note.n_namesz);
400 note.n_descsz = bswap32(note.n_descsz);
401 note.n_type = bswap32(note.n_type);
405 * XXX: Name and descriptor are 4 byte aligned, however,
406 * the size given doesn't include the padding.
408 namesz = roundup2(note.n_namesz, 4);
409 name = malloc(namesz);
411 warn("malloc() failed.");
414 descsz = roundup2(note.n_descsz, 4);
415 if (read(fd, name, namesz) < namesz) {
416 warnx("elf note name too short");
420 read_total += namesz;
422 if (note.n_namesz != 8 ||
423 strncmp("FreeBSD", name, 7) != 0 ||
424 note.n_type != NT_FREEBSD_FEATURE_CTL) {
425 /* Not the right note. Skip the description */
426 if (lseek(fd, descsz, SEEK_CUR) < 0) {
427 warn("lseek() failed.");
431 read_total += descsz;
436 if (note.n_descsz < sizeof(uint32_t)) {
437 warnx("Feature descriptor can't "
438 "be less than 4 bytes");
444 * XXX: For now we look at only 4 bytes of the
445 * descriptor. This should respect descsz.
447 if (note.n_descsz > sizeof(uint32_t))
448 warnx("Feature note is bigger than expected");
449 if (read(fd, features, sizeof(uint32_t)) <
450 (ssize_t)sizeof(uint32_t)) {
451 warnx("feature note data too short");
456 *features = bswap32(*features);
458 *off = phdr.p_offset + read_total;
464 warnx("NT_FREEBSD_FEATURE_CTL note not found");