Python 使用“/usr/lib/libcrypto.dylib”在 MacOS 10.15 Beta (19A582a) 上崩溃

Python crashing on MacOS 10.15 Beta (19A582a) with "/usr/lib/libcrypto.dylib"

我 运行 我的 Django 项目使用新的 macOS Catalina 并且 运行 运行良好。
我安装了 oh_my_zsh 然后我尝试 运行 同一个项目,它正在崩溃并出现以下错误。我卸载了 oh_my_zsh 并再次尝试,但没有成功。

Path:                  /usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python
Identifier:            Python
Version:               3.7.4 (3.7.4)
Code Type:             X86-64 (Native)
Parent Process:        Python [7526]
Responsible:           Terminal [7510]
User ID:               501

Date/Time:             2019-10-07 20:59:20.675 +0530
OS Version:            Mac OS X 10.15 (19A582a)
Report Version:        12
Anonymous UUID:        CB7F20F6-96C0-4F63-9EC5-AFF3E0989687


Time Awake Since Boot: 3000 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Application Specific Information:
/usr/lib/libcrypto.dylib
abort() called
Invalid dylib load. Clients should not load the unversioned libcrypto dylib as it does not have a stable ABI.

一定是使用了一些依赖项,例如 cryptography

解决方案:

cd your-site-packages-path/
vim ./asn1crypto/_int.py

找到这一行;删掉就OK了

# from ._perf._big_num_ctypes import libcrypto

这是我的问题

Process:               Python [85179]
Path:                  /usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python
Identifier:            Python
Version:               3.7.4 (3.7.4)
Code Type:             X86-64 (Native)
Parent Process:        ??? [85161]
Responsible:           iTerm2 [11711]
User ID:               501

Date/Time:             2019-10-07 23:00:25.143 +0800
OS Version:            Mac OS X 10.15 (19A582a)
Report Version:        12
Bridge OS Version:     3.0 (14Y906)
Anonymous UUID:        32C73ADD-1291-FA0E-DC02-48D539674325


Time Awake Since Boot: 42000 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Application Specific Information:
/usr/lib/libcrypto.dylib
abort() called
Invalid dylib load. Clients should not load the unversioned libcrypto dylib as it does not have a stable ABI.

注意:我不是安全专家,这个解决方案与加密库混淆!

我不认为您的问题源于 zsh 或 oh-my-zsh。我的最佳猜测:一些随 MacOS 10.15 安装的加密库与 Homebrew 的 python3 安装不兼容。

这是为我解决问题的方法

# Install openssl via homebrew.
# Note: According to homebrew, "openssl is keg-only, which means it was
# not symlinked into /usr/local, because Apple has deprecated use of
# OpenSSL in favor of its own TLS and crypto libraries."
brew install openssl
# Symlink those versions into /usr/local/lib, which gets Python to dynamically
# link against those instead of the version in /usr/lib/.
# Got the idea from https://forums.developer.apple.com/thread/119429
cd /usr/local/lib
sudo ln -s /usr/local/Cellar/openssl/1.0.2t/lib/libssl.1.0.0.dylib libssl.dylib
sudo ln -s /usr/local/Cellar/openssl/1.0.2t/lib/libcrypto.1.0.0.dylib libcrypto.dylib

我的上下文情况:

  • 最近升级到 MacOS 10.15
  • 我使用 python/pip 通过自制软件安装:brew install python
  • pip3 失败 SIGABRT

Header系统报错:

Process:               Python [52429]
Path:                  /usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python
Identifier:            Python
Version:               3.7.4 (3.7.4)
Code Type:             X86-64 (Native)
Parent Process:        zsh [43309]
Responsible:           iTerm2 [2316]
User ID:               501

Date/Time:             2019-10-09 09:52:18.148 -0700
OS Version:            Mac OS X 10.15 (19A583)
Report Version:        12
Bridge OS Version:     4.0 (17P572)
Anonymous UUID:        

Sleep/Wake UUID:       

Time Awake Since Boot: 9900 seconds
Time Since Wake:       7300 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Application Specific Information:
/usr/lib/libcrypto.dylib
abort() called
Invalid dylib load. Clients should not load the unversioned libcrypto dylib as it does not have a stable ABI.

