Skip to content

Commit 5d9418b

Browse files
committed
bring CONFIG_FORTIFY_SOURCE back from accidental removal...
Notice that it is not working anymore. Rename pci.c into qemu_edu.c Organize kernel_module readmes further
1 parent 9b4c198 commit 5d9418b

File tree

12 files changed

+80
-53
lines changed

12 files changed

+80
-53
lines changed

README.adoc

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4473,14 +4473,17 @@ TODO: it does not work if I try to immediately launch `sleep`, why?
44734473
insmod /kprobe_example.ko && sleep 4 & sleep 4 &
44744474
....
44754475

4476-
Docs: https://github.com/torvalds/linux/blob/v4.16/Documentation/kprobes.txt
4477-
44784476
I don't think your code can refer to the surrounding kernel code however: the only visible thing is the value of the registers.
44794477

44804478
You can then hack it up to read the stack and read argument values, but do you really want to?
44814479

44824480
There is also a kprobes + ftrace based mechanism with `CONFIG_KPROBE_EVENTS=y` which does read the memory for us based on format strings that indicate type... https://github.com/torvalds/linux/blob/v4.16/Documentation/trace/kprobetrace.txt Horrendous. Used by: https://github.com/brendangregg/perf-tools/blob/98d42a2a1493d2d1c651a5c396e015d4f082eb20/execsnoop
44834481

4482+
Bibliography:
4483+
4484+
* https://github.com/torvalds/linux/blob/v4.16/Documentation/kprobes.txt
4485+
* https://github.com/torvalds/linux/blob/v4.17/samples/kprobes/kprobe_example.c
4486+
44844487
==== Count boot instructions
44854488

44864489
* https://www.quora.com/How-many-instructions-does-a-typical-Linux-kernel-boot-take
@@ -4595,6 +4598,37 @@ I have observed a single match for that instruction, so it must be the init, and
45954598
`CONFIG_NET=n` did not significantly reduce instruction counts, so maybe replacing `init` is enough.
45964599
* gem5 simulates memory latencies. So I think that the CPU loops idle while waiting for memory, and counts will be higher.
45974600

4601+
=== Linux kernel hardening
4602+
4603+
Make it harder to get hacked and easier to notice that you were, at the cost of some (small?) runtime overhead.
4604+
4605+
==== CONFIG_FORTIFY_SOURCE
4606+
4607+
Detects buffer overflows for us:
4608+
4609+
....
4610+
./build -C 'CONFIG_FORTIFY_SOURCE=y' -L fortify
4611+
./run -F 'insmod /strlen_overflow.ko' -L fortify
4612+
....
4613+
4614+
Possible dmesg output:
4615+
4616+
....
4617+
strlen_overflow: loading out-of-tree module taints kernel.
4618+
detected buffer overflow in strlen
4619+
------------[ cut here ]------------
4620+
....
4621+
4622+
followed by a trace.
4623+
4624+
You may not get this error because this depends on `strlen` overflowing at least until the next page: if a random `\0` appears soon enough, it won't blow up as desired.
4625+
4626+
I did observe this at link:http://github.com/cirosantilli/linux-kernel-module-cheat/commit/1b451a70d46a5c4619992ad4dd2e4b8f5a84c252[1b451a70d46a5c4619992ad4dd2e4b8f5a84c252] but not at link:http://github.com/cirosantilli/linux-kernel-module-cheat/commit/9b4c1984fc2cb04de0b4d62749cc1f8eabf26c6f[9b4c1984fc2cb04de0b4d62749cc1f8eabf26c6f] TODO: find a more reproducible failure.
4627+
4628+
Source: link:kernel_module/strlen_overflow.c[]
4629+
4630+
Bibliography: https://www.reddit.com/r/hacking/comments/8h4qxk/what_a_buffer_overflow_in_the_linux_kernel_looks/
4631+
45984632
=== User mode Linux
45994633

46004634
I once got link:https://en.wikipedia.org/wiki/User-mode_Linux[UML] running on a minimal Buildroot setup at: https://unix.stackexchange.com/questions/73203/how-to-create-rootfs-for-user-mode-linux-on-fedora-18/372207#372207
@@ -5337,7 +5371,7 @@ This section documents:
53375371
For the more complex interfaces, we focus on simplified educational devices, either:
53385372

