Is it safe to save static data in the .data segment

From the beginning, I want to note that I know that all programs have their own virtual addresses.

I read somewhere that OS randomizes all address spaces, static variables are always stored in the same address. In theory, an attacker may try to access it. Is it correct that the str type is stored as a static variable?

Your post contains a bunch of unrelated questions. Can you elaborate and rephrase it more coherently? I don't understand what you think can go wrong (?).

...and some unsubstantiated assumptions. Some references would also be welcome.

Whatever you read is probably referring not to static variables but to the static relocation model, where programs use absolute, fixed addresses to refer to functions and variables, and the program data cannot be relocated with ASLR (address space layout randomization). In Rust, this is set with the -C relocation-model codegen option in the compiler. By default, no programs use the static relocation model (compiled for a typical OS), but instead position-independent code, so even static variables and &'static str data will be moved by ASLR between executions. So this is not something you have to worry about unless you start messing with the RUSTFLAGS.

3 Likes

However, I don't quite understand such advanced topics, and the idea for the question came to mind spontaneous, thank you for answering my really unfinished question, it explains everything

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.