How to share user defined structure object between user mode and kernel mode

Hi,
I am writing WUSB driver. I have defined one structure that shares data
between user mode and kernel mode. I pass structure pointer from user mode
to kernel mode thru IOCTL. I do some processing in kernel mode such that
structure pointer sent from user mode get modified. I need to retain
structure pointer value in both mode. Now i am not able to retain the value.
I am getting error code as 0x000003E6 i.e. “Invalid access to memory
location.” in user mode.

I am using WdfRequestSetInfomrmation() to set changed object value
code snippet is as follows-

//
// Set the completion status information.
//
WdfRequestSetInformation(
wdfRequest,
(ULONG_PTR)&pStructPtr //
structure pointer retrieved using WdfRequestRetrieveInputBuffer
);

//
// Complete the request with information.
//
WdfRequestComplete(
wdfRequest,
ntStatus
);

Please suggest me.

Thanks in advance,
Shalaka

Hi Shalaka,

You cant directly use user mode and kernel mode addresses interchangeably. The error you are getting is because you are trying to access memory which you dont have access permissions for. I think you are trying to use a kernel address space pointer.

  1. What method of IOCTL communication are you using ??? (i.e BUFFERED or DIRECT or NEITHER)
  2. Are you sure the structure you are receiving in kernel mode is correct. I mean have you tried setting some fields in user space and checking their corresponding values in kernel space ?? I dont think that passing a user mode address and directly accessing it in kernel mode will work.

Much more easy is not to pass a pointer to your structure, but pass the structure itself using buffered I/O. Otherwise you will need to learn how to probe, lock, and map your buffer or do your own buffering. Read the articles about usermode/kernelmode communication and methods for passing data buffers in the WDK.

//Daniel

“Shalaka Gujar” wrote in message news:xxxxx@ntdev…
Hi,

I am writing WUSB driver. I have defined one structure that shares data between user mode and kernel mode. I pass structure pointer from user mode to kernel mode thru IOCTL. I do some processing in kernel mode such that structure pointer sent from user mode get modified. I need to retain structure pointer value in both mode. Now i am not able to retain the value. I am getting error code as 0x000003E6 i.e. “Invalid access to memory location.” in user mode.

I am using WdfRequestSetInfomrmation() to set changed object value
code snippet is as follows-

//
// Set the completion status information.
//
WdfRequestSetInformation(
wdfRequest,
(ULONG_PTR)&pStructPtr // structure pointer retrieved using WdfRequestRetrieveInputBuffer
);

//
// Complete the request with information.
//
WdfRequestComplete(
wdfRequest,
ntStatus
);

Please suggest me.

Thanks in advance,
Shalaka

> You cant directly use user mode and kernel mode addresses interchangeably.

Theoretically, yes. However, please note that UM address is valid only in context of its address space. Therefore, if the OP tries to access this address some context other than current thread, this address may be plainly invalid. Second, and even more important, there is no guarantee that this address is valid even it its own address space, in the first place - you cannot trust UM code, can you.

Therefore, unless you are just desperate to spew out yet another piece of crap into the market, you should not access UM address directly - instead, you should build MDL for it, probe and lock it, and then map it into the system space. At this point you will be able to safely access it in any context…

Anton Bassov

Why do you want to share a structure between the two? Even if you pull this off, how are you going to synchronize access to this structure and its fields between your KM and UM components?

From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Shalaka Gujar
Sent: Tuesday, September 30, 2008 5:16 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] How to share user defined structure object between user mode and kernel mode

Hi,

I am writing WUSB driver. I have defined one structure that shares data between user mode and kernel mode. I pass structure pointer from user mode to kernel mode thru IOCTL. I do some processing in kernel mode such that structure pointer sent from user mode get modified. I need to retain structure pointer value in both mode. Now i am not able to retain the value. I am getting error code as 0x000003E6 i.e. “Invalid access to memory location.” in user mode.

I am using WdfRequestSetInfomrmation() to set changed object value
code snippet is as follows-

//
// Set the completion status information.
//
WdfRequestSetInformation(
wdfRequest,
(ULONG_PTR)&pStructPtr // structure pointer retrieved using WdfRequestRetrieveInputBuffer
);

//
// Complete the request with information.
//
WdfRequestComplete(
wdfRequest,
ntStatus
);

Please suggest me.

Thanks in advance,
Shalaka
— 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

Hi Doron,

I need to share information between user mode and kernel mode. i have
written one common structure for that. am i in wrong direction? is there any
other way to share information between the two?

I am beginner to device driver. Can you please guide me?

Thanks & Regards,

Shalaka

