【问题标题】:dyld_insert_libraries ignored when calling application through bash通过 bash 调用应用程序时忽略 dyld_insert_libraries
【发布时间】:2017-10-11 23:39:10
【问题描述】:

对于我的应用程序,我使用 DYLD_INSERT_LIBRARIES 来切换库。我正在运行 Mac OS X,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 将被忽略。

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

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

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

有什么方法可以让bash脚本继承并传递DYLD_INSERT_LIBRARIES

【问题讨论】:

  • 为什么不将导出添加到您的个人资料或脚本中?
  • 这个脚本应该是跨平台的。 DYLD_INSERT_LIBRARIES 可能应该只在启动脚本中使用我的应用程序的命令调用。现在我更好奇如何让 bash 尊重 DYLD_INSERT_LIBRARIES 和 DYLD_LIBRARY_PATH。
  • DYLD_INSERT_LIBRARIES不适用于Linux,它是LD_PRELOAD

标签: bash macos shared-libraries


【解决方案1】:

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

系统 bash 可执行文件已被标记为“受限”,禁用 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 权利。您也许可以直接测试受限状态,或者您可以根据这些信息构建一个函数来自己测试这些条件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多