Outils pour utilisateurs

Outils du site


tech:compilation-kernel-grsecurity

Ceci est une ancienne révision du document !


Compilation Noyau patch Grsecurity PAX Linux sous Debian

Compile Noyau Linux, patch avec Grsecurity sous Debian

Vérif user_xattr ?

apt-get update
apt-get install kernel-package
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.9.1-19' --with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --with-arch-32=i586 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.9.1 (Debian 4.9.1-19)
apt-get install gcc-4.9-plugin-dev
apt-get install attr

Téléchargement du patch grsecurity

wget https://grsecurity.net/stable/grsecurity-3.0-3.2.64-201411062032.patch
wget https://grsecurity.net/stable/grsecurity-3.0-3.2.64-201411062032.patch.sig
gpg --verify grsecurity-3.0-3.2.64-201411062032.patch.sig grsecurity-3.0-3.2.64-201411062032.patch

Téléchargement des sources du noyau Linux

wget https://www.kernel.org/pub/linux/kernel/v3.0/linux-3.2.64.tar.xz
wget https://www.kernel.org/pub/linux/kernel/v3.0/linux-3.2.64.tar.sign
7z x linux-3.2.64.tar.xz
gpg --verify linux-3.2.64.tar.sign linux-3.2.64.tar

On va “patcher” : On test avant

patch --dry-run -p1 < ../grsecurity-3.0-3.2.64-201411062032.patch

Allons-y

patch -p1 < ../grsecurity-3.0-3.2.64-201411062032.patch

C'est fait. Compilons…. Re

ls -lrt /boot/config-3.*
cp /boot/config-3.16-3-amd64 .config
make oldconfig
make menuconfig

ou

make defconfig

Si vous avez un quadcore pas chargé

$ #getconf _NPROCESSORS_ONLN
$ nproc
4
export CONCURRENCY_LEVEL=4

C'est l’équivalent du make -j 4 ou de export MAKEFLAGS=“-j4”. Mais pour make-kpkg, la variable MAKEFLAGS ne doit pas être définie. unset MAKEFLAGS si besion.

Voir http://askubuntu.com/questions/22276/does-concurrency-level-x-affect-to-all-compilations-made-with-make

Ou plus simple

export CONCURRENCY_LEVEL=$(nproc)

Pour compiler le noyau Linux sous Debian

Ca y ai, on compile

fakeroot make-kpkg --initrd --append-to-version="gnugrs" kernel-image kernel-headers

On va avoir besoin de changer des droits PAX (inclus avec Grsecurity) Install du pachet attr qui inclue l'outil setfattr (on va en avoir besoin, sinon certain programe de marcherons plus que nous démarrrons avec le nouveau noyeau)

apt-get install attr
  1. # Ancienne méthode

Téléchargement du script fourni avec le projet Mempo

wget https://raw.githubusercontent.com/mempo/deterministic-kernel/master/apps/grsec-setpax/postinstall/fs_attr_grsecurity_standard_debian.sh
chmod +x fs_attr_grsecurity_standard_debian.sh
sudo ./fs_attr_grsecurity_standard_debian.sh

Personnellement, c'est été oublié à rajouté :

setfattr -n user.pax.flags -v "rm" /usr/lib/icedove/icedove
setfattr -n user.pax.flags -v "rm" /usr/lib/iceweasel/iceweasel
setfattr -n user.pax.flags -v "m"  /usr/lib/iceweasel/plugin-container
setfattr -n user.pax.flags -v "E"  /usr/bin/python3.4
setfattr -n user.pax.flags -v "m"  /usr/bin/nodejs
setfattr -n user.pax.flags -v "m"  /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java

Ou avec paxctl

Configuration PAX paxctld.conf

/etc/paxctld.conf
/usr/lib/icedove/icedove				rm
/usr/lib/iceweasel/iceweasel				rm
/usr/lib/iceweasel/plugin-container			m
/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java		m

 Nouvelle méthode

gpg --verify paxctld_1.0-2_amd64.deb.sig paxctld_1.0-2_amd64.deb
gpg: Signature faite le jeu. 01 janv. 2015 18:09:39 CET
gpg:                avec la clef RSA 0x44D1C0F82525FE49
gpg: Bonne signature de « Bradley Spengler (spender) <spender@grsecurity.net> » [inconnu]
gpg: Attention : cette clef n'est pas certifiée avec une signature de confiance.
gpg:             Rien n'indique que la signature appartient à son propriétaire.
Empreinte de clef principale : DE94 52CE 46F4 2094 907F  108B 44D1 C0F8 2525 FE49
dpkg -i paxctld_1.0-2_amd64.deb

