CWE Rule 498
Description
Rule Description
The code contains a class with sensitive data, but the class is cloneable. The data can then be accessed by cloning the class.
Polyspace Implementation
The rule checker checks for the issue Sensitive information accessible through copy constructor.
Examples
Sensitive information accessible through copy constructor
The issue Sensitive information accessible through copy constructor occurs when a class contains both sensitive information and one of the following:
A public copy constructor, including an implicitly declared one.
An overloaded copy assignment operator.
You can specify sensitive data members using the option -code-behavior-specifications
and the code behavior CRITICAL_DATA
. See Specifying Critical Data Members.
Copying a class allows sensitive data to be accessible even when you mark the
sensitive data as private
. You can inadvertently introduce
vulnerabilities if your code copies the sensitive data.
To fix this violation, either delete the copy constructor or overloaded copy
assignment operator or mark it as private
.
#include <string> #include <iostream> class Login { public: Login(std::string n, std::string c) : username(n), password(c) {} Login(const Login& t) = default; std::string get_username(){return username;} private: std::string username; //Noncompliant std::string password; //Noncompliant }; class CopyUser { public: CopyUser() { Login t1("user1", "a1B2c3D4"); // ... Login t2(t1); // ... } static void main() { new CopyUser(); } }; int main() { CopyUser::main(); }
In this example, you declare the data members username
and password
as private
. Specify these variables as sensitive in a code behavior XML file:
<?xml version="1.0" encoding="UTF-8"?> <specifications xmlns="http://www.mathworks.com/PolyspaceCodeBehaviorSpecifications"> <members> <member name="password" kind="variable"> <behavior name="CRITICAL_DATA"/> </member> <member name="username" kind="variable"> <behavior name="CRITICAL_DATA"/> </member> </members> </specifications>
The copy constructor Login(const Login& t) = default;
is
public, which allows the class CopyUser
to copy a
Login
object and access the sensitive data members
username
and password
through the copy.
To fix this violation, either delete the copy constructor or overloaded assignment
copy assignment operator or mark it as private. If the class contains an implicit copy
constructor, explicitly declare the copy constructor and mark it as
private
or =delete
.
Because you mark Login(const Login& t)
as
=delete
in this code, the CopyUser
class is no
longer able to access the copy constructor keeping sensitive information from being
copied.
#include <string> #include <iostream> class Login { public: Login(std::string n, std::string c) : username(n), password(c) {} Login(const Login& t) = delete; private: std::string username; //Compliant std::string password; //Compliant };
The code behavior specifications XML file can continue to be the same as before:
<?xml version="1.0" encoding="UTF-8"?> <specifications xmlns="http://www.mathworks.com/PolyspaceCodeBehaviorSpecifications"> <members> <member name="password" kind="variable"> <behavior name="CRITICAL_DATA"/> </member> <member name="username" kind="variable"> <behavior name="CRITICAL_DATA"/> </member> </members> </specifications>
Check Information
Category: Others |
Version History
Introduced in R2023b
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)