Part 3 · Networking and hardening · Lesson 09 · 55 minutes
Inspect, tune and build the kernel
- Read the kernel version, its parameters and its modules
- Change a kernel parameter at run time and keep it with a configuration file
- List the ordered steps that build and install a kernel on Ubuntu, and run the ones that are safe
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
A security review of your server says: "The kernel is not tuned. Redirects are on, some modules load that nobody needs, and the team has never built a kernel with its own options." You do not reboot a production machine for a review. So you read the settings, change one at run time and prove it, make it permanent, and then prepare a kernel build as far as it is safe: the tools, the source and the configuration.
Recall
Which two firewall rules do you write first in an input chain that has policy drop?
A rule for known connections (ct state established,related accept), and a rule for the way in that you use, such as SSH on port 22.
What does "chmod 640" mean for the web files, and which group reads them?
The owner reads and writes, the group reads, and others have no access. The group is www-data, the user that Apache runs as.
The idea
The kernel is the part of the system that talks to the hardware and shares it between programs. It manages memory, processes, files and the network, and it checks every request that a program makes. Everything else runs on top of it.
You can reach the kernel at three points:
| What | Where | Tool |
|---|---|---|
| Version | uname, /proc/version | uname -r |
| Parameters (run-time settings) | /proc/sys/ | sysctl |
| Modules (parts that load on demand) | /lib/modules/VERSION/ | lsmod, modinfo, modprobe |
A parameter name is its path under /proc/sys, with dots for slashes: net.ipv4.ip_forward is the file /proc/sys/net/ipv4/ip_forward. A change is live at once and is lost at a reboot, unless a file in /etc/sysctl.d/ holds it.
To change what is compiled in, you build your own kernel. The steps come in a fixed order:
- Get source → Configure: pick the options
- Configure → Build: compile the kernel and modules
- Build → Install modules: copy them to /lib/modules
- Install modules → Install kernel: copy it to /boot
- Install kernel → Update boot loader: add a menu entry
Worked example
1. Read the version
uname prints facts about the kernel. -r is the release, -s the name, and -m the machine type.
uname -r
uname -sm6.8.0-149-generic
Linux x86_64Your release differs. Read it like this:
cat /proc/version also shows the compiler that built it.
2. Read the parameters
sysctl NAME prints one parameter, -n prints only its value, and sysctl -a lists all of them. The file under /proc/sys holds the same value.
sysctl net.ipv4.icmp_echo_ignore_all
sysctl -n net.ipv4.icmp_echo_ignore_all
cat /proc/sys/net/ipv4/icmp_echo_ignore_all
sysctl -a 2>/dev/null | wc -lnet.ipv4.icmp_echo_ignore_all = 0
0
0
1001A name is a path with dots, and the line ends with the value:
You have about a thousand parameters. Your count differs. The ones that matter for security:
| Parameter | Safe value | What it does |
|---|---|---|
kernel.randomize_va_space | 2 | Puts programs at random places in memory, which makes exploits harder. |
kernel.kptr_restrict | 1 or 2 | Hides kernel addresses from normal users. |
kernel.dmesg_restrict | 1 | Only root may read the kernel log. |
kernel.yama.ptrace_scope | 1 | A process may only trace its own children. |
kernel.unprivileged_bpf_disabled | 1 or 2 | Normal users cannot load kernel programs. |
fs.protected_symlinks, fs.protected_hardlinks | 1 | Stops link tricks in folders such as /tmp. |
net.ipv4.tcp_syncookies | 1 | Keeps the server up under a flood of half-open connections. |
net.ipv4.conf.all.rp_filter | 1 or 2 | Drops packets whose source address cannot come from that side. |
net.ipv4.conf.all.accept_redirects, send_redirects | 0 | Ignores and does not send route changes of other machines. |
net.ipv4.ip_forward | 0 on a host | A machine that forwards packets is a router. |
Read some of them:
sysctl kernel.randomize_va_space kernel.kptr_restrict kernel.dmesg_restrict net.ipv4.tcp_syncookieskernel.randomize_va_space = 2
kernel.kptr_restrict = 1
kernel.dmesg_restrict = 1
net.ipv4.tcp_syncookies = 1Your values can differ, and a distribution sets most of them already.
3. Change a parameter and prove it
sysctl -w NAME=VALUE writes a value. The parameter net.ipv4.icmp_echo_ignore_all makes the machine ignore ping. ping -c1 -W1 ADDRESS sends one ping and waits one second, and its exit code is 0 when an answer comes.
ping -c1 -W1 127.0.0.1 > /dev/null; echo "ping: $?"
sudo sysctl -w net.ipv4.icmp_echo_ignore_all=1
ping -c1 -W1 127.0.0.1 > /dev/null; echo "ping: $?"
sudo sysctl -w net.ipv4.icmp_echo_ignore_all=0
ping -c1 -W1 127.0.0.1 > /dev/null; echo "ping: $?"ping: 0
net.ipv4.icmp_echo_ignore_all = 1
ping: 1
net.ipv4.icmp_echo_ignore_all = 0
ping: 0The last command is the way back. The kernel.* and fs.* parameters change the same way, for example sudo sysctl -w kernel.randomize_va_space=2. In a container most of them are read-only, because the kernel is shared with the host.
4. Make a setting permanent
Go to the lab folder, so that the file is saved there and not in the folder of lesson 7.
cd ~/krlab/linux-advancedSave this file as 90-krlab.conf and copy it to /etc/sysctl.d/. Each line is NAME = VALUE. The files of that folder load in name order at boot. sysctl -p FILE loads one file now, and sysctl --system loads all of them.
90-krlab.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.accept_source_route = 0sudo cp 90-krlab.conf /etc/sysctl.d/90-krlab.conf
sudo sysctl -p /etc/sysctl.d/90-krlab.conf
sysctl -n net.ipv4.conf.all.send_redirectsnet.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
05. Read the modules
A module is a piece of the kernel that is loaded when it is needed, for example a driver or a firewall part. lsmod lists the loaded modules and what uses them. sudo modprobe NAME loads a module, and sudo modprobe -r NAME removes it again. The module dummy is harmless: it makes the dummy interfaces of lesson 2. modinfo prints facts about a module, and -F FIELD prints one field. Here it reads the licence of the module (the field is called license).
sudo modprobe dummy
lsmod | grep '^dummy'
modinfo -F license dummy
modinfo -F description dummydummy 12288 0
GPL
Dummy netdevice driver which discards all packets sent to itmodprobe loads a module and the modules that it needs. -n (dry run) with -v (verbose) shows what it would load and changes nothing.
sudo modprobe -n -v vxlaninsmod /lib/modules/6.8.0-149-generic/kernel/net/ipv4/udp_tunnel.ko.zst
insmod /lib/modules/6.8.0-149-generic/kernel/net/ipv6/ip6_udp_tunnel.ko.zst
insmod /lib/modules/6.8.0-149-generic/kernel/drivers/net/vxlan/vxlan.ko.zstThe two helper modules load first, because vxlan needs them. Your list and paths differ. Remove dummy again with sudo modprobe -r dummy; if a dummy interface still uses it, delete the interface first. To keep a module away, put the line blacklist NAME in a file in /etc/modprobe.d/, or install NAME /bin/false to refuse even a direct load: this is a hardening step for rare file systems and protocols. In a container, loading and removing modules is refused.
6. Get the build tools and the source
Use apt (lesson 9 of Part 2). The tools are a compiler and make (build-essential), bc (a calculator that the build uses), flex and bison (parser generators), the development libraries libssl-dev and libelf-dev, and libncurses-dev for the menu. The package linux-source holds the source of the kernel of your release as a compressed archive in /usr/src.
sudo apt install -y build-essential bc flex bison libssl-dev libelf-dev libncurses-dev
sudo apt install -y linux-source
ls -lL /usr/src/linux-source-*.tar.bz2
mkdir -p ~/krlab/linux-advanced/kernel && cd ~/krlab/linux-advanced/kernel
tar -xf /usr/src/linux-source-*.tar.bz2
cd linux-source-*
make kernelversion-rw-r--r-- 1 root root 360940265 Oct 23 12:10 /usr/src/linux-source-6.8.0.tar.bz2
6.8.12The download is 360 MB, and unpacking takes a few minutes. The source is the version of the Ubuntu archive, which can differ from your running kernel (here 6.8.12 for the source, and 6.8.0-149 for the running kernel). The tool make builds a program by rules in a Makefile, and make kernelversion prints the version of the source.
7. Configure
The configuration is a file .config with one line for each option: =y built in, =m a module, and # ... is not set off. make defconfig writes a default one. scripts/config changes options without a menu: --set-str sets a text, --disable and --enable switch an option, and --state reads one (y, m, n). make olddefconfig fills in every option that the file does not name with its default.
make defconfig
grep -c '=y' .config
scripts/config --set-str LOCALVERSION "-krlab"
scripts/config --disable ISO9660_FS
make olddefconfig
grep '^CONFIG_LOCALVERSION=' .config
scripts/config --state ISO9660_FS1540
CONFIG_LOCALVERSION="-krlab"
nLOCALVERSION is the suffix that your kernel gets in uname -r, so that you can tell it from the others. For a kernel that you will boot, start from the config of the running kernel and not from defconfig: the Ubuntu config of the test machine has 3,065 options built in and 6,275 as modules, so it knows the hardware that defconfig may miss. The Ubuntu config needs two changes before it builds from the plain source archive. The first command below replaces the .config that defconfig wrote, and with it your suffix, so the block sets LOCALVERSION again.
cp /boot/config-$(uname -r) .config
scripts/config --set-str LOCALVERSION "-krlab"
scripts/config --set-str SYSTEM_TRUSTED_KEYS "" --set-str SYSTEM_REVOCATION_KEYS ""
scripts/config --disable DEBUG_INFO_BTF --disable DEBUG_INFO_DWARF5 --enable DEBUG_INFO_NONE
make olddefconfigThe Ubuntu config names two certificate files in a debian/ folder that the source archive does not have, and without the first change make stops with "No rule to make target 'debian/canonical-certs.pem'". It also asks for debug data, which adds many gigabytes and hours to the build; the second change turns it off. These lines were not run to the end of a build here. make menuconfig opens a menu for the options; it is interactive and was not run here. Your counts differ.
8. Build and install (not run here)
These steps take an hour or more, need about 25 GB of free disk space, and replace the kernel that boots, so they were not run on the test machine: read them as a procedure. Run them only on a practice system that you can restore.
make -j"$(nproc)"
sudo make modules_install
sudo make install
sudo update-grub| Step | Command | What it does |
|---|---|---|
| Build | make -j"$(nproc)" | Compiles the kernel image and the modules. -j runs one job for each CPU. |
| Install modules | sudo make modules_install | Copies the modules to /lib/modules/VERSION/. |
| Install the kernel | sudo make install | Copies the image and the map to /boot, and makes the start-up image initrd. |
| Boot loader | sudo update-grub | Adds a menu entry for the new kernel. Keep the old kernel as a way back. |
After a reboot, uname -r shows your LOCALVERSION. On Ubuntu, make bindeb-pkg builds .deb packages instead, which apt can install and remove cleanly. Secure Boot refuses a kernel that nobody signed: turn it off in the firmware of the practice system, or the new kernel does not start. The boot loader (GRUB) shows an error and its menu: choose the old kernel under Advanced options.
Your turn
Do three small jobs. There are no steps here: use the commands of the worked example.
- Save a file
91-krtask.confwith the one linenet.ipv4.icmp_echo_ignore_all = 1, copy it to/etc/sysctl.d/, and load it withsysctl -p. Ping127.0.0.1once and read the exit code. Then set the value back to0withsysctl -w, ping again, and remove the file. - Read the licence of the module
vxlanwithmodinfo, without loading it. - Read the values of
kernel.randomize_va_spaceandnet.ipv4.tcp_syncookieson your own machine.
Optional, only if you unpacked the source in step 6: in your kernel source folder, run make defconfig. Set LOCALVERSION to -krtask, disable ISO9660_FS, and run make olddefconfig.
Check yourself
You run "sysctl -w" and the setting is gone after a reboot. How do you keep it?
Write the line NAME = VALUE into a file in /etc/sysctl.d/ with the ending .conf. The boot process loads that folder. sysctl -w changes only the running kernel.
Why does "modprobe" load some modules that you did not name?
A module can depend on others. modprobe reads the dependency list and loads those first. insmod loads one file only and fails when a dependency is missing.
Why do you keep the old kernel when you install your own?
A new kernel can fail to boot, for example if you left out the driver of the disk. The boot loader menu lets you start the old kernel and repair the problem.
Which two parameters make it harder to use a memory flaw, and what does each one do?
kernel.randomize_va_space = 2 puts the code and data at random places, so an attacker cannot know an address. kernel.kptr_restrict hides the addresses of kernel code from normal users, which removes a hint for an attacker.
Field notes
- Keep your hardening settings in one file with a high number, such as
90-hardening.conf, so that they win over the defaults of the distribution. Check the result withsysctl --systemandsysctl -a. - Do not turn on
rp_filter = 1on a machine with more than one route to the same network without a test: it drops legitimate packets on an asymmetric path.2(loose mode) is the safer choice. - Most servers never need a custom kernel. Build one only for a feature that the distribution kernel lacks, and then you must follow its security updates yourself. Prefer the kernel of the distribution and
apt upgrade. kernel.modules_disabled = 1stops all module loading until the next boot. Use it only after every needed module is loaded.- The CIS Benchmarks for Linux list the network and file system parameters of this lesson and the modules to blacklist.