Commit Graph

1608 Commits

Author SHA1 Message Date
maxv
ed6d73e306 Make sure we're dealing with a static binary. Otherwise we could crash if
the user mistakenly tries to boot a KASLR kernel with 'boot' instead of
'pkboot'. Now we fail cleanly. Reported by cryo@.
2019-10-17 14:00:28 +00:00
mrg
21303c93e9 convert HAVE_GCC == 7 to HAVE_GCC >= 7. 2019-09-29 23:44:58 +00:00
maxv
b0bea7daa3 Add ifdefs to eliminate false positives on lgtm, same as coverity. 2019-09-20 13:38:00 +00:00
maxv
1f8d4ff48b Add KASAN instrumentation for memmove. 2019-09-07 09:46:07 +00:00
rin
a1eeaadb07 Add SA_ENABLE_BIENDIAN option, which enable us to load kernel image of
opposite byte order (for arm EFI bootloader).

XXX
Currently, it is restricted to load_elf*.c. It would be nice if we can
recognize disklabel and filesystem of opposite byte order.
2019-07-21 16:53:17 +00:00
pgoyette
9ca404d49f Now that the ufs module has been split out from ffs and ext2fs, we need
to update the boot-loader to push all modules required to support the
booted filesystem.  We treat the fsmod string as a slash-separated list
of module names (relative to kern.module.path), rather than as a single
module path name.

Note that ffsv1 and ffsv2 are still exempted from the boot-loader's
auto-push, but the list of required filesystems is still noted in the
source.

Also note that arch/sandpoint needs a similar change.  I have not made
this change because I am totally unable to test it.

Tested on my kernel with _no_ built-in file-systems and with the ffs
bootloader settings of fsmod enabled.
2019-06-24 13:58:24 +00:00
maxv
61683f02cb Add KASLR support in UEFI. 2019-06-20 17:33:30 +00:00
christos
b776acde1b Go back ot using 0x%x instead of %#x because we don't always support the
format. See subr_prf.c
2019-04-05 20:09:29 +00:00
thorpej
49da5e3bf1 clzdi2 and ctzdi2 are needed on alpha. (counting instructions are in the
optional CIX extension, and thus not used by the default compiler configuation).
2019-04-05 14:00:16 +00:00
christos
94ad267eb1 fix sign-compare and sign-passing errors. 2019-04-02 22:25:10 +00:00
christos
10497fd285 fix warnings, printf formats, etc. 2019-03-31 20:08:45 +00:00
mlelstv
fff1131959 correct debug message, d->myport has network byte order. 2019-03-31 16:14:48 +00:00
mrg
7b8d5ea7c0 don't use -fnon-call-exceptions on arm. not needed and triggers eh_frame generation. 2019-02-07 10:44:54 +00:00
mrg
1143174b7d apply the previous to all GCC-7 platforms. it affects vax as well. 2019-02-07 05:22:09 +00:00
mrg
11d7172b93 for sh3 and GCC 7 pass -fnon-call-exceptions for udivmoddi4.c.
avoids it emitting a call to abort() for an explicit divide by zero.
2019-02-07 04:33:24 +00:00
mrg
0333538f7b use __builtin_unreachable() after calls that panic or switch context 2019-02-04 09:54:47 +00:00
mrg
d64bb6b12a - remove unreachable code 2019-02-03 11:59:43 +00:00
rin
e9ac1fc994 - Determine KERN_AS automatically depending on whether OPT_MODULAR is
set or not, in the same way as libcompat.

- Specify OPT_MODULAR in the port Makefile instead of KERN_AS.

Now, KERN_AS=library is used for kernels without module(7) for all ports.

OK christos
2018-09-22 12:24:01 +00:00
riastradh
8efd1f3e95 We have popcount; no need to reimplement it. 2018-09-03 18:52:33 +00:00
riastradh
82a15b88fc Rename min.c -> uimin.c, max.c -> uimax.c in libkern. 2018-09-03 16:54:54 +00:00
riastradh
d1579b2d70 Rename min/max -> uimin/uimax for better honesty.
These functions are defined on unsigned int.  The generic name
min/max should not silently truncate to 32 bits on 64-bit systems.
This is purely a name change -- no functional change intended.

HOWEVER!  Some subsystems have

	#define min(a, b)	((a) < (b) ? (a) : (b))
	#define max(a, b)	((a) > (b) ? (a) : (b))

