界: API Abuse

API は、呼び出し元と呼び出し先の間のコントラクトです。最も一般的な API の不正使用の形態は、呼び出し元がこのコントラクトの終わりを守らないことによって発生します。たとえば、プログラムが chroot() を呼び出した後に chdir() を呼び出すのに失敗すると、アクティブなルート ディレクトリを安全に変更する方法を指定したコントラクトに違反することになります。ライブラリの悪用のもう 1 つの良い例は、呼び出し先が信頼できる DNS 情報を呼び出し元に返すことを期待することです。この場合、呼び出し元は、呼び出し先の API の動作 (戻り値が認証目的に使用できること) についてある種の仮定をすることで、呼び出し先の API を悪用します。また、相手側から、呼び出し元と呼び出し先のコントラクトを違反することもできます。例えば、コーダーが SecureRandom をサブクラス化し、ランダムではない値を返した場合、コントラクトに違反することになります。

81 見つかった項目
脆弱性
Abstract
Equals()Equals() を実装しないオブジェクトでコールされています。
Explanation
開発者がオブジェクトを比較する場合、通常はオブジェクトのプロパティを比較します。しかし、明示的に Equals() を実装していないクラス (あるいは任意のスーパークラス/インターフェイス) で Equals() をコールすると、System.Object から継承した Equals() メソッドがコールされる結果となります。Object.Equals() はオブジェクトのメンバー フィールドやその他のプロパティを比較する代わりに、2 つのオブジェクト インスタンスを比較してその 2 つが同一のものであるかを確認します。Object.Equals() の正規の利用法も存在しますが、通常はコードにバグがあることを示しています。

例 1:

public class AccountGroup
{
private int gid;

public int Gid
{
get { return gid; }
set { gid = value; }
}
}
...
public class CompareGroup
{
public bool compareGroups(AccountGroup group1, AccountGroup group2)
{
return group1.Equals(group2); //Equals() is not implemented in AccountGroup
}
}
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 2
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398
desc.structural.dotnet.code_correctness_class_does_not_implement_equals
Abstract
equals() メソッドが、equals() を実装しないオブジェクトでコールされています。
Explanation
開発者がオブジェクトを比較する場合、通常はオブジェクトのプロパティを比較します。しかし、明示的に equals() を実装していないクラス (あるいは任意のスーパークラス/インターフェイス) で equals() をコールすると、java.lang.Object から継承した equals() メソッドがコールされる結果となります。Object.equals() はオブジェクトのメンバーフィールドやその他のプロパティを比較する代わりに、2 つのオブジェクトインスタンスを比較してその 2 つが同一のものであるかを確認します。Object.equals() の正規の利用法も存在しますが、通常はコードにバグがあることを示しています。

例 1:

public class AccountGroup
{
private int gid;

public int getGid()
{
return gid;
}

public void setGid(int newGid)
{
gid = newGid;
}
}
...
public class CompareGroup
{
public boolean compareGroups(AccountGroup group1, AccountGroup group2)
{
return group1.equals(group2); //equals() is not implemented in AccountGroup
}
}
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 2
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398
desc.structural.java.code_correctness_class_does_not_implement_equals
Abstract
この finalize() メソッドは super.finalize() をコールします。
Explanation
Java 言語仕様は、finalize() メソッドで super.finalize() のコールを行うことは適切な方法であるとしています [1]。

例 1: 次のメソッドは、super.finalize() のコールを省略しています。


protected void finalize() {
discardNative();
}
References
[1] J. Gosling, B. Joy, G. Steele, G. Bracha The Java Language Specification, Second Edition Addison-Wesley
[2] MET12-J. Do not use finalizers CERT
[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 568
desc.structural.java.code_correctness_erroneous_finalize_method
Abstract
シリアライゼーションに誤ったメソッドの署名を使用すると、一度もコールされることがないという結果につながる可能性があります。
Explanation
シリアライズ可能なメソッドの署名が誤っているというコードの正確性に関する問題は、シリアライズ可能なクラスがシリアライズ関数またはデシリアライズ関数を作成するものの、正しい署名に従っていない場合に発生します。


private void writeObject(java.io.ObjectOutputStream out) throws IOException;
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;
private void readObjectNoData() throws ObjectStreamException;


シリアライゼーションが必要とするメソッドの署名からの逸脱は、メソッドがシリアライゼーション/デシリアライゼーションの間コールされることがなくなり、不完全なシリアライゼーション/デシリアライゼーションや、信頼できないコードがオブジェクトにアクセスすることになりかねません。
スローされない例外がある場合は、シリアライゼーション/デシリアライゼーションが失敗してアプリケーションがクラッシュするか、目に見えるエラーなく失敗してオブジェクトが部分的にだけ正しく構成され、デバッグがきわめて困難な欠陥につながる可能性があります。コール元は、クラッシュや部分的なオブジェクト構成などを発生させずに、これらの例外をキャッチしてシリアライゼーション/デシリアライゼーションの誤りを適切に処理する必要があります。
References
[1] SER01-J. Do not deviate from the proper signatures of serialization methods 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
desc.structural.java.code_correctness_incorrect_serializable_method_signature
Abstract
サーブレットの出力ストリームがすでにコミットされた後には、ストリームバッファをリセットしたり、ストリームを再コミットする他のアクションを実行するとエラーが発生しやすくなります。同様に、getOutputStream をコールした後に、getWriter() をコールする場合もエラーが発生しやすくなります。
Explanation
HttpServletRequest の転送、HttpServletResponse のリダイレクト、またはサーブレットの出力ストリームバッファーのフラッシュによって、関連ストリームがコミットされます。後続のバッファーがストリームをリセットまたはコミットすると (追加のフラッシュまたはリダイレクトなど)、IllegalStateException となります。

さらに、Java サーブレットによって、ServletOutputStream または PrintWriter のいずれか (両方ではありません) を使用して、レスポンスストリームへのデータの書き込みが許可されます。getOutputStream() をコールした後に、getWriter() をコールする、また逆の処理を行った場合、IllegalStateException となります。



実行時には、IllegalStateException によって、レスポンスハンドラの実行が防止され、レスポンスがドロップすることになります。これによりサーバーの動作が不安定になります。また、サーブレットが適切に実装されていないことを示します。

例 1: 次のコードは、出力ストリームバッファーがフラッシュされた後に、サーブレットのレスポンスをリダイレクトしています。

public class RedirectServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
...
OutputStream out = res.getOutputStream();
...
// flushes, and thereby commits, the output stream
out.flush();
out.close(); // redirecting the response causes an IllegalStateException
res.sendRedirect("http://www.acme.com");
}
}
例 2: 逆に、次のコードは、リクエストが転送された後に、書き込みを行い、PrintWriter のバッファーをフラッシュしようとしています。

public class FlushServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
...
// forwards the request, implicitly committing the stream
getServletConfig().getServletContext().getRequestDispatcher("/jsp/boom.jsp").forward(req, res);
...

// IllegalStateException; cannot redirect after forwarding
res.sendRedirect("http://www.acme.com/jsp/boomboom.jsp");

PrintWriter out = res.getWriter();

// writing to an already-committed stream will not cause an exception,
// but will not apply these changes to the final output, either
out.print("Writing here does nothing");

// IllegalStateException; cannot flush a response's buffer after forwarding the request
out.flush();
out.close();
}
}
References
[1] IllegalStateException in a Servlet - when & why do we get?
[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 1
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 398
[7] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-001094
[8] Standards Mapping - NIST Special Publication 800-53 Revision 4 SC-5 Denial of Service Protection (P1)
[9] Standards Mapping - NIST Special Publication 800-53 Revision 5 SC-5 Denial of Service Protection
[10] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002400 CAT II
[11] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002400 CAT II
[12] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002400 CAT II
[13] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002400 CAT II
[14] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002400 CAT II
[15] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002400 CAT II
[16] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002400 CAT II
[17] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002400 CAT II
[18] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002400 CAT II
[19] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002400 CAT II
[20] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002400 CAT II
[21] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002400 CAT II
[22] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002400 CAT II
[23] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002400 CAT II
desc.controlflow.java.code_correctness_multiple_stream_commits
Abstract
Content-Length ヘッダーには負の数が設定されています。
Explanation
ほとんどの場合、リクエストの Content-Length ヘッダーを設定した開発者は、
サーバーに送信されたPOSTデータの長さのやり取りに興味を持っています。ただし、このヘッダーには 0 または正の数を
設定する必要があります。

例 1: 次のコードでは、不正な Content-Length を設定することになります。

URL url = new URL("http://www.example.com");
HttpURLConnection huc = (HttpURLConnection)url.openConnection();
huc.setRequestProperty("Content-Length", "-1000");
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 3
[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
desc.structural.java.api_abuse_code_correctness_negative_content_length
Abstract
Content-Length ヘッダーには負の数が設定されています。
Explanation
ほとんどの場合、リクエストの Content-Length ヘッダーを設定した開発者は、
サーバーに送信されたPOSTデータの長さのやり取りに興味を持っています。ただし、このヘッダーには 0 または正の数を
設定する必要があります。

例 1: 次のコードでは Content-Length ヘッダーに誤って負の数を設定しています。

xhr.setRequestHeader("Content-Length", "-1000");
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 3
[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
desc.structural.javascript.api_abuse_code_correctness_negative_content_length
Abstract
ToString() は配列でコールされます。
Explanation
ほとんどの場合、配列で ToString() がコールされているということは、開発者が配列の内容を文字列として返そうとしていることを意味します。しかし、配列で ToString() を直接コールすると、その配列のタイプを含んだ文字列値が返されます。

例 1: 次のコードでは、System.String[] を出力します。

String[] stringArray = { "element 1", "element 2", "element 3", "element 4" };
System.Diagnostics.Debug.WriteLine(stringArray.ToString());
References
[1] Class Arrays Microsoft
[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.structural.dotnet.code_correctness_tostring_on_array
Abstract
toString() は配列でコールされます。
Explanation
ほとんどの場合、配列で toString() がコールされているということは、開発者が配列の内容を文字列として返そうとしていることを意味します。しかし、配列で toString() を直接コールすると、その配列のタイプおよびメモリのハッシュコードを含んだ文字列値が返されます。
例 1: 次のコードは、[Ljava.lang.String;@1232121 を出力します。

String[] strList = new String[5];
...
System.out.println(strList);
References
[1] Class Arrays Sun Microsystems
[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.structural.java.code_correctness_tostring_on_array
Abstract
フィールドには、危険であるというアノテーションが付けられています。使用されるすべての場合でフラグが付けられます。
Explanation
このフィールドには FortifyDangerous というアノテーションが付けられています。このアノテーションは、危険であること、および安全を確保するにはすべての使用をチェックすべきであることを示すために使用されます。
desc.structural.java.dangerous_field