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

OSX - Sometimes C.hid_read returns all zeros #6

Open
chillyvee opened this issue Apr 21, 2022 · 0 comments
Open

OSX - Sometimes C.hid_read returns all zeros #6

chillyvee opened this issue Apr 21, 2022 · 0 comments

Comments

@chillyvee
Copy link

chillyvee commented Apr 21, 2022

On a Mac M1(and x64), especially with Ledger Nano X:

The function call at line 216: https://github.com/Zondax/hid/blob/master/hid_enabled.go#L216

// Execute the read operation
	read := int(C.hid_read(device, (*C.uchar)(&b[0]), C.size_t(len(b))))

Sometimes returns read == 64, and a zero filled buffer
C.hid_read [64] => [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

This is most commonly seen during a sign request : INS_GET_ADDR_SECP256K1

Where:

  • the Command is fully sent, a success 0x9000 is received
  • the first return packet is 64 bytes of 0x00
  • the second return packet is the tail of the signature with sequenceIdx == 1 (causing the front part of the signature to be missing)

This error occurs "randomly" from time to time.

We can also share that at this point of the program:
https://github.com/Zondax/hid/blob/master/hidapi/mac/hid.c#L802

/* Helper function, so that this isn't duplicated in hid_read(). */
static int return_data(hid_device *dev, unsigned char *data, size_t length)
{
	/* Copy the data out of the linked list item (rpt) into the
	   return buffer (data), and delete the liked list item. */
	struct input_report *rpt = dev->input_reports;
	size_t len = (length < rpt->len)? length: rpt->len;
	memcpy(data, rpt->data, len);  <------- HERE rpt->data = [0,0,0,0,0,0,.....0,0]

The contents of rpt->data are all 0x00

Checking the event source:
hid_report_callback
https://github.com/Zondax/hid/blob/master/hidapi/mac/hid.c#L566

/* The Run Loop calls this function for each input report received.
   This function puts the data into a linked list to be picked up by
   hid_read(). */
static void hid_report_callback(void *context, IOReturn result, void *sender,
                         IOHIDReportType report_type, uint32_t report_id,
                         uint8_t *report, CFIndex report_length)
{
	struct input_report *rpt;
	hid_device *dev = context;

	/* Make a new Input Report object */
	rpt = calloc(1, sizeof(struct input_report));
	rpt->data = calloc(1, report_length);
	memcpy(rpt->data, report, report_length); <-----

The contents of rpt->data are all 0x00 at the source as well

Is it an OSX Bug? Why only on signature returns? Why do all other APDUs work without issue?

  • Affects Big Sur (OS X 11) and Monterey (OS X 12)

Maybe it's a ledger-cosmos bug? But why does this APDU only fail on CLI and not Keplr (Browser extension)?

Maybe it's a NanoX+Cosmos Bug+CLI Bug? NanoSPlus doesn't experience this problem very often. Faster processor?

NanoX ledger-cosmos v2.34.3
NanoSPlus ledger-cosmos v2.34.3

This affects cosmos-sdk cli applications as documented here:
cosmos/ledger-cosmos#57

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

No branches or pull requests

1 participant