Bug 185138 - [Curl] Add OpenSSL/LibreSSL multi-threading support
Summary: [Curl] Add OpenSSL/LibreSSL multi-threading support
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Basuke Suzuki
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2018-04-30 11:10 PDT by Basuke Suzuki
Modified: 2018-05-03 00:24 PDT (History)
11 users (show)

See Also:


Attachments
PATCH (4.08 KB, patch)
2018-04-30 11:15 PDT, Basuke Suzuki
no flags Details | Formatted Diff | Diff
fix (4.02 KB, patch)
2018-05-01 22:14 PDT, Basuke Suzuki
no flags Details | Formatted Diff | Diff
fix (3.91 KB, patch)
2018-05-01 22:16 PDT, Basuke Suzuki
pvollan: review+
pvollan: commit-queue-
Details | Formatted Diff | Diff
Fix potential security issue (4.85 KB, patch)
2018-05-02 14:29 PDT, Basuke Suzuki
no flags Details | Formatted Diff | Diff
Fix style (4.89 KB, patch)
2018-05-02 15:14 PDT, Basuke Suzuki
pvollan: review+
Details | Formatted Diff | Diff
Fox (5.04 KB, patch)
2018-05-02 23:27 PDT, Basuke Suzuki
no flags Details | Formatted Diff | Diff
typo (5.04 KB, patch)
2018-05-02 23:41 PDT, Basuke Suzuki
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Basuke Suzuki 2018-04-30 11:10:43 PDT
The older OpenSSL manual says the locking_function and threadid_funcion should set when use it in multi-threading environment. This applies to LibreSSL also.
https://www.openssl.org/docs/man1.0.2/crypto/threads.html

For unix and other similar os, the default threadId_function implementation is good enough. We'll set custom callback only for Windows OS.

Note it's not required for OpenSSL 1.1.0 and after.
https://www.openssl.org/blog/blog/2017/02/21/threads/
Comment 1 Basuke Suzuki 2018-04-30 11:15:16 PDT
Created attachment 339130 [details]
PATCH
Comment 2 Per Arne Vollan 2018-04-30 13:50:24 PDT
Would it be sufficient to require that OpenSSL 1.1.0 or later is used?
Comment 3 Basuke Suzuki 2018-04-30 15:44:18 PDT
We believe so. What is your worry for instance?
Comment 4 Per Arne Vollan 2018-04-30 18:52:20 PDT
I was just wondering if this change is strictly needed, since this issue has been addressed in OpenSSL 1.1.0 or later. But this is perhaps not the case for LibreSSL?
Comment 5 Basuke Suzuki 2018-04-30 21:18:28 PDT
Oh, that’s not true. This is required for both OpenSSL prior to 1.1.0 and whole version of LibreSSL.
Comment 6 Per Arne Vollan 2018-05-01 07:09:06 PDT
Comment on attachment 339130 [details]
PATCH

View in context: https://bugs.webkit.org/attachment.cgi?id=339130&action=review

> Source/WebCore/platform/network/curl/CurlSSLHandle.cpp:147
> +    if (!lock || (type > lockNum))
> +        return;

I think this should be '(type >= lockNum)'. Also, should we return early if type < 0?
Comment 7 Basuke Suzuki 2018-05-01 18:04:06 PDT
Comment on attachment 339130 [details]
PATCH

View in context: https://bugs.webkit.org/attachment.cgi?id=339130&action=review

Thank you for the review.

>> Source/WebCore/platform/network/curl/CurlSSLHandle.cpp:147
>> +        return;
> 
> I think this should be '(type >= lockNum)'. Also, should we return early if type < 0?

type argument are hard-coded in OpenSSL/LibreSSL code and used internally only. I think it should be not run-time check, but assert. Also lock shouldn't be checked like this. If it failed, it may fail in multi thread env. I will move it to more robust place to define locks.
Comment 8 Basuke Suzuki 2018-05-01 22:14:37 PDT
Created attachment 339278 [details]
fix
Comment 9 Basuke Suzuki 2018-05-01 22:16:19 PDT
Created attachment 339279 [details]
fix
Comment 10 Per Arne Vollan 2018-05-02 07:03:49 PDT
Comment on attachment 339279 [details]
fix

