File Path Race Condition & How To Prevent It - bin 0x31

Поделиться
HTML-код
  • Опубликовано: 12 июл 2024
  • In this video we look at a typical race condition involving file paths. We learn about the renameat syscall to exploit it and also how to fix it.
    Source code: gist.github.com/LiveOverflow/...
    =[ 🔴 Stuff I use ]=
    → Microphone:* amzn.to/2LW6ldx
    → Graphics tablet:* amzn.to/2C8djYj
    → Camera#1 for streaming:* amzn.to/2SJ66VM
    → Lens for streaming:* amzn.to/2CdG31I
    → Connect Camera#1 to PC:* amzn.to/2VDRhWj
    → Camera#2 for electronics:* amzn.to/2LWxehv
    → Lens for macro shots:* amzn.to/2C5tXrw
    → Keyboard:* amzn.to/2LZgCFD
    → Headphones:* amzn.to/2M2KhxW
    =[ ❤️ Support ]=
    → per Video: / liveoverflow
    → per Month: / @liveoverflow
    =[ 🐕 Social ]=
    → Twitter: / liveoverflow
    → Website: liveoverflow.com/
    → Subreddit: / liveoverflow
    → Facebook: / liveoverflow
    =[ 📄 P.S. ]=
    All links with "*" are affiliate links.
    LiveOverflow / Security Flag GmbH is part of the Amazon Affiliate Partner Programm.

Комментарии • 125

  • @PwnFunction
    @PwnFunction 4 года назад +64

    Wow, didn't know about the swapping syscall !

  • @joonasfi
    @joonasfi 4 года назад +147

    I really appreciate that you showed the way to fix this vulnerability also!! Very nice.

  • @vladde
    @vladde 4 года назад +64

    haha, you upload informational content on par with the best tutorials online, and your worried you don't upload enough. get some well needed rest buddy!

  • @dukap8669
    @dukap8669 4 года назад +39

    HOW IS HE UPLOADING THIS MUCH YOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

    • @JoakimBB
      @JoakimBB 4 года назад +1

      He's not wasting time:)

  • @mina86
    @mina86 4 года назад +20

    Interestingly, file contents may also suffer from race condition. I believe it was Samsung TVs which would read in firmware upgrade files from USB stick to verify their signature and if that check passed then read the file again to apply the upgrade. Issue is that one can easily create a USB device which returns some contents on first read but something completely different on another.
    By the way, this file path race is essentially the reason why one cannot setuid shell scripts.

    • @SeanCMonahan
      @SeanCMonahan 4 года назад +1

      Can you expand on how this ties in with setuid on shell scripts?

    • @mina86
      @mina86 4 года назад +2

      @@SeanCMonahan, the way scripts work is that the kernel checks whether the first line of a file is a shebang line and then executes interpreter specified in that line with path to the file as an argument. Once the interpreter starts, it then reads contents of the file and executes it. This is the exact same scenario described in the video. If the executed script was a symbolic link, the kernel would resolve it once when checking ownership and permissions and then the interpreter would resolve the symbolic link again when it actually started executing the script.
      And there isn’t really a way to solve this since the kernel cannot assume ‘/proc’ exists and besides passing a different path than the one pointing at the script may change how it is executed by the interpreter.

  • @AshnSilvercorp
    @AshnSilvercorp 4 года назад +1

    I'm extremely new to programming and still don't understand all of it. But this person is actively explaining code very well.

  • @simonzelenski2458
    @simonzelenski2458 4 года назад +5

    Awesome video. Never thought that race conditions on file paths would be a thing..
    Hope you get well soon enough to enjoy Christmas!

  • @schrodyn
    @schrodyn 4 года назад +1

    Feel better man. Thanks for the video. Learned something new, as always from your channel.

  • @Ookami8raven
    @Ookami8raven 4 года назад

    It feels like Christmas, because so many new content! LOL. I appreciate your hard work LiveOverflow!

  • @bap9394
    @bap9394 4 года назад +3

    Out of all your videos, that's the first thing I geniunely didn't know about 🤩

  • @cybermagician4215
    @cybermagician4215 4 года назад +60

    I am surprised to see you uploading so many videos haha I hope you keep this up! love your content!

    • @0tiii
      @0tiii 4 года назад +6

      pretty sure this is because it's "hexember"

  • @theotherjim123
    @theotherjim123 4 года назад

    That was really interesting, even better that you showed how to prevent the race condition. Thank you!

  • @elikelik3574
    @elikelik3574 4 года назад

    Each video shows me, that I must no no have to learn more and more =D Thanks a lot for the explanation, and also for fixing part. It is really helpful.A few weeks ago I started to watch your binary series and now comes new episodes, they somehow filling each other. Really great recourse. Thanks again. By the way "Gute Beserrung" =) PS:I'm not a german,so hope I wrote it write =D

  • @sreyanchakravarty7694
    @sreyanchakravarty7694 4 года назад

    I have been using file paths without doing anything. Now I know. Please keep making more videos like this.

  • @joecarter4512
    @joecarter4512 4 года назад

    This is really cool, I've written similar racey file code before but never bothered to look into how you avoid it.

  • @MrLeeFergusson
    @MrLeeFergusson 4 года назад

    A great explanation of a common and easy to overlook bug.

  • @tesilia2394
    @tesilia2394 4 года назад

    Deine Videos retten gerade meine Klausur!

  • @0okaze
    @0okaze 4 года назад

    A classic well explained. It can help newcomers understand the newly introduced (thanks to systemd) pidfd in the Linux kernel.

  • @badrelmazaz
    @badrelmazaz 10 месяцев назад

    Thanks, it really helped me to perform a priv esc

  • @greob
    @greob 4 года назад +1

    Thanks for the video, and thanks for the solution to the race condition. :)

  • @matiasm.3124
    @matiasm.3124 4 года назад

    This is a really useful video ..!! Keep this great work..

  • @TimschneiderSchneider
    @TimschneiderSchneider 4 года назад

    Great Video relly enjoyed it keep up the great work

  • @amannegi288
    @amannegi288 4 года назад

    thanks for such awesome tutorial. Keep it up

  • @cjhackerz
    @cjhackerz 4 года назад

    Reminds me of good old suid based dirtycow exploits, awesome video :D

  • @Philbertsroom
    @Philbertsroom 4 года назад

    Cool fix. Thanks!

  • @bartholomewgander540
    @bartholomewgander540 4 года назад

    Take your time and get better!

  • @Vagelis_Prokopiou
    @Vagelis_Prokopiou 4 года назад

    Exquisite! Thanks man 👍

  • @Marenthyu
    @Marenthyu 4 года назад +5

    If you need a break, take it! Have a nice christmas and get better ASAP, we can wait ^_^

  • @applehelpproductions
    @applehelpproductions 4 года назад

    Super interesting video, enjoyed it very much. Please take some rest if you are sick ;)

  • @rootabeta9015
    @rootabeta9015 4 года назад +1

    Get well soon!

  • @tequila2717
    @tequila2717 4 года назад +2

    Very cool I now know how to race a file path!!

  • @perli216
    @perli216 4 года назад

    I tried another approach, making a symlink to a empty file I have permissions to, breaking the readflag on an open() syscall and then pointing the symlink to the actual flag. This didnt work unfortunately, but I still learned stuff and you got me to do something, which is very lovely. Thank you.

    • @happygimp0
      @happygimp0 4 года назад

      " breaking the readflag on an open() syscall"
      How? Did you attach a debugger? In case yes, then the program runs as a normal user, not as root, and it does not work.

    • @perli216
      @perli216 4 года назад

      @@happygimp0 yes yes, that makes sense

    • @happygimp0
      @happygimp0 4 года назад

      @MERL
      You can still run it as root but then you have to start the debugger as root and that defeats the purpose of an exploit.

  • @mal-nr3ym
    @mal-nr3ym 4 года назад

    Really cool trick. Somewhat the same concept is using mkfifo to 'pause' the race - if a program writes to a pipe it'll then wait till something reads from it, and if it tries to read it'll wait till something writes.

  • @ClassicGameHacking
    @ClassicGameHacking 4 года назад

    I hope you get better master!

  • @j3r3miasmg
    @j3r3miasmg 4 года назад +1

    A simple way to show modifications through time, you can use the command watch -n 1 to execute a command each second to get the differences. Something like: ```watch -n 1 ls -lha```

  • @noobhunter2986
    @noobhunter2986 4 года назад

    Thanks for this boss

  • @realkorgo
    @realkorgo 4 года назад +2

    Nice video!
    Don't worry about 24 videos! Get better first! :)

  • @marcoantonio7648
    @marcoantonio7648 4 года назад

    Amazing!

  • @npip99
    @npip99 3 года назад

    If you want to use only C without manual lesser-known syscalls, you can just make two files: One file is a blank local file owned by you, the other file is a symlink to the root-owned flag. Then in your while loop, you cp one of the two files into a third path, swapping back and forth between them. You would then call ./readflag on the third path.

  • @matevarga3040
    @matevarga3040 4 года назад

    I just realized it's an X-mas calendar. LOL
    Get better man. Here comes the holidays abd family. :)

  • @NeoCortex3
    @NeoCortex3 4 года назад

    Gute Besserung

  • @thejswaroop5230
    @thejswaroop5230 3 года назад +1

    Cool

  • @PinkDraconian
    @PinkDraconian 4 года назад

    Would you be able to manually change the symlink if you set a breakpoint in the binary?

    • @user-qm4ev6jb7d
      @user-qm4ev6jb7d 4 года назад

      In a CTF context, the binary itself is owned by root, and executed as root (by setuid), so you can't debug it as regular user.

    • @paulstelian97
      @paulstelian97 4 года назад +1

      In the fixed variant it doesn't matter anyway. Open would dereference the symlink and get to the file, and then paths will have zero relevance.
      Also don't try running gdb on setuid executables. Ain't working.

    • @PinkDraconian
      @PinkDraconian 4 года назад

      @@paulstelian97 I was talking about the non-fixed version. I just wanted to play around with it a bit.

    • @PinkDraconian
      @PinkDraconian 4 года назад

      @@user-qm4ev6jb7d hmm Yea. Didn't consider that. Thanks mate!

    • @paulstelian97
      @paulstelian97 4 года назад

      @@PinkDraconian ...the race itself with renameat does it as much justice as the breakpoint will do. And again, gdb on setuid is probably not fine.

  • @ChodaBoyUSA
    @ChodaBoyUSA 4 года назад

    @LiveOverflow, I noticed you did not close the file in readflag.c (before and after the refactor). Are you relying on cleanup to occur when the program exits? (I hope you feel well soon.)

    • @mina86
      @mina86 4 года назад +1

      Kernel is pretty good at cleaning up after processes which terminate and can easily deal with open file descriptors, allocated memory and other resources. Other than pedagogical, there’s no reason to worry about any of that in toy, non-production applications which terminate quickly.

    • @alerighi
      @alerighi 4 года назад

      Open file descriptors are closed when the program terminates. The only time where open file descriptors are not closed automatically and you need to close them is before an `exec()` system call: if you don't close them they are inherited by the program that you exec, if you didn't specify the option CLOSE_ON_EXEC when you opened the file (in which case they are closed).

    • @happygimp0
      @happygimp0 4 года назад

      And on a clone() / fork() call.

  • @unh0lys0da16
    @unh0lys0da16 4 года назад +1

    What about checking the inode of the file, can that be changed?
    Apparantly with debugfs you can change the inode of a file, perhaps this can be used to still exploit the fstat trick?

    • @TimLF
      @TimLF 4 года назад

      At that point you can just ignore file permissions completely

    • @unh0lys0da16
      @unh0lys0da16 4 года назад

      @@TimLF Ooh ofcourse, debugfs requires root doesn't it?

    • @mina86
      @mina86 4 года назад

      How would you check an inode? To get inode of a file you need to use stat. You potentially could store (device id, inode) pair from the stat call and then do another fstat once the file is open to compare if you’ve opened the same file but at that point the first stat was pointless. Maybe it could make sense if the check and use happen at completely different times and places in the code so during the check you store the inode but even then it’d probably be easier to just open the file at check time and keep an open file descriptor.

    • @unh0lys0da16
      @unh0lys0da16 4 года назад

      @@mina86 I guess the point of this inquiry is more to understand what happens if you assign a different file to the same inode once a file descriptor was already created, does it keep using the inode to address the file and potentially address the different file, or does it use the original file? I know this is not a security vulnerability, because as Tim L pointed out, debugfs requires root privileges, but I'm just interested in the theory at this point.

  • @weednweights
    @weednweights 3 года назад

    How does renaming symlink gives you root permission? Not sure to understand this part. I either get file is owned by root or couldn't open file.

  • @bit2shift
    @bit2shift 4 года назад +3

    𝚒𝚏(𝚏𝚍

  • @daviddelille1443
    @daviddelille1443 4 года назад

    Don't worry about making it to 24 videos. Your health is more important than an arbitrary challenge.

  • @happygimp0
    @happygimp0 4 года назад

    With your program, you can read the files from all other non-root users even without the race condition.

  •  4 года назад

    Get well soon ☕

  • @WizardNumberNext
    @WizardNumberNext 4 года назад

    Handle points to data and it does not care of file name. It does not care to that point that you can delete file and still read data behind non - existing file

  • @Hamstermoviespro
    @Hamstermoviespro 4 года назад

    Couldn't we also just store the filepath locally and only use the locally stored string instead of directly calling argv[1]?
    Would there be any problems doing it this way?

    • @a544jh
      @a544jh 4 года назад

      It doesn't matter because the argument (which is the file path) is staying the same anyways. It's the file the path is pointing to that is changing.

    • @Hamstermoviespro
      @Hamstermoviespro 4 года назад

      @@a544jh Ah I see, that is clever! Thanks for the help!

    • @happygimp0
      @happygimp0 4 года назад

      You did not understand the problem. The program asks the kernel "is the file ./flags owned by root?" when the kernel then says no, the program says "I want to open the file ./flags and read it", but in the meantime, the file ./flags was replaced by file owned by root.
      There is no problem of someone changing argv[1], that should not be possible without root privileges. argv[1] always has the path ./flags stored.

  • @InfiniteQuest86
    @InfiniteQuest86 4 года назад

    I'm confused. If you can do the file renaming part, can't you just run cat on the file and read it?

  • @fusca14tube
    @fusca14tube 4 года назад +1

    Well... I tried to reproduce this procedure but without success. I'm using Arch Linux and I always get "Couldn't open /tmp/flag" or "File /tmp/flag is owned by root". What I am doing wrong? Thanks in advance.

  • @clarksoft
    @clarksoft 3 года назад +1

    I came here from THM.

  • @imyxh
    @imyxh 4 года назад

    couldn't you just rewrite the actual file at /tmp/hax/flag from being a text file to the symlink? I mean, there's no guarantee the file can't change on disk after open(), is there?

  • @user-cx5jj3zq1r
    @user-cx5jj3zq1r 4 года назад

    gws

  • @happygimp0
    @happygimp0 4 года назад

    6:21 Can't you switch the descriptor in /proc/$PID/fd/ ? Of course you need to have root access to change it and the exploit does not work.

    • @mina86
      @mina86 4 года назад

      If ‘/proc’ is mounted as procfs then you cannot change what ‘/proc/$pid/fd/$fd’ maps to even if you’re root. It’s always the file descriptor opened by $pid. As root you can mess around with mount points but then as you’ve pointed out, you’re already root.

  • @demonicoli4049
    @demonicoli4049 4 года назад +2

    24 Hex-cember videos, so you are doing 36 Videos for Dec-ember!!??

  • @YuKonSama
    @YuKonSama 4 года назад

    The look during "it's a really awesome linux feature" talking about the "race-condition syscall" is priceless. That's basically the same telling someone about %n is a really awesome C feature :P

  • @akaash
    @akaash 4 года назад

    ༼ つ ◕_◕ ༽つ ⚡ TAKE MY ENERGY ⚡ ༼ つ ◕_◕ ༽つ

  • @mrlithium69
    @mrlithium69 4 года назад +2

    Idk what this "rena meat" thing is but it sounds dangerous

  • @MrSpikegee
    @MrSpikegee 4 года назад

    Mmh but this means you can move a file owned by root around even if you are not root ?

    • @happygimp0
      @happygimp0 4 года назад

      If it is in a directory you have write access to, then yes. You can't read or write to it, but you can move and delete it.

  • @fouzaialaa7962
    @fouzaialaa7962 4 года назад

    i watched a few times and i understood the solution but i didnt understand the problem !!! what happens when you use the file path the 2nd time ??

    • @LiveOverflow
      @LiveOverflow  4 года назад

      You can have switched the files between the first and second use of the file path.

    • @fouzaialaa7962
      @fouzaialaa7962 4 года назад

      @@LiveOverflow oh thx i understand now !!!

  • @JamesJones-zt2yx
    @JamesJones-zt2yx Год назад

    Did I miss the part about renameat?

  • @xdcountry
    @xdcountry 4 года назад

    I think your white blood cells are giving you a race condition. Hope you nab your bugs. Love your vids

  • @bugr33d0_hunter8
    @bugr33d0_hunter8 4 года назад

    So theres absolutely no way to change a file descriptor to change where it points, its permanent??

  • @chair547
    @chair547 4 года назад

    Never suid.

  • @sakari_n
    @sakari_n 4 года назад +1

    The syscall is not the cause of the race condition here.
    It is often necessary to rename or swap file path in legitimate programs.
    This same race condition here can be achieve with just renaming two files separately to swap them.
    The syscall used here just makes this quicker (making the example bug more likely to happen).
    The example program is the one containing the race condition bug.
    It first tests required permissions and then later reads some file with out any verifications that it is even the same file.
    Just had to post this commend after reading some mislead commenters.

    • @LiveOverflow
      @LiveOverflow  4 года назад +1

      I didn’t say it’s the reason for race condition. I call it a race condition syscall because it’s beautiful perfect to exploit race conditions with it.

  • @shaheenfazim
    @shaheenfazim 4 года назад

    youtube rabit hole :)

  • @Thorrez
    @Thorrez 4 года назад +3

    The inconsistent curly braces irritate me lol.

    • @Thorrez
      @Thorrez 4 года назад

      @@da_frank C programmers use inconsistent braces within a single program? Is it a measurably worse problem in C than other languages? The C++ code that I work on has consistent braces.

  • @2nafish117
    @2nafish117 4 года назад +1

    are you sleeping well? so many videos so fast recently ... also that stubble

  • @labangker3459
    @labangker3459 2 года назад

    I'd love to like this channel, but I wouldn't... You put such beautiful themes, I want to see so much! But then, everything goes so fast and you start cutting parts of the video randomly? Can you start explaining a little more about the things you're doing? I'm studying cybersecurity, and I just want to do more and more, your topics are very good, but please start explaining in more detail what's happening, I'm really clueless

  • @DantalionNl
    @DantalionNl 4 года назад +3

    "File Path Race Condition" do you mean Time of check vs time of use (TOCTOU)?

    • @LiveOverflow
      @LiveOverflow  4 года назад +6

      do you mean watch the video?

    • @DantalionNl
      @DantalionNl 4 года назад

      @@LiveOverflow I like guessing what it will be about before I watch, it's like a mini exam questions~

    • @DantalionNl
      @DantalionNl 4 года назад

      seems I passed the course

  • @steefant
    @steefant 4 года назад

    The demonstration of the exploit is actually somewhat buggy. It does not necessarily show that the root file was opened via open just that the stat call was reading the meta data of the file that is not owned by root... it might very well happen that the stat *and* the open call target the same non-root file, i.e. the exploit kind of fails. Edit: this even happens in the video at around 6:44.

    • @LiveOverflow
      @LiveOverflow  4 года назад

      Of course. That’s not buggy. That’s just the nature and uncertainty of race conditions.

    • @steefant
      @steefant 4 года назад

      @@LiveOverflow My point is that you did not explain it at all. Only at the end you mention the contents of the files. Without that information the viewer cannot comprehend what's actually happening when you execute the program. Also, no, this is not just the nature of RCs but the way the "exploit" is designed. Practical file toctou implementations try to increase the probability of winning the races, e.g. by creating file system mazes.

  • @bekircandal3528
    @bekircandal3528 4 года назад +3

    stop making videos just take care of yourself

  • @0xff733
    @0xff733 4 года назад

    I only clicked cuz I've never been this early 🙄 dont even have time to watch this rn 🤣

  • @w0ttheh3ll
    @w0ttheh3ll 4 года назад +1

    stop worrying about the amount of videos you post and get some rest.

  • @effleurager
    @effleurager 4 года назад

    Your render settings are causing some serious colour banding 🙈

    • @LiveOverflow
      @LiveOverflow  4 года назад

      Huh where?

    • @effleurager
      @effleurager 4 года назад

      @@LiveOverflow It's most noticable when you're using VSCode - flickering at 0:16, but also the beginning of the video that has some overall brightness shifts due to the banding

  • @0xc0ffee_
    @0xc0ffee_ 4 года назад

    But I don't get:
    - Why would a file launched as a non root user be able to read a root file? Isn't that an insane vulnerability being able to read any file on the system simply by writing some c code?
    - Why would you be able to rename a file not owned by you?

    • @LiveOverflow
      @LiveOverflow  4 года назад +3

      That’s setuid. Look through your system binaries. Many programs such as “sudo” or “ping” can be executrs by regular users but run as root. It’s a perfectly fine permission model. As long as these programs don’t have bugs like in this example.
      I’m not renaming the file I don’t own. I rename the symlink I created. So I own that symlink

    • @happygimp0
      @happygimp0 4 года назад +1

      You can rename files owned by root as a normal user, when it is in a directory owned by you where you have write access to the directory. You can not read or write the file, but you can move it and delete it.

  • @bgill7475
    @bgill7475 4 года назад

    This video is...racist.

  • @scanerang
    @scanerang 4 года назад

    I want you to know that I lost my interest interest in this channel. What got me here were the CTF videos. I liked them, as they are easily understandable (for someone who likes programming, but not security/hacking related) and don't require me to fully know how that specific platform works.
    Now you have shifted toward tutorials about specific exploits. While all of this is about hacking, what your viewers do with it is different.
    I just wanted to let you know. Good luck with your channel which ever way you go!

    • @LiveOverflow
      @LiveOverflow  4 года назад +2

      Wat. I have started this channel on tutorials. Just last month I released a CTF video and todays video was inspired by a CTF challenge.
      It’s always interesting to hear the completely skewed public perception versus the actual data.