Write safe driver input/output check
The input/output check is a process of checking the validity of untrusted input/output addresses and data lengths. This method is widely used in Windows Kernel APIs.
For example, in the ntreadfile function, if the previusmode is not kernelmode, that is, the ntreadfile function is called from the user State, you can use the probeforwrite function to check whether the input/output buffer is writable. See the code in reactos as follows:
Ntstatus ntapi ntreadfile (inhandle filehandle,
In handle event optional,
In pio_apc_routine apcroutineoptional,
In pvoid apccontext optional,
Out pio_status_block iostatusblock,
Out pvoid buffer,
In ulong length,
In plarge_integer byteoffset optional,
In Pulong key optional)
{
Kprocessor_mode previusmode = kegetpreviusmode ();
// Omit Part of the code ......
/* Validate user-mode buffers */
If (previusmode! = Kernelmode)
{
_ Seh2_try
{
/* Probe the status block */
Probeforwriteiostatusblock (iostatusblock );
/* Probe the read buffer */
Probeforwrite (buffer, length, 1 );
// Omit Part of the code ......
Similarly, when the ntwritefile function finds that previusmode is not kernelmode, that is, it is called from the user State, you can use the probeforread function for detection.
In iocontrol, if the mthod specified by iocontrolcode is method_neither, use the probeforread and probeforwrite functions for the input and output addresses.
This article is excerpted from "0-day security: software vulnerability analysis technology (version 2nd.
Book details: http://blog.csdn.net/broadview2006/article/details/6596921