cargo audit )的时候,审计反馈 chrono 这个的日期时间处理库有脆弱性,于是追究起其发生的原因。首先,最初的问题是来自于 chrono 库发现 localtime_r 在直接使用 libc 提供的 getenv、setenv时可能会引起不安全性(unsound) ,而该问题在使用 std 的时候可以通过上锁来保证安全性(sound)。在这个问题底下有网友指向了 rust-lang 关于 getaddrinfo 是否需要获取 ENV 锁 的讨论,里面 alexcrichton 贴出了 Linux man page 里的描述:env Functions marked with env as an MT-Safety issue access the
environment with getenv(3) or similar, without any guards to
ensure safety in the presence of concurrent modifications.
We do not mark these functions as MT-Unsafe, however, because
functions that modify the environment are all marked with
const:env and regarded as unsafe. Being unsafe, the latter
are not to be called when multiple threads are running or
asynchronous signals are enabled, and so the environment can
be considered effectively constant in these contexts, which
makes the former safe.
被多线程安全性(MT-Safety)标记 env 标记的函数在用 getenv或者类似函数访问环境变量会出问题,如果不使用 guard 来保证在并发修改环境时的安全性。然而我们不把这些函数标记为多线程不安全(MT-Unsafe),因为修改环境的的函数均被用 const:env 标记并且认为是不安全的。因为不安全,所有后者不会在多线程运行或异步信号启用时被调用,所以这些环境变量可以被有效认为在上下文中是常量,因此前者(getenv,小编注)是安全的。
std::env::set_var 标记为 unsafesetenvsetenvstd::env::set_var--ignore参数来忽略:cargo audit --ignore RUSTSEC-2020-0159
原文作者:Rust语言中文社区;如有侵权,请联系管理员删除。