even though our standard name for that is MIN/MAX.  Although these
may invite multiple evaluation bugs, these do _not_ cause integer
truncation.

To avoid `fixing' these cases, I first changed the name in libkern,
and then compile-tested every file where min/max occurred in order to
confirm that it failed -- and thus confirm that nothing shadowed
min/max -- before changing it.

I have left a handful of bootloaders that are too annoying to
compile-test, and some dead code:

cobalt ews4800mips hp300 hppa ia64 luna68k vax
acorn32/if_ie.c (not included in any kernels)
macppc/if_gm.c (superseded by gem(4))

It should be easy to fix the fallout once identified -- this way of
doing things fails safe, and the goal here, after all, is to _avoid_
silent integer truncations, not introduce them.

Maybe one day we can reintroduce min/max as type-generic things that
never silently truncate.  But we should avoid doing that for a while,
so that existing code has a chance to be detected by the compiler for
conversion to uimin/uimax without changing the semantics until we can
properly audit it all.  (Who knows, maybe in some cases integer
truncation is actually intended!)
2018-09-03 16:29:22 +00:00
maxv
a0301d655f Add kasan interceptors for strcpy/strcmp/strlen. 2018-08-27 08:53:19 +00:00
jmcneill
b655919a97 Support loading read-only data sections. ARM64 ELF kernels need this. ok skrll@ 2018-08-23 17:35:42 +00:00
maxv
acb2576569 Add support for kASan on amd64. Written by me, with some parts inspired
from Siddharth Muralee's initial work. This feature can detect several
kinds of memory bugs, and it's an excellent feature.

It can be enabled by uncommenting these three lines in GENERIC:

	#makeoptions 	KASAN=1		# Kernel Address Sanitizer
	#options 	KASAN
	#no options	SVS

The kernel is compiled without SVS, without DMAP and without PCPU area.
A shadow area is created at boot time, and it can cover the upper 128TB
of the address space. This area is populated gradually as we allocate
memory. With this design the memory consumption is kept at its lowest
level.

The compiler calls the __asan_* functions each time a memory access is
done. We verify whether this access is legal by looking at the shadow
area.

We declare our own special memcpy/memset/etc functions, because the
compiler's builtins don't add the __asan_* instrumentation.

Initially all the mappings are marked as valid. During dynamic
allocations, we add a redzone, which we mark as invalid. Any access on
it will trigger a kASan error message. Additionally, the compiler adds
a redzone on global variables, and we mark these redzones as invalid too.
The illegal-access detection works with a 1-byte granularity.

For now, we cover three areas:

	- global variables
	- kmem_alloc-ated areas
	- malloc-ated areas

More will come, but that's a good start.
2018-08-20 15:04:51 +00:00
joerg
4e5ca533a9 The semantics of DW_CFA_GNU_args_size have changed subtile over the
years. Adopt the new convention that it is call-site specific and that
it should be applied before moving the IP by personality routines, but
not during normal unwinding. Further discussion can be found in
LLVM's phabricator review D38680.
2018-07-17 19:01:16 +00:00
joerg
364f0c0b54 Remove redundant setIP call overwritten a few lines later. 2018-07-17 18:58:58 +00:00
christos
4231a89cbc provide memmem 2018-07-08 17:54:42 +00:00
christos
de3955c3eb Use the standard build rules. 2018-06-05 00:57:47 +00:00
christos
c50a4e606c - Introduce :q modifier for make variables and make it double escape $'s so
that passing variables to recursive makes with :q works as expected.
- Revert :Q to work as before.
- Adjust makefiles that use recursive make to use :q

Discussed on tech-toolchain@
XXX: pullup 8
2018-05-27 01:14:50 +00:00
maxv
93eba481f5 Remove annoying things, style, and fix buffer overflows. 2018-05-09 06:49:48 +00:00
christos
87fd18f8e5 s/static inline/static __inline/g for consistency. 2018-04-19 21:50:06 +00:00
nonaka
95e6c1170a efiboot: try to read boot.cfg from /EFI/NetBSD on ESP of the booted disk. 2018-04-02 09:44:18 +00:00
ryo
e55e7ad672 use memmove.S 2018-03-16 07:56:50 +00:00
nonaka
58c029ad93 Add missed file in previous commit.
> efiboot: system can boot from CD/DVD-ROM media.
2018-03-08 23:02:50 +00:00
martin
c9526096cc PR port-alpha/52520: provide float64 -> uint64 conversion and use
that when converting positive numbers.
2017-12-31 11:43:42 +00:00
maxv
556958a38c Make sure we're loading a relocatable binary, to give the user a chance to
correct the kernel name if he mistakenly typed pkboot on a static kernel,
without having to reboot the machine (currently the prekern sees it's a
static kernel and panics).
2017-12-21 14:28:39 +00:00
christos
489063e9bc Even smaller and takes print function. 2017-12-09 00:51:52 +00:00
christos
cbb79c448d don't ifdef _KERNEL 2017-12-08 23:57:57 +00:00
christos
f0d31504f3 use a different, more readable approach. 2017-12-08 23:49:01 +00:00
christos
4a1f5c48a9 coalesce the two copies of hexdump into libkern 2017-12-08 21:51:07 +00:00
riastradh
969998948d Import SHA-3 code into libc and libkern.
No new public symbols in libc, but publishing the symbols is a simple
matter if/when we decide to do so.

Proposed on tech-kern and tech-userlevel with no objections:

https://mail-index.NetBSD.org/tech-kern/2017/11/11/msg022581.html
https://mail-index.NetBSD.org/tech-userlevel/2017/11/11/msg010968.html
2017-11-30 05:47:24 +00:00
maxv
26e9e80df3 Support large pages on KASLR kernels, in a way that does not reduce
randomness, but on the contrary that increases it.

The size of the kernel sub-blocks is changed to be 1MB. This produces a
kernel with sections that are always < 2MB in size, that can fit a large
page.

Each section is put in a 2MB physical chunk. In this chunk, there is a
padding of approximately 1MB. The prekern uses a random offset aligned to
sh_addralign, to shift the section in physical memory.

For example, physical memory layout created by the bootloader for .text.4
and .rodata.0:
 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
 |+---------------+                  |+---------------+                  |
 ||    .text.4    |       PAD        ||   .rodata.0   |       PAD        |
 |+---------------+                  |+---------------+                  |
 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
 PA                                  PA+2MB                         PA+4MB

Then, physical memory layout, after having been shifted by the prekern:
 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
 | P +---------------+               |          +---------------+        |
 | A |    .text.4    |      PAD      |   PAD    |   .rodata.0   |   PAD  |
 | D +---------------+               |          +---------------+        |
 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
 PA                                  PA+2MB                         PA+4MB

The kernel maps these 2MB physical chunks with 2MB large pages. Therefore,
randomness is enforced at both the virtual and physical levels, and the
resulting entropy is higher than that of our current implementaion until
now.

The padding around the section is filled by the prekern. Not to consume
too much memory, the sections that are smaller than PAGE_SIZE are mapped
with normal pages - because there is no point in optimizing them. In these
normal pages, the same shift is applied.

This change has two additional advantages: (a) the cache attacks based on
the TLB are mostly mitigated, because even if you are able to determine
that a given page-aligned range is mapped as executable you don't know
where exactly within that range the section actually begins, and (b) given
that we are slightly randomizing the physical layout we are making some
rare physical attacks more difficult to conduct.

NOTE: after this change you need to update GENERIC_KASLR / prekern /
bootloader.
2017-11-15 18:02:36 +00:00
maxv
85357f1eab Don't process ELF sections that don't have the ALLOC flag set.
NOTE: you need to update both the prekern and the bootloader after this
change.
2017-11-13 21:32:21 +00:00
maxv
2c3f7fd267 Revert my last revision, that is to say, don't group sections into
segments anymore. Initially I did this because I wanted to compress the
sections by reducing the padding between them; but we'll handle that
differently.
2017-11-13 20:21:10 +00:00
christos
4fac11b8ba Avoid negative shift. 2017-11-02 16:09:33 +00:00
maxv
2e01a9a26d Add END(). 2017-10-30 17:13:39 +00:00
maxv
e1daa555fd Group the sections into segments, and align to KERNALIGN only between
segments. Prerequisite for other changes. Unfortunately the code is not
very compact, but whatever.
2017-10-18 16:29:56 +00:00
maxv
f184c30907 Introduce two functions, and dedup code. 2017-10-13 10:39:26 +00:00
maxv
1b305151b2 Constify offset, it must not change. 2017-10-13 10:04:27 +00:00
maxv
b0f8eacf51 Improve comments. 2017-10-08 13:51:31 +00:00