• Aucun résultat trouvé

Sécurité des logiciels

N/A
N/A
Protected

Academic year: 2022

Partager "Sécurité des logiciels"

Copied!
44
0
0

Texte intégral

(1)

1

Sécurité des logiciels

print? pwnd!

escape? pwnd!

Samuel Thibault <samuel.thibault@u-bordeaux.fr>

CC-BY-NC-SA

(2)

Printf is evil

(3)

3

Printf is evil

int main(void) { char buf[128];

while (1) {

fgets(buf, sizeof(buf), stdin);

printf(buf);

}}

What is the bug?

(4)

Printf is evil

int main(void) { char buf[128];

while (1) {

fgets(buf, sizeof(buf), stdin);

printf("%s", buf);

}}

What is the bug?

(5)

5

Printf is evil

int main(void) { char buf[128];

while (1) {

fgets(buf, sizeof(buf), stdin);

printf(buf);

}}

What is the bug?

(6)

Printf is evil

printf("%p %p %p %p %p\n");

Parameters do “exist”, even if none was passed!

Leaks whatever you want from the stack

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

“6th param”

“5th param”

“7th param”

“8th param”

“2nd param”

“3rd param”

“4th param”

(7)

7

Printf is really evil

printf("%2$p %6$p\n");

You can even choose what to print!

Note: %2$p means:

- the second parameter after the format, - so here it is the third parameter of printf

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

0x0fac ... ...

“6th param”

“5th param”

“7th param”

“8th param”

“%2$p %6$p\n”

main...

“2nd param”

“3rd param”

“4th param”

ESP

(8)

Printf is awfully evil

printf("aa%n\n");

What the hell is that?

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

“6th param”

“5th param”

“7th param”

“8th param”

“2nd param”

“3rd param”

“4th param”

(9)

9

Printf is awfully evil

printf("aa%n\n");

What the hell is that?

Normal use:

int n;

printf("aa%n\n", &n);

Writes in n the number of printed chars so far, i.e. 2 here (aa).

Yes, printf can write to memory

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

0x0fac ... ...

“aa%n\n”

main...

&n

ESP

(10)

Printf is awfully evil

printf("aa%n\n");

Uses 2nd parameter as address where to write 2.

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

“6th param”

“5th param”

“7th param”

“8th param”

“2nd param”

“3rd param”

“4th param”

(11)

11

Printf is awfully evil

printf("aa%4$n\n");

Uses 5th parameter as address where to write 2.

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

0x0fac ... ...

“6th param”

“5th param”

“7th param”

“8th param”

“aa%4$n\n”

main...

“2nd param”

“3rd param”

“4th param”

ESP

(12)

Printf is awfully evil

printf("%12345p%4$n\n");

Uses 5th parameter as address where to write 12345.

Smells really bad...

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

“6th param”

“5th param”

“7th param”

“8th param”

“2nd param”

“3rd param”

“4th param”

(13)

13

Printf is awfully evil

Remember that our victim code was buf[128];

fgets(buf, sizeof(buf), stdin);

printf(buf);

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

0x0fac ... ...

“6th param”

“5th param”

“7th param”

“8th param”

“aa%n\n”

main...

“2nd param”

“3rd param”

“4th param”

ESP

(14)

Printf is awfully evil

Remember that our victim code was buf[128];

fgets(buf, sizeof(buf), stdin);

printf(buf);

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

“6th param”

“5th param”

“7th param”

“8th param”

“2nd param”

“3rd param”

“4th param”

buf

(15)

15

Printf is awfully evil

Remember that our victim code was buf[128];

fgets(buf, sizeof(buf), stdin);

printf(buf);

I.e. we actually control what is on the stack...

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

0x0fac ... ...

“6th param”

“5th param”

“7th param”

“8th param”

“aa%n\n”

main...

“2nd param”

“3rd param”

“4th param”

ESP buf

(16)

Printf is awfully evil

Remember that our victim code was buf[128];

fgets(buf, sizeof(buf), stdin);

printf(buf);

I.e. we actually control what is on the stack...

Let’s pass “AAAA\x01\x02\x03\x04%3$p”

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

“6th param”

“5th param”

“7th param”

“8th param”

“2nd param”

“3rd param”

“4th param”

buf

(17)

17

Printf is awfully evil

Remember that our victim code was buf[128];

fgets(buf, sizeof(buf), stdin);

printf(buf);

I.e. we actually control what is on the stack...

Let’s pass “AAAA\x01\x02\x03\x04%3$p”

Prints 0x04030201

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

0x0fac ... ...

0x00 0x70243325

“7th param”

“8th param”

“AAAA\x01...”

main...

“2nd param”

0x41414141 0x04030201

ESP buf

(18)

Printf is awfully evil

Remember that our victim code was buf[128];

fgets(buf, sizeof(buf), stdin);

printf(buf);

I.e. we actually control what is on the stack...

Let’s pass “AAAA\x01\x02\x03\x04%3$n”

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

0x00 0x6e243325

“7th param”

“8th param”

“2nd param”

0x41414141 0x04030201 buf

(19)

19

Printf is awfully evil

Remember that our victim code was buf[128];

fgets(buf, sizeof(buf), stdin);

printf(buf);

I.e. we actually control what is on the stack...

Let’s pass “%50p\x01\x02\x03\x04%3$n”

Writes 54 in memory at 0x04030201!

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

...

0x0fac ... ...

0x00 0x6e243325

“7th param”

“8th param”

“%50p\x01...”

main...

“2nd param”

0x70303525 0x04030201

ESP buf

(20)

Printf is definitely evil

Remember that our victim code was buf[128];

fgets(buf, sizeof(buf), stdin);

