I think the last bug stumped a few people. Can you find the security vulnerability in this one?
Courtesy of Neelay Shah, Consultant, Foundstone

#define STD_HASH_LEN 11
#define MAX_HASH_LEN 31                 

char * strPassHash = (char*)malloc(sizeof(char)*STD_HASH_LEN);

            :

            :     //Create the hash

            :

// Now suppose you need to recreate the hash which would be of length = MAX_HASH_LEN

      strPassHash = (char*)realloc(strPassHash, MAX_HASH_LEN);

            :

            :    

            :

Solution

      #define STD_HASH_LEN   11

      #define MAX_HASH_LEN 31

                 

      char * strPassHash = (char*)malloc(sizeof(char)*STD_HASH_LEN);

                  :

                  :     //Create the hash

                  :

      // Now suppose you need to recreate the hash which would be of length = //MAX_HASH_LEN

            char * strNewPassHash = (char*)realloc(strPassHash, MAX_HASH_LEN);

      if(NULL == strNewPassHash)

      {

            // Not enough free memory…free the old hash and return an error.

            free(StrPassHash);

            printf(“Error…Not enough free memory”);

            return;

      }

 

      strPassHash = strNewPassHash;

                  :

                  :    

                  :

 

Description:

In the bad way, if realloc() fails for want of memory it returns a NULL and the pointer to the old memory is lost. Now in normal cases this memory leak may not be a security threat but in case the memory is shared and its contents are sensitive like a password hash for example it may lead to a security threat. The good way of programming gets around this by using an extra pointer.