Commit fd7325d
authored
Limit PKCS11 session creation (#49)
* Limit PKCS11 session creation
The PKCS#11 BCCSP previously fell back to OpenSession unbounded whenever
the session cache was empty, allowing the number of concurrently checked
out sessions to grow with the number of concurrent callers. Under high
sign concurrency this surfaces from HSM/PKCS#11 libraries as
CKR_SESSION_COUNT on OpenSession and CKR_DEVICE_ERROR on subsequent
operations (see issue #50).
Gate concurrent OpenSession calls behind a semaphore.Weighted whose
weight equals sessionCacheSize, so the number of outstanding sessions
is bounded. Cached sessions in sessPool intentionally do not occupy a
slot: getSession reacquires its own slot when pulling one out, and
returnSession releases the slot when caching, so a caller blocked on
the bound is unblocked the moment another session is returned or closed.
Expose the bound via a new public PKCS11Opts.SessionCacheSize field
(with json/yaml/mapstructure tags) so it can be configured through the
existing BCCSP factory configuration alongside Library/Label/Pin etc.,
with no environment variable required. A value of 0 selects the default
(30); a negative value disables the bound and preserves the original
unbounded code path for callers that want it.
Update closeSession to release the slot only for sessions that were
previously registered, guarding against double-release when an open
succeeds but the subsequent Login fails and the partial session is
closed before being tracked.
Tests in pkcs11_test.go (TestPKCS11GetSession, TestSessionHandleCaching)
exercise the bound: they fill the cache, spawn an extra getSession in
a goroutine, and assert it blocks until a session is returned. Without
this change those tests would fail because the goroutine would proceed
straight through to createSession instead of blocking.
Signed-off-by: Evan <evanyan@sign.global>
* pkcs11: simplify returnSession to a direct send
sessPool and sessSem now share cacheSize as capacity, so the channel
send in returnSession can never block: the caller still holds an
outstanding sem.Acquire(1) from its prior getSession(), which bounds
acquired-but-not-returned sessions A >= 1, leaving cached L <= cacheSize-1.
The select+default branch was therefore unreachable; remove it so the
control flow reflects the actual invariant.
Signed-off-by: Evan <evanyan@sign.global>
---------
Signed-off-by: Evan <evanyan@sign.global>1 parent 8fe16c9 commit fd7325d
6 files changed
Lines changed: 385 additions & 117 deletions
File tree
- bccsp/pkcs11
- vendor
- golang.org/x/sync/semaphore
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| 30 | + | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
| |||
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
52 | 59 | | |
53 | 60 | | |
54 | 61 | | |
| |||
90 | 97 | | |
91 | 98 | | |
92 | 99 | | |
93 | | - | |
94 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
95 | 103 | | |
96 | 104 | | |
97 | 105 | | |
| |||
100 | 108 | | |
101 | 109 | | |
102 | 110 | | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | 111 | | |
109 | 112 | | |
110 | 113 | | |
111 | 114 | | |
112 | 115 | | |
113 | 116 | | |
114 | | - | |
| 117 | + | |
| 118 | + | |
115 | 119 | | |
116 | 120 | | |
117 | 121 | | |
| |||
161 | 165 | | |
162 | 166 | | |
163 | 167 | | |
164 | | - | |
| 168 | + | |
165 | 169 | | |
166 | 170 | | |
167 | 171 | | |
| |||
329 | 333 | | |
330 | 334 | | |
331 | 335 | | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
332 | 351 | | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
| 352 | + | |
| 353 | + | |
341 | 354 | | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
342 | 367 | | |
343 | 368 | | |
344 | 369 | | |
| |||
379 | 404 | | |
380 | 405 | | |
381 | 406 | | |
382 | | - | |
383 | | - | |
384 | | - | |
385 | 407 | | |
| 408 | + | |
386 | 409 | | |
387 | 410 | | |
388 | 411 | | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
389 | 415 | | |
390 | 416 | | |
391 | 417 | | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | | - | |
| 418 | + | |
| 419 | + | |
399 | 420 | | |
400 | 421 | | |
401 | 422 | | |
| |||
0 commit comments