Make named pipe on W7 generally accessible

Hi all,

I’ve written code that opens a named pipe within a C++ application so that other programs can pass data packets in. This generally works fine, particularly if the application is running on XP or earlier, but when the application is running on Windows7 we are seeing failures when the clent end tries to open the pipe. It does not always fail but fails more often depending upon the networking environment - for example it seems to work OK when machines & users are members of the same domain but I can never seem to get it to work when the machines are part of an ad-hoc network and a user is having trouble with machines within a workgroup.

A bit of research has revealed that Win7 is much more fussy about the security information provided when creating things like pipes and I have spent some time trying to generate security settings that come closest to ‘just let anybody connect’. This has not made things much better but annoyingly the guy who showed me this example code says that when his program tries to connect to *his* pipe, a logon dialog pops up allowing him to enter a username & password and then the connection goes through. With my code I just get access denied error codes and the like - it does seem to vary a lot.

It seems clear that there is no way that I can create a pipe that just anyone can connect to, but can anybody see anything in my code below (error handling removed for legibility) that could be changed to make a successful connection easier?

PSID pEveryoneSID = NULL; // Security ID referring to everyone
SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;

// Create a well-known SID for the Everyone group
if (!AllocateAndInitializeSid(&SIDAuthWorld, 1,
SECURITY_WORLD_RID,
0, 0, 0, 0, 0, 0, 0,
&pEveryoneSID))
{
return 0;
}

// Initialize an EXPLICIT_ACCESS structure for an ACE.
// The ACE will allow Everyone full access to the pipe.
EXPLICIT_ACCESS ea;
ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
ea.grfAccessPermissions = FILE_ALL_ACCESS;
ea.grfAccessMode = SET_ACCESS;
ea.grfInheritance = NO_INHERITANCE;
ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
ea.Trustee.ptstrName = (LPTSTR)pEveryoneSID;

// Create a new ACL that contains our new ACE.
PACL pACL = NULL;
DWORD dwRes = SetEntriesInAcl(1, &ea, NULL, &pACL);
if (ERROR_SUCCESS != dwRes)
{
return 0;
}

// Create and initialise a security descriptor.
PSECURITY_DESCRIPTOR pSD = NULL; // We need a security descriptor first
pSD = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
if (!pSD) // Check that this has succeeded!
{
return 0;
}

if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
{
return 0;
}

// Add the ACL to the security descriptor. Not a default as we built it.
if (!SetSecurityDescriptorDacl(pSD, TRUE, pACL, FALSE))
{
return 0;
}