Plus rien à faire. La conf se trouve dans */etc/paxctld.conf* et le daemon paxctld s'occupe de tout. Dans mon cas, la conf par défaut suffit.

Il est important de vérifier (et de superviser) que le service paxctld soit bien démarré, et activé pour démarrer automatiquement.

En cas de mise à jour faut-il systématiquement redémarrer le service paxctld !?

sysctl

Pour voir les propriétés modifiable à chaud

sysctl -a | egrep "kernel.pax.|kernel.grsecurity."

Exemple de fichier de conf (ordi de bureau)

/etc/sysctl.d/05-grsecurity.conf
kernel.grsecurity.linking_restrictions = 1
kernel.grsecurity.enforce_symlinksifowner = 1
kernel.grsecurity.deter_bruteforce = 1
kernel.grsecurity.fifo_restrictions = 1
kernel.grsecurity.ptrace_readexec = 1
kernel.grsecurity.consistent_setxid = 1
 
kernel.grsecurity.ip_blackhole = 1
kernel.grsecurity.lastack_retries = 4
 
kernel.grsecurity.chroot_deny_shmat = 1
kernel.grsecurity.chroot_deny_unix = 1
kernel.grsecurity.chroot_deny_mount = 1
kernel.grsecurity.chroot_deny_fchdir = 1
kernel.grsecurity.chroot_deny_chroot = 1
kernel.grsecurity.chroot_deny_pivot = 1
kernel.grsecurity.chroot_enforce_chdir = 1
kernel.grsecurity.chroot_deny_chmod = 1
kernel.grsecurity.chroot_deny_mknod = 1
kernel.grsecurity.chroot_restrict_nice = 1
kernel.grsecurity.chroot_caps = 1
kernel.grsecurity.chroot_deny_sysctl = 1
kernel.grsecurity.chroot_findtask = 1
 
# TPE : Trusted Path Execution. 
kernel.grsecurity.tpe = 1
# Drastique. Plus d exec dans le home
kernel.grsecurity.tpe_restrict_all = 1
 
#kernel.grsecurity.socket_all = 1
#kernel.grsecurity.socket_client = 1
#kernel.grsecurity.socket_server = 1
kernel.grsecurity.harden_ptrace = 1
 
# For mplayer2 with x11 drivers (full screen)
# else "vo=x11,sdl" => "vo=xv,directfb" in /etc/mplayer2/mplayer.conf
kernel.grsecurity.harden_ipc = 0
 
# Drastique. Rebbot necessaire pour revenir en arriere
# Empeche ecryptfs de fonctionner
#kernel.grsecurity.romount_protect = 1
 
## Desactiv
kernel.grsecurity.dmesg = 0
kernel.grsecurity.deny_new_usb = 0
 
## Groupes
#kernel.grsecurity.socket_all_gid = 1004
#kernel.grsecurity.socket_client_gid = 1003
#kernel.grsecurity.socket_server_gid = 1002
#kernel.grsecurity.audit_gid = 1007
kernel.grsecurity.tpe_gid = 1005
#kernel.grsecurity.symlinkown_gid = 1006
 
## Audit
#kernel.grsecurity.audit_group = 1
 
# Si activé vraiment bavard
kernel.grsecurity.audit_chdir = 0
 
kernel.grsecurity.audit_mount = 1
kernel.grsecurity.audit_ptrace = 1
 
 
## Logging
#kernel.grsecurity.exec_logging = 1
#kernel.grsecurity.rwxmap_logging = 1
kernel.grsecurity.signal_logging = 1
kernel.grsecurity.forkfail_logging = 1
kernel.grsecurity.timechange_logging = 1
#kernel.grsecurity.chroot_execlog = 1
#kernel.grsecurity.resource_logging = 1
 
# Test
kernel.grsecurity.disable_priv_io = 1
 
## Dernier parametre
# Drastique. Interdit toutes modifications de ces parametres. Reboot necessaire pour rechanger
#kernel.grsecurity.grsec_lock = 1

Cette conf sera appliquée au démarrage du PC ou bien :

sysctl -p /etc/sysctl.d/05-grsecurity.conf

Exemple de changement de conf à chaud La pluspart des problèmes sont résolut en abaissant la sécurité de la façon suivantes :

sysctl -w kernel.pax.softmode=1
sysctl -w kernel.grsecurity.tpe=0

Plus de son ? Pour un ordi de bureau : CONFIG_GRKERNSEC_SYSFS_RESTRICT=n

http://arunraghavan.net/2012/10/grsec-and-pulseaudio/

Liens :

http://www.chromium.org/chromium-os/chromiumos-design-docs/system-hardening

