기본 콘텐츠로 건너뛰기

Secure-Coding-C-DCL-5

Secure-Coding-C-DCL-5

C 언어 시큐어코딩 - Declare and Initialization 5

올바른 구문을 사용하여 가변 길이 배열 선언

가변 길이 배열은 하나 이상의 멤버가 있는 구조의 마지막 요소가 불완전한 특별한 유형의 배열이다. 즉, 배열의 크기가 구조 내에서 명시적으로 지정되어 있지 않다.

가변 길이 배열 구조 정의 시 몇 가지 제한 사항이 존재한다.

  • 불완전한 배열 유형은 구조 내의 마지막 요소 여야 한다.
  • 가변 길이 배열 멤버가 포함된 구조체 배열은 있을 수 없다.
  • 가변 길이 배열 멤버가 포함된 구조체는 다른 구조체의 멤버로 사용할 수 없다.
  • 구조체에는 가변 길이 배열 멤버 외에 하나 이상의 명명된 멤버가 있어야 한다.

잘못된 코드 예제

C 표준에서 가변 길이 배열을 도입하기 전에 유사한 요소를 얻기 위해 단일 요소 배열을 최종 멤버로 사용하는 구조가 사용되었었다.

예제는 하나의 요소을 가진 배열을 가변 길이 배열과 같이 최종 멤버를 할당하려고 시도한다. 구조체가 인스턴스화되면 malloc()에 계산된 크기가 동적 배열의 실제 크기를 고려하여 수정된다.

#include <stdlib.h>

struct flexArrayStruct {
	int num;
	int data[1];
};

void func(size_t array_size) {
	/* Space is allocated for the struct */
	struct flexArrayStruct *structP = (struct flexArrayStruct *)malloc(sizeof(struct flexArrayStruct) + sizeof(int) * (array_size -1));
	if (structP == NULL) {
		/* Handle malloc failure */
	}

	structP->num = array_size;

	/*
	* Access data[] as if it had been allocated
	* as data[array_size].
	*/
	for (size_t i = 0; i < array_size; ++i) {
		structP->data[i] = 1;
	}
}

올바른 코드 예제

가변 길이 배열 멤버를 사용하여 동적 크기 구조체를 구현 한다.

#include <stdlib.h>

struct flexArrayStruct {
	int num;
	int data[];
};

void func(size_t array_size) {
	/* Space is allocated for the struct */
	struct flexArrayStruct *structP = (struct flexArrayStruct *)malloc(sizeof(struct flexArrayStruct) + sizeof(int) * array_size);
	if (structP == NULL) {
		/* Handle malloc failure */
	}
	
	structP->num = array_size;

	/*
	* Access data[] as if it had been allocated
	* as data[array_size].
	*/
	for (size_t i = 0; i < array_size; ++i) {
		structP->data[i] = 1;
	}
}

C 표준에 부합하는 방식으로 멤버 data[]가 data[array_size]로 선언된 것 처럼 처리되도록 구조를 허용한다.

이 블로그의 인기 게시물

Insecure-programming-stack5

Insecure-programming-stack5 Insecure Programming by example Stack 5 이번 문제에서는 조건절 안에 “you win!” 문구 대신 "you loose!"가 있는 것을 볼 수 있다. # include <stdio.h> int main ( ) { int cookie ; char buf [ 80 ] ; printf ( "buf: %08x cookie: %08xn" , & buf , & cookie ) ; gets ( buf ) ; if ( cookie == 0x000d0a00 ) printf ( "you loose!\n" ) ; } 따라서 이번 문제를 해결하기 위해서는 임의로 작성한 별도의 코드를 프로그램에서 호출하도록 하여 “you win!” 구문 출력을 이끌어 내야한다. "you win!"을 출력하고 종료하는 간단한 어셈블리어 코드를 작성한다. section .text global _start _start: jmp short ender starter: xor eax, eax xor ebx, ebx xor ecx, ecx xor edx, edx mov al, 4 mov dbl, 91 pop ecx mov bdl, 14 int 0x80 xor eax, eax inc eaxal dec bl int 0x80 ender: call starter db 'you win!\n' 작성이 완료되었다면 nasm 을 이용하여 오브젝트 파일을 생성한 후 ld 명령어로 실행 파일을 만들어 제대로 동작하는 지 확인해보하고 objdump 를 이용하여 코드 덤프를 만들자. # nasm -f elf -o stack54_sh_code....

Insecure-programming-abo1

Insecure-programming-abo1 Insecure Programming by example Advanced Buffer Overflow 1 int main ( int argv , char * * argc ) { char buf [ 256 ] ; strcpy ( buf , argc [ 1 ] ) ; } Advanced Buffer Overflow 첫 번째 문제는 사용자 인자 값 검증 없이 버퍼에 strcpy() 함수로 복사하여 발생되는 오버플로우 예이다. 먼저 objdump 를 이용하여 어셈블리어를 살펴보자. # objdump -d -M intel ./abo1 | grep -A 16 "<main>" 08048374 <main>: 8048374: 55 push ebp 8048375: 89 e5 mov ebp,esp 8048377: 81 ec 18 01 00 00 sub esp,0x118 804837d: 83 e4 f0 and esp,0xfffffff0 8048380: b8 00 00 00 00 mov eax,0x0 8048385: 29 c4 sub esp,eax 8048387: 8b 45 0c mov eax,DWORD PTR [ebp+12] 804838a: 83 c0 04 add eax,0x4 804838d: 8b 00 mov eax,DWORD PTR [eax] 804838f: 89 44 24 04 mov ...

Remove-Server-Header

응답 메시지 내 서버 버전 정보 제거 1. Apache 1) 조치 방법 “/etc/htpd/conf/httpd.conf” 파일 안에서 1. ServerTokens OS → ServerTokens Prod 2. ServerSignature On → ServerSignature Off 로 변경한 후 아파치를 재시작하면 헤더 값의 아파치 버전 정보 및 OS 정보를 제거할 수 있다. 2) 참고 URL http://zetawiki.com/wiki/CentOS_ 아파치_보안권장설정_ServerTokens_Prod,_ServerSignature_Off 2. IIS 1) 조치 방법 IIS 6.0 urlscan_setup 실행. 설치. \windows\system32\inetsrv\urlscan\urlscan.ini 파일을 열어 다음 수정(RemoveServerHeader=0 을 RemoveServerHeader=1 로 변경) 서비스에서 IIS Admin Service 재시작. IIS 7.0 IIS 관리자를 열고 관리하려는 수준으로 이동합니다. 기능 보기에서 HTTP 응답 헤더를 두 번 클릭합니다. HTTP 응답 헤더 페이지에서 제거할 헤더를 선택합니다. 작업 창에서 제거를 클릭하고 예를 클릭합니다. 2) 참고 URL IIS 6.0 : http://gonnie.tistory.com/entry/iis6- 응답헤더-감추기 IIS 7.0 : https://technet.microsoft.com/ko-kr/library/cc733102(v=ws.10).aspx 3. jetty 1) 조치 방법 “jetty.xml” 파일에서 jetty.send.server.version=false 설정 2) 참고 URL http://attenuated-perspicacity.blogspot.kr/2009/09/jetty-61x-hardening.html 4. Nginx ...