Escape Nils Asmussen MKC, 07/12/2018 1 / 43 Introduction Tasks - - PowerPoint PPT Presentation

escape
SMART_READER_LITE
LIVE PREVIEW

Escape Nils Asmussen MKC, 07/12/2018 1 / 43 Introduction Tasks - - PowerPoint PPT Presentation

Introduction Tasks Memory VFS IPC Security UI Escape Nils Asmussen MKC, 07/12/2018 1 / 43 Introduction Tasks Memory VFS IPC Security UI Outline Introduction 1 Tasks 2 Memory 3 VFS 4 IPC 5 Security 6 UI 7 2 / 43


slide-1
SLIDE 1

Introduction Tasks Memory VFS IPC Security UI

Escape

Nils Asmussen MKC, 07/12/2018

1 / 43

slide-2
SLIDE 2

Introduction Tasks Memory VFS IPC Security UI

Outline

1

Introduction

2

Tasks

3

Memory

4

VFS

5

IPC

6

Security

7

UI

2 / 43

slide-3
SLIDE 3

Introduction Tasks Memory VFS IPC Security UI

Outline

1

Introduction

2

Tasks

3

Memory

4

VFS

5

IPC

6

Security

7

UI

3 / 43

slide-4
SLIDE 4

Introduction Tasks Memory VFS IPC Security UI

Motivation

Beginning Writing an OS alone? That’s way too much work! Port of UNIX32V to ECO32 during my studies Started with Escape in October 2008 Goals Learn about operating systems and related topics Experiment: What works well and what doesn’t? What problems occur and how can they be solved?

4 / 43

slide-5
SLIDE 5

Introduction Tasks Memory VFS IPC Security UI

Overview

Basic Properties UNIX-like microkernel-based OS Open source, available on github.com/Nils-TUD/Escape Mostly written in C++, some parts in C Runs on x86, x86 64, ECO32 and MMIX Only third-party code: libgcc, libsupc++, x86emu, inflate ECO32 MIPS-like, 32-bit big-endian RISC architecture, developed by Prof. Geisse for lectures and research MMIX 64-bit big-endian RISC architecture of Donald Knuth as a successor for MIX (the abstract machine from TAOCP)

5 / 43

slide-6
SLIDE 6

Introduction Tasks Memory VFS IPC Security UI

Overview

Hardware µ-kernel

Tasks Memory VFS libc libcpp libgui

privileged mode user mode

vesa tcpip ata ext2

Drivers

vterm fileman ping cat ls ps

Applications

winmng uimng ps2 guishell head less ... ... ... libfs libesc

6 / 43

slide-7
SLIDE 7

Introduction Tasks Memory VFS IPC Security UI

Outline

1

Introduction

2

Tasks

3

Memory

4

VFS

5

IPC

6

Security

7

UI

7 / 43

slide-8
SLIDE 8

Introduction Tasks Memory VFS IPC Security UI

Processes and Threads

Process Virtual address space File descriptors Mountspace Threads (at least one) . . . Thread User and kernel stack State (running, ready, blocked, . . . ) Scheduled by a round-robin scheduler with priorities Signals . . .

8 / 43

slide-9
SLIDE 9

Introduction Tasks Memory VFS IPC Security UI

Processes and Threads

Synchronization Process-local semaphores (can also be created for interrupts) Global semaphores, named by a path to a file Userspace builds other synchronization primitives on top

Combination of atomic ops and process-local semaphores Readers writer lock . . .

Priority Management Priorities are dynamically adjusted based on compute intensity High CPU usage → downgrade, low CPU usage → upgrade

9 / 43

slide-10
SLIDE 10

Introduction Tasks Memory VFS IPC Security UI

Outline

1

Introduction

2

Tasks

3

Memory

4

VFS

5

IPC

6

Security

7

UI

10 / 43

slide-11
SLIDE 11

Introduction Tasks Memory VFS IPC Security UI

Memory Management

Physical Memory Mostly, memory is managed by a stack (fast for single frames) A small part handled by a bitmap for contiguous phys. memory Virtual Memory Kernel part is shared among all processes User part is managed by a region-based concept mmap-like interface for the userspace

11 / 43

slide-12
SLIDE 12

Introduction Tasks Memory VFS IPC Security UI

Virtual Memory Management

