説明
要求ごとの処理量に上限がない場合や使用済みのリソースを解放しない場合、CPU、メモリ、ファイル記述子、接続を使い切り、他の要求を処理できなくなるおそれがあります。
想定される影響
- 応答の遅延、停止、サービスの利用不能
- CPUやメモリなどの過剰な消費による正常な処理の妨害
対処方法
- 同時に実行する処理の数と、1 回の処理で使えるリソース量に上限を設けます。上限を超える要求は遅延させるか拒否します。
- 成功時も失敗時も、開いたファイルやデータベース・ネットワークの接続を閉じ、その処理の実行枠を返します。
例
Spring コントローラーで同時ダウンロード数を制限する例です。実際の転送は try 内で完了する必要があります。転送処理、ファイルパスの検証、転送サイズ・時間の上限、必要な import は省略しています。
変更前
java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FileDownloadController {
@GetMapping("/download")
public void downloadFile(@RequestParam String fileName) {
// 同時実行数を制限せずにダウンロード処理を行う
File file = new File(fileName);
// 実際のダウンロード処理は省略
}
}
変更後
java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FileDownloadController {
private static final int MAX_CONNECTIONS = 10; // 同時処理数の上限
private AtomicInteger activeConnections = new AtomicInteger(0);
@GetMapping("/download")
public ResponseEntity<String> downloadFile(@RequestParam String fileName) {
if (activeConnections.incrementAndGet() > MAX_CONNECTIONS) {
activeConnections.decrementAndGet();
return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).body("Too Many Requests");
}
try {
File file = new File(fileName);
// 実際のダウンロード処理は省略
} finally {
activeConnections.decrementAndGet();
}
return ResponseEntity.ok("Download started");
}
}
カウンターの上限は、コントローラーの 1 インスタンスで実行中の同期処理 10 件です。finally は実行枠を返します。開いたファイルや接続は別途閉じてください。非同期転送と、複数インスタンスにまたがる上限は別に管理します。
関連CVE
- CVE-2022-21668: Pythonライブラリーの画像バンド数制限不備によるメモリ枯渇・整数オーバーフロー(CWE-1284/CWE-789/CWE-190)
- CVE-2020-7218: Go製ワークロード管理の未認証接続による資源枯渇
- CVE-2020-3566: 分散OSのIGMPキューによる資源枯渇(CISA KEV掲載)