News for hackers. Stuff that matters.
Re:What is your favorite C programming trick? (2009) by btrask on Sunday November 22, 2020 @11:30AM (#25178708)
Here's a trick that will actually help produce more secure and reliable programs.

    *outArg = myPtr; myPtr = NULL;
    free(aPtr); aPtr = NULL;
Set your pointers to null when you free them! Set them to null when you transfer ownership! Stop leaving dangling pointers everywhere!

Some people say they like dangling pointers because they want their program to crash if something is freed when they don't expect it to be. Good! Do this:

    assert(ptr);
There are also many more tricks you can do once you start nulling pointers. You can use const to mark pointers that you don't own and thus can't free. You can check that buffers are all zero before you free them to catch memory leaks (this requires zeroing out other fields too of course).

Please, null out your pointers and stop writing (most) use-after-free bugs!

Re:What is your favorite C programming trick? (2009) by nn3 on Sunday November 22, 2020 @12:53PM (#25179404)
Better then

#define ZFREE(p) do { free(p); p = NULL; } while(0)

Re:What is your favorite C programming trick? (2009) by okl on Sunday November 22, 2020 @01:03PM (#25179480)
You can even do

  #define free(p) do { free(p); p = NULL; } while(0)
And when you want to call the original free:

  (free)(p);
The preprocessor will not substitute this occurence of free.
Re:What is your favorite C programming trick? (2009) by devenblake on Sunday November 22, 2020 @07:50PM (#25182496)
C n00b here - why the `do {} while(0)`? Couldn't you just do something like `#define free(p) { free(p); p = NULL; }`?
Re:What is your favorite C programming trick? (2009) by deaddodo on Sunday November 22, 2020 @08:21PM (#25182673)
Semi-experienced C user here, I believe the anonymous block is perfectly adequate here. No idea why they are wrapping it in a single instance do loop, unless they’re unaware of block scoping or I’m unaware of some UB here.
Re:What is your favorite C programming trick? (2009) by btrask on Sunday November 22, 2020 @08:29PM (#25182721)
do {} while(0) is a common idiom for macros in C, because it consumes the trailing semicolon, which a bare {} block doesn't do.

    if(x) MACRO();
    else something();
expands to

    if(x) { ... }; // Error!
    else something();
Re:What is your favorite C programming trick? (2009) by btrask on Sunday November 22, 2020 @01:15PM (#25179563)
Here's the actual macro I (sometimes) use:

    #define FREE(ptrptr) do { \
        __typeof__(ptrptr) const __x = (ptrptr); \
        free(*__x); *__x = NULL; \
    } while(0)
There might be a better way of doing it though. Also, __typeof__() obviously isn't standard C.

Edit to add: I've honestly been moving away from using a macro and just putting both statements on one line like in the OP. For something so simple, using a macro seems like overkill.

Re:What is your favorite C programming trick? (2009) by Thorrez on Monday November 23, 2020 @01:36AM (#25184127)
What's the benefit of that over nn3's version?
Re:What is your favorite C programming trick? (2009) by dromtrund on Monday November 23, 2020 @02:24AM (#25184324)
It'll only evaluate the pointer once. It's possible to make this a function though, that might be preferable
Re:What is your favorite C programming trick? (2009) by Thorrez on Monday November 23, 2020 @04:12AM (#25184931)
Good point. But it seems like it would require usage like this:

    int* p = malloc(sizeof(int));
    FREE(&p);
What if we instead define the macro like this:

    #define FREE(ptr) do { \
        __typeof__(ptr)* const __x = &(ptr); \
        free(*__x); *__x = NULL; \
    } while(0)
Then make usage slightly shorter, as well as more similar to free():

    int* p = malloc(sizeof(int));
    FREE(p);
Re:What is your favorite C programming trick? (2009) by btrask on Monday November 23, 2020 @04:52AM (#25185128)
Taking a pointer-to-pointer is intentional to make it clear that the pointer will be modified. That's actually the most important difference from nn3's version IMHO.
Re:What is your favorite C programming trick? (2009) by btrask on Monday November 23, 2020 @03:13AM (#25184598)
I tried making it a plain function at one point but ran into some weirdness around using void * * with certain arguments (const buffers?). You don't want to accept plain void * because it's too easy to pass a pointer instead of a pointer to a pointer. Using a macro is (ironically) more type safe.

Maybe someone else could figure out how to do it properly, since I'd definitely prefer a function.

Re:What is your favorite C programming trick? (2009) by saagarjha on Sunday November 22, 2020 @11:50AM (#25178848)
Your approach requires extra checks, though, which are easy to forget. Also, NULL is not guaranteed to be the stored as zeros, plus padding is going to make your life annoying.
Re:What is your favorite C programming trick? (2009) by btrask on Sunday November 22, 2020 @11:58AM (#25178918)
Well, dangling pointers are also easy to forget... Yes, it requires some discipline. Good code requires discipline, doesn't it?

The trick of checking that buffers are zeroed is purely a debugging tool, so it's okay if it doesn't work on some platforms. And if you allocate with calloc(), the padding will be zeroed for you. It's actually very rare that you will have to call memset() with this technique.

Re:What is your favorite C programming trick? (2009) by saagarjha on Sunday November 22, 2020 @12:05PM (#25178995)
> Good code requires discipline, doesn't it?

This is like the most clichéd way of saying “my code has security vulnerabilities” that there is. I have yet to see code that has remained secure solely on the “discipline” of programmers remembering to check things.

> The trick of checking that buffers are zeroed is purely a debugging tool, so it's okay if it doesn't work on some platforms.

Fair.

> And if you allocate with calloc(), the padding will be zeroed for you.

It might get unzeroed if you work with the memory.

Re:What is your favorite C programming trick? (2009) by btrask on Sunday November 22, 2020 @12:27PM (#25179177)
All code is full of vulnerabilites. If you say your code isn't, then I'm sure it is. I just do the best I can to keep the error rate as low as possible. But it's a rate, and it's never zero.

Also, it's not just about vulns in security-critical code. It's also about ordinary bugs. Why not be a little more careful? It won't hurt.

> It might get unzeroed if you work with the memory.

Maybe, but it isn't very common. I'm not sure when the C standard allows changing padding bytes, but in practice the compilers I've used don't seem to do it. And again, it's just a debugging aid, if it causes too much trouble on some platform, just turn it off.

Re:What is your favorite C programming trick? (2009) by saagarjha on Sunday November 22, 2020 @03:47PM (#25180918)
It’s better to have automatic checks than rely on programmers being careful enough to remember to add them. For padding: this probably happens more on architectures that don’t do unaligned accesses very well.
Re:What is your favorite C programming trick? (2009) by btrask on Sunday November 22, 2020 @04:23PM (#25181232)
Help me out here, because I'm really trying to understand. Are you saying that dangling pointers that blow up if you double-free them is an "automatic check"? If not, what kind of automatic check are you talking about?

If the extra code is really that bothersome, just use a macro or wrapper function.

Re:What is your favorite C programming trick? (2009) by saagarjha on Monday November 23, 2020 @03:22AM (#25184650)
It's a much better situation than NULLing them out, because that hides bugs and makes tools like Address Sanitizer useless. A dangling pointer, when freed, will often throw an assert in your allocator; here's an example of how this looks like on my computer:

  $ clang -x c -
  #include <stdlib.h>
  
  int main(int argc, char **argv) {
      char *foo = malloc(10);
      free(foo);
      free(foo);
  }
  $ ./a.out
  a.out(14391,0x1024dfd40) malloc: *** error for object 0x11fe06a30: pointer being freed was not allocated
  a.out(14391,0x1024dfd40) malloc: *** set a breakpoint in malloc_error_break to debug
  Abort trap
As you turn up your (automatic) checking this will be caught more and more often. Setting the pointer to NULL will silently hide the error as free(NULL) is a no-op and nothing will catch it. Thus, the suggestion here was

1. advocating adding additional code, which has historically been hard to actually do in practice, and

2. providing a suggestion that is generally worse.

Re:What is your favorite C programming trick? (2009) by btrask on Monday November 23, 2020 @04:11AM (#25184924)
Good points, thank you for explaining!

I can see an argument for wrapping it in a macro so you can turn off nulling in debug builds (ASan might even have hooks so you can automate this, I know Valgrind does). But use-after-free is worse than just double-frees, and if you read a dangling pointer in production there's no real way to catch it AFAIK. Last I heard (admittedly been a few years since I checked), you're not supposed to deploy ASan builds because they actually increase the attack surface.

So, your program's memory is full of these dangling pointers, and at some point you will have a bug you didn't catch and use one. And you can't even write an assertion to check that it's valid. What do you propose?

And again to clarify, I'm not trying to advocate for hiding bugs. I want to catch them early (e.g. with assertions), but I also want to avoid reading garbage at runtime at all costs, because that's how programs get pwn'd.

Re:What is your favorite C programming trick? (2009) by saagarjha on Tuesday November 24, 2020 @03:34PM (#25202737)
> But use-after-free is worse than just double-frees

From an exploitability point of view they are largely equivalent.

As for the rest of your comment: my point of view is largely "you should catch these with Address Sanitizer in debug", so I don't usually write code like "I should assert if I marked this as freed by NULLing it out". If I actually need to check this for program logic, then of course I'll add something like this.

The macro you suggest would alleviate my concerns, I suppose, and it wouldn't really be fair for me to shoot that solution down solely because I personally don't like these kinds of assertions in production. So it's not a bad option by any means, other than my top-level comment of this requiring extra code. I know some libraries like to take a pointer-to-a-pointer so they can NULL it out for you, so that is an option for your wrapper. And a double-free that doesn't crash can sometimes open up exploitable bugs too since it messes with program invariants that you didn't expect. But these are much rarer than the typical "attacker controlled uninitialized memory ended up where it shouldn't" so it's not a big deal.

Re:What is your favorite C programming trick? (2009) by btrask on Tuesday November 24, 2020 @06:30PM (#25204268)
Very reasonable! Thank you for the discussion :)
Re:What is your favorite C programming trick? (2009) by okl on Sunday November 22, 2020 @01:32PM (#25179709)
> I have yet to see code that has remained secure solely on the “discipline” of programmers remembering to check things.

That's not what the parent comment said.

Re:What is your favorite C programming trick? (2009) by saagarjha on Sunday November 22, 2020 @03:48PM (#25180928)
I’m not sure what it could have said after saying that programmers should have disciple after I mentioned that their thing required extra checks to work.
Re:What is your favorite C programming trick? (2009) by okl on Sunday November 22, 2020 @04:01PM (#25181062)
Parent said: "Good code requires discipline, doesn't it?"

You retort: "I have yet to see code that has remained secure solely on the “discipline” of programmers remembering to check things."

I think that is a dishonest misrepresentation of what the parent comment said, isn't it?

Re:What is your favorite C programming trick? (2009) by saagarjha on Sunday November 22, 2020 @04:24PM (#25181248)
The “discipline” in this case (see the whole thread) is “have programmers remember to insert checks”, which has historically been a good way to have security holes crop up. So I’m not sure what was dishonest about it?
Re:What is your favorite C programming trick? (2009) by okl on Sunday November 22, 2020 @04:36PM (#25181333)
They argued that discipline is necessary, not sufficient, to produce good code. You represented the argument as: "discipline is sufficient for secure (good) code"

You took the original argument, changed it to be fallacious, and used it as a strawman. That's what was dishonest about it.

Re:What is your favorite C programming trick? (2009) by btrask on Sunday November 22, 2020 @04:45PM (#25181382)
I appreciate you defending me, but I don't think he was trying to be dishonest.
Re:What is your favorite C programming trick? (2009) by btrask on Sunday November 22, 2020 @04:34PM (#25181319)
I don't think that's fair in this case because nulling out pointers isn't the first line of defense. If you forget to do it once, it's not going to cause a bug in and of itself. You can easily grep the code periodically to find any cases you missed.
Re:What is your favorite C programming trick? (2009) by saagarjha on Monday November 23, 2020 @03:31AM (#25184697)
I think that's the misunderstanding, then, because to me it seemed to be a defensive coding practice (I think it was certainly presented as such in the top comment). My "you need extra checks" claim was mostly aimed at the additional things you add on to your code assuming that you are now zeroing out freed pointers, which I think can lead to dangerous situations where you may come to rely on this being done consistently when it's a manual process that is easy to forget.

Left unsaid due to the fact I was out doing groceries this morning when I posted that was that I don't think this is even a very good practice in general, as I explained in more detail in other comments here.

Re:What is your favorite C programming trick? (2009) by Quekid5 on Sunday November 22, 2020 @05:36PM (#25181772)
Indeed, it shouldn't be a first line of defense (nulling + an assert seems reasonable, fwiw), and accessing a nulled out pointer is just as UB as any other UB. It's probably more likely to crash immediately in practice, but it's also easier for an optimizer to "see through", so you may get surprising optimizations if you get it wrong.

Honestly, unless you really cannot afford it time-budget wise, I would just ship everything with ASAN, UBSAN, etc. and deal with the crash reports.

Re:What is your favorite C programming trick? (2009) by saagarjha on Monday November 23, 2020 @03:27AM (#25184676)
Shipping code with Address Sanitizer enabled is generally not advisable; it has fairly high overhead. You should absolutely use it during testing, though!
Re:What is your favorite C programming trick? (2009) by Quekid5 on Friday November 27, 2020 @03:15PM (#25232210)
That's why I added the "afford it" bit.
Re:What is your favorite C programming trick? (2009) by nsajko on Sunday November 22, 2020 @12:05PM (#25178997)
> NULL is not guaranteed to be the stored as zeros

Is that a real issue, though?

> padding is going to make your life annoying

Just memset?

Re:What is your favorite C programming trick? (2009) by huhtenberg on Sunday November 22, 2020 @01:46PM (#25179851)
>> NULL is not guaranteed to be the stored as zeros

> Is that a real issue, though?

Of course, it's not, but that's one of those factoids that everyone learns at some point and feels like needing to rub it into everyone else's face assuming that these poor schmucks are as oblivious to it as they once were. A circle of life and all that.

Re:What is your favorite C programming trick? (2009) by saagarjha on Sunday November 22, 2020 @03:52PM (#25180967)
Forgive me for encouraging the adoption of portable, compliant code to those who may not otherwise be aware of it. If you want to assume all the world’s an x86 that’s great but you should at least know what part of your code is going to be wrong elsewhere.
Re:What is your favorite C programming trick? (2009) by huhtenberg on Sunday November 22, 2020 @04:09PM (#25181136)
Name a single platform where NULL is not 0.
Re:What is your favorite C programming trick? (2009) by tjalfi on Sunday November 22, 2020 @03:24PM (#25180726)
AMD GPUs use a non-zero NULL pointer[0].

[0] https://reviews.llvm.org/D26196

Re:What is your favorite C programming trick? (2009) by nsajko on Sunday November 22, 2020 @05:32PM (#25181743)
Interesting. And it isn't even always non-zero; sometimes it's 32-bit -1, sometimes it's 32-bit 0 and sometimes it's 64-bit 0:

https://llvm.org/docs/AMDGPUUsage.html#address-spaces

Re:What is your favorite C programming trick? (2009) by spc476 on Sunday November 22, 2020 @06:41PM (#25182141)
NULL is required to be stored as all zeros on POSIX systems.
Re:What is your favorite C programming trick? (2009) by annilt on Sunday November 22, 2020 @11:59AM (#25178929)
Please don’t get me wrong but these precautions sound like you are sweeping problems under the carpet which will come out one day back again. It sounds like you have ownership issues in the design and trying to hide ‘possible future bugs’.

Do you use sanitizers for use-after free bugs? I see many people still don’t use them even though sanitizers have become very good in the last 5-6 years

Re:What is your favorite C programming trick? (2009) by btrask on Sunday November 22, 2020 @12:17PM (#25179087)
It's defensive coding. Do you think defensive driving is 'sweeping problems under the carpet'? (It is, but it's still useful...)

I use every tool at my disposal. Sanitizers, static analyzers... and also not leaving dangling pointers in the first place. Why would I do anything less? It doesn't cost anything except a little effort.

Take a look at this recent HN link: https://www.radsix.com/dashboard1/ . Look at all those use-after-free bugs. Even if it only happens 1% or 0.01% of the time... It's a huge class of bugs in C code. Why not take such a simple step?

Re:What is your favorite C programming trick? (2009) by annilt on Sunday November 22, 2020 @12:50PM (#25179373)
If it works for you, then it is okay. It is not ‘a little effort’ for me to worry about someone else might use this pointer mistakenly, so I need to think about that all the time. It shifts my focus from problem solving to preventing future undefined behavior bugs. These bugs in the link, I don’t know C++, it is a big language which does a lot of things automatically, so it is already scary for me :) Maybe that is it, I write C server side code mostly(database) with very well defined ownership rules. Things are a bit more straightforward compared to any c++ project I believe. I just checked again, we don’t have any use-after free bugs in the bug history, probably that is because of %100 branch coverage test suite + fuzzing + sanitizers. So I rather adding another test to the suite than doing defensive programming. It is a personal choice I guess.
Re:What is your favorite C programming trick? (2009) by okl on Sunday November 22, 2020 @01:30PM (#25179693)
Generally, it is considered preferable to find problems as early as possible. If a program fails to compile or quickly crashes (because of a failed assertion), then I consider that better than having to unit test and fuzz test your code to find that particular problem.

As an added benefit the code also becomes more robust in the production environment, if there are use cases you failed to consider -- 100% branch coverage does not guarantee that there are none!

Re:What is your favorite C programming trick? (2009) by dllthomas on Sunday November 22, 2020 @09:32PM (#25183068)
> Generally, it is considered preferable to find problems as early as possible.

Whole heartedly agree.

> If a program fails to compile or quickly crashes (because of a failed assertion), then I consider that better than having to unit test and fuzz test your code to find that particular problem.

This confuses me. My typical order would be:

fails to compile > unit test > quick crash at runtime > slow crash at runtime (fuzzing)

I am curious to understand why we differ there.

Re:What is your favorite C programming trick? (2009) by btrask on Sunday November 22, 2020 @01:01PM (#25179467)
Every problem can be solved in many different ways. If you think you've already got use-after-free bugs under control, then more power to you! You absolutely have to concentrate your effort on whatever your biggest problems are.

But I'll also say that if you don't have any use-after-free bugs in the history of a large C codebase... you might not even be on the lookout for them? I still have them sometimes, mainly when it comes to multiple ownership. And those are just the ones I found eventually.

So yes, different strokes for different folks, but if you make the effort to incorporate tricks like this into your "unconscious" coding style, the ongoing effort is pretty minimal. Even if you decide this trick isn't worth it, there are countless others that you might find worthwhile. I'm always on the lookout for better ways of doing things.

Re:What is your favorite C programming trick? (2009) by annilt on Sunday November 22, 2020 @01:46PM (#25179856)
I meant no use-after-free bugs in production, otherwise we find a lot in development with daily tests etc. but looks like we catch them pretty effectively. It works good for us but doesn’t mean it’ll work for all other projects, so yeah I can imagine myself applying such tricks to a project some time, especially when you jump to another project which has messy code, you become paranoid and start to ‘fix’ possible crash scenarios proactively :)
Re:What is your favorite C programming trick? (2009) by Gibbon1 on Sunday November 22, 2020 @05:47PM (#25181852)
Big reason for defensive coding like nulling pointers is to make the code fail hard when someone messes up when they make a change. One can imagine the sort of hell unleashed if later the code is changed to make use of a dangling pointer. That's often the type of bug that slips through testing and ends up causing rare unexplained crashes/corruption in shipped code. Worse it can take multiple iterations of changes to finally expose the bug.
Re:What is your favorite C programming trick? (2009) by saagarjha on Monday November 23, 2020 @03:25AM (#25184665)
This makes UAF easier to detect but double-free impossible to detect. I would consider that to be worse than not doing anything at all, especially since it isn't amenable to modern tooling that is much better at catching these issues than hand-rolled defensive coding.