{"action":"created","issue":{"url":"https://api.github.com/repos/ARMmbed/mbedtls/issues/359","labels_url":"https://api.github.com/repos/ARMmbed/mbedtls/issues/359/labels{/name}","comments_url":"https://api.github.com/repos/ARMmbed/mbedtls/issues/359/comments","events_url":"https://api.github.com/repos/ARMmbed/mbedtls/issues/359/events","html_url":"https://github.com/ARMmbed/mbedtls/issues/359","id":119673066,"number":359,"title":"Pre-emptive defense: #define mbedtls_free to free + NULL","user":{"login":"guidovranken","id":6846644,"avatar_url":"https://avatars.githubusercontent.com/u/6846644?v=3","gravatar_id":"","url":"https://api.github.com/users/guidovranken","html_url":"https://github.com/guidovranken","followers_url":"https://api.github.com/users/guidovranken/followers","following_url":"https://api.github.com/users/guidovranken/following{/other_user}","gists_url":"https://api.github.com/users/guidovranken/gists{/gist_id}","starred_url":"https://api.github.com/users/guidovranken/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/guidovranken/subscriptions","organizations_url":"https://api.github.com/users/guidovranken/orgs","repos_url":"https://api.github.com/users/guidovranken/repos","events_url":"https://api.github.com/users/guidovranken/events{/privacy}","received_events_url":"https://api.github.com/users/guidovranken/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-12-01T09:15:49Z","updated_at":"2015-12-01T14:36:49Z","closed_at":null,"body":"As you're aware, I recently found a bunch of double-free's in mbed TLS.\r\n\r\nYou might want to change this:\r\n\r\n```c\r\n#define mbedtls_free       free\r\n```\r\n\r\ninto something like this: https://github.com/torproject/tor/blob/master/src/common/util.h#L102\r\n\r\n```c\r\n#define tor_free(p) STMT_BEGIN \\\r\n    if (PREDICT_LIKELY((p)!=NULL)) {                \\\r\n      dmalloc_free(SHORT_FILE__, __LINE__, (p), 0); \\\r\n      (p)=NULL;                                     \\\r\n    }                                               \\\r\n  STMT_END\r\n```\r\n\r\nThis will basically guarantee the absence of double-free's, as long as the macro is used sensibly.\r\n\r\nJust like using ```calloc``` for every memory allocation to guarantee that memory is initialized to zeroes to forestall leaking of sensitive information even when bugs in the code over-read buffers, my proposed change could have prevented the double-free's found, and prevent any other double-free instances currently present or introduced in the future."},"comment":{"url":"https://api.github.com/repos/ARMmbed/mbedtls/issues/comments/160985439","html_url":"https://github.com/ARMmbed/mbedtls/issues/359#issuecomment-160985439","issue_url":"https://api.github.com/repos/ARMmbed/mbedtls/issues/359","id":160985439,"user":{"login":"olsner","id":222346,"avatar_url":"https://avatars.githubusercontent.com/u/222346?v=3","gravatar_id":"","url":"https://api.github.com/users/olsner","html_url":"https://github.com/olsner","followers_url":"https://api.github.com/users/olsner/followers","following_url":"https://api.github.com/users/olsner/following{/other_user}","gists_url":"https://api.github.com/users/olsner/gists{/gist_id}","starred_url":"https://api.github.com/users/olsner/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/olsner/subscriptions","organizations_url":"https://api.github.com/users/olsner/orgs","repos_url":"https://api.github.com/users/olsner/repos","events_url":"https://api.github.com/users/olsner/events{/privacy}","received_events_url":"https://api.github.com/users/olsner/received_events","type":"User","site_admin":false},"created_at":"2015-12-01T14:36:49Z","updated_at":"2015-12-01T14:36:49Z","body":"Perhaps a more interesting definition would be to set the pointer to an explicitly poisoned value like `(void*)-1` - then instead of a silent no-op, double-frees would become obvious crashes."}}