Linux & Security
2021-11-15 @ KNI Kernel AGH UST
By Disconnect3d
1
# whoami
�@disconnect3d_pl�https://disconnect3d.pl/
#sponsors
3
4
Trail of Bits
Blog: https://blog.trailofbits.com/ oraz https://trailofbits.com/
Sporo klientów/produktów:�Kubernetes, HashiCorp, Microsoft, Zoom, Parity, blockchainy, etc.
Publikacje: https://github.com/trailofbits/publications
Ofc zatrudniamy, również na płatne staże (full remote): https://jobs.lever.co/trailofbits/
5
6
User Space vs Kernel Space
7
User Space vs Kernel Space
Source: https://imgur.com/Oufl1R1 (or https://www.reddit.com/r/AskProgramming/comments/7wzkj7/user_space_and_kernel_space/)
8
User Space vs Kernel Space
Source: https://imgur.com/Oufl1R1 (or https://www.reddit.com/r/AskProgramming/comments/7wzkj7/user_space_and_kernel_space/)
9
IZOLACJA
+ kernel może “wszystko”
Jak to właściwie jest odseparowane?
==> ficzery architektur procesorów!
10
Intel Architecture Protection Rings
11
Intel Architecture Protection Rings
12
FWIW de facto tylko ring0 i ring3 są używane �w Linuxie*
ARM privilege/exception levels
Via: https://developer.arm.com/documentation/102412/0100/Privilege-and-Exception-levels
13
ARM privilege/exception levels
Via: https://developer.arm.com/documentation/102412/0100/Privilege-and-Exception-levels
14
Kernel space
User space
Wywołania systemowe�(syscalle)
czyli sposób na gadanie z kernelem
15
Syscalle
16
#include <stdio.h>
int main() {
FILE* fp = fopen("plik", "w");
// TODO: check fp == NULL
fwrite("content", 4, 1, fp);
fclose(fp);
}
Syscalle
17
#include <stdio.h>
int main() {
FILE* fp = fopen("plik", "w");
// TODO: check fp == NULL
fwrite("content", 4, 1, fp);
fclose(fp);
}
* zadziała tylko jak jest wyłączone FULL RELRO
Syscalle
18
#include <stdio.h>
int main() {
FILE* fp = fopen("plik", "w");
// TODO: check fp == NULL
fwrite("content", 4, 1, fp);
fclose(fp);
}
* zadziała tylko jak jest wyłączone FULL RELRO
Syscalle
19
#include <stdio.h>
int main() {
FILE* fp = fopen("plik", "w");
// TODO: check fp == NULL
fwrite("content", 4, 1, fp);
fclose(fp);
}
* zadziała tylko jak jest wyłączone FULL RELRO
Syscalle
20
#include <stdio.h>
int main() {
FILE* fp = fopen("plik", "w");
// TODO: check fp == NULL
fwrite("content", 4, 1, fp);
fclose(fp);
}
* zadziała tylko jak jest wyłączone FULL RELRO
Przykład ioctl na /dev/ptmx(?)�https://github.com/torvalds/linux/blob/fa55b7dcdc43c1aa1ba12bca9d2dd4318c2a0dbf/drivers/tty/pty.c#L652-L671
21
Socket ładujący moduł kernela
#include <sys/socket.h>
#include <netatalk/at.h>
�int main() {
int ddp_socket = socket(AF_APPLETALK, SOCK_DGRAM, 0);
printf("ddp_socket = %d\n", ddp_socket);
}
// DEMO
22
Socket ładujący moduł kernela
#include <sys/socket.h>
#include <netatalk/at.h>
�int main() {
int ddp_socket = socket(AF_APPLETALK, SOCK_DGRAM, 0);
printf("ddp_socket = %d\n", ddp_socket);
}
23
User w Linuxie
24
Nazwa usera to tylko abstrakcja
dc@dc:~$ ps auxf
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 164728 11128 ? Ss 18:05 0:00 /sbin/init splash
root 2 0.0 0.0 0 0 ? S 18:05 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< 18:05 0:00 \_ [rcu_gp]
root 4 0.0 0.0 0 0 ? I< 18:05 0:00 \_ [rcu_par_gp]
root 6 0.0 0.0 0 0 ? I< 18:05 0:00 \_ [kworker/0:0H-events_highpri]
[...]
root 1904 0.0 0.0 3148532 12600 ? Sl 18:05 0:01 /opt/teamviewer/tv_bin/teamviewerd -d
dc 2274 0.0 0.0 16240 9676 ? Ss 18:06 0:00 /lib/systemd/systemd --user
dc 5302 0.0 0.0 739576 45856 ? Sl 18:07 0:00 | \_ update-notifier
dc 2520 1.4 0.2 3905684 368372 ? Ssl 18:06 0:36 \_ /usr/bin/gnome-shell
dc 2541 0.0 0.0 320992 11724 ? Sl 18:06 0:00 | \_ ibus-daemon --panel disable --xim
dc 2546 0.0 0.0 243612 7504 ? Sl 18:06 0:00 | | \_ /usr/libexec/ibus-dconf
dc 2547 0.0 0.0 279276 30560 ? Sl 18:06 0:00 | | \_ /usr/libexec/ibus-extension-gtk3
dc 2810 0.0 0.0 169780 7384 ? Sl 18:06 0:00 | | \_ /usr/libexec/ibus-engine-simple
dc 6412 0.1 0.0 771148 71224 ? Sl 18:10 0:03 | \_ /usr/bin/python3 /usr/bin/terminator
dc 6437 0.0 0.0 17396 6016 pts/0 Ss 18:10 0:00 | | \_ /bin/bash
dc 9009 0.0 0.0 18892 4256 pts/0 R+ 18:46 0:00 | | | \_ ps auxf
25
Nazwa usera to tylko abstrakcja
dc@dc:~$ ps auxf
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 164728 11128 ? Ss 18:05 0:00 /sbin/init splash
root 2 0.0 0.0 0 0 ? S 18:05 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< 18:05 0:00 \_ [rcu_gp]
root 4 0.0 0.0 0 0 ? I< 18:05 0:00 \_ [rcu_par_gp]
root 6 0.0 0.0 0 0 ? I< 18:05 0:00 \_ [kworker/0:0H-events_highpri]
[...]
root 1904 0.0 0.0 3148532 12600 ? Sl 18:05 0:01 /opt/teamviewer/tv_bin/teamviewerd -d
dc 2274 0.0 0.0 16240 9676 ? Ss 18:06 0:00 /lib/systemd/systemd --user
dc 5302 0.0 0.0 739576 45856 ? Sl 18:07 0:00 | \_ update-notifier
dc 2520 1.4 0.2 3905684 368372 ? Ssl 18:06 0:36 \_ /usr/bin/gnome-shell
dc 2541 0.0 0.0 320992 11724 ? Sl 18:06 0:00 | \_ ibus-daemon --panel disable --xim
dc 2546 0.0 0.0 243612 7504 ? Sl 18:06 0:00 | | \_ /usr/libexec/ibus-dconf
dc 2547 0.0 0.0 279276 30560 ? Sl 18:06 0:00 | | \_ /usr/libexec/ibus-extension-gtk3
dc 2810 0.0 0.0 169780 7384 ? Sl 18:06 0:00 | | \_ /usr/libexec/ibus-engine-simple
dc 6412 0.1 0.0 771148 71224 ? Sl 18:10 0:03 | \_ /usr/bin/python3 /usr/bin/terminator
dc 6437 0.0 0.0 17396 6016 pts/0 Ss 18:10 0:00 | | \_ /bin/bash
dc 9009 0.0 0.0 18892 4256 pts/0 R+ 18:46 0:00 | | | \_ ps auxf
26
Nazwa usera to tylko abstrakcja
dc@dc:~$ ps auxf
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 164728 11128 ? Ss 18:05 0:00 /sbin/init splash
root 2 0.0 0.0 0 0 ? S 18:05 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< 18:05 0:00 \_ [rcu_gp]
root 4 0.0 0.0 0 0 ? I< 18:05 0:00 \_ [rcu_par_gp]
root 6 0.0 0.0 0 0 ? I< 18:05 0:00 \_ [kworker/0:0H-events_highpri]
[...]
root 1904 0.0 0.0 3148532 12600 ? Sl 18:05 0:01 /opt/teamviewer/tv_bin/teamviewerd -d
dc 2274 0.0 0.0 16240 9676 ? Ss 18:06 0:00 /lib/systemd/systemd --user
dc 5302 0.0 0.0 739576 45856 ? Sl 18:07 0:00 | \_ update-notifier
dc 2520 1.4 0.2 3905684 368372 ? Ssl 18:06 0:36 \_ /usr/bin/gnome-shell
dc 2541 0.0 0.0 320992 11724 ? Sl 18:06 0:00 | \_ ibus-daemon --panel disable --xim
dc 2546 0.0 0.0 243612 7504 ? Sl 18:06 0:00 | | \_ /usr/libexec/ibus-dconf
dc 2547 0.0 0.0 279276 30560 ? Sl 18:06 0:00 | | \_ /usr/libexec/ibus-extension-gtk3
dc 2810 0.0 0.0 169780 7384 ? Sl 18:06 0:00 | | \_ /usr/libexec/ibus-engine-simple
dc 6412 0.1 0.0 771148 71224 ? Sl 18:10 0:03 | \_ /usr/bin/python3 /usr/bin/terminator
dc 6437 0.0 0.0 17396 6016 pts/0 Ss 18:10 0:00 | | \_ /bin/bash
dc 9009 0.0 0.0 18892 4256 pts/0 R+ 18:46 0:00 | | | \_ ps auxf
27
Nazwa usera to tylko abstrakcja
dc@dc:~$ ps auxf
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 164728 11128 ? Ss 18:05 0:00 /sbin/init splash
root 2 0.0 0.0 0 0 ? S 18:05 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< 18:05 0:00 \_ [rcu_gp]
root 4 0.0 0.0 0 0 ? I< 18:05 0:00 \_ [rcu_par_gp]
root 6 0.0 0.0 0 0 ? I< 18:05 0:00 \_ [kworker/0:0H-events_highpri]
[...]
root 1904 0.0 0.0 3148532 12600 ? Sl 18:05 0:01 /opt/teamviewer/tv_bin/teamviewerd -d
dc 2274 0.0 0.0 16240 9676 ? Ss 18:06 0:00 /lib/systemd/systemd --user
dc 5302 0.0 0.0 739576 45856 ? Sl 18:07 0:00 | \_ update-notifier
dc 2520 1.4 0.2 3905684 368372 ? Ssl 18:06 0:36 \_ /usr/bin/gnome-shell
dc 2541 0.0 0.0 320992 11724 ? Sl 18:06 0:00 | \_ ibus-daemon --panel disable --xim
dc 2546 0.0 0.0 243612 7504 ? Sl 18:06 0:00 | | \_ /usr/libexec/ibus-dconf
dc 2547 0.0 0.0 279276 30560 ? Sl 18:06 0:00 | | \_ /usr/libexec/ibus-extension-gtk3
dc 2810 0.0 0.0 169780 7384 ? Sl 18:06 0:00 | | \_ /usr/libexec/ibus-engine-simple
dc 6412 0.1 0.0 771148 71224 ? Sl 18:10 0:03 | \_ /usr/bin/python3 /usr/bin/terminator
dc 6437 0.0 0.0 17396 6016 pts/0 Ss 18:10 0:00 | | \_ /bin/bash
dc 9009 0.0 0.0 18892 4256 pts/0 R+ 18:46 0:00 | | | \_ ps auxf
28
Nazwa usera to tylko abstrakcja
dc@dc:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
[...]
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
dc:x:1000:1000:dc,,,:/home/dc:/bin/bash
[...]
29
Nazwa usera to tylko abstrakcja
dc@dc:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
[...]
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
dc:x:1000:1000:dc,,,:/home/dc:/bin/bash
[...]
30
Nazwa usera to tylko abstrakcja
dc@dc:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
[...]
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
dc:x:1000:1000:dc,,,:/home/dc:/bin/bash
[...]
31
Nazwa usera to tylko abstrakcja
dc@dc:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
[...]
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
dc:x:1000:1000:dc,,,:/home/dc:/bin/bash
[...]
32
Nazwa usera to tylko abstrakcja
dc@dc:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
[...]
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
dc:x:1000:1000:dc,,,:/home/dc:/bin/bash
[...]
33
Nazwa usera to tylko abstrakcja
dc@dc:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
[...]
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
dc:x:1000:1000:dc,,,:/home/dc:/bin/bash
[...]
^-- opis formatu w: man 5 passwd
34
Nazwa usera to tylko abstrakcja
dc@dc:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
[...]
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
dc:x:1000:1000:dc,,,:/home/dc:/bin/bash
[...]
^-- opis formatu w: man 5 passwd
dc@dc:~$ cat /etc/shadow
cat: /etc/shadow: Permission denied
dc@dc:~$ sudo cat /etc/shadow
root:!:18859:0:99999:7:::
daemon:*:18737:0:99999:7:::
bin:*:18737:0:99999:7:::
sys:*:18737:0:99999:7:::
[...]
dc:$6$joW3tLtIp1eZE2/y$w5y**REDACTED**zQn2pUoK3.:18859:0:99999:7:::
35
Nazwa usera to tylko abstrakcja
dc@dc:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
[...]
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
dc:x:1000:1000:dc,,,:/home/dc:/bin/bash
[...]
^-- opis formatu w: man 5 passwd
dc@dc:~$ cat /etc/shadow
cat: /etc/shadow: Permission denied
dc@dc:~$ sudo cat /etc/shadow
root:!:18859:0:99999:7:::
daemon:*:18737:0:99999:7:::
bin:*:18737:0:99999:7:::
sys:*:18737:0:99999:7:::
[...]
dc:$6$joW3tLtIp1eZE2/y$w5y**REDACTED**zQn2pUoK3.:18859:0:99999:7:::
36
Nazwa usera to tylko abstrakcja
dc@dc:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
[...]
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
dc:x:1000:1000:dc,,,:/home/dc:/bin/bash
[...]
^-- opis formatu w: man 5 passwd
dc@dc:~$ cat /etc/shadow
cat: /etc/shadow: Permission denied
dc@dc:~$ sudo cat /etc/shadow
root:!:18859:0:99999:7:::
daemon:*:18737:0:99999:7:::
bin:*:18737:0:99999:7:::
sys:*:18737:0:99999:7:::
[...]
dc:$6$joW3tLtIp1eZE2/y$w5y**REDACTED**zQn2pUoK3.:18859:0:99999:7:::
37
Nazwa usera to tylko abstrakcja
dc@dc:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
[...]
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
dc:x:1000:1000:dc,,,:/home/dc:/bin/bash
[...]
^-- opis formatu w: man 5 passwd
dc@dc:~$ cat /etc/shadow
cat: /etc/shadow: Permission denied
dc@dc:~$ sudo cat /etc/shadow
root:!:18859:0:99999:7:::
daemon:*:18737:0:99999:7:::
bin:*:18737:0:99999:7:::
sys:*:18737:0:99999:7:::
[...]
dc:$6$joW3tLtIp1eZE2/y$w5y**REDACTED**zQn2pUoK3.:18859:0:99999:7:::
38
Nazwa usera to tylko abstrakcja
dc@dc:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
[...]
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
dc:x:1000:1000:dc,,,:/home/dc:/bin/bash
[...]
^-- opis formatu w: man 5 passwd
dc@dc:~$ cat /etc/shadow
cat: /etc/shadow: Permission denied
dc@dc:~$ sudo cat /etc/shadow
root:!:18859:0:99999:7:::
daemon:*:18737:0:99999:7:::
bin:*:18737:0:99999:7:::
sys:*:18737:0:99999:7:::
[...]
dc:$6$joW3tLtIp1eZE2/y$w5y**REDACTED**zQn2pUoK3.:18859:0:99999:7:::
39
Nazwa usera to tylko abstrakcja
dc@dc:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
[...]
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
dc:x:1000:1000:dc,,,:/home/dc:/bin/bash
[...]
^-- opis formatu w: man 5 passwd
dc@dc:~$ cat /etc/shadow
cat: /etc/shadow: Permission denied
dc@dc:~$ sudo cat /etc/shadow
root:!:18859:0:99999:7:::
daemon:*:18737:0:99999:7:::
bin:*:18737:0:99999:7:::
sys:*:18737:0:99999:7:::
[...]
dc:$6$joW3tLtIp1eZE2/y$w5y**REDACTED**zQn2pUoK3.:18859:0:99999:7:::
^-- opis formatu w: man 5 shadow
Również:
40
Wracając do “nazwa usera to tylko abstrakcja”
41
W kernel nie ma nazwy użytkowników:�jest za to struct_task i cred
42
struct task_struct
struct task_struct {
int exit_code;
pid_t pid;
/* Process credentials: */
/* Tracer's credentials at attach: */
const struct cred __rcu *ptracer_cred;
/* Objective and real subjective task credentials (COW): */
const struct cred __rcu *real_cred;
/* Effective (overridable) subjective task credentials (COW): */
const struct cred __rcu *cred;
/* Used by LSM modules for access restriction: */
void *security;
/* CPU-specific state of this task: */
struct thread_struct thread;
https://elixir.bootlin.com/linux/v5.15.2/source/include/linux/sched.h#L723
43
=== struktura która istnieje dla każdego procesu/wątku (+wątków kernela)
struct task_struct
struct task_struct {
int exit_code;
pid_t pid;
/* Process credentials: */
/* Tracer's credentials at attach: */
const struct cred __rcu *ptracer_cred;
/* Objective and real subjective task credentials (COW): */
const struct cred __rcu *real_cred;
/* Effective (overridable) subjective task credentials (COW): */
const struct cred __rcu *cred;
/* Used by LSM modules for access restriction: */
void *security;
/* CPU-specific state of this task: */
struct thread_struct thread;
https://elixir.bootlin.com/linux/v5.15.2/source/include/linux/sched.h#L723
44
=== czyli to co zwraca funkcja “main” w C
struct task_struct
struct task_struct {
int exit_code;
pid_t pid;
/* Process credentials: */
/* Tracer's credentials at attach: */
const struct cred __rcu *ptracer_cred;
/* Objective and real subjective task credentials (COW): */
const struct cred __rcu *real_cred;
/* Effective (overridable) subjective task credentials (COW): */
const struct cred __rcu *cred;
/* Used by LSM modules for access restriction: */
void *security;
/* CPU-specific state of this task: */
struct thread_struct thread;
https://elixir.bootlin.com/linux/v5.15.2/source/include/linux/sched.h#L723
45
=== Process Identifier (PID)
Problemy z PIDami...
46
Problemy z PIDami...
47
Problemy z PIDami...
48
== PIDy są inkrementalne (na Linuxie)
Problemy z PIDami...
49
== PIDy są inkrementalne (na Linuxie)
kernel.pid_max = 32768
Problemy z PIDami...
50
== PIDy są inkrementalne (na Linuxie)
kernel.pid_max = 32768
Co gdy wykorzystamy?
Problemy z PIDami...
51
== PIDy są inkrementalne (na Linuxie)
kernel.pid_max = 32768
Co gdy wykorzystamy?
int main() {
while (1) {
int p = fork();
if (p <= 0) break;
printf("PID = %d\n", p);
wait(NULL);
}
}
// może wymagać uruchomienia kilka razy / przykład via https://saelo.github.io/presentations/warcon18_dont_trust_the_pid.pdf
Problemy z PIDami...
52
== PIDy są inkrementalne (na Linuxie)
kernel.pid_max = 32768
Co gdy wykorzystamy?
...
PID = 32765
PID = 32766
PID = 32767
PID = 300
PID = 301
PID = 302
PID = 303
...
int main() {
while (1) {
int p = fork();
if (p <= 0) break;
printf("PID = %d\n", p);
wait(NULL);
}
}
// może wymagać uruchomienia kilka razy / przykład via https://saelo.github.io/presentations/warcon18_dont_trust_the_pid.pdf
“overflow”
Problemy z PIDami...
53
kernel.pid_max = 32768
(wysyłanie sygnałów, wyświetlanie o nich info, ptrace, dodawanie do cgroup, itd.)
== PIDy są inkrementalne (na Linuxie)
===> pid-reuse attack
Przykład za https://saelo.github.io/presentations/warcon18_dont_trust_the_pid.pdf
54
===> pid-reuse attack
2. Co jeśli PO security_check dany PID umrze i pojawi się inny na jego miejscu?
Przykład za https://saelo.github.io/presentations/warcon18_dont_trust_the_pid.pdf
55
===> pid-reuse attack
2. Co jeśli PO security_check dany PID umrze i pojawi się inny na jego miejscu?
3. ...i jeśli perform_action daje coś danemu PIDowi? :)
Przykład za https://saelo.github.io/presentations/warcon18_dont_trust_the_pid.pdf
56
===> pid-reuse attack
2. Co jeśli PO security_check dany PID umrze i pojawi się inny na jego miejscu?
3. ...i jeśli perform_action daje coś danemu PIDowi? :)
===> jeśli atakujący umie sprawić, żeby PID umarł po security_check i
i stworzyć własny proces z danym PID, to może np. zyskać uprawnienia
Przykład za https://saelo.github.io/presentations/warcon18_dont_trust_the_pid.pdf
57
Ja zapobiegać pid-reuse?
TL;DR: pidfd
...taki nowy ficzer kernela, ale nie działa ze wszystkim jeszcze
See also:
58
Wracając do�struct task_struct
59
struct task_struct
struct task_struct {
int exit_code;
pid_t pid;
/* Process credentials: */
/* Tracer's credentials at attach: */
const struct cred __rcu *ptracer_cred;
/* Objective and real subjective task credentials (COW): */
const struct cred __rcu *real_cred;
/* Effective (overridable) subjective task credentials (COW): */
const struct cred __rcu *cred;
/* Used by LSM modules for access restriction: */
void *security;
/* CPU-specific state of this task: */
struct thread_struct thread;
https://elixir.bootlin.com/linux/v5.15.2/source/include/linux/sched.h#L723
60
struct cred od “credentials”
Tu siedzi info o “użytkownikach”
struct task_struct
struct task_struct {
int exit_code;
pid_t pid;
/* Process credentials: */
/* Tracer's credentials at attach: */
const struct cred __rcu *ptracer_cred;
/* Objective and real subjective task credentials (COW): */
const struct cred __rcu *real_cred;
/* Effective (overridable) subjective task credentials (COW): */
const struct cred __rcu *cred;
/* Used by LSM modules for access restriction: */
void *security;
/* CPU-specific state of this task: */
struct thread_struct thread;
https://elixir.bootlin.com/linux/v5.15.2/source/include/linux/sched.h#L723
61
Dodatkowe info o procesie, używane przez LSMy typu AppArmor/SELinux/Yama etc
struct cred
62
struct cred
63
FWIW tylko debug...
struct cred
64
struct cred
65
struct cred
66
struct cred
67
struct cred
68
struct cred
69
Pliki, uprawnienia
70
Pliki, ich typy i uprawnienia
71
Pliki, ich typy i uprawnienia
72
Typ pliku
Pliki, ich typy i uprawnienia
73
katalog d
unix domain socket s
fifo (named pipe) p
link symboliczny l�zwykły plik -
urządzenie znakowe c
urządzenie blokowe b
Typ pliku
Pliki, ich typy i uprawnienia
74
Uprawnienia� (owner/group/others)
Pliki, ich typy i uprawnienia
75
owner + group
Pliki, ich typy i uprawnienia
76
Rozmiar pliku�LUB
Major + minor device number�(potrzebne do utworzenia pliku urządzenia � przez mknod)
Uprawnienia plików tzn. to całe:
�- rwx rw- ---�(owner group other)
�ma 12 bitów
(Mimo że wydawałoby się że 3*3=9)
77
Uprawnienia plików mają 12 bitów
OWNER GROUP OTHER
Bity: 0 0 0 1 1 0 1 1 0 1 0 0
r w x r w x r w x
Inspired by https://twitter.com/b0rk/status/982641594305273856/photo/1
78
Uprawnienia plików mają 12 bitów
OWNER GROUP OTHER
Bity: 0 0 0 1 1 0 1 1 0 1 0 0
r w x r w x r w x
Inspired by https://twitter.com/b0rk/status/982641594305273856/photo/1
79
Read => 4 R+W+X = 7
Write => 2 R+W = 6
eXecute => 1 R = 4
Uprawnienia plików mają 12 bitów
OWNER GROUP OTHER
Bity: 0 0 0 1 1 0 1 1 0 1 0 0
r w x r w x r w x
0 5 5 4
Inspired by https://twitter.com/b0rk/status/982641594305273856/photo/1
80
Btw. jak ustawiamy uprawnienia?
81
Bash: chmod 740 plik
Python: os.chmod("plik", 740)
Go: os.Chmod("plik", 740)
Java: N sposobów...
82
Java - na przykład:
Via https://stackoverflow.com/questions/664432/how-do-i-programmatically-change-file-permissions
83
Bash: chmod 740 plik
Python: os.chmod("plik", 740)
Go: os.Chmod("plik", 740)
Java: N sposobów...
84
Coś w tym złego?
(DEMO)
Bash: chmod 740 plik
Python: os.chmod("plik", 740)
Go: os.Chmod("plik", 740)
Java: N sposobów...
85
Coś w tym złego?
Czy ktoś tak robi?
86
Tak w ogóle kiedy są sprawdzane �uprawnienia do plików?
Przy open?�Czy przy read/write?
88
Tak w ogóle kiedy są sprawdzane �uprawnienia do plików?
Przy open?�Czy przy read/write?
89
Wracając do 12 bitów...
90
Uprawnienia plików mają 12 bitów
setgid
|
|
v OWNER GROUP OTHER
Bity: 0 0 0 1 1 0 1 1 0 1 0 0
^ ^ r w x r w x r w x
| |
| sticky
|
setuid
Inspired by https://twitter.com/b0rk/status/982641594305273856/photo/1
91
Uprawnienia plików mają 12 bitów
A tak się to wyświetla w ‘ls’ (gdyby bity były zapalone):
setgid
|
|
v OWNER GROUP OTHER
Bity: 0 0 0 1 1 0 1 1 0 1 0 0
^ ^ r w x r w x r w x
| |
| sticky
|
setuid
Inspired by https://twitter.com/b0rk/status/982641594305273856/photo/1 & pic from https://linuxhandbook.com/suid-sgid-sticky-bit/
92
“Suidowe binarki”
93
“Suidowe binarki”
94
“Suidowe binarki”
95
“Suidowe binarki”
96
“Suidowe binarki”
97
“Suidowe binarki”
98
Jak działa su?
99
Inne “cechy” plików...
100
Inne “cechy” plików...
101
Dziwne przykłady
102
Dziwne przykłady
103
That’s all,
Kk thx bye
104