계: Code Quality

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

93 개 항목 찾음
취약점
Abstract
상태 변수가 표시 유형 수준을 명시적으로 지정하지 않습니다.
Explanation
개발자는 Solidity 스마트 계약 개발 시 상태 변수의 표시 유형을 설정하여 해당 함수를 가져오거나 설정할 수 있는 사용자를 제어해야 합니다.

상태 변수 표시 유형을 명시적으로 설정하면 해당 변수에 액세스할 수 있는 사용자에 대한 잘못된 가정을 더욱 쉽게 파악할 수 있습니다.

예제 1: 다음 코드는 변수의 명시적 표시 유형 수준을 설정하지 않습니다.


bytes16 data = "data";
References
[1] Enterprise Ethereum Alliance Code Linting
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 2
[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 710
[7] Standards Mapping - Smart Contract Weakness Classification SWC-108
desc.structural.solidity.swc108
Abstract
계약이 생성자를 선언하지 않습니다.
Explanation
0.5.0 이전 버전의 Solidity 컴파일러 사용 시 개발자는 함수가 포함되는 계약과 같은 이름으로 함수를 생성하여 생성자를 정의할 수 있습니다. 생성자는 대개 중요한 기능용으로 예약되며 계약 생성 시에만 실행됩니다. 생성자에 오타가 있어서 생성자 이름이 계약 이름과 일치하지 않으면 생성자의 중요한 기능이 노출됩니다.

예제 1: 다음 코드는 0.5.0 이전 버전의 Solidity 컴파일러를 사용하며 계약 이름과 정확하게 일치하지 않는 이름으로 생성자 선언을 시도합니다. 이 예에서는 계약 이름과 생성자 이름의 대/소문자가 Missing, missing으로 서로 일치하지 않습니다.


pragma solidity 0.4.20;

contract Missing {
address private owner;
function missing() public {
owner = msg.sender;
}
}
References
[1] Enterprise Ethereum Alliance Declare Explicit Constructors
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 2
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 2
[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 665
[7] Standards Mapping - Smart Contract Weakness Classification SWC-118
desc.structural.solidity.swc118
Abstract
계약이 오래된 Solidity 컴파일러 버전을 사용하므로 공개된 버그와 취약성에 노출될 수 있습니다.
Explanation
개발자는 Solidity 스마트 계약을 생성할 때 테스트 시 기준으로 사용했던 버전을 명확하게 설정하여 다른 컴파일러 버전 사용 시의 문제를 방지하기 위해 사용할 컴파일러 버전을 지정할 수 있습니다. 하지만 컴파일러와 관련된 여러 가지 취약성이 장기간에 걸쳐 공개되어 왔으며 이러한 문제를 해결하기 위해 최신 패치가 적용된 컴파일러 버전이 생성되었습니다.

References
[1] Enterprise Ethereum Alliance Compiler Bugs
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 5
[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 - Smart Contract Weakness Classification SWC-102
desc.structural.solidity.swc102
Abstract
계약이 부동 pragma를 사용하며 Solidity 컴파일러가 특정 버전으로 잠금 설정되어 있지 않습니다.
Explanation
개발자는 스마트 계약 생성 시에 사용할 호환 가능한 Solidity 컴파일러 버전의 범위를 지정할 수 있습니다. 그런데 계약 개발과 테스트는 대개 사용 가능한 버전 중 하나에서만 진행되므로 이러한 지정 방식은 사용하지 않는 것이 좋니다. 이러한 지정 방식을 사용하는 경우 계약 컴파일 시 알려진 보안 취약성이 있는 오래된 컴파일러 버전가 사용될 수 있습니다.

예제 1: 다음 코드 줄은 스마트 계약이 0.4.5 이전 버전에서 컴파일되지 않는 동시에 0.5.0 이상 버전 컴파일러에서 작동하지 않도록 pragma를 설정합니다.


pragma solidity ^0.4.5;
References
[1] Enterprise Ethereum Alliance Source code, pragma, and compilers
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 5
[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 664
[7] Standards Mapping - Smart Contract Weakness Classification SWC-103
desc.structural.solidity.swc103
Abstract
이 함수는 int로 배정된 unsigned char를 반환하지만 반환 값은 char 형식에 할당됩니다.
Explanation
정수로 배정된 부호 없는 문자가 부호 있는 문자에 할당되는 경우, 해당 값이 EOF와 구별되지 않을 수도 있습니다.

예제 1: 다음 코드는 문자를 읽고 이를 EOF와 비교합니다.


char c;

while ( (c = getchar()) != '\n' && c != EOF ) {
...
}


이 경우, getchar()의 반환 값은 char로 배정되고 EOF(int)와 비교됩니다. c가 부호가 있는 8 비트 값이고 EOF 부호가 있는 32 비트 값이라고 가정하여 getchar()가 0xFF로 표시된 문자를 반환하는 경우, c의 값은 EOF와 비교해 볼 때 0xFFFFFFFF로 확장된 부호가 됩니다. EOF는 일반적으로 -1(0xFFFFFFFF)로 정의되기 때문에 루프가 실수로 종료됩니다.
References
[1] Distinguish between characters read from a file and EOF or WEOF CERT
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[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 192
[7] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2012 Rule 10.3
[8] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C++ Guidelines 2008 Rule 5-0-3
[9] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 6.5.6
[10] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 6.5.6
[11] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 6.5.6
[12] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 6.5.6
[13] Standards Mapping - Payment Card Industry Data Security Standard Version 4.0 Requirement 6.2.4
[14] Standards Mapping - Payment Card Industry Software Security Framework 1.0 Control Objective 4.2 - Critical Asset Protection
[15] Standards Mapping - Payment Card Industry Software Security Framework 1.1 Control Objective 4.2 - Critical Asset Protection
[16] Standards Mapping - Payment Card Industry Software Security Framework 1.2 Control Objective 4.2 - Critical Asset Protection
[17] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3550 CAT I
[18] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3550 CAT I
[19] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3550 CAT I
[20] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3550 CAT I
[21] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3550 CAT I
[22] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3550 CAT I
[23] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3550 CAT I
desc.structural.cpp.type_mismatch_integer_to_character
Abstract
함수가 부호 없는 값을 반환하도록 선언되어 있는데 경우에 따라 음수 값 반환을 시도합니다.
Explanation
예상치 않은 값이 나타날 수 있고 프로그램에서 약한 가정의 위반이 발생할 수 있으므로 부호 있는 수와 부호 없는 수 간의 암시적 캐스트에 의지하는 것은 위험합니다.

예제 1: 이 예제에서 변수 amount는 반환될 때 음수 값을 가질 수 있습니다. 함수가 부호 없는 정수를 반환하도록 선언되었으므로 amount는 암시적으로 부호가 없는 상태로 변환됩니다.


unsigned int readdata () {
int amount = 0;
...
if (result == ERROR)
amount = -1;
...
return amount;
}
Example 1의 코드에서 오류 조건이 충족되는 경우 32비트 정수를 사용하는 시스템에서 readdata()의 반환 값은 4,294,967,295입니다.

부호 있는 값과 부호 없는 값 간의 변환은 다양한 오류를 발생시키지만 보안 측면에서 보면 integer overflow 및 buffer overflow 취약점과 가장 많이 관련되어 있습니다.
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 195
[6] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-002824
[7] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2012 Rule 10.3
[8] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C++ Guidelines 2008 Rule 5-0-3
[9] Standards Mapping - NIST Special Publication 800-53 Revision 4 SI-16 Memory Protection (P1)
[10] Standards Mapping - NIST Special Publication 800-53 Revision 5 SI-16 Memory Protection
[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 Data Security Standard Version 4.0 Requirement 6.2.4
[16] Standards Mapping - Payment Card Industry Software Security Framework 1.0 Control Objective 4.2 - Critical Asset Protection
[17] Standards Mapping - Payment Card Industry Software Security Framework 1.1 Control Objective 4.2 - Critical Asset Protection
[18] Standards Mapping - Payment Card Industry Software Security Framework 1.2 Control Objective 4.2 - Critical Asset Protection
[19] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3550 CAT I
[20] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3550 CAT I
[21] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3550 CAT I
[22] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3550 CAT I
[23] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3550 CAT I
[24] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3550 CAT I
[25] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3550 CAT I
[26] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002590 CAT I
[27] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002590 CAT I
[28] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002590 CAT I
[29] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002590 CAT I
[30] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002590 CAT I
[31] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002590 CAT I
[32] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002590 CAT I
[33] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002590 CAT I
[34] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002590 CAT I
[35] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002590 CAT I
[36] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002590 CAT I
[37] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002590 CAT I
[38] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002590 CAT I
[39] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002590 CAT I
desc.structural.cpp.type_mismatch_negative_to_unsigned
Abstract
부호 없는 변수에 부호 있는 숫자가 할당됩니다.
Explanation
예상치 않은 값이 나타날 수 있고 프로그램에서 약한 가정의 위반이 발생할 수 있으므로 부호 있는 수와 부호 없는 수 간의 암시적 캐스트에 의지하는 것은 위험합니다.

예제 1: 이 예제에서 accecssmainframe()의 반환 값에 따라 변수 amount는 반환될 때 음수 값을 가질 수 있습니다. 함수가 부호 없는 값을 반환하도록 선언되었으므로 amount는 암시적으로 부호가 없는 수를 가리킵니다.


unsigned int readdata () {
int amount = 0;
...
amount = accessmainframe();
...
return amount;
}
accessmainframe()의 반환 값이 -1인 경우 32비트 정수를 사용하는 시스템에서 readdata()의 반환 값은 4,294,967,295입니다.

부호 있는 값과 부호 없는 값 간의 변환은 다양한 오류를 발생시키지만 보안 측면에서 보면 integer overflow 및 buffer overflow 취약점과 가장 많이 관련되어 있습니다.
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 195
[6] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-002824
[7] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2012 Rule 10.3
[8] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C++ Guidelines 2008 Rule 5-0-3
[9] Standards Mapping - NIST Special Publication 800-53 Revision 4 SI-16 Memory Protection (P1)
[10] Standards Mapping - NIST Special Publication 800-53 Revision 5 SI-16 Memory Protection
[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 Data Security Standard Version 4.0 Requirement 6.2.4
[16] Standards Mapping - Payment Card Industry Software Security Framework 1.0 Control Objective 4.2 - Critical Asset Protection
[17] Standards Mapping - Payment Card Industry Software Security Framework 1.1 Control Objective 4.2 - Critical Asset Protection
[18] Standards Mapping - Payment Card Industry Software Security Framework 1.2 Control Objective 4.2 - Critical Asset Protection
[19] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3550 CAT I
[20] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3550 CAT I
[21] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3550 CAT I
[22] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3550 CAT I
[23] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3550 CAT I
[24] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3550 CAT I
[25] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3550 CAT I
[26] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002590 CAT I
[27] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002590 CAT I
[28] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002590 CAT I
[29] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002590 CAT I
[30] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002590 CAT I
[31] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002590 CAT I
[32] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002590 CAT I
[33] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002590 CAT I
[34] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002590 CAT I
[35] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002590 CAT I
[36] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002590 CAT I
[37] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002590 CAT I
[38] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002590 CAT I
[39] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002590 CAT I
desc.structural.cpp.type_mismatch_signed_to_unsigned