Tampilkan postingan dengan label FREEBSD. Tampilkan semua postingan
Tampilkan postingan dengan label FREEBSD. Tampilkan semua postingan

Minggu, 08 Mei 2011

Dtrace di FreeBSD

Proses tracing adalah salah satu bagian penting dari task seorang administrator sistem, melihat proses yang sedang berjalan, idle atau yang lainnya akan sangat membantu dalam proses pengaturan sistem itu sendiri. Banyak tools tracing yang tersedia, seperti  ktrace , strace dan yang lainnya. Salah satu yang cukup powerfull bagi saya adalah dtrace.
Dtrace adalah sebuah tools yang datang dengan berbagai fungsi dan kegunaan yang sangat membantu proses tracing yang dibuat oleh SUN dan menjadi bagian dari sistem operasi SunOS maupun OpenSolaris. Salah satu feature yang sangat menarik dari dtrace adalah penggunaan D  language sebagian scripting language buat dtrace itu sendiri.
FreeBSD sebagai salah satu sistem “Unix like” ikut memanfaatkan penggunaan dtrace sebagai salah satu tracing tool. Bagi para pengguna sistem ini, perlu sedikit modifikasi terhadap kernel, apabila ingin menggunakannya. Berikut langkah-langkah penggunaan dtrace di dalam sistem FreeBSD:
Pada langkah pertama saya menambahkan beberapa options untuk penggunaan dtrace, kemudian melakukan compile ulang pada kernel FreeBSD saya dengan option WITH_CTF (C Type Format).

vanzoel-21# uname -sr
FreeBSD 7.1-RELEASE-p2
vanzoel-21# cd /usr/src/sys/i386/conf/
vanzoel-21# tail -n 2 VANZOEL-21
options         KDTRACE_HOOKS
options         DDB_CTF
vanzoel-21# cd /usr/src/
vanzoel-21# make WITH_CTF=1 buildkernel KERNCONF=VANZOEL-21
vanzoel-21# make WITH_CTF=1 installkernel KERCONF=VANZOEL-21
vanzoel-21# reboot
 
Langkah kedua,, ane nge Load module dtraceall ke-dalam kernel FreeBSD,  jadi klo ingin melakukan secara otomatis setiap kali ketika sistem boot,,  edit file /boot/loader.conf ,,
vanzoel-21# kldload dtraceall
vanzoel-21# kldstat
Id Refs Address    Size     Name
1   12 0xc0400000 990bb8   kernel
2    1 0xc0d91000 20d8     dtraceall.ko
3    4 0xc0d94000 3f98     cyclic.ko
4    9 0xc0d98000 23cc     opensolaris.ko
5    7 0xc0d9b000 ba188    dtrace.ko
6    2 0xc0e56000 26d8     dtmalloc.ko
7    2 0xc0e59000 456c     fbt.ko
8    2 0xc0e5e000 2788     sdt.ko
9    2 0xc0e61000 e340     systrace.ko
10    2 0xc0e70000 33b0     profile.ko
11    1 0xc0e74000 6a2c4    acpi.ko
vanzoel-21# cat /boot/loader.conf
dtraceall_load="YES"

Seperti terlihat diatas ni,, module dtrace udah di load oleh kernel,, untuk mencobanya,, buat script sederhana sbb :

vanzoel-21# cat counter.d
dtrace:::BEGIN
{
i = 10;
}

profile:::tick-1sec
/i > 0/
{
trace(i--);
}

profile:::tick-1sec
/i == 0/
{
trace("blastoff!");
exit(0);
}
vanzoel-21# dtrace -s counter.d
dtrace: script 'counter.d' matched 3 probes
CPU     ID                    FUNCTION:NAME
0  33203                       :tick-1sec        10
0  33203                       :tick-1sec         9
0  33203                       :tick-1sec         8
0  33203                       :tick-1sec         7
0  33203                       :tick-1sec         6
0  33203                       :tick-1sec         5
0  33203                       :tick-1sec         4
0  33203                       :tick-1sec         3
0  33203                       :tick-1sec         2
0  33203                       :tick-1sec         1
0  33203                       :tick-1sec   blastoff!
Dtrace sendiri buanyak memiliki fungsi-fungsi yang bisa anda gunakan untuk keperluan tracing, dokumentasinya bisa anda baca disini,

vanzoel-21# dtrace -l | more
ID   PROVIDER            MODULE                          FUNCTION NAME
1     dtrace                                                     BEGIN
2     dtrace                                                     END
3     dtrace                                                     ERROR
4   dtmalloc                                          madt_table malloc
5   dtmalloc                                          madt_table free
6   dtmalloc                                             acpidev malloc
7   dtmalloc                                             acpidev free
8   dtmalloc                                                 fbt malloc
9   dtmalloc                                                 fbt free
10   dtmalloc                                              apmdev malloc
11   dtmalloc                                              apmdev free
vanzoel-21# dtrace -l | wc -l
33204
 
Ok, Selamat bereksplorasi dengan dtrace di FreeBSD ,, :)

visit kecoak-E

Typo bugs fmtmsg() di FreeBSD

