Notice
Recent Posts
Recent Comments
Link
IgnatiusHeo
구현단계 보안약점 제거 기준-부적절한 자원 해제 본문
작성일: 230704
※ 본 게시글은 학습 목적으로 행정안전부·KISA의 소프트웨어 보안약점 진단 가이드, 소프트웨어 개발보안 가이드를 참고하여 작성하였습니다.
정리 내용: 소프트웨어 보안약점 진단 가이드(445~453p)
구분 | - 코드오류 |
설계단계 | |
개요 | 프로그램의 자원, 예를 들면 열린 파일디스크립터(Open File Descriptor), 힙 메모리(Heap Memory), 소켓(Socket) 등은 유한한 자원이다. 이러한 자원을 할당받아 사용한 후, 더 이상 사용하지 않는 경우에는 적절히 반환하여야 하는데, 프로그램 오류 또는 에러로 사용이 끝난 자원을 반환하지 못하는 경우이다. |
진단 세부사항 (설계단계) |
|
보안대책 (구현단계) |
자원을 획득하여 사용한 다음에는 반드시 자원을 해제하여 반환한다. |
진단방법 (구현단계) |
자원(파일기술자, 힙메모리, 소켓)이 선언되고, 선언된 자원이 할당된 경우 해제되는지 확인한다. 진단자는 제어문, 예외처리문 등의 분기 등에 따라 모든 흐름(control flow)을 판단하여 자원해제 여부를 체크해야 한다. 할당된 자원이 해제될 경우 안전하다고 판단하고 자원이 해제되지 않는 분기가 존재할 경우 취약하다. |
다. 코드예제
ㅇ 분석
1: InputStream in = null;
2: OutputStream out = null;
3: try {
4: in = new FileInputStream(inputFile);
5: out = new FileOutputStream(outputFile);
6: ...
7: FileCopyUtils.copy(fis, os);
8: //자원반환 실행 전에 오류가 발생할 경우 자원이 반환되지 않으며, 할당된 모든 자원을 반환해야
한다.
9: in.close();
10: out.close();
11:} catch (IOException e) {
12: logger.error(e);
13:}
ㅇ 내용
1. r9의 close 전에 자원 반환을 수행해야 하나?
ㅇ 수정
1: InputStream in = null;
2: OutputStream out = null;
3: try {
4: in = new FileInputStream(inputFile);
5: out = new FileOutputStream(outputFile);
6: ...
7: FileCopyUtils.copy(fis, os);
8: } catch (IOException e) {
9: logger.error(e);
10: //항상 수행되는 finally 블록에서 할당받은 모든 자원에 대해 각각 null검사를 수행 후 예외처리를
하여 자원을 해제하여야 한다.
11: } finally {
12: if (in != null) {
13: try {
14: in.close();
15: } catch (IOException e) {
16: logger.error(e);
17: }
18: }
19: if (out != null) {
20: try {
21: out.close();
22: } catch (IOException e) {
23: logger.error(e);
24: }
25: }
26:}
ㅇ 내용
1. null이 아닌 경우에만 자원을 해제함
ㅇ 분석
1: public void FileStreamTest()
2: {
3: // fsSource에 자원이 할당되었으나 해제되지 않습니다.
4: FileStream fsSource = new FileStream(pathSource, FileMode.Open, FileAccess.Read);
5: byte[] bytes = new byte[fsSource.Length];
6: int numBytesToRead = (int)fsSource.Length;
7: int numBytesRead = 0;
8: while(numBytesToRead > 0)
9: {
10: int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);
11: if(n==0) break;
12: numBytesToRead += n;
13: numBytesToRead -= n;
14: }
15: using(FileStream fsNew = new FileStream(pathNew, FileMode.Create,
FileAccess.Write)) { /* OK */
16: fsNew.Write(bytes, 0, numBytesToRead);
17: }
18:}
ㅇ 내용
1. 16라인 뒤에 자원해제를 넣어야 되는건가?
ㅇ 수정
1: public void FileStreamTest()
2: {
//using 구문으로 자원을 할당하면 구문이 끝나는 지점에서 자동으로 자원이
해제됩니다.
3: using(FileStream fsSource = new FileStream(pathSource, FileMode.Open,
FileAccess.Read)){
4: byte[] bytes = new byte[fsSource.Length];
5: int numBytesToRead = (int)fsSource.Length;
6: int numBytesRead = 0;
7: while(numBytesToRead > 0)
8: {
9: int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);
10: if(n==0)
11: break;
12: numBytesToRead += n;
13: numBytesToRead -= n;
14: }
15: }
16: using(FileStream fsNew = new FileStream(pathNew, FileMode.Create,
FileAccess.Write)) { /* OK */
17: fsNew.Write(bytes, 0, numBytesToRead);
18: }
19:}
ㅇ 내용
1. 3라인의 파일스트림에 대해 using구문 붙여서 자동해제를 해야함..
ㅇ 분석
1: void ImproperResourceRelease(char* filename) {
2: char buf[BUF_SIZE];
3: FILE *f = fopen(filename, “r”);
4: if(!checkSomething()) {
5: printf(“Something is wrong”);
6: return;
7: }
8: // checkSomething에서 false를 반환하는 경우, 파일 핸들러를 종료할 수
없습니다.
9: fclose(f);
10:}
ㅇ 내용
1. 5번 뒤에 넣어야되는거지?
ㅇ 수정
1: void ImproperResourceRelease(char* filename) {
2: char buf[BUF_SIZE];
3: FILE *f = fopen(filename, “r”);
4: if(!checkSomething()) {
5: printf(“Something is wrong”);
6: // checkSomthing에서 false를 반환해도 파일 핸들러를 종료하도록 수정
7: fclose(f);
8: return;
9: }
10: fclose(f);
11:}
ㅇ 내용
끝.
'자격 > SW보안약점진단원' 카테고리의 다른 글
구현단계 보안약점 제거 기준-초기화되지 않은 변수 사용 (0) | 2023.07.06 |
---|---|
구현단계 보안약점 제거 기준-해제된 자원 사용 (0) | 2023.07.06 |
구현단계 보안약점 제거 기준-Null Pointer 역참조 (0) | 2023.07.06 |
구현단계 보안약점 제거 기준-부적절한 예외 처리 (0) | 2023.07.06 |
구현단계 보안약점 제거 기준-오류상황 대응 부재 (0) | 2023.07.06 |