Accelerated Linux API Training Course Slides 1686684670
Accelerated Linux API Training Course Slides 1686684670
● ●
● ●
Dmitry Vostokov
Software Diagnostics Services
Prerequisites
Development experience
and (optional)
Lots of pictures
Lots of examples
Cybersecurity
C#
Scala Native
Golang
Rust
Python
Core Dumps
Included in Exercise L0
Exercise Transcripts
Included in this book
\LAPI-Dumps\Exercise-L0-GDB.pdf
\LAPI-Dumps\Exercise-L0-WinDbg.pdf
Category theory
Libraries
Syscalls
Machine language
Counter: RCX
Examples:
RSP-0x20 → ← RBP-0x20
RSP-0x18 → ← RBP-0x18
Stack grows
RSP-0x10 → ← RBP-0x10
RSP-0x8 → ← RBP-0x8
RSP → ← RBP
RSP+0x8 → ← RBP+0x8
RSP+0x10 → ← RBP+0x10
RSP+0x18 → ← RBP+0x18
RSP+0x20 → ← RBP+0x20
Higher addresses
© 2023 Software Diagnostics Services
Instructions: memory load (x64)
Opcode Offset(SRC), DST
Opcode DST
Examples:
Opcode SRC|DST
Examples:
Examples:
Stack grows
addr: RSP-0x10 → 0 ← RBP-0x8
# void proc2(); RSP-0x8 → RBP ← RBP
# void proc(int p1, long p2) {
# long local = 0; RSP → addr ← RBP-0x8
# proc2();
# } RSP+0x8 → ← RBP
proc: RSP+0x10 → ← RBP+0x8
push %rbp
mov %rsp, %rbp RSP+0x18 → ← RBP+0x10
sub $0x8, %rsp
mov $0, -0x8(%rbp) RSP+0x20 → ← RBP+0x18
call proc2
adr2:
...
Higher addresses
© 2023 Software Diagnostics Services
CPU Registers (A64)
X0 – X28, W0 – W28 X 64-bit W 32-bit
Next instruction: PC
128-bit Q0 – Q31
© 2023 Software Diagnostics Services
Instructions: registers (A64)
Opcode DST, SRC, SRC2
Examples:
SP-0x20 → ← X29-0x20
Stack grows SP-0x18 → ← X29-0x18
SP-0x10 → ← X29-0x10
SP-0x8 → ← X29-0x8
SP → ← X29
SP+0x8 → ← X29+0x8
SP+0x10 → ← X29+0x10
SP+0x18 → ← X29+0x18
SP+0x20 → ← X29+0x20
Higher addresses
© 2023 Software Diagnostics Services
Instructions: memory load (A64)
Opcode DST, DST2, [SRC, Offset]
Examples:
Examples:
Examples:
b 0x10493fc1c // PC ← 0x10493fc1c
// (goto 0x10493fc1c)
br x17 // PC ← the value of X17
0x10493fc14: // PC == 0x10493fc14
bl 0x10493ff74 // LR ← PC+4 (0x10493fc18)
// PC ← 0x10493ff74
// (goto 0x10493ff74)
© 2023 Software Diagnostics Services
Function Call and Prolog (A64)
Lower addresses
X30 adr2
// void proc(int p1, long p2);
mov w0, #0x1 SP → X29 ← X29
mov x1, #0x2
bl proc SP-0x18 → X30 ← X29-0x18
Stack grows
addr: SP+0x10 → 0 ← X29+16
// void proc2(); SP-0x8 → ← X29-0x8
// void proc(int p1, long p2) {
// long local = 0; SP → ← X29
// proc2();
// } SP+0x8 → ← X29+0x8
proc: SP+0x10 → ← X29+0x10
stp x29, x30, [sp, #-32]!
mov x29, sp SP+0x18 → ← X29+0x18
str zxr, [x29, #16]
bl proc2 SP+0x20 → ← X29+0x20
adr2:
...
Higher addresses
© 2023 Software Diagnostics Services
General Linux API Aspects
IAT ↔ PLT
DLL ↔ SO
Module ↔ Shared Library
Headers
Kernel source code cross-reference
Manual pages 0
inttypes.h
uint64_t
sys/types.h
size_t, pid_t
Indirect
Pointer (.got.plt)
shell_execve:
...
0x000055e16508f89b <+251>: callq 0x55e165078aa0 <open@plt>
open@plt:
...
0x000055e165078aa0 <+0>: jmpq *0xe7aaa(%rip) # 0x55e165160550 <[email protected]>
Indirect
Pointer (.got)
open@plt:
...
0x0000aaaabb6d1ec0 <+0>: adrp x16, 0xaaaabb7ff000
0x0000aaaabb6d1ec4 <+4>: ldr x17, [x16, #1464]
...
0x0000aaaabb6d1ecc <+12>: br x17
© 2023 Software Diagnostics Services
API as Interface
Provided by (exported from) some .so library
executable/.so .so
ldd
readelf
objdump
$ LD_DEBUG=libs bash
.dynsym
...
“open@GLIBC_2.2.5”: open
.dynsym
...
...
“open@GLIBC_2.2.5”
...
open:
...
.dynsym
2
1 open:
...
...
callq open@plt
...
.dynsym
2
1 open:
...
...
callq open@plt
...
ELF (libc.so.6)
open:
...
ELF (libc.so.6)
open:
...
\LAPI-Dumps\Exercise-L1-GDB.pdf
\LAPI-Dumps\Exercise-L1-WinDbg.pdf
Get started
Troubleshooting
Add to .bashrc
alias code="'/mnt/c/Users/[USER]/AppData/Local/Programs/Microsoft VS Code/Code.exe'"
extern int ioctl (int __fd, unsigned long int __request, ...) __THROW;
x64 left-to-right via RDI, RSI, RDX, RCX, R8, R9, right-to-left PUSH ...
Caller Callee
EDI (p1) EDI (p1)
ESI (p2) ESI (p2)
EDX (p3) EDX (p3)
ESX (p4) ESX (p4)
R8D (p5) R8D (p5)
R9D (p6) R9D (p6)
call
Caller Callee
W0 (p1) W0 (p1)
W1 (p2) W1 (p2)
W2 (p3) W2 (p3)
W3 (p4) W3 (p4)
W4 (p5) W4 (p5)
W5 (p6) W5 (p6)
W6 (p7) call W6 (p7)
W7 (p8) W7 (p8)
\LAPI-Dumps\Exercise-L2-GDB.pdf
\LAPI-Dumps\Exercise-L2-WinDbg.pdf
Example
main:
...
callq 0x43c1f0 <sleep>
...
callq 0x43d6e0 <ioctl>
...
executable libc.so
b.so
Module Collection
Coupled Modules
Duplicated Module
\LAPI-Dumps\Exercise-L3-GDB.pdf
\LAPI-Dumps\Exercise-L3-WinDbg.pdf
Hidden Module
GDB Commands
\LAPI-Dumps\Exercise-L4-GDB.pdf
First call:
open@plt:
0x0000555555581aa0 <+0>: jmpq *0xe7aaa(%rip) # 0x555555669550 <[email protected]>
0x0000555555581aa6 <+6>: pushq $0xa7
0x0000555555581aab <+11>: jmpq 0x555555581020 DL
Subsequent calls:
LIBC
open@plt:
0x0000555555581aa0 <+0>: jmpq *0xe7aaa(%rip) # 0x555555669550 <[email protected]>
0x0000555555581aa6 <+6>: pushq $0xa7
0x0000555555581aab <+11>: jmpq 0x555555581020
\LAPI-Dumps\Exercise-L5-GDB.pdf
open, …, close
malloc, …, free
pthread_create, …, pthread_join
wl_surface_damage, …, wl_surface_attach, …,
wl_surface_commit
Horizontal
Code disassembly
Traces and logs (Thread of Activity analysis pattern)
Vertical
Stack trace
Traces and logs (Fiber Bundle analysis pattern)
liba_b libpthread
liba_c libc
syscalls
0:000> u fname
0:000> uf /c fname
\LAPI-Dumps\Exercise-L6-GDB.pdf
\LAPI-Dumps\Exercise-L6-WinDbg.pdf
create/open/delete/close
pthread
display/surface/window
create_a
Paradigmatic axis
b_create
Syntagmatic axis
Example:
execve [-]
socket [+]
connect [+]
GDB Commands
create [-] (gdb) info functions @plt
Pattern descriptions
API Query
User space
Process memory dumps
Function calls:
• Thread of Activity
• Fiber of Activity
• Adjoint Thread of Activity
• Strand of Activity
• Discontinuity
• Fiber Bundle
• Weave of Activity
errno.h (values)
errno
rtld_errno
\LAPI-Dumps\Exercise-L7-GDB.pdf
\LAPI-Dumps\Exercise-L7-WinDbg.pdf
strlen
strlen(s), strlen(s) → n, n
read
Side effects
Vulnerability: How
SAST
Static code analysis tools
CopyFile/CopyFileEx
CreateThread/CreateRemoteThread
openat/openat2
accept/accept4
read/pread/readv/preadv/preadv2
Principle of compositionality
Code glue
API call
API call
Objects
Arrows between objects (must be transitive, if A → B and B → C then A → C)
Functor
Natural Transformation
Adjunction
Functor Adjoint
Functors
Category
Category Functor
Natural
Adjunction
Transformation
open
read
write
close
open
D
read
close
Working code
Non-working
Working code
code
Similar diagrams
API1
1-category API (semigroup)
C
API2
API3
... API2
int socketpair(
int domain,
int type,
socketpair
int protocol,
int sv[2]
);
Installation
Platform Invoke
\LAPI-Dumps\Exercise-L8.pdf
Documentation
Scala Native
\LAPI-Dumps\Exercise-L9.pdf
unix
Example:
package main
import (
"golang.org/x/sys/unix"
)
func main() {
unix.Exec("/bin/ps", nil, nil)
}
Unsafe: libc
Example:
use libc::{c_char, execve};
fn main() {
unsafe {
execve("/bin/ps".as_ptr() as *const c_char, std::ptr::null(), std::ptr::null());
}
}
os
ctypes
Example:
import ctypes
ctypes.CDLL("libc.so.6").execve("/bin/ps".encode("utf-8"), 0, 0)
Examples
strace bash
ltrace bash
ltrace -S bash
Syscalls
Identification uname
Layers
Stream-based f(open|seek|read|write|close) FILE*
Syscall-based creat|open|lseek|read|write|ioctl|close fd
Buffering
Block-based f(read|write)
Line-based (f)puts, (f)printf
Unbuffered (p)read(v), (p)write(v)
Syscalls
Buffering f(data)sync
Access posix_fadvise
Control fcntl
Locking flock
Truncation (f)truncate
Syscalls
Mounting (u)mount
Metadata (f|l)stat(at|vfs)
Attributes (list|get|set)(f|l)xattr
Permissions (l|f)ch(own|mod), umask
Time utime(s)
Links (un)link, rename
Symlinks (sym|read)link
Directories (mk|rm|ch)dir, chroot
Monitoring inotify_(init|(add|rm)_watch)
© 2023 Software Diagnostics Services
Dynamic Memory API
Library
Control mall(opt|info), memalign
Allocation (m|c|re)alloc
Deallocation free
Debugging m(un)trace, m(check|probe)
Stack alloca
Syscalls
Adjustment (s)brk
Syscalls
Mapping m((un|re)map|sync), remap_file_pages
Protection mprotect
Locking m(un)lock(all)
Residence mincore
Usage madvise
Syscalls
Creation fork, clone
Execution execve
Termination _exit
Waiting wait(pid|id|3|4)
Resources acct, getrusage, (get|set)rlimit
Priority (get|set)priority, sched_get_priority_(min|max)
Scheduling sched_((set|get)(scheduler|param)|yield|rr_get_interval)
Affinity sched_(set|get)affinity
Capabilities prctl
© 2023 Software Diagnostics Services
IPC API
Library
Pipes p(open|close) FILE*
FIFO mkfifo
Keys ftok
POSIX (mq|sem|shm)_(open|unlink|*)
Syscalls
Pipes pipe
Handles dup(2), close
Message queues msg(get|snd|rcv|ctl)
Semaphores sem(get|ctl|op)
Shared memory shm(get|at|ctl|dt)
Syscalls
Groups (get|set)pgid
Sessions (get|set)sid
Syscalls
Disposition signal, sigaction
Mask sig(procmask|pending)
Sending kill, sigqueue
Waiting sig(suspend|waitinfo), signalfd
Stack sigaltstack
Syscalls
Sockets socket, bind, listen, accept, connect, close, shutdown
Streaming write, send, read, recv
Datagrams recvfrom, sendto
Messages (send|recv)msg
Files sendfile
Domain socketpair
Addresses get(sock|peer)name
Options (get|set)sockopt
Syscalls
Calendar (get|set)timeofday, (s)time
Process times
Syscalls
Intervals (get|set)itimer, timer_(create|settime|delete)
Repeating timer_getoverrun
Once alarm
File timerfd_(create|(get|set)time)
Clock clock_(get|set)(time|res)
Hi-res (clock_)nanosleep
Syscalls
Process ptrace
Performance and monitoring perf_event_open
eBPF bpf
Syscalls
ID (get|set)((r)e)(s)(u|g)id
Filesystem setfs(u|g)id