As of version 8 (later backported to 7.3), GCC has added retpoline support [0]. While I understand that it is intended[citation needed] for use in kernel patching for Spectre (ie: [1][2]), that does not prevent normal developers from using those flags.

As such, should I use the flags for retpoline (-mindirect-branch, -mfunction-return, and related)?

For example, I have written a cryptographically-secure password generation program in C [3]. When I compile using -mindirect-branch=thunk -mfunction-return=thunk, the resulting binary is different than without using those flags, presumably from the switch between call and jump to call and return thunk. Is there any reason for doing this, or any benefits from a security perspective?

  • 1
    I believe it is acceptable and recommended to use it for userspace, at least when the performance impact is acceptable. – forest May 21 '18 at 4:47
  • Answer at StackOverflow. It will introduce a performance hit in the program, and since the OS is fixed compiling user code with the switches should be unnecessary (but possible). – Rob Nov 22 '18 at 17:01

There is a benefit from doing this. These options enabled retpoline to mitigate Spectre V2. This is important in programs which handle sensitive or confidential data where the variable performance impact is not a concern. The binary is different because return trampolines are being added to the code.

The second flag, -mfunction-return, is required on Skylake+ processors as they are additionally vulnerable to attacks against the branch target buffer (BTB) that return prediction may use as a fallback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.