everyone should test their code this way

  Рет қаралды 76,959

Low Level Learning

Low Level Learning

7 ай бұрын

Did you know you can just SCREAM at your code to find bugs? Yeah seriously it's that easy. In this video we'll talk about libfuzzer, which is a simple to use tool to write code that finds bugs in your code. In this video we write some code, find a bug, and patch it.
🏫 COURSES 🏫 Learn to code in C at lowlevel.academy
📰 NEWSLETTER 📰 Sign up for our newsletter at mailchi.mp/lowlevel/the-low-down
🙌 SUPPORT THE CHANNEL 🙌 Become a Low Level Associate and support the channel at / lowlevellearning
Why Are Switch Statements so FAST? • why are switch stateme...
Why Do Header Files Exist? • why do header files ev...
How Does Return Work? • do you know how "retur...
🔥🔥🔥 SOCIALS 🔥🔥🔥
Low Level Merch!: lowlevel.store/
Follow me on Twitter: / lowleveltweets
Follow me on Twitch: / lowlevellearning
Join me on Discord!: / discord

Пікірлер: 138
@Dev-Siri
@Dev-Siri 7 ай бұрын
I already yell around 5-10 times a day at my computer
@mu11668B
@mu11668B 7 ай бұрын
Just for fun though, there's a footgun hidden in the example code, too. As the recv buffer has a hardcoded length limit of 1024 bytes, directly casting the input buffer into a struct that contains a user-controlled length field is not really a good idea. If somehow the codebase got updated in a certain way and the memcpy destination was a heap allocation, it may lead to information leak. E.g. ask the server to echo a 65535-byte data chunk from a 1024-byte input.
@anon_y_mousse
@anon_y_mousse 7 ай бұрын
Seems like nearly every video I'm warning about magic numbers. He really needs to tighten up his examples.
@01001000010101000100
@01001000010101000100 7 ай бұрын
Well, the quicker way to ensure no crash here is just sanitize the input data. Then probably add a unit test for some edge cases. However - with much more complex example maybe using a fuzzer would be simpler, IDK. But THIS is probably the simplest explanation on how to use a fuzzer to begin with.
@macchiato_1881
@macchiato_1881 7 ай бұрын
Its always these hardcoded buffers that blow up in your face.
@MrAsddasdasda
@MrAsddasdasda 7 ай бұрын
@@anon_y_mousse It's on purpose to get engagement from these comments.
@anon_y_mousse
@anon_y_mousse 7 ай бұрын
@@MrAsddasdasda You may be right because I leave a comment every time just to say something about it.
@matthias916
@matthias916 7 ай бұрын
"like literally yelling at the code" proceeds not to yell at the code
@millax-ev6yz
@millax-ev6yz 7 ай бұрын
Why is fuzzing better than boundary tests?...after watching I withdraw my question.
@adissentingopinion848
@adissentingopinion848 7 ай бұрын
I'm on the HDL/Hardware side where something like this is called Constrained Random Verification. Of course we do checks on boundary conditions in directed specific tests, but these devices have 30+ interfaces, so complex interactions can occur. Boundaries cover the 3 cases of too low, too high, or just right data inputs. But what if, say, if condition A AND Condition B And Condition C occur within x milliseconds to error out? A, B, and C are all within bounds, but this specific combination is deadly. For example, if on a server client 1 is somehow allowed to delete files in use by client 2 via an unsafe delete(file f) function, unless you know exactly how this exploit works you won't make a test for it. Two fake clients banging on a virtual keyboard, however, might find the right inputs over time to crash.
@millax-ev6yz
@millax-ev6yz 7 ай бұрын
@@adissentingopinion848 I'm sold. I commented too early and using the Int vs unit is what did it, something that could be missed with the range. I have not used a tool like this so is it full path coverage or random? I wonder if the expense for full path doesn't become too high in terms of time....
@adissentingopinion848
@adissentingopinion848 7 ай бұрын
​@@millax-ev6yzIt's probably not going to get full code coverage UNLESS you explicitly get into a specific state for operation first. That "harness" mentioned for interfacing with the code can be very large and very customized. Simulations for hardware are terribly slow, but purely software testing ought to be rather fast up to a point. In hardware at least, you can set assertions that cover functional requirements such as message format. That way you don't have to error out, just capture the incorrect functionality from the harness itself.
@trexake
@trexake 7 ай бұрын
Use -fsanitize=fuzzer,address and you should be able to find another bug in the parse code. If the input is less than the size of the struct you would read outside the memory. Does not always cause crash without address sanitizer. However not a bug in the program due to the receiving buffer size.
@piotrkaminski3443
@piotrkaminski3443 7 ай бұрын
I love this type of videos where you show a useful tool and an example using this tool, and what's even cooler is the fact that using it you were able to detect a bug that wasn't intentional
@mk72v2oq
@mk72v2oq 7 ай бұрын
That's why I used to use unsigned everywhere by default, until negative values are explicitly required by design. And yes, using e.g. -1 magic value to represent things like a non-existent index is a bad design. Don't do it.
@joaquinnapan3237
@joaquinnapan3237 7 ай бұрын
what could I do instead for non-existent index??
@gigachad8810
@gigachad8810 7 ай бұрын
@@joaquinnapan3237 In rust you would do Option don't know about other languages.
@jacquesfaba55
@jacquesfaba55 7 ай бұрын
Error-as-types. Like Rust
@mk72v2oq
@mk72v2oq 7 ай бұрын
@@joaquinnapan3237 Rust → Option C++ → std::optional C# → Nullable ... For languages with no option-like concept out of the box, you certainly can come up with something. E.g. in C you can utilize out parameter for the actual value and return the error code, or vice versa. Or return something like struct optional_uint32 { bool has_value; uint32_t value; }
@yeet1337
@yeet1337 7 ай бұрын
​@@gigachad8810in C?
@dsdy1205
@dsdy1205 7 ай бұрын
I already do this every day
@holmybeer
@holmybeer 7 ай бұрын
It would be really funny if he said "there's no more bugs in this code" and libfuzzer just crashed.
@mc4ndr3
@mc4ndr3 7 ай бұрын
I publish fuzzers. Applied to tech roles for nineteen months without success. Hiring teams are ass.
@LowLevelLearning
@LowLevelLearning 7 ай бұрын
Shoot me an email
@maxterrain
@maxterrain 7 ай бұрын
And I wrote a cryptographic library that was "fuzzed" with Python's Hypothesis library. Do hiring teams understand it? Of course not.
@uuu12343
@uuu12343 7 ай бұрын
Satisfied customer here, been doing this for the last 10 years 10/10 - my code has feared me ever since
@LunarSoul255
@LunarSoul255 7 ай бұрын
Ah, there's a name for it. I do this regularly the manual way in my own projects, though granted those are all smaller projects where my scope of potential issues is "is there some way a user can force invalid data down this thing's throat". Useful to know if I ever manage to get a real job, lol(being a dev without a college degree is the dark souls of job hunting, I swear)
@Rose-ec6he
@Rose-ec6he 7 ай бұрын
Segmentation fault (Core dumped)
@wecann.clinic
@wecann.clinic 3 ай бұрын
Amazing brother, you have the gift of communicate complex concepts into simple terms. Thanks! Glad to find your channel! ;)
@markojojic6223
@markojojic6223 7 ай бұрын
Well, because I am so good at messing up function calls by using function pointers and structs/unions, I need no help. The code would yell either way nevertheless.
@markojojic6223
@markojojic6223 7 ай бұрын
Also, I am obsessed with keeping the memory usage low, so it's likely that I am gonna use a goofy assembly or stuff for my personal performance-intensive stuff. Especially on microcontrollers, but those don't count.
@romanferguson4032
@romanferguson4032 6 ай бұрын
id love a video of you describing your linux setup. i use wsl and customize very few things but would love more insight into your setup for vim and tmux/whatever multi shell youre using
@darkrasen
@darkrasen 7 ай бұрын
I didn't quite catch why 7:45 is an issue. Would anyone mind please clarifying?
@Ferrolune
@Ferrolune 7 ай бұрын
overflow probably, would be my first guess.
@turun_ambartanen
@turun_ambartanen 7 ай бұрын
It checks if len>64, to prevent writing more than the allocated buffer. But negative numbers are also smaller than 64, so they also pass the check. The program then crashes in the memcp again, because it tries to copy a negative number of bytes.
@darkrasen
@darkrasen 7 ай бұрын
@@turun_ambartanen thanks so much!
@louispetrick
@louispetrick 7 ай бұрын
For an signed number we're using the two's complement to represent negative and positive numbers. Here the MSB decides whether the number is interpreted as an positive or negative number, where 0 = positive and 1 = negative. Looking at 7:45 for example, a hex value of 0xFF is represented in binary with 0b1111_1111. When assigned to a signed variable, this is actually a -1 in decimal. Since we use this variable "len" to access entries in an array, this will result in an error as it doesn't have negative entries to point at.
@darkrasen
@darkrasen 7 ай бұрын
@@louispetrick thanks so much for the thorough explanation 😁
@mp_rho
@mp_rho 7 ай бұрын
why did i think we might actually be yelling at code?
@sunofabeach9424
@sunofabeach9424 7 ай бұрын
the most reasonable action in the world of C programming
@coolbrotherf127
@coolbrotherf127 7 ай бұрын
I yell at my code, but it doesn't usually fix any bugs lol
@Kim_YoJong
@Kim_YoJong 6 ай бұрын
Because you're a fan of slamming desks.
@abdulfatahmohammed6800
@abdulfatahmohammed6800 7 ай бұрын
Interesting, I have no idea this type of testing exists. Thanks man
@GeoffryGifari
@GeoffryGifari 7 ай бұрын
at first we code safely by yelling in time elaborate rituals involving chanting, holy oils and incense is necessary to please the machine spirit and banish demonic bugs
@PeepoStrong
@PeepoStrong 7 ай бұрын
It reminds me some OOM error bug that got in project that was caused by using msgpack library (Java). The msgpack library deserializes byte stream into some objects - it was deserializing a base64 string to object. Apparently the library supports read a big array of bytes. Msgpack reads the message in sequence - does not know what data comes next - when the byte with flag for huge byte arrays comes in it pre-allocates array of 2^32-1 byte-elements. Found it because we had a malformed string that was not object we wanted to deserialize but rather random string. Later to confirm to architects that any idiot with msgpack documentation, paper and pencil can do it - prepared a base64 string on paper that mimic the good object to deserialize and then put the bytes of memory doom. They wanted to do some happy checking of first few bytes - after short demonstration - they changed their minds. With some java like fuzzer I would do that automatically (and probably the error could be found earlier), but fun of playing with bytes was awesome.
@bowiemtl
@bowiemtl 7 ай бұрын
While this tool is awesome as is, is there any way to get it into an IDE? I think productivity would go up a lot of you can just select a function and some extension can do all the work for you returning only the result. Maybe I'm overlooking something that'd make you not want to use an extension like that but I think it'd be cool
@TechnopolisDotTV
@TechnopolisDotTV 7 ай бұрын
As always chef's kiss!
@1oglop1
@1oglop1 7 ай бұрын
Hi, sorry of the OT but I have a Rust/C question nobody was able to point me in the right direction. With redhook (unmaintained lib) I used LD_PRELOAD to override getenv which worked fine in NodeJs but Rust did not care about it at all. Do you know what is different or what should I read to understand how this all work? Thank you so much
@sudo-gera
@sudo-gera 7 ай бұрын
Flag '-g' makes stack traces of gdb or any sanitizer look pretty. Use it.
@romsthe
@romsthe 7 ай бұрын
2:38 in, I expect your issue is that you didn't check the length argument in your payload. This should pop up with many static analyzers. But I get it, it's just an example. Fuzzing is more for discovering weird edge cases and undefined behaviors as I understand it. Or I'm totally wrong and length was not the issue :D
@pierreabbat6157
@pierreabbat6157 7 ай бұрын
I'm partial to American Fuzzy Lop, which compiles C++ code so that it knows which branches were taken. Can Rust code be fuzzed the same way, and is there a way to fuzz Haskell code that does something similar?
@owenheckmann6962
@owenheckmann6962 7 ай бұрын
“Port 1337” that took me a second. Very funny
@TRex-fu7bt
@TRex-fu7bt 7 ай бұрын
Does it statically analyze the wrapped function to deduce how to do the fuzzing? I’m struck by how it got the magic word immediately.
@StuartLoria
@StuartLoria 7 ай бұрын
Those if statements are not very readable, but that is the prefered way, implementations details rather than intensions or requirements, if that is what people do then there is no alternative. Para pensar, señores.
@defnlife1683
@defnlife1683 7 ай бұрын
This was awesome. Fuzz all the things.
@fulconandroadcone9488
@fulconandroadcone9488 7 ай бұрын
I was hoping for Torvalds kind of screaming at someone else code, but I guess this is fine.
@maxmyzer9172
@maxmyzer9172 7 ай бұрын
0:05 should have been the end lmao
@torarinvik4920
@torarinvik4920 7 ай бұрын
Also related but not the same: Property-based testing, those who haven't tried it will be amazed at it's usefulness.
@bryan0x05
@bryan0x05 7 ай бұрын
I really like the terminal environment you're using, how can I get my setup to look like that?
@funkdefied1
@funkdefied1 7 ай бұрын
Vim, prolly
@funkdefied1
@funkdefied1 7 ай бұрын
Neovim *
@gustavoaguilar7999
@gustavoaguilar7999 7 ай бұрын
i3wm
@avishjha4030
@avishjha4030 7 ай бұрын
This is so cool, does something like this also exist in the Java world?
@31redorange08
@31redorange08 7 ай бұрын
To test for what?
@trexake
@trexake 7 ай бұрын
Jazzer does exactly that and is based on this.
@mytechnotalent
@mytechnotalent 7 ай бұрын
I yell at code all day.
@theblankuser
@theblankuser 5 ай бұрын
This is cool af
@drdca8263
@drdca8263 7 ай бұрын
How does this compare to concolic testing?
@davidpatry4195
@davidpatry4195 7 ай бұрын
pretty cool.
@backupmemories897
@backupmemories897 7 ай бұрын
how do i remove the path stuff inside my exe.. i see it exposes my directory in the exe.
@abraarsameer9521
@abraarsameer9521 7 ай бұрын
Instructions unclear I’ve been yelling at code this whole time
@uis246
@uis246 7 ай бұрын
1:09 I already can guess r will be less than REQ_SIZE because recv doesn't have WAIT_ALL flag.
@FadkinsDiet
@FadkinsDiet 7 ай бұрын
Even with WAIT_ALL maliciously crafted input could cause errors or DoS
@CjqNslXUcM
@CjqNslXUcM 7 ай бұрын
simple good video
@lefteriseleftheriades7381
@lefteriseleftheriades7381 5 ай бұрын
At 2:33 i see the bug. He copies data based on the user inputed length on a buffer that ia limited to 64 bytes. I will watch more to see if this is what the fuzzer finds
@elzabethtatcher9570
@elzabethtatcher9570 7 ай бұрын
I presume this fuzzer actually looks at the soruce code of the program, to predict how to best gain different outputs? It is not just random text generator?
@user-qm4ev6jb7d
@user-qm4ev6jb7d 7 ай бұрын
It doesn't exactly look at the source code. Instead, it memorizes which random inputs caused which if-branches to be taken, and randomly mutates those inputs to "cover" as many routes through the program as possible. They call it "coverage-guided fuzzing".
@woosix7735
@woosix7735 7 ай бұрын
@@user-qm4ev6jb7d thanks for the explanation, it's pretty cool
@21centuryschizoid
@21centuryschizoid 5 ай бұрын
can you share the code with the bug ? thanks
@PieroUlloa
@PieroUlloa 7 ай бұрын
This seems great. I expect those eagle eyed developers saw the h- >len value, and thought to themselves about how user input is always evil :p but hey, the unsigned one did surprise me too! Luckily i like writing u32 u64 et al.
@lollertoaster
@lollertoaster 7 ай бұрын
My favorite part of testing is "cat /dev/urandom | ./a.out" But that's specifically for testing proper error handling.
@FadkinsDiet
@FadkinsDiet 7 ай бұрын
6:41 shell users everywhere are screaming at you there's no need to use cat, just use the
@jefersonlemos4135
@jefersonlemos4135 7 ай бұрын
I thought you were doing like me and really cursing while programming, well that will prevent me from cursing
@ramsey2155
@ramsey2155 7 ай бұрын
This is how that belt makes your child stronger
@miniflint2423
@miniflint2423 7 ай бұрын
Hi ! I don’t understand the unsigned problem. Could someone explain?
@coolbrotherf127
@coolbrotherf127 7 ай бұрын
Signed numbers include negative numbers which the program had no way to handle so they caused a crash. By making it unsigned, it forces all values to be positive integer values 0-255 which the program could easily check.
@versacebroccoli7238
@versacebroccoli7238 7 ай бұрын
Fuzzing is how Zenbleed was found!
@itsjustrobby
@itsjustrobby 5 ай бұрын
What’s that you say? I’m not retarded I’m just left handed. This video just made me literally cry 😭
@AvalancheGameArt
@AvalancheGameArt 7 ай бұрын
I could see the bug even before the first test iteration...
@abanoubha
@abanoubha 7 ай бұрын
go fuzz 🎉
@uis246
@uis246 7 ай бұрын
2:30 not validated user input
@maxrepin6491
@maxrepin6491 7 ай бұрын
Although slightly off-topic, I was wondering if you could make a video explaining how cheat codes function in games like GTA San Andreas or Vice City. How they interact with the memory and what processes occur behind the scenes. I'd really appreciate a deep dive into this. Thank you!
@Hellscaped
@Hellscaped 7 ай бұрын
They're just series of inputs that the game checks for and does something in response. Its not really complicated.
@EdKolis
@EdKolis 7 ай бұрын
Yeah, you're probably thinking of Game Genie. Which I would like to see a video about how it works!
@sritharan20
@sritharan20 7 ай бұрын
goat
@TheInspctrcat
@TheInspctrcat 7 ай бұрын
Wow, sharp transitions should be smoothed out, otherwise this is an ultra-useful video
@AndreDeLimburger
@AndreDeLimburger 7 ай бұрын
Even faster with a switch statement? You are already using a switch statement!
@maxmuster7003
@maxmuster7003 6 ай бұрын
Limit the stack size to zero.😂
@tomtravis858
@tomtravis858 7 ай бұрын
I love Rust
@amankashyap7842
@amankashyap7842 7 ай бұрын
i = 4; cout
@sudo-gera
@sudo-gera 7 ай бұрын
C++ and C languages have very interesting thing: "Undefined behavior". This doesn't mean that behavior would be randomly chosen from "a set of possible behaviors". This means that behavior would be completely undefined. It can run into segfault or start erasing data on your PC. Anything is possible. Nothing is guaranteed. And for this case: In "a+b" expression, computation of a and b is not sequenced. They can happen in any order. Side effects of unsequenced operations cause Undefined behavior. Once it happened - nothing is guaranteed.
@amankashyap7842
@amankashyap7842 6 ай бұрын
@@sudo-gera thanks
Ай бұрын
I'll use Zig
@labkome
@labkome 7 ай бұрын
Rust is good, but confusing for me
@granitium
@granitium 6 ай бұрын
You weren't yelling at the code wth
@TatharNuar
@TatharNuar 7 ай бұрын
a
@EmilMacko
@EmilMacko 7 ай бұрын
This guy: "Make your code safer by yelling at it. That's right, LITERALLY yelling at your code, in a very literal sense, can make your code, literally, safer. Legit stretch those vocal chords, open your mouth all the way, and just let out the biggest scream at the very top of your lungs, at your code, to make it safer!" This guy 20 seconds later: "So this process involves feeding random data into your program and..."
@sunofabeach9424
@sunofabeach9424 7 ай бұрын
clickbate my beloved
@raulr994
@raulr994 7 ай бұрын
*Pauses video 29 seconds in* You can't say LITERALLY yelling at your code if you don't mean to actually YELL at it vocally. That's the opposite of what LITERALLY means. :/
@maxmuster7003
@maxmuster7003 6 ай бұрын
You need a pump gun to fix your code.😂
@questionmarc8
@questionmarc8 3 ай бұрын
8:27 Wrong. switch statements are not faster than switch statements.
@kayakMike1000
@kayakMike1000 7 ай бұрын
Rust is silly.
@dhaneshabhipraya
@dhaneshabhipraya 7 ай бұрын
23h ago
@deusvult4214
@deusvult4214 7 ай бұрын
at 0:24 you said tha it's about "literally yelling at your code", but i didnt hear any yelling, though. Literally yelling means moving your face muscles to produce loud noise, yet during all your vide you were very calm. Why did you lie about this technique?
@CoolProgramer123
@CoolProgramer123 7 ай бұрын
third
@FranLegon
@FranLegon 7 ай бұрын
0:24 two incorrect uses of "literally" in less than half a minute. Congrats
@tanishkmahakalkar761
@tanishkmahakalkar761 7 ай бұрын
First..!!!!😁🤩😍💯💥✨💫🔥👍🏻👏🏻✊🏻🤜🏻🤛🏻🙌🏻🫶🏻🙏🏻👌🏻
@RussoIncendiario
@RussoIncendiario 7 ай бұрын
first
@xntumrfo9ivrnwf
@xntumrfo9ivrnwf 7 ай бұрын
This is too powerful... people should just stick to Python
everything is open source if you can reverse engineer (try it RIGHT NOW!)
13:56
Low Level Learning
Рет қаралды 1,2 МЛН
Using Numbers in Your Code is BAD?!? (low level code review)
14:33
Low Level Learning
Рет қаралды 117 М.
小路飞姐姐居然让路飞小路飞都消失了#海贼王  #路飞
00:47
路飞与唐舞桐
Рет қаралды 93 МЛН
Why? 😭 #shorts by Leisi Crazy
00:16
Leisi Crazy
Рет қаралды 45 МЛН
1❤️
00:20
すしらーめん《りく》
Рет қаралды 31 МЛН
КАКОЙ ВАШ ЛЮБИМЫЙ ЦВЕТ?😍 #game #shorts
00:17
how a simple programming mistake ended 6 lives
9:14
Low Level Learning
Рет қаралды 856 М.
The Unreasonable Effectiveness Of Plain Text
14:37
No Boilerplate
Рет қаралды 570 М.
arrays in C are friggin weird
6:57
Low Level Learning
Рет қаралды 100 М.
how does source become code?
8:47
Low Level Learning
Рет қаралды 92 М.
Writing Code That Runs FAST on a GPU
15:32
Low Level Learning
Рет қаралды 533 М.
A Vulnerability to Hack The World - CVE-2023-4863
18:00
LiveOverflow
Рет қаралды 104 М.
why do hackers love strings?
5:42
Low Level Learning
Рет қаралды 389 М.
nation state hackers caught exploiting cisco firewalls
8:15
Low Level Learning
Рет қаралды 200 М.
why does inheritance suck?
8:05
Low Level Learning
Рет қаралды 201 М.
Apple watch hidden camera
0:34
_vector_
Рет қаралды 48 МЛН
Как я сделал домашний кинотеатр
0:41
RICARDO
Рет қаралды 1,5 МЛН
Apple iPhone 15 Pro Max With Smallrig Professional Photography kit #shorts
0:14
How charged your battery?
0:14
V.A. show / Магика
Рет қаралды 2 МЛН