Using smartcards in a service - SCardAccessStartedEvent

Dear all,

Migrating our core development platform to Windows Vista, one of our Windows services using smartcards is failing.

We have debugged the problem and found that the event provided by the function ScardAccessStartedEvent does not receive notification. This event is supposed to signal when the SmartCard Resource Manager starts. As we have a WaitForSingleObject(event,INFINITE), the service hungs.

Testing the same service as an ordinary executable ( registering with -regserver ) I can see it works properly. In XP it works properly both as an executable and service.

I have read something about Service Hardening in Vista and I guess it may be caused by privilege reduction. So, I've created some code to enumerate the existing privileges when the application is launched as an executable and as a service. These are the results:

COMSERVER registered with -REGSERVER

-

PrivilegeName[SeIncreaseQuotaPrivilege]=Disabled

PrivilegeName[SeSecurityPrivilege]=Disabled

PrivilegeName[SeTakeOwnershipPrivilege]=Disabled

PrivilegeName[SeLoadDriverPrivilege]=Disabled

PrivilegeName[SeSystemProfilePrivilege]=Disabled

PrivilegeName[SeSystemtimePrivilege]=Disabled

PrivilegeName[SeProfileSingleProcessPrivilege]=Disabled

PrivilegeName[SeIncreaseBasePriorityPrivilege]=Disabled

PrivilegeName[SeCreatePagefilePrivilege]=Disabled

PrivilegeName[SeBackupPrivilege]=Disabled

PrivilegeName[SeRestorePrivilege]=Disabled

PrivilegeName[SeShutdownPrivilege]=Disabled

PrivilegeName[SeDebugPrivilege]=Disabled

PrivilegeName[SeSystemEnvironmentPrivilege]=Disabled

PrivilegeName[SeChangeNotifyPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeRemoteShutdownPrivilege]=Disabled

PrivilegeName[SeUndockPrivilege]=Disabled

PrivilegeName[SeManageVolumePrivilege]=Disabled

PrivilegeName[SeImpersonatePrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeCreateGlobalPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeIncreaseWorkingSetPrivilege]=Disabled

PrivilegeName[SeTimeZonePrivilege]=Disabled

PrivilegeName[SeCreateSymbolicLinkPrivilege]=Disabled

COMSERVER registered with -SERVICE

--

PrivilegeName[SeAssignPrimaryTokenPrivilege]=Disabled

PrivilegeName[SeLockMemoryPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeIncreaseQuotaPrivilege]=Disabled

PrivilegeName[SeTcbPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeSecurityPrivilege]=Disabled

PrivilegeName[SeTakeOwnershipPrivilege]=Disabled

PrivilegeName[SeLoadDriverPrivilege]=Disabled

PrivilegeName[SeSystemProfilePrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeSystemtimePrivilege]=Disabled

PrivilegeName[SeProfileSingleProcessPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeIncreaseBasePriorityPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeCreatePagefilePrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeCreatePermanentPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeBackupPrivilege]=Disabled

PrivilegeName[SeRestorePrivilege]=Disabled

PrivilegeName[SeShutdownPrivilege]=Disabled

PrivilegeName[SeDebugPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeAuditPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeSystemEnvironmentPrivilege]=Disabled

PrivilegeName[SeChangeNotifyPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeUndockPrivilege]=Disabled

PrivilegeName[SeManageVolumePrivilege]=Disabled