// Initialize a security attributes structure to contain our security descriptor.
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof (SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = pSD;
sa.bInheritHandle = FALSE;

g_hTMSPipe = CreateNamedPipe("\\.\Pipe\"TALK_MASTER_PIPE,
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 1,
TALK_MASTER_BSIZE, TALK_MASTER_BSIZE, 0, &sa);

All suggestions or pointers to errors gratefully received!

So this has absolutely nothing at all to do with device drivers, but I
just happen to have implemented a named pipe server for win7:

//
// set security to D:(D;;FA;;;NU)(A;;0x12019f;;;WD)(A;;0x12019f;;;CO)
// 0x12019f =
// 0x00100000 - SYNCHRONIZE
//
// 0x00020000 - READ_CONTROL
// 0x00000100 - FILE_WRITE_ATTRIBUTES
// 0x00000080 - FILE_READ_ATTRIBUTES
// 0x00000010 - FILE_WRITE_EA
// 0x00000008 - FILE_READ_EA
// 0x00000004 - FILE_CREATE_PIPE_INSTANCE
// 0x00000002 - FILE_WRITE_DATA
// 0x00000001 - FILE_READ_DATA
//
// this should deny network access, but allow
// local logged on users to have write access.
//
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = FALSE;
ConvertStringSecurityDescriptorToSecurityDescriptor(
_T(“D:(D;;FA;;;NU)(A;;0x12019f;;;WD)(A;;0x12019f;;;CO)”),
SDDL_REVISION_1,
&sa.lpSecurityDescriptor,
NULL);

Pipe[i].oOverlap.hEvent = hEvents[i];

Pipe[i].hPipeInst = CreateNamedPipe(
lpszPipename, // pipe name
PIPE_ACCESS_DUPLEX | // read/write access
FILE_FLAG_OVERLAPPED, // overlapped mode
PIPE_TYPE_MESSAGE | // message-type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
INSTANCES, // number of instances
BUFSIZE*sizeof(TCHAR), // output buffer size
BUFSIZE*sizeof(TCHAR), // input buffer size
PIPE_TIMEOUT, // client time-out
&sa); // security attributes

“Works for me”.

Mark Roddy

On Wed, May 4, 2011 at 11:06 AM, wrote:
> Hi all,
>
> I’ve written code that opens a named pipe within a C++ application so that other programs can pass data packets in. This generally works fine, particularly if the application is running on XP or earlier, but when the application is running on Windows7 we are seeing failures when the clent end tries to open the pipe. It does not always fail but fails more often depending upon the networking environment - for example it seems to work OK when machines & users are members of the same domain but I can never seem to get it to work when the machines are part of an ad-hoc network and a user is having trouble with machines within a workgroup.
>
> A bit of research has revealed that Win7 is much more fussy about the security information provided when creating things like pipes and I have spent some time trying to generate security settings that come closest to ‘just let anybody connect’. This has not made things much better but annoyingly the guy who showed me this example code says that when his program tries to connect to his pipe, a logon dialog pops up allowing him to enter a username & password and then the connection goes through. With my code I just get access denied error codes and the like - it does seem to vary a lot.
>
> It seems clear that there is no way that I can create a pipe that just anyone can connect to, but can anybody see anything in my code below (error handling removed for legibility) that could be changed to make a successful connection easier?
>
> ? ?PSID pEveryoneSID = NULL; ? ? ? ? ? ? ? ? ? // Security ID referring to everyone
> ? ?SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
>
> ? ?// Create a well-known SID for the Everyone group
> ? ?if (!AllocateAndInitializeSid(&SIDAuthWorld, 1,
> ? ? ? ? ? ? ? ? ? ? SECURITY_WORLD_RID,
> ? ? ? ? ? ? ? ? ? ? 0, 0, 0, 0, 0, 0, 0,
> ? ? ? ? ? ? ? ? ? ? &pEveryoneSID))
> ? ?{
> ? ? ? ?return 0;
> ? ?}
>
> ? ?// Initialize an EXPLICIT_ACCESS structure for an ACE.
> ? ?// The ACE will allow Everyone full access to the pipe.
> ? ?EXPLICIT_ACCESS ea;
> ? ?ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
> ? ?ea.grfAccessPermissions = FILE_ALL_ACCESS;
> ? ?ea.grfAccessMode = SET_ACCESS;
> ? ?ea.grfInheritance = NO_INHERITANCE;
> ? ?ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
> ? ?ea.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
> ? ?ea.Trustee.ptstrName ?= (LPTSTR)pEveryoneSID;
>
> ? ?// Create a new ACL that contains our new ACE.
> ? ?PACL pACL = NULL;
> ? ?DWORD dwRes = SetEntriesInAcl(1, &ea, NULL, &pACL);
> ? ?if (ERROR_SUCCESS != dwRes)
> ? ?{
> ? ? ? ?return 0;
> ? ?}
>
> ? ?// Create and initialise a security descriptor.
> ? ?PSECURITY_DESCRIPTOR pSD = NULL; ? ? ? ? ? ?// We need a security descriptor first
> ? ?pSD = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
> ? ?if (!pSD) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // Check that this has succeeded!
> ? ?{
> ? ? ? ?return 0;
> ? ?}
>
> ? ?if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
> ? ?{
> ? ? ? ?return 0;
> ? ?}
>
> ? ?// Add the ACL to the security descriptor. Not a default as we built it.
> ? ?if (!SetSecurityDescriptorDacl(pSD, TRUE, pACL, FALSE))
> ? ?{
> ? ? ? ?return 0;
> ? ?}
>
> ? ?// Initialize a security attributes structure to contain our security descriptor.
> ? ?SECURITY_ATTRIBUTES sa;
> ? ?sa.nLength = sizeof (SECURITY_ATTRIBUTES);
> ? ?sa.lpSecurityDescriptor = pSD;
> ? ?sa.bInheritHandle = FALSE;
>
> ? ?g_hTMSPipe = CreateNamedPipe("\\.\Pipe\"TALK_MASTER_PIPE,
> ? ? ? ? ? ? ? ? ? ? ? ?PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
> ? ? ? ? ? ? ? ? ? ? ? ?PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 1,
> ? ? ? ? ? ? ? ? ? ? ? ?TALK_MASTER_BSIZE, TALK_MASTER_BSIZE, 0, &sa);
>
> All suggestions or pointers to errors gratefully received!
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>

> So this has absolutely nothing at all to do with device drivers, but I just happen

to have implemented a named pipe server for win7:

Really sorry - thought I was posting to a more general forum and this is the one I know perfectly well is for device drivers.

Thanks for your code Mark.

Can/should this thread be moved to a different forum?

Mark Roddy wrote:

So this has absolutely nothing at all to do with device drivers, but I
just happen to have implemented a named pipe server for win7:

ConvertStringSecurityDescriptorToSecurityDescriptor(
_T(“D:(D;;FA;;;NU)(A;;0x12019f;;;WD)(A;;0x12019f;;;CO)”),
SDDL_REVISION_1,
&sa.lpSecurityDescriptor,
NULL);

Damn. Anyone who can code up a string like that is operating on a
different plane. I am not worthy.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Until you ask how much he cut and pasted from an existing example. :slight_smile:

Gary G. Little
Certified Contest Administrator
C 952-454-4629
H 952-223-1349

On May 4, 2011, at 12:15, Tim Roberts wrote:

> Mark Roddy wrote:
>> So this has absolutely nothing at all to do with device drivers, but I
>> just happen to have implemented a named pipe server for win7:
>> …
>> ConvertStringSecurityDescriptorToSecurityDescriptor(
>> _T(“D:(D;;FA;;;NU)(A;;0x12019f;;;WD)(A;;0x12019f;;;CO)”),
>> SDDL_REVISION_1,
>> &sa.lpSecurityDescriptor,
>> NULL);
>
> Damn. Anyone who can code up a string like that is operating on a
> different plane. I am not worthy.
>
> –
> Tim Roberts, xxxxx@probo.com
> Providenza & Boekelheide, Inc.
>
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Darn you wrecked my triumph!

I did modify the stuff I pasted, does that count?

Mark Roddy

On Wed, May 4, 2011 at 1:41 PM, Gary Little wrote:
> Until you ask how much he cut and pasted from an existing example. :slight_smile:
>
> Gary G. Little
> Certified Contest Administrator
> C 952-454-4629
> H 952-223-1349
>
>
> On May 4, 2011, at 12:15, Tim Roberts wrote:
>
>> Mark Roddy wrote:
>>> So this has absolutely nothing at all to do with device drivers, but I
>>> just happen to have implemented a named pipe server for win7:
>>> …
>>> ? ? ? ?ConvertStringSecurityDescriptorToSecurityDescriptor(
>>> ? ? ? ? ? ?_T(“D:(D;;FA;;;NU)(A;;0x12019f;;;WD)(A;;0x12019f;;;CO)”),
>>> ? ? ? ? ? ?SDDL_REVISION_1,
>>> ? ? ? ? ? ?&sa.lpSecurityDescriptor,
>>> ? ? ? ? ? ?NULL);
>>
>> Damn. ?Anyone who can code up a string like that is operating on a
>> different plane. ?I am not worthy.
>>
>> –
>> Tim Roberts, xxxxx@probo.com
>> Providenza & Boekelheide, Inc.
>>
>>
>> —
>> NTDEV is sponsored by OSR
>>
>> For our schedule of WDF, WDM, debugging and other seminars visit:
>> http://www.osr.com/seminars
>>
>> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>

It better, 'cuz I’m screwed if it doesn’t!? I found a string whose description was the closest fit to my needs and then cut and pasted the hell out of it. I think that’s how most security string problems are solved.

Gary G. Little
Certified Contest Administrator
C 952-454-4629
H 952-223-1349

On May 4, 2011, at 13:25, Mark Roddy wrote:

> Darn you wrecked my triumph!
>
> I did modify the stuff I pasted, does that count?
>
> Mark Roddy
>
>
>
> On Wed, May 4, 2011 at 1:41 PM, Gary Little wrote:
>> Until you ask how much he cut and pasted from an existing example. :slight_smile:
>>
>> Gary G. Little
>> Certified Contest Administrator
>> C 952-454-4629
>> H 952-223-1349
>>
>>
>> On May 4, 2011, at 12:15, Tim Roberts wrote:
>>
>>> Mark Roddy wrote:
>>>> So this has absolutely nothing at all to do with device drivers, but I
>>>> just happen to have implemented a named pipe server for win7:
>>>> …
>>>> ConvertStringSecurityDescriptorToSecurityDescriptor(
>>>> _T(“D:(D;;FA;;;NU)(A;;0x12019f;;;WD)(A;;0x12019f;;;CO)”),
>>>> SDDL_REVISION_1,
>>>> &sa.lpSecurityDescriptor,
>>>> NULL);
>>>
>>> Damn. Anyone who can code up a string like that is operating on a
>>> different plane. I am not worthy.
>>>
>>> –
>>> Tim Roberts, xxxxx@probo.com
>>> Providenza & Boekelheide, Inc.
>>>
>>>
>>> —
>>> NTDEV is sponsored by OSR
>>>
>>> For our schedule of WDF, WDM, debugging and other seminars visit:
>>> http://www.osr.com/seminars
>>>
>>> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>>
>> —
>> NTDEV is sponsored by OSR
>>
>> For our schedule of WDF, WDM, debugging and other seminars visit:
>> http://www.osr.com/seminars
>>
>> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>>
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer