2006-11-10 23:55:37 +03:00
|
|
|
/* $NetBSD: paxctl.c,v 1.5 2006/11/10 20:55:37 christos Exp $ */
|
2006-05-16 04:08:24 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2006 Elad Efrat <elad@NetBSD.org>
|
|
|
|
* 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.
|
|
|
|
* 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. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Elad Efrat.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2006-11-10 19:31:58 +03:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2006-11-10 23:55:37 +03:00
|
|
|
__RCSID("$NetBSD: paxctl.c,v 1.5 2006/11/10 20:55:37 christos Exp $");
|
2006-11-10 19:31:58 +03:00
|
|
|
#endif /* not lint */
|
2006-05-16 04:08:24 +04:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2006-11-10 23:55:37 +03:00
|
|
|
#if __LP64__
|
|
|
|
#define ELFSIZE 64
|
|
|
|
#else
|
|
|
|
#define ELFSIZE 32
|
|
|
|
#endif
|
2006-05-16 04:08:24 +04:00
|
|
|
#include <elf.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
static void usage(void) __attribute__((__noreturn__));
|
|
|
|
static int pax_flag(const char *);
|
|
|
|
static int pax_flags_sane(u_long);
|
|
|
|
static int pax_haveflags(u_long);
|
|
|
|
static void pax_printflags(u_long);
|
2006-05-16 06:37:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
static const struct paxflag {
|
2006-05-16 04:08:24 +04:00
|
|
|
char mark;
|
2006-11-10 19:31:58 +03:00
|
|
|
const char *name;
|
2006-05-16 04:08:24 +04:00
|
|
|
int bits;
|
|
|
|
} flags[] = {
|
|
|
|
{ 'M', "mprotect(2) restrictions, explicit enable", PF_PAXMPROTECT },
|
|
|
|
{ 'm', "mprotect(2) restrictions, explicit disable", PF_PAXNOMPROTECT },
|
|
|
|
};
|
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
static void
|
2006-05-16 04:08:24 +04:00
|
|
|
usage(void)
|
|
|
|
{
|
2006-11-10 19:31:58 +03:00
|
|
|
(void)fprintf(stderr, "Usage: %s [ <-|+>m | <-|+>M ] <file>\n",
|
|
|
|
getprogname());
|
|
|
|
exit(1);
|
2006-05-16 04:08:24 +04:00
|
|
|
}
|
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
static int
|
|
|
|
pax_flag(const char *s)
|
2006-05-16 04:08:24 +04:00
|
|
|
{
|
2006-11-10 19:31:58 +03:00
|
|
|
size_t i;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
if (s[0] == '\0' || s[1] != '\0')
|
|
|
|
return -1;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
for (i = 0; i < __arraycount(flags); i++)
|
2006-05-16 04:08:24 +04:00
|
|
|
if (*s == flags[i].mark)
|
2006-11-10 19:31:58 +03:00
|
|
|
return flags[i].bits;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
return -1;
|
2006-05-16 04:08:24 +04:00
|
|
|
}
|
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
static int
|
|
|
|
pax_flags_sane(u_long f)
|
2006-05-16 04:08:24 +04:00
|
|
|
{
|
2006-11-10 19:31:58 +03:00
|
|
|
size_t i;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
for (i = 0; i < __arraycount(flags) - 1; i += 2) {
|
|
|
|
int g = flags[i].bits | flags[i+1].bits;
|
|
|
|
if ((f & g) == g)
|
|
|
|
return 0;
|
|
|
|
}
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
return 1;
|
2006-05-16 04:08:24 +04:00
|
|
|
}
|
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
static int
|
|
|
|
pax_haveflags(u_long f)
|
2006-05-16 04:08:24 +04:00
|
|
|
{
|
2006-11-10 19:31:58 +03:00
|
|
|
size_t i;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
for (i = 0; i < __arraycount(flags); i++)
|
2006-05-16 04:08:24 +04:00
|
|
|
if (f & flags[i].bits)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
static void
|
|
|
|
pax_printflags(u_long f)
|
2006-05-16 04:08:24 +04:00
|
|
|
{
|
2006-11-10 19:31:58 +03:00
|
|
|
size_t i;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
for (i = 0; i < __arraycount(flags); i++)
|
2006-05-16 04:08:24 +04:00
|
|
|
if (f & flags[i].bits)
|
2006-11-10 19:31:58 +03:00
|
|
|
(void)printf(" %c: %s\n",
|
|
|
|
flags[i].mark, flags[i].name);
|
2006-05-16 04:08:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
Elf_Ehdr eh;
|
|
|
|
Elf_Phdr ph;
|
|
|
|
char *opt = NULL;
|
2006-09-28 00:01:50 +04:00
|
|
|
int fd, i, add_flags = 0, del_flags = 0, list = 0, ok = 0, flagged = 0;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
opt = argv[i];
|
|
|
|
|
|
|
|
if (*opt == '-' || *opt == '+') {
|
|
|
|
int t;
|
|
|
|
|
|
|
|
t = pax_flag(opt + 1);
|
|
|
|
if (t == -1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (*opt == '-')
|
|
|
|
del_flags |= t;
|
|
|
|
else
|
|
|
|
add_flags |= t;
|
|
|
|
|
|
|
|
opt = NULL;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opt == NULL)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (add_flags || del_flags) {
|
|
|
|
if (list)
|
|
|
|
usage();
|
|
|
|
} else
|
|
|
|
list = 1;
|
|
|
|
|
|
|
|
fd = open(opt, O_RDWR, 0);
|
|
|
|
if (fd == -1) {
|
|
|
|
if (!list || (fd = open(opt, O_RDONLY, 0)) == -1)
|
2006-11-10 19:31:58 +03:00
|
|
|
err(EXIT_FAILURE, "Can't open `%s'", opt);
|
2006-05-16 04:08:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (read(fd, &eh, sizeof(eh)) != sizeof(eh))
|
2006-11-10 19:31:58 +03:00
|
|
|
err(EXIT_FAILURE, "Can't read ELF header from `%s'", opt);
|
2006-05-16 04:08:24 +04:00
|
|
|
|
|
|
|
if (memcmp(eh.e_ident, ELFMAG, SELFMAG) != 0)
|
2006-11-10 19:31:58 +03:00
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"Bad ELF magic from `%s' (maybe it's not an ELF?)", opt);
|
2006-05-16 04:08:24 +04:00
|
|
|
|
|
|
|
for (i = 0; i < eh.e_phnum; i++) {
|
|
|
|
if (pread(fd, &ph, sizeof(ph),
|
|
|
|
eh.e_phoff + i * sizeof(ph)) != sizeof(ph))
|
2006-11-10 19:31:58 +03:00
|
|
|
err(EXIT_FAILURE, "Can't read data from `%s'", opt);
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
if (ph.p_type != PT_NOTE)
|
|
|
|
continue;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
ok = 1;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
if (list) {
|
|
|
|
if (!pax_haveflags((u_long)ph.p_flags))
|
|
|
|
break;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
if (!pax_flags_sane((u_long)ph.p_flags))
|
|
|
|
warnx("Current flags %lx don't make sense",
|
|
|
|
(u_long)ph.p_flags);
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
(void)printf("PaX flags:\n");
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
pax_printflags((u_long)ph.p_flags);
|
2006-09-28 00:01:50 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
flagged = 1;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
break;
|
|
|
|
}
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
ph.p_flags |= add_flags;
|
|
|
|
ph.p_flags &= ~del_flags;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
if (!pax_flags_sane((u_long)ph.p_flags))
|
|
|
|
errx(EXIT_FAILURE, "New flags %lx don't make sense",
|
|
|
|
(u_long)ph.p_flags);
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
if (pwrite(fd, &ph, sizeof(ph),
|
|
|
|
eh.e_phoff + i * sizeof(ph)) != sizeof(ph))
|
|
|
|
err(EXIT_FAILURE, "Can't modify flags on `%s'", opt);
|
|
|
|
|
|
|
|
break;
|
2006-05-16 04:08:24 +04:00
|
|
|
}
|
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
(void)close(fd);
|
|
|
|
|
2006-05-16 04:08:24 +04:00
|
|
|
if (!ok)
|
2006-11-10 19:31:58 +03:00
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"Could not find an ELF PT_NOTE section in `%s'", opt);
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-09-28 00:01:50 +04:00
|
|
|
if (list && !flagged)
|
|
|
|
(void)printf("No PaX flags.\n");
|
2006-11-10 19:31:58 +03:00
|
|
|
return 0;
|
2006-05-16 04:08:24 +04:00
|
|
|
}
|