Associated Vulnerability
Title:Infinite loop in BN_mod_sqrt() reachable when parsing certificates (CVE-2022-0778)Description:The BN_mod_sqrt() function, which computes a modular square root, contains a bug that can cause it to loop forever for non-prime moduli. Internally this function is used when parsing certificates that contain elliptic curve public keys in compressed form or explicit elliptic curve parameters with a base point encoded in compressed form. It is possible to trigger the infinite loop by crafting a certificate that has invalid explicit curve parameters. Since certificate parsing happens prior to verification of the certificate signature, any process that parses an externally supplied certificate may thus be subject to a denial of service attack. The infinite loop can also be reached when parsing crafted private keys as they can contain explicit elliptic curve parameters. Thus vulnerable situations include: - TLS clients consuming server certificates - TLS servers consuming client certificates - Hosting providers taking certificates or private keys from customers - Certificate authorities parsing certification requests from subscribers - Anything else which parses ASN.1 elliptic curve parameters Also any other applications that use the BN_mod_sqrt() where the attacker can control the parameter values are vulnerable to this DoS issue. In the OpenSSL 1.0.2 version the public key is not parsed during initial parsing of the certificate which makes it slightly harder to trigger the infinite loop. However any operation which requires the public key from the certificate will trigger the infinite loop. In particular the attacker can use a self-signed certificate to trigger the loop during verification of the certificate signature. This issue affects OpenSSL versions 1.0.2, 1.1.1 and 3.0. It was addressed in the releases of 1.1.1n and 3.0.2 on the 15th March 2022. Fixed in OpenSSL 3.0.2 (Affected 3.0.0,3.0.1). Fixed in OpenSSL 1.1.1n (Affected 1.1.1-1.1.1m). Fixed in OpenSSL 1.0.2zd (Affected 1.0.2-1.0.2zc).
Readme
# CVE-2022-0778
**Contributors**
- [정준수(@jeongjunsoo)](https://github.com/jeongjunsoo)
<br/>
### 요약
- CVE-2022-0778 취약점은 OpenSSL에서 소수의 제곱근을 계산하는 `BN_mod_sqrt()`함수에 영향을 미칩니다.
- 이 함수가 소수 기반이 아닌 악의적으로 조작된 인증서를 처리할 때, 패치되지 않은 서버는 DoS 공격에 노출될 수 있습니다.
<br/>
### Target
- OpenSSL 1.0.2
- OpenSSL 1.1.1
- OpenSSL 3.0
<br/>
### 패치 전
```
i = 1;
if (!BN_mod_sqr(t, b, p, ctx))
goto end;
while (!BN_is_one(t)) {
i++;
if (i == e) {
ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE);
goto end;
}
if (!BN_mod_mul(t, t, t, p, ctx))
goto end;
}
```
- 이 알고리즘에 따르면, i 값이 e 값에 도달하면 loop가 종료되어야 합니다.
- 그러나 특정 입력을 통해 i와 e 모두 1의 값을 갖도록 만들면 해당 loop는 끝나지 않고 무한 loop가 발생합니다.
- 이로 인해 서비스 거부가 발생하게 됩니다.
<br/>
### 패치 후
```
for (i = 1; i < e; i++) {
if (i == 1) {
if (!BN_mod_sqr(t, b, p, ctx))
goto end;
} else {
if (!BN_mod_mul(t, t, t, p, ctx))
goto end;
}
if (BN_is_one(t))
break;
}
/* If not found, a is not a square or p is not prime. */
if (i >= e) {
ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE);
goto end;
}
```
- 기존의 `while` 문을 `for` 문으로 변경되었습니다.
- 이에 따라 i의 값이 e에 도달하면 자동으로 loop가 종료되게 패치되었습니다.
- 위 패치를 통해 무한 loop 위험을 제거했습니다.
<br/>
### 환경 구성 및 실행
- `Ubuntu 환경 구성`
- `docker compose up -d` 테스트 환경 실행
- `docker run -it --rm -p 12345:12345 yywing/cve-2022-0778 --addr 0.0.0.0:12345`
- `docker compose exec curl top` 프로세스 상태 확인
- `docker compose exec curl bash` bash 실행
- `curl -k https://172.17.0.1:12345` 12345 포트로 HTTPS 요청 전송
<br/>
### 실행
- 실습을 위해 세 개의 터미널 창(A, B, C)를 준비합니다.
<br/>

- A 터미널에서 `docker run -it --rm -p 12345:12345 yywing/cve-2022-0778 --addr 0.0.0.0:12345` 명령을 실행하여 Docker 이미지를 시작합니다.
<br/>

- 다음으로 B 터미널에서 `docker compose exec curl top` 명령어를 실행해 현재 실행 중인 프로세스들의 상황을 살펴봅니다.
<br/>

- 마지막으로 C 터미널에서는 `docker compose exec curl bash`를 입력한 후, `curl -k https://172.17.0.1:12345` 12345 포트로 HTTPS 요청 전송을 합니다.
<br/>
### 결과

- B 터미널에서 실행된 top 명령의 결과로 curl 프로세스가 CPU 자원을 거의 100% 사용하는 것을 확인할 수 있습니다. 이는 무한 loop에 빠져서 발생한 것입니다.
<br/>
### 정리
- 해당 취약점은 소수 여부의 검증 부재로 인해 발생한 취약점입니다.
- 무한 loop 문제를 방지하기 위해 while 문을 for 문으로 변경하여 패치 되었습니다.
<br/>
### 레퍼런스
- https://github.com/drago-96/CVE-2022-0778
- https://hackyboiz.github.io/2022/03/19/syru/cve-2022-0778/
- https://github.com/yywing/cve-2022-0778/
<br/>
File Snapshot
[4.0K] /data/pocs/f3524ce20b5e068efda8d28447c8d31072c80da9
├── [ 126] docker-compose.yml
├── [3.3K] README.md
├── [ 30K] result1.png
├── [138K] result2.png
├── [ 21K] result3.png
└── [150K] result4.png
0 directories, 6 files
Remarks
1. It is advised to access via the original source first.
2. Local POC snapshots are reserved for subscribers — if the original source is unavailable, the local mirror is part of the paid plan.
3. Mirroring, verifying, and maintaining this POC archive takes ongoing effort, so local snapshots are a paid feature. Your subscription keeps the archive online — thank you for the support. View subscription plans →