Fireship · @fireship

When bad C code puts you behind bars...

Uploaded June 9, 2025 · Length 4:58

When bad C code puts you behind bars...

Summary

The video explains common C code mistakes that can lead to security issues, such as memory management errors, buffer overflows, and double frees.

  • c programming language
  • memory management in c
  • buffer overflow
  • c code security
  • c programming mistakes
  • double free error

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:02

    ISO/IEC9899 is arguably the most

  2. 0:04

    important document in the history of

  3. 0:06

    humankind. More significant than the

  4. 0:07

    Magna Carta has built more wealth than

  5. 0:09

    Satoshi's Bitcoin white paper and is

  6. 0:11

    even more sacred than the moon landing

  7. 0:13

    telemetry data which was lost by NASA.

  8. 0:15

    This document is the official spec for

  9. 0:17

    the C programming language. the mother

  10. 0:19

    of almost all modern languages like C++,

  11. 0:22

    Java, JavaScript, Python, and is also

  12. 0:24

    the foundation for operating systems,

  13. 0:26

    compilers, encryption tools, game

  14. 0:28

    engines, and smart toilets. It's a

  15. 0:30

    simple, humble language that built the

  16. 0:32

    world, but sometimes it also likes to

  17. 0:34

    destroy the world. In today's video,

  18. 0:35

    we'll look at five simple code examples

  19. 0:37

    of memory management in C. The five code

  20. 0:40

    examples that go hard, the code that's

  21. 0:41

    cost companies billions of dollars and

  22. 0:43

    put programmers in jail. But you might

  23. 0:45

    be saying to yourself, "Wait a minute,

  24. 0:46

    I'm just a soy dev who relies on garbage

  25. 0:48

    collection, and this is above my pay

  26. 0:50

    grade." Well, actually, memory

  27. 0:51

    management in C is something you can

  28. 0:53

    learn in 30 seconds. The C forces you to

  29. 0:55

    grow up and manage memory by yourself.

  30. 0:57

    That means if you want to put some data

  31. 0:58

    into the computer's RAM, you allocate

  32. 1:00

    some bytes with the Malik function. It

  33. 1:02

    returns a memory address that looks like

  34. 1:04

    this, which you can then access in your

  35. 1:06

    code using a pointer, which itself is

  36. 1:08

    just a variable that points to a memory

  37. 1:10

    address. And now we can start putting

  38. 1:11

    crap in it. But every Malik also needs a

  39. 1:14

    free, which will release the memory when

  40. 1:16

    you're done with it. That's pretty much

  41. 1:17

    all there is to it, and it didn't even

  42. 1:18

    take 30 seconds to master it. But with

  43. 1:20

    great power comes a million different

  44. 1:22

    ways to shoot yourself in the foot, like

  45. 1:24

    don't call free twice, never use freed

  46. 1:26

    memory, don't call free after midnight,

  47. 1:28

    and definitely don't forget to do bounds

  48. 1:30

    checking. One of the first large- scale

  49. 1:31

    attacks on the internet was the Morris

  50. 1:33

    worm. In 1988, it took out about 6,000

  51. 1:36

    machines, which at the time was 10% of

  52. 1:37

    the internet. And it was able to do that

  53. 1:39

    because libraries like Fingered and

  54. 1:41

    Sendmail had code that looks similar to

  55. 1:42

    this. Here we have a buffer with 512

  56. 1:45

    bytes. Then we put some data into it.

  57. 1:47

    But the problem is we're not checking

  58. 1:49

    the size, which could result in a buffer

  59. 1:51

    overflow. This allowed the worm to

  60. 1:53

    overwrite return addresses on the stack,

  61. 1:55

    allowing for arbitrary code execution.

  62. 1:57

    That's because C doesn't automatically

  63. 1:59

    perform bounds checking. And if you try