pkcs11-logger icon indicating copy to clipboard operation
pkcs11-logger copied to clipboard

Received SIGSEGV

Open MrWildanMD opened this issue 9 months ago • 2 comments

Hi im trying to using this logger library alongside with golang and https://github.com/miekg/pkcs11 that load this module, and i have set the original .so of the HSM but i get:

[danzz@poc testlogger]$ go run main.go
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4ccaee]

goroutine 1 [running]:
github.com/miekg/pkcs11.(*Ctx).Initialize.func1(...)
        /home/danzz/go/pkg/mod/github.com/miekg/[email protected]/pkcs11.go:808
github.com/miekg/pkcs11.(*Ctx).Initialize(0x50755a?)
        /home/danzz/go/pkg/mod/github.com/miekg/[email protected]/pkcs11.go:808 +0xe
main.main()
        /home/danzz/testlogger/main.go:20 +0x98
exit status 2

but when using the original pkcs11 lib it throws no error:

[danzz@poc testlogger]$ go run main.go
Available slots: [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39]
PKCS#11 session opened successfully!
Successfully logged in to slot 0

sample code used:

package main

import (
	"fmt"
	"log"

	"github.com/joho/godotenv"
	"github.com/miekg/pkcs11"
)

func main() {
	err := godotenv.Load()
	if err != nil {
		log.Fatal("Error loading .env file")
	}
	modulePath := "/home/danzz/testlogger/pkcs11-logger-x64.so" // Logger
	// modulePath := "/home/danzz/libcs_pkcs11_R2.so" // Original

	p := pkcs11.New(modulePath)
	err = p.Initialize()
	if err != nil {
		log.Fatalf("Failed to initialize PKCS#11 module: %v", err)
	}
	defer p.Destroy()
	defer p.Finalize()

	// Get a list of available slots
	slots, err := p.GetSlotList(true)
	if err != nil {
		log.Fatalf("Failed to get slot list: %v", err)
	}

	fmt.Printf("Available slots: %v\n", slots)

	if len(slots) == 0 {
		log.Fatal("No available slots found.")
	}

	session, err := p.OpenSession(slots[0], pkcs11.CKF_SERIAL_SESSION|pkcs11.CKF_RW_SESSION)
	if err != nil {
		log.Fatalf("Failed to open session: %v", err)
	}
	defer p.CloseSession(session)

	fmt.Println("PKCS#11 session opened successfully!")

	err = p.Login(session, pkcs11.CKU_USER, "hsmpin")
	if err != nil {
		panic(err)
	}
	defer p.Logout(session)

	fmt.Println("Successfully logged in to slot 0")
}

HSM Used: Utimaco Original PKCS#11 lib: libcs_pkcs11_R2.so

or did this only work with C# only?

MrWildanMD avatar Feb 25 '25 07:02 MrWildanMD

PKCS11-LOGGER is not bound to C# in any way. It should work with apps written in any language. Could you please analyze where exactly does SIGSEGV occur? I've written a guide few years ago: https://jimrich.sk/basic-analysis-of-segmentation-fault-on-linux-platforms/

jariq avatar Feb 28 '25 13:02 jariq

@jariq here is the result, im running this on centos 7 btw:

[danzz@poc testlogger]$ gdb logging core.25422 
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/danzz/testlogger/logging...Dwarf Error: wrong version in compilation unit header (is 0, should be 2, 3, or 4) [in module /home/danzz/testlogger/logging]
(no debugging symbols found)...done.
[New LWP 25422]
[New LWP 25425]
[New LWP 25424]
[New LWP 25423]
[New LWP 25426]
[New LWP 25427]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Core was generated by `./logging'.
Program terminated with signal 6, Aborted.
#0  0x0000000000475b81 in runtime.raise.abi0 ()
warning: File "/usr/local/go/src/runtime/runtime-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load:/usr/bin/mono-gdb.py".
To enable execution of this file add
        add-auto-load-safe-path /usr/local/go/src/runtime/runtime-gdb.py
line to your configuration file "/home/danzz/.gdbinit".
To completely disable this security protection add
        set auto-load safe-path /
line to your configuration file "/home/danzz/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
        info "(gdb)Auto-loading safe path"
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.3.x86_64
(gdb) where
warning: skipping .debug_frame info of /home/danzz/testlogger/logging: Found an FDE when not expecting it.
#0  0x0000000000475b81 in runtime.raise.abi0 ()
#1  0x0000000000452da5 in runtime.dieFromSignal ()
#2  0x0000000000453306 in runtime.sigfwdgo ()
#3  0x0000000000451c65 in runtime.sigtrampgo ()
#4  0x0000000000475e66 in runtime.sigtramp.abi0 ()
#5  0x0000000000000006 in ?? ()
#6  0x000000c00000d4b0 in ?? ()
#7  0x000000c00000d380 in ?? ()
#8  0x0000000000000000 in ?? ()
(gdb) 

MrWildanMD avatar Mar 05 '25 06:03 MrWildanMD