http://en.wikibooks.org/wiki/Grsecurity/The_RBAC_System

http://linux.developpez.com/cours/securedeb/?page=annexe12

http://www.cs.virginia.edu/~jcg8f/GrsecuritySELinuxCaseStudy.pdf

http://www.cs.virginia.edu/~jcg8f/SELinux%20grsecurity%20paper.pdf

http://judepereira.com/blog/playing-with-grsecurity-a-brief-tutorial/

http://resources.infosecinstitute.com/gentoo-hardening-part-2-introduction-pax-grsecurity/

http://wiki.gentoo.org/wiki/Hardened/PaX_Quickstart

http://wiki.gentoo.org/wiki/Hardened/Grsecurity2_Quickstart

https://grsecurity.net/gracldoc.htm

http://www.youtube.com/watch?v=38Nu7YOt6lQ

https://github.com/pruby/pmcma

Pb

Pb Apache prefork fork bruteforce

Voir : https://serverfault.com/questions/460429/clone2-30-sec-delay-in-apache

Symptôme : 30 seconde pour avoir la réponse HTTP d'un fichier statique :

time curl http://localhost/robots.txt

Logs dmesg ou /var/log/kern.log

# dmesg
bruteforce prevention initiated for the next 30 minutes or until service restarted, stalling each fork 30 seconds.
 Please investigate the crash report for /usr/sbin/apache2[/usr/sbin/apach:49719] uid/euid:33/33 gid/egid:33/33, parent /usr/sbin/apache2[/usr/sbin/apach:52102] uid/euid:0/0 gid/egid:0/0
/var/log/apache2/error.log
[Thu Jan 17 11:31:03.452626 2019] [mpm_itk:error] [pid 53254] (12)Cannot allocate memory: fork: Unable to fork new process
[Thu Jan 17 12:07:55.121999 2019] [mpm_prefork:error] [pid 27073] (12)Cannot allocate memory: AH00159: fork: Unable to fork new process
[Thu Jan 17 12:07:55.516731 2019] [mpm_itk:error] [pid 60456] child died with signal 11
[Thu Jan 17 13:57:28.126583 2019] [mpm_itk:error] [pid 13856] (12)Cannot allocate memory: fork: Unable to fork new process
[Thu Jan 17 14:30:49.613734 2019] [mpm_itk:error] [pid 18798] (12)Cannot allocate memory: fork: Unable to fork new process
[Thu Jan 17 14:30:49.628835 2019] [mpm_itk:error] [pid 20078] (12)Cannot allocate memory: fork: Unable to fork new process
[Thu Jan 17 14:30:49.735962 2019] [mpm_itk:error] [pid 20018] (12)Cannot allocate memory: fork: Unable to fork new process
[Thu Jan 17 14:30:49.748244 2019] [mpm_itk:error] [pid 18798] (12)Cannot allocate memory: fork: Unable to fork new process
[Thu Jan 17 14:30:49.852928 2019] [mpm_itk:error] [pid 20073] (12)Cannot allocate memory: fork: Unable to fork new process
[Thu Jan 17 14:30:49.865209 2019] [mpm_itk:error] [pid 20018] (12)Cannot allocate memory: fork: Unable to fork new process
[Thu Jan 17 14:30:50.006418 2019] [mpm_itk:error] [pid 20078] (12)Cannot allocate memory: fork: Unable to fork new process
[crit] Memory allocation failed, aborting process.

Apache forkant souvent et rapidement grsec prend cela pour une tentative de bruteforce

Cela est dû à GRSecurity, à plus précisément à l'option suivante de compilation du Kernel :
CONFIG_GRKERNSEC_BRUTE=y

Solution possible :

  • Passer Apache en mode worker ou lieu de prefork
  • Désactiver grsec
  • Modifier la conf Apache pour qu'il fork moins
  • Maintenance palliative : chien de garde (A tester)
/etc/apache2/mods-available/mpm_prefork.conf
<IfModule mpm_prefork_module>
    #StartServers          5
    StartServers          140
    #MinSpareServers       5
    MinSpareServers       20
    #MaxSpareServers      10
    MaxSpareServers      30
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>

Tentative de chien de garde via crontab chaque minute

/usr/local/bin/fix_apache_grkernsecbrut.sh
#! /bin/bash
 
curl --resolve www.acme.fr:443:127.0.0.1 --max-time 10 --connect-timeout 10 http://www.acme.fr/robots.txt >/dev/null 2>&1
 
if [[ $? == 28 ]]
then
    date >> /root/${0}.log
    /usr/sbin/apachectl graceful &
fi
tech/compilation-kernel-grsecurity.1742825205.txt.gz · Dernière modification : de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki