界: Code Quality

コードの質が低いと、予測できない動作につながります。ユーザーの視点には、それがしばしば使い勝手の悪さとなって現れます。攻撃者にとっては、予期せぬ方法でシステムにストレスを与える機会となります。

93 見つかった項目
脆弱性
Abstract
クラス内の readObject() メソッドは、オーバーライドされる可能性のある関数を呼び出します。
Explanation
デシリアライゼーションの間、readObject() は、コンストラクターのように振る舞うため、オブジェクトの初期化は関数が終わるまで完了しません。そのため、SerializablereadObject() 関数がオーバーライド関数をコールすると、完全に初期化される前にオブジェクトの状態にオーバーライド メソッドがアクセスできるようになります。

例 1: 次の readObject() 関数は、オーバーライドされる可能性のあるメソッドをコールしています。


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

public void checkStream(ObjectInputStream stream){
...
}


関数 checkStream() と括弧で囲まれたクラスは final ではなくパブリックであるため、関数がオーバーライドできるようになります。つまり、攻撃者は 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
readonly キーワードにより、変数は宣言した時点またはコンストラクタ内で初期化する必要があり、他の場所では変更できないというルールが強制適用されます。これは値の型に対しては期待どおりに動作しますが、オブジェクトのコンテンツとリストは、private readonly として宣言されている場合にも引き続き変更可能です。
Explanation
private readonly リスト変数を getter-only プロパティから戻すと、呼び出し元のコードはリストのコンテンツを変更でき、実質的にリストに書き込みアクセスを許可するので、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
リエントランシー攻撃は、最初の呼び出しが終了する前に、悪意のあるコントラクトが呼び出し元コントラクトにコールバックするときに発生します。これは、脆弱なコントラクトが、現在の呼び出しのエフェクトをローカルで実行する前に、外部コントラクトとインタラクションを行う関数を公開するときに発生します。これにより、外部コントラクトがインタラクションの制御フローを引き継ぐ可能性があります。

悪意のあるコントラクトが、呼び出し元コントラクトと安全でない方法で対話している被害者の公開済み関数を呼び出すと、攻撃者のコントラクトはそのインタラクションを (たとえばフォールバック関数経由で) 受信して処理し、call-interact-call ループに入るためにすぐに被害者の公開済み関数をコールバックします。この再帰的な状態になると、被害者側ではそれ以上コードを実行できなくなり、その結果、インタラクションの性質に応じて資産や価値が部分的または全体的に流出する可能性があります。

Checks-Effects-Interaction パターンは、誤ったロジックを防ぐためにスマート コントラクトでよく使用されます。このパターンは、コードがまず必要な条件をチェックし、続いて関連する状態変更を実行し (エフェクト)、最後にそのエフェクトに関連して外部コントラクトとのインタラクションを行うことを指定しています。

例 1: 次の例は、Checks-Effects-Interaction パターンに従わずに、チェック、インタラクション、エフェクトを実行することにより、リエントランシー攻撃を許可しています。

コード:

1.送信者の残高を確認します (チェック)。
2.msg.sender.call.value 経由で呼び出し元にイーサを送信します (インタラクション)。
3.送信者の残高を減らすことで状態変化を行います (エフェクト)。


function withdraw(uint amount) public{
if (credit[msg.sender] >= amount) {
require(msg.sender.call.value(amount)());
credit[msg.sender]-=amount;
}
}
Example 1 のコードは、Checks-Interaction-Effects を実行するのではなく、Checks-Effects-Interaction パターンを壊しています。これは、攻撃するスマート コントラクトがフォールバック関数でイーサを受け取り、すぐに withdraw をコールバックした場合に、リエントランシーにつながるおそれがあります。これにより、残高を減らすコード行 (エフェクト) が実行されないためにイーサが流出するという再帰的な状況が作られます。
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"、ゼロを含む一定の小数値を持つ値、または指数フィールドを含む値になる場合があります。16 進数の String に変換された場合も、表現が大幅に異なることがあります。

例 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
このステートメントは、周辺のコードによって実行されてないようになっています。

例: 2 つ目の if ステートメントで条件が満たされることはありません。変数 s には NULL 以外の値が求められていますが、一方で、s に NULL 以外の値を割り当てることができるパスには、return ステートメントが記述されています。


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