printf(buf);

I.e. we actually control what is on the stack...

Let’s pass “AAAA\x01\x02\x03\x04%3$s”

0x1000 0x0ffc 0x0ff8 0x0ff4 0x0ff0 0x0fec 0x0fe8 0x0fe4

0x00 0x73243325

“7th param”

“8th param”

“2nd param”

0x41414141 0x04030201 buf

(21)

21

Printf is definitely evil

printf(s); is definitely evil, never do that Replace with printf("%s", s);

(22)

Escaping is tricky...

(23)

23

Escaping is tricky

printf("Hello, world!\n");

What about printing the " character?

(24)

Escaping is tricky

printf("Hello, world!\n");

What about printing the " character?

printf("\"Hello, world!\"\n");

\ is called an escape character.

(25)

25

Escaping is tricky

printf("Hello, world!\n");

What are issues with escaping characters?

Escaping itself "\\"

Unknown escaping "\z"

Escaping at end "\"

The January 2021 sudo exploit was an escaping-at-end issue

(26)

Escaping is tricky

SMTP protocol (Simple Mail Transfer Protocol):

354 Enter message, ending with "." on a line by itself

But then how can the mail contain a “.” on a line by itself?

Emit ".." on a line by itself

But then how can the mail contain a “..” on a line by itself?

Emit "..." on a line by itself

(27)

27

Escaping is tricky

SMTP protocol (Simple Mail Transfer Protocol)

Its escaping looks quite lame... But it is systematic.

(28)

Escaping is tricky

Web form to register for something

First name

Family name

etc.

char *query;

asprintf(&query,

"INSERT INTO people VALUES('%s', '%s')", first_name, last_name);

mysql_query(con, query);

(29)

29

Escaping is tricky

From https://xkcd.com/327/

(30)

Escaping is tricky

(31)

31

Escaping is tricky

Web form to register for something

First name

Family name

etc.

printf("\\begin{description}\n");

printf("\\item[First name]: %s\n", first_name);

printf("\\item[Last name]: %s\n", last_name);

printf("\\end{description}\n");

Which characters have some meaning in TeX??

#$%&\^_{}~

Also @ if \makeatletter is used

(32)

Escaping is tricky

Web form to register for something

First name

Family name

etc.

And then used on a webforum

Avoid html tags etc.

(33)

33

Text is tricky

(34)

Text is tricky

(35)

35

Text is tricky

Unicode has

Plain letters: A

Ligatures: f i -> fi

Characters with double-spacing: ideograms

Characters with no space:

Zero-Width Space (U+200B)

Writing direction: Left-to-Right, Right-to-Left

Combining characters: e + U+0301 → é

https://www.youtube.com/watch?v=jC4NNUYIIdM

(36)

Text is tricky

Fortunately Unicode does not have

An embedded interpreter

Yes, it got proposed...

(37)

37

Implementation-defined / Undefined /

Unspecified

behaviors

(38)

Odd behaviors

From C99:

Implementation-defined behavior

unspecified behavior where each implementation documents how the choice is made

Undefined behavior

behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

Unspecified behavior

(39)

39

Odd behaviors

Put another way:

Implementation-defined behavior

can be anything, but at least sane, and stable.

Undefined behavior

can be anything. Including insane such as eating your disk.

Unspecified behavior

can be anything among a few specified sane things, and stable.

(40)

Odd behaviors

int x = -1;

int y = x >> 2;

int x = 32;

int y = 1 << x;

int x = 0x7fffffff;

x++;

f(printf("a"),

implementation-defined

undefined

undefined

unspecified

(41)

41

“On x86 the add instruction will be used for signed add. This has two’s complement behavior on overflow. I’ll thus get two’s

complement on int”

“Somebody told me that in basketball you can’t hold the ball and run. But I tried it and it worked. He doesn’t actually know

basketball...”

In 2010, more than half of the SPECINT2006 benchmarks had some integer undefined behavior.

(42)

Why such odd behaviors?

Performance

Gives freedom to compiler to optimize code int i;

for (i = 1; i <= n; i++) { ... } will iterate exactly n times.

And n equal to INT_MAX is *not* supposed to happen

(43)

43

Why such odd behaviors?

Compilers tend to use this more and more

Beware of that!!

e.g. memcpy vs memmove

Optimization of memcpy in glibc broke bogus usage

glibc had to keep a compatibility symbol...

-fsanitize=undefined

We’ll discuss more of this in the coming weeks

(44)

Do other languages do better?

They try to, e.g. Rust

But still bugs: “I-unsound” bugs

Poses optimization concerns

Or runtime-checks that add overhead

Références

Documents relatifs

Figure 2.9: A model for the torsional vibration of the drillstring 38 Figure 2.10: Drillstring in the transverse vibration state 40 Figure 2.11: Schematic of the

Extraction of wrong collocations: we used the implemented module to extract candidate collocations (named as the NUCLE List) from the National University of Singapore Corpus

The C++ language files contain the definition of the stochastic types on which the control of accuracy can be performed, CADNA C specific functions (for instance to enable or

times put by saying that the terms or concepts entering into the questions, statements and arguments of, say, the High Court Judge are of different &lt; categories' from

In this chapter we address OS robustness as regards possible erroneous inputs provided by the application software to the OS via the Application Programming Interface (API). More

Information on potential human exposure to a pesticide as a result of its proposed use in occupational and nonoccupational settings, data from poison control centres,

Formation of multistage network as- sumes the process of serial transformation of correlated space areas and creation of decor related in time element of physical environment while

The account we would like to build up here focuses on the issue of whether culture- specific factors (esp. language, narrative practices or social scripts) function