Repository navigation
Conversation
New ignored the return value of the module's C_GetFunctionList and never checked the pointer it received. A module whose C_GetFunctionList fails leaves sym NULL, and New returned a usable-looking *Ctx for it. The first call into the module then dereferences that NULL function list and the process dies with a SIGSEGV instead of the caller getting an error: SIGSEGV: segmentation violation PC=0x1e392a9 m=7 sigcode=1 addr=0x8 signal arrived during cgo execution github.com/miekg/pkcs11._Cfunc_Initialize(0x7f7e64000e20) github.com/miekg/pkcs11.(*Ctx).Initialize(...) The faulting address is offset 8, where C_Initialize sits in CK_FUNCTION_LIST. Callers cannot defend against this: the nil check they are told to do on New passes, and sym is not reachable from Go. Check that C_GetFunctionList returns CKR_OK and hands out a non-NULL function list, and return NULL otherwise, so the failure surfaces as the nil *Ctx that New already documents. Also release the module on the C_GetFunctionList lookup failure path, which freed the ctx but left the library loaded. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
New returns nil for a module it cannot use and says nothing else. The reason exists at the point of failure — dlerror has a message, the Windows loader has an error code, and C_GetFunctionList has a CK_RV — but all of it was thrown away, leaving callers to guess between a missing file, a module that is not a PKCS#11 one, and a module that failed to start up. Hand that reason back from the C New: a message in a char **err out-parameter, filled from dlerror or from the CK_RV, and on Windows the GetLastError code in an unsigned long *code, which syscall.Errno renders into words in Go rather than FormatMessage in C. New(module string) *Ctx keeps its signature and its behaviour, so this is backwards compatible. The new NewWithError returns the reason: pkcs11: /path/module.so: cannot open shared object file: No such file or directory pkcs11: /path/module.so: undefined symbol: C_GetFunctionList pkcs11: /path/module.so: C_GetFunctionList failed: 0x5 pkcs11: /path/module.so: C_GetFunctionList returned no function list p11.OpenModule already returns an error, so it passes this one on instead of its own wording. The README skeleton uses NewWithError too, where it would otherwise have panicked on a nil p. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Author
|
@miekg do you have time to leave a review? |
|
I believe I'm seeing the same issue with HashiCorp Vault while talking to an Entrust HSM: It seems that the immediate cause was a filesystem permission issue: once the vault user was able to read |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Newignores the return value of the module'sC_GetFunctionListand never checks the pointer it receives:A module whose
C_GetFunctionListfails — returns non-CKR_OKwithout writing*ppFunctionList— therefore leavessymNULL whileNewstill returns a non-nil*Ctx. The first call into the module dereferences that NULL function list and the process dies:addr=0x8is the offset ofC_InitializeinCK_FUNCTION_LIST, after the padded two-byteCK_VERSION— that isc->sym->C_Initializewithc->sym == NULL.Callers cannot defend against this: the
New(...) == nilcheck they are told to do passes, andsymis not reachable from Go. We hit it in production with a vendor HSM client library that fails inC_GetFunctionListwhen it cannot read its own configuration. The process segfaulted instead of the caller getting an error.Fix
Check that
C_GetFunctionListreturnsCKR_OKand hands out a non-NULL function list, and return NULL otherwise — the nil*CtxthatNewalready documents for a module it cannot use. Applied to both the Windows and the dlfcn branch.Also releases the module on the
C_GetFunctionListlookup failure path, which previously freed the ctx but left the library loaded.Reporting why
Newreturning a bare nil says nothing about what went wrong, anddlerror's message was thrown away. The CNewnow reports why it failed. On the dlfcn side it fills achar **errout-parameter fromdlerror, or from theCK_RVwhenC_GetFunctionListis the one that failed; the Go side owns and frees that message. The Windows loader reports a code rather than a message, so thereNewonly capturesGetLastErrorinto aunsigned long *codeout-parameter and Go renders it withsyscall.Errno, which already callsFormatMessageand trims the trailing newline.New(module string) *Ctxkeeps its signature and behaviour, so this is backwards compatible. The newNewWithError(module string) (*Ctx, error)hands out the reason:The errors now read:
p11.OpenModulealready returns an error, so it now passes this one on instead of its ownfailed to load module %q. The README skeleton usesNewWithErrortoo, since as written it would have panicked on a nilprather than reporting anything.Tests
new_test.gobuilds throwaway modules with the host C compiler, so it does not need SoftHSM. Those tests skip on Windows, where the modules are not ELF shared libraries, and wherever no C compiler is present.checkNewFailsasserts that bothNewandNewWithErrorreject the module, and that the error names the module and says what went wrong. The two failure-mode tests fail without the fix and pass with it:(captured by taking the
C_GetFunctionListcheck back out, rather than against master, since the tests needNewWithErrorto compile.)Note
The Windows branch is written but not compiled here — no mingw toolchain on this machine — so it wants a look from someone who builds for Windows. It is the smaller half of the change:
NewcapturesGetLastError, and Go says it in words.callocinNewis still unchecked, so a NULL return would crash on the following line. Left alone to keep this focused — happy to add it if you would like.🤖 Generated with Claude Code