Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make crypt and crypt_gensalt use thread-local output buffers. #201

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

besser82
Copy link
Owner

@besser82 besser82 commented Jan 6, 2025

This change makes crypt and crypt_gensalt as thread-safe as they can be without changing their interfaces. Solaris already made this change, and it’s being discussed by glibc (with suggestion that it should be pushed upstream to the C
and POSIX standards committees): https://sourceware.org/ml/libc-alpha/2018-10/msg00437.html

Portable programs should still use the r-variants, though, because this is not a guaranteed feature that is portable to other implementations of these functions. Also it doesn’t help threads to not clobber their corresponding output buffer on a second call from within the same thread.

The way I've implemented this mimicks the implementation from Solaris.

I've also taken into account some problems with -fno-plt code and thread-local storage on PPC64-architectures. See: https://sourceware.org/PR32387


This also fixes #62.

@besser82 besser82 requested review from zackw, ldv-alt and solardiz January 6, 2025 17:26
@besser82 besser82 self-assigned this Jan 6, 2025
Copy link
Collaborator

@solardiz solardiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't review the m4 stuff.

Overall, I am not sure this is worth the complexity and reduced portability, but I don't mind (once the issues I pointed out are addressed - especially what looks like a trivial yet major bug).

doc/crypt.3 Outdated Show resolved Hide resolved
doc/crypt.3 Outdated Show resolved Hide resolved
doc/crypt_gensalt.3 Outdated Show resolved Hide resolved
doc/crypt_gensalt.3 Outdated Show resolved Hide resolved
static TLS char output[CRYPT_OUTPUT_SIZE];
struct crypt_data nr_crypt_ctx;

crypt_r (key, setting, &nr_crypt_ctx);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now this calls crypt_r with uninitialized stack data in nr_crypt_ctx - a bug? IIRC, at least the initialized field should be set to 0.

Also, we probably incur some performance penalty by having this struct recreated each time, but perhaps only for descrypt?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now this calls crypt_r with uninitialized stack data in nr_crypt_ctx - a bug? IIRC, at least the initialized field should be set to 0.

Fixed by initializing the whole struct with explicit_bzero.

Also, we probably incur some performance penalty by having this struct recreated each time, but perhaps only for descrypt?

Well, the performance penalty is meassurable, but not noticeable: This change adds on average about 220 milliseconds of computing time for 1 Million invocations; so in a range of 200 - 300 nanoseconds per call. As those numbers are measured on an Intel Celeron 4205U with Fedora Workstation 41, the real life time penalty on affordable faster hardware, like a Ryzen 5 2400G, are expected be lower.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by initializing the whole struct with explicit_bzero.

Feels like mild misuse of explicit_bzero, but that's not necessarily bad. If we want to use it not only to initialize, but also to wipe any sensitive data that might have been left from a previous call, then perhaps we also want to do it for the output buffer before the strcpy_or_abort. And then maybe move the explicit_bzero of the data struct to after the strcpy_or_abort and introduce simple initialization (just memset)?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strcpy_or_abort already zeroizes the left over space after the copied string.

The cleanup of struct crypt_data is also already done by do_crypt (called from inside crypt_r) after the computation has been finished.

I've used explicit_bzero here intentionally to ensure the just stack-allocated struct crypt_data is really initialized properly before use.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like mild misuse of explicit_bzero, but that's not necessarily bad.

Changed to memset for initialization.

@besser82 besser82 force-pushed the topic/besser82/mt_tls branch 2 times, most recently from 74493c5 to a46e32d Compare January 14, 2025 13:35
Copy link

codecov bot commented Jan 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.16%. Comparing base (fe5b8b8) to head (beec01e).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #201      +/-   ##
===========================================
+ Coverage    90.15%   90.16%   +0.01%     
===========================================
  Files           32       32              
  Lines         3626     3631       +5     
  Branches       689      690       +1     
===========================================
+ Hits          3269     3274       +5     
  Misses         226      226              
  Partials       131      131              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@besser82 besser82 force-pushed the topic/besser82/mt_tls branch 2 times, most recently from 2222fc1 to bb9edb0 Compare January 14, 2025 15:10
@zackw
Copy link
Collaborator

zackw commented Jan 14, 2025

@besser82 I should have time to look at this early next week, but not before that.

This change makes crypt and crypt_gensalt as thread-safe as
they can be without changing their interfaces.  Solaris
already made this change, and it’s being discussed by glibc
(with suggestion that it should be pushed upstream to the C
and POSIX standards committees):
https://sourceware.org/ml/libc-alpha/2018-10/msg00437.html

Portable programs should still use the r-variants, though,
because this is not a guaranteed feature that is portable
to other implementations of these functions. Also it doesn’t
help threads to not clobber their corresponding output buffer
on a second call from within the same thread.
On ppc64 architectures we need to turn off the __tls_get_addr
runtime optimization for -fno-plt code in dynamic shared objects
by passing a certain flag (-Wl,--no-tls-get-addr-optimize) to
the linker, when compiling with gcc and using ld.bfd < 2.44.

See: https://sourceware.org/PR32387
@besser82 besser82 force-pushed the topic/besser82/mt_tls branch from faadf7b to beec01e Compare January 16, 2025 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants