r/ProgrammingLanguages • • Oct 14 '26

Improved article about memory safety

Thank you to everyone who gave feedback on my article about memory safety.

I used the input to vastly improve the article (see "Achieving memory safety").

Please take a look and share your thoughts again and consider starring the Seed7 project on GitHub.

13 Upvotes

11 comments sorted by

View all comments

Show parent comment

4

u/Athas Futhark Oct 15 '26

Protecting a program against reading its own memory via /proc needs a different strategy.

This is impossible to do. You need to rethink the definition of memory safety, because if you include arbitrary IO, then there is no way to avoid arbitrary system-level actions. What would prevent an IO function from launching another process, perhaps a shell script, to do whatever you are trying to prevent in your library? I see that Seed7 has a Shell library, so this is straightforward today. There is also /dev, which contains ways of modifying system memory if the process is privileged.

These are not Seed7-specific concerns. Any memory safe language that supports IO must be careful to specify what they mean by memory safety, and my guess (it's not my field) is that it's usually a guarantee "modulo external effects caused by IO", or something like that.

1

u/ThomasMertes Oct 15 '26 edited Oct 15 '26

Protecting a program against reading its own memory via /proc needs a different strategy.

This is impossible to do.

Why?

As far as I know a process that uses prctl(PR_SET_DUMPABLE, 0); cannot read its own memory via the /proc filesystem. And the PR_SET_DUMPABLE flag is inherited to child processes so they can't access the memory of their parent either. So launching another process or a shell script will not help.

What a privileged process can do is something different. Keep in mind that memory safety weaknesses are usually exploited to get superuser rights. If you are already superuser you have all power to do dangerous things.

These are not Seed7-specific concerns.

Yes. This is something that the operating system needs to fix, since this memory safety loophole is provided by it. prctl(PR_SET_DUMPABLE, 0); comes quite near to a fix, but it prevents debugging and maybe other useful things. What is needed from the operating system is something that prevents any access to the own process memory and also prevents any child process to do so (this is what prctl(PR_SET_DUMPABLE, 0); does) and additionally allows debuggers to read the memory (since the debugger started the process).

Hopefully Linux will do its homework. :-)

modulo external effects caused by IO

I will think over such a refinement of my definition of memory safety. Loopholes provided by the OS are definitely a thing.

2

u/Athas Futhark Oct 15 '26

Why?

I am not a security expert, so a specific analysis is beyond my ken. However, the basic problem is that the Unix security model is not intended to protect processes from other processes running as the same user. This means that if you take a sufficiently broad view of "memory safety" (broader than any other language), then performing IO (or in particular launching other processes) becomes a problem. I think the right solution is to not use such a broad definition of memory safety.

What a privileged process can do is something different.

In some sense it is more important that root processes are safe, because they can compromise the entire system. This matters only on servers of course; on personal machines the normal user account has access to all data that matters. Also, various common container techniques (like Docker) often run processes as the root user, and it would be strange if a language security guarantee depends on what user ID it runs under.

Yes. This is something that the operating system needs to fix, since this memory safety loophole is provided by it.

They will not fix it, since your definition of memory safety is not shared by other languages, and the security properties you expect are intentionally not part of the security model guaranteed by operating systems. I'm not a Windows expert, but since you can inject threads into processes with the same user, I'm sure you can easily break memory safety that way if you keep this extremely strict definition of memory safety.

1

u/ThomasMertes Oct 16 '26

I just added the chapter "A loophole introduced by operating systems" to the "Achieving memory safety" article.

What do you think about that?