Ubuntu – Compiling an individual kernel module (Debian/Ubuntu)

debiankernelkernel-modulesUbuntuubuntu-14.04

I need to modify the ELF loader's kernel implementation of an Ubuntu 14.04 distribution. Having downloaded the sources using:

sudo apt-get source linux-image-$(uname -r)

I ran the configuration script:

make config

in the root source tree. After a seemingly endless sequence of input requests, the script created the .config file needed to build the kernel(or a set of modules). The kernel version I am using is linux-3.13.0 and has the following source tree layout:

$ ls 
arch   COPYING  crypto         Documentation  dropped.txt  FileSystemMakefile  fs       init  Kbuild   kernel  MAINTAINERS  mm   README          samples  security   sound  ubuntu  virt
block  CREDITS  debian.master  drivers        elf.dat      firmware            include  ipc   Kconfig  lib     Makefile     net  REPORTING-BUGS  scripts  shortcuts  tools  usr

The ELF loader is located in /path/to/source/fs/binfmt_elf.c. Following this question,in order to compile an individual module it is sufficient to run

make /path/to/module/directory 

In this case that would be:

make ./path/to/source/fs

The compilation is quite lengthy; it takes about twenty minutes(on a virtual machine) and the output is written(by default) in the same directory in which the module is located. I've found the object files by running:

find . -name "*.o"

in /path/to/source/fs. Filtering by name the ELF loader can be located by running:

find . -name "*elf*.o"

In the current sources it is written(by default) in:

/path/to/source/fs/binfmt_elf.o

Having gone through this tutorial, I've noticed that kernel modules have the naming convention [module_name].ko in order to distinguish them from user space object files.

My question is how can I insert the new(modified) ELF loader into the kernel given that the current ELF loader is present(as unloading it may prevent binaries from being executed)?

Edit #1:

Running lsmod gives:

$ lsmod
Module                  Size  Used by
nls_utf8               12557  1 
isofs                  39835  1 
vboxsf                 39690  0 
snd_intel8x0           38153  2 
snd_ac97_codec        130285  1 snd_intel8x0
ac97_bus               12730  1 snd_ac97_codec
snd_pcm               102099  2 snd_ac97_codec,snd_intel8x0
snd_page_alloc         18710  2 snd_intel8x0,snd_pcm
snd_seq_midi           13324  0 
snd_seq_midi_event     14899  1 snd_seq_midi
rfcomm                 69160  0 
snd_rawmidi            30144  1 snd_seq_midi
bnep                   19624  2 
bluetooth             391196  10 bnep,rfcomm
snd_seq                61560  2 snd_seq_midi_event,snd_seq_midi
snd_seq_device         14497  3 snd_seq,snd_rawmidi,snd_seq_midi
snd_timer              29482  2 snd_pcm,snd_seq
joydev                 17381  0 
snd                    69238  12 snd_ac97_codec,snd_intel8x0,snd_timer,snd_pcm,snd_seq,snd_rawmidi,snd_seq_device,snd_seq_midi
serio_raw              13462  0 
vboxguest             248441  7 vboxsf
i2c_piix4              22155  0 
soundcore              12680  1 snd
mac_hid                13205  0 
parport_pc             32701  0 
ppdev                  17671  0 
vboxvideo              12658  0 
drm                   303102  1 vboxvideo
lp                     17759  0 
parport                42348  3 lp,ppdev,parport_pc
hid_generic            12548  0 
usbhid                 52570  0 
hid                   106148  2 hid_generic,usbhid
psmouse               106678  0 
ahci                   25819  2 
libahci                32560  1 ahci
e1000                 145174  0 

Which module needs to be compiled as a LKM in order to include the ELF loader. By default the loader is built into the base kernel.

Best Answer

Try this:

How do I build a single in-tree kernel module?

Alternatively, the way I normally do this is something like the following. This is from memory and may or may not work for you. It also builds all the modules.

Install current kernel source:

apt-get source linux-image-$(uname -r)
cd /usr/src/linux-$(uname -r)
cp /boot/config-$(uname -r) .
make menuconfig
... enable the device

then...
make modules
make modules_install
reboot

Some devices need to be added the module name to /etc/modules if it doesn't get automatically loaded.

Related Topic