Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by sciSpider Java v3.0

...

Code Block
bgColor#ffcccc
class SensitiveClass {
  private char[] filename;
  private Boolean shared = false;
 
  protected SensitiveClass(String filename) {
    this.filename = filename.toCharArray();
  }

  protected void replace(){
    if(!shared)
      for(int i=0;i<filenamei&lt;filename.length;i++) {
    	filename[i]= 'x';
    }
  }

  protected String get(){
    if(!shared){	
      shared = true;
      return String.valueOf(filename);
    } else {
      throw new Error("&quot;Error getting instance"&quot;);
    }
  }
  
  protected void printFilename(){
    System.out.println(String.valueOf(filename));
  }
}

class MaliciousSubclass extends SensitiveClass implements Cloneable {	
  protected MaliciousSubclass(String filename) {
    super(filename);
  }
  
  @Override public MaliciousSubclass clone() {  // Well-behaved clone() method
    MaliciousSubclass s = null;
    try {
      s = (MaliciousSubclass)super.clone();	        
    } catch(Exception e) { 
      System.out.println("&quot;not cloneable"&quot;); 
    }
    return s;
  }

  public static void main(String[] args){
    MaliciousSubclass ms1 = new MaliciousSubclass("&quot;file.txt"&quot;);
    MaliciousSubclass ms2 = ms1.clone(); // Creates a copy 
    String s = ms1.get(); // Returns filename
    System.out.println(s); // Filename is "&quot;file.txt"&quot;
    ms2.replace(); // Replaces all characters with x'
    // Both ms1.get() and ms2.get() will subsequently return filename = 'xxxxxxxx'
    ms1.printFilename(); // Filename becomes 'xxxxxxxx' 
    ms2.printFilename(); // Filename becomes 'xxxxxxxx'
  }
}

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

References

Wiki Markup
\[[Mcgraw 98|AA. Java References#Mcgraw 98]\] 
\[[Wheeler 03|AA. Java References#Wheeler 03]\] 10.6. Java 
\[[MITRE 09|AA. Java References#MITRE 09]\] [CWE ID 498|http://cwe.mitre.org/data/definitions/498.html] "&quot;Information Leak through Class Cloning"&quot;, [CWE ID 491|http://cwe.mitre.org/data/definitions/491.html] "&quot;Public cloneable() Method Without Final (aka 'Object Hijack')"&quot;

...

IDS07-J. Understand how escape characters are interpreted when String literals are compiled      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;49. Miscellaneous (MSC)      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MSC33-J. Do not modify the underlying collection when an iteration is in progress