dynlink (text) VM (proc 1) VM (proc 2) flags=shared,exec size=16K, procs=1,2 flags=write,grow,stack size=12K, procs=2 flags=write,grow size=16K, procs=1 flags=shared,exec size=20K, procs=1,2 0x00000000 text 0xBFFFFFFF 0xA0000000 data stack1 stack2 libc.so (text) text data stack1 libc.so (text) MMIO free area layouted area free area layouted area /bin/hello /lib/libc.so dynlink (text)

12 / 43

slide-13
SLIDE 13

Introduction Tasks Memory VFS IPC Security UI

Outline

1

Introduction

2

Tasks

3

Memory

4

VFS

5

IPC

6

Security

7

UI

13 / 43

slide-14
SLIDE 14

Introduction Tasks Memory VFS IPC Security UI

Basics

The kernel provides the virtual file system System-calls: open, read, mkdir, mount, . . . It’s used for:

1

Provide information about the state of the system

2

Access userspace filesystems

3

Access devices

4

Access interrupts 14 / 43

slide-15
SLIDE 15

Introduction Tasks Memory VFS IPC Security UI

Drivers and Devices

Drivers are ordinary user programs They create devices via the system call createdev These are usually put into /dev Devices can also be used to implement on-demand-generated files (such as /sys/net/sockets) Communication is based on asynchronous message passing

15 / 43

slide-16
SLIDE 16

Introduction Tasks Memory VFS IPC Security UI

Message Passing

inbox

  • utbox

send(id,msg) recv(id | 42,msg) recv(id | 42,msg) send(id | 42,msg) Channel

Client Driver

id | 42 id | 43 id | 42 id | 43

16 / 43

slide-17
SLIDE 17

Introduction Tasks Memory VFS IPC Security UI

Devices Can Behave Like Files

As in UNIX: Devices should be accessable like files Messages: FILE OPEN, FILE READ, FILE WRITE, FILE CLOSE Devices may support a subset of these message Kernel handles communication for open/read/write/close Type of file transparent for applications

17 / 43

slide-18
SLIDE 18

Introduction Tasks Memory VFS IPC Security UI

Devices Can Behave Like Filesystems

Messages: FS OPEN, FS READ, FS WRITE, FS CLOSE, FS STAT, FS SYNC, FS LINK, FS UNLINK, FS RENAME, FS MKDIR, FS RMDIR, FS CHMOD, FS CHOWN Kernel handles communication, if syscall refers to userspace fs Filesystems are mounted using the mount system call

18 / 43

slide-19
SLIDE 19

Introduction Tasks Memory VFS IPC Security UI

Achieving Higher Throughput

Copying everything twice hurts for large amounts of data sharebuf establishes shmem between client and driver Easy to use: just call sharebuf once and use this as the buffer Clients don’t need to care whether a driver supports it or not Drivers need to handle DEV SHFILE to support it In read/write, they check if SHM should be used

19 / 43

slide-20
SLIDE 20

Introduction Tasks Memory VFS IPC Security UI

Achieving Higher Throughput – Code Example