53395373
* present in the QEMU upstream:
5340-
** <<edu>>
5374+
** <<qemu-edu>>
53415375
* added in link:https://github.com/cirosantilli/qemu[our fork of QEMU]:
53425376
** <<pci_min>>
53435377
** <<platform_device>>
@@ -5386,22 +5420,22 @@ Works because we add to our default QEMU CLI:
53865420

53875421
Probe already does a MMIO write, which generates an IRQ and tests everything.
53885422

5389-
[[edu]]
5423+
[[qemu-edu]]
53905424
===== QEMU edu PCI device
53915425

53925426
Small upstream educational PCI device:
53935427

53945428
....
5395-
/pci.sh
5429+
/qemu_edu.sh
53965430
....
53975431

53985432
This tests a lot of features of the edu device, to understand the results, compare the inputs with the documentation of the hardware: https://github.com/qemu/qemu/blob/v2.12.0/docs/specs/edu.txt
53995433

54005434
Sources:
54015435

5402-
* kernel module: link:kernel_module/pci.c[]
5436+
* kernel module: link:kernel_module/qemu_edu.c[]
54035437
* QEMU device: https://github.com/qemu/qemu/blob/v2.12.0/hw/misc/edu.c
5404-
* test script: link:rootfs_overlay/pci.sh[]
5438+
* test script: link:rootfs_overlay/qemu_edu.sh[]
54055439

54065440
Works because we add to our default QEMU CLI:
54075441

@@ -6164,7 +6198,7 @@ c
61646198
And in QEMU:
61656199

61666200
....
6167-
/pci.sh
6201+
/qemu_edu.sh
61686202
....
61696203

61706204
When in <<graphic-mode,non graphic mode>>, using `-D` makes Ctrl-C not get passed to the QEMU guest anymore: it is instead captured by GDB itself, so allow breaking. So e.g. you won't be able to easily quit from a guest progra like:
@@ -7440,6 +7474,8 @@ Lets try to understand some stats better.
74407474

74417475
==== rdtsc
74427476

7477+
link:https://en.wikipedia.org/wiki/Time_Stamp_Counter[x86 instruction] that returns the cycle count since reset:
7478+
74437479
....
74447480
./build -kg && ./run -E '/rdtsc.out;m5 exit;' -g
74457481
./gem5-stat
@@ -7463,7 +7499,7 @@ See also:
74637499

74647500
===== pmccntr
74657501

7466-
Unfortunately-we didn't manage to find an ARM analogue: link:kernel_module/pmccntr.c[] is oopsing, and even it if weren't, it likely won't give the cycle count since boot since it needs to be activate before it starts counting anything:
7502+
TODO We didn't manage to find a working ARM analogue to <<rdtsc>>: link:kernel_module/pmccntr.c[] is oopsing, and even it if weren't, it likely won't give the cycle count since boot since it needs to be activate before it starts counting anything:
74677503

74687504
* https://stackoverflow.com/questions/40454157/is-there-an-equivalent-instruction-to-rdtsc-in-arm
74697505
* https://stackoverflow.com/questions/31620375/arm-cortex-a7-returning-pmccntr-0-in-kernel-mode-and-illegal-instruction-in-u/31649809#31649809
@@ -8349,7 +8385,7 @@ The action seems to be happening at: `hw/arm/virt.c`.
83498385
** `data/readfile`: see <<m5-readfile>>
83508386
** `data/9p`: see <<9p>>
83518387
** `data/gem5/<variant>`: see: <<gem5-build-variants>>
8352-
* `kernel_module`: Buildroot package that contains our kernel modules and userland C tests
8388+
* link:kernel_module[]: Buildroot package that contains our kernel modules and userland C tests
83538389
* `out`: gitignored Build outputs. You won't lose data by deleting this folder since everything there can be re-generated, only time.
83548390
** `out/<arch>`: arch specific outputs
83558391
*** `out/<arch>/buildroot`: standard Buildroot output

kernel_module/README.adoc

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
= kernel_module
2-
3-
Our kernel modules!
1+
https://github.com/cirosantilli/linux-kernel-module-cheat#directory-structure
42

