계: Code Quality

코드 품질이 낮으면 예측할 수 없는 동작이 발생합니다. 사용자 입장에서는 사용 편의성이 떨어지는 것으로 나타나는 경우가 많습니다. 공격자에게는 예상치 못한 방법으로 시스템에 부담을 줄 수 있는 기회가 됩니다.

93 개 항목 찾음
취약점
Abstract
함수가 assert Solidity 함수를 사용하여 false 문을 확인합니다.
Explanation
assert Solidity 함수는 true로 평가되는 문 확인 전용입니다. 이 함수에 false 문이 전달되면 함수가 올바르게 작동하지 않는 코드를 가리키거나 입력 유효성 검사 등에서 함수가 잘못 사용됩니다.

예제 1: 다음 코드는 assert 함수를 사용하여 false 문을 확인합니다.


contract A {
B b = new B(7);

function checkWithAssert(){
assert(b.retValue() == 21);
...
}

}

contract B {
uint _par;
constructor(uint par){
_par = par;
}

function retValue() returns(uint){
return _par;
}
}
References
[1] Enterprise Ethereum Alliance No failing assert statements
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 3
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 670
[7] Standards Mapping - Smart Contract Weakness Classification SWC-110
desc.structural.solidity.swc110
Abstract
마지막 괄호가 없기 때문에 이 식은 함수의 반환 값이 아닌 함수 포인터의 값을 참조합니다.
Explanation
이 표현식은 함수의 반환 값보다 함수 포인터를 참조하므로 항상 null이 아닙니다.

예제 1: 다음과 같은 조건절은 작동하지 않습니다. 조건부 getChunk == NULLgetChunk이 프로그램에 정의된 함수의 이름이므로 항상 false가 됩니다.


if (getChunk == NULL)
return ERR;
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4.1
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398
[6] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 6.5.6
[7] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 6.5.6
[8] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 6.5.6
[9] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 6.5.6
[10] Standards Mapping - Payment Card Industry Software Security Framework 1.0 Control Objective 4.2 - Critical Asset Protection
[11] Standards Mapping - Payment Card Industry Software Security Framework 1.1 Control Objective 4.2 - Critical Asset Protection
[12] Standards Mapping - Payment Card Industry Software Security Framework 1.2 Control Objective 4.2 - Critical Asset Protection
[13] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3050 CAT II
[14] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3050 CAT II
[15] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3050 CAT II
[16] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3050 CAT II
[17] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3050 CAT II
[18] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3050 CAT II
[19] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3050 CAT II
desc.structural.cpp.code_correctness_function_not_invoked
Abstract
스택 변수의 주소를 반환하면 의도하지 않은 프로그램 동작이 중단 형태로 나타납니다.
Explanation
로컬 변수가 스택에서 할당되므로 프로그램이 로컬 변수로 포인터를 반환할 때 스택 주소가 반환됩니다. 이후의 함수 호출은 같은 스택 주소를 다시 사용할 가능성이 높고 따라서 함수의 스택 프레임이 반환될 때 무효화된 이후 같은 변수와 더 이상 일치하지 않는 포인터의 값을 덮어쓰게 됩니다. 이 경우에는 예상치 않게 포인터의 값이 변경되는 문제만 일어납니다. 상당수의 경우, 포인터가 역참조된 다음에 프로그램이 중단됩니다. 이 문제는 증상과는 관계가 없는 경우도 있어 디버깅이 어려울 수 있습니다.

예제 1: 다음 함수는 스택 주소를 반환합니다.


char* getName() {
char name[STR_MAX];
fillInName(name);
return name;
}
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 562
[6] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-001094
[7] Standards Mapping - NIST Special Publication 800-53 Revision 4 SC-5 Denial of Service Protection (P1)
[8] Standards Mapping - NIST Special Publication 800-53 Revision 5 SC-5 Denial of Service Protection
[9] Standards Mapping - OWASP Top 10 2004 A9 Application Denial of Service
[10] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 Requirement 6.5.9
[11] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 6.5.6
[12] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 6.5.6
[13] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 6.5.6
[14] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 6.5.6
[15] Standards Mapping - Payment Card Industry Software Security Framework 1.0 Control Objective 4.2 - Critical Asset Protection
[16] Standards Mapping - Payment Card Industry Software Security Framework 1.1 Control Objective 4.2 - Critical Asset Protection
[17] Standards Mapping - Payment Card Industry Software Security Framework 1.2 Control Objective 4.2 - Critical Asset Protection
[18] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP6080 CAT II
[19] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP6080 CAT II
[20] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP6080 CAT II
[21] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP6080 CAT II
[22] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP6080 CAT II
[23] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP6080 CAT II
[24] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP6080 CAT II
[25] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002400 CAT II
[26] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002400 CAT II
[27] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002400 CAT II
[28] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002400 CAT II
[29] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002400 CAT II
[30] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002400 CAT II
[31] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002400 CAT II
[32] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002400 CAT II
[33] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002400 CAT II
[34] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002400 CAT II
[35] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002400 CAT II
[36] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002400 CAT II
[37] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002400 CAT II
[38] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002400 CAT II
[39] Standards Mapping - Web Application Security Consortium Version 2.00 Denial of Service (WASC-10)
[40] Standards Mapping - Web Application Security Consortium 24 + 2 Denial of Service
desc.controlflow.cpp.code_correctness_function_returns_stack_address
Abstract
정적 메서드는 재정의할 수는 없지만, 인스턴스 메서드로 호출하는 경우 숨겨진 것으로 표시될 수 있습니다.
Explanation
정적 메서드는 클래스의 인스턴스가 아닌 클래스 자체에 속하므로 원칙적으로 오버라이드할 수 없습니다. 정적 메서드가 하위 클래스에서 오버라이드된 것처럼 표시되는 경우도 있는데, 이로 인해 혼동을 하여 잘못된 메서드 버전을 호출하게 될 수도 있습니다.

예제 1: 다음 코드는 사용자 인증을 위한 API를 정의합니다.


class AccessLevel{
public static final int ROOT = 0;
//...
public static final int NONE = 9;
}
//...
class User {
private static int access;
public User(){
access = AccessLevel.ROOT;
}
public static int getAccessLevel(){
return access;
}
//...
}
class RegularUser extends User {
private static int access;
public RegularUser(){
access = AccessLevel.NONE;
}
public static int getAccessLevel(){
return access;
}
public static void escalatePrivilege(){
access = AccessLevel.ROOT;
}
//...
}
//...
class SecureArea {
//...
public static void doRestrictedOperation(User user){
if (user instanceof RegularUser){
if (user.getAccessLevel() == AccessLevel.ROOT){
System.out.println("doing a privileged operation");
}else{
throw new RuntimeException();
}
}
}
}


이 코드는 처음 보기에는 아무 문제가 없는 것 같습니다. 그러나 User 또는 RegularUser 클래스가 아닌 user 인스턴스에 대해 getAccessLevel() 메서드를 호출하므로 이 조건에서는 항상 true가 반환됩니다. 그리고 이 if/else 블록 부분으로 진입하기 위해 instanceof를 사용했더라도 제한된 작업이 수행됩니다.
References
[1] MET07-J. Never declare a class method that hides a method declared in a superclass or superinterface CERT
[2] Java Language Specification Chapter 8. Classes Oracle
[3] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[4] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 3
[5] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[6] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[7] Standards Mapping - Common Weakness Enumeration CWE ID 486
desc.structural.java.code_correctness_hidden_method
Abstract
serialPersistentFields를 올바르게 사용하려면 private, staticfinal로 선언되어야 합니다.
Explanation
Java Object Serialization Specification을 사용하면 개발자는 serialPersistentFields 배열에서 지정하여 클래스에 대해 Serializable 필드를 수동으로 정의할 수 있습니다. 이 기능은 serialPersistentFieldsprivate, staticfinal로 선언된 경우에만 작동합니다.

예제 1: 다음 serialPersistentFields 선언은 private, staticfinal이 아니기 때문에 Serializable 필드를 정의하는 데 사용되지 않습니다.

class List implements Serializable {
public ObjectStreamField[] serialPersistentFields = { new ObjectStreamField("myField", List.class) };
...
}
References
[1] Sun Microsystems, Inc. Java Sun Tutorial
[2] SERIAL-2: Guard sensitive data during serialization Oracle
[3] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[4] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[5] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[6] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[7] Standards Mapping - Common Weakness Enumeration CWE ID 485
desc.structural.java.code_correctness_incorrect_serialpersistentfields_modifier
Abstract
이 프로그램은 java.util.Arrays.equals(). 대신 배열에 대해 Object.equals()를 호출합니다.
Explanation
대부분의 경우에는 배열에 대한 Object.equals() 호출은 잘못된 것입니다. 이와 같이 호출하면 배열 요소가 아니라 배열 주소가 같은지를 확인하기 때문입니다. 일반적으로는 java.util.Arrays.equals()를 대신 호출해야 합니다.

예제 1: 다음 코드에서는 Object.equals() 함수를 사용하여 두 배열 확인을 시도합니다.


...
int[] arr1 = new int[10];
int[] arr2 = new int[10];
...
if (arr1.equals(arr2)){
//treat arrays as if identical elements
}
...


특정 지점에서 한 배열을 다른 배열에 할당하는 경우가 아니면 이러한 코드는 거의 항상 실행되지 않습니다.
References
[1] EXP02-J. Do not use the Object.equals() method to compare two arrays CERT
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 398, CWE ID 754
[7] Standards Mapping - OWASP Application Security Verification Standard 4.0 11.1.7 Business Logic Security Requirements (L2 L3)
[8] Standards Mapping - SANS Top 25 2010 Risky Resource Management - CWE ID 754
desc.structural.java.code_correctness_call_to_object_equals
Abstract
공유 리소스에서 작동하고 일부 플랫폼에서 매크로로 구현되는 함수 패밀리는 동일한 프로그램 범위에서 호출되어야 합니다.
Explanation
특정한 함수 패밀리는 일부 플래폼 및 다른 매크로에서 함수로써 구현됩니다. 호출되는 곳으로 전달되기 보다는 내부적으로 유지되는 공유 리소스를 함수가 의지하는 경우, 이 함수는 동일한 프로그램 범위에서 사용해야 하며 그렇지 않을 경우 공유 리소스에 접근할 수 없습니다.

예제 1: 다음 코드는 pthread_cleanup_push()를 사용하여 함수 routine을 호출 스레드의 정리 스택으로 푸시하고 반환합니다. pthread_cleanup_push()와 해당 파트너 함수인 pthread_cleanup_pop()은 IBM AIX가 아닌 플랫폼에서 매크로로 구현되므로 pthread_cleanup_push()에 의해 생성되는 데이터 구조는 이후의 pthread_cleanup_pop() 호출에 액세스할 수 없게 됩니다. 코드가 컴파일에 실패하거나 이러한 함수가 매크로로 구현되는 모든 플랫폼에서는 런타임 시 코드가 올바로 작동하지 않습니다.


void helper() {
...
pthread_cleanup_push (routine, arg);
}
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 3
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 730
[6] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-001094
[7] Standards Mapping - NIST Special Publication 800-53 Revision 4 SC-5 Denial of Service Protection (P1)
[8] Standards Mapping - NIST Special Publication 800-53 Revision 5 SC-5 Denial of Service Protection
[9] Standards Mapping - OWASP Top 10 2004 A9 Application Denial of Service
[10] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 Requirement 6.5.9
[11] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP6080 CAT II
[12] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP6080 CAT II
[13] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP6080 CAT II
[14] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP6080 CAT II
[15] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP6080 CAT II
[16] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP6080 CAT II
[17] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP6080 CAT II
[18] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002400 CAT II
[19] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002400 CAT II
[20] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002400 CAT II
[21] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002400 CAT II
[22] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002400 CAT II
[23] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002400 CAT II
[24] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002400 CAT II
[25] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002400 CAT II
[26] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002400 CAT II
[27] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002400 CAT II
[28] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002400 CAT II
[29] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002400 CAT II
[30] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002400 CAT II
[31] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002400 CAT II
[32] Standards Mapping - Web Application Security Consortium Version 2.00 Denial of Service (WASC-10)
[33] Standards Mapping - Web Application Security Consortium 24 + 2 Denial of Service
desc.controlflow.cpp.code_correctness_macro_misuse