PDA

View Full Version : HOW TO: Compile kernel 2.4.31 from Source with GRSECURITY Patch for Red Hat Linux


choon
01-09-2004, 06:36 AM
Hi everyone,

I am going to list down those steps that I used to compile my own kernel version 2.4.31 with grsecuirty 2.1.6-2.4.31-200506141150 patch. This is mainly for Red Hat Linux and will work on version 7.2, 7.3, 8.0 & 9. I have also used these steps for Fedora Core 1 and CentOS 3.x. Actually some of these steps can be used for any linux distributions provided you know what you are doing :D

Before you try these steps, you must have some experiences to compile your own kernel from source and have some basic understanding about LILO (http://www.google.com.sg/search?hl=en&ie=UTF-8&oe=UTF-8&q=lilo+HOWTO&btnG=Google+Search&meta=) or GRUB (http://www.google.com.sg/search?hl=en&ie=UTF-8&oe=UTF-8&q=grub+HOWTO&btnG=Google+Search&meta=). I am using LILO as an example here.

It is a good idea that you update your system first using up2date before this. Here is my little up2date HOWTO (http://www.webhostingtalk.com/showthread.php?s=&threadid=227083).

FOLLOW THIS GUIDE AT YOUR OWN RISK AS I AM NOT RESPONSIBLE IN ANY DAMAGES CAUSED! YOU HAVE BEEN WARNED!!!

BEFORE YOU BEGIN

Before we build our custom kernel, you'll need to know what's in your server. Issue the following command as root to get the necessary names of your hardware, their PCI addresses, and their IRQs:
lspci
An example result for one of my server:
00:00.0 Host bridge: Intel Corp.: Unknown device 2578 (rev 02)
00:01.0 PCI bridge: Intel Corp.: Unknown device 2579 (rev 02)
00:03.0 PCI bridge: Intel Corp.: Unknown device 257b (rev 02)
00:1d.0 USB Controller: Intel Corp. 82801EB USB (Hub #1) (rev 02)
00:1d.1 USB Controller: Intel Corp. 82801EB USB (Hub #2) (rev 02)
00:1d.2 USB Controller: Intel Corp. 82801EB USB (Hub #3) (rev 02)
00:1d.3 USB Controller: Intel Corp. 82801EB USB EHCI Controller #2 (rev 02)
00:1d.7 USB Controller: Intel Corp. 82801EB USB EHCI Controller (rev 02)
00:1e.0 PCI bridge: Intel Corp. 82801BA/CA/DB PCI Bridge (rev c2)
00:1f.0 ISA bridge: Intel Corp. 82801EB ISA Bridge (LPC) (rev 02)
00:1f.1 IDE interface: Intel Corp. 82801EB ICH5 IDE (rev 02)
00:1f.2 RAID bus controller: Intel Corp.: Unknown device 24df (rev 02)
00:1f.3 SMBus: Intel Corp. 82801EB SMBus (rev 02)
02:01.0 Ethernet controller: Intel Corp.: Unknown device 1019
03:06.0 VGA compatible controller: ATI Technologies Inc Rage XL (rev 27)
03:08.0 Ethernet controller: Intel Corp. 82801EB (ICH5) PRO/100 VE Ethernet Controller (rev 01)
Now you can find more information related to the hardware shown above by issuing the following command:
lspci -s 03:08.0 -vv
The output for the above as below:
03:08.0 Ethernet controller: Intel Corp. 82801EB (ICH5) PRO/100 VE Ethernet Controller (rev 01)
Subsystem: Intel Corp.: Unknown device 342a
Control: I/O+ Mem+ BusMaster+ SpecCycle- MemWINV+ VGASnoop- ParErr- Stepping- SERR+ FastB2B-
Status: Cap+ 66Mhz- UDF- FastB2B+ ParErr- DEVSEL=medium >TAbort- <TAbort- <MAbort- >SERR- <PERR-
Latency: 32 (2000ns min, 14000ns max), cache line size 08
Interrupt: pin A routed to IRQ 20
Region 0: Memory at feafe000 (32-bit, non-prefetchable) size=4K
Region 1: I/O ports at bc00 size=64
Capabilities: [dc] Power Management version 2
Flags: PMEClk- DSI+ D1+ D2+ AuxCurrent=0mA PME(D0+,D1+,D2+,D3hot+,D3cold+)
Status: D0 PME-Enable- DSel=0 DScale=2 PME-

You can use lsmod and cat /proc/interrupts to find out more about your current modules etc...
After you have all the necessary information, you can search for it at Google (http://google.com) with a query such as linux 82801EB to know which modules name to use for your hardware.

Ok, here we start... ...

As root, do the following steps in order:

INSTALL DEPENDENCIES PACKAGES

Step 1: Install gcc and all its dependencies packages

We need gcc to compile kernel. Simply run this command to install if it is not installed gcc and all its dependencies:
up2date gcc
OR if you are using Fedora you can use up2date or yum:
yum install gcc

Step 2: Install ncurses-devel and all its dependencies packages

ncurses-devel package is needed while compiling kernel from source specially for running make menuconfig:
up2date ncurses-devel
OR if you are using Fedora you can use up2date or yum:
yum install ncurses-devel

Step 3: Install patch package

You need patch package to be install so that you can use it to patch the kernel source:
up2date patch
OR if you are using Fedora you can use up2date or yum:
yum install patch

HERE WE REALLY BEGIN

Step 1: Change your current working directory

For me, I choose to download the kernel to /usr/local/src but you are free to choose a directory to store and build/compile your kernel.
cd /usr/local/src
Step 2: Download the latest stable kernel version 2.4.x series which is 2.4.31
wget -c http://www.kernel.org/pub/linux/kernel/v2.4/linux-2.4.31.tar.bz2
Step 3: Download the grsecurity patch
wget -c http://www.grsecurity.net/grsecurity-2.1.6-2.4.31-200506141150.patch.gz
Step 4: Unpack the kernel source
tar xvfj linux-2.4.31.tar.bz2
Step 5: Patch the kernel source with grsecurity patch
gunzip < grsecurity-2.1.6-2.4.31-200506141150.patch.gz | patch -p0
Step 6: Change your current working directory to the kernel source directory
cd linux-2.4.31
Step 7: Clean your kernel source configuration etc...
make clean && make mrproper
Step 8: Use your current configuration to configure your new kernel

You might want to backup your current kernel modules and configuration.
cp /boot/config-`uname -r` .config
make oldconfig
make menuconfig
When you configure your kernel configuration using make oldconfig to use the current booted kernel configuration, please set CONFIG_CRYPTO=y and CONFIG_CRYPTO_SHA256=y within the CRYPTO option and nothing else within it. Then when you come to GRSECURITY option, set it to y and in Security level option can be set to Low, Medium or High. I set mine to Medium. The following are the explanation for each option:

Low additional security

If you choose this option, several of the grsecurity options will be enabled that will give you greater protection against a number of attacks, while assuring that none of your software will have any conflicts with the additional security measures. If you run a lot of unusual software, or you are having problems with the higher security levels, you should say Y here. With this option, the following features are enabled:

linking restrictions
fifo restrictions
random pids
enforcing nproc on execve()
restricted dmesg
random ip ids
enforced chdir("/") on chroot

Medium additional security

If you say Y here, several features in addition to those included in the low additional security level will be enabled. These features provide even more security to your system, though in rare cases they may be incompatible with very old or poorly written software. If you enable this option, make sure that your auth service (identd) is running as gid 10 (usually group wheel). With this option the following features (in addition to those provided in the low additional security level) will be enabled:

random tcp source ports
failed fork logging
time change logging
signal logging
deny mounts in chroot
deny double chrooting
deny sysctl writes in chroot
deny mknod in chroot
deny access to abstract AF_UNIX sockets out of chroot
deny pivot_root in chroot
denied writes of /dev/kmem, /dev/mem, and /dev/port
/proc restrictions with special gid set to 10 (usually wheel)
address space layout randomization
removal of addresses from /proc/<pid>/[maps|stat]

High additional security

If you say Y here, many of the features of grsecurity will be enabled, that will protect you against many kinds of attacks against your system. The heightened security comes at a cost of an increased chance of incompatibilities with rare software on your machine. Since this security level enables PaX, you should view <http://pax.grsecurity.net> and read about the PaX project. While you are there, download chpax and run it on binaries that cause problems with PaX. Also remember that since the /proc restrictions are enabled, you must run your identd as group wheel (gid 10). This security level enables the following features in addition to those listed in the low and medium security levels:

additional /proc restrictions
chmod restrictions in chroot
no signals, ptrace, or viewing processes outside of chroot
capability restrictions in chroot
deny fchdir out of chroot
priority restrictions in chroot
segmentation-based implementation of PaX
mprotect restrictions
kernel stack randomization
mount/unmount/remount logging
kernel symbol hiding

If you need to customized the Grsecurity options, you will need to run make menuconfig then go to Grsecurity option there and set the Security level to Customized and you are on your own since I am not a grsecurity guru :stickout

Many thanks to BigGorilla for more information about grsecurity configuration:
Spender's directory (http://grsecurity.net/~spender/) - includes a quickstart guide to grsecurity.
Grsecurity Config Help (http://grsecurity.net/confighelp.php) - list of all the grsec config options and what they do.

It is also a good idea for you to run make menuconfig so that you can disable one or more of the following which you have to make sure you really don't need it:
Telephony Support
Fusion MPT device support
IEEE 1394 (FireWire) support (EXPERIMENTAL)
Amateur Radio support
IrDA (infrared) support
ISDN subsystem
Multimedia devices
Sound
USB support
Old CD-ROM drivers (not SCSI, not IDE)
Bluetooth support
Library routines
Step 9: make dep
make dep
Setp 10: make bzImage
nohup make bzImage &
You will see something like:
nohup: appending output to `nohup.out'
Just press ENTER then issue this command to view the nohup.out
tail -f nohup.out
To exit from the above, use CTRL+C keys.
Check for any errors and when in doubts do not continue. Use the error message as the search terms to do your search at google might helps you.

Step 11: make modules & make modules_install

Bring up another new xterm shell window and follow these steps: This step is required ONLY if you had enabled Loadable module support in step "Configure Step" above. Loadable module are located in /lib/modules. You MUST do this step if you enabled or disabled any modules, otherwise you will get 'unresolved symbols' errors during or after kernel boot.
nohup make modules 1> modules.out 2> modules.err &

HAVE A BREAK

Step 12: Check
Make modules will take several minutes depending on your server specs. If you are curious about whether did make modules finish, issue ps auwx|grep make to check. Once you know that make modules finishes, check all the previous make...
less nohup.out
less modules.err
less modules.out
Again check for any errors and when in doubts do not continue. Use the error message as the search terms to do your search at google might helps you.

Step 13: Install the modules
After checking and there is no error for make bzImage and make modules, it is time to install your newly build modules.
make modules_install

Step 14: Bootloader and others
cp .config /boot/config-2.4.31-grsec
cp arch/i386/boot/bzImage /boot/vmlinuz-2.4.31-grsec
cp System.map /boot/System.map-2.4.31-grsec
mkinitrd /boot/initrd-2.4.31-grsec.img 2.4.31-grsec
If you are using LILO as your bootloader, edit your /etc/lilo.conf file to add this new kernel but do not set it as default boot kernel. An example as below:

image=/boot/vmlinuz-2.4.31-grsec
label=2.4.31-grsec
append="root=LABEL=/"
read-only
initrd=/boot/initrd-2.4.31-grsec.img
If you are using GRUB as your bootloader, edit /boot/grub/grub.conf file to add this new kernel but do not set it as default boot kernel. An example as below:
title Red Hat Linux (2.4.31-grsec)
root (hd0,0)
kernel /vmlinuz-2.4.31-grsec ro root=LABEL=/
initrd /initrd-2.4.31-grsec.img
Step 15: Test your new kernel
If you are using LILO, use the following command to check errors and update your LILO:
lilo -v -v
lilo
Then set LILO to boot to the new kernel for the next reboot:
lilo -R 2.4.31-grsec
If you are using GRUB, check out this thread (http://www.webhostingtalk.com/showthread.php?s=&threadid=235241)

Assume your default boot kernel is in the first entry among the rest of the kernels.
Add your new kernel in the first entry among the rest of the kernels (on top of your first original kernel which is the first entry before changes). You just change default=1 and fallback=2.
grub shell
grub> savedefault --default=0 --once
grub> quit
Then reboot:
reboot
If your system unable to boot up to the new kernel or causing all type of errors, then you have to reboot the server back to its default/old kernel. Thus it is good to have Remote Reboot Port (RRP) or APC MasterSwitch or similiar so that you don't have to call up your provider just for a reboot. Alternatively you can ask your provider to test your new kernel for you.

If the system is able to boot to your new kernel, please use lsmod, dmesg, cat /var/log/boot.log etc... to check any errors. Leave the system there running with this new kernel at least for a week to test for any errors before setting it as the default kernel.

Again, NEVER set your new kernel as default boot kernel until you have tested it and run FINE for a period of time like a week or so to make sure that your kernel is stable with no error.

References:
Grsecurity (http://www.grsecurity.net/)
Grsecurity Features (http://www.grsecurity.net/features.php)
Kernel (http://kernel.org/)
Kernel HOW TO (http://en.tldp.org/HOWTO/Kernel-HOWTO.html)
Kernel Trap (http://www.kerneltrap.org/)
Kernel Newbies (http://www.kernelnewbies.org/)
Upgrading the Linux Kernel on Red Hat Linux systems (http://www.redhat.com/support/resources/howto/kernel-upgrade/)
Grsecurity at SecurityFocus.com (http://www.securityfocus.com/infocus/1551)

Hope this helps :)

Thanks.

Kindest regards,
Choon
P.S. I might be wrong in certain steps but these steps as listed above work for me
P.P.S. Suggestions or addons are very welcome :)

choon
05-01-2004, 08:29 AM
If you are using Red Hat Enterprise Linux ES release 3 (Taroon Update 1), then take a look at this script (http://choon.net/nptl.php) and follow the instruction or else your will get Segfaults with bind and other programs cannot be started.

choon
07-21-2004, 04:06 PM
If you see the following result in top which is highlighted in bolded red after upgrade for your Red Hat Linux 9:
08:57:43 up 2 min, 1 user, load average: 0.37, 0.17, 0.06
74 processes: 72 sleeping, 2 running, 0 zombie, 0 stopped
CPU states: 0.1% user 0.1% system 0.0% nice 0.0% iowait 99.6% idle
Mem: 482960k av, 135784k used, 347176k free, 0k shrd, 7696k buff
37932k active, 87096k inactive
Swap: 522104k av, 0k used, 522104k free 60164k cached

PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME CPU COMMAND
9588 root 10 0 1196 1196 868 R 0.1 0.2 0:00 0 top -c
1 root 8 0 484 484 428 S 0.0 0.1 0:04 0 init [3]
2 root 9 0 0 0 0 SW 0.0 0.0 0:00 0 keventd
3 root 9 0 0 0 0 SW 0.0 0.0 0:00 0 kapmd
4 root 19 19 0 0 0 SWN 0.0 0.0 0:02 0 ksoftirqd_CPU0
5 root 9 0 0 0 0 SW 0.0 0.0 0:00 0 kswapd
6 root 9 0 0 0 0 SW 0.0 0.0 0:00 0 bdflush
7 root 9 0 0 0 0 SW 0.0 0.0 0:00 0 kupdated
8 root 18446744073709551615 -20 0 0 0 SW< 0.0 0.0 0:00 0 mdrecoveryd
14 root 18446744073709551615 -20 0 0 0 SW< 0.0 0.0 0:00 0 raid1d
15 root 9 0 0 0 0 SW 0.0 0.0 0:00 0 kjournald
2900 root 9 0 0 0 0 SW 0.0 0.0 0:00 0 khubd
3716 root 9 0 560 560 476 S 0.0 0.1 0:00 0 syslogd -m 0
21749 root 9 0 440 440 380 S 0.0 0.0 0:00 0 klogd -x
11206 nobody 9 0 1968 1968 1792 S 0.0 0.4 0:00 0 proftpd: (accepting connections)
31983 root 9 0 1448 1448 1316 S 0.0 0.2 0:00 0 /usr/sbin/sshd
That is due to a bug in your procps package I believe. There was something like a printf("%ull", prio) with prio=-1, which prints 2^64-1 (the high number seen here). Upgrading to 2.0.13 fixed the problem.

hung
11-18-2004, 11:46 AM
Choon,

Is there any chance to do this with CentOS? I have a CentOS box, need to recompile kernel with libata. So I want to make it with grsecurity as well

thanks,
Hung

choon
11-24-2004, 01:21 AM
Hi,

If you really need libata1 patch, let me know.

Thanks.

hung
08-25-2006, 04:14 PM
Hello,

I wonder if compiling kernel in 64bit environment has any difference? I have tried with a CentOS 4.3 box, in 32bit environment without any problem. But trying with new Opteron server with CentOS 4.3 64bit without any success. I am trying to compile kernel 2.6.17.11 few times, but after rebooting, it didnt want to come back

thanks
Hung

choon
08-25-2006, 04:23 PM
When you compile from source, where config file you use? Is it 64bits base? And the amount of memory in the system?

hung
08-25-2006, 05:00 PM
what do you mean when saying config file?
when compiling, I just copied the old one
cp /boot/config-`uname -r` .config

after compiling, the new bzImage file is located at arch/x86-64/...

system has 2GB RAM. It has CentOS 4.3 64bit running already, but it's using stock kernel, so I want to upgrade to the newer one

thanks

choon
08-25-2006, 08:38 PM
Contact me via MSN/Yahoo!IM/etc... I will see what I can do... ...