核心修复:getrandom compat shim 的 errno 透传 - shim 原先把 syscall 失败一律写成 errno=EAGAIN,覆盖了内核真实 errno - Rust std sys/random/linux.rs 的 EAGAIN 分支带 guard flags==GRND_NONBLOCK, 而 ZYNQ 内核 4.14 不认 GRND_INSECURE(真实 errno=EINVAL)⇒ 落到 linux.rs:126 的 panic 分支 ⇒ tdc-connector 启动即 failed to generate random data - 改为原样透传内核 errno(ZYNQ 实测:INSECURE→EINVAL、NONBLOCK→32、EFAULT 正确) 配套: - deploy-card-zynq.sh / deploy-tdc-zynq.sh / tds-login.sh(真机部署链路) - register-device-connector.sh 修复 ipWhitelist/envInfo 必须是 JSON 字符串、 证书按连接器签发(certType=02),四闸门 6/7 → 8/8 - bind-tdc-to-tds.sh 去掉硬编码 token,改动态取 ConfigMap - verify-3party-topology.sh 逐节点挑 token(各设备 token 可能不同) - deploy-tdc-zynq.sh 修 macOS tar 打入 AppleDouble 残留 ZYNQ 真机实测:L1 四闸门 5/5、L2 绑定、L3 出向 TDS、L4 产品上架、L5 PGlite tables=30
105 lines
4.1 KiB
C
105 lines
4.1 KiB
C
/*
|
||
* getrandom-compat.c —— 为 glibc < 2.25 提供 getrandom(3)
|
||
*
|
||
* 背景:Rust std 的 `std::collections::hash_map::RandomState` 会调用 libc 的
|
||
* getrandom() 来播种哈希随机数。getrandom(3) 是 **glibc 2.25 才引入**的。
|
||
* 链接器把弱引用(weak undefined)符号也计入 .gnu.version_r 版本需求,
|
||
* ⇒ 即使运行时不走这条路径,动态链接器也会在启动时报
|
||
* `version 'GLIBC_2.25' not found`。
|
||
*
|
||
* 解决:链接期提供本地强符号,优先于 libc.so.6 的版本化符号。
|
||
* 实现走 syscall(SYS_getrandom, ...),Linux 3.17+ 内核原生支持。
|
||
*
|
||
* ============================================================================
|
||
* 🔴🔴🔴 2026-11-13 修复:**errno 必须原样透传,绝不能改写**
|
||
* ============================================================================
|
||
*
|
||
* 现象:tdc-connector 在 ZYNQ(内核 4.14 / glibc 2.26)启动即 panic:
|
||
* panicked at library/std/src/sys/random/linux.rs:126:26:
|
||
* failed to generate random data
|
||
*
|
||
* 根因链(Rust std `sys/random/linux.rs` 的 getrandom() 错误分派):
|
||
* EINTR => continue
|
||
* EINVAL if flags==GRND_INSECURE => 标记 INSECURE 不可用,改用 GRND_NONBLOCK
|
||
* EAGAIN if flags==GRND_NONBLOCK => 池未就绪,退到 /dev/urandom
|
||
* ENOSYS | EPERM => 系统调用不可用,退到 /dev/urandom
|
||
* _ => panic!("failed to generate random data") ← 命中的就是这行
|
||
*
|
||
* `RandomState::new()` → `hashmap_random_keys()` → insecure=true
|
||
* ⇒ 首次 flags = GRND_INSECURE(0x4)。
|
||
* ZYNQ 内核 4.14 **不认识 GRND_INSECURE**(5.6 才引入)⇒ 内核返回 EINVAL。
|
||
*
|
||
* · 不注入 shim 时:glibc 原生 getrandom@GLIBC_2.25 原样传 EINVAL
|
||
* ⇒ 命中 `EINVAL if flags == GRND_INSECURE` ⇒ 降级 GRND_NONBLOCK ⇒ 成功。
|
||
* · 注入本 shim 时:shim 把 errno **硬编码成 EAGAIN**
|
||
* ⇒ EAGAIN 分支的 guard `flags == GRND_NONBLOCK` 不成立(此时 flags=0x4)
|
||
* ⇒ 落到 `_ =>` ⇒ **panic**。
|
||
*
|
||
* ZYNQ 实测对照(同一台设备、同一内核、同一份探针):
|
||
* [shim INSECURE] flags=0x4 ret=-1 errno=11(Resource temporarily unavailable) ← 谎报
|
||
* [raw INSECURE] flags=0x4 ret=-1 errno=22(Invalid argument) ← 真实
|
||
*
|
||
* ⇒ 结论:**glibc 的 syscall() 本来就正确设置了 errno,是 shim 主动覆盖了它。**
|
||
* 修复 = 删掉那句 `errno = EAGAIN`,让内核的 errno 原样传给 Rust std。
|
||
*
|
||
* ⚠️ 为什么不用内联 ARM EABI `svc #0`:
|
||
* ARM32 内核在 EABI 下从 **r0-r2** 取参数、r7 取系统调用号;
|
||
* 把参数放到 r4-r6 会让内核读到垃圾值(实测段错误)。
|
||
* 而 glibc 的 syscall() 封装已经把 EABI 约定处理对了,且 errno 语义正确
|
||
* ⇒ 直接用它最稳,不引入手写汇编的寄存器约定风险。
|
||
*/
|
||
|
||
#define _GNU_SOURCE
|
||
#include <errno.h>
|
||
#include <stddef.h>
|
||
#include <sys/syscall.h>
|
||
#include <unistd.h>
|
||
|
||
#ifndef SYS_getrandom
|
||
#define SYS_getrandom 384
|
||
#endif
|
||
|
||
/* Linux 内核约定的最大 errno 值(用于区分「负返回=错误」与「正常大数」) */
|
||
#define PGL_MAX_ERRNO 4095
|
||
|
||
ssize_t
|
||
getrandom(void *buf, size_t buflen, unsigned int flags)
|
||
{
|
||
long ret;
|
||
|
||
if (buf == NULL && buflen > 0)
|
||
{
|
||
errno = EFAULT;
|
||
return -1;
|
||
}
|
||
|
||
if (buflen == 0)
|
||
return 0;
|
||
|
||
ret = syscall(SYS_getrandom, buf, buflen, flags);
|
||
if (ret < 0)
|
||
{
|
||
/*
|
||
* 🔴🔴🔴 **绝不覆盖内核给的 errno**。
|
||
*
|
||
* glibc 的 syscall() 规范行为:出错时返回 -1 并设置 errno。
|
||
* 少数老 glibc/特殊 ABI 直接返回 -errno 而不置 errno,
|
||
* 这里两种都兼容。
|
||
*/
|
||
if (ret < -1 && ret >= -PGL_MAX_ERRNO)
|
||
{
|
||
/* syscall() 返回的是裸 -errno ⇒ 自己置上 */
|
||
errno = (int) -ret;
|
||
}
|
||
else if (errno == 0)
|
||
{
|
||
/* 兜底:errno 未被设置(极老 glibc),至少给个合理的默认值 */
|
||
errno = EAGAIN;
|
||
}
|
||
/* errno 已被 glibc 正确设置时,这里什么都不做 —— 原样透传 */
|
||
return -1;
|
||
}
|
||
|
||
return (ssize_t) ret;
|
||
}
|