계: Code Quality

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

93 개 항목 찾음
취약점
Abstract
이 클래스 내의 readObject() 메서드는 재정의할 수 있는 함수를 호출합니다.
Explanation
deserialization 중에 readObject()는 생성자로 사용되므로 이 함수가 종료될 때까지는 개체 초기화가 완료되지 않습니다. 따라서 Serializable 클래스의 readObject() 함수가 오버라이드 가능 함수를 호출하면 개체가 완전히 초기화되기 전에 오버라이드 메서드가 개체 상태에 접근할 수 있습니다.

예제 1: 다음 readObject() 함수는 오버라이드할 수 있는 메서드를 호출합니다.


...
private void readObject(final ObjectInputStream ois) throws IOException, ClassNotFoundException {
checkStream(ois);
ois.defaultReadObject();
}

public void checkStream(ObjectInputStream stream){
...
}
checkStream() 함수와 이 함수를 포함하는 클래스는 final 및 public이 아니므로 함수를 오버라이드할 수 있습니다. 이로 인해 공격자는 checkStream() 함수를 오버라이드하여 역직렬화 중에 개체에 접근할 수 있습니다.
References
[1] SER09-J. Do not invoke overridable methods from the readObject() method CERT
[2] EXTEND-5: Limit the extensibility of classes and methods Oracle
[3] SERIAL-3: View deserialization the same as object construction Oracle
[4] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[5] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4.1
[6] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[7] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
desc.structural.java.code_correctness_readobject_invokes_overridable_function
Abstract
읽기 전용 키워드에 변수를 선언한 대로 또는 구성자에서 초기화해야 하고 다른 위치에서 수정할 수 없다는 규칙이 적용됩니다. 이는 값 유형에 대해 예상한 대로 작동하지만 개체 및 목록의 콘텐트는 개인 정보 읽기 전용으로 선언한 경우에도 수정할 수 있습니다.
Explanation
getter 전용 속성에서 private readonly 목록 변수를 반환하면 호출 코드가 목록의 콘텐트를 수정할 수 있기 때문에 사실상 목록 쓰기 액세스 권한을 부여하게 되어 private readonly로 선언한 프로그래머의 의도와는 반대가 됩니다.

예제 1: 다음 코드에는 private readonly로 선언된 _item 목록이 포함되어 있습니다.