r.xuan from this Apple Dev thread 确定了错误解决方法的步骤 Invalid dylib load. Clients should not load the unversioned libcrypto dylib as it does not have a stable ABI. 通过将 /usr/local/lib 中的 libssl.dyliblibcrypto.dylib 链接替换为 Homebrew 安装的 openssl.

中的库链接

步骤是:

获取新库

1) brew update && brew upgrade && brew install openssl

2) cd /usr/local/Cellar/openssl/1.0.2t/lib

3) sudo cp libssl.1.0.0.dylib libcrypto.1.0.0.dylib /usr/local/lib/

备份旧的

4) cd /usr/local/lib

5) mv libssl.dylib libssl_bak.dylib

6) mv libcrypto.dylib libcrypto_bak.dylib

创建新链接

7) sudo ln -s libssl.1.0.0.dylib libssl.dylib

8) sudo ln -s libcrypto.1.0.0.dylib libcrypto.dylib

我刚遇到同样的问题,手动 link 周围的东西感觉有点不舒服。

我可以简单地解决问题

  1. 正在通过自制软件安装 openssl:
    brew install openssl
    
  2. 通过 DYLD_LIBRARY_PATH 从 openssl 指向动态库:
    export DYLD_LIBRARY_PATH=/usr/local/opt/openssl/lib:$DYLD_LIBRARY_PATH
    

我刚刚将该行添加到我的 .zshrc。

编辑:根据 this question,使用 DYLD_FALLBACK_LIBRARY_PATH 可能优于 DYLD_LIBRARY_PATH

编辑 2:正如下面评论中提到的, 可能应该是公认的答案。只需重新安装 cryptography 软件包。

我更喜欢@bixel、@Juro Oravec 和@honkaboy 答案的组合:

brew install openssl
cd /usr/local/lib
sudo ln -s /usr/local/opt/openssl/lib/libssl.dylib libssl.dylib
sudo ln -s /usr/local/opt/openssl/lib/libcrypto.dylib libcrypto.dylib

这样,至少在理论上,更新 openssl 时,dylib 将始终指向最新版本。 /usr/local/opt/openssl其实是link到/usr/local/Cellar/openssl/Cellar/openssl/1.0.2t(brew安装的openssl版本)

问题发生的原因实际上是由 brew 解释的:

openssl is keg-only, which means it was not symlinked into /usr/local, because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

正在尝试 运行 brew link openssl:

Warning: Refusing to link macOS-provided software: openssl If you need to have openssl first in your PATH run: echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl you may need to set: export LDFLAGS="-L/usr/local/opt/openssl/lib" export CPPFLAGS="-I/usr/local/opt/openssl/include"

For pkg-config to find openssl you may need to set: export PKG_CONFIG_PATH="/usr/local/opt/openssl/lib/pkgconfig"

所以,基本上您需要手动 link 它们。

我在 ansible 上看到了类似的问题。罪魁祸首是 asn1crypto,问题是 already fixed

我的解决方案是手动删除它并使用 pip:

重新安装
  1. rm -r /usr/local/lib/python2.7/site-packages/asn1crypto*。这让 pip 可以毫无问题地工作。
  2. pip install asn1crypto,安装了 1.2.0:
  Found existing installation: asn1crypto 0.24.0
    Uninstalling asn1crypto-0.24.0:
      Successfully uninstalled asn1crypto-0.24.0
Successfully installed asn1crypto-1.2.0

注意:您可以在详细模式下通过 运行 python 检查 asn1crypto 是否是罪魁祸首,例如python -v $(which ansible)。就我而言,它在执行一些 asn1crypto 相关导入时崩溃了:

# /usr/local/lib/python2.7/site-packages/asn1crypto/_perf/_big_num_ctypes.pyc matches /usr/local/lib/python2.7/site-packages/asn1crypto/_perf/_big_num_ctypes.py
import asn1crypto._perf._big_num_ctypes # precompiled from /usr/local/lib/python2.7/site-packages/asn1crypto/_perf/_big_num_ctypes.pyc
[1]    59247 abort      python -v $(which ansible)

相关:https://github.com/Homebrew/homebrew-core/issues/44996

如果您使用 DevMate 的 Kevlar,请升级到 4.3.1,即 "Fixed macOS Catalina crash caused by version of libcrypto.dylib"。

看起来这是一个 Homebrew 问题。我做了 brew reinstall python3 并且成功了。

对我来说,重新安装 Python 的加密包就足够了。

pip uninstall cryptography
pip install cryptography

尝试:

python3 -m pip install oscrypto

对我有用!

按照上面提到的答案,想link libssl.dylib 文件但发现没有如下位置:

/usr/local/Cellar/openssl/1.0.2t/lib/

然而,@bixel 接受的答案在以下位置找到了文件

/usr/local/opt/openssl/lib

它对我有用。

我在使用 ctypes.cdll 打开 /usr/lib/libcrypto.dylibPython 3.7 时遇到了同样的问题。但是 dylib 可以用 Python 2.7 打开。

我用 brew install 安装了最新的 openssl,然后设置环境变量并按照他们上面的建议创建 links,没有发生任何好事。

经过几个小时的挖掘,我找到了解决方法。

我在 /usr 中找到了一些 libcrypto.X.dylib 如下,

/usr/lib/libcrypto.dylib
/usr/lib/libcrypto.0.9.7.dylib
/usr/lib/libcrypto.0.9.8.dylib
/usr/lib/libcrypto.35.dylib
/usr/lib/libcrypto.41.dylib
/usr/lib/libcrypto.42.dylib
/usr/lib/libcrypto.44.dylib

/usr/local/opt/openssl/lib/libcrypto.1.1.dylib
/usr/local/opt/openssl/lib/libcrypto.dylib

首先,我用下面的那个来代替 /usr/lib 中的那个。

os.environ['DYLD_FALLBACK_LIBRARY_PATH'] = '/usr/local/opt/openssl/lib'

可以加载但缺少一些api,

AttributeError: dlsym(0x..., ECDH_OpenSSL): symbol not found

我在脚本路径中为 /usr/lib/libcrypto.X.dylib 创建了一个 link。

ln -s /usr/lib/libcrypto.X.dylib lib/libcrypto.dylib

然后把路径添加到DYLD_FALLBACK_LIBRARY_PATH

os.environ['DYLD_FALLBACK_LIBRARY_PATH'] = 'lib' # It should be a absolute path

终于成功了。

