Skip to main content

Record the Process of Changing WSL2 Kernel

· 4 min read
MuelNova
Pwner who wants to write codes.

I'm about to start looking into Kernel Pwn recently, but it seems that the kernel of WSL2 is not the original one, so I decided to replace it.

Prerequisites

Make sure you are using WSL2 instead of WSL, and your Windows version meets the requirement of Windows build 19041 or higher.

Check Kernel Version

First, let's check your current kernel version.

I have installed neofetch, so I used neofetch directly, but you can also use commands like cat /proc/version or uname -a to check.

The original kernel version is 5.15.79.1.

kernel_version

Choose Kernel Version

When I installed WSL2, the Ubuntu version was 22.04.1 LTS. As of 2023/02/13, the latest kernel version is 6.1.9, which is compatible with my Ubuntu version, so I chose to upgrade to the latest version.

tip

If there are compatibility issues: for example, if you want to install version 6.X kernel for Ubuntu16, you need to upgrade the Ubuntu version first; otherwise, it won't work.

Download the kernel from The Linux Kernel Archives. I used a mirror site.

# Replace the filename and the folder name according to your cases!
curl -LO https://mirrors.edge.kernel.org//pub/linux/kernel/v6.x/linux-6.1.9.tar.gz
tar xf linux-6.1.9.tar.gz
cd linux-6.1.9

Use WSL2-Linux-Kernel Configuration File

This .config file has been adjusted and optimized for wsl2. Although the branch was still linux-msft-wsl-5.15.y when I installed it, which is compiled for the 5.15 kernel, it still works for the 6.1.9 kernel I installed.

curl https://raw.githubusercontent.com/microsoft/WSL2-Linux-Kernel/linux-msft-wsl-5.15.y/Microsoft/config-wsl >> .config

(Optional) Adjust the Configuration File

I modified the CONFIG_LOCALVERSION field in the .config file to indicate the kernel characteristics, and you can make more modifications as needed.

Compile the Kernel

Since we already have the .config file, we can just run make to compile the kernel.

note

Before that, make sure you have installed all the dependencies. You can install them using the following commands.

sudo apt install build-essential flex bison dwarves libssl-dev libelf-dev
# Go to https://github.com/microsoft/WSL2-Linux-Kernel#build-instructions for the latest build dependencies
tip

Here, adjust the number of jobs based on your own configuration.

My CPU i7-12700F has 12 cores and 20 threads, so I can use the -j20 option.

sudo make -j20 && sudo make modules -j20 && sudo make modules_install -j20 && sudo make install -j20

Before compilation, there will be many configuration options (i.e., new kernel configuration options not specified in the .config) that you need to specify. Unless you have specific requirements, you can just press Enter all the way through.

Wait patiently for the installation to finish. You can find a similar field in the standard output indicating the kernel path after Kernel:.

Kernel: arch/x86/boot/bzImage is ready  (#2)
DESCEND objtool
DESCEND bpf/resolve_btfids
CALL scripts/checksyscalls.sh
DEPMOD /lib/modules/6.1.9-linux-standard-WSL2
INSTALL /boot

image-20230213122206839

Modify the Kernel

Copy it to the desired location. In my case, I copied it to E:\Ubuntu-2204LTS\linux_kernel_6.1.9.

note

Note that this location should be on your main hard drive, not inside WSL.

cp arch/x86/boot/bzImage /mnt/E/Ubuntu-2204LTS/linux_kernel_6.1.9

Open your user directory and modify the .wslconfig file. Add a kernel field under the [wsl2] tag, with the value being the Windows path with escaped backslashes.

For example, in my case, the .wslconfig file looks like this.

[wsl2]
kernelCommandLine = vsyscall=emulate
kernel=E:\\Ubuntu-2204LTS\\linux_kernel_6.1.9

[user]
default=root
tip

You can open your user directory by pressing Win+R and entering %USERPROFILE. Typically, it is located in C:\Users\<YOUR_USER_NAME>\.

tip

If the .wslconfig file does not exist, then create it.

If the [wsl2] tag does not exist, then create it.

Restart WSL

Now, restart your WSL2 and check the kernel version again.

If everything goes well, your kernel should have been successfully updated.

wsl --shutdown
wsl

image-20230213123158068

Loading Comments...