PrivilegeName[SeImpersonatePrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeCreateGlobalPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeIncreaseWorkingSetPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeTimeZonePrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeCreateSymbolicLinkPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

Do you have any idea about this strange behaviour of ScardAccessStartedEvent function?

Thanks in advance

[4672 byte] By [SergioOrtiz] at [2008-2-13]
# 1

Dear all,

Migrating our core development platform to Windows Vista, one of our Windows services using smartcards is failing.

We have debugged the problem and found that the event provided by the function ScardAccessStartedEvent does not receive notification. This event is supposed to signal when the SmartCard Resource Manager starts. As we have a WaitForSingleObject(event,INFINITE), the service hungs.

Testing the same service as an ordinary executable ( registering with -regserver ) I can see it works properly. In XP it works properly both as an executable and service.

I have read something about Service Hardening in Vista and I guess it may be caused by privilege reduction. So, I've created some code to enumerate the existing privileges when the application is launched as an executable and as a service. These are the results:

COMSERVER registered with -REGSERVER

-

PrivilegeName[SeIncreaseQuotaPrivilege]=Disabled

PrivilegeName[SeSecurityPrivilege]=Disabled

PrivilegeName[SeTakeOwnershipPrivilege]=Disabled

PrivilegeName[SeLoadDriverPrivilege]=Disabled

PrivilegeName[SeSystemProfilePrivilege]=Disabled

PrivilegeName[SeSystemtimePrivilege]=Disabled

PrivilegeName[SeProfileSingleProcessPrivilege]=Disabled

PrivilegeName[SeIncreaseBasePriorityPrivilege]=Disabled

PrivilegeName[SeCreatePagefilePrivilege]=Disabled

PrivilegeName[SeBackupPrivilege]=Disabled

PrivilegeName[SeRestorePrivilege]=Disabled

PrivilegeName[SeShutdownPrivilege]=Disabled

PrivilegeName[SeDebugPrivilege]=Disabled

PrivilegeName[SeSystemEnvironmentPrivilege]=Disabled

PrivilegeName[SeChangeNotifyPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeRemoteShutdownPrivilege]=Disabled

PrivilegeName[SeUndockPrivilege]=Disabled

PrivilegeName[SeManageVolumePrivilege]=Disabled

PrivilegeName[SeImpersonatePrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeCreateGlobalPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeIncreaseWorkingSetPrivilege]=Disabled

PrivilegeName[SeTimeZonePrivilege]=Disabled

PrivilegeName[SeCreateSymbolicLinkPrivilege]=Disabled

COMSERVER registered with -SERVICE

--

PrivilegeName[SeAssignPrimaryTokenPrivilege]=Disabled

PrivilegeName[SeLockMemoryPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeIncreaseQuotaPrivilege]=Disabled

PrivilegeName[SeTcbPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeSecurityPrivilege]=Disabled

PrivilegeName[SeTakeOwnershipPrivilege]=Disabled

PrivilegeName[SeLoadDriverPrivilege]=Disabled

PrivilegeName[SeSystemProfilePrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeSystemtimePrivilege]=Disabled

PrivilegeName[SeProfileSingleProcessPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeIncreaseBasePriorityPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeCreatePagefilePrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeCreatePermanentPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeBackupPrivilege]=Disabled

PrivilegeName[SeRestorePrivilege]=Disabled

PrivilegeName[SeShutdownPrivilege]=Disabled

PrivilegeName[SeDebugPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeAuditPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeSystemEnvironmentPrivilege]=Disabled

PrivilegeName[SeChangeNotifyPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeUndockPrivilege]=Disabled

PrivilegeName[SeManageVolumePrivilege]=Disabled

PrivilegeName[SeImpersonatePrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeCreateGlobalPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeIncreaseWorkingSetPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeTimeZonePrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

PrivilegeName[SeCreateSymbolicLinkPrivilege]=SE_PRIVILEGE_ENABLED+SE_PRIVILEGE_ENABLED_BY_DEFAULT

Do you have any idea about this strange behaviour of ScardAccessStartedEvent function?

Thanks in advance

SergioOrtiz at 2007-9-3 > top of Msdn Tech,Software Development for Windows Vista,Application Compatibility for Windows Vista...
# 2

Hi,

I've been working on the issue and I almost discard anything to do with Privileges.

Using SysInternals Process Explorer to explore event handles received by our application in return for ScardAccessStartedEvent in both XP and Vista, I've noticed relevant differences.

In XP case, our application receives a '\BaseNamedObjects\Microsoft Smart Card Resource Manager Started' while in Vista, we receive a '\BaseNamedObjects\TermSrvReadyEvent'. Therefore, it is normal that our application gets hunged while waiting for the event as it is a DIFFERENT event!

I've also noticed that, in Vista, Windows smartcard component is hosted ( loaded in svchost.exe ) instead of the being deployed in scardsvr.exe.

Does anyone know what's happening here? I can do a workaround waiting for the new event by its name to solve the problem but I would expect a "documented solution" for the issue.

Thanks in advance

SergioOrtiz at 2007-9-3 > top of Msdn Tech,Software Development for Windows Vista,Application Compatibility for Windows Vista...
# 3

Hello Sergio,

Just to let you know we are looking into this, I will follow up soon with more information.

Thanks,
Louis Shanks

Microsoft AppCompat

LouSha at 2007-9-3 > top of Msdn Tech,Software Development for Windows Vista,Application Compatibility for Windows Vista...
# 4

Hi Louis,


Thanks for the info. Don't know whether it helps, but we have also noticed that PC/SC SCardEstablishContext fails with SCARD_E_NO_SERVICE when it is used from a Windows Vista service. We had similar problems in the past with Fast User Switching under XP and there was some activity on internet forums about the subject.

As in the case of SCardAccessStartedEvent, it works perfectly when the component is launched from the user context.

I would say that Session 0 isolation has something to do with it...but ignore PC/SC implementation internals.

Thanks in advance

SergioOrtiz at 2007-9-3 > top of Msdn Tech,Software Development for Windows Vista,Application Compatibility for Windows Vista...
# 5

Sergio,

In order to ‘talk to’ the smart card resource manger from a service you must hold the Service SID or the Local System SID or another option is to impersonate a user.

The fact you see this working under the user context would indicate you could get this working as a service by impersonating a user if this is a method you would like to use.

Thanks,
Louis Shanks

Microsoft AppCompat

LouSha at 2007-9-3 > top of Msdn Tech,Software Development for Windows Vista,Application Compatibility for Windows Vista...
# 6

Good afternoon - Bill Wesse from Microsoft Critical Problem Resolution (CPR) here; I have recently joined the forum.

On Vista, services no longer have direct access to a user desktop. That is, I expect the service may need to be logged on with an account with the following right ():

SE_INTERACTIVE_LOGON_NAME (SeInteractiveLogonRight).

Please let me know if acquiring the right takes care of the problem - if it does not, I will be glad to delve to whatever level is needed.

Regards,

Bill Wesse

BillWesse at 2007-9-3 > top of Msdn Tech,Software Development for Windows Vista,Application Compatibility for Windows Vista...
# 7

Dear Bill,

Thanks for you information. Unfortunately, I'll be out of office for two weeks. People in the office is testing different approaches. I'll report on them when I come back.

Best regards,

SergioOrtiz at 2007-9-3 > top of Msdn Tech,Software Development for Windows Vista,Application Compatibility for Windows Vista...
# 8

Dear Louis,

Thanks for you information. Unfortunately, I'll be out of office for two weeks. People in the office is testing different approaches. I'll report on them when I come back.

Best regards,

SergioOrtiz at 2007-9-3 > top of Msdn Tech,Software Development for Windows Vista,Application Compatibility for Windows Vista...
# 9

I have a service that runs under LocalSystem, depends on SCardSvr and
tries to establish a context using:

LONG rv = SCardEstablishContext( SCARD_SCOPE_SYSTEM, NULL, NULL,
&m_handle );

The call returns with SCARD_E_NO_SERVICE...
Am I overlooking something obvious ?
(I do a LoadLibrary on winscard.dll and it loads successfully. It has returned a valid address to all the scard* calls.)

Additional Info:

I start the Service manually after SCardSvr is already running. The
above code works if I call it from a normal EXE that runs under the
current user account. I also tried to establish the context under
SCARD_SCOPE_USER, with the same result. And I also tried to install the
service under LocalService (like SCardSvr), with again the same result.

Any ideas?

Thanks,

Deepak Chawla.

dchawla at 2007-9-3 > top of Msdn Tech,Software Development for Windows Vista,Application Compatibility for Windows Vista...
# 10

Thanks for the heads up; I'll watch this thread for your next update!

Regards,

Bill Wesse

BillWesse at 2007-9-3 > top of Msdn Tech,Software Development for Windows Vista,Application Compatibility for Windows Vista...
# 11

Hi,

A solution was proposed in the security forum. It seems there was a bug in Beta 2 which prevented access to the smartcard resource managers unless impersonating a user. This was the solution we intuitively tested - impersonating Terminal Services User. RC1 is supposed to solve this issue - no need to impersonate. However, to take full advantage of TS redirection a user must be impersonated - if not, you have only access to local resource manager.

Hope this helps

SergioOrtiz at 2007-9-3 > top of Msdn Tech,Software Development for Windows Vista,Application Compatibility for Windows Vista...
# 12
Hello,

I've the same problem. There is a smart card handler COM object based on Smart Card API. I want to

use this COM object from a service. It works in XP, but it fails in

Vista (6.0.6000) as service. SCardEstablishContext returns

SCARD_E_NO_SERVICE. I've tried it to call with SCARD_SCOPE_USER and

SCARD_SCOPE_SYSTEM, both failed.

It _works_ when I use it from an application, and it fails called from service.

Any thoughts?

Thanks,

Zoltan Schavel

ZoltanSchavel at 2007-9-3 > top of Msdn Tech,Software Development for Windows Vista,Application Compatibility for Windows Vista...

Software Development for Windows Vista

Site Classified