Difference between revisions of "Mutable object returned"

From OWASP
Jump to: navigation, search
 
(Examples)
Line 47: Line 47:
 
In C\C++:
 
In C\C++:
  
 +
<pre>
 
private:
 
private:
 
   externalClass foo;
 
   externalClass foo;
Line 55: Line 56:
 
     return foo;
 
     return foo;
 
   }
 
   }
 +
</pre>
 +
 
In Java:
 
In Java:
  
 +
<pre>
 
public class foo {
 
public class foo {
 
  private externalClass bar = new externalClass();
 
  private externalClass bar = new externalClass();
Line 63: Line 67:
 
   return bar;
 
   return bar;
 
  }
 
  }
 +
</pre>
 +
 
==Related problems ==
 
==Related problems ==
  

Revision as of 12:02, 16 April 2006



Overview

Sending non-cloned mutable data as a return value may result in that data being altered or deleted by the called function, thereby putting the class in an undefined state.

Consequences

  • Access Control / Integrity: Potentially data could be tampered with by another function which should not have been tampered with.

Exposure period

  • Implementation: This flaw is a simple logic issue, introduced entirely at implementation time.

Platform

  • Languages: C,C++ or Java
  • Operating platforms: Any

Required resources

Any

Severity

Medium

Likelihood of exploit

Medium

Avoidance and mitigation

  • Implementation: Pass in data which should not be alerted as constant or immutable.
  • Implementation: Clone all mutable data before returning references to it. This is the preferred mitigation. This way, regardless of what changes are made to the data, a valid copy is retained for use by the class.

Discussion

In situations where functions return references to mutable data, it is possible that this external code, which called the function, may make changes to the data sent. If this data was not previously cloned, you will be left with modified data which may, or may not, be valid in the context of the class in question.

Examples

In C\C++:

private:
  externalClass foo;

public:
  void doStuff() {
//..//Modify foo
    return foo;
  }

In Java:

public class foo {
 private externalClass bar = new externalClass();
 public doStuff(...){
   //..//Modify bar
   return bar;
 }

Related problems

Not available.

Categories