Port patch setting FD_CLOEXEC on curl sockets #858

Open
opened 2025-06-12 04:22:44 +00:00 by jade · 4 comments
Owner
Code here: https://github.com/NixOS/nix/pull/12439
Author
Owner

Note that this patch looks wrong and may not pass review in Lix in its current state as it looks to need rewriting as it is calling fcntl to set the fd as cloexec which is racy (or at the very least requires that you open the curl at a point in the program where it is single threaded/guaranteed not to fork before it is done, which, umm, bad!). You can set SOCK_CLOEXEC while opening the socket instead which is much safer and AFAIK supported on the platforms we care about. I don't know how to set that up via the curl API but surely there is some way to control the socket creation options; I would advise grepping for socket\( in the curl source code.

Note that this patch looks wrong and may not pass review in Lix in its current state as it looks to need rewriting as it is calling fcntl to set the fd as cloexec which is racy (or at the very least requires that you open the curl at a point in the program where it is single threaded/guaranteed not to fork before it is done, which, umm, bad!). You can set SOCK_CLOEXEC while opening the socket instead which is much safer and AFAIK supported on the platforms we care about. I don't know how to set that up via the curl API but surely there is some way to control the socket creation options; I would advise grepping for `socket\(` in the curl source code.
Author
Owner

Oh no... okay their patch is correct on macOS because apparently you can't create a network socket safely on Mac. wat! Still ideally we should set it on socket creation on Linux, that's surprisingly goofy.

Oh no... okay their patch is correct on macOS because apparently you can't create a network socket safely on Mac. wat! Still ideally we should set it on socket creation on Linux, that's surprisingly goofy.
Member

This is best done by hooking into CURLOPT_OPENSOCKETFUNCTION it looks like, so we either create it with SOCK_CLOEXEC or set FD_CLOEXEC with fcntl immediately after.

I may implement it, but anyone else, feel free

This is best done by hooking into [`CURLOPT_OPENSOCKETFUNCTION`](https://curl.se/libcurl/c/CURLOPT_OPENSOCKETFUNCTION.html) it looks like, so we either create it with `SOCK_CLOEXEC` or set `FD_CLOEXEC` with fcntl immediately after. I may implement it, but anyone else, feel free
helle self-assigned this 2025-06-12 13:15:53 +00:00
Member

This issue was mentioned on Gerrit on the following CLs:

  • commit message in cl/3351 ("Set SOCK_CLOEXEC/FD_CLOEXEC on sockets created by curl")
<!-- GERRIT_LINKBOT: {"cls": [{"backlink": "https://gerrit.lix.systems/c/lix/+/3351", "number": 3351, "kind": "commit message"}], "cl_meta": {"3351": {"change_title": "Set SOCK_CLOEXEC/FD_CLOEXEC on sockets created by curl"}}} --> This issue was mentioned on Gerrit on the following CLs: * commit message in [cl/3351](https://gerrit.lix.systems/c/lix/+/3351) ("Set SOCK_CLOEXEC/FD_CLOEXEC on sockets created by curl")
Sign in to join this conversation.
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: lix-project/lix#858
No description provided.