Fixes #2397 Add shared session cache in SSL_CTX#3761
Fixes #2397 Add shared session cache in SSL_CTX#3761tmshort wants to merge 2 commits intoopenssl:masterfrom
Conversation
|
If I recall correctly @vdukhovni was interested in something like this. |
ssl/ssl_lib.c
Outdated
There was a problem hiding this comment.
Indent removal is unjustified.
ssl/ssl_lib.c
Outdated
There was a problem hiding this comment.
If you have styling issue in near vicinity, fix it. I mean it should be return l;, right?
ssl/ssl_sess.c
Outdated
There was a problem hiding this comment.
I'd say this kind of calls for "philosophical" problem. Locking kind of implies that you "legitimate" "battle" of callback setters. I mean you kind of say "everybody is allowed to set/change callback to their liking at any time and I ensure that it's properly serialized among threads". But this is not true. It's more than likely that it would be inappropriate to change callbacks once object is instantiated. Or at least allow different threads to have different opinions on what callbacks are...
There was a problem hiding this comment.
Yes. These callback were originally left in the SSL_CTX (in our original implementation), but were moved to the cache structure for some consistency. However; one only of these callbacks is invoked (regardless of the number of SSL_CTX's sharing the cache), depending on which SSL_CTX is "active" when an SSL_SESSION is added, removed, etc; since the SSL_CTX is passed into the callback. If a list of SSL_CTX's were kept in the cache, then a callback could be invoked for each of them, at which point, having SSL_CTX-specifc callbacks makes sense (but so does having a single callback).
So, I'm kinda "meh" on how the callbacks are stored.
There was a problem hiding this comment.
What I'm trying to say is that this seems like case of excessive locking. Or on certain level meaningless locking. Don't callbacks kind of define how SSL_SESSION operates and should be set prior it becomes appropriate to share it? Because in such case, locking is not necessary...
There was a problem hiding this comment.
I was thinking of a bigger picture; whether these should even be part of the SSL_SESSION_CACHE structure or remain in SSL_CTX. The locking may seem like overkill, but it's safe in all conditions.
ssl/ssl_sess.c
Outdated
There was a problem hiding this comment.
this is not synchronized like SSL_CTX_sess_set_remove_cb ?
There was a problem hiding this comment.
It's a read, not a write, so it's safe. If I were reading multiple things, or had to read multiple values to generate a return value, it would be locked. For example, |session_cache| is always non-NULL, if there was a chance it would be NULL, I would use a lock since I'd have to do a check.
There was a problem hiding this comment.
The access to ctx->session_cache is not safe, if session_cache can be changed
simultaneously.
However I agree with @dot-asm that such use should be considered invalid.
There was a problem hiding this comment.
A processor cannot do a simultaneous read and write to memory; so the pointer will never have an invalid value. If anything, you will get stale data, which may not matter. But @dot-asm's point still applies; and it's questionable if these functions should be in the SSL_SESSION or SSL_CTX; but I think that the functions associate closer with an SSL_SESSION than an SSL_CTX.
There was a problem hiding this comment.
I mean, if you overwrite the the session_cache in one thread, and another thread executes something
that uses ctx->session_cache, it will load the pointer into a register and access the object creating
a race with the other thread deleting the old value of session_cache.
If the read/write locking in SSL_CTX_share_session_cache is good for anything then only
if the value is read with read lock at all times.
Have you tried this with thread sanitizer?
There was a problem hiding this comment.
I see your point, but it only becomes a problem if the memory is unmapped when freed. The chance that this occurring is infinitely small, but non-zero.
But, the problem you are referring to is slightly bigger as the old session cache is freed before the new one is assigned, so there is an extremely small window where the old session_cache may be freed before the new one is assigned.
I have not had the chance to run it through thread-sanitizer.
But the bigger picture I'm interested in is if these functions belong with the SSL_SESSION_CACHE, or should stay with the SSL_CTX.
ssl/ssl_lib.c
Outdated
There was a problem hiding this comment.
would this dead-lock if a == b ?
There was a problem hiding this comment.
Yes, and that'd be bad mojo. I consider that a serious user error. But, from a different viewpoint, it should always succeed.
|
Well it’s a “safe” read if the address isn’t being modified by another thread. We generally don’t care about that, but I just came across code (RAND_get_method) that does. Shrug. This is just a side-note.
|
Yes, even if the pointer is being modified by another thread, a pointer modification is generally done in one memory operation, so this won't segfault or anything. You might get stale data, which would've been correct at the time the function was invoked. |
The only counter-example I can think of is far pointers in 16-bit x86 code; which we don't support anymore. |
|
And possibly 32-bit mode on a 80386SX (which has a 16-bit external bus, or any other processor where the memory data bus is less than the address size, such as 68008), but that would be two bus cycles, and still considered a single memory operation. |
37f6f16 to
7eff280
Compare
|
Fixes #2397 |
I think non-x86 platforms can have non-atomic accesses to word-sized variables, though don't have an example handy. |
|
Sorry for starting this sub-thread. My main point was that we’re not consistent.
|
ssl/ssl_lib.c
Outdated
There was a problem hiding this comment.
blank line after decl ...
You could merge declaration with his next assignment line too .
7eff280 to
dfb11f3
Compare
|
I'm updating the get/set routines of SSL_SESSION_CACHE. These should resolve most of the issues raised.
Also updated the SSL_CTX_share_session_cache() to avoid the very small window where session_cache might point to a stale object. |
dfb11f3 to
c553771
Compare
|
I wonder if it would be more straight forward to ask the application |
|
@bernd-edlinger, this does not replace those callbacks, but provides a simpler mechanism to share sessions among SSL_CTX without having to write a lot of code. |
617a62b to
b73195a
Compare
b73195a to
22938f8
Compare
|
I'm okay with this; are you, @bernd-edlinger ? |
ssl/ssl_lib.c
Outdated
There was a problem hiding this comment.
This lock might theoretically cause a dead-lock.
Suppose one thread calls
SSL_CTX_share_session_cache(a.b);
and the other thread calls
SSL_CTX_share_session_cache(b,a);
There was a problem hiding this comment.
Yup, although I would except that SSL_CTX setup occur in an initialization thread, and that two threads wouldn't be modifying separate SSL_CTX's. Although, I did have a design where there was one SSL_CTX per thread that was affinity locked to a processor core... not that we used that.
|
@richsalz actually I would be more happy to see an explicit SSL_SESSION_CACHE API object, |
|
yes, that would be a better approach. Todd's on vacation, let's see what he says when he gets back. |
|
Hmm, would that have to wait for 1.2? |
8f95bfb to
12be35a
Compare
|
Ok, rebased, updated the locks and the documentation. I'm still thinking of adding SSL_SESSION_CACHE_set/get APIs, but have yet to do that. |
12be35a to
39630b1
Compare
39630b1 to
b56d956
Compare
|
Rebased |
b56d956 to
7e9cb34
Compare
|
Looks to be a doc-nits travis failure. |
7e9cb34 to
7d47ef8
Compare
|
It looks like at least both this and #4848 are trying to modify and/or extend the behavior of SSL_CTX_new() -- perhaps we should step back and think about what sorts of things we might want to do at the time of SSL_CTX creation and how to have a minimal number of APIs that supports all of those things in a reasonable fashion. |
e0fff70 to
5aa4e1d
Compare
|
Given that the SSL_SESSION_CACHE exists as a separate object, and there are potential changes (PR #5945) to add a different version of SSL_CTX_new_ex(), perhaps SSL_SESSION_CACHE can be assigned to a newly created SSL_CTX, i.e. one that has a reference count of 1, and/or no sessions attached to it? |
de12e07 to
2a14408
Compare
Support for shared session cache via SSL_CTX_share_session_cache(). The SSL_CTX_sessions() API always returns NULL, as the lock for the shared session cache is not exposed to library consumers. Use this method to share the cache for the resumption tests.
2a14408 to
957d143
Compare
|
We are not going to attempt to get this in... |
Support for shared session cache via SSL_CTX_share_session_cache().
The SSL_CTX_sessions() API always returns NULL, as the lock for the shared
session cache is not exposed to library consumers.
Use this method to share the cache for the resumption tests.
Checklist