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.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)