You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 16 Next »

The POSIX function putenv() is used to set environment variable values. The putenv() function does not create a copy of the string supplied to it as a parameter, rather it inserts a pointer to the string into the environment array. If an automatic variable is supplied as a parameter to putenv(), the memory allocated for that variable may be overwritten when the containing function returns and stack memory is recycled. This behavior is noted in the Open Group Base Specifications Issue 6 [[Open Group 04]]:

A potential error is to call putenv() with an automatic variable as the argument, then return from the calling function while string is still part of the environment.

Note that this behavior violates rule DCL30-C. Do not refer to an object outside of its lifetime.

Non-Compliant Code Example

In this example, an automatic variable is added to the environment via a call to putenv(). When that environment variable is retrieved using getenv(), it may have an unexpected value.

int func() {
  char env[10];
  strcpy(env,"VAR=1");
  putenv(env);
  return 0;
}

int main (int argc, char * argv[]) {
  char *var;
  func();
  /* ... */
  var = getenv("VAR");
  return 0;
}

Compliant Solution

To make this example compliant env should not be declared as an automatic variable.

static char *env= "VAR=1";
putenv(env);

Risk Assessment

Using an automatic variable as an argument to putenv() may cause that variable to take on an unintended value. Depending on how and when that variable is used, this can cause unexpected program behavior, or possibly allow an attacker to run arbitrary code.

Referencing an object outside of its lifetime could result in an attacker being able to run arbitrary code.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

ENV80-C

3 (high)

1 (unlikely)

1 (high)

P3

L3

Examples of vulnerabilities resulting from the violation of this rule can be found on the CERT website.

References

  • No labels