Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Do not make any assumptions about the size of environment variables, as an adversary could have full control over the environment. Calculate the length of the strings yourself, and dynamically allocate memory for your copies. There is nothing you can do to avoid the race conditions inherent here, but you can limit your exposure.

...

Code Block
bgColor#FFcccc
char *temp;
char buffcopy[16];

temp = getenv("TEST_ENV");

if(temp != NULL)
  strcpy(buff, temp);

...

Wiki Markup
\[[ISO/IEC 9899-1999:TC2|AA. C References#ISO/IEC 9899-1999TC2]\] Section 7.20.4, "Communication with the environment"
\[[Open Group 04|AA. C References#Open Group 04]\] Chapter 8, "Environment Variables", [strdup|http://www.opengroup.org/onlinepubs/009695399/functions/strdup.html]
\[[Viega 03|AA. C References#Viega 03]\] Section 3.6, "Using Environment Variables Securely"
http://it-reading.org/book/secure%20programming%20cookbook%20c%20and%20c++/content/0596003943_secureprgckbk-chp-3-sect-6.html