View in context: https://bugs.webkit.org/attachment.cgi?id=339279&action=review

R=me.

> Source/WebCore/platform/network/curl/CurlSSLHandle.cpp:144
> +    ASSERT(type >= 0 && type < CRYPTO_NUM_LOCKS);

I think we should use RELEASE_ASSERT here.
Comment 11 Per Arne Vollan 2018-05-02 07:55:44 PDT
Comment on attachment 339279 [details]
fix

View in context: https://bugs.webkit.org/attachment.cgi?id=339279&action=review

> Source/WebCore/platform/network/curl/CurlSSLHandle.cpp:143
> +    static Lock lock[CRYPTO_NUM_LOCKS];

Also, I think you need to make sure the initialization of lock is thread safe, by using call_once, for example.
Comment 12 Basuke Suzuki 2018-05-02 10:21:18 PDT
Comment on attachment 339279 [details]
fix

View in context: https://bugs.webkit.org/attachment.cgi?id=339279&action=review

Thanks for the review.

>> Source/WebCore/platform/network/curl/CurlSSLHandle.cpp:143
>> +    static Lock lock[CRYPTO_NUM_LOCKS];
> 
> Also, I think you need to make sure the initialization of lock is thread safe, by using call_once, for example.

I think Lock is constexpr constructor class and it is initialized in compile time, which means no run-time initialization. I am not confident about this behavior so I am asking about this my co-workers.

>> Source/WebCore/platform/network/curl/CurlSSLHandle.cpp:144
>> +    ASSERT(type >= 0 && type < CRYPTO_NUM_LOCKS);
> 
> I think we should use RELEASE_ASSERT here.

Sure I do.
Comment 13 Basuke Suzuki 2018-05-02 14:29:32 PDT
Created attachment 339340 [details]
Fix potential security issue
Comment 14 EWS Watchlist 2018-05-02 14:31:58 PDT
Attachment 339340 [details] did not pass style-queue:


ERROR: Source/WebCore/platform/network/curl/CurlSSLHandle.h:69:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
ERROR: Source/WebCore/platform/network/curl/CurlSSLHandle.h:74:  Place brace on its own line for function definitions.  [whitespace/braces] [4]
Total errors found: 2 in 3 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 15 Basuke Suzuki 2018-05-02 15:14:22 PDT
Created attachment 339353 [details]
Fix style
Comment 16 Per Arne Vollan 2018-05-02 16:13:53 PDT
Comment on attachment 339353 [details]
Fix style

View in context: https://bugs.webkit.org/attachment.cgi?id=339353&action=review

R=me.

> Source/WebCore/ChangeLog:6
> +        The older OpenSSL manual says the locking_function and threadid_funcion should

Small typo, change phrase to 'threadid_function should be set'.

> Source/WebCore/platform/network/curl/CurlSSLHandle.h:74
> +            static ThreadSupport shared;

I think we can use NeverDestroyed here.
Comment 17 Basuke Suzuki 2018-05-02 23:27:35 PDT
Created attachment 339391 [details]
Fox

Thanks for r+ Per Arne.
Comment 18 Basuke Suzuki 2018-05-02 23:41:56 PDT
Created attachment 339392 [details]
typo
Comment 19 WebKit Commit Bot 2018-05-03 00:23:14 PDT
Comment on attachment 339392 [details]
typo

Clearing flags on attachment: 339392

Committed r231297: <https://trac.webkit.org/changeset/231297>
Comment 20 WebKit Commit Bot 2018-05-03 00:23:16 PDT
All reviewed patches have been landed.  Closing bug.
Comment 21 Radar WebKit Bug Importer 2018-05-03 00:24:15 PDT
<rdar://problem/39934014>