CWE-374: Passing Mutable Objects to an Untrusted Method

Description

The product sends non-cloned mutable data as an argument to a method or function.

Submission Date :

July 19, 2006, midnight

Modification Date :

2023-06-29 00:00:00+00:00

Organization :

MITRE
Extended Description

The function or method that has been called can alter or delete the mutable data. This could violate assumptions that the calling function has made about its state. In situations where unknown code is called with references to mutable data, this external code could make changes to the data sent. If this data was not previously cloned, the modified data might not be valid in the context of execution.

Example Vulnerable Codes

Example - 1

The following example demonstrates the weakness.


int foo;complexType bar;String baz;otherClass externalClass;
externalClass.doOtherStuff(foo, bar, baz)void doStuff() {}private:public:

In this example, bar and baz will be passed by reference to doOtherStuff() which may change them.

Example - 2

In the following Java example, the BookStore class manages the sale of books in a bookstore, this class includes the member objects for the bookstore inventory and sales database manager classes. The BookStore class includes a method for updating the sales database and inventory when a book is sold. This method retrieves a Book object from the bookstore inventory object using the supplied ISBN number for the book class, then calls a method for the sales object to update the sales information and then calls a method for the inventory object to update inventory for the BookStore.


// // constructor for BookStore// 

this.inventory = new BookStoreInventory();this.sales = new SalesDBManager();...

// // Get book object from inventory using ISBN// 
// // update sales information for book sold// 
// // update inventory// 
Book book = inventory.getBookWithISBN(bookISBN);sales.updateSalesInformation(book);inventory.updateInventory(book);
// // other BookStore methods// 
private BookStoreInventory inventory;private SalesDBManager sales;...public BookStore() {}public void updateSalesAndInventoryForBookSold(String bookISBN) {}...

// // Book object constructors and get/set methods// 
private String title;private String author;private String isbn;...public class BookStore {}public class Book {}

However, in this example the Book object that is retrieved and passed to the method of the sales object could have its contents modified by the method. This could cause unexpected results when the book object is sent to the method for the inventory object to update the inventory.

In the Java programming language arguments to methods are passed by value, however in the case of objects a reference to the object is passed by value to the method. When an object reference is passed as a method argument a copy of the object reference is made within the method and therefore both references point to the same object. This allows the contents of the object to be modified by the method that holds the copy of the object reference. [REF-374]

In this case the contents of the Book object could be modified by the method of the sales object prior to the call to update the inventory.

To prevent the contents of the Book object from being modified, a copy of the Book object should be made before the method call to the sales object. In the following example a copy of the Book object is made using the clone() method and the copy of the Book object is passed to the method of the sales object. This will prevent any changes being made to the original Book object.



// // Get book object from inventory using ISBN// 
// // Create copy of book object to make sure contents are not changed// 
// // update sales information for book sold// 
// // update inventory// 
Book book = inventory.getBookWithISBN(bookISBN);Book bookSold = (Book) book.clone();sales.updateSalesInformation(bookSold);inventory.updateInventory(book);
...public void updateSalesAndInventoryForBookSold(String bookISBN) {}...

Related Weaknesses

This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.

Visit http://cwe.mitre.org/ for more details.

© cvefeed.io
Latest DB Update: Dec. 25, 2024 3:01