1. Chroot içindeChroot içinde
{DNS, NTP, Nginx, PHP-FPM}{DNS, NTP, Nginx, PHP-FPM}
Ali Erdinç KöroğluAli Erdinç Köroğlu
aekoroglu@gmail.com - http://ae.koroglu.org -- http://twitter.com/erdinc
Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
2. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Chroot Nedir ?
● Chroot sistem çağrısı “chroot()” ilk olarak Unix Version7 gelişimi sırasında (1979) duyuruldu.
● Uygulamaları veya kullanıcıları bilgisayar kaynaklarından izole etmenin bir yoludur.
● Unix benzeri işletim sistemlerinde çalışan uygulamalar (process) ve bu uygulamaların alt
işlemleri (children) için mevcut root dizininin değiştirilmesini sağlayan işlemdir.
3. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Niçin ?
● Test ve geliştirme süreçleri
Çalışan kritik sistemler üzerinde yazılımların test edilmeden uygulanmarak risk alınması
yerine chroot içinde test ortamı oluşturarak yazılımların test edilmesi
● Bağımlılık kontrolü
Yazılımların chroot içinde geliştirilip derlenmesiyle sadece beklenen bağımlılıkların
oluştuğunun doğrulanması ve böylece olası linkleme ve farklı kütüphanelerin sisteme
kurulmasının önlenmesi
● Uyumluluk
Eski yazılım veya farklı ABI'lere sahip uygulamaların aynı sistem üzerinde isim veya link
çakışmalarına uğramadan destekledikleri kütüphaneler ve verilerle chroot ortamı içinde
çalışması
● Kurtarma
Zarar görmüş açılamayan ortamların alternatif sistemlerle (kurulum ortamı, canlı cd vb.)
veri kurtarma, dosya sistemi kontrolü vb. Işlemlerin yapılması
● Yetki/erişim kontrolü
Programların chroot dizini içerisine hapsedilerek dizin dışı erişimlerinin engellenmesiyle
potansiyel zaafı olan yazılımların olası güvenlik ihlallerine karşı önlem alınması
4. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Bind-DNS Chroot
Bind oldukça yayın kullanılan alan adı sunucularından biridir. RHEL ve CentOS dağıtımlarında
chroot edilmiş hazır RPM paketi bulunmaktadır.
[root@aek ~]# yum install bind-chroot
[root@aek ~]# yum update -y
Önce kullandığınız dağıtımı güncelleyin
CentOS7'nin resmi deposundaki chroot destekli dns sunucusu kurulumu
5. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Bind-DNS Chroot
İşlem tamamlandığında sistemimize bind, bind-libs ve bind-chroot paketleri kurulmuş olacak.
Bizi ilgilendiren dosyalar /etc, /var/named ve /var/named/chroot dizinleri içinde bulunuyor.
İlgili ayar dosyaları chroot içine
[root@aek ~]# mv /etc/named.* /var/named/chroot/etc
[root@aek ~]# cp -av /var/named/named.* /var/named/chroot/var/named/
Loglar için chroot yapısı içinde dizin oluşturulmalı
[root@aek ~]# mkdir /var/named/chroot/var/log/named
[root@aek ~]# chown -R named:named /var/named/chroot/var/log/named
6. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Bind-DNS Chroot
Örnek named.conf
options {
directory "/var/named";
pid-file "/var/run/named/named.pid";
version "not currently available";
recursion no;
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
zone-statistics yes;
};
controls { };
logging {
channel master_log {
file "/var/log/named/master.log" versions 3 size 5m;
severity info;
print-severity yes;
print-time yes;
print-category yes;
};
category default {
master_log;
};
};
zone "." IN {
type hint;
file "named.ca";
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";
zone "koroglu.org" {
type master;
file "zones/koroglu.org";
};
7. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Bind-DNS Chroot
Ayar dosyası içinde yapılan tanımlar için gerekli dizinler oluşturulmalı
[root@aek ~]# mv /etc/named.* /var/named/chroot/etc
[root@aek ~]# cp -av /var/named/named.* /var/named/chroot/var/named/
Loglar için chroot yapısı içinde dizin oluşturulmalı
[root@aek ~]# mkdir /var/named/chroot/var/log/named
[root@aek ~]# chown -R named:named /var/named/chroot/var/log/named
8. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Bind-DNS Chroot
Named.conf içinde belirtilen ayarlar için gerekli dizinler oluşturulmalı
[root@aek ~]# mkdir /var/named/chroot/var/named/data
[root@aek ~]# mkdir /var/named/chroot/var/named/zones
[root@aek ~]# chown -R named:named /var/named/chroot/var/named/data
[root@aek ~]# chown -R named:named /var/named/chroot/var/named/zones
$TTL 86400
@ IN SOA ns1.8bitplus.com. erdinc.8bitplus.com. (
2012030701 ; serial
43200 ; refresh (12hours)
3600 ; retry (1hour)
1209600 ; expire (2week)
6200) ; TTL minimum (2hour)
IN NS ns1.8bitplus.com.
IN NS ns2.8bitplus.com.
IN A 78.46.226.107
www IN A 78.46.226.107
ae IN A 78.46.226.107
Örnek bir alan adı ayar dosyası : /var/named/chroot/var/named/zones/koroglu.org
9. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Bind-DNS Chroot
RHEL7 ve CentOS7 ile birlikte SysVinit yerine Systemd kullanılmaya başlandı
[root@aek ~]# systemctl start named-chroot
Servisi başlatmak için
[root@aek ~]# systemctl enable named-chroot
Boot sonrası otomatik başlaması için
[root@aek etc]# systemctl status named-chroot
named-chroot.service - Berkeley Internet Name Domain (DNS)
Loaded: loaded (/usr/lib/systemd/system/named-chroot.service; disabled)
Active: active (running) since Wed 2015-03-25 23:17:45 EET; 27s ago
Process: 5225 ExecStart=/usr/sbin/named -u named -t /var/named/chroot $OPTIONS (code=exited,
status=0/SUCCESS)
Process: 5223 ExecStartPre=/usr/sbin/named-checkconf -t /var/named/chroot -z /etc/named.conf
(code=exited, status=0/SUCCESS)
Main PID: 5227 (named)
CGroup: /system.slice/named-chroot.service
└─5227 /usr/sbin/named -u named -t /var/named/chroot
Servis sürecini görmek için
10. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Network Time Protocol daemon (ntpd), sistem zamanını standart zaman sunucularıyla
senkron ederek ilgili ayar ve düzenlemeleri yapan bir servistir. Ancak dns servisinde olduğu
gibi hazır bir chroot yapısı bulunmamaktadır.
[root@aek ~]# yum install ntp
[root@aek ~]# cd /
[root@aek ~]# mkdir chroot
[root@aek ~]# mkdir /chroot/ntp
Temel kurulum
Chroot için dizin oluşumu
NTPD Chroot
11. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
mknod -m 666 /chroot/ntp/dev/null c 1 3
mknod -m 666 /chroot/ntp/dev/zero c 1 5
mknod -m 444 /chroot/ntp/dev/random c 1 8
mkdir /chroot/ntp/etc
mkdir /chroot/ntp/proc
mkdir /chroot/ntp/var
mkdir /chroot/ntp/var/lib
mkdir /chroot/ntp/var/lib/ntp
mv /var/lib/ntp/drift /chroot/ntp/var/lib/ntp/
chown -R ntp:ntp /chroot/ntp/var/lib/ntp
mkdir /chroot/ntp/var/log
mkdir /chroot/ntp/var/log/ntpstats
chown -R ntp:ntp /chroot/ntp/var/log/ntpstats
mv /etc/ntp.conf /chroot/ntp/etc
ln -s /chroot/ntp/etc/ntp.conf /etc/ntp.conf
cp -a /etc/localtime /chroot/ntp/etc
Chroot dizin oluşumu
NTPD Chroot
12. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Chroot dizin yapısı
/chroot/ntp/
├── dev
│ ├── null
│ ├── random
│ └── zero
├── etc
│ ├── localtime
│ ├── ntp.conf
│ └── resolv.conf
├── proc
└── var
├── lib
│ └── ntp
└── log
├── ntp.log
└── ntpstats
NTPD Chroot
13. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
/chroot/ntp/etc/ntp.conf
server 0.tr.pool.ntp.org
server ntp.ulakbim.gov.tr
server 127.127.1.0
fudge 127.127.1.0 stratum 10
restrict 95.0.124.102 mask 255.255.255.240 nomodify notrap
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 127.0.0.1
#interface ignore wildcard
#interface listen 192.168.1.254
driftfile /var/lib/ntp/drift
logfile /var/log/ntp.log
logconfig =syncevents +peerevents +sysevents +allclock
# CVE-2013-5211
disable monitor
NTPD Chroot
14. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
/lib/systemd/system/ntpd-chroot.service
[Unit]
Description=Network Time Service (Chroot)
After=syslog.target ntpdate.service sntp.service
Requires=ntpd-chroot-mount.service
After=ntpd-chroot-mount.service
[Service]
Type=forking
EnvironmentFile=-/etc/sysconfig/ntpd
ExecStart=/usr/sbin/ntpd -i /chroot/ntp -u ntp:ntp $OPTIONS
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit]
Description=Mount proc environment for NTP Chroot
BindsTo=ntpd-chroot.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/mount --bind /proc/ /chroot/ntp/proc
ExecStop=/bin/umount /chroot/ntp/proc
/lib/systemd/system/ntpd-chroot-mount.service
NTPD Chroot
15. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
[root@aek ~]# systemctl start ntpd-chroot
Servisi başlatmak için
[root@aek ~]# systemctl enable ntpd-chroot
Boot sonrası otomatik başlaması için
[root@aek etc]# systemctl status ntpd-chroot
ntpd-chroot.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd-chroot.service; disabled)
Active: active (running) since Thu 2015-03-26 20:02:36 EET; 1min 30s ago
Process: 23190 ExecStart=/usr/sbin/ntpd -i /chroot/ntp -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 23191 (ntpd)
CGroup: /system.slice/ntpd-chroot.service
└─23191 /usr/sbin/ntpd -i /chroot/ntp -u ntp:ntp -g
Mar 26 20:02:36 aek ntpd[23191]: proto: precision = 0.067 usec
Mar 26 20:02:36 aek ntpd[23191]: 0.0.0.0 c01d 0d kern kernel time sync enabled
Mar 26 20:02:36 aek ntpd[23191]: ntp_io: estimated max descriptors: 1024, initial socket boundary: 16
Mar 26 20:02:36 aek ntpd[23191]: Listen and drop on 0 v4wildcard 0.0.0.0 UDP 123
Mar 26 20:02:36 aek ntpd[23191]: Listen and drop on 1 v6wildcard :: UDP 123
Mar 26 20:02:36 aek ntpd[23191]: Listen normally on 2 lo 127.0.0.1 UDP 123
Mar 26 20:02:36 aek ntpd[23191]: Listen normally on 3 ens32 95.0.124.102 UDP 123
Mar 26 20:02:36 aek ntpd[23191]: Listen normally on 4 lo ::1 UDP 123
Mar 26 20:02:36 aek ntpd[23191]: Listen normally on 5 ens32 fe80::250:56ff:feb8:6403 UDP 123
Mar 26 20:02:36 aek ntpd[23191]: Listening on routing socket on fd #22 for interface updates
NTPD Chroot
16. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
NTPD Chroot
ntp 23191 0.0 0.1 29364 2072 ? Ss 20:02 0:00 /usr/sbin/ntpd -i /chroot/ntp -u ntp:ntp -g
ps aux
proc /chroot/ntp/proc proc rw,nosuid,nodev,noexec,relatime 0 0
/proc/mounts
[root@aek etc]# ntpstat
synchronised to NTP server (193.140.100.40) at stratum 3
time correct to within 8406 ms
polling server every 64 s
[root@aek etc]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================
*samur.ulak.net. 150.214.94.5 2 u 13 64 1 79.898 374.274 0.000
LOCAL(0) .LOCL. 10 l 76 64 2 0.000 0.000 0.000
17. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Nginx Chroot
Nginx büyük yükler altında dahi çok başarılı çalışan bir HTTP, ters proxy ve eposta proxy
Sunucusudur. RHEL EPEL deposu daha düşük bir versiyon'a sahip olduğundan nginx
için yum'a nginx resmi deposunu eklemek gerekmektedir.
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
/etc/yum.repos.d/nginx.repo
yum upgrade
yum install nginx
Yum repolarını güncelleyerek nginx kurulumu
19. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Nginx Chroot
mknod -m 0666 /chroot/nginx/dev/null c 1 3
mknod -m 0666 /chroot/nginx/dev/random c 1 8
mknod -m 0444 /chroot/nginx/dev/urandom c 1 9
Gerekli aygıtlar
[root@aek etc]# grep -i nginx "/etc/passwd" > /chroot/nginx/etc/passwd
[root@aek etc]# grep -i nginx "/etc/group" > /chroot/nginx/etc/group
[root@aek etc]# grep -i nginx "/etc/shadow" > /chroot/nginx/etc/shadow
[root@aek etc]# grep -i nginx "/etc/gshadow" > /chroot/nginx/etc/gshadow
[root@aek etc]# mv /etc/nginx /chroot/nginx/etc
[root@aek etc]# ln -s /chroot/nginx/etc/nginx /etc/nginx
Nginx kullanıcısı için gerekli dosyalar
Nginx ayar dosyaları chroot içine
[root@aek etc]# cd /chroot/nginx
[root@aek nginx]# ln -s usr/sbin/ sbin
[root@aek nginx]# ln -s usr/lib64 lib64
[root@aek nginx]# cd var/
[root@aek var]# ln -s ../run/ run
Gerekli symlink'ler
20. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Nginx Chroot
cp $(ldd /usr/sbin/nginx | grep /lib | sed -sre 's/(.+)(/usr/lib/S+).+/2/g') /chroot/nginx/lib64
Gerekli kütüphaneler chroot içine
[root@aek ~]# cp -a /usr/lib64/libnss_files-2.17.so /chroot/nginx/usr/lib64/
[root@aek ~]# cd /chroot/nginx/usr/lib64/
[root@aek lib64]# ln -s libnss_files-2.17.so libnss_files.so.2
[root@aek lib64]# chown -R nginx:root /chroot/nginx/var/cache/nginx/
Nginx kullanıcı için gerekli nss kütüphanesi
Cache dizin erişimi
[root@aek lib64]# cd /usr/share/nginx/html
[root@aek html]# cp -a * /chroot/nginx/usr/share/nginx/html/
Nginx html dosyaları
setcap 'cap_net_bind_service=+ep' /chroot/nginx/usr/sbin/nginx
Root kullanıcısı haricinde 1-1023 portları arası port açma izni
21. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Nginx Chroot
/etc/fstab
[Unit]
Description=Nginx (Chroot) - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/chroot/nginx/run/nginx.pid
ExecStartPre=/sbin/chroot --userspec=nginx:nginx /chroot/nginx /usr/sbin/nginx -t -q
ExecStart=/sbin/chroot --userspec=nginx:nginx /chroot/nginx /usr/sbin/nginx
ExecReload=/sbin/chroot --userspec=nginx:nginx /chroot/nginx /usr/sbin/nginx -s reload
ExecStop=/sbin/chroot --userspec=nginx:nginx /chroot/nginx /usr/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
tmpfs /chroot/nginx/run tmpfs rw,noexec,relatime,size=1024k 0 0
tmpfs /chroot/nginx/tmp tmpfs rw,noexec,relatime,size=102400k 0 0
/proc /chroot/nginx/proc auto bind 0 0
/sys /chroot/nginx/sys auto bind 0 0
/lib/systemd/system/nginx-chroot.service
22. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Nginx Chroot
[root@aek run]# systemctl status nginx-chroot
nginx-chroot.service - Nginx (Chroot) - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx-chroot.service; disabled)
Active: active (running) since Thu 2015-03-26 23:21:58 EET; 15min ago
Docs: http://nginx.org/en/docs/
Process: 24666 ExecStop=/sbin/chroot --userspec=nginx:nginx /chroot/nginx /usr/sbin/nginx -s quit (code=exited, status=0/SUCCESS)
Process: 24676 ExecStart=/sbin/chroot --userspec=nginx:nginx /chroot/nginx /usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 24675 ExecStartPre=/sbin/chroot --userspec=nginx:nginx /chroot/nginx /usr/sbin/nginx -t -q (code=exited, status=0/SUCCESS)
Main PID: 24679 (nginx)
CGroup: /system.slice/nginx-chroot.service
├─24679 nginx: master process /usr/sbin/nginx
└─24680 nginx: worker process
nginx 24679 0.0 0.0 45824 1124 ? Ss 23:21 0:00 nginx: master process /usr/sbin/nginx
nginx 24680 0.0 0.1 46344 2168 ? S 23:21 0:00 nginx: worker process
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 24679/nginx: master
24. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
PHP-FPM Chroot
FPM(FastCGI Process Manager) PHPFastCGI'a alternatif ve bazı ek özellikler eklenmiş
halidir.
CentOS7 sürümü resmi depolarında PHP-FPM bulunduğundan ek bir depo ekleme vb.
Gibi işlemler yapmaya gerek bulunmamaktadır.
yum install php-fpm
25. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
PHP-FPM Chroot
[test]
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
user = nginx
group = nginx
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
chroot = /chroot/php-fpm
chdir = /
php_admin_value[disable_functions] = dir,chdir,opendir,readdir
php_admin_value[error_log] = /var/log/php-fpm/test-error.log
php_admin_flag[log_errors] = on
php_admin_value[error_reporting] = E_ALL & ~E_NOTICE
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
/etc/php-fpm.d/test.conf
26. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
PHP-FPM Chroot
Chroot için gerekli dizinler
[root@aek ~]# cd /
[root@aek ~]# mkdir /chroot/php-fpm
[root@aek ~]# mkdir /chroot/php-fpm/dev
[root@aek ~]# mkdir /chroot/php-fpm/etc
[root@aek ~]# mkdir /chroot/php-fpm/lib64
[root@aek ~]# mkdir /chroot/php-fpm/srv-www
[root@aek ~]# mkdir /chroot/php-fpm/srv-www/test
[root@aek ~]# mkdir /chroot/php-fpm/usr
[root@aek ~]# mkdir /chroot/php-fpm/usr/lib64
[root@aek ~]# mkdir /chroot/php-fpm/usr/share
[root@aek ~]# mkdir /chroot/php-fpm/usr/share/zoneinfo
[root@aek ~]# mkdir /chroot/php-fpm/var/share/zoneinfo/Europe
mknod -m 0444 /chroot/nginx/dev/urandom c 1 9
Chroot için gerekli cihazlar
27. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
PHP-FPM Chroot
├── dev
│ └── urandom
├── etc
│ ├── host.conf
│ ├── hosts
│ ├── localtime -> ../usr/share/zoneinfo/Europe/Istanbul
│ └── resolv.conf
├── lib64
│ ├── libnss_dns-2.17.so
│ └── libnss_dns.so.2 -> libnss_dns-2.17.so
├── srv-www
│ └── test
│ ├── index.html
│ └── info.php
└── usr
├── lib64
│ └── libsoftokn3.so
└── share
└── zoneinfo
├── Europe
│ └── Istanbul
└── zone.tab
28. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
PHP-FPM Chroot
[test]
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
user = nginx
group = nginx
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/test-slow.log
chroot = /chroot/php-fpm
chdir = /
php_admin_value[disable_functions] = dir,chdir,opendir,readdir
php_admin_value[error_log] = /var/log/php-fpm/test-error.log
php_admin_flag[log_errors] = on
php_admin_value[error_reporting] = E_ALL & ~E_NOTICE
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
/etc/php-fpm.d/test.conf
29. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
PHP-FPM Chroot
location ~ .php$ {
try_files $uri = 404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
/etc/nginx/conf.d/test.conf
30. Özgür Yazılım ve Linux Günleri 2015 - Istanbul / 27.03.2015
Sonuç ve sorular ??
Ali Erdinç KöroğluAli Erdinç Köroğlu
aekoroglu@gmail.com - http://ae.koroglu.org -- http://twitter.com/erdinc