Skip to content

Support thread-safe reading of the subordinate ID database #1362

@blm768

Description

@blm768

For an project I've been working on, I've found it would be handy if the read-only functions public functions in libsubid were thread-safe to call. I can protect my own calls to the library with a mutex, but that doesn't protect against issues if my code gets included as part of another application that calls into libsubid without using that mutex. I suppose that case is a bit contrived, so my mutex workaround seems workable for now, but I'd prefer for this to eventually be bulletproof.

Factors I've found so far that could affect thread safety:

  • subordinate_uid_db and subordinate_gid_db (subordinateio.cpp) keep a fair amount of mutable state with no thread synchronization.

    Just making them thread-local might be enough to solve that issue, although that would technically be a breaking change. I'm not seeing any thread-locals elsewhere in the source, so I'm not sure if there would be compatibility issues with them on some platform that the project actively supports. It looks like the locking logic might also need some revamping if these were thread-local since it seems to be written with the assumption that there will be one lock per process.

    Putting a mutex around them might also be an option as long as all supported platforms have support for C11 mutexes. Alternatively, if the read-only functions used their own instances of commonio_db, we could skip some of the locking concerns.

  • The static lock_count and nscd_need_reload variables in commonio.c aren't thread-safe.

  • The various calls to getpwnam, etc. would have to be switched to use getpwnam_r.

If there's general interest in making the library thread-safe where feasible, I can dedicate some time to reading through the code more thoroughly and seeing if I can get some reasonable patches together.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions