繁体   English   中英

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

[英]dyld_insert_libraries ignored when calling application through bash

对于我的应用程序,我使用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脚本中,我的应用程序将再次运行。

看起来像DYLD_INSERT_LIBRARIES bash脚本时未设置DYLD_INSERT_LIBRARIES ,正如此测试脚本所证明的那样。

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

有没有办法让bash脚本继承并传递DYLD_INSERT_LIBRARIES

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

系统bash可执行文件已标记为“受限制”,禁用DYLD_ *功能。 要解决此问题,您可以复制bash并使用它。

通过在dyld的实现中查找以下细节,我发现这个限制至少可以追溯到10.6。

在macOS 10.13 dyld 实现中,此逻辑位于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权利。 您可能可以直接测试受限状态,或者您可以根据此信息构建一个自行测试这些条件的函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM