gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:21:11 25 4
gpt4 key购买 nike

对于我的应用程序,我使用 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 将被忽略。

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

当调用 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 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 权利。您可以直接测试受限状态,或者您可以构建一个函数来根据此信息自行测试这些条件。

关于bash - 通过 bash 调用应用程序时忽略 dyld_insert_libraries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43941322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com