Zum Hauptinhalt springen
Intelligence View
⚡ tsecurity.de Intelligence

Secure File Descriptor Handling

During the 2.6.27 merge window a number of my patches were merge and now we are at the point where we can securely create file descriptors without the danger…

Beitrag
0
Seite
0
↗ Quelle (udrepper.livejournal.com)
Cloud & Tech-Stack · 6 Min. Lesezeit
Quelle: udrepper.livejournal.com
Rubrik: Unix & Linux Server

During the 2.6.27 merge window a number of my patches were merge and now we are at the point where we can securely create file descriptors without the danger of possibly leaking information. Before I go into the details let's get some background information.



A file descriptor in the Unix/POSIX world has lots of state associated with it. One bit of information determines whether the file descriptor is automatically closed when the process executes an exec call to start executing another program. This is useful, for instance, to establish pipelines.
Traditionally, when a file descriptor is created (e.g., with the default open() mode) this close-on-exec flag is not set and a programmer has to explicitly set it using




fcntl(fd, F_SETFD, FD_CLOEXEC);


Closing the descriptor is a good idea for two main reasons:




  • the new program's file descriptor table might fill up. For every open file descriptor resources are consumed.

  • more importantly, information might be leaked to the second program. That program might get access to information it normally wouldn't have access to.



It is easy to see why the latter point is such a problem. Assume this common scenario:




A web browser has two windows or tabs open, both loading a new page (maybe triggered through Javascript). One connection is to your bank, the other some random Internet site. The latter contains some random object which must be handled by a plug-in. The plug-in could be an external program processing some scripting language. The external program will be started through a fork() and exec sequence, inheriting all the file descriptors open and not marked with close-on-exec from the web browser process.


The result is that the plug-in can have access to the file descriptor used for the bank connection. This is especially bad if the plug-in is used for a scripting language such a Flash because this could make the descriptor easily available to the script. In case the author of the script has malicious intentions you might end up losing money.



Until not too long ago the best programs could to is to set the close-on-exec flag for file descriptors as quickly as possible after the file descriptor has been created. Programs would break if the default for new file descriptors would be changed to set the bit automatically.



This does not solve the problem, though. There is a (possibly brief) period of time between the return of the open() call or other function creating a file descriptor and the fcntl() call to set the flag. This is problematic because the fork() function is signal-safe (i.e., it can be called from a signal handler). In multi-threaded code a second thread might call fork() concurrently. It is theoretically possible to avoid these races by blocking all signals and by ensuring through locks that fork() cannot be called concurrently. This very quickly get far too complicated to even contemplate:




  • To block all signals, each thread in the process has to be interrupted (through another signal) and in the signal handler block all the other signals. This is complicated, slow, possibly unreliable, and might introduce deadlocks.

  • Using a lock also means there has to be a lock around fork() itself. But fork() is signal safe. This means this step also needs to block all signals. This by itself requires additional work since child processes inherit signal masks.

  • Making all this work in projects which come from different sources (and which non-trivial program doesn't use system or third-party libraries?) is virtually impossible.



It is therefore necessary to find a different solution. The first set of patches to achieve the goal went into the Linux kernel in 2.6.23, the last, as already mentioned, will be in the 2.6.27 release. The patches are all rather simple. They just extend the interface of various system calls so that already existing functionality can be taken advantage of.



The simplest case is the open() system call. To create a file descriptor with the close-on-exec flag atomically set all one has to do is to add the O_CLOEXEC flag to the call. There is already a parameter which takes such flags.



The next more complicated is the solution chosen to extend the socket() and socketcall() system calls. No flag parameter is available but the second parameter to these interfaces (the type) has a very limited range requirement. It was felt that overloading the parameter is an acceptable solution. It definitely makes using the new interfaces simpler.



The last group are interfaces where the original interface simply doesn't provide a way to pass additional parameters. In all these cases a generic flags parameter was added. This is preferable to using specialized new interfaces (like, for instance, dup2_cloexec) because we do and will need other flags. O_NONBLOCK is one case. Hopefully we'll have non-sequential file descriptors at some point and we can then request them using the flags, too.



The (hopefully complete) list of interface changes which were introduced is listed below. Note: these are the userlevel change. Inside the kernel things look different.




















Userlevel Interface     What changed?
openO_CLOEXEC flag added
fcntlF_DUPFD_CLOEXEC command added
recvmsgMSG_CMSG_CLOEXEC flag for transmission of file descriptor over Unix domain socket which has close-on-exec set atomically
dup3New interface taking an addition flag parameter (O_CLOEXEC, O_NONBLOCK)
pipe2New interface taking an addition flag parameter (O_CLOEXEC, O_NONBLOCK)
socketSOCK_CLOEXEC and SOCK_NONBLOCK flag added to type parameter
socketpairSOCK_CLOEXEC and SOCK_NONBLOCK flag added to type parameter
pacceptNew interface taking an addition flag parameter (SOCK_CLOEXEC, SOCK_NONBLOCK) and a temporary signal mask
fopenNew mode 'e' to open file with close-on-exec set
popenNew mode 'e' to open pipes with close-on-exec set
eventfdTake new flags EFD_CLOEXEC and EFD_NONBLOCK
signalfdTake new flags SFD_CLOEXEC and SFD_NONBLOCK
timerfdTake new flags TFD_CLOEXEC and TFD_NONBLOCK
epoll_create1New interface taking a flag parameter. Support EPOLL_CLOEXEC and EPOLL_NONBLOCK
inotify_init1New interface taking a flag parameter (IN_CLOEXEC, IN_NONBLOCK)


When should these interfaces be used? The answer is simple: whenever the author is not sure that no asynchronous fork()+exec can happen or a concurrently running threads executes fork()+exec (or posix_spawn(), BTW).



Application writers might have control over this. But I'd say that in all library code one has to play it safe. In glibc we do now in almost all interfaces open the file descriptor with the close-on-exec flag set. This means a lot of work but it has to be done. Applications also have to change (see this autofs bug, for instance).

Social ReaktionenReagiere als Erste:r — dein Feedback zählt!
Informations-Bewertung:
✏️ Information ergänzen
🛡️ Versioniert via CMS-Revisions-Engine (Freigabe-Queue für Gäste)

💬 Dyskusja społeczności i komentarze

🌐 Dołącz do głównego czatu

Rozpocznij dyskusję w strumieniu transmisji Social Chat lub prześlij wyżej wkład ekspercki przez formularz uzupełnień.

Security Briefing Bezpłatnie · Wypisanie 1 kliknięciem

Intelligence Digest - bezpłatnie

Codziennie najważniejsze wiadomości bezpieczeństwa · można pobrać w każdej chwili

Community Rating & Social Proof
⭐ Ocena czytelników
–
Ø / 5.0
Brak recenzji czytelników
Czy ten artykuł był pomocny?
0
Dodaj swoją ocenę
Ocena szczegółowa (opcjonalnie)
Treść i jakość
Źródło i wiarygodność
Aktualność

Powiązany klaster opowieści i źródła (wektor AI)

6 Cluster
26 źródeł
Black Friday Oura Ring deals: The best of the deals so far, picked by a wearable-tech expert
Latest from TechRadar 16 minut temu
25 kolejnych źródeł
CNET6 lat temuOur Favorite Blenders of 2026 Are on Sale Ahead of Black Friday CNET20 minut temuBrighten Up Your Day by Saving 20% Off Flashlights With Olight’s Black Friday Sale CNET12 minut temuLive: Black Friday Deals Are Here. Shop Expert Picks Across Tech, Everyday Essentials and More at Up to 60% Off Golem.de26 minut temuAnzeige: Logitech-Tastatur MX Keys S am Black Friday zum Sensationspreis auf Amazon PC-WELT38 minut temuLuminar zum Black Friday: KI-Bildbearbeitung für nur 79,20 Euro Latest from TechRadar53 minuty temuThis is the pair of open earbuds to hunt down over Black Friday Latest from TechRadar1 godzinę temuBest AirPods Black Friday deals: all live savings on AirPods Pro 3 and more Computer Bild1 godzinę temuPhilips Sonicare DiamondClean 9000 zum Black Friday günstiger netzwelt.de2 godziny temuESR startet mit Black-Friday-Rabatten: Diese AirPods-Hüllen lohnen sich jetzt netzwelt.de2 godziny temuPlayStation 5 geschenkt: Diese Black-Friday-Aktion legt die Konsole gratis obendrauf netzwelt.de1 godzinę temu6 Dyson-Staubsauger im Vergleich: Dieser Sauger wird am Black Friday überraschend zum Preis/Leistungssieger netzwelt.de1 godzinę temuMonitore im Angebot zum Black Friday: Gutes Bild für Office, Gaming und mehr Golem.de56 minut temuAnzeige: Akkuschrauber mit 20 Bits zum Black Friday erstmals unter 50 Euro bei Amazon PC-WELT1 godzinę temuDie günstigsten Handytarife an Black Friday von 10 GB bis Unlimited: Hier lohnt sich ein Wechsel PC-WELT1 godzinę temuBlack Friday 2026 bei LiberNovo: Ergonomische Bürostühle mit bis zu 44 Prozent Rabatt Latest from TechRadar1 godzinę temuThe best Black Friday robot vacuum deals: Dreame, Shark, Dyson, and Roborock Golem.de1 godzinę temuAnzeige: Topdon-Starthilfe-Powerbank für Kfz zum Black-Friday-Bestpreis nur rund 85 Euro heise online News1 godzinę temuBlack Friday: Die besten Kopfhörer zum Tiefstpreis – Sonos Ace, Nothing & Shokz Latest from Windows Central1 godzinę temuDell's Black Friday deals drop major savings on Alienware OLED monitors CNET2 lata temuMake Every Night Game Night and Save $90 on the Nex Playground With This Black Friday Deal DEV Community1 godzinę temuThe app is part of the gadget: 14,000 App Store reviews of 28 device apps before Black Friday Latest from TechRadar2 godziny temuBlack Friday tech deals under $100 / £100: Fire TV Sticks, earbuds, coffee machines and more Caschys Blog3 godziny temuGoogle One: Black-Friday-Angebot mit bis zu 50 Prozent Rabatt netzwelt.de2 godziny temuDem Testsieger ebenbürtig: Zum Black Friday stürzt Xiaomis stärkster E-Scooter im Preis ab Macerkopf.de2 godziny temuFotos verbessern mit Aiarty: 52 % Rabatt + Gratisgeschenke zum Black Friday
Podobne wkłady
🔍 Verwandte News

Auch interessante Nachrichten Secure File Descriptor Handling

Thematisch verwandte Begriffe: Secure, File, Descriptor, Handling · 6 Treffer

Laden...

Filmy są załadowane...

Laden...

Składki są załadowane...

Laden...

Filmy są załadowane...

Laden...

Składki są załadowane...

Laden...

Filmy są załadowane...

Laden...

Składki są załadowane...

Laden...

Filmy są załadowane...

Laden...

Składki są załadowane...

Laden...

Filmy są załadowane...

Uwagi Komentarze są ładowane..
Rysunek do aktualizacji
Widgets & Topics
Loading widgets…
Nächster Beitrag