How to lock the memory in kernel

Hi All,
I am allocating memory in the user mode application like follows.

pulTemp = (ULONG *)malloc(1024);

Then filling the input parameter structure like follows

WRITE_DEVICE_DATA_INPARAMS m_WRITE_DEVICE_DATA_INPARAMS;
::memset(&m_WRITE_DEVICE_DATA_INPARAMS,0,sizeof(WRITE_DEVICE_DATA_INPARAMS));
m_WRITE_DEVICE_DATA_INPARAMS.ucBarIndex = 1; m_WRITE_DEVICE_DATA_INPARAMS.ulPhysicalAddress = 0x34; // Dword-aligned register offset. m_WRITE_DEVICE_DATA_INPARAMS.ulItemSize = 1024;
m_WRITE_DEVICE_DATA_INPARAMS.data.vulDataBuf[0] = pulTemp ;

after that i am passing that structure to the driver through DeviceIoCtl

but before using the address of memory region allocated by malloc in user mode i want to lock that memory so that it won’t get swapped out.

i tried to map it to kernel virtual address by using MmMapIoSpace but i am getting bug check and its message is that i must lock that before calling MmMapIoSpace.

how can i do that ?
any help will be appreciated.

Regards


Catch the latest fashion shows, get beauty tips and learn more on fashion and lifestyle.
http://video.msn.com/?mkt=en-in

This is an extremely poor design, it is a variant on METHOD_NEITHER which
has all kinds of security holes. You can’t just lock it in memory, you
have to validate the address region then lock it in memory. Consider
instead using an IOCTL that passes the control block as the input param, and
the buffer as the “output” paramerer with a buffering type of
METHOD_IN_DIRECT.


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

“nayan kumar” wrote in message
news:xxxxx@ntdev…

Hi All,
I am allocating memory in the user mode application like follows.

pulTemp = (ULONG *)malloc(1024);

Then filling the input parameter structure like follows

WRITE_DEVICE_DATA_INPARAMS m_WRITE_DEVICE_DATA_INPARAMS;
::memset(&m_WRITE_DEVICE_DATA_INPARAMS,0,sizeof(WRITE_DEVICE_DATA_INPARAMS));
m_WRITE_DEVICE_DATA_INPARAMS.ucBarIndex = 1;
m_WRITE_DEVICE_DATA_INPARAMS.ulPhysicalAddress = 0x34; // Dword-aligned
register offset. m_WRITE_DEVICE_DATA_INPARAMS.ulItemSize = 1024;
m_WRITE_DEVICE_DATA_INPARAMS.data.vulDataBuf[0] = pulTemp ;

after that i am passing that structure to the driver through DeviceIoCtl

but before using the address of memory region allocated by malloc in user
mode i want to lock that memory so that it won’t get swapped out.

i tried to map it to kernel virtual address by using MmMapIoSpace but i am
getting bug check and its message is that i must lock that before calling
MmMapIoSpace.

how can i do that ?
any help will be appreciated.

Regards

_________________________________________________________________
Catch the latest fashion shows, get beauty tips and learn more on fashion
and lifestyle.
http://video.msn.com/?mkt=en-in

>but before using the address of memory region allocated by malloc in user mode
i

want to lock that memory so that it won’t get swapped out.

IoAllocateMdl+MmProbeAndLockPages+MmGetSystemAddressForMdlSafe


Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

nayan kumar wrote:

Hi All,
I am allocating memory in the user mode application like
follows.

pulTemp = (ULONG *)malloc(1024);

Then filling the input parameter structure like follows

WRITE_DEVICE_DATA_INPARAMS m_WRITE_DEVICE_DATA_INPARAMS;
::memset(&m_WRITE_DEVICE_DATA_INPARAMS,0,sizeof(WRITE_DEVICE_DATA_INPARAMS));
m_WRITE_DEVICE_DATA_INPARAMS.ucBarIndex = 1;
m_WRITE_DEVICE_DATA_INPARAMS.ulPhysicalAddress = 0x34; //
Dword-aligned register offset.
m_WRITE_DEVICE_DATA_INPARAMS.ulItemSize = 1024;
m_WRITE_DEVICE_DATA_INPARAMS.data.vulDataBuf[0] = pulTemp ;

after that i am passing that structure to the driver through DeviceIoCtl

Maxim gave you the right advice here, but please allow me to be a bit
more explicit.

There is a tendency to believe that the “input” and “output” parameters
in an ioctl are somehow exclusive. This is not the case. It is more
accurate to think about them as “parameter 1” and “parameter 2”.

The correct way to solve your problem is to remove the “data” element
from your WRITE_DEVICE_DATA_INPARAMS structure. Pass the structure as
the input parameter to DeviceIoControl, and pass pulTemp as the output
parameter. Make the ioctl METHOD_OUT_DIRECT.

Now, in your driver, the input structure will be copied, just like with
METHOD_BUFFERED, and the output parameter will be a properly locked down
kernel virtual address that you can use to write the output data.

Any time you try to use a user-mode pointer in a kernel driver, it
should raise a bright red flag.


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

Tim Roberts wrote:

Any time you try to use a user-mode pointer in a kernel driver, it
should raise a bright red flag.

Indeed.

Though if anyone really needs to pass more complex data structures to a
driver - anything more complex than an array of structures - I found the
following blog post quite informative:

http://tinyurl.com/4g4dmt

(http://blogs.msdn.com/itgoestoeleven/archive/2008/03/31/why-your-user-mode-pointer-captures-are-probably-broken.aspx)

However, I should also state that this is a rare situation. I’m a
believer that IOCTLs should be kept simple.

-Stephen Cleary

Thanks for your help.

One doubt i have that i want to make it clear.

In which case it becomes neccessary to map the user mode virtual memory address to kernel mode virtual memory address for which we usually use MmMapIoSpace before calling IoAllocateMdl.

and in those cases how to lock the memory before calling MmMapIoSpace .

Regards

From: xxxxx@storagecraft.com> Subject: Re:[ntdev] How to lock the memory in kernel> Date: Wed, 21 May 2008 21:51:12 +0400> To: xxxxx@lists.osr.com> > >but before using the address of memory region allocated by malloc in user mode> i> >want to lock that memory so that it won’t get swapped out.> > IoAllocateMdl+MmProbeAndLockPages+MmGetSystemAddressForMdlSafe> > – > Maxim Shatskih, Windows DDK MVP> StorageCraft Corporation> xxxxx@storagecraft.com> http://www.storagecraft.com> > > —> 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


2000 Placements last year. Are You next ? Find out
http://ss1.richmedia.in/recurl.asp?pid=499

You don’t use MmMapIoSpace to map a user mode virtual address to kernel
mode. The documentation clearly states that this interface “maps the given
physical address range to nonpaged system space”. Maxim already answered
‘how to map user mode virtual address to kernel mode’:
IoAllocateMdl+MmProbeAndLockPages+MmGetSystemAddressForMdlSafe.

On Thu, May 22, 2008 at 6:56 AM, nayan kumar
wrote:

> Thanks for your help.
>
> One doubt i have that i want to make it clear.
>
> In which case it becomes neccessary to map the user mode virtual memory
> address to kernel mode virtual memory address for which we usually use
> MmMapIoSpace before calling IoAllocateMdl.
>
> and in those cases how to lock the memory before calling MmMapIoSpace .
>
> Regards
>
> > From: xxxxx@storagecraft.com
> > Subject: Re:[ntdev] How to lock the memory in kernel
> > Date: Wed, 21 May 2008 21:51:12 +0400
> > To: xxxxx@lists.osr.com
> >
> > >but before using the address of memory region allocated by malloc in
> user mode
> > i
> > >want to lock that memory so that it won’t get swapped out.
> >
> > IoAllocateMdl+MmProbeAndLockPages+MmGetSystemAddressForMdlSafe
> >
> > –
> > Maxim Shatskih, Windows DDK MVP
> > StorageCraft Corporation
> > xxxxx@storagecraft.com
> > http://www.storagecraft.com
> >
> >
> > —
> > 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
>
>
> ------------------------------
> WL Messenger : Stay connected to friends and family with the New Windows
> Live Messenger. Get it now. Try it now!http:
> —
> 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
>


Mark Roddy</http:>