dyld_insert_libraries 通过 bash 调用应用程序时被忽略

dyld_insert_libraries ignored when calling application through bash

对于我的应用程序,我使用 DYLD_INSERT_LIBRARIES 来切换库。我是运行MacOSX,El Capitan

如果我在 shell 中设置这些环境变量:

export PYTHONHOME=${HOME}/anaconda
export DYLD_INSERT_LIBRARIES=${HOME}/anaconda/lib/libpython2.7.dylib:${HOME}/anaconda/lib/libmkl_rt.dylib

如果我直接启动我的应用程序,它可以正常工作。但是,如果我通过我编写的 bash 脚本调用它,DYLD_INSERT_LIBRARIES 将被忽略。

如果我将相同的两行添加到我的 bash 脚本中,我的应用程序将再次运行。

调用 bash 脚本时,似乎 DYLD_INSERT_LIBRARIES 被取消设置,正如此测试脚本所证明的那样。

#!/bin/bash
set -e
echo ${DYLD_INSERT_LIBRARIES}

有没有什么办法可以让bash脚本继承和传承DYLD_INSERT_LIBRARIES

这是最新 macOS 版本的安全功能。

系统 bash 可执行文件已被标记为 "restricted",禁用 DYLD_* 功能。要解决此问题,您可以复制 bash 并改用它。

通过在 dyld 的实现中查找以下详细信息,我发现此限制至少可以追溯到 10.6。

在 macOS 10.13 dyld implementation 中,此逻辑在 pruneEnvironmentVariables 中,注释为:

// For security, setuid programs ignore DYLD_* environment variables.
// Additionally, the DYLD_* enviroment variables are removed
// from the environment, so that any child processes don't see them.

然而,设置限制的实际逻辑在 configureProcessRestrictions:

// any processes with setuid or setgid bit set or with __RESTRICT segment is restricted
if ( issetugid() || hasRestrictedSegment(mainExecutableMH) ) {
    gLinkContext.processIsRestricted = true;
}
...
if ( csops(0, CS_OPS_STATUS, &flags, sizeof(flags)) != -1 ) {
    // On OS X CS_RESTRICT means the program was signed with entitlements
    if ( ((flags & CS_RESTRICT) == CS_RESTRICT) && usingSIP ) {
        gLinkContext.processIsRestricted = true;
    }
    // Library Validation loosens searching but requires everything to be code signed
    if ( flags & CS_REQUIRE_LV ) {
        gLinkContext.processIsRestricted = false;
...

如您所见,它取决于 issetugidhasRestrictedSegmentCS_RESTRICT / SIP 权利。您可能能够直接测试受限状态,或者您可以构建一个函数来根据此信息自行测试这些条件。