FreeBSD hacker juga manusia :P.. Hal tersebut terbukti pada sebuah kesalahan kecil yang dibuat oleh developer FreeBSD pada file /usr/src/lib/libc/gen/fmtmsg.c, khususnya yang ada pada fungsi printfmt().

01    /*

02     * Returns NULL on memory allocation failure,otherwise returns a   pointer to
03     * a newly malloc()'d output buffer.
04     */

05    static char *

06    printfmt(char *msgverb, long class, const char *label, int sev,

07        const char *text, const char *act, const char *tag)

08    {
09            size_t size;

10            char *comp, *output;

11            const char *sevname;

12

13            size = 32;

14        ...

15            if (text != MM_NULLTXT)
16                    size += strlen(text);

17            if (text != MM_NULLACT)

18                    size += strlen(act);

19       ...

20            if ((output = malloc(size)) == NULL)

21       ...

22            return (output);

23    }
Seperti diketahui,, fmtmsg adalah sebuah sebuah routines yang menghandle display message pada standard error dan atau system console. Berdasarkan manual page dari fmtmsg itu sendiri,, kita bisa men-trigger bugs yang dimaksud dengan code sederhana kaya gini misal nya :

1#include <fmtmsg.h>
2
3int main(int argc, char * argv[])
4{
5fmtmsg(MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR,
6"illegal option -- z", MM_NULLACT, "BSD:ls:001");
7return 0;
8}</fmtmsg.h>
dan hasilnya…

1# gcc -o fmtbugs fmtbugs.c
2# ./fmtbugs
3Segmentation fault (core dumped)
Ok, kenapa bisa kondisi Segfault terjadi ??.
Jadi hal tersebut terjadi karena pada saat variable “MM_NULLACT” di check apakah bernilai NULL atau tidak,, developer FreeBSD ‘tidak sengaja’ mengambil argument ‘text’ yang pada contoh code diatas bernilai “illegal option — z” dan tentunya akan terjadi segmentation fault karena mengambil nilai yang salah. Seharusnya nilai yang diambil adalah argument ‘act’ yang pada contoh code diatas bernilai MM_NULLACT.


Info bugs ini ane ambil dari  XORL blog dan FreeBSD Official site.

sumber kecoak-E

Rabu, 04 Mei 2011

Configurasi Squid di Freebsd

Code

# tar zxvf squid-2.7.STABLE9.tar.gz
# cd  squid-2.7.STABLE9
# ./configure '--sysconfdir=/etc/squid' '--enable-storeio=diskd,ufs,aufs' '--enable-delay-pools' \
'--enable-pf-transparent' '--enable-ipf-transparent' '--disable-ident-lookups' \
'--enable-removal-policies'
# make
# make install

* PENJELASAN
Quote:

- Enable-delay-pools - Enable delay pools untuk membatasi penggunaan bandwidth.
 Jadi kita harus mengaktifkan pilihan untuk menggunakan Squid untuk membatasi penggunaan bandwidth. Ini akan memberikan penggunaan bandwidth yang adil untuk semua orang. Dalam kasus saya ini,, saya tidak mau satu orang mengisap semua bandwidth yang tersedia dengan men-download film besar,, menyebabkan orang lain lemot.

- Enable-IPF-transparan - Transparent Proxy Mengaktifkan dukungan untuk sistem yang menggunakan IP Filter network address redirection.
Dengan pilihan ini, Anda tidak harus mengkonfigurasi pengaturan proxy browser klien. Juga merupakan cara yang baik untuk memaksa klien untuk menggunakan setiap proxy.

- Enable-storeio = diskd, ufs - Enable diskd
Disk Meningkatkan performa I / O. Menurut FAQ squid, jika Anda mengaktifkan diskd Anda dapat memperoleh peningkatan 400% dari kinerja. Namun, Anda perlu mengkompilasi ulang kernel karena sistem operasi Anda harus mendukung antrian pesan dan memori bersama.

-enable-removal-policie - Membangun dukungan untuk daftar kebijakan penghapusan.
Secara default, Squid menggunakan LRU, tapi ada dua kebijakan yang lebih baik: GDSF dan LFUDA. Lihat Squid config untuk penjelasan lebih rinci.

- Disable-ident-lookups - ini memungkinkan Anda untuk menghapus kode yang melakukan ident (RFC 931) lookup.
Tidak benar-benar penting. By the way, jika Anda transparent proxy, ident lookups tidak akan bekerja.

- Enable-snmp
Opsional: fitur ini dan Anda dapat memonitor squid dengan MRTG atau RRDTool. Cara melakukan ini adalah di luar lingkup artikel ini. Mungkin dalam satu berikutnya.

SQUID.CONF

#Transparent Proxy
http_port 127.0.0.1:8080 transparent
http_port 192.168.100.2:8080

dead_peer_timeout 30 seconds
peer_connect_timeout 30 seconds
icp_query_timeout 5000

#Regex for download-file
acl download-file urlpath_regex -i "/etc/squid/download-file"
acl download-spesial urlpath_regex -i "/etc/squid/download-spesial"

