2013-05-23 2 views
0

나는 strcpy 명령을 사용하여 충돌 의도로 C++ winsock (win 32)을 구현했습니다. 소켓 자체는 스레드 내부에서 인스턴스화됩니다. 그러나 을 recv 루프 안에 넣으면 충돌이 발생하지 않습니다.왜 스레드 된 winsock 서버가 중단되지 않습니까?

나는 단지 strcpy과 하나를 작성한 이후 컴파일러에 아무런 문제가 없다는 것을 알고 있으며, 프로세스에서 블록을 시작하기 때문에 recv과 관련이 있다고 생각하고 있습니다.

아래의 전체 코드는 서버에 대한 것입니다. 구현하려고하는 충돌은 일반적인 상황에서는 strcpy(a, "AAAA..."); 형태로 발생하지만 충돌이 발생하지는 않습니다. 이유를 알고 싶습니다. 귀하의 strcpy() 전화는 단지 글로벌 메모리에 후에 될 일이 어떤 다른 다음과 부수고있다

#define WIN32_LEAN_AND_MEAN 
#include<windows.h> 
#include<winsock2.h> 
#include<stdlib.h> 
#include<stdio.h> 
#include<ws2tcpip.h> 
#include <iostream.h> 
#include <conio.h> 

#define DEFAULT_PORT "1133" 
#define DEFAULT_BUFLEN 512 

struct thread_data 
{ 
    int m_id; 
    thread_data(int id) : m_id(id){} 
}; 

char a[10]; 

DWORD WINAPI ServerThread (LPVOID pParam){ 
WSADATA wsaData; 
struct addrinfo *result =NULL; 
struct addrinfo hints; 
SOCKET ListenSocket = INVALID_SOCKET; 


do{ 
    ZeroMemory(&hints, sizeof(hints)); 
    hints.ai_family = AF_INET; 
    hints.ai_socktype = SOCK_STREAM; 
    hints.ai_protocol = IPPROTO_TCP; 
    hints.ai_flags = AI_PASSIVE; 

    int iResult; 
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData); 

    iResult = getaddrinfo(NULL,DEFAULT_PORT,&hints, &result); 



    if (iResult != 0){ 
    printf("get addrinfo failed with error %d\n", iResult); 
    WSACleanup(); 
    return 1; 
    } //end if 

ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); 
if (ListenSocket ==0){ 
    printf("socket creation failed with error %d\n", WSAGetLastError()); 
} 
//bind socket 
iResult= bind(ListenSocket , result->ai_addr, (int)result->ai_addrlen); 


if(iResult == SOCKET_ERROR){ 
    printf("bind failed with error %d\n", WSAGetLastError()); 
    freeaddrinfo(result); 

    closesocket(ListenSocket); 
    WSACleanup(); 
    return 1; 

} 

printf ("initializing socket\n "); 

iResult= listen(ListenSocket,SOMAXCONN); 

if (iResult== SOCKET_ERROR){ 
    printf("listen failed with %d\n",WSAGetLastError()); 
    closesocket(ListenSocket); 
    WSACleanup(); 
    return 1; 
} 



SOCKET client ; 
sockaddr_in from; 
int fromlen=sizeof(from); 




char temp[1024]; 
char temp_to_send[1024]; 
char temp_to_send_vuln[512]; 
printf("accepting client request\n"); 
client=accept(ListenSocket, (struct sockaddr*) &from, &fromlen); 
printf("accepted socket\n"); 



iResult =1; 
int iSendResult =1; 
char c; 
//start receiving from client 
while( (iResult = recv(client,temp,1024,0)) > 0){ 



    c = temp[0]; 
    temp[iResult] = '\0'; 
    if(c!=13) 
    strcat(temp_to_send,temp); 


    //if enter is hit echo sent data to client 
    if(c ==13){ 
    printf("sending %s \n",temp_to_send); 

    //I WANT TO CRASH THE PGORAM HERE!!   
      strcpy(a,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); 

    strcat(temp_to_send_vuln,temp_to_send); 
    strcat(temp_to_send_vuln,"\r\n"); 
    iSendResult = send(client,temp_to_send,strlen(temp_to_send),0); 

    //if user types "exit" the client socket would terminate     
    if (strcmp(temp_to_send,"exit") ==0){ 
     printf("exit entered\n"); 


     closesocket(client); 
     WSACleanup(); 
     break; 

    } 



    re-initialize variables for next input        
     temp[0] = '\0'; 
     temp_to_send[0] = '\0'; 

    }//end if(ch ==13) 


}//end recv 

printf("termination of socket with error %d and buffer length is ", WSAGetLastError()); 

printf("client said %s\n", temp) ; 
if (iResult == SOCKET_ERROR){ 
    printf("receiving failed with error %d",WSAGetLastError()); 
} 





if (iSendResult == SOCKET_ERROR){ 
    printf("seding failed with error %d", WSAGetLastError()); 
    closesocket(client); 
    WSACleanup(); 
    exit(1); 

} 






} while(1); 

closesocket(ListenSocket); 

WSACleanup(); 
printf("program ended\n"); 

return 0; 


} 

//the main function that calls the thread 
int main(void) 
{ 
    //create thread here 
    CreateThread(NULL, 0 ,ServerThread, new thread_data(0), 0,0); 

    //terminate program when escape character is hit 
    while(_getch()!=27); 

    return 0; 

} 
+0

직장에서 구할 수 있습니까? 나는 이것에 능숙하다. –

답변

0

; 충돌할지 여부는 정의되지 않았습니다. 충돌 사고가 실제로 발생하면 strcpy(NULL, "whatever")으로 전화하십시오.

+1

정말로 강제로 충돌을 원한다면, RTL을 던지기 위해 자신의 예외를 직접 던지십시오. –

+0

답장을 보내 주셔서 감사 합니다만, 실제로 버퍼 오버플로를 시뮬레이트하려고합니다. strcpy가 "a"를 어떻게 버리고 있습니까? – iamus

+0

실제로 'a'다음에 오는 것은 모두 삭제합니다. 문제는 그것이 자신이 소유 한 메모리인지 여부가 정의되어 있지 않다는 것입니다. OS 예외를 원하면 NULL과 같이 소유하지 않는 것이 확실한 곳을 작성해야합니다. – HerrJoebob

관련 문제