run so that this locore0.o is always at the start of the executable.
But randomize the link order of all other .o files in the kernel, so
that their exec/rodata/data/bss segments land all over the place.
Late during kernel boot, smash the startup code with traps so that
it does not point to the other randomly placed code. It has be smashed,
because loongson runs in the kseg0 space.
As a result, the internal layout of every newly build bsd kernel is
different from past kernels. Internal relative offsets are not known
to an outside attacker.
Ramdisk kernels cannot be compiled like this, because they are gzip'd.
When the internal pointer references change, the compression dictionary
bloats and results in poorer compression.
-# $OpenBSD: Makefile.loongson,v 1.56 2017/06/05 12:43:59 deraadt Exp $
+# $OpenBSD: Makefile.loongson,v 1.57 2017/06/08 11:44:00 visa Exp $
# For instructions on building kernels consult the config(8) and options(4)
# manual pages.
CMACHFLAGS= -mno-abicalls ${ABI} -msoft-float -Wa,-mfix-loongson2f-btb -G 0
CMACHFLAGS+= -ffreestanding ${NOPIE_FLAGS}
+SORTR= sort -R
.if ${IDENT:M-DNO_PROPOLICE}
CMACHFLAGS+= -fno-stack-protector
.endif
+.if ${IDENT:M-DSMALL_KERNEL}
+SORTR= cat
+.endif
DEBUG?= -g
COPTS?= -O2
# ${SYSTEM_LD_HEAD}
# ${SYSTEM_LD} swapxxx.o
# ${SYSTEM_LD_TAIL}
-SYSTEM_HEAD= locore.o param.o ioconf.o
-SYSTEM_OBJ= ${SYSTEM_HEAD} ${OBJS}
+SYSTEM_HEAD= locore0.o gap.o
+SYSTEM_OBJ= ${SYSTEM_HEAD} ${OBJS} param.o ioconf.o
SYSTEM_DEP= Makefile ${SYSTEM_OBJ} ld.script
SYSTEM_LD_HEAD= @rm -f $@
SYSTEM_LD= @echo ${LD} ${LINKFLAGS} -o $@ '$${SYSTEM_HEAD} vers.o $${OBJS}'; \
umask 007; \
- ${LD} ${LINKFLAGS} -o $@ ${SYSTEM_HEAD} vers.o ${OBJS}
+ echo ${OBJS} param.o ioconf.o vers.o | tr " " "\n" | ${SORTR} > lorder; \
+ ${LD} ${LINKFLAGS} -o $@ ${SYSTEM_HEAD} `cat lorder`
SYSTEM_LD_TAIL= @${SIZE} $@
.if ${DEBUG} == "-g"
sh $S/conf/newvers.sh
${CC} ${CFLAGS} ${CPPFLAGS} ${PROF} -c vers.c
+gap.S: ${SYSTEM_SWAP_DEP} Makefile $S/conf/makegap.sh
+ umask 007; sh $S/conf/makegap.sh 0xef > gap.S
+
+gap.o: gap.S
+ umask 007; ${CC} ${AFLAGS} ${CPPFLAGS} ${PROF} -c gap.S
+
clean:
- rm -f *bsd *bsd.gdb *.[dio] [a-z]*.s assym.* ${DB_STRUCTINFO} param.c
+ rm -f *bsd *bsd.gdb *.[dio] [a-z]*.s assym.* ${DB_STRUCTINFO} \
+ gap.S lorder param.c
cleandir: clean
rm -f Makefile *.h ioconf.c options machine ${_mach} vers.c
objdump -g db_structinfo.o | perl $S/ddb/parse_structinfo.pl > $@
rm -f db_structinfo.o
-locore.o: ${_machdir}/${_mach}/locore.S assym.h
-context.o cp0access.o exception.o: assym.h
+locore0.o: ${_machdir}/${_mach}/locore0.S assym.h
+context.o cp0access.o exception.o locore.o: assym.h
lcore_access.o lcore_ddb.o lcore_float.o tlbhandler.o: assym.h
pmon32.o: assym.h
-# $OpenBSD: files.loongson,v 1.24 2016/11/17 14:41:21 visa Exp $
+# $OpenBSD: files.loongson,v 1.25 2017/06/08 11:44:00 visa Exp $
# Standard stanzas config(8) can't run without
maxpartitions 16
file arch/loongson/loongson/generic2e_machdep.c cpu_loongson2
file arch/loongson/loongson/generic3a_machdep.c cpu_loongson3
file arch/loongson/loongson/isa_machdep.c isa
+file arch/loongson/loongson/locore.S
file arch/loongson/loongson/loongson2_machdep.c
file arch/loongson/loongson/loongson3_intr.c cpu_loongson3
file arch/loongson/loongson/loongson3_machdep.c cpu_loongson3
-/* $OpenBSD: autoconf.c,v 1.6 2013/06/02 21:46:04 pirofti Exp $ */
+/* $OpenBSD: autoconf.c,v 1.7 2017/06/08 11:44:00 visa Exp $ */
/*
* Copyright (c) 2009 Miodrag Vallat.
*
extern char pmon_bootp[];
+void
+unmap_startup(void)
+{
+ extern uint32_t kernel_text[], endboot[];
+ uint32_t *word = kernel_text;
+
+ /* Cannot unmap kseg0; smash with trap. */
+ while (word < endboot)
+ *word++ = 0x00000034u; /* TEQ zero, zero */
+}
+
void
cpu_configure(void)
{
softintr_init();
(void)config_rootfound("mainbus", NULL);
+ unmap_startup();
+
splinit();
cold = 0;
}
-/* $OpenBSD: locore.S,v 1.7 2017/01/19 15:09:04 visa Exp $ */
+/* $OpenBSD: locore.S,v 1.8 2017/06/08 11:44:00 visa Exp $ */
/*
* Copyright (c) 2001-2004 Opsycon AB (www.opsycon.se / www.opsycon.com)
.set noreorder # Noreorder is default style!
- .globl start
- .globl kernel_text
-kernel_text = start
-start:
+ .globl locore_start
+ .ent locore_start, 0
+locore_start:
mfc0 v0, COP_0_STATUS_REG
li v1, ~SR_INT_ENAB
and v0, v1
PTR_L sp, 0(sp)
jr ra
nop
+ .end locore_start
#ifdef HIBERNATE
--- /dev/null
+/* $OpenBSD: locore0.S,v 1.1 2017/06/08 11:44:00 visa Exp $ */
+
+/*
+ * Copyright (c) 2001-2004 Opsycon AB (www.opsycon.se / www.opsycon.com)
+ *
+ * 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.
+ *
+ * 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.
+ *
+ */
+
+#include <machine/param.h>
+#include <machine/asm.h>
+
+#include "assym.h"
+
+ .set noreorder # Noreorder is default style!
+
+ .globl kernel_text
+ .globl start
+ .ent start, 0
+kernel_text = start
+start:
+ j locore_start
+ nop
+ .end start