Subject: RE: How to share user defined structure object between user mode
and kernel mode
From: Doron Holan
Date: Wed, 1 Oct 2008 10:04:36 -0700
X-Message-Number: 20

Why do you want to share a structure between the two? Even if you pull this
off, how are you going to synchronize access to this structure and its
fields between your KM and UM components?

From: xxxxx@lists.osr.com [mailto:
xxxxx@lists.osr.com] On Behalf Of Shalaka Gujar
Sent: Tuesday, September 30, 2008 5:16 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] How to share user defined structure object between user
mode and kernel mode

Hi,

I am writing WUSB driver. I have defined one structure that shares data
between user mode and kernel mode. I pass structure pointer from user mode
to kernel mode thru IOCTL. I do some processing in kernel mode such that
structure pointer sent from user mode get modified. I need to retain
structure pointer value in both mode. Now i am not able to retain the value.
I am getting error code as 0x000003E6 i.e. “Invalid access to memory
location.” in user mode.

I am using WdfRequestSetInfomrmation() to set changed object value
code snippet is as follows-

//
// Set the completion status information.
//
WdfRequestSetInformation(
wdfRequest,
(ULONG_PTR)&pStructPtr //
structure pointer retrieved using WdfRequestRetrieveInputBuffer
);

//
// Complete the request with information.
//
WdfRequestComplete(
wdfRequest,
ntStatus
);

Please suggest me.

Thanks in advance,
Shalaka
— 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

Pass the information in to the driver with an IOCTL and return the
iinformation you need from the driver with an IOCTL. Using shared data
structures has all the synchronization problems as Doron mentioned and
problems with handling application failure. Using shared info that way
should be for enviroments looking for multiple hundred megabytes per second.

Take a look at the IOCTL sample in the WDK. And consider using KMDF which
will do much of the work for you.


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr
Remove StopSpam to reply

“Shalaka Gujar” wrote in message
news:xxxxx@ntdev…
> Hi Doron,
>
> I need to share information between user mode and kernel mode. i have
> written one common structure for that. am i in wrong direction? is there
> any
> other way to share information between the two?
>
> I am beginner to device driver. Can you please guide me?
>
> Thanks & Regards,
>
> Shalaka
>
>
> Subject: RE: How to share user defined structure object between user mode
> and kernel mode
> From: Doron Holan
> Date: Wed, 1 Oct 2008 10:04:36 -0700
> X-Message-Number: 20
>
> Why do you want to share a structure between the two? Even if you pull
> this
> off, how are you going to synchronize access to this structure and its
> fields between your KM and UM components?
>
> From: xxxxx@lists.osr.com [mailto:
> xxxxx@lists.osr.com] On Behalf Of Shalaka Gujar
> Sent: Tuesday, September 30, 2008 5:16 AM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] How to share user defined structure object between user
> mode and kernel mode
>
> Hi,
>
> I am writing WUSB driver. I have defined one structure that shares data
> between user mode and kernel mode. I pass structure pointer from user mode
> to kernel mode thru IOCTL. I do some processing in kernel mode such that
> structure pointer sent from user mode get modified. I need to retain
> structure pointer value in both mode. Now i am not able to retain the
> value.
> I am getting error code as 0x000003E6 i.e. “Invalid access to memory
> location.” in user mode.
>
> I am using WdfRequestSetInfomrmation() to set changed object value
> code snippet is as follows-
>
>
> //
> // Set the completion status information.
> //
> WdfRequestSetInformation(
> wdfRequest,
> (ULONG_PTR)&pStructPtr //
> structure pointer retrieved using WdfRequestRetrieveInputBuffer
> );
>
> //
> // Complete the request with information.
> //
> WdfRequestComplete(
> wdfRequest,
> ntStatus
> );
>
> Please suggest me.
>
> Thanks in advance,
> Shalaka
> — 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
>

Hi Shalaka,
Do you really want to “share” a data structure bet user mode and kernel mode which can be (or may be ) modified at both ends simultaneously ???
OR
just wish to, use a structure appropriately filled in user mode, to be passed to kernel mode, modify it (or use it) and return the structure values back in user mode ?

If its the former , you will have ur hands filled with synchronization issues to safeguard the structure.

If its latter case then its just the matter of passing the structure contents using an IOCTL to kernel mode. If its just a small amount of data METHOD_BUFFERED will do for IOCTL.

For the latter case you can look for passing IOCTL’s to drivers in WDK.

Hope this helps.

Regards,

Imtiaz Pathan
Developer
Calsoft Pvt. Ltd.

Hi,

In my case, second case is applicable i.e

  • i am using a structure in a user mode
  • passing it in kernel mode thru IOCTL
  • Modifying / using it in kernel mode
  • Returning in user mode with modified structure values in kernel mode

I am passing pointer to structure as a IOCTL input parameter
and returning same as output parameter. Here, i am using “directIO” method.
After modifying structure in kernel mode i had written code as follows which
will give me updated structure in user mode.

Code snippet is as follows -

//
// Complete the request with information.
//
WdfRequestCompleteWithInformation(
wdfRequest,
ntStatus,
nInputBufferLength
);

Am i on right track? please guide me.

Thanks & Regards,
Shalaka

Subject: RE: How to share user defined structure object between user mode
and kernel mode
From: xxxxx@yahoo.com
Date: Fri, 3 Oct 2008 11:44:30 -0400 (EDT)
X-Message-Number: 15

Hi Shalaka,
Do you really want to “share” a data structure bet user mode and kernel mode
which can be (or may be ) modified at both ends simultaneously ???
OR
just wish to, use a structure appropriately filled in user mode, to be
passed to kernel mode, modify it (or use it) and return the structure values
back in user mode ?

If its the former , you will have ur hands filled with synchronization
issues to safeguard the structure.

If its latter case then its just the matter of passing the structure
contents using an IOCTL to kernel mode. If its just a small amount of data
METHOD_BUFFERED will do for IOCTL.

For the latter case you can look for passing IOCTL’s to drivers in WDK.

Hope this helps.

Regards,

Imtiaz Pathan
Developer
Calsoft Pvt. Ltd.

See the WDK sample “nonpnp” in the src\kmdf folder for a good illustration
of handling IOCTLs.

Thomas F. Divine

From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Shalaka Gujar
Sent: Monday, October 06, 2008 8:48 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] How to share user defined structure object between user
mode and kernel mode

Hi,

In my case, second case is applicable i.e

* i am using a structure in a user mode
* passing it in kernel mode thru IOCTL
* Modifying / using it in kernel mode
* Returning in user mode with modified structure values in kernel mode

I am passing pointer to structure as a IOCTL input parameter and returning
same as output parameter. Here, i am using “directIO” method.

After modifying structure in kernel mode i had written code as follows which
will give me updated structure in user mode.

Code snippet is as follows -

//

// Complete the request with information.

//

WdfRequestCompleteWithInformation(

wdfRequest,

ntStatus,

nInputBufferLength

);

Am i on right track? please guide me.

Thanks & Regards,

Shalaka

Subject: RE: How to share user defined structure object between user mode
and kernel mode
From: mailto:xxxxx xxxxx@yahoo.com
Date: Fri, 3 Oct 2008 11:44:30 -0400 (EDT)
X-Message-Number: 15

Hi Shalaka,
Do you really want to “share” a data structure bet user mode and kernel mode
which can be (or may be ) modified at both ends simultaneously ???
OR
just wish to, use a structure appropriately filled in user mode, to be
passed to kernel mode, modify it (or use it) and return the structure values
back in user mode ?

If its the former , you will have ur hands filled with synchronization
issues to safeguard the structure.

If its latter case then its just the matter of passing the structure
contents using an IOCTL to kernel mode. If its just a small amount of data
METHOD_BUFFERED will do for IOCTL.

For the latter case you can look for passing IOCTL’s to drivers in WDK.

Hope this helps.

Regards,

Imtiaz Pathan
Developer
Calsoft Pvt. Ltd.

— 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</mailto:xxxxx>

Shalaka Gujar wrote:

In my case, second case is applicable i.e

* i am using a structure in a user mode
* passing it in kernel mode thru IOCTL
* Modifying / using it in kernel mode
* Returning in user mode with modified structure values in kernel mode

I am passing pointer to structure as a IOCTL input parameter
and returning same as output parameter. Here, i am using “directIO”
method.

METHOD_IN_DIRECT or METHOD_OUT_DIRECT? Actually, as it happens, it
doesn’t matter.

With METHOD_x_DIRECT, it is not helpful to think of the two buffers in
DeviceIoControl as “input” and “output”. You need to think of them as
“first buffer” and “second buffer”. With METHOD_x_DIRECT, the first
buffer is copied to kernel mode, just like in METHOD_BUFFERED. The
second buffer is mapped directly.

So, if you have a buffer going both in and out, then you should just use
the second buffer (which KM sees as the output buffer) by itself. You
can read from it, and write to it.

Alternatively, if the buffer is small (less than a page), then just use
METHOD_BUFFERED. In that case, the input/output distinction is meaningful.

//
// Complete the request with information.
//
WdfRequestCompleteWithInformation(
wdfRequest,
ntStatus,
nInputBufferLength
);

If you do it this way, you need to assert that nInputBufferLength <=
nOutputBufferLength.


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