acl QUERY urlpath_regex cgi-bin \? \.php$ \.asp$ \.shtml$ \.cfm$ \.cfml$ \.phtml$ \.php3$
acl nocache-domain dstdomain .mail.yahoo.com .login.yahoo.com .gmail.com .rapidshare.de .rapidshare.com
no_cache deny QUERY
no_cache deny nocache-domain

acl myself dst 127.0.0.1 192.168.100.2
always_direct allow myself

always_direct allow nocache-domain
always_direct allow QUERY

cache_mem 96 MB
#How many squid will use space of harrdisk.
#You can use my formula, use 80% from free space of harddisk use for cache
#64 Number of directory
#128 Number of sub-directory for each directory
cache_dir aufs /cache 20000 64 128

maximum_object_size 4096 KB
minimum_object_size 4 KB
maximum_object_size_in_memory 16 KB
ipcache_size 4096
fqdncache_size 4096

logformat custom %{%Y-%m-%d %H:%M:%S}tl %03tu %>a %tr %ul %ui %Hs %mt %rm %ru %rv %st %Sh %Ss
#cache_access_log /var/squid/logs/access.log custom
cache_access_log none
cache_log /var/squid/logs/cache.log custom
cache_store_log none
pid_filename /var/squid/logs/squid.pid

refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i \.gif$ 10080 100% 43200
refresh_pattern -i \.jpg$ 10080 100% 43200
refresh_pattern -i \.jpeg$ 10080 100% 43200
refresh_pattern -i \.bmp$ 10080 100% 43200
refresh_pattern -i \.mid$ 10080 100% 43200
refresh_pattern -i \.wav$ 10080 100% 43200
refresh_pattern -i \.ico$ 10080 100% 43200
refresh_pattern -i \.yim$ 10080 100% 43200
refresh_pattern -i \.jar$ 10080 100% 43200
refresh_pattern -i \.ldict$ 10080 100% 43200
refresh_pattern -i \.swf$ 10080 100% 43200
refresh_pattern -i \.class$ 10080 100% 43200
refresh_pattern -i \.cab$ 10080 100% 43200
refresh_pattern . 10 100% 10080

negative_ttl 5 minutes
positive_dns_ttl 6 hours
negative_dns_ttl 1 minute

connect_timeout 60 seconds
request_timeout 3 minutes
persistent_request_timeout 1 minute
read_timeout 15 minutes
client_lifetime 1 day
half_closed_clients off
pconn_timeout 2 minutes
shutdown_lifetime 1 seconds

#Access List
#I have two subnets, one for 'user' and another one for 'spesial'
acl all src 0.0.0.0/0.0.0.0
acl localhost src 127.0.0.1/255.255.255.255
acl user src 192.168.200.0/255.255.255.0
acl spesial src 192.168.200.102 192.168.200.103

http_access allow user
http_access deny all

http_reply_access allow user
http_reply_access deny all

icp_access allow user
icp_access deny all

miss_access allow user
miss_access deny all

cache_mgr nofee26@mustnofee.com
cache_effective_user squid
cache_effective_group squid
visible_hostname Proxy
coredump_dir /cache

forwarded_for off
log_icp_queries off

delay_pools 2

#Bandwidth for 'spesial'
#When file more than 512KB, then 'spesial' will get 20KB = 160kbps
delay_class 1 2
delay_parameters 1 -1/-1 20000/512000
delay_access 1 allow spesial download-spesial
delay_access 1 deny all

#Bandwidth for 'user'
#When file more than 256KB, then 'user' will get 8KB = 64kbps
delay_class 2 2
delay_parameters 2 -1/-1 8000/256000
delay_access 2 allow user download-file
delay_access 2 deny all

via off

server_persistent_connections off
client_persistent_connections off
*.  Membuat dan configurasi cache directory, swap, and log file

# mkdir -p /var/squid/logs
# chmod 777 /var/squid/logs/
# chmod 777 /cache
# chmod 777 /etc/squid/
# chmod 777 /dev/pf/
# /usr/local/squid/sbin/squid -z

*. Configurasi pf.conf for Transparent Proxy

#add this line after nat :
rdr on $user_if proto tcp from 192.168.200.0/24 to any port 80 -> 127.0.0.1 port 8080
 #!/bin/sh
# By No Fee (c) 2007

case "$1" in
start)
echo "Starting Squid..."
/usr/local/squid/sbin/squid -D
;;
stop)
echo "Stoping Squid..."
/usr/local/squid/sbin/squid -k shutdown
;;
restart)
echo "Restarting Squid..."
/usr/local/squid/sbin/squid -k reconfigure
;;
rotate)
echo "Rotating Squid Log..."
/usr/local/squid/sbin/squid -k rotate
;;
ver)
echo "You're using : "
/usr/local/squid/sbin/squid -v
;;
*)
echo "Usage: `basename $0` {start|stop|restart|rotate|ver}" >&2
exit 64
;;
esac


Save di : /usr/sbin/squid
rubah dah file permission : chmod 755 /usr/sbin/squid  ,,

^_^

Visit to devilzc0de.org/forum