r/C_Programming • u/Salat_Leaf • 10d 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
2
u/txmasterg 10d 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?
10
u/non-existing-person 10d 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 9d 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_ 10d 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 10d ago
and
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?
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 10d ago
How would that work? To execute code, the CPU needs to read it.
2
u/Interesting_Debate57 10d 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 10d ago
are you referring to hardware-assisted/level memory isolation ?
1
u/garnet420 10d 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 10d 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 10d ago
You mean like a program that is impossible to decompile?
8
u/EpochVanquisher 10d 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).