help organizing driver for session scope usermode access

Hi, I have a driver that communicates with a svc via ioctls, the svc then sends data from driver to singleton clients per windows sessions (think citrix / RDS). The driver organizes its data via session ids and the svc only sends the data to the client that is running in that windows session.

Everything works fine.

Now, I’d like to improve on this by eliminating the svc and modifying the driver so a win32 client in a given session can connect to it like a file.

Something like this (note the device name, I’m pretty clueless here on what to do)

CreateFileA( “\\.\MY_DEVICE\session_id_x”,
GENERIC_WRITE,
FILE_SHARE_READ,
0,
OPEN_EXISTING,
0,
NULL);
while ReadFile()
{
process_line()
}

// then in the drivers ioctl for read it would send back line by line

What is the best way to achieve something like this? Or does it even make sense?

In the driver I could create a device each time I see items for a new session. The client running in a new windows session would “know” the device name, but that doesn’t seem right to me.
Can’t my one device just have a memory only file of some type created on the fly that the client in the new session could read from like a normal file / pipe?

Is this a job for ZwCreateSection? Can that be accessed like a normal file from usermode?
What’s the best way to organize something like this?

Thanks.

In your example, “\session_id_x” is just a namespace. The I/O Manager ignores this part of the name when determining which device the user is opening.

When the user issues the CreateFile you show above, your driver will be called at EvtFileCreate with the handle to the associated File Object… you then call WdfFileObjectGetFileName to get the namespace name.

Is that what you want to be able to do??

Peter
OSR
@OSRDrivers

Does it have to protect against faking the session ID by the client application?

Sorry I wasn’t clear enough. It can be hard to convey everything… Again, I’m pretty clueless on how to do this. So please try to bear with me.

I’m not sure I want a file, I just think I do, perhaps there’s something better. My desire is for the “file” to be accessible with the standard ioctls for normal files, but I don’t want anything on disk, just all memory. Shared memory would be fine.

I’d like the driver to create a “file” or an equivalent that represents the collection of data destined for each session. So if the citrix server has 20 sessions the driver will have 20 “files”.
Each session based usermode client of the driver would then connect to its instance of that “file”.

So, when the driver starts running, it will create a file for session 0.

RtlInitUnicodeString(&uszDriverString, L"\Device\MyDevice\0.dat");

// is the above even valid? it’s the desired “file” for session 0, but its a device, not a file, is that what I want? 20 devices, 1 for each session? Or 20 “files” if “file”, how do I create that??

IoCreateDeviceSecure( DriverObject,
sizeof(HANDLE), &uszDriverString,
FILE_DEVICE_UNKNOWN, 0,FALSE, &uszSD, (LPCGUID)&GUID_DEVCLASS_MYDEVICE, &pDeviceObject ));

When someone hits ctrl alt del to logon and a new session is created, the driver will see processes in a new session created and then create that new “file” ( maybe: L"\Device\MyDevice\1.dat" ) and then write information into the file for the usermode consumer to read.

Currently the driver only creates the single device ( L"\Device\MyDevice") , and the usermode process only connects to that device.

@Alex Grig, I have an admin only SDDL on IoCreateDeviceSecure right now, I’d hope for similar on the “files”

Thanks again for your time

Did you not read my previous reply?

To repeat: Just create one device. In your EvtFileCreate Event Processing Callback your driver can get the NAME of the “file” the user specified … which we call a device namespace… and do anything it wants to do.

Just like a user app can open C:\fred\bob.txt, it can also open ANY device and specify (the equivalent of) a directory and a file name. It’s just that almost ALL device drivers ignore the namespace part of the name. You get the name from the WDF File Object as I previously described. The contents of the device namespace part of the name is entirely arbitrary and is therefore up to your driver (note that your driver must validate the format of the name as well).

This is, by the way, one of my very favorite but most under-utilized features of the Windows I/O subsystem.

