Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation Fault #48

Open
kurenai-ryu opened this issue May 30, 2019 · 9 comments
Open

Segmentation Fault #48

kurenai-ryu opened this issue May 30, 2019 · 9 comments
Labels

Comments

@kurenai-ryu
Copy link

pkcs11js version: "1.0.17",

I have been testing node-webcrypto-p11 and graphene with a feitian epass2003 token on ubuntu18, and everything seems to work, I can login(auth) and sign some data..

now I'm trying to make it work on a raspberry (4.14.98-v7+ ARM7l debian/raspbian), most tools, works ok (I can login and list the private cert with pkcs11-tool --module /usr/lib/arm-linux-gnueabihf/opensc-pkcs11.so --login --list-objects)

but it gives me error on Node. I isolated up to pkcs11js if I try a pkcs11.C_Login it exists with a "Segmentation Fault" message and nothing more...

is there someway to get more information or debug logs? pcscd doesn't show anything unnatural BTW

@microshine
Copy link
Contributor

Can you try the latest version of the [email protected]? This version uses the latest version of the nan and it can fix the error

@microshine
Copy link
Contributor

microshine commented May 30, 2019

You can update C++ code to gathering segmentation fault stack trace

  • Update main.cpp
  • Rebuild pkcs11js
npm rebuild

or

cd node_modules/pkcs11js
node-gyp counfigure build

main.cpp

#include <nan.h>
#include <node.h>

#include <execinfo.h>
#include <signal.h>
#include <unistd.h>

#include "const.h"
#include "node.h"

void handler(int sig)
{
	void *array[10];
	size_t size;

	// get void*'s for all entries on the stack
	size = backtrace(array, 10);

	// print out all the frames to stderr
	fprintf(stderr, "Error: signal %d:\n", sig);
	backtrace_symbols_fd(array, size, STDERR_FILENO);
	exit(1);
}

NAN_MODULE_INIT(init)
{
	signal(SIGSEGV, handler); // install our handler

	Nan::HandleScope scope;

	WPKCS11::Init(target);

	declare_objects(target);
	declare_attributes(target);
	declare_ket_types(target);
	declare_mechanisms(target);
	declare_flags(target);
	declare_certificates(target);
	declare_mgf(target);
	declare_kdf(target);
	declare_params(target);
	declare_initialize_flags(target);
	declare_user_types(target);
}

NODE_MODULE(pkcs11, init)

Output example

Error: signal 11:
0   pkcs11.node                         0x00000001069e0bc1 _Z7handleri + 33
1   libsystem_platform.dylib            0x00007fff7393db5d _sigtramp + 29
2   node                                0x00000001001e2975 _ZN2v88internal6String9VisitFlatINS_17Utf8WriterVisitorEEEPNS0_10ConsStringEPT_PS1_i + 165
3   pkcs11.node                         0x0000000106a14644 _ZN7WPKCS117C_LoginERKN3Nan20FunctionCallbackInfoIN2v85ValueEEE + 1354
4   pkcs11.node                         0x0000000106a3117c _ZN3Nan3impL23FunctionCallbackWrapperERKN2v820FunctionCallbackInfoINS1_5ValueEEE + 170
5   node                                0x000000010023663f _ZN2v88internal25FunctionCallbackArguments4CallEPNS0_15CallHandlerInfoE + 623
6   node                                0x0000000100235b81 _ZN2v88internal12_GLOBAL__N_119HandleApiCallHelperILb0EEENS0_11MaybeHandleINS0_6ObjectEEEPNS0_7IsolateENS0_6HandleINS0_10HeapObjectEEESA_NS8_INS0_20FunctionTemplateInfoEEENS8_IS4_EENS0_16BuiltinArgumentsE + 689
7   node                                0x0000000100235220 _ZN2v88internalL26Builtin_Impl_HandleApiCallENS0_16BuiltinArgumentsEPNS0_7IsolateE + 288
8   ???                                 0x00001c4b44fdbe3d 0x0 + 31109605604925
9   ???                                 0x00001c4b44f918d5 0x0 + 31109605300437

@kurenai-ryu
Copy link
Author

thanks for your response, sadly updating to 1.0.18 didn't help,
also after adding the signal handler, it gave an obscure error:

> var pkcs11js = require("pkcs11js");
undefined
> var pkcs11 = new pkcs11js.PKCS11();
undefined
> pkcs11.load("/usr/lib/arm-linux-gnueabihf/opensc-pkcs11.so")
undefined
> pkcs11.C_Initialize()
undefined
> pkcs11.C_GetInfo()
{ cryptokiVersion: { major: 2, minor: 20 },
  manufacturerID: 'OpenSC Project                  ',
  flags: 0,
  libraryDescription: 'OpenSC smartcard framework      ',
  libraryVersion: { major: 0, minor: 16 } }
> slots = pkcs11.C_GetSlotList(true)
[ <Buffer 00 00 00 00> ]
> slot = slots[0]
<Buffer 00 00 00 00>
> pkcs11.C_GetSlotInfo(slot)
{ slotDescription:
   'Feitian ePass2003 00 00                                         ',
  manufacturerID: 'FS                              ',
  flags: 7,
  hardwareVersion: { major: 1, minor: 16 },
  firmwareVersion: { major: 0, minor: 0 } }
> pkcs11.C_GetTokenInfo(slot)
{ label: '*my label* (User PIN)           ',
  manufacturerID: 'EnterSafe                       ',
  model: 'PKCS#15         ',
  serialNumber: '*the serial number*',
  flags: 1037,
  maxSessionCount: 0,
  sessionCount: 0,
  maxRwSessionCount: 0,
  rwSessionCount: 0,
  maxPinLen: 16,
  minPinLen: 4,
  hardwareVersion: { major: 0, minor: 0 },
  firmwareVersion: { major: 0, minor: 0 },
  utcTime:
   '\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000',
  totalPublicMemory: 4294967295,
  freePublicMemory: 4294967295,
  totalPrivateMemory: 4294967295,
  freePrivateMemory: 4294967295 }
> session = pkcs11.C_OpenSession(slot, pkcs11js.CKF_RW_SESSION | pkcs11js.CKF_SERIAL_SESSION)
<Buffer f0 94 d4 02>
> info = pkcs11.C_GetSessionInfo(session)
{ slotID: <Buffer 00 00 00 00>,
  state: 2,
  flags: 6,
  deviceError: 0 }
> pkcs11.C_Login(session, 1, "mypass")
*** Error in `node': malloc(): memory corruption: 0x02d493e8 ***

and now it hangs! I can't exit node! any idea?

@microshine
Copy link
Contributor

microshine commented May 30, 2019

As I can see you are using Cryptoki v2.20
I'm checking it's API

pkcs11js is based on Cryptoki v2.30

@microshine
Copy link
Contributor

Can you add logs like puts("Log message"); to node.cpp:609-626 and pkcs11.cpp:365-376 files?

@kurenai-ryu
Copy link
Author

kurenai-ryu commented Jun 4, 2019

after some debug messages, if hangs on C_Login

void PKCS11::C_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType, Scoped<string> pin) {
	try {
		puts("bfore pkcs11 login");
		CHECK_PKCS11_RV(functionList->C_Login(
			hSession,
			userType,
			pin->length() ? (CK_UTF8CHAR_PTR)pin->c_str() : NULL_PTR, (CK_ULONG)pin->length()
		));
		puts("after pkcs11 login (never shown...)");
	}
	CATCH_ERROR;
}

where is this functionList and how can I further debug? (how can I update the Cryptoki or it's inherent of my device?)

@microshine
Copy link
Contributor

The functionLsit is CK_FUNCTION_LIST structure from PKCS#11.

functionList->C_Login - calls C_Login function from PKCS#11 library. You can print the incomming parameters to check that they are right

@kurenai-ryu
Copy link
Author

it seems hSession is the content of session buffer, usertype is 1 as in the example, pin length and pin c_str is the provided pin

void PKCS11::C_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType, Scoped<string> pin) {
	try {
		puts("bfore pkcs11 login");
		printf("session %lx\n", hSession);
		printf("utype %lx\n", userType);
		printf("pin lenght %u\n", pin->length());
		printf("pin str %s\n", pin->c_str());
		CHECK_PKCS11_RV(functionList->C_Login(
			hSession,
			userType,
			pin->length() ? (CK_UTF8CHAR_PTR)pin->c_str() : NULL_PTR, (CK_ULONG)pin->length()
		));
		puts("after pkcs11 login (never shown)");
	}
	CATCH_ERROR;
}

@kurenai-ryu
Copy link
Author

something I noted is that the session buffer is 4 bytes in the raspberry and 8 bytes on a PC

@microshine microshine added the bug label Nov 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants