11.07.2015 Views

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

168 Part III: Building Secure <strong>Web</strong> <strong>Application</strong>sUnmanaged CodeIf you have existing COM components or Win32 DLLs that you want to reuse, use thePlatform Invocation Services (P/Invoke) or COM Interop layers. When you callunmanaged code, it is vital that your managed code validates each input parameterpassed to the unmanaged API to guard against potential buffer overflows. Also, becareful when h<strong>and</strong>ling output parameters passed back from the unmanaged API.You should isolate calls to unmanaged code in a separate wrapper assembly. Thisallows you to s<strong>and</strong>box the highly privileged code <strong>and</strong> to isolate the code accesssecurity permission requirements to a specific assembly. For more details abouts<strong>and</strong>boxing <strong>and</strong> about additional code access security related guidelines that youshould apply when calling unmanaged code, see “Unmanaged Code” in Chapter 8,“Code Access <strong>Security</strong> in Practice.” The following recommendations help improvethe security of your unmanaged API calls, without using explicit code access securitycoding techniques:● Validate input <strong>and</strong> output string parameters.●●●●Validate array bounds.Check file path lengths.Compile unmanaged code with the /GS switch.Inspect unmanaged code for “dangerous” APIs.Validate Input <strong>and</strong> Output String ParametersString parameters passed to unmanaged APIs are a prime source of buffer overflows.Check the length of any input string inside your wrapper code to ensure it does notexceed the limit defined by the unmanaged API. If the unmanaged API accepts acharacter pointer you may not know the maximum permitted string length, unlessyou have access to the unmanaged source. For example, the following is a commonvulnerability.void SomeFunction( char *pszInput ){char szBuffer[10];// Look out, no length checks. Input is copied straight into the buffer// Check length or use strncpystrcpy(szBuffer, pszInput);. . .}If you cannot examine the unmanaged code because you do not own it, make surethat you rigorously test the API by passing in deliberately long input strings.If your code uses a StringBuilder to receive a string passed from an unmanaged API,make sure that it can hold the longest string that the unmanaged API can h<strong>and</strong> back.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!