class Order
{
private readonly List<string> _item = new List<string>();
public IEnumerable<string> Item { get { return _item; } }

public Order()
{
/*class initialize */
}

/*some important function......*/
}
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[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
desc.structural.dotnet.code_correctness_readonly_collection_reference
Abstract
함수가 Checks-Effects-Interaction 패턴을 중단하거나 재진입 공격을 방지하지 않습니다.
Explanation
첫 번째 호출이 완료되기 전에 악의적인 계약이 호출 계약을 다시 호출하면 재진입 공격이 발생합니다. 취약한 계약에서 현재 호출의 결과를 로컬에 적용하기 전에 외부 계약과 상호 작용하는 함수를 노출하면 이러한 공격이 진행됩니다. 그러면 외부 계약이 상호 작용의 제어 흐름을 통제하게 될 수 있습니다.

악의적인 계약에서 피해자가 노출한 함수를 호출하여 해당 함수가 호출 계약과 안전하지 않은 방식으로 상호 작용하는 경우, 공격 계약이 폴백 함수 등을 통해 상호 작용을 수신하여 처리한 후 피해자가 노출한 함수를 즉시 다시 호출함으로써 호출-상호 작용-호출 루프 상태를 야기합니다. 이와 같은 재귀 상태에서는 피해자가 추가 코드를 실행할 수 없게 되며, 상호 작용의 특성에 따라 자산이나 값 중 일부 또는 전체가 유출될 수 있습니다.

Checks-Effects-Interaction 패턴은 보통 스마트 계약에서 잘못된 논리를 방지하는 데 사용됩니다. 해당 패턴은 코드가 필수 조건을 먼저 확인하고 관련 상태 변경(효과)을 수행한 후에 마지막으로 해당 효과와 관련된 외부 계약과 상호 작용을 하도록 지정합니다.

예제 1: 다음 예에서는 Checks-Effects-Interaction 패턴을 준수하지 않고 확인, 상호 작용, 효과 적용을 차례로 수행하므로 재진입 공격이 허용됩니다.

각 코드가 수행하는 작업은 다음과 같습니다.

1. 보낸 사람의 잔액을 확인합니다(Checks).
2. msg.sender.call.value를 통해 호출자에게 Ether를 전송합니다(상호 작용).
3. 보낸 사람의 잔액을 차감하여 상태 변경을 수행합니다(확인).


function withdraw(uint amount) public{
if (credit[msg.sender] >= amount) {
require(msg.sender.call.value(amount)());
credit[msg.sender]-=amount;
}
}
Example 1의 코드는 Checks-Effects-Interaction 패턴을 중단하기 위해 Checks-Interaction-Effects를 대신 수행합니다. 따라서 공격 스마트 계약이 폴백 함수에서 Ether를 수신한 직후 withdraw를 다시 호출하는 경우 재진입이 진행될 수 있으며, 그러면 Ether가 유출되는 재귀 상황이 발생합니다. 잔액을 차감(효과)하는 코드 줄은 실행되지 않기 때문입니다.
References
[1] Enterprise Ethereum Alliance External Calls and Re-entrancy
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 4
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[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 841
[7] Standards Mapping - Smart Contract Weakness Classification SWC-107
desc.structural.solidity.swc107
Abstract
데이터 구조에서 순환 링크를 생성할 수 있는 프로그램은 데이터 구조를 재귀적으로 처리할 때 스택 소진을 유발할 수 있습니다.
Explanation
재귀를 사용하는 것은 연결된 데이터 구조를 만들고 관리하기 위한 필수 요소입니다. 데이터가 순환 링크를 포함하는 경우 재귀는 무한정 처리될 위험이 있으며, 이는 차례로 스택을 고갈시키고 프로그램을 중단시킵니다.

예제 1: 다음 코드 조각은 Apache Log4j2를 사용하여 이 취약점을 보여줍니다.

Marker child = MarkerManager.getMarker("child");
Marker parent = MarkerManager.getMarker("parent");

child.addParents(parent);
parent.addParents(child);

String toInfinity = child.toString();


자식이 재귀적 처리 메서드가 포함된 toString()을 호출할 때 스택 오버플로 예외(스택 소진)를 트리거합니다. 이 예외는 자식과 부모 간의 순환 링크로 인해 발생합니다.
References
[1] DOS-1: Beware of activities that may use disproportionate resources Oracle
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 2
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 2
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 674
[7] Standards Mapping - Payment Card Industry Software Security Framework 1.2 Control Objective C.3.3 - Web Software Attack Mitigation
desc.controlflow.java.code_correctness_stack_exhaustion
Abstract
부동 소수점 값을 String 개체와 비교하는 작업은 불안정하므로 수행해서는 안 됩니다.
Explanation
부동 소수점 값을 String 개체에 비교하려면 먼저 해당 값을 String 개체로 변경해야 합니다. 일반적으로 Double.toString()과 같은 함수를 통해 변경합니다. 부동 소수점 변수의 값과 유형에 따라 String 개체로 변환할 때 해당 값은 "NaN", "Infinity", "-Infinity"가 되거나, 0이 포함된 후행 소수점을 일정 수만큼 포함하거나, 지수 필드를 포함할 수 있습니다. 16진수 문자열로 변환하는 경우에도 표현이 매우 달라질 수 있습니다.

예제 1: 다음 코드는 부동 소수점 변수를 String과 비교합니다.


...
int initialNum = 1;
...
String resultString = Double.valueOf(initialNum/10000.0).toString();
if (s.equals("0.0001")){
//do something
...
}
...
References
[1] NUM11-J. Do not compare or inspect the string representation of floating-point values 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 398
desc.dataflow.java.code_correctness_string_comparison_of_float
Abstract
admin, administrator, guest, root 또는 sa 계정 중 하나를 사용하여 데이터베이스에 연결하려고 했습니다.
Explanation
Windows Azure SQL 데이터베이스는 SQL Server 인증만 지원합니다. Windows 인증(통합 보안)은 지원되지 않습니다. 사용자는 Windows Azure SQL 데이터베이스에 연결할 때마다 자격 증명(로그인 및 암호)을 제공해야 합니다. Microsoft Windows Azure SQL 데이터베이스 일반 지침 및 제한 사항에 따르면 admin, administrator, guest, root, sa와 같은 계정 이름은 사용할 수 없습니다.
References
[1] Security Guidelines and Limitations (Windows Azure SQL Database)
[2] Windows Azure SQL Database Concepts
[3] Transact-SQL Support (Windows Azure SQL Database)
[4] Development Considerations in Windows Azure SQL Database
[5] Managing Databases and Logins in Windows Azure SQL Database
[6] Configure and manage Azure AD authentication with Azure SQL
[7] How to: Connect to Windows Azure SQL Database Using sqlcmd
[8] Copying Databases in Windows Azure SQL Database
[9] Data Types (Windows Azure SQL Database)
[10] Deprecated Database Engine Features in SQL Server 2012
[11] EXECUTE AS (Transact-SQL)
[12] Security Statements
[13] System Stored Procedures (Windows Azure SQL Database)
[14] Guidelines and Limitations (Windows Azure SQL Database)
[15] General Guidelines and Limitations (Windows Azure SQL Database)
[16] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[17] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 1
[18] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 3
[19] Standards Mapping - CIS Google Kubernetes Engine Benchmark confidentiality
[20] Standards Mapping - CIS Kubernetes Benchmark partial
[21] Standards Mapping - Common Weakness Enumeration CWE ID 272
[22] Standards Mapping - Common Weakness Enumeration Top 25 2023 [22] CWE ID 269
[23] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-000381, CCI-002233, CCI-002235
[24] Standards Mapping - General Data Protection Regulation (GDPR) Access Violation
[25] Standards Mapping - NIST Special Publication 800-53 Revision 4 AC-6 Least Privilege (P1)
[26] Standards Mapping - NIST Special Publication 800-53 Revision 5 AC-6 Least Privilege
[27] Standards Mapping - OWASP Top 10 2021 A01 Broken Access Control
[28] Standards Mapping - OWASP Application Security Verification Standard 4.0 1.4.3 Access Control Architectural Requirements (L2 L3), 10.2.2 Malicious Code Search (L2 L3)
[29] Standards Mapping - OWASP Mobile 2024 M1 Improper Credential Usage
[30] Standards Mapping - Payment Card Industry Data Security Standard Version 1.2 Requirement 7.1.1
[31] Standards Mapping - Payment Card Industry Data Security Standard Version 2.0 Requirement 7.1.1
[32] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 7.1.2
[33] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 7.1.2
[34] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 7.1.2
[35] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 7.1.2
[36] Standards Mapping - Payment Card Industry Data Security Standard Version 4.0 Requirement 7.2.2
[37] Standards Mapping - Payment Card Industry Software Security Framework 1.0 Control Objective 5.3 - Authentication and Access Control
[38] Standards Mapping - Payment Card Industry Software Security Framework 1.1 Control Objective 5.3 - Authentication and Access Control
[39] Standards Mapping - Payment Card Industry Software Security Framework 1.2 Control Objective 5.3 - Authentication and Access Control, Control Objective C.2.1.2 - Web Software Access Controls
[40] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3500 CAT II
[41] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3500 CAT II
[42] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3500 CAT II
[43] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3500 CAT II
[44] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3500 CAT II
[45] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3500 CAT II
[46] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3500 CAT II
[47] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-000500 CAT II, APSC-DV-000510 CAT I, APSC-DV-001500 CAT II
[48] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-000500 CAT II, APSC-DV-000510 CAT I, APSC-DV-001500 CAT II
[49] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-000500 CAT II, APSC-DV-000510 CAT I, APSC-DV-001500 CAT II
[50] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-000500 CAT II, APSC-DV-000510 CAT I, APSC-DV-001500 CAT II
[51] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-000500 CAT II, APSC-DV-000510 CAT I, APSC-DV-001500 CAT II
[52] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-000500 CAT II, APSC-DV-000510 CAT I, APSC-DV-001500 CAT II
[53] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-000500 CAT II, APSC-DV-000510 CAT I, APSC-DV-001500 CAT II
[54] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-000500 CAT II, APSC-DV-000510 CAT I, APSC-DV-001500 CAT II
[55] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-000500 CAT II, APSC-DV-000510 CAT I, APSC-DV-001500 CAT II
[56] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-000500 CAT II, APSC-DV-000510 CAT I, APSC-DV-001500 CAT II
[57] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-000500 CAT II, APSC-DV-000510 CAT I, APSC-DV-001500 CAT II
[58] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-000500 CAT II, APSC-DV-000510 CAT I, APSC-DV-001500 CAT II
[59] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-000500 CAT II, APSC-DV-000510 CAT I, APSC-DV-001500 CAT II
[60] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-000500 CAT II, APSC-DV-000510 CAT I, APSC-DV-001500 CAT II
[61] Standards Mapping - Web Application Security Consortium Version 2.00 Insufficient Authorization (WASC-02)
[62] Standards Mapping - Web Application Security Consortium 24 + 2 Insufficient Authorization
desc.structural.sql.code_quality_database_authentication_use_of_restricted_accounts
Abstract
이 문은 결코 실행되지 않습니다.
Explanation
주변의 코드가 이 문의 실행을 불가능하게 합니다.

예제: 두 번째 if 문에 대한 조건을 만족할 수 없습니다. s에 null이 아닌 값을 지정할 수 있는 경로에서만, return 문이 있는 동안 s가 null이 아닌 값이어야 합니다.


String s = null;

if (b) {
s = "Yes";
return;
}

if (s != null) {
Dead();
}
desc.internal.cpp.dead_code
Abstract
함수가 효과가 없는 코드를 정의합니다.
Explanation
개발자는 Solidity에서는 효과가 없는 코드를 작성할 수 있습니다. 그런데 이러한 코드를 작성하면 예기치 않은 동작이 발생하거나 코드가 의도한 작업을 수행하지 않게 될 수 있습니다.

예제 1: 다음 코드는 = 대신 ==를 사용해 msg.sender의 잔액 업데이트를 시도하는 효과가 없는 코드입니다.


function deposit(uint amount) public payable {
require(msg.value == amount, 'incorrect amount');
balance[msg.sender] == amount;
}
desc.structural.solidity.swc135