What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or:...

33
What is wrong? How can we fix it? 3i Why does the fix suck? 3i + 1 3i + 2

Transcript of What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or:...

Page 1: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

What is wrong?

How can we fix it?

3i

Why does the fix suck?

3i + 1

3i + 2

Page 2: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

kcgi : sandboxed CGI frameworkOr: quantifying the price of web application security

AsiaBSDCon 2015, Tokyo

Kristaps [email protected], [email protected]

BSD.lv Project, k-Consulting

May 28, 2015

Page 3: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

Part I: i0: web application security

Page 4: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

web server

CGI server

HTTP

CGI client

web application

CGIHTTP

Page 5: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

1. Client passes HTTP request to web server.

2. Web server receives HTTP request from client.

3. . . . maps request to CGI.

4. . . . spawns CGI script.

5. . . . passes HTTP request to CGI script.

6. CGI script processes.

7. . . . passes HTTP response to server.

8. Web server passes HTTP response to client.

Page 6: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

6. CGI script processes.

. . . is really. . .

6. 6.1 CGI script parses header request information.6.2 . . . reads request into memory/file.6.3 . . . parses key-value pairs from request stream.6.4 . . . processes key-value pairs.

Page 7: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

1. Client passes HTTP request to web server.

2. Web server receives HTTP request from client.

3. . . . maps request to CGI.

4. . . . spawns CGI script.

5. . . . drop privileges and chroot(2) child.

6. . . . re-write components of HTTP request into CGI.

7. . . . passes HTTP request to CGI script.

8. CGI script processes.

9. . . . passes HTTP response to server.

10. Web server post-processes HTTP response.

11. Web server passes HTTP response to client.

Page 8: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

chroot(2)

CGI client

CGI server

network data

databasefilesystemsocketsprocesses

Page 9: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

Problem i0: adversarial network data directly in contact withsystem resources.

I Database and application-specific resources.

I System resources (sockets, processes, . . . )

I File-system within chroot(2).

I Memory of application (!).

I . . .

I All your base.

Page 10: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons
Page 11: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

chroot(2)

CGI client

CGI server

network data

database filesystem sockets processes

Page 12: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

Specifically, problem i0 is connection of application logic with thecode that parses HTTP form data (and HTTP environment) fromthe CGI request.

Solution i0 + 1?

Page 13: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

CGI client

application logic

CGI server

HTTP

HTTP parser

key-value pairs

CGI

Page 14: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

By splitting apart the parser, we can protect web application logic(the process) from requests exploiting the parser.

We can do better. . .

Page 15: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

CGI client

application logic

CGI server

HTTP

HTTP parser

input validator

validated pairs

CGI

Page 16: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

1. CGI script spawns child processes before anything else.

2. . . . hands off standard input to child.

3. . . . sets up socket pair with child.

4. Untrusted child parses header request information.

5. . . . reads request into memory/file.

6. . . . parses key-value pairs from request stream.

7. . . . passes key-value pairs back to CGI script.

8. CGI script processes key-value pairs.

Page 17: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

The untrusted child can still access system resources.

We can do better. . .

Page 18: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons
Page 19: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

CGI client

sandboxed child

application logic

CGI server

HTTP

HTTP parser

input validator

validated pairs

CGI

Page 20: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

Sandboxing is a way of constraining the environment available to aprocess.

Most well-known sandbox? chroot(2).

More thorough sandboxes: systrace(4) (OpenBSD), Capsicum(FreeBSD), “sandbox” (Darwin), ed(1), . . .

Page 21: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

By sandboxing the parse sequence, we limit the damage caused byuntrusted network data. As for what the application logic doeswith that data. . . You’re on your own.

Now on to i0 + 2 and i1. . .

Page 22: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

Part II: i0 + 2: kcgi

Page 23: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

CGI client

CGI server

web application

1. fork(2)

kcgi child

4. CGI

7. HTTP

kcgi

2. khttp_parse(2) 6. pairs

3. fork(2) 5. pairs

Page 24: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

kcgi, kristaps.bsd.lv/kcgi, is a C library that is1. . .

1. designed to fail

2. slow

3. resource-intensive

1It also has manpage documentation, a regression framework, AFL testingframework, automatic HTTP compression, and considerable MIME parsing.

Page 25: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

kcgi, kristaps.bsd.lv/kcgi, is a C library that is. . .

1. designed to fail

1.1 parse and validate everything in child process1.2 sandbox following OpenSSH’s example

2. slow

2.1 at least twice as slow as raw parsing

3. resource-intensive

3.1 one extra process per CGI client3.2 two extra file descriptors3.3 reads full request into memory twice

Page 26: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

0

0.25

0.5

0.75

1

0 0.5 1 1.5 2 2.5 3 3.5

milliseconds

Figure : OpenBSD 5.5, nginx, slowcgi(8)

Page 27: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

0

0.5

1

1.5

2

2.5

3

˜compress

˜sandbox

kcgislowcgi(8)

static

ms

Figure : OpenBSD 5.5, nginx, slowcgi(8)

Page 28: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

0

0.25

0.5

0.75

1

0 10 20 30 40 50 60

milliseconds

Figure : Mac OS X “Lion”, Apache

Page 29: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

0

5

10

15

20

25

˜compress

˜sandbox

kcgislowcgi(8)

static

ms

Figure : Mac OS X “Lion”, Apache

Page 30: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

kcgi suffers greatly from the penalty of fork(2), systrace(4),and socketpair(2) I/O. It also suffers from double-allocation ofdata (original for parse, parsed pairs in parent).

Problem i1? Performance and resource usage.

The allocation problem can be improved by smart programming.But the former?

Page 31: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

Solution i1 + 1: FastCGI. This fixes the amount of forking.

This is a work in progressˆwˆwˆwˆwfuture work.

Page 32: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

web server

CGI server

control

FastCGI

CGI client

web application

HTTP parser

HTTP

parser

HTTP

pairs pairs

Page 33: What is wrong? 3i 3i + 2 3i + 1 Why does the fix suck? · kcgi: sandboxed CGI framework Or: quantifying the price of web application security AsiaBSDCon 2015, Tokyo Kristaps Dzonsons

While this is being finished. . . kcgi is available atkristaps.bsd.lv/kcgi.

Questions?

Thank you!