go get my/vulnerabilities Green threads are not eco friendly threads

1

Who ●

( Web|Mobile ) penetration tester



Code reviewer



Programmer Roberto Clapis @empijei

2

Go ●

Google’s language



Born in 2007 (quite new)



Widespread

3

Cool, but how do I break it? ●

Memory safety, Garbage Collection



Anti-XSS/SQLi sanitization



Built-in thread-safe constructs

4

Let’s start the digging ●

New features usually lead to new vulnerabilities



Goroutines are one of the main new features introduced by Go

5

Goroutines are concurrent function calls

go

go

go fmt.Println(“Hello goroutines”) 6

Let’s try this for i := 0; i <= 9; i++ { go func() { fmt.Println(i) }() } 7

Expectation

Reality

1

10

3

10

2

10

...

...

8

10

9

10 8

Wait...

9

Special functions #1: goroutines ●

Concurrent



Lightweight

go func(){ //Code here }()



Multiplexed on OS Threads

10

Special functions #2: closures freeVar := “Hello ” f := func(s string){ fmt.Println(freeVar + s) } f(“Closures”) // Hello Closures 11

Special functions all together for i := 0; i <= 9; i++ { go func() { fmt.Println(i) }() } // Here i == 10

12

Performance ●

Writing to file is slow



Aware scheduling



Runtime waits only if necessary

13

The (odd) fix for i := 0; i <= 9; i++ { i := i go func() { fmt.Println(i) }() } 14

Channels ch <- data

ch := make(chan..

go

close(ch)

Buffer

Buffer

data <- ch

data, ok <- ch

for data := range ch { 15

Information Leakage func Serve(queue chan *http.Request) { for req := range queue { go func() { process(req) }() } }

responses to the wrong requests 16

Checkpoint ●

Variable scoping is a nice point to focus on



Aware scheduling can make it easier to abuse races

how aware is the scheduler? 17

MPG model G

P M

M

OS Threads

G

P

GOMAXPROCS Logical processors

G

Goroutines 18

Schedule me please Computation

go

I/O

Write to channel

Read buffer

Scheduler calls are emitted at compile time 19

Consequences are weird go func() { for i := 0; true ; i++ { } }() time.Sleep(2 * time.Second) fmt.Println("Done") 20

Cores amount matter Endless for ch := make(chan..

go

sleep

End program

Endless for

ch := make(chan..

go

sleep

End program 21

Runs the same way everywhere... runtime.GOMAXPROCS(1)

22

Statically Strongly Typed go func() { for i := range lst { for ; i <= 255 ; i++ { // Computation } } }()

23

Hidden problem: Garbage Collector Request memory Garbage Collection go

Lose reference

Stop the world

Start the world

Garbage collector needs to stop goroutines 24

Garbage Collection? Endless for Garbage Collection?? go

Free needed

Stop the world

Start the world

GC politely asks goroutines to stop 25

Consequences are bad go func() { var i byte for i = 0; i <= 255; i++ { } }() runtime.Gosched() //yield execution runtime.GC() fmt.Println("Done") 26

Note to make it worse Golang internal deadlock detector does not detect this deadlocks. Do not expect it to perform magic.

fatal error: all goroutines are asleep - deadlock! goroutine 1 [semacquire]: sync.runtime_SemacquireMutex(0xc42000e114) /usr/lib/go/src/runtime/sema.go:62 +0x34 sync.(*Mutex).Lock(0xc42000e110) /usr/lib/go/src/sync/mutex.go:87 +0x9d main.main() /home/rob/go/gopath/src/github.com/empijei/gotr ials/deadlock/main.go:14 +0x6e exit status 2

27

Here is the solution Weird, less efficient solution: use non-inlinable function calls in loops

The correct one: use channels

28

Checkpoint ●

Scheduling must be taken into account



Goroutines that don’t yield have potential for DoS

how do goroutines die? 29

Goroutines end The only way for a goroutine to terminate is for it to return, or for the program to end.

30

Goroutines are not Garbage Collected

They must be signalled to end or they constitute an insidious opening for DoS

31

select the right solution? ch1 <- data1

select { case d1 <- ch1: case d2, ok <- ch2:

ch2 <- data2

default: go

select

} 32

Max execution time in PHP // Maximum execution time of // 2 seconds exceeded

33

Max execution time in go

So is this magic? 34

This is NOT PHP type simpleHandler struct { } func (t *simpleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { time.Sleep(10 * time.Second) fmt.Println("Got here") } func main() { sh := &simpleHandler{} tsh := http.TimeoutHandler(sh, time.Second*2, "Timeout!") http.ListenAndServe(":8080", tsh) 35

Just a click away

36

Dive into sources // Create timer go func() { h.handler.ServeHTTP(tw, r) // Signal done channel }() select { case <-done: // Handle HTTP stuff case <-timeout: // Write error } 37

Mind the gap The standard library isn’t more powerful than you are, if you can’t kill a goroutine, neither can the standard library.

38

Some more problems with signals // The worker goroutine for { select{ case job <- jobs: process(job) case <-done: return } }

// The main goroutine: go worker() // Work needs to end: done <- true

39

Other (still not) correct fixes go worker() go worker() go worker() done <- true done <- true done <- true

case <-done: done <- true return go worker() done <- true 40

Even worse case <- done

done <- true

? done := make(chan..

go

done <- true

41

Other (still not) correct fixes case <-done: done <- true return go worker() done <- true <- done 42

Just close it go worker() go worker() go worker() close(done)

43

Close channels close(ch)

ch := make(chan..

go

data, ok <- ch

if !ok {

for data := range ch { 44

Conclusions ●

Mind race conditions



Dive into sources



Follow signals



Check for yielding calls 45

Thanks Roberto Clapis @empijei

46

go get my/vulnerabilities - HackInBo

Who. ○ ( Web|Mobile ) penetration tester. ○ Code reviewer. ○ Programmer. Roberto Clapis. @empijei. 2. Page 3. Go. ○ Google's language. ○ Born in 2007 (quite new). ○ Widespread. 3. Page 4. Cool, but how do I break it? 4. ○ Memory safety, Garbage Collection. ○ Anti-XSS/SQLi sanitization. ○ Built-in thread-safe ...

889KB Sizes 1 Downloads 251 Views

Recommend Documents

Security in Agile HiB Final 5 - HackInBo
What is Agile. Agile - Disclaimer. • Agile Manifesto. • Am I believer? • Iterative approach. • Short feedback. • Fail Fast. • Ready to Pivot. • No Dependencies. • Empowerment. Page 9 ... What is Agile. Fail Fast: Not suitable for ever

Catch me if you can! - HackInBo
Oct 29, 2016 - A real system (high-interaction honeyclient) or an emulated ... “The Document Object Model is a platform- and language-neutral interface that will allow .... dynamic analysis.. Symbols identification for later dynamic analysis. Easil

Data Security Model and Data Protection - HackInBo
Oct 29, 2016 - Credit Card Number DE_CCN. Tokenize. (expose first 6, last 4). Payments, CSR. 9 – 5,. M -F. EDW,. Hadoop. Unauthorized. Authorized. E-mail Address. DE_EMAIL. Tokenize All. HR, CSR,. DS_Haddop. EDW,. Hadoop. Unauthorized. Authorized.

How To Bring HID Attacks To The Next Level - HackInBo
Oct 14, 2017 - 9 | WWW.WHID.NINJA | @LucaBongiorni | 2017-10-14. What's Next? Test for Social Engineering weaknesses within your target organization (e.g. DLP policy violations) and to bypass physical access restrictions to a Target's device!

Go santa go
Journey to Destruction in Steinbeck's Flightand London's To Build a Fire. Notmany people haveto face death in thecold ... Biology pdf 2015.Howdo they do its10e17.Go santa go.Usatop 40 single. charts.American black cop.3.3.5 no install.61) Fortunately

My go go
Kikiminajmaid.Theadventure of puss boots.The. legend ofzeldatwilight princess. ... go.One piece 607 [email protected] 2013 pdf.SECRETS OF THEBIBLE ...

Go Quick Reference Go Quick Reference Go Quick Reference - GitHub
Structure - Package package mylib func CallMeFromOutside. Format verbs. Simpler than Cās. MOAR TABLE package anothermain import (. "fmt". ) func main() {.

Get In, Get Connected, Get Hired
Some students effectively leverage their education to transform ... An essential read for those seeking a degree or a career in business, Get In, Get Connected, ...

pdf-1792\go-for-no-yes-is-the-destination-no-is-how-you-get-there ...
DOWNLOAD EBOOK : GO FOR NO! YES IS THE DESTINATION, NO IS HOW. YOU GET THERE BY RICHARD FENTON, ANDREA WALTZ PDF. Page 1 of 9 ...