53
. Asynchronous
64
.. link:irq.c[]
@@ -9,7 +7,3 @@ Our kernel modules!
97
.. link:timer.c[]
108
.. link:work_from_work.c[]
119
.. link:workqueue_cheat.c[]
12-
. Hardening
13-
.. link:strlen_overflow.c[]
14-
. Tracing
15-
.. link:kprobe_example.c[]

kernel_module/fops.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#file-operations */
2+
13
#include <linux/debugfs.h>
24
#include <linux/errno.h> /* EFAULT */
35
#include <linux/fs.h> /* file_operations */

kernel_module/kprobe_example.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kprobes
2+
*
3+
* Adapted from: https://github.com/torvalds/linux/blob/v4.17/samples/kprobes/kprobe_example.c
4+
*/
5+
16
/*
27
* NOTE: This example is works on x86 and powerpc.
38
* Here's a sample kernel module showing the use of kprobes to dump a

kernel_module/pci_min.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#pci_min */
2+
13
#include <linux/cdev.h>
24
#include <linux/fs.h>
35
#include <linux/init.h>

kernel_module/pmccntr.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/*
2-
ARM only.
3-
*/
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#pmccntr */
42

53
#include <linux/debugfs.h>
64
#include <linux/errno.h> /* EFAULT */

kernel_module/pci.c renamed to kernel_module/qemu_edu.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#qemu-edu */
2+
13
#include <linux/cdev.h> /* cdev_ */
24
#include <linux/fs.h>
35
#include <linux/init.h>

kernel_module/strlen_overflow.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
/*
2-
https://www.reddit.com/r/hacking/comments/8h4qxk/what_a_buffer_overflow_in_the_linux_kernel_looks/
3-
*/
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#config_fortify_source */
42

53
#include <linux/kernel.h>
64
#include <linux/module.h>
75
#include <linux/string.h>
86

97
static int myinit(void)
108
{
9+
/* Missing terminaing NUL '\0'. */
1110
char buf[] = {'p', 'w', 'n'};
1211
pr_info("%llu\n", (long long unsigned)strlen(buf));
1312
return 0;

kernel_module/user/README.adoc

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1-
= user
2-
3-
Userland C programs used to test our kernel modules.
4-
5-
`sh` programs are simpler, and installed by copying directly with an overlay.
6-
7-
C programs require cross compiling, but give us more control over system calls.
8-
9-
These programs can also be compiled and used on host.
10-
11-
. Standalone
12-
.. link:hello.c[]
13-
.. link:hello_cpp.cpp[]
14-
.. link:sched_getaffinity.c[]
15-
.. link:usermem.c[]
16-
... link:pagemap_dump.c[]
17-
.. inits
18-
.... link:sleep_forever.c[]
19-
.... link:poweroff.c[]
20-
.... link:init_dev_kmsg.c[]
21-
.. link:uio_read.c[]
22-
.. link:rand_check.c[]
23-
.. x86_64
24-
... link:rdtsc.c[]
25-
... link:ring0.c[]
1+
https://github.com/cirosantilli/linux-kernel-module-cheat#rootfs_overlay
2+
3+
. link:hello.c[]
4+
. link:hello_cpp.cpp[]
5+
. link:sched_getaffinity.c[]
6+
. link:usermem.c[]
7+
.. link:pagemap_dump.c[]
8+
. inits
9+
... link:sleep_forever.c[]
10+
... link:poweroff.c[]
11+
... link:init_dev_kmsg.c[]
12+
. link:uio_read.c[]
13+
. link:rand_check.c[]
14+
. x86_64
15+
.. link:rdtsc.c[]

kernel_module/user/proc_events.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
/*
2-
This file is licensed under the GPL v2 (http://www.gnu.org/licenses/gpl2.txt) (some parts was originally borrowed from proc events example)
3-
4-
https://stackoverflow.com/questions/6075013/detect-launching-of-programs-on-linux-platform/8255487#8255487
5-
*/
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#config_proc_events
2+
*
3+
* Adapted from: https://stackoverflow.com/questions/6075013/detect-launching-of-programs-on-linux-platform/8255487#8255487
4+
*/
65

76
#if defined(__aarch64__)
87

0 commit comments

Comments
 (0)