News for hackers. Stuff that matters.
Re:Ode to the use-after-free: one vulnerable function, a tho... by ktRolster on Monday May 08, 2017 @12:47PM (#14293286)
A simple way to approach this problem in C, that will work in most cases, is something like this:

   void safer_free(void **x) {
      if(x!=NULL && *x!=NULL) {
         free(*x);
      }
      *x = NULL;
   }
the key part being to set the original pointer to NULL so a future use will cause an exception, not a vulnerability.
Re:Ode to the use-after-free: one vulnerable function, a tho... by panic on Monday May 08, 2017 @02:26PM (#14294224)
Minor pedantic note: the *x!=NULL check is unnecessary. Calling free(NULL) is specified to do nothing.
Re:Ode to the use-after-free: one vulnerable function, a tho... by TheSoftwareGuy on Monday May 08, 2017 @01:06PM (#14293467)
Except this won't work when multiple pointers exist to the same data.. which is arguably when this kind of stuff is most difficult
Re:Ode to the use-after-free: one vulnerable function, a tho... by lossolo on Monday May 08, 2017 @01:40PM (#14293781)
If you have multiple pointers to the same data with different lifetimes you already need some kind of reference counting or garbage collection.
Re:Ode to the use-after-free: one vulnerable function, a tho... by pornel on Monday May 08, 2017 @02:05PM (#14294038)
Not necessarily. It's safe if lifetimes of the other pointers are guaranteed to be shorter than the lifetime of the object.
Re:Ode to the use-after-free: one vulnerable function, a tho... by lossolo on Monday May 08, 2017 @02:50PM (#14294411)
That's why I wrote "with different lifetimes".
Re:Ode to the use-after-free: one vulnerable function, a tho... by ktRolster on Monday May 08, 2017 @02:44PM (#14294367)
That's why "safer" :)

It works in the vast majority of cases. In the rare other cases, you should spend some extra thought on your pointer usage to make sure you have a set of abstractions that let a human reason about the memory sensibly (either that or an insane number of test cases to catch any problem). Someone else mentioned reference counting, and that can be a good idea.

The bottom line is you need to do something to make sure you're not doing use-after-free, and it's mostly not difficult.

Re:Ode to the use-after-free: one vulnerable function, a tho... by marcosdumay on Monday May 08, 2017 @06:00PM (#14296028)
It works on most cases that people normally get right, and is useless on most cases that people normally get wrong.

Overall, it's not useless, but it's really not worth modifying the language for it.

Re:Ode to the use-after-free: one vulnerable function, a tho... by shandor on Monday May 08, 2017 @04:28PM (#14295297)
I was just going to post a comment asking why your proposed way is not the standard way. Surely free is (usually) not called that often that the overhead would be anything close to unbearable? This seems like a so simple method of handling most of the cases, as you point out, that I feel I'm missing some obvious downside.
Re:Ode to the use-after-free: one vulnerable function, a tho... by to3m on Monday May 08, 2017 @05:30PM (#14295792)
There is no generic pointer-to-pointer in C. You get an incompatible-pointer-types warning in gcc when trying to use void * * : https://godbolt.org/g/vxWFvl

There is probably scope for use of C11 _Generic, but I don't think you could support user-defined types in that way.

The usual macro problems apply when trying to do it as a macro:

    #define FREE(X) (free(X),(X)=NULL,(void)0)
In C++ you can do something like this, which works well enough: (I don't use this specifically myself, but I have something similar for COM objects)

    template<class T>
    static void Free(T *&p) {
        free(p);
        p=nullptr;
    }
This isn't completely useless, but it's not worth sweating blood over. Personally I think I've had more mileage from good-quality debug functionality in the memory allocator: fill newly-allocated and newly-freed regions with junk; check junk and metadata periodically; unmap regions promptly; and so on.
Re:Ode to the use-after-free: one vulnerable function, a tho... by ktRolster on Monday May 08, 2017 @05:58PM (#14296007)
When the language was made 40 years ago, the extra overhead was unbearable.

Or rather, more likely, they were so focused on efficiency (because that's what you do when you have a 1Mhz computer), that the idea of making things easier for people in this way didn't cross their mind.

Re:Ode to the use-after-free: one vulnerable function, a tho... by pjmlp on Tuesday May 09, 2017 @12:54AM (#14297817)
Apparently this myth never dies.

Burroughs did not have this issue with ESPOL and NEWP almost 10 years earlier before C was born.

Nor did the UK Navy with Algol 68RS or the multiple institutes using PL/I variants.

Re:Ode to the use-after-free: one vulnerable function, a tho... by ktRolster on Tuesday May 09, 2017 @02:35AM (#14298151)
I'm going to suggest you didn't read my whole comment.
Re:Ode to the use-after-free: one vulnerable function, a tho... by pjmlp on Tuesday May 09, 2017 @03:07AM (#14298246)
I sure did.

Burroughs B5500 was created in 1961 and executed far slower and had less memory than a PDP-11, yet it was able to have a full OS written in ESPOL, an ALGOL dialect with intrisics for CPU instructions and explicit UNSAFE statements.

So if a 10 year old machine by the time C was created for computers with " extra overhead was unbearable." and " what you do when you have a 1Mhz computer" was already able to execute an Algol dialect with enough performance, what does that say of C in regards to language design?

Re:Ode to the use-after-free: one vulnerable function, a tho... by ktRolster on Tuesday May 09, 2017 @02:43PM (#14302775)
You have written nothing that indicates you've read my second paragraph.
Re:Ode to the use-after-free: one vulnerable function, a tho... by pjmlp on Tuesday May 09, 2017 @06:14PM (#14304280)
Only if you implying that people that wrote operating systems before C was designed, were lazy and didn't care about performance, in spite of having less powerful hardware at their disposal.