`dtls_get_cookie`: dereference before the length check (currently masked by `dtls_create_cookie`)
维护者通常 2 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 dtls_get_cookie 中的 dtls.c:439 开始,检查 SKIP_VAR_FIELD 的边界检查以及后续的长度检查。运行提供的 AddressSanitizer 复现程序,以确认发生了越界读取,然后验证新增的长度保护会返回相同的警报,且不再出现 sanitizer 错误。
由索引模型根据 Issue 内容生成。
描述
Not urgent — I believe this is unreachable in the current tree, and I say why
below. It is a hardening report: the check that makes the dereference safe lives
in a different function.
The site
dtls.c:439
msglen -= DTLS_HS_LENGTH + DTLS_CH_LENGTH;
msg += DTLS_HS_LENGTH + DTLS_CH_LENGTH;
/* skip session id */
SKIP_VAR_FIELD(msg, msglen, uint8, DTLS_ALERT_HANDSHAKE_FAILURE,
"get_cookie, session_id");
if (msglen < (*msg & 0xff) + sizeof(uint8))
return dtls_alert_fatal_create(DTLS_ALERT_HANDSHAKE_FAILURE);
SKIP_VAR_FIELD is correctly bounded but can leave msglen == 0, with msg
pointing one past the end of the message. The next line then reads *msg.
This site reads a length-prefixed field by hand, and it is doing the second
half of what SKIP_VAR_FIELD does while omitting the first:
#define SKIP_VAR_FIELD(P, L, T, A, M) { \
size_t skip_length = sizeof(T); \
if (L < skip_length) { ... return ...; } \ /* <-- this one */
skip_length += dtls_ ## T ## _to_int(P); \
if (L < skip_length) { ... return ...; } \ /* <-- present */
Reproduction
dtls_get_cookie is static, so the driver includes the translation unit.
Clean clone of master (3482499), the project's own CMake build with
-fsanitize=address added and nothing else changed:
git clone --depth 1 https://github.com/eclipse-tinydtls/tinydtls.git td && cd td
cmake -S . -B asan -DCMAKE_C_FLAGS="-fsanitize=address -g -O0" \
-DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address"
cmake --build asan -j4
direct.c:
#include <stdlib.h>
#include <stdio.h>
#include "dtls.c"
int main(void) {
size_t msglen = 60;
uint8 *msg = malloc(msglen); /* exactly msglen bytes */
memset(msg, 0, msglen);
msg[12] = 0xfe; msg[13] = 0xfd; /* version, at msg + DTLS_HS_LENGTH */
msg[46] = 13; /* session_id length; body ends at 60 */
uint8 *cookie = NULL;
printf("dtls_get_cookie -> %d\n", dtls_get_cookie(msg, msglen, &cookie));
free(msg); return 0;
}
gcc -fsanitize=address -g -O0 -I. -Iasan -o direct direct.c asan/libtinydtls.a && ./direct
==251536==ERROR: AddressSanitizer: heap-buffer-overflow
READ of size 1 at 0x50600000005c
#0 dtls_get_cookie dtls.c:455
0x50600000005c is located 0 bytes to the right of 60-byte region
The message is a 60-byte handshake message whose session id ends exactly at the
end of the buffer: 12 (handshake header) + 34 (dtls_client_hello_t) + 1
(session id length = 13) + 13 = 60.
Why I think it cannot happen today
The only caller is dtls_0_verify_peer (dtls.c:2249), which calls
dtls_create_cookie first with the same (data, data_length) and returns on
error. dtls_create_cookie performs the longer parse — after the session id it
also skips the cookie, the cipher suites and the compression list — and its
cookie skip requires at least one byte to remain. That is the byte
dtls_get_cookie dereferences unchecked, so no message accepted by
dtls_create_cookie can drive dtls_get_cookie out of bounds.
I checked this with a bounded model checker rather than only by reading:
composing the two calls the way dtls_0_verify_peer does, and running every
message up to 60 bytes with all bytes unconstrained, reports no violation — and a
reachability probe confirms messages do reach dtls_get_cookie, so the result is
not vacuous. Caveats: bounded at 60 bytes, and the HMAC digest was replaced by
its documented length (the cookie's contents cannot affect whether
dtls_get_cookie is reached).
Why it still seems worth a line
- The invariant is non-local and undocumented: nothing in
dtls_get_cookie
says a caller must have rundtls_create_cookiefirst. A change to the cookie
path, or a second caller, makes it live. - A unit-level fuzz target on
dtls_get_cookie— a natural thing to add, since
the repository currently has none — hits it immediately.
Fix
if (msglen < sizeof(uint8) || msglen < (*msg & 0xff) + sizeof(uint8))
Behaviour-preserving for every input. When msglen == 0 the original
condition 0 < (*msg & 0xff) + sizeof(uint8) is true whatever the out-of-bounds
byte happens to be, since the right-hand side is at least 1; so both versions
take the same branch and return the same alert. When msglen >= 1 the added
disjunct is false and the two conditions are identical.
Confirmed: with the change, the reproduction above prints
dtls_get_cookie -> -552 — the same value as before — and ASan is silent.
- 主要语言
- C
- 星标
- 122
- 派生
- 67
- 平均合并
- 6 天 8 小时
- 30 天内合并 PR
- 5
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
eclipse-tinydtls/tinydtls 的其他 Issue
-
ECDHE_ECDSA ClientKeyExchange rejected as too short when DTLS_PSK_MAX_CLIENT_IDENTITY_LEN > 64可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭bug good first issue please retest
难度 2/5 1-3 小时 新手友好度 76/100
eclipse-tinydtls/tinydtls#298 · 2 条评论 ·
维护者通常 2 天内回复
-
MSVC build is broken (undeclared `DTLS_DEBUG_BUF_SIZE`)可能已有人在做 @mrdeep1 于 13 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 25/100
eclipse-tinydtls/tinydtls#283 · 5 条评论 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 57/100
eclipse-tinydtls/tinydtls#278 · 3 条评论 · 1 个 reaction ·
维护者通常 2 天内回复
-
难度 4/5 3-5 天 新手友好度 45/100
eclipse-tinydtls/tinydtls#274 · 2 条评论 ·
维护者通常 2 天内回复
-
bug
难度 4/5 3-5 天 新手友好度 48/100
eclipse-tinydtls/tinydtls#273 · 2 条评论 ·
维护者通常 2 天内回复
查看 eclipse-tinydtls/tinydtls 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 66/100
FujiNetWIFI/fujinet-lib#186 ·
维护者通常 1 天内回复
-
VIEW_TYPE set for computer players可能已有人在做 @Loobinex 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
-
bug
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 2 天内回复
-
bug
难度 2/5 1 小时以内 新手友好度 78/100
brunodev85/winlator-app#90 ·