Fireship · @fireship
When bad C code puts you behind bars...
Uploaded June 9, 2025 · Length 4:58
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.
- 0:02
ISO/IEC9899 is arguably the most
- 0:04
important document in the history of
- 0:06
humankind. More significant than the
- 0:07
Magna Carta has built more wealth than
- 0:09
Satoshi's Bitcoin white paper and is
- 0:11
even more sacred than the moon landing
- 0:13
telemetry data which was lost by NASA.
- 0:15
This document is the official spec for
- 0:17
the C programming language. the mother
- 0:19
of almost all modern languages like C++,
- 0:22
Java, JavaScript, Python, and is also
- 0:24
the foundation for operating systems,
- 0:26
compilers, encryption tools, game
- 0:28
engines, and smart toilets. It's a
- 0:30
simple, humble language that built the
- 0:32
world, but sometimes it also likes to
- 0:34
destroy the world. In today's video,
- 0:35
we'll look at five simple code examples
- 0:37
of memory management in C. The five code
- 0:40
examples that go hard, the code that's
- 0:41
cost companies billions of dollars and
- 0:43
put programmers in jail. But you might
- 0:45
be saying to yourself, "Wait a minute,
- 0:46
I'm just a soy dev who relies on garbage
- 0:48
collection, and this is above my pay
- 0:50
grade." Well, actually, memory
- 0:51
management in C is something you can
- 0:53
learn in 30 seconds. The C forces you to
- 0:55
grow up and manage memory by yourself.
- 0:57
That means if you want to put some data
- 0:58
into the computer's RAM, you allocate
- 1:00
some bytes with the Malik function. It
- 1:02
returns a memory address that looks like
- 1:04
this, which you can then access in your
- 1:06
code using a pointer, which itself is
- 1:08
just a variable that points to a memory
- 1:10
address. And now we can start putting
- 1:11
crap in it. But every Malik also needs a
- 1:14
free, which will release the memory when
- 1:16
you're done with it. That's pretty much
- 1:17
all there is to it, and it didn't even
- 1:18
take 30 seconds to master it. But with
- 1:20
great power comes a million different
- 1:22
ways to shoot yourself in the foot, like
- 1:24
don't call free twice, never use freed
- 1:26
memory, don't call free after midnight,
- 1:28
and definitely don't forget to do bounds
- 1:30
checking. One of the first large- scale
- 1:31
attacks on the internet was the Morris
- 1:33
worm. In 1988, it took out about 6,000
- 1:36
machines, which at the time was 10% of
- 1:37
the internet. And it was able to do that
- 1:39
because libraries like Fingered and
- 1:41
Sendmail had code that looks similar to
- 1:42
this. Here we have a buffer with 512
- 1:45
bytes. Then we put some data into it.
- 1:47
But the problem is we're not checking
- 1:49
the size, which could result in a buffer
- 1:51
overflow. This allowed the worm to
- 1:53
overwrite return addresses on the stack,
- 1:55
allowing for arbitrary code execution.
- 1:57
That's because C doesn't automatically
- 1:59
perform bounds checking. And if you try


