CWE Rule 666
Description
Rule Description
The product performs an operation on a resource at the wrong phase of the resource's lifecycle, which can lead to unexpected behaviors.
Polyspace Implementation
The rule checker checks for Incorrect order of network connection operations.
Examples
This issue occurs when you perform operations on a network connection at the wrong point of the connection lifecycle.
Sending or receiving data to an incorrectly connected socket can cause unexpected behavior or disclosure of sensitive information.
If you do not connect your socket correctly or change the connection by mistake, you can send sensitive data to an unexpected port. You can also get unexpected data from an incorrect socket.
During socket connection and communication, check the return of each call and the length of the data.
Before reading, writing, sending, or receiving information, create sockets in this order:
For a connection-oriented server socket (
SOCK_STREAM
orSOCK_SEQPACKET
):socket(...); bind(...); listen(...); accept(...);
For a connectionless server socket (
SOCK_DGRAM
):socket(...); bind(...);
For a client socket (connection-oriented or connectionless):
socket(...); connect(...);
# include <stdio.h>
# include <string.h>
# include <time.h>
# include <arpa/inet.h>
# include <unistd.h>
enum { BUF_SIZE=1025 };
volatile int rd;
int stream_socket_server(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
char sendBuff[BUF_SIZE];
time_t ticks;
struct tm * timeinfo;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, 48, sizeof(serv_addr));
memset(sendBuff, 48, sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
ticks = time(NULL);
timeinfo = localtime(&ticks);
strftime (sendBuff,BUF_SIZE,"%I:%M%p.",timeinfo);
write(listenfd, sendBuff, strlen(sendBuff)); //Noncompliant
close(connfd);
sleep(1);
}
}
This example creates a connection-oriented network connection.
The function calls the correct functions
in the correct order: socket
, bind
, listen
, accept
.
However, the program should write to the connfd
socket
instead of the listenfd
socket.
One possible correction is to write to the connfd
function
instead of the listenfd
socket.
# include <stdio.h>
# include <string.h>
# include <time.h>
# include <arpa/inet.h>
# include <unistd.h>
enum { BUF_SIZE=1025 };
volatile int rd;
int stream_socket_server_good(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
char sendBuff[BUF_SIZE];
time_t ticks;
struct tm * timeinfo;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, 48, sizeof(serv_addr));
memset(sendBuff, 48, sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
ticks = time(NULL);
timeinfo = localtime(&ticks);
strftime (sendBuff,BUF_SIZE,"%I:%M%p.",timeinfo);
write(connfd, sendBuff, strlen(sendBuff));
close(connfd);
sleep(1);
}
}
Check Information
Category: Others |
Version History
Introduced in R2024a
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)