r/C_Programming • • 18d ago

Question How to manipulate read-restricted data (possibly without OS)?

Imagine you have some block of code/data chunk that under no circumstances must never ever get read (rewrites are not critical, since the data is basically gone). There's a thing about Execute only memory, but afaik it requires OS support.

So how do we make sure some of the data is only overriden or executed (if interpreted as machine code) but never read? If possible, it would be nice to be able to set up some custom restrictions like a very specific offset in a program which has access to some data in memory for a one-time constant address read (imagine it compiles into assembly, something like mov %(rax), %rax, then does side-channel secure stuff and overrides the data with rdrand and stuff)

Appreciate any help! which doesn't include redditor behavior in its body

7 Upvotes

22 comments sorted by

2

u/txmasterg 18d ago

The important thing is what do you want to do with the data which depends on what problem you are trying to solve.

Are you trying to defend against hardware attacks, cross-process and or cross-user attacks? Won't a program need to be able to temporarily read the data? Would encrypting it in memory when not in use be good enough? Are you only wanting to do calculations on the data but don't need to use the results on the same machine? If so look up homomorphic encryption or trusted computing.

Or is this post more about academic curiosity?

2

u/Salat_Leaf 18d ago edited 18d ago

Well, I need to send some data to some website, get a response and send this response to the destination. Both unencrypted request and response must not be read

0

u/[deleted] 18d ago

[deleted] — view deleted comment

1

u/Salat_Leaf 18d ago

Thanks for the detailed elaboration!

10

u/non-existing-person 18d ago

This can be done, but requires hardware support in CPU. NXP has encrypted extension for XIP (https://mcuxpresso.nxp.com/mcuxsdk/25.03.00/html/examples/ota_examples/_docs/encrypted_xip_readme.html)

So you can have encrypted code on the flash, which is decrypted on the fly in secure cpu memory.

Afaik, there is not way to do it on amd64 platform.

4

u/gm310509 18d ago

This sounds a bit like an X-Y Problem.

You could, for example, upload the information into the flash memory of a micro controller and set the "Write Only" fuse bit.

But if you did that, then no one - not even you - can access it. The purpose of this is to protect code that is uploaded into the MCU from being extracted and reverse engineered - the CPU can read it for the purposes of running it, but the hardware prevents it from being read back through the "programming interface".

FWIW, to upload new code, you have to reset the fuse bit and the only way to reset the fuse bit is via a complete memory erase operation.

This doesn't sound like what you are looking for but I think it satisfies your stated question - hence my thinking that this is an X-Y Problem. What is it your are trying to achieve?

3

u/grigus_ 18d ago

as far as I know, in C there's no mechanism of data reading protection. it is possible only with hardware aid, MMU. in ARM machines, there is an exception called data abort, but involves hardware support for it. in x86, needs MMU proper configuration and OS collaboration. in bare metal, without MMU and/or OS support, just plain C, I don't see any solution

5

u/d1722825 18d ago

that under no circumstances must never ever get read

and

I don't care about reverse engineering,

If nobody ever need to read that data back, just don't store it... (or clear it after used)

Could you elaborate a bit more, who or when that data should be read and against what type of attacks do you want to protect it?

Might this be an X-Y problem?

I just have a specific chunk of memory (in RAM) which might contain sensitive data and it's extremely important to make sure it's untouched even if some other program tries to read it

Modern operating systems prevents other processes (or other users) to access the memory of your process (but that could be circumvented if you have root privileges or you (and not other users) attach a debugger to your process.

There are some options to strengthen that protections, but root can undo all of those. Windows probably have similar functionality, too, but I'm not familiar with it. You might be interested in these:

https://lwn.net/Articles/804658/

https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/7/html/selinux_users_and_administrators_guide/sect-security-enhanced_linux-working_with_selinux-disable_ptrace

https://lwn.net/Articles/865256/

https://en.wikipedia.org/wiki/TRESOR

Some AMD CPUs support the transparent encryption of the RAM per application basis, so other software running on the same machine would just see encrypted data. But I don't know much support does it have in the real world.

https://docs.amd.com/api/khub/documents/ZcsCCmeL80dbtuf_VlGpvw/content

1

u/kabekew 18d ago

How would that work? To execute code, the CPU needs to read it.

1

u/Salat_Leaf 18d ago

Well, sort of like a pointer which you cannot dereference, maybe? Or a register which you can only read on certain condition (e.g. some CPU flag is set)

2

u/Interesting_Debate57 18d ago

I haven't carefully thought this through, and it wouldn't work if you need nanosecond decision making, but there are applications in error-correcting codes and the relevant math that can help with things similar to what you're describing. You can DM me.

1

u/Critical-Echo-923 18d ago

are you referring to hardware-assisted/level memory isolation ?

1

u/garnet420 18d ago

If you don't have OS support, you'll need to have some kind of hardware support for this. These days, most microcontroller platforms have some sort of security features you can use.

1

u/Daveinatx 18d ago

It all depends on how secure you need security. Your best bet is to encrypt code in RAM behind an FPGA, such as key authentication. For execution, x86 GDT has the ability to execute-only by disabling the readable bit. That said, that said, it won't be bullet-proof without disabling JTAG debugging, caching, and reducing the system to a single uP. Even with all this, it isn't fully secured if an attacker has physical access.

Edit: OP-TEE is good for MPSoCs and US+.

0

u/onlyonequickquestion 18d ago

You mean like a program that is impossible to decompile? 

2

u/Salat_Leaf 18d ago

No, I don't care about reverse engineering, I just have a specific chunk of memory (in RAM) which might contain sensitive data and it's extremely important to make sure it's untouched even if some other program tries to read it (in case there's something funny going on with jump overrides)

1

u/onlyonequickquestion 18d ago

I was googling it because I'd never really thought about it before, by the time I got back, looks like others have answered. I found Intel sgx might work? Most solutions seemed hardware dependent though. Sounds like some other people have more experience and will be able to give you a better answer. Good luck, sounds like a fun problem to crack. 

2

u/Salat_Leaf 18d ago

Still appreciate the help regardless!

2

u/Interesting_Debate57 18d ago

The owner of the data should cryptographically sign the data using a public key encryption system and then you can let basically anyone read it, and enforce the thing you're worried about (not sharing the information) via key control.

If this is in hardware that means that there's a special USB key required to be plugged in, otherwise you'll just get random garbage when you try to use the read.

Yes that means that the authorized party has to do 10x the normal amount of effort, but if it's hyper-critical to keep the data cold, this is a good way.

0

u/[deleted] 18d ago

[deleted] — view deleted comment

1

u/Interesting_Debate57 17d ago

Encrypt and sign, I think.

0

u/[deleted] 17d ago

[deleted] — view deleted comment

1

u/Salat_Leaf 17d ago

There's that minor window of vulnerability when I have to decrypt the data itself from custom AEAD scheme before encrypting it again into TLS-compatible profile for e.g. HTTPS. I wanma make it impossible to read even if somebody wants to via the host CPU/process/system

0

u/[deleted] 17d ago

[deleted] — view deleted comment

1

u/Salat_Leaf 17d ago

Alrighty, thx!

8

u/EpochVanquisher 18d ago

The basic way you do this is with a “trusted execution environment”

https://en.wikipedia.org/wiki/Trusted_execution_environment

it requires hardware support, and you need special privileges to make something like this work. You can use it for things like encryption keys, where the TEE contains the key, and the key never leaves the TEE. You can send data in and get decrypted / encrypted data out.

This design is really common. It's available on ordinary desktop/laptop computers, phones, and servers. But it may be a massive amount of work to use it, and these TEE are designed to prevent you from messing with them (some of them won't even let you run your own code in them).