使用以下步骤解决:

  • brew 更新 && brew 升级 && brew 重新安装 openssl
  • cd /usr/local/Cellar/openssl@1.1/1.1.1g/lib
  • sudo cp libssl.1.1.1.dylib libcrypto.1.1.1.dylib /usr/local/lib/
  • sudo ln -s libssl.1.0.0.dylib libssl.dylib
  • sudo ln -s libcrypto.1.0.0.dylib libcrypto.dylib`

恐怕 none 这些答案我可以接受。因为这些答案并没有解决问题。有的是侥幸,有的是误会,有的甚至是错误的。所以我会提供我自己的解决方案,并针对问题进行详细的解释。

Invalid dylib load. Clients should not load the unversioned libcrypto dylib as it does not have a stable ABI.

根本原因本身就很清楚了。您的 Python 试图打开(通过 dlopen)一个名为 libcrypto 的未版本控制的 OpenSSL 共享库。自 Catalina 以来,出于安全原因,Apple 不允许任何人使用它。所以解决方案很简单。仅使用版本控制 OpenSSL。

我编写了一个名为 openlib.py 的 python 脚本来重现该问题。

import sys
from ctypes.util import find_library
from ctypes import CDLL

name = sys.argv[1]
path = find_library(name)
print(f"path: {path}")
lib = CDLL(path)

我这里用系统Python做演示

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.15.7
BuildVersion:   19H15
$ /usr/bin/python3 --version
Python 3.8.2
$ ls -al /usr/lib/ | grep 'libcrypto\|libssl'
.rwxr-xr-x 1.1M root 22 Sep  8:29 libcrypto.0.9.7.dylib
.rwxr-xr-x 1.4M root 22 Sep  8:29 libcrypto.0.9.8.dylib
.rwxr-xr-x 1.5M root 22 Sep  8:29 libcrypto.35.dylib
.rwxr-xr-x 1.5M root 22 Sep  8:29 libcrypto.41.dylib
.rwxr-xr-x 1.5M root 22 Sep  8:29 libcrypto.42.dylib
.rwxr-xr-x 1.5M root 22 Sep  8:29 libcrypto.44.dylib
.rwxr-xr-x  32k root 22 Sep  8:29 libcrypto.dylib
.rwxr-xr-x 212k root 22 Sep  8:29 libssl.0.9.7.dylib
.rwxr-xr-x 335k root 22 Sep  8:30 libssl.0.9.8.dylib
.rwxr-xr-x 330k root 22 Sep  8:28 libssl.35.dylib
.rwxr-xr-x 313k root 22 Sep  8:29 libssl.43.dylib
.rwxr-xr-x 300k root 22 Sep  8:29 libssl.44.dylib
.rwxr-xr-x 294k root 22 Sep  8:29 libssl.46.dylib
.rwxr-xr-x  32k root 22 Sep  8:29 libssl.dylib
$ /usr/bin/python3 openlib.py libcrypto
path: /usr/lib/libcrypto.dylib
Abort trap: 6
$ /usr/bin/python3 openlib.py libcrypto.35
path: /usr/lib/libcrypto.35.dylib
$ /usr/bin/python3 openlib.py libcrypto.44
path: /usr/lib/libcrypto.44.dylib

如你所见。 Python 已因参数 libcrypto 崩溃,如下诊断报告所示。看起来很像,对吧?宾果!

Process:               Python [97291]
Path:                  /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/Resources/Python.app/Contents/MacOS/Python
Identifier:            Python
Version:               3.8.2 (3.8.2)
Build Info:            python3-73040006000000~117
Code Type:             X86-64 (Native)
Parent Process:        bash [84388]
Responsible:           iTerm2 [7705]
User ID:               501

Date/Time:             2020-12-26 00:28:00.281 +0800
OS Version:            Mac OS X 10.15.7 (19H15)
Report Version:        12
Anonymous UUID:        1C43F3DB-1783-4B94-B663-7F7E8D331B56


Time Awake Since Boot: 53000 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Application Specific Information:
/usr/lib/libcrypto.dylib
abort() called
Invalid dylib load. Clients should not load the unversioned libcrypto dylib as it does not have a stable ABI.

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib          0x00007fff69bba33a __pthread_kill + 10
1   libsystem_pthread.dylib         0x00007fff69c76e60 pthread_kill + 430
2   libsystem_c.dylib               0x00007fff69b41808 abort + 120
3   libcrypto.dylib                 0x00007fff6766b7e4 __report_load + 415

根据你的Python/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python的路径。显然,您的 Python 是由 Homebrew 安装的。我相信 Homebrew Python 3 已经 linked with keg-only OpenSSL. So It must be some packages that using unversioned OpenSSL. Looking at file ctypes/macholib/dyld.py 2。它按指定顺序在以下目录中搜索任何库:

DEFAULT_LIBRARY_FALLBACK = [
    os.path.expanduser("~/lib"),
    "/usr/local/lib",
    "/lib",
    "/usr/lib",
]
$ /usr/bin/python3
Python 3.8.2 (default, Nov  4 2020, 21:23:28)
[Clang 12.0.0 (clang-1200.0.32.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes.util import find_library
>>> find_library('libcrypto')
'/usr/lib/libcrypto.dylib'
>>>

因此,简单的解决方法是将版本控制 OpenSSL 链接到主目录中的库路径。

$ pwd
/Users/gasolwu
$ ln -s /usr/lib/libcrypto.44.dylib $HOME/lib/libcrypto.dylib
$ $ ls ~/lib
libcrypto.dylib

之后。通过 运行 提供的脚本 openlib.py 测试打开 OpenSSL。它 returns 库路径成功,没有崩溃。

$ /usr/bin/python3 openlib.py libcrypto
path: /Users/gasolwu/lib/libcrypto.dylib

我用Homebrew Python 3 too. So I fixed it a couple of days ago and have sent a pull request。如果您已经升级到 Python 的最新版本并且如果 PR 已经合并并且 bottle 已经构建并发布。只需 运行 带有 brew reinstall python@3.9 的命令将是解决问题的最简单方法。

不要通过禁用 SIP 和使用 sudo 覆盖系统 OpenSSL 来破坏您的系统。

不要安装浪费磁盘的重复库space。无需在任何位置安装另一个 OpenSSL。

不要使用环境变量DYLD_LIBRARY_PATH如下如果你不把这行添加到你的shell。如果你这样做,它会影响你机器上的每个程序。

export DYLD_LIBRARY_PATH=/usr/local/opt/openssl/lib:$DYLD_LIBRARY_PATH

终于。 如果您通过更新 python 依赖项解决了问题。你可能很幸运。一些软件包通过寻找版本控制 OpenSSL 解决了这个问题。但很多不是。