编译过时的 PHP 会产生带有 OpenSSL 函数的核心转储。 OpenSSL 版本不匹配?

compiling outdated PHP yields core dump with OpenSSL functions. OpenSSL version mismatch?

我使用这些 ./config 选项编译了 OpenSSL 0.9.8x:

./config --prefix=/usr/local/openssl-0.9.8 --openssldir=/usr/local/openssl-0.9.8

我正在使用这些 ./configure 选项(以及其他选项)编译我的 PHP 版本:

   --with-openssl=/usr/local/openssl-0.9.8
   --with-openssl-dir=/usr/local/openssl-0.9.8

问题是当我 运行 一个带有 openssl_public_encrypt 的 PHP 脚本时,我遇到了分段错误。

这是 gdb 所说的:

Program terminated with signal 11, Segmentation fault.
#0  0x00007fd3381c5a48 in RSA_public_encrypt () from /usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.0

在 /usr/local/openssl-0.9.8/lib/ 中有 libcrypto.so.0.9.8 那么为什么不使用它呢?

这是我的 OpenSSL Makefile:

https://pastebin.com/0QSqLCr8

这是我的 PHP Makefile:

https://pastebin.com/dGmu0SYZ

这是一个重现问题的 Dockefile:

https://pastebin.com/ziZzvTh8

有什么想法吗?谢谢!

问题是 ld-linux 在运行时将 link 解析为错误的库。

尝试通过设置LD_LIBRARY_PATH来执行它: LD_LIBRARY_PATH=/usr/local/openssl-0.9.8/lib php very_old_php_44_script_which_shall_not_be_used.php

如果有效,您可能希望使用以下方法执行它:LD_DEBUG=all php ... 检查动态 linker 在哪里以及为什么不使用 0.9.8 openssl 版本。

使用 LD_LIBRARY_PATH 通常可以解决问题,但在这种情况下不适用于您的问题。

为了修复段错误,我已经根据您的文件重写了 Dockerfile。

  1. 在 PHP.

    的配置阶段删除 --disable-rpath--libdir 参数

    我们使用 rpath 在运行时定位共享对象,以便在 /usr/local/openssl-0.9.8x.

    上自定义构建 openssl

    请参阅 https://linux.die.net/man/1/ld

    中的选项 -rpath=dir

    Add a directory to the runtime library search path. This is used when linking an ELF executable with shared objects. All -rpath arguments are concatenated and passed to the runtime linker, which uses them to locate shared objects at runtime. The -rpath option is also used when locating shared objects which are needed by shared objects explicitly included in the link

  2. 在 OpenSSL 的配置脚本中添加 shared 选项

    构建共享库(libcrypto.so.0.9.8 和 libssl.so.0.9.8)

  3. Link kerberos 库仅在 /usr/kerberos/lib

    不是链接从/usr/lib/x86_64-linux-gnu到/usr/kerberos/lib的所有库,运行时搜索路径的顺序是/usr/kerberos/lib:/usr/local/openssl-0.9.8/lib

这里是变化

# https://pastebin.com/ziZzvTh8
--- ziZzvTh8.txt    2019-10-08 10:31:33.229217226 +0800
+++ Dockerfile   2019-10-08 12:07:03.271948150 +0800
@@ -8,7 +8,7 @@
     && wget --no-check-certificate http://www.openssl.org/source/openssl-0.9.8x.tar.gz \
     && tar xvfz openssl-0.9.8x.tar.gz \
     && cd openssl-0.9.8x \
-    && ./config --prefix=/usr/local/openssl-0.9.8 \
+    && ./config shared --prefix=/usr/local/openssl-0.9.8 \
     && make \
     && make install

@@ -23,7 +23,8 @@
     && ln -s /usr/lib/x86_64-linux-gnu/libexpat.so /usr/lib/ \
     && ln -s /usr/lib/x86_64-linux-gnu/libmysqlclient.so /usr/lib/libmysqlclient.so \
     && mkdir /usr/kerberos \
-    && ln -s /usr/lib/x86_64-linux-gnu /usr/kerberos/lib
+    && ln -s /usr/lib/x86_64-linux-gnu/mit-krb5 /usr/kerberos/lib
+

 RUN apt-get build-dep -y php5

@@ -43,7 +44,6 @@
        --with-zlib \
        --with-gd \
        --with-pgsql \
-       --disable-rpath \
        --enable-inline-optimization \
        --with-bz2 \
        --with-zlib \
@@ -62,7 +62,6 @@
        --enable-gd-native-ttf \
        --with-openssl=/usr/local/openssl-0.9.8 \
        --with-openssl-dir=/usr/local/openssl-0.9.8 \
-       --with-libdir=/lib/x86_64-linux-gnu \
        --enable-ftp \
        --with-imap \
        --with-imap-ssl \
@@ -72,4 +71,6 @@
      && make \
      && make install-cli

+ADD test.php /root/test.php
+
 CMD ["bash"]

test.php

<?php

$key = <<<EOF
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmHzD76i8DA25nC+Qsswi
OM0lW+gViiQD4tEm7suxBc2BGibtdlrsprVIId92hSjQKx4x8+XVWU6k89T5vy8Y
txpXN759OWdGkDi8uvZuYclMjW9Rao+oqSvbXH37R7oSY287I+6uOHclGhniQN3q
RyoXBkbhDk0/FTI/i549q/gGk1UZYv449KLrDOqmtohRcIyAYVnvvWtD1kIzourq
hMtEIrPqwoBqTaUA9kOIXw1jMovao2TN52j48KgOg9KjqtdwUwD9e6n7hJd/subF
6woc8L7zjJFOHH5gacUC7vtiMpBpnSyLQpjFLepYYwftjsRmg4xLdh+Zvgw3xqi4
lwIDAQAB
-----END PUBLIC KEY-----
EOF;

var_dump(openssl_public_encrypt($data, $crypted, $key));
var_dump($crypted);

结果

root@7c5df089bcb0:/# php -v
PHP 4.4.9 (cli) (built: Oct  8 2019 04:09:29)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

root@7c5df089bcb0:/# php -i | grep OpenSSL
CURL Information => libcurl/7.26.0 OpenSSL/0.9.8x zlib/1.2.7 libidn/1.25 libssh2/1.4.2 librtmp/2.3
OpenSSL support => enabled
OpenSSL Version => OpenSSL 0.9.8x 10 May 2012

root@7c5df089bcb0:/# php /root/test.php
bool(true)
string(256) "W`r�b��e��',뱌Zł^�$�֗��S����w�j�د<������� �)<��j��JL(f@�A���5_S�X=g-?0M�(�d�����+���     �nD*gzË��ڞc'�\'͗�'vnmo�G�Bv�
#~�y D!��lb�t^���| )[za��5���y�G{�\�"

root@7c5df089bcb0:/# ldd `which php` | egrep 'libssl|libcrypto'
        libssl.so.0.9.8 => /usr/local/openssl-0.9.8/lib/libssl.so.0.9.8 (0x00007efe86da1000)
        libcrypto.so.0.9.8 => /usr/local/openssl-0.9.8/lib/libcrypto.so.0.9.8 (0x00007efe86a0b000)
        libssl.so.1.0.0 => /usr/lib/x86_64-linux-gnu/libssl.so.1.0.0 (0x00007efe8401b000)
        libcrypto.so.1.0.0 => /usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.0 (0x00007efe83c21000)

root@7c5df089bcb0:/# objdump -p `which php` | grep RPATH
  RPATH                /usr/lib/x86_64-linux-gnu:/usr/kerberos/lib:/usr/local/openssl-0.9.8/lib