Commit d71301d
fix(hotp): bound Non-Secure key size before copy into K[]
register_shared_key() copied params[0].memref.size bytes into the fixed
64-byte secure buffer K[] without bounding the length. That size is
supplied verbatim by the Non-Secure caller, so any NS caller could
overflow a secure .bss buffer with attacker-controlled data.
This is not covered by the pointer-attribution hardening in 27e314c:
nsec_check() proves the source range is Non-Secure resident, which is a
different property from "the source fits in the destination". A
legitimate 4 KB Non-Secure buffer passes that check exactly as designed
and is then copied into 64 bytes.
The correct bounds check already existed in this file (MIN_KEY_SIZE /
MAX_KEY_SIZE in hmac_sha1()) but ran on the GET_HOTP path, a later and
separate invocation, long after the corruption. Move the identical
rejection ahead of the copy. Reject rather than truncate, matching
apps/aes/ta/aes_ta.c: a silently shortened HMAC key would be a
cryptographic defect of its own. Not a compatibility break, since such a
key already failed later at hmac_sha1().
Two further defects in the same code:
- DMSG traced K with "%s": it printed the HMAC shared secret to the
console, and a key of exactly sizeof(K) leaves no NUL for %s to stop
at, so it also over-read past the array. Trace only the length.
- get_hotp() called truncate_() without checking res from hmac_sha1(),
truncating uninitialised stack when the HMAC failed.
Verified under QEMU (mps2-an505, cortex-m33): sizes 4096, 65 and 4 are
now rejected with TEE_ERROR_BAD_PARAMETERS, the 20-byte RFC4226 key still
registers and GET_HOTP still yields the specified value 755224. With this
patch reverted the 4096-byte request returns TEE_SUCCESS and the secure
world then stops responding.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>1 parent 102d3dc commit d71301d
1 file changed
Lines changed: 29 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
147 | 166 | | |
148 | 167 | | |
149 | 168 | | |
150 | 169 | | |
151 | 170 | | |
152 | | - | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
153 | 177 | | |
154 | 178 | | |
155 | 179 | | |
| |||
173 | 197 | | |
174 | 198 | | |
175 | 199 | | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
176 | 204 | | |
177 | 205 | | |
178 | 206 | | |
| |||
0 commit comments