CWE Rule 521
Description
Weak Password Requirements
Polyspace Implementation
The rule checker checks for Password not checked before setting
Examples
This issue occurs when these events occur in sequence:
A password is obtained from a tainted source.
The password is set without passing it to the function that checks the password validity.
To use this coding rule checker, specify these in a Datalog file:
Source of taint — You can either use the default taint sources or you can specify a function as the taint source. To use the default taint sources, add this line of code:
To specify a functionCustom_CWE_521.useDefaultTaintSources().
foo()as the taint source:Sources of taint are identified in the event list and the specified string is the event message.Custom_CWE_521.Basic.taintSource("foo", $OutReturnDeref(), "Taint source").The function that sets the password — This code specifies the function
setPassword()as the password setting function:The password setting function is identified in the event list and the specified string is the event message.Custom_CWE_521.Basic.passwordSet("setPassword", $InParameterDeref(0), "Setting a password!").The function that checks the password validity — This code specifies the function
checkPasswordas the password checking function:The password checking function is identified in the event list and the specified string is the event message.Custom_CWE_521.Basic.passwordCheck("isValidPassword", $OutParameterDeref(0)).Function that allocate memory — If your code saves the password in memory, specify the function that allocates the necessary memory. This code specifies that the function
foo()allocates memory:Alias.Basic.allocates("foo", $OutReturnValue()).
Setting passwords without checking their validity can result in passwords that are easy to guess. Attackers can gain access easily when the password is weak.
Before setting a password that the user specifies, check the validity of the password.
In this code, the function func() accepts a password from the user
and then sets the password by calling setPassword. Because the password
is not checked before setting, Polyspace® reports a violation.
#include <stdio.h>
#include <string.h>
extern int setPassword(const char *password);
void func() {
char password[100];
printf("Enter your password: ");
scanf("%99s", password);
if(1 == setPassword(password)) { // Noncompliant
printf("Password accepted.\n");
}
}-code-behavior-specificationCustom_CWE_521.useDefaultTaintSources().
Custom_CWE_521.Basic.passwordSet("setPassword", $InParameterDeref(0), "Setting a password!").
To correct this violation, check the password before setting it.
#include <stdio.h>
#include <string.h>
extern int setPassword(const char *password);
extern int isValidPassword(const char *password);
void func() {
char password[100];
printf("Enter your password: ");
scanf("%99s", password);
if(1 == isValidPassword(password)) { // Compliant
if(1 == setPassword(password)) {
printf("Password accepted.\n");
}
}
}Custom_CWE_521.useDefaultTaintSources().
Custom_CWE_521.Basic.passwordSet("setPassword", $InParameterDeref(0), "Setting a password!").
Custom_CWE_521.Basic.passwordCheck("isValidPassword", $OutParameterDeref(0)).Check Information
| Category: Credentials Management Errors |
PQL Name: std.cwe_native.R521 |
Version History
Introduced in R2026a
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.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- 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)