From ludovic.rousseau at gmail.com Wed Dec 2 07:28:14 2020 From: ludovic.rousseau at gmail.com (Ludovic Rousseau) Date: Wed, 2 Dec 2020 13:28:14 +0100 Subject: [Pcsclite-muscle] Crash in SCardStatus In-Reply-To: References: Message-ID: Le mar. 24 nov. 2020 ? 13:28, Jonathan Verner a ?crit : > Hello, Hello Jonathan. Sorry for the delay. Your email was in my gmail spam folder. > I work for Nexus where we develop smartcard middleware and we use libpcsc-lite. Recently, we have started seeing crashes on Linux (Ubuntu 18.04, which has libpcsc-lite version 1.8.23) when calling SCardStatus from our code. I suspect that the problem has to do with multiple threads calling into libpcsc-lite. > > In particular, the crash seems to happen when SCardStatus is called and, before it finishes, SCardDisconnect is called: > > The crash happens in SCardStatus on line 1440 of winscard_clnt.c due to r (=pChannelMap->readerName) > no longer pointing to valid memory. My hypothesis is that, while calling getReaderStates on line 1434, a call to > SCardDisconnect arrives and frees the memory pointed to by pChannelMap->readerName. Are you able to reproduce the problem with pcsc-lite version 1.9.0? I ask because I fixed a race condition in SCardGetStatusChange() in version 1.8.24 https://ludovicrousseau.blogspot.com/2018/10/new-version-of-pcsc-lite-1824.html > On the other hand, I am quite new to pcsc and perhaps my analysis is incorrect (I am attaching the logs I get from pcscd and pcsc-spy if someone would be willing to look at them). Anyway, I wanted to know, if there is some documentation on how to safely call libpcsc-lite functions from multiple threads. For multiple threads be sure to read SCardEstablishContext() documentation https://pcsclite.apdu.fr/api/group__API.html#gaa1b8970169fd4883a6dc4a8f43f19b67 Bye -- Dr. Ludovic Rousseau From jonathan.verner at nexusgroup.com Thu Dec 3 10:32:52 2020 From: jonathan.verner at nexusgroup.com (Jonathan Verner) Date: Thu, 3 Dec 2020 15:32:52 +0000 Subject: [Pcsclite-muscle] Crash in SCardStatus In-Reply-To: References: , Message-ID: Hi Ludovic, (sorry for the second mail, the first one didn't go to the list, I am still getting used to this outlook webmail thingy) > Sorry for the delay. Your email was in my gmail spam folder. thanks for the response and no worries. Spam filters are too good these days :-) > Are you able to reproduce the problem with pcsc-lite version 1.9.0? > I ask because I fixed a race condition in SCardGetStatusChange() in > version 1.8.24 I think I saw that change and tried with 1.9.0 already but I had some trouble with protocol mismatch between the app and pscd (although our software links libpscs-lite dynamically, so I don't see why) and gave up. I will try again and get back to the list with the results. > For multiple threads be sure to read SCardEstablishContext() documentation So, in principle, having a different scardcontext for each thread should be enough to keep the application thread safe? (In principle, because of course one can't rule out bugs :-)) Thanks again for your response! Best, Jonathan Jonathan Verner, PhD. software engineer Nexus Group Evropska 33/A 160 00 Praha 6 Czech Republic Od: pcsclite-muscle za u?ivatele Ludovic Rousseau Odesl?no: st?eda 2. prosince 2020 13:28 Komu: pcsclite-muscle at lists.infradead.org P?edm?t: Re: [Pcsclite-muscle] Crash in SCardStatus ? Warning: This message was sent from outside of Nexus. Do not click on links or open attachments unless you are sure the content is safe. Le mar. 24 nov. 2020 ? 13:28, Jonathan Verner a ?crit : > Hello, Hello Jonathan. Sorry for the delay. Your email was in my gmail spam folder. > I work for Nexus where we develop smartcard middleware and we use? libpcsc-lite. Recently, we have started seeing crashes on Linux (Ubuntu 18.04, which has libpcsc-lite version 1.8.23) when calling SCardStatus from our code. I suspect that the problem has to do with multiple threads calling into libpcsc-lite. > > In particular, the crash seems to happen when SCardStatus is called and, before it finishes, SCardDisconnect is called: > >??? The crash happens in SCardStatus on line 1440 of winscard_clnt.c due to r (=pChannelMap->readerName) >??? no longer pointing to valid memory. My hypothesis is that, while calling getReaderStates on line 1434, a call to >??? SCardDisconnect arrives and frees the memory pointed to by pChannelMap->readerName. Are you able to reproduce the problem with pcsc-lite version 1.9.0? I ask because I fixed a race condition in SCardGetStatusChange() in version 1.8.24 https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fludovicrousseau.blogspot.com%2F2018%2F10%2Fnew-version-of-pcsc-lite-1824.html&data=04%7C01%7Cjonathan.verner%40nexusgroup.com%7C4f3a2d7a08b449f995bb08d896bddc50%7C89f9cd6ffab54f61a85eb1b24768f7f6%7C1%7C0%7C637425089490812063%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=19QPMlV4Ub%2FlORyNZI8NaETEfNN1M%2F%2BSYhpb1QPUZY4%3D&reserved=0 > On the other hand, I am quite new to pcsc and perhaps my analysis is incorrect (I am attaching the logs I get from pcscd and pcsc-spy if someone would be willing to look at them).? Anyway, I wanted to know, if there is some documentation on how to safely call libpcsc-lite functions from multiple threads. For multiple threads be sure to read SCardEstablishContext() documentation https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpcsclite.apdu.fr%2Fapi%2Fgroup__API.html%23gaa1b8970169fd4883a6dc4a8f43f19b67&data=04%7C01%7Cjonathan.verner%40nexusgroup.com%7C4f3a2d7a08b449f995bb08d896bddc50%7C89f9cd6ffab54f61a85eb1b24768f7f6%7C1%7C0%7C637425089490812063%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=CfVh0AteqxQWdj8LgLQ1Z3czShBPzTlTzfAQaShGDg8%3D&reserved=0 Bye -- ?Dr. Ludovic Rousseau _______________________________________________ pcsclite-muscle mailing list pcsclite-muscle at lists.infradead.org https://eur01.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.infradead.org%2Fmailman%2Flistinfo%2Fpcsclite-muscle&data=04%7C01%7Cjonathan.verner%40nexusgroup.com%7C4f3a2d7a08b449f995bb08d896bddc50%7C89f9cd6ffab54f61a85eb1b24768f7f6%7C1%7C0%7C637425089490812063%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=sMxsdwJJ%2FjiC0ziGsOkOAiRMmJoGuR2nNi7Da%2FENykQ%3D&reserved=0