|
|
Log in / Subscribe / Register

Memory-safe C code

Memory-safe C code

Posted Nov 16, 2025 19:36 UTC (Sun) by alx.manpages (subscriber, #145117)
In reply to: Memory-safe C code by khim
Parent article: Rust in Android: move fast and fix things (Google Security Blog)

> Anything that doesn't handle memory allocations would be “a minor thing”.

Agree. Why did you assume it wouldn't handle memory allocations? :-)

When I said any buffer overflows, I actually meant it. No buffer overflows even in malloc(3)'d memory.

> That would be a good thing, definitely… but to achieve similar reduction in the bug rate you need to handle memory allocations, somehow.
>
> That's the biggest problem and biggest pay-off.

Yup.


to post comments

Memory-safe C code

Posted Nov 16, 2025 20:26 UTC (Sun) by khim (subscriber, #9252) [Link] (4 responses)

> Agree. Why did you assume it wouldn't handle memory allocations? :-)

Because these are, essentially, impossible to track without extra viral markup everywhere. Even a simple call to strstr leaves you with a dilemma of validness of returned pointer (is its validity tied to needle or to haystack?) and, as an added bonus, it also gives you a write access to the buffer than sometimes could and sometimes couldn't be modified.

These problems are not impossible to track… but getting people to add (and correctly add!) such markup sounds like a more-or-less impossible thing to do.

Essentially: it's hard but possible to add all these things to the language. But after you'll do that you'll need to rewrite almost everything anyway… and if you are doing it why not pick a nicer, more modern, language?

Memory-safe C code

Posted Nov 16, 2025 20:48 UTC (Sun) by alx.manpages (subscriber, #145117) [Link] (3 responses)

> [strstr] gives you a write access to the buffer than sometimes could and sometimes couldn't be modified.

That was solved already in C23. strstr(3) now returns QChar*, which is as qualified as the input.

<https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf>

> Even a simple call to strstr leaves you with a dilemma of validness of returned pointer (is its validity tied to needle or to haystack?)

I don't understand what you mean by 'validness'. I assume you mean provenance?

If so, I agree that GCC should add an attribute such as [[gnu::returns_derived_ptr()]], which would add that information to the API. There exist similar attributes already, so this wouldn't be very difficult.

[[gnu::returns_derived_ptr(1)]]
[[gnu::null_terminated_string_arg(1)]]
[[gnu::null_terminated_string_arg(2)]]
char *strstr(const char *haystack, const char *needle);

> Because these are, essentially, impossible to track without extra viral markup everywhere.

Yes, the attribute would fall under that "viral markup". libc is already full of those things. And I also use many of them in my own code.

> These problems are not impossible to track… but getting people to add (and correctly add!) such markup sounds like a more-or-less impossible thing to do.

If you show them a C code base that is as safe as Rust in terms of buffer overflows, and adding some attributes in some function prototypes is the only thing that needs to be done, I hope some programmers can be convinced.

About doing it correctly, the compiler should be able to tell if it's wrong when seeing the implementation. If the pointer isn't derived from the first argument in some path, it would of course diagnose.

> Essentially: it's hard but possible to add all these things to the language. But after you'll do that you'll need to rewrite almost everything anyway… and if you are doing it why not pick a nicer, more modern, language?

There's a difference between adding attributes to function prototypes and entirely rewriting the code.
These improvements can be done piecemeal, verifying that each change is self-correct, resulting in minimal logic bugs in the rewrite, compared to writing from scratch.

Memory-safe C code

Posted Nov 16, 2025 21:22 UTC (Sun) by khim (subscriber, #9252) [Link] (2 responses)

> I don't understand what you mean by 'validness'. I assume you mean provenance?

I mean: how would your compiler know whether to accept the program or reject it. Rust uses lifetimes for that and that use, in turn, means that it needs to mark them on all the functions that return references and then on structures that may carry references and then on pointers to functions (because now function that receives pointer to first element and function that returns pointer to the second element are different) then you need borrow checker and so on.

It's huuuuge can of worms to do that properly.

> If so, I agree that GCC should add an attribute such as [[gnu::returns_derived_ptr()]], which would add that information to the API. There exist similar attributes already, so this wouldn't be very difficult.

Bwahaha. Try it and you'll find hundreds of use-cases that seem “natural” yet are rejected by a naïve validator.

Or, better yet, you may read papers that describe these things. You'll need borrows, reborrows, split borrows and bazillion other things to make it ergonomic. And then, after all that… you would still need to rewrite all code to ensure that complies with these rules.

You would probably need to spend 5 to 10 years to just plug all the loopholes in your typesystem… and “let's rewrite it in Rust” guys wouldn't wait all that time thumbing their fingers.

> That was solved already in C23. strstr(3) now returns QChar*, which is as qualified as the input.

Typical story of disruptive technology: it's really nice to know that C have done first two steps (out of hundred) needed to achieve memory safety… but would anyone still care 10 (or 20 or 30…) years down the road when it would finally traverse it and provide something that actually works? We would see, but past history is not encouraging.

It's like Bucyrus: it understood that hydraulics is the future very early, tried to build something that his customers would buy for half century… but their customers had no need for underpowered hydroshovel and the ones who needed it knew hydroshovels are made by Caterpillar, not by Bucyrus… thus the whole thing ended with Caterpillar buying them, after half-century. Similarly here: even if after 5-10-20-30 years all that stuff would be finished… you'll find no users for it: people who are willing and capable of rewriting things would be using Rust by then, and the remaining guys would continue to use C-as-it-exist-today, because they just don't want or couldn't rewrite everything.

> If you show them a C code base that is as safe as Rust in terms of buffer overflows, and adding some attributes in some function prototypes is the only thing that needs to be done

Nope, that's precisely the issue: that's just a tiny part of “what needs to be done”. Even such a simple structure as a doubly-linked list is more-or-less impossible to describe in these terms. Rust solution is to give you an unsafe-based LinkedList that you are supposed to use in place of your ad-hoc lists.

For many C codebases this would imply more-or-less total rewrite.

> There's a difference between adding attributes to function prototypes and entirely rewriting the code.

Yes. And for small, really small percentage of C code the former approach may work. For the majority the latter is strict requirement. As in: more-or-less full rewrite. Because too many data structures couldn't be handled with a markup. And to ensure that you code is safe you need small amount of well-designed and tested implementations used by everything… which is precisely the worst side of C/C++ in practice: practically everyone creates their own ad-hoc data structures instead of reusing them.

Worse yet: benefits for the codebases that belong to the latter category (just add markup and everything works) is negligible (they, usually, don't have any memory bugs to begin with because if lifetime story is so simple that you can trivially describe to the compiler then you can describe it to the fellow developer, too), while for codebases where you really want to make it approach work (because they have very complicate story about liveness of the variables in their codebase) you need a rewrite.

So your improvements to C would benefit projects that barely need them yet would leave unfixed bugs in complicated codebases that suffer from memory unsafety worst.

Memory-safe C code

Posted Nov 18, 2025 0:23 UTC (Tue) by alx.manpages (subscriber, #145117) [Link] (1 responses)

> Bwahaha.

Please, moderate yourself. This is quite inappropriate.

Memory-safe C code

Posted Nov 18, 2025 0:45 UTC (Tue) by khim (subscriber, #9252) [Link]

Well… you are proposing to add something that took more than a decade of development, that both including people with PhD and was deemed novel enough and complicated enough to warrant new PhD and professor's position in a prestigious college and you call all that some more work needed to make it perfectly safe?

I honestly don't know whether to laugh at naïvety or applaud the audacity… developing rules that would be both airtight and ergonomic to use is hard… and given the fact that you still need to rewrite more-or-less all the code after you'll do it… adoption would be EVEN HARDER.

Don't get me wrong: if you would manage to retrofit enough markup in C to make it memory safe and then manage to convince people to actually adopt the result (the harder part)… it would be huge achievement, gigantic achievement, awe-worthy achievement, but… I don't hold my breath. Chances of that happening are almost zero.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds