opentitan icon indicating copy to clipboard operation
opentitan copied to clipboard

[otbn] Evaluate the removal of START_ADDR

Open imphil opened this issue 6 years ago • 9 comments

Currently, OTBN has the ability to set an arbitrary entry point into the code through the START_ADDR register (https://docs.opentitan.org/hw/ip/otbn/doc/index.html#Reg_start_addr). This entry point adds an additional control flow feature which is easy to miss, tricky to analyze (since it semantics different from other control flow), and potentially not needed.

As alternative solution, OTBN could always start execution at address 0, and software could have a jump table/reset vector there that deviates control flow based on a word in DMEM.

  • [x] Modify one software application (e.g. RSA) to check feasibility in software.
  • [x] Discuss and evaluate security impact.
  • [ ] If agreed, move forward with spec change.
  • [ ] Implement removal in RTL, ISS and software

estimate 6 remaining 2021-09-20 4

imphil avatar Nov 16 '20 10:11 imphil

To explore further what we'd end up with if we got rid of START_ADDR (and always start execution at address 0x0), I modified the rsa.s example code to add a main routine at address 0. Here's how it could look in assembly. The code creates a calling convention where we store the function to be executed at address 0x0. That's arbitrary for now.

.text

.globl FUNC_RSA_ENCRYPT
.set FUNC_RSA_ENCRYPT, 1

.globl FUNC_RSA_DECRYPT
.set FUNC_RSA_DECRYPT, 2

.globl main
main:
  lui     x7,%hi(func)
  lw      x6,%lo(func)(x7)

  addi    x7,x0,FUNC_RSA_ENCRYPT
  beq     x6,x7,rsa_encrypt

  addi    x7,x0,FUNC_RSA_DECRYPT
  beq     x6,x7,rsa_decrypt

rsa_encrypt:
  jal      x1, modload
  jal      x1, modexp_65537
  ecall

rsa_decrypt:
  jal      x1, modload
  jal      x1, modexp
  ecall


.data

/* Operation to be executed. See FUNC_* constants for values. */
.globl func
func:
  .word 0x00000000

The complete code example, including a prototype of the changes required to Ibex software, is available at https://github.com/imphil/opentitan/commit/ebda95e49bd18b9ce1a19e0e920b97835a4eaa22 (it builds on top of the yet unmerged RSA end-to-end example).

Here are my thoughts:

  • Performance: We already have quite self-contained algorithms. Removing START_ADDR will cost us a couple instructions for the main() function, but it's negligible compared to the whole runtime.
  • Security: Not having potentially arbitrary entry points into our OTBN code enables us to reason about control flow by looking at the assembly code only, with should make formal/static analysis much easier.
  • Security: The example code uses trivial constants to select operations (1 and 2), but of course we can choose constants with greater Hamming distance to make glitches into the wrong subroutine harder. We can't do so if we pass the start address directly.
  • By exporting the FUNC_* constants from the assembly code we can use them in Ibex software and can call functions in a similar way as before (all constants are defined in one place and used appropriately in both assembly and Ibex software).

To summarize, I don't see a reason why we shouldn't go ahead and remove START_ADDR; it's a simplification to OTBN, and a win for security.

@felixmiller @cdgori @moidx @tjaychen Do you have further thoughts?

imphil avatar Nov 23 '20 17:11 imphil

That looks good to me. There should probably also be an "oh no!" case at the bottom of main for if nothing matches but, yep, looks like it will work. I like the exported symbols!

rswarbrick avatar Nov 23 '20 17:11 rswarbrick

Ibex is still trusted to load code on OTBN, so TCB wise no changes here.

If all the PKA functions don't fit into IMEM, I assume we can break them down into separate programs.

@imphil I have a few follow up questions to try to parse the proposal in more detail:

  • What would replace START_ADDR? I expect Ibex to be able to start OTBN execution with an expected deadline.
  • From a perturbation perspective, we can claim we are mitigating a glitch point by removing START_ADDR, but we are adding new potential points in the new branching code. What are the tradeoffs from a FI perspective?
  • Moreover, by adding additional compare/branches you are claiming that it is easier to statically analyze the assembly, why isn't this the same as just tagging the API, and have each entry function handle the control flow state initialization?

moidx avatar Nov 25 '20 00:11 moidx

What would replace START_ADDR?

Instead of calling an arbitrary function (more accurately: program counter) in OTBN code by setting the START_ADDR register, we would now always call a main() function (at address 0x0), which then branches out to the right address. The branch decision (which function we want to call) comes from a word in data memory, which is written by Ibex before starting the routine. The example code shows that.

I expect Ibex to be able to start OTBN execution with an expected deadline.

I'm not sure how the start address is related to execution deadlines. Ibex was and continues to be able to start OTBN executions. If a watchdog/execution deadline is required Ibex can continue to implement that either using a busy wait + counter, or a timer interrupt.

From a perturbation perspective, we can claim we are mitigating a glitch point by removing START_ADDR, but we are adding new potential points in the new branching code. What are the tradeoffs from a FI perspective?

My view on it: by removing START_ADDR we reduce the attack surface by removing one glitch point. We already rely on and need to harden "normal" branches. We now use this same machinery, and hence the same hardening we already have, for one more purpose. Of course, we have now more branches in the code, but I don't think those initial branches to choose a function (encrypt or decrypt) are more at risk than other branches (e.g. choose the number of rounds) within OTBN software. @felixmiller what's your take on that?

Moreover, by adding additional compare/branches you are claiming that it is easier to statically analyze the assembly, why isn't this the same as just tagging the API, and have each entry function handle the control flow state initialization?

To analyze OTBN software currently, you need to effectively create one control flow graph per entry point, and (depending on the type of analysis) overlay them. (Or create an artificial main() function which does the initial branching to the chosen subroutine.) Additionally, we need to ensure in hardware that only those entry points can be called, i.e. limit allowed values in START_ADDR to the permitted entry points. (A CFI implementation is likely to be able to ensure that.) While keeping START_ADDR and statically analyzing OTBN code is clearly possible, it is more effort than analyzing the OTBN software standalone with the assumption "execution always starts at address 0x0".

imphil avatar Nov 25 '20 11:11 imphil

Hi @imphil,

I am still not convinced that adding more code at runtime to simplify static analysis that happens at build time is a good tradeoff. As I said in the previous response, here the tradeoff is attacking START_ADDR versus the new code, and I want to see an analysis of the tradeoffs there.

Yes, you need to ensure that only those entry points are called, but that can be done in many different ways, without making things complex. As I said earlier this change does not change the TCB, so Ibex is still trusted to load the code and call the main or entry functions...

moidx avatar Nov 25 '20 16:11 moidx

I have to admit I'm also struggling to understand what the justification for this change is. It doesn't seem motivated by clearly established facts. There's some speculation about glitch resistance, but I don't really see how this is better than what exists today.

Things like static analysis and formal methods are speculative research. Nothing for production relies on them. And, as has been pointed out the TCB remains the same. So there's no clear security win here.

What I do see that worries me in this change is insertion of an addt'l branch point and clustering all the branches together in a vector table. It seems as though this actually adds complexity, and I'm struggling to understand how it doesn't without the clear articulation of tradeoffs that @moidx is asking about.

domrizz0 avatar Nov 25 '20 17:11 domrizz0

Pushing this item into the backlog until we have more details on the CFI implementation plan.

imphil avatar Dec 18 '20 17:12 imphil

We discussed this in the Security Meeting on 19th August. All sides were happy to remove the feature.

rswarbrick avatar Aug 23 '21 13:08 rswarbrick

RSA example converted and I have local changes that rip out the functionality from the RTL and ISS. Still to do: tidy up and push.

rswarbrick avatar Sep 20 '21 10:09 rswarbrick