Patching An OpenWRT Kernel Module

Recently I was hit by a null pointer dereference bug in OpenWRT. It was part of the 5GHz Wi-Fi driver and interrupted my internet connection 3 or 4 times already in the last few months. The fix was merged to the driver more than one week ago, but OpenWRT has not yet updated the driver. To avoid random interruptions on my internet again, I decided to patch the kernel myself and record what I have done.

The first step is to check out the desired version of OpenWRT. I used the exact same version that is running on my router.

The second step is to create an OpenWRT build environment. I used an Ubuntu 20.04 docker and installed the packages as recommended by the documentation. Interestingly, I also had to additionally install flex.

The third step is to patch OpenWRT. Initially I tried to bump the driver to the latest version, but the resulting code does not compile, most likely due to the fact that I used an old version of OpenWRT. Thus, I decided to only apply the fix itself, which can be done quite easily.

wget \
  -O package/kernel/mt76/patches/002-mt76-mt7915-fix-potential-NPE-in-TXS-processing.patch

The next step is to create a kmod package. This is done as follows.

wget \
  -O .config
make defconfig
make target/linux/compile
make package/kernel/mt76/compile

The package then appears as kmod-mt7915e_5.10.63+2021-07-15-bbebea7d-4_aarch64_cortex-a53.ipk under bin/targets/mediatek/mt7622/packages/. The first line downloads the official build config so that the kernel vermagic of the built package matches what is running on my router. The second line expands the config. Without this line, the next line will launch the config menu which creates a different vermagic. I’m not too sure what the last two lines do. I tried to follow the instructions to build a single kmod, but make package/kernel/linux/install keeps failing. make package/kernel/mt76/compile does succeed. It also invokes make package/kernel/linux/compile so I don’t have to do it.

The last step was to install the package on the router and reboot. I tried to rmmod mt7915e and then modprobe mt7915e, but the router apparently does not like this idea. I had to reboot.

The router has been up and running for a while. Hopeful this fixes the bug for good.