File 0001-Handling-multiple-USB-tokens-in-IFD-handler.patch of Package openct
From a2516aa49f4822ddc4fd37ee4ee0d8d3d5336ffa Mon Sep 17 00:00:00 2001
From: Ludovic Rousseau <ludovic.rousseau@gmail.com>
Date: Tue, 1 May 2012 14:19:15 +0000
Subject: [PATCH] Handling multiple USB tokens in IFD handler
" Probably the problem I'm gonna describe is already known: OpenCT's IFD
handler, used by pcscd, does not handle multiple USB tokens correctly.
With one token everything works fine, but if you insert another one, it
leads either to error, or even to pcscd's segmentation fault.
The problem hides in CT_init() and CT_close() functions. The first one
calculates wrong channel number for a new device, and the second causes
memory corruption when deleting an item from a linked list.
I've made a simple patch that corrects these problems and makes IFD
handler work good - see the attachment. Hope it'll be useful. "
Thanks to Alexander Gozman for the patch
http://www.opensc-project.org/pipermail/opensc-devel/2012-April/017981.html
---
src/ctapi/ctapi.c | 34 ++++++++++++++++++++++------------
src/pcsc/pcsc.c | 2 +-
2 files changed, 23 insertions(+), 13 deletions(-)
diff --git a/src/ctapi/ctapi.c b/src/ctapi/ctapi.c
index 771ecc1..76d6a20 100644
--- a/src/ctapi/ctapi.c
+++ b/src/ctapi/ctapi.c
@@ -597,18 +597,28 @@ char CT_init(unsigned short ctn, unsigned short pn)
char CT_close(unsigned short ctn)
{
- struct CardTerminal **ct, *this;
-
- for (ct = &cardTerminals; *ct && (*ct)->ctn != ctn; ct = &(*ct)->next) ;
- this = *ct;
- if (!this)
- return ERR_INVALID;
- ct_reader_disconnect(this->h);
- ct = &(this->next);
-
- this->next = NULL;
- free(this);
- return OK;
+ struct CardTerminal *curr = cardTerminals,
+ *prev = NULL;
+
+ while (curr)
+ {
+ if (curr->ctn == ctn)
+ {
+ if (prev)
+ prev->next = curr->next;
+ else
+ cardTerminals = curr->next;
+
+ free(curr);
+ }
+ else
+ {
+ prev = curr;
+ curr = curr->next;
+ }
+ }
+
+ return OK;
}
char CT_data(unsigned short ctn, unsigned char *dad, unsigned char *sad,
diff --git a/src/pcsc/pcsc.c b/src/pcsc/pcsc.c
index 0ed9a61..d364b42 100644
--- a/src/pcsc/pcsc.c
+++ b/src/pcsc/pcsc.c
@@ -91,7 +91,7 @@ RESPONSECODE IFDHCreateChannel(DWORD Lun, DWORD Channel)
if (Channel > IFDH_MAX_READERS) {
pn = 0;
} else {
- pn = ((Channel == 0) ? 0 : Channel - 1);
+ pn = Channel;
}
ret = CT_init(ctn, pn);
--
2.4.3