158 points · by alexggordon · 11y ago · 88 comments
"Also in his Turing Award lecture, he described how he had incorporated a backdoor security hole in the original UNIX C compiler. To do this, the C compiler recognized when it was recompiling itself and the UNIX login program. When it recompiled itself, it modified the compiler so the compiler backdoor was included. When it recompiled the UNIX login program, the login program would allow Thompson to always be able to log in using a fixed set of credentials."
I'd be surprised if these changes haven't made it into FreeBSD, but afaik Linux doesn't work this way (by default, anyway).
And of course, some programs use replacements like dlmalloc and do all their own allocation management anyway.
Yeah. I wrote my own allocator in C++ a long time ago. I wouldn't be surprised if there weren't quite a few other bits of software out there doing the same thing.
Do you have a URL to a git ... hahaha, pardon me, CVS repo?
And some context around malloc and the various techniques OpenBSD implements to make exploitation of bugs more difficult: http://www.openbsd.org/papers/dev-sw-hostile-env.html
The tl;dr is : if your code compiles and runs on OpenBSD, chances are it will fine on other Unices. The opposite is not necessarily true though.
I would argue every language has that property. But with C/C++ being so closely tied to the ABI of the machine perhaps they are more underhanded than others. But to me, this branding does feel a bit unfair.
Still, a fun contest and an interesting read.
Do not attempt to write secure software with C / C++. It is a Bad Idea(TM). Because what you wrote is not what gets run.
For instance: there is no way to write a portable secure memset in portable C / C++. (You can write a "secure" memset that works in all current compilers, but that is not the same thing. What doesn't get optimized now can and will be optimized tomorrow.)
It's dependent on the function attributes of memset (e.g. __attribute__((pure))) - it won't always be optimized out.
https://www.cs.auckland.ac.nz/references/c/gcc4.7/Function-A...
Of course there is, you just use the volatile keyword. volatile guarantees that all read/writes have corresponding memory accesses and cannot be optimized away.
It's not going to be as fast as memset but it's definitely portable and it won't be THAT slow. Then for platforms that have memset_s defer to that instead, otherwise fallback to the totally portable volatile + for loop.
I think some systems have a "secure memset" function that can be used for things like this - i.e. one that's guaranteed not to be optimized out.
There is, quite literally, no way to ensure data is not leaked (namely, that data is zeroed / etc) in portable C / C++.
The compiler would just optimize that set right back out.
memcpy(filter->buffer, output->piu_text_utf8, sizeof(output->piu_text_utf8));
1. memcpy is less safe than memmove and strncpy. strncpy should be used.2. The two character arrays should use the same constant in defining their length, and that constant should be used both in the struct definitions and here in the copy operation.
3. The code is written in C in spite of it being 2014 at the time.