Skip to content

BUGFIX: Fail New when C_GetFunctionList does not return a function list - #193

Open
inteon wants to merge 2 commits into
miekg:masterfrom
inteon:fix-new-unchecked-getfunctionlist
Open

inteon wants to merge 2 commits into
miekg:masterfrom
inteon:fix-new-unchecked-getfunctionlist

Conversation

@inteon

@inteon inteon commented Sep 22, 2026

Copy link
Copy Markdown

Problem

New ignores the return value of the module's C_GetFunctionList and never checks the pointer it receives:

list = (CK_C_GetFunctionList) dlsym(c->handle, "C_GetFunctionList");
if (list == NULL) {
	free(c);
	return NULL;
}
list(&c->sym);   // return value ignored, c->sym may still be NULL
return c;

A module whose C_GetFunctionList fails — returns non-CKR_OK without writing *ppFunctionList — therefore leaves sym NULL while New still returns a non-nil *Ctx. The first call into the module dereferences that NULL function list and the process dies:

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(...)

addr=0x8 is the offset of C_Initialize in CK_FUNCTION_LIST, after the padded two-byte CK_VERSION — that is c->sym->C_Initialize with c->sym == NULL.

Callers cannot defend against this: the New(...) == nil check they are told to do passes, and sym is not reachable from Go. We hit it in production with a vendor HSM client library that fails in C_GetFunctionList when it cannot read its own configuration. The process segfaulted instead of the caller getting an error.

Fix

Check that C_GetFunctionList returns CKR_OK and hands out a non-NULL function list, and return NULL otherwise — the nil *Ctx that New already documents for a module it cannot use. Applied to both the Windows and the dlfcn branch.

Also releases the module on the C_GetFunctionList lookup failure path, which previously freed the ctx but left the library loaded.

Reporting why

New returning a bare nil says nothing about what went wrong, and dlerror's message was thrown away. The C New now reports why it failed. On the dlfcn side it fills a char **err out-parameter from dlerror, or from the CK_RV when C_GetFunctionList is the one that failed; the Go side owns and frees that message. The Windows loader reports a code rather than a message, so there New only captures GetLastError into a unsigned long *code out-parameter and Go renders it with syscall.Errno, which already calls FormatMessage and trims the trailing newline.

New(module string) *Ctx keeps its signature and behaviour, so this is backwards compatible. The new NewWithError(module string) (*Ctx, error) hands out the reason:

p, err := pkcs11.NewWithError("/path/to/module.so")
if err != nil {
	return err
}

The errors now read:

pkcs11: /path/to/module.so: cannot open shared object file: No such file or directory
pkcs11: /path/to/module.so: undefined symbol: C_GetFunctionList
pkcs11: /path/to/module.so: C_GetFunctionList failed: 0x5
pkcs11: /path/to/module.so: C_GetFunctionList returned no function list

p11.OpenModule already returns an error, so it now passes this one on instead of its own failed to load module %q. The README skeleton uses NewWithError too, since as written it would have panicked on a nil p rather than reporting anything.

Tests

new_test.go builds 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.

checkNewFails asserts that both New and NewWithError reject 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:

--- FAIL: TestNewGetFunctionListFails (0.07s)
    new_test.go:86: New succeeded for a module it cannot use
    new_test.go:86: NewWithError succeeded for a module it cannot use
--- FAIL: TestNewGetFunctionListReturnsNil (0.09s)
    new_test.go:100: New succeeded for a module it cannot use
    new_test.go:100: NewWithError succeeded for a module it cannot use

(captured by taking the C_GetFunctionList check back out, rather than against master, since the tests need NewWithError to 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: New captures GetLastError, and Go says it in words.

calloc in New is 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

inteon and others added 2 commits September 22, 2026 11:59
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>
@inteon

inteon commented Oct 5, 2026

Copy link
Copy Markdown
Author

@miekg do you have time to leave a review?

@ncabatoff

ncabatoff commented Nov 6, 2026 •

Copy link
Copy Markdown

I believe I'm seeing the same issue with HashiCorp Vault while talking to an Entrust HSM:

SIGSEGV: segmentation violation
PC=0xbbdf3ad m=3 sigcode=1 addr=0x8
signal arrived during cgo execution
goroutine 1540 gp=0x26806e0d4780 m=3 mp=0x26806c43d008 [syscall]:
runtime.cgocall(0xbbe11c0, 0x26806ffe0ca0)
#011/opt/go/src/runtime/cgocall.go:167 +0x4b fp=0x26806ffe0c78 sp=0x26806ffe0c40 pc=0x493f6b
github.com/miekg/pkcs11._Cfunc_Initialize(0x7efcc0000ef0)
#011_cgo_gotypes.go:1227 +0x45 fp=0x26806ffe0ca0 sp=0x26806ffe0c78 pc=0x19f99a5
github.com/miekg/pkcs11.(*Ctx).Initialize.func1(...)
#011/go-mod-cache/github.com/miekg/pkcs11@v1.1.1/pkcs11.go:808
github.com/miekg/pkcs11.(*Ctx).Initialize(0x26806d793c21?)

It seems that the immediate cause was a filesystem permission issue: once the vault user was able to read /opt/nfast/cknfastrc the crashes went away. I presume the absence of the config from that file resulted in C_GetFunctionList returning a null list, though I wasn't able to confirm that theory.

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.

2 participants