i n t fd = open ( ”/ dev / zero ” ,IO READ) ; s t a t i c char buf [ SIZE ] ; while ( read ( fd , buf , SIZE ) ) > 0) { // . . . } c l o s e ( fd ) ;

20 / 43

slide-21
SLIDE 21

Introduction Tasks Memory VFS IPC Security UI

Achieving Higher Throughput – Code Example

i n t fd = open ( ”/ dev / zero ” ,IO READ) ; s t a t i c char buf [ SIZE ] ; while ( read ( fd , buf , SIZE ) ) > 0) { // . . . } c l o s e ( fd ) ; i n t fd = open ( ”/ dev / zero ” ,IO READ) ; void ∗buf ; i f ( s h a r e b u f ( fd , SIZE ,& buf , 0 ) < 0) { i f ( buf == NULL) e r r o r ( ” Unable to mmap buf ” ) ; } while ( read ( fd , buf , SIZE ) > 0) { // . . . } d e s t r o y b u f ( buf ) ; c l o s e ( fd ) ;

20 / 43

slide-22
SLIDE 22

Introduction Tasks Memory VFS IPC Security UI

Achieving Higher Throughput – Usage Example

cp ext2 ata

21 / 43

slide-23
SLIDE 23

Introduction Tasks Memory VFS IPC Security UI

Achieving Higher Throughput – Usage Example

cp ext2 ata ftpfs tcpip e1000 cp

22 / 43

slide-24
SLIDE 24

Introduction Tasks Memory VFS IPC Security UI

File Exchange

Files (=capabilities) can be exchanged via channel Client can delegate/obtain files from driver:

int delegate(int chan,int fd,uint perm,int arg) int obtain(int chan,int arg)

Used for:

Establishing shared memory Connecting control and event channel of uimng Accepting incoming network connections (accept) . . . 23 / 43

slide-25
SLIDE 25

Introduction Tasks Memory VFS IPC Security UI

File Descriptors For Everything

Interrupts Escape uses semaphores for interrupts For each interrupt, Escape creates a file /sys/irq/$irq Syscall semirqcrt expects fd for IRQ file On an IRQ, all semaphores in the list are up’ed Signals The kill syscall expects fd for process directory Only if it has write permission, the signal can be sent

24 / 43

slide-26
SLIDE 26

Introduction Tasks Memory VFS IPC Security UI

Outline

1

Introduction

2

Tasks

3

Memory

4

VFS

5

IPC

6

Security

7

UI

25 / 43

slide-27
SLIDE 27

Introduction Tasks Memory VFS IPC Security UI

IPC between Client and Driver (Low Level)

dev foo int id = createdev("/dev/foo",...); driver

creates

26 / 43

slide-28
SLIDE 28

Introduction Tasks Memory VFS IPC Security UI

IPC between Client and Driver (Low Level)

dev foo int id = createdev("/dev/foo",...); driver

creates

int fd = open("/dev/foo",IO_MSGS);

points to creates inbox

client channel

  • utbox

27 / 43

slide-29
SLIDE 29

Introduction Tasks Memory VFS IPC Security UI

IPC between Client and Driver (Low Level)

dev foo int id = createdev("/dev/foo",...); driver

creates

int fd = open("/dev/foo",IO_MSGS);

points to creates inbox

client channel

  • utbox

msg.arg1 = 10; mid = send(fd,42,&msg,sizeof(msg)); receive(fd,&mid,&msg,sizeof(msg));

28 / 43

slide-30
SLIDE 30

Introduction Tasks Memory VFS IPC Security UI

IPC between Client and Driver (Low Level)

dev foo int id = createdev("/dev/foo",...); driver

creates

int fd = open("/dev/foo",IO_MSGS);

points to creates inbox

client channel

  • utbox

msg.arg1 = 10; mid = send(fd,42,&msg,sizeof(msg)); receive(fd,&mid,&msg,sizeof(msg));

points to

driver int fd = getwork(id,&mid,&msg,sizeof(msg),0);

29 / 43

slide-31
SLIDE 31

Introduction Tasks Memory VFS IPC Security UI

IPC between Client and Driver (Low Level)

dev foo int id = createdev("/dev/foo",...); driver

creates

int fd = open("/dev/foo",IO_MSGS);

points to creates inbox

client channel

  • utbox

msg.arg1 = 10; mid = send(fd,42,&msg,sizeof(msg)); receive(fd,&mid,&msg,sizeof(msg));

points to

driver int fd = getwork(id,&mid,&msg,sizeof(msg),0); msg.arg1 = 1; send(fd,mid,&msg,sizeof(msg));

30 / 43

slide-32
SLIDE 32

Introduction Tasks Memory VFS IPC Security UI

Driver Example: /dev/zero

s t r u c t ZeroDevice : p u b l i c C l i e n t D e v i c e< > { e x p l i c i t ZeroDevice ( const char ∗name , mode t mode) : C l i e n t D e v i c e (name , mode , DEV TYPE BLOCK,DEV OPEN | DEV DELEGATE | DEV READ | DEV CLOSE) { s e t (MSG FILE READ , std : : make memfun ( t h i s ,& ZeroDevice : : read ) ) ; } void read ( IPCStream &i s ) { s t a t i c char z e r o s [ BUF SIZE ] ; C l i e n t ∗c = get ( i s . fd ( ) ) ; FileRead : : Request r ; i s > > r ; i f ( r . shmemoff != −1) memset ( c− >shm ( ) + r . shmemoff , 0 , r . count ) ; i s < < FileRead : : Response ( r . count ) < < Reply ( ) ; i f ( r . shmemoff == −1 && r . count ) i s < < ReplyData ( zeros , r . count ) ; } }; i n t main ( ) { ZeroDevice dev ( ”/ dev / zero ” ,0400) ; dev . loop () ; return EXIT SUCCESS ; }

31 / 43

slide-33
SLIDE 33

Introduction Tasks Memory VFS IPC Security UI

Client Example: vterm

// get console−s i z e i p c : : VTerm vterm ( std : : env : : get ( ”TERM” ) . c s t r () ) ; i p c : : Screen : : Mode mode = vterm . getMode ( ) ; // implementation

  • f

vterm . getMode ( ) : Mode getMode ( ) { Mode mode ; i n t r e s ; i s < < SendReceive (MSG SCR GETMODE) > > r e s > > mode ; i f ( r e s < 0) VTHROWE( ”getMode ( ) ” , r e s ) ; return mode ; }

32 / 43

slide-34
SLIDE 34

Introduction Tasks Memory VFS IPC Security UI

Outline

1

Introduction

2

Tasks

3

Memory

4

VFS

5

IPC

6

Security

7

UI

33 / 43

slide-35
SLIDE 35

Introduction Tasks Memory VFS IPC Security UI

General Idea

Goals Keep the powerful and convenient UNIX concepts Improve the security, reliability and maintainability Approach Structure it as a microkernel-based system Permissions can only be downgraded (e.g., no setuid) Mountspace as a first layer: control entire subtrees ACL as a second layer: control at file-level

34 / 43

slide-36
SLIDE 36

Introduction Tasks Memory VFS IPC Security UI

Mountspaces

Every process has a mountspace, inherited to childs Mountspace is represented as a directory Child mountspaces become child directories Changing a mountspace requires write permission Mountspace translates: path → (FS, perm, subpath) perm defines upperbound for files in subpath Can be done by unprivileged users

Filesystems and drivers run in userspace . . . with the user+group of the mounter Overmounting system directories is no security issue 35 / 43

slide-37
SLIDE 37

Introduction Tasks Memory VFS IPC Security UI

Mounting for the User

Tools mount creates a new FS for a device and makes it visible

$ mount /dev/hda1 /mnt /sbin/ext2

bind makes an existing FS visible at a different place

$ bind /dev/ext2-hda1 /home/me/mnt

What does bind do?

int fs = open("/dev/ext2 -hda1", ...); int ms = open("/sys/pid/self/ms", O_WRITE); mount(ms , fs , "/home/me/mnt"); // open ("/ home/me/mnt/a/b", ...) -> FS_OPEN ("/a/b")

36 / 43

slide-38
SLIDE 38

Introduction Tasks Memory VFS IPC Security UI

Sandbox

Reasoning Some applications are not trusted Running them as a different user is inconvenient Instead: run with same user, but less permissions The sandbox tool Allows to leave groups Allows to reduce permissions to entire subtrees Example: sandbox -g netuser -m /home:r app Sandboxes can be nested and used by unprivileged users

37 / 43

slide-39
SLIDE 39

Introduction Tasks Memory VFS IPC Security UI

Outline

1

Introduction

2

Tasks

3

Memory

4

VFS

5

IPC

6

Security

7

UI

38 / 43

slide-40
SLIDE 40

Introduction Tasks Memory VFS IPC Security UI

UI Concept

uimng keyb mouse vga vesa

39 / 43

slide-41
SLIDE 41

Introduction Tasks Memory VFS IPC Security UI

UI Concept

uimng keyb mouse vga vesa shell ls vterm

...

40 / 43

slide-42
SLIDE 42

Introduction Tasks Memory VFS IPC Security UI

UI Concept

uimng keyb mouse vga vesa shell ls vterm

...

desktop fileman winmng

41 / 43

slide-43
SLIDE 43

Introduction Tasks Memory VFS IPC Security UI

UI Concept

uimng keyb mouse vga vesa shell ls vterm

...

desktop fileman winmng

42 / 43

slide-44
SLIDE 44

Introduction Tasks Memory VFS IPC Security UI

Questions Get the code, ISO images, etc. on: https://github.com/Nils-TUD/Escape Questions?

43 / 43