Coincidentally, I have a blog entry already done for this topic… it’s scheduled to be made live in a couple of weeks (sorry it can’t be sooner). Keep an eye on http:</http:>

Peter
OSR
@OSRDrivers

I did read your reply, unfortunately I did not understand all of your implications.
I will attempt what I believe you’re telling me to do.

Thanks

xxxxx@yahoo.com wrote:

…
Something like this (note the device name, I’m pretty clueless here on what to do)

CreateFileA( “\\.\MY_DEVICE\session_id_x”,
GENERIC_WRITE,
FILE_SHARE_READ,
0,
OPEN_EXISTING,
0,
NULL);
while ReadFile()
{
process_line()
}

// then in the drivers ioctl for read it would send back line by line

What is the best way to achieve something like this? Or does it even make sense?

In the driver I could create a device each time I see items for a new session. The client running in a new windows session would “know” the device name, but that doesn’t seem right to me.

Why do you need the client to specify the session? What would happen if
you had all of the clients simply use the name “\.\MyDevice”? Can’t
your driver figure out in the FileCreate handler what session they are?
Remember that the I/O system will keep all of the instances separate.

By the way, the name “\\.\MyDevice” would be created in your driver
as a symbolic link. KMDF has APIs to do that. Device Interfaces are a
more flexible way of doing it, but that’s more trouble for the application.

Can’t my one device just have a memory only file of some type created on the fly that the client in the new session could read from like a normal file / pipe?

I think you’re making it more complicated than it needs to be. Once a
client has a file handle, it can issue ReadFile and WriteFile requests
to its heart’s content. They will land in your driver, and you get to
decide how to respond. You don’t have to have a “memory only file” –
every driver is a pseudo-file.

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

Right, just call IoGetRequestorSessionId and you can determine which session “stream” the caller should get a handle to without even needing to specifying it in the file name itself.

d
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Tim Roberts
Sent: Thursday, September 4, 2014 9:29 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] help organizing driver for session scope usermode access

xxxxx@yahoo.com wrote:

…
Something like this (note the device name, I’m pretty clueless here on
what to do)

CreateFileA( “\\.\MY_DEVICE\session_id_x”,
GENERIC_WRITE,
FILE_SHARE_READ,
0,
OPEN_EXISTING,
0,
NULL);
while ReadFile()
{
process_line()
}

// then in the drivers ioctl for read it would send back line by line

What is the best way to achieve something like this? Or does it even make sense?

In the driver I could create a device each time I see items for a new session. The client running in a new windows session would “know” the device name, but that doesn’t seem right to me.

Why do you need the client to specify the session? What would happen if you had all of the clients simply use the name “\.\MyDevice”? Can’t your driver figure out in the FileCreate handler what session they are?
Remember that the I/O system will keep all of the instances separate.

By the way, the name “\\.\MyDevice” would be created in your driver as a symbolic link. KMDF has APIs to do that. Device Interfaces are a more flexible way of doing it, but that’s more trouble for the application.

Can’t my one device just have a memory only file of some type created on the fly that the client in the new session could read from like a normal file / pipe?

I think you’re making it more complicated than it needs to be. Once a client has a file handle, it can issue ReadFile and WriteFile requests to its heart’s content. They will land in your driver, and you get to decide how to respond. You don’t have to have a “memory only file” – every driver is a pseudo-file.

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


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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

and

Cool. I learned something today!

Peter
OSR
@OSRDrivers

Scary that I knew something you didn’t considering I haven’t written a driver in 5+ years :wink:

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@osr.com
Sent: Thursday, September 4, 2014 11:27 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] help organizing driver for session scope usermode access

and

[quote]
Right, just call IoGetRequestorSessionId [/quote]

Cool. I learned something today!

Peter
OSR
@OSRDrivers


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

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

Worse, you’re a MANAGER now. Imagine. My tech chops have been shown up my a manager. It’s embarrassing.

Peter
OSR
@OSRDrivers