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.

Chapter 7: Building Secure Assemblies 169Validate Array BoundsIf you pass input to an unmanaged API using an array, check that the managedwrapper verifies that the capacity of the array is not exceeded.Check File Path LengthsIf the unmanaged API accepts a file name <strong>and</strong> path, check that it does not exceed260 characters. This limit is defined by the Win32 MAX_PATH constant. It is verycommon for unmanaged code to allocate buffers of this length to manipulate filepaths.Note Directory names <strong>and</strong> registry keys can only be a maximum of 248 characters long.DelegatesCompile Unmanaged Code With the /GS SwitchIf you own the unmanaged code, compile it using the /GS switch to enable stackprobes to help detect buffer overflows. For more information about the /GS switch,see Microsoft Knowledge Base article 325483, “<strong>Web</strong>Cast: Compiler <strong>Security</strong> Checks:The -GS compiler switch.”Inspect Unmanaged Code for Dangerous APIsIf you have access to the source code for the unmanaged code that you are calling,you should subject it to a thorough code review, paying particular attention toparameter h<strong>and</strong>ling to ensure that buffer overflows are not possible <strong>and</strong> that it doesnot use potentially dangerous APIs. For more information see Chapter 21, “CodeReview.”Delegates are the managed equivalent of type safe function pointers <strong>and</strong> are used bythe .NET Framework to support events. The delegate object maintains a reference to amethod, which is called when the delegate is invoked. Events allow multiplemethods to be registered as event h<strong>and</strong>lers. When the event occurs, all event h<strong>and</strong>lersare called.Do Not Accept Delegates from Untrusted SourcesIf your assembly exposes a delegate or an event, be aware that any code canassociate a method with the delegate <strong>and</strong> you have no advance knowledge ofwhat the code does. The safest policy is not to accept delegates from untrustedcallers. If your assembly is strong named <strong>and</strong> does not include theAllowPartiallyTrustedCallersAttribute, only Full Trust callers can pass youa delegate.

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

Saved successfully!

Ooh no, something went wrong!