Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Safe Exception Handling in C++ Builder

For Windows 8 application certification, there are (among other) these requirements:

  • 3.2 Your app must be compiled using the /SafeSEH flag to ensure safe exceptions handling
  • 3.3 Your app must be compiled using the /NXCOMPAT flag to prevent data execution
  • 3.4 Your app must be compiled using the /DYNAMICBASE flag for address space layout randomization (ASLR)

I wasn't able to find out how to enable either of these in C++Builder XE.

For /NXCOMPAT and /DYNAMICBASE, one can use editbin.exe from VS or peflags.exe from Cygwin. Though I would feel more confident about possible side-effects, if there was native way to enable these.

Anyway, I'm totally at loss regarding /SafeSEH.

like image 845
Martin Prikryl Avatar asked Jun 25 '12 20:06

Martin Prikryl


People also ask

How do I enable EHsc?

For details, see How to: Open Project Property Pages. Click the C/C++ folder. Click the Code Generation property page. Set Enable C++ Exceptions to Yes (/EHsc).

What are safe exception handlers?

SafeSEH (Safe Structured Exception Handlers) is a Windows binary protection mechanism for 32-bit executables that has been around for a while now. When the option is enabled, the linker creates a list of valid exception handler addresses in the SEHandlerTable when the binary is being built.

How do I turn off safeseh?

Turn off SAFESEH by opening your project properties, going to Linker > Advanced and setting Image Has Safe Exception Handlers to No.

How do you implement top level exception handling in C++?

C++ try and catch:Exception handling in C++ consists of three keywords: try, throw and catch: The try statement allows you to define a block of code to be tested for errors while it is being executed. The throw keyword throws an exception when a problem is detected, which lets us create a custom error.


2 Answers

For the issue related to /NXCOMPAT and /DYNAMICBASE, I have created a request for the C++ Builder linker to support these flags here: https://quality.embarcadero.com/browse/RSP-13072

Using editbin.exe from Visual C++ is hardly an ideal solution, and their linker needs to support these flags natively.

UPDATE: An additional request has been created here for the C++ Builder / Delphi runtime files (DLLs/BPLs) to be distributed with these flags already set, so as to avoid having to use EDITBIN from Visual C++ to set them yourself: https://quality.embarcadero.com/browse/RSP-13231

like image 21
James Johnston Avatar answered Oct 27 '22 12:10

James Johnston


First, /SafeSEH only applies to x86, not x64 or ARM. It requires that your compiler generate additional tables indicating the function addresses that are considered valid exception handlers for security reasons. There's a slim chance you could do this yourself, but it would require that you look at the fs:0 exception handling chain in your compiled assembly code and enumerate all addresses that are ever pushed on that chain, then describe them as documented here: http://msdn.microsoft.com/en-us/library/9a89h429(v=VS.80).aspx. There's a (slim) chance that your code doesn't actually have any handlers, and they're all in the C++Builder's runtime (might make it easy if the runtime is a separate DLL).

You should try to convince C++Builder to update their compiler to support SafeSEH. It's been around in the Windows platform since XP SP2, and plugs a pretty nasty security hole (exception handler addresses exist on the stack in x86, just waiting for a buffer overflow to put any random address there to be executed)

like image 134
Kevin Frei Avatar answered Oct 27 '22 12:10

Kevin Frei