CWE Rule 307
R2026bDescription
Improper Restriction of Excessive Authentication Attempts
Polyspace Implementation
The rule checker checks for Uncontrolled authentication attempts.
Examples
This issue occurs when your code calls an authentication function inside a loop that has no upper bound on the number of iterations.
To use this checker, provide a -code-behavior-specifications file
that identifies authentication functions using the AUTHENTICATE_USER
behavior type and specifies a maximum call threshold. See Modify Bug Finder Checkers Through Code Behavior Specifications.
If an authentication function is called inside a loop that does not enforce an upper bound on iterations, Polyspace® reports a violation. The threshold value in the specification file determines the maximum number of allowed authentication attempts.
If the option -code-behavior-specifications is not set, the checker
is not reported.
Without a limit on authentication attempts, an attacker can use brute-force techniques to guess valid credentials. Repeated attempts consume system resources and can lock out legitimate users or compromise accounts.
Bound the loop that calls the authentication function so that it exits after a fixed maximum number of attempts. For example, use a counter variable and compare it to a predefined limit in the loop condition.
#include <stdio.h>
#define USERNAME_SIZE 256
#define PASSWORD_SIZE 256
#define FAIL -1
#define SUCCESS 0
int openSocketConnection(char *host, int port);
int getNextMessage(int socket, char *buffer, int size);
int AuthenticateUser(char *username, char *password);
int validateUser(char *host, int port)
{
int socket = openSocketConnection(host, port);
if (socket < 0) {
return FAIL;
}
int isValidUser = 0;
char username[USERNAME_SIZE];
char password[PASSWORD_SIZE];
while (isValidUser == 0) {
if (getNextMessage(socket, username, USERNAME_SIZE) > 0) {
if (getNextMessage(socket, password, PASSWORD_SIZE) > 0) {
isValidUser = AuthenticateUser(username, password); // Noncompliant
}
}
}
return SUCCESS;
}The function validateUser calls AuthenticateUser inside
a while loop that continues indefinitely until authentication succeeds.
There is no limit on how many times the authentication function can be called, which
allows unlimited login attempts. Polyspace reports violations. To detect this issue, specify
AuthenticateUser as a function that authenticates a
user:
<?xml version="1.0" encoding="UTF-8"?>
<specifications xmlns="http://www.mathworks.com/PolyspaceCodeBehaviorSpecifications">
<functions>
<function name="AuthenticateUser">
<behavior name="AUTHENTICATE_USER" value="5" />
</function>
</functions>
</specifications>To fix this issue, include a loop condition which limits the number of authentication
attempts to 5. After this many failed attempts, the function returns
FAIL without allowing further tries.
#include <stdio.h>
#define USERNAME_SIZE 256
#define PASSWORD_SIZE 256
#define MAX_ATTEMPTS 5
#define FAIL -1
#define SUCCESS 0
int openSocketConnection(char *host, int port);
int getNextMessage(int socket, char *buffer, int size);
int AuthenticateUser(char *username, char *password);
int validateUser(char *host, int port)
{
int socket = openSocketConnection(host, port);
if (socket < 0) {
return FAIL;
}
int isValidUser = 0;
char username[USERNAME_SIZE];
char password[PASSWORD_SIZE];
int count = 0;
while ((isValidUser == 0) && (count < MAX_ATTEMPTS)) {
if (getNextMessage(socket, username, USERNAME_SIZE) > 0) {
if (getNextMessage(socket, password, PASSWORD_SIZE) > 0) {
isValidUser = AuthenticateUser(username, password); // Compliant
}
}
count++;
}
return isValidUser ? SUCCESS : FAIL;
}Specify the function AuthenticateUser as a function that
authenticates a
user:
<?xml version="1.0" encoding="UTF-8"?>
<specifications xmlns="http://www.mathworks.com/PolyspaceCodeBehaviorSpecifications">
<functions>
<function name="AuthenticateUser">
<behavior name="AUTHENTICATE_USER" value="5" />
</function>
</functions>
</specifications>Check Information
| Category: Others |
PQL Name: std.cwe_native.R307 |
Version History
Introduced in R2026b
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)