Weakness · Medium
CWE-375: Returning a Mutable Object to an Untrusted Caller
What it is
Sending non-cloned mutable data as a return value may result in that data being altered or deleted by the calling function.
- Severity (our rating)
- Medium
- Likelihood of exploit
- Medium
- Introduced during
- Implementation
- Exploited in the wild
- No CVE in CISA's catalog
In more detail
In situations where functions return references to mutable data, it is possible that the external code which called the function may make changes to the data sent. If this data was not previously cloned, the class will then be using modified data which may violate assumptions about its internal state.
What an attacker can do
- Modify Memory (Access Control, Integrity)
How to prevent it
Implementation
- Declare returned data which should not be altered as constant or immutable.
- 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.
H2 Security
Found it in your code?
We review where it appears, fix it with your team, and check it was not already used against you.
Talk to our teamT Academy
Stop writing it in the first place
Learn to build software and systems without weaknesses like this one, with practice on real hosts in your browser, every objective checked on the machine.
Start freeSource: MITRE Common Weakness Enumeration, CWE version 4.19.1. CWE is a trademark of The MITRE Corporation.