界: Code Quality

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

93 見つかった項目
脆弱性
Abstract
予期しない移植の問題は、ロケールが指定されていない場合に発生します。
Explanation
ロケール依存のデータを比較する場合、適切なロケールを指定する必要があります。

例 1: 次の例では、ユーザー入力に <script> タグが含まれているかを判定するための検証を実行しています。

...
public String tagProcessor(String tag){
if (tag.toUpperCase().equals("SCRIPT")){
return null;
}
//does not contain SCRIPT tag, keep processing input
...
}
...
Example 1 の問題は、ロケールを指定せずに java.lang.String.toUpperCase() を使用した場合、デフォルトのロケールを使用するルールが適用されることです。トルコ語ロケールを使用すると "title".toUpperCase() は "T\u0130TLE" を返します。ここで "\u0130" は、"上に点が付いたラテン語の大文字 I" になります。これは、Example 1 のように "script" という単語が検証でキャッチされないなどの予期しない結果につながる場合があり、クロスサイト スクリプティングの脆弱性を引き起こす可能性があります。
References
[1] STR02-J. Specify an appropriate locale when comparing locale-dependent data CERT
[2] String (JavaDoc) 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 474
[8] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-001310
[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 4.1 APSC-DV-002520 CAT II
[18] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002520 CAT II
[19] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002520 CAT II
[20] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002520 CAT II
[21] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002520 CAT II
[22] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002520 CAT II
[23] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002520 CAT II
[24] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002520 CAT II
[25] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002520 CAT II
[26] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002520 CAT II
[27] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002520 CAT II
[28] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002520 CAT II
[29] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002520 CAT II
[30] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002520 CAT II
desc.controlflow.java.portability_flaw_locale_dependent_comparison
Abstract
ネイティブ SQL を使用すると、移植に関する問題が引き起こされる可能性があります。
Explanation
SAP システムは、プラットフォームに依存しないように設計されています。SAP の移植性のある SQL 言語である Open SQL を使用すると、アプリケーションが特定のデータベース ベンダーの JDBC ドライバーに依存しなくなります。Open SQL を使用すると、基盤となるデータベースの複雑さを抽象化し、アプリケーション プログラムにすべてのデータベース操作に対する共通インターフェイスを提供することができます。しかし、ネイティブ SQL は基盤となるデータベースに固有であるため、他のプラットフォームで使用すると、アプリケーション ロジックが正しく実行されずサービスの妨害となる可能性もあります。
例 1: 次のコードは、ネイティブ SQL を使用しています。


...
import java.sql.PreparedStatement;
import com.sap.sql.NativeSQLAccess;

String mssOnlyStmt = "...";
// variant 1
PreparedStatement ps =
NativeSQLAccess.prepareNativeStatement(
conn, mssOnlyStmt);
. . .
// variant 2
Statement stmt =
NativeSQLAccess.createNativeStatement(conn);
int result = stmt.execute(mssOnlyStmt);
. . .
// variant 3
CallableStatement cs =
NativeSQLAccess.prepareNativeCall(
conn, mssOnlyStmt);
. . .
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 474
desc.structural.java.portability_flaw_native_sql
Abstract
静的フィールドを新しいオブジェクトに割り当てると、他の変数の初期化に依存している場合でもコンストラクターが呼び出され、オブジェクトが正しく初期化されない可能性があります。
Explanation
Java クラスが初期化された場合、クラスで宣言された static フィールドの初期化子をクラス コンストラクターより前にコールします。つまり、割り当てられたコンストラクターはその他のコードの前にコールされ、初期化されるその他のフィールドや変数にコンストラクターが依存する場合、部分的に初期化されたオブジェクトや誤った値で初期化されたオブジェクトにつながる可能性があります。

例 1: 次のクラスでは static フィールドを宣言し、新しいオブジェクトに割り当てています。


...
public class Box{
public int area;
public static final int width = 10;
public static final Box box = new Box();
public static final int height = (int) (Math.random() * 100);

public Box(){
area = width * height;
}
...
}
...
Example 1 では、width の値が 10 であるため、開発者は box.area が 10 の倍数のランダム整数であると予想します。しかし実際は、常にハードコーディングされた 0 の値です。コンパイル時定数で宣言された static final フィールドは最初に初期化され、順番に実行されます。これは、height はコンパイル時定数ではないため box の宣言の後に宣言され、コンストラクターはフィールド height が初期化される前にコールされることを意味します。

例 2: 次のクラスでは、相互に依存する static フィールドを宣言しています。


...
class Foo{
public static final int f = Bar.b - 1;
...
}
...
class Bar{
public static final int b = Foo.f + 1;
...
}

This example is perhaps easier to identify, but would be dependent on which class is loaded first by the JVM. In this example Foo.f could be either -1 or 0, and Bar.b could be either 0 or 1.
References
[1] DCL00-J. Prevent class initialization cycles CERT
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Microsoft Azure Foundations Benchmark complete
[4] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 2
[5] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 4
[6] Standards Mapping - CIS Google Kubernetes Engine Benchmark integrity
[7] Standards Mapping - CIS Kubernetes Benchmark partial
[8] Standards Mapping - Common Weakness Enumeration CWE ID 362, CWE ID 367
[9] Standards Mapping - Common Weakness Enumeration Top 25 2022 [22] CWE ID 362
[10] Standards Mapping - Common Weakness Enumeration Top 25 2023 [21] CWE ID 362
[11] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-003178
[12] Standards Mapping - General Data Protection Regulation (GDPR) Indirect Access to Sensitive Data
[13] Standards Mapping - OWASP Top 10 2021 A04 Insecure Design
[14] Standards Mapping - OWASP Application Security Verification Standard 4.0 1.11.2 Business Logic Architectural Requirements (L2 L3), 1.11.3 Business Logic Architectural Requirements (L3), 11.1.6 Business Logic Security Requirements (L2 L3)
[15] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 6.5.6
[16] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 6.5.6
[17] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 6.5.6
[18] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 6.5.6
[19] Standards Mapping - Payment Card Industry Data Security Standard Version 4.0 Requirement 6.2.4
[20] Standards Mapping - Payment Card Industry Software Security Framework 1.0 Control Objective 4.2 - Critical Asset Protection
[21] Standards Mapping - Payment Card Industry Software Security Framework 1.1 Control Objective 4.2 - Critical Asset Protection, Control Objective B.3.3 - Terminal Software Attack Mitigation
[22] Standards Mapping - Payment Card Industry Software Security Framework 1.2 Control Objective 4.2 - Critical Asset Protection, Control Objective B.3.3 - Terminal Software Attack Mitigation
[23] Standards Mapping - SANS Top 25 2009 Insecure Interaction - CWE ID 362
[24] Standards Mapping - SANS Top 25 2010 Insecure Interaction - CWE ID 362
[25] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3630.1 CAT II
[26] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3630.1 CAT II
[27] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3630.1 CAT II
[28] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3630.1 CAT II
[29] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3630.1 CAT II
[30] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3630.1 CAT II
[31] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3630.1 CAT II
[32] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-001995 CAT II
[33] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-001995 CAT II
[34] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-001995 CAT II
[35] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-001995 CAT II
[36] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-001995 CAT II
[37] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-001995 CAT II
[38] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-001995 CAT II
[39] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-001995 CAT II
[40] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-001995 CAT II
[41] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-001995 CAT II
[42] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-001995 CAT II
[43] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-001995 CAT II
[44] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-001995 CAT II
[45] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-001995 CAT II
desc.structural.java.race_condition_class_initialization_cycle
Abstract
プログラムでは NULL ポインターを間接参照する可能性があり、これがセグメンテーション違反の原因になります。
Explanation
NULL ポインタの例外は通常、プログラマの想定が破られた結果として発生します。この問題には、少なくとも次の 3 種類があります。間接参照後チェック、チェック後間接参照、格納後間接参照です。間接参照後チェックのエラーが発生するのは、プログラムで、ポインタが null であるかを確認する前に、null の可能性があるポインタを間接参照する場合です。チェック後間接参照のエラーが発生するのは、プログラムが null に対して明示的チェックを実行したにも関わらず、null であることが判明しているポインタを間接参照した場合です。この種のエラーの多くは、タイプミスかプログラマの不注意が原因です。格納後間接参照のエラーは、プログラムが明示的にポインタを null に設定し、後でそのポインタを間接参照した場合に発生します。このエラーは通常、変数の宣言時にプログラマがその変数を null に初期化したことが原因で発生します。

NULL ポインターの問題点の大半は一般にソフトウェアの信頼性の問題につながります。しかし、攻撃者が意図的に NULL ポインター間接参照を引き起こせる場合、その結果生じた例外を利用してセキュリティ ロジックをバイパスすることで Denial of Service 攻撃を実装したり、アプリケーションにデバッグ情報を開示させてそれ以降の攻撃に役立てたりすることができます。

例 1: 次のコードでは、プログラマはオブジェクト foonull であることを確認してから、それを誤って間接参照しています。fooif ステートメントでチェックされたときに null になっていると、null 間接参照 が発生し、これが NULL ポインター例外の原因となります。


if (foo is null) {
foo.SetBar(val);
...
}
例 2: 次のコードでは、プログラマは変数 foonull ではないと仮定しており、オブジェクトを間接参照することによってこの仮定を確認しています。しかし、その後プログラマが foonull を比較したときに、この仮定は成り立たなくなります。fooif ステートメントでチェックされたときに null であるとすれば、間接参照されたときにも null である可能性があり、これが NULL ポインタ例外の原因となる場合があります。間接参照が安全でないか、後で実行するチェックが不要であるかのいずれかです。


foo.SetBar(val);
...
if (foo is not null) {
...
}
例 3: 次のコードでは、プログラマは明示的に変数 foonull に設定しています。続いて、オブジェクトの null 値をチェックする前に foo を間接参照しています。


Foo foo = null;
...
foo.SetBar(val);
...
}
desc.controlflow.dotnet.redundant_null_check
Abstract
プログラムでは潜在的に NULL ポインタを間接参照する可能性があり、これがセグメンテーション違反の原因になります。
Explanation
NULL ポインタの例外は通常、プログラマの想定が破られた結果として発生します。この問題には、少なくとも次の 3 種類があります。間接参照後チェック、チェック後間接参照、格納後間接参照です。間接参照後チェックのエラーが発生するのは、プログラムで、ポインタが null であるかを確認する前に、null の可能性があるポインタを間接参照する場合です。チェック後間接参照のエラーが発生するのは、プログラムが null に対して明示的チェックを実行したにも関わらず、null であることが判明しているポインタを間接参照した場合です。この種のエラーの多くは、タイプミスかプログラマの不注意が原因です。格納後間接参照のエラーは、プログラムが明示的にポインタを null に設定し、後でそのポインタを間接参照した場合に発生します。このエラーは通常、変数の宣言時にプログラマがその変数を null に初期化したことが原因で発生します。

NULL ポインタの問題点の大半は一般にソフトウェアの信頼性の問題につながります。しかし、攻撃者が意図的に NULL ポインタ間接参照を引き起こせる場合、その結果生じた例外を利用してセキュリティ ロジックをバイパスすることで Denial of Service 攻撃を実装したり、アプリケーションにデバッグ情報を開示させてそれ以降の攻撃に役立てたりすることができます。

例 1: 次のコードでは、プログラマは変数 ptrNULL ではないと仮定してします。この仮定は、プログラマがポインタを間接参照したときに明らかになります。その後プログラマが ptrNULL を比較したときに、この仮定は成り立たなくなります。ptrif ステートメントでチェックされたときに NULL であるとすれば、間接参照されたときにも NULL である可能性があり、これがセグメンテーション違反の原因となる場合があります。


ptr->field = val;
...
if (ptr != NULL) {
...
}
例 2: 次のコードでは、プログラマは変数 ptrNULL であることを確認してから、続いてそれを誤って間接参照しています。ptrif ステートメントでチェックされたときに NULL になっていると、null Dereference が発生し、これがセグメンテーション違反の原因となります。


if (ptr == null) {
ptr->field = val;
...
}
例 3: 次のコードでは、プログラマが文字列 '\0' (実際は 0) または NULL を忘れたため、NULL ポインタを間接参照し、セグメンテーション違反を引き起こしています。


if (ptr == '\0') {
*ptr = val;
...
}
例 4: 次のコードでは、プログラマは明示的に変数 ptrNULL に設定しています。続いて、オブジェクトの null 値をチェックする前に ptr を間接参照しています。


*ptr = NULL;
...
ptr->field = val;
...
}
desc.controlflow.cpp.redundant_null_check
Abstract
プログラムでは NULL ポインタを間接参照する可能性があり、これが NULL ポインタ例外の原因になります。
Explanation
NULL ポインタの例外は通常、プログラマの想定が破られた結果として発生します。具体的には、チェック後間接参照のエラーが発生するのは、プログラムが null に対して明示的チェックを実行したにも関わらず、null であることが判明しているオブジェクトを間接参照した場合です。この種のエラーの多くは、タイプミスかプログラマの不注意が原因です。

NULL ポインタの問題点の大半は一般にソフトウェアの信頼性が損なわれる原因となります。しかし、攻撃者が意図的に NULL ポインタの間接参照を引き起こせる場合、その結果生じた例外を利用して Denial of Service 攻撃を仕掛けたり、アプリケーションにデバッグ情報を開示させてそれ以降の攻撃に役立てたりすることができます。

例 1: 次のコードでは、プログラマは変数 foonull であることを確認してから、続いてそれを誤って間接参照しています。fooif ステートメントでチェックされたときに null になっていると、null Dereference が発生し、これが NULL ポインタ例外の原因となります。


if (foo == null) {
foo.setBar(val);
...
}
desc.internal.java.null_dereference_dereference_after_check
Abstract
関数が明示的なアクセス修飾子を指定していません。
Explanation
Solidity スマート コントラクトを開発する際、開発者は関数のアクセス修飾子を設定して、関数を呼び出すことができるユーザーを制御できます。開発者がアクセス修飾子を指定しない場合、関数はデフォルトで public になり、悪意のある攻撃者が許可なく関数を呼び出すことができます。

例 1: 次のコードは両方の関数でアクセス修飾子の設定に失敗するため、誰でも関数を呼び出すことができ、ユーザーは require への呼び出しをバイパスできます。


function withdrawWinnings() {
require(uint32(msg.sender) == 0);
_sendWinnings();
}

function _sendWinnings() {
msg.sender.transfer(this.balance);
}
References
[1] Enterprise Ethereum Alliance Code Linting
[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 710
[7] Standards Mapping - Smart Contract Weakness Classification SWC-100
desc.structural.solidity.swc100
Abstract
関数がコントラクトの残高を特定のイーサ値と比較しています。
Explanation
コントラクトに特定のイーサ残高があると想定すると、コントラクトにイーサを送信するなどしてコントラクトの残高が強制的に変更される可能性があるため、誤った動作や予期しない動作が発生する可能性があります。

例 1: 次のコードは、assert を使用して、Lock コントラクト インスタンスが特定の値 (msg.value) であることを確認します。


contract Lock {
constructor (address owner, uint256 unlockTime) public payable {
...
}
}

contract Lockdrop {
...
function lock(...) {
uint256 eth = msg.value;
address owner = msg.sender;
uint256 unlockTime = unlockTimeForTerm(term);

Lock lockAddr = (new Lock).value(eth)(owner, unlockTime);

assert(address(lockAddr).balance == msg.value);
}
}
References
[1] Enterprise Ethereum Alliance No Exact Balance Check
[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-132
desc.structural.solidity.swc132
Abstract
コントラクトが、固定量のガスを定義しているか、固定量のガスを使用して関数を呼び出しています。
Explanation
ガスに関するトランザクション コストは、現在のネットワーク状況に応じて変化する可能性があります。たとえば、ハード フォーク中の EVM (イーサリアム仮想マシン) 命令のガス コストは、大きく影響を受ける可能性があります。これにより、固定量のガスに依存する既存の機能が壊れたり、価値転送に利用されるトランザクション (固定量の 2300 ガスを使用する transfer()send() など) に影響を与えたりする可能性があります。

例 1: 次のコードは呼び出しを実行し、固定量のガスを指定します。


interface ICallable {
function callMe() external;
}

contract HardcodedNotGood {
function callWithArgs() public {
callable.callMe{gas: 10000}();
}
}
References
[1] Enterprise Ethereum Alliance Gas and Gas Prices
[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 665
[7] Standards Mapping - Smart Contract Weakness Classification SWC-134
desc.structural.solidity.swc134