TAOSSA on Novel C++ Bug Class: delete/delete[]
Thomas Ptacek | January 3rd, 2007 | Filed Under: Defenses, New Findings, Uncategorized
From the Law Offices of Dowd, McDonald, and Schuh: a fantastic post on a C++ bug-class: misuse of delete and delete[].
The problem: the C++ runtime needs to invoke the destructors for every element of an array when it’s released. But given a bare pointer, the runtime doesn’t know whether it’s looking at an array of objects or a single object. You have to tell it. You do this by invoking “delete foo” for a single object, and “delete [] foo” for an array.
This leads to two problems:
If you screw up and invoke “delete []” on a single object, the runtime will interpret an offset from that pointer as a count variable. If an attacker controls the value at that offset, she can trick the runtime into invoking destructors on invalid memory locations.
If you screw up and involve bare “delete” on an array —- a mistake I made all the time on my last C++ project —- the runtime will call “free” on an invalid pointer; depending on how much control you have over the array, this devolves into a simple malloc attack.
An observation, and a generalization of it:
C++ is considered “safer” than C (primarily because it offers a standard dynamic string class, though most developers still just use char-stars). But it has a drastically more complicated runtime than C’s crt0.c, and minimal language-level protections. I’m ambivalent about C++, and terrified of the STL (how many hidden bug classes are there in iterators alone?).
Higher-level languages are considered “safer” than C. At some threshold of complexity, such as is crossed by Tcl, Ruby, Perl, and Python, they probably are. But below that threshold, most language features offer as many footholds for attackers as they do obstacles. How carefully are people considering C++ and Objective C?


Add New Comment
Viewing 12 Comments
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks