OSX和chroot无法找到/ bin / bash

时间:2013-10-09 03:37:13

标签: linux macos bash chroot

我正试图让chroot在我的mac书上工作。我设置了jail目录以包含/ bin / sh和/ bin / bash的所有要求(递归地称为/ usr / bin / otool)

# ls /var/chroot/*/*
/var/chroot/bin/bash    /var/chroot/bin/sh

/var/chroot/usr/lib:
libDiagnosticMessagesClient.dylib       libauto.dylib
libc++abi.dylib                         libobjc.A.dylib
libSystem.B.dylib                       libc++.1.dylib
libncurses.5.4.dylib                    system

当我尝试在/ var / chroot上使用chroot时,它一直说它无法找到/ bin / bash

# chroot /var/chroot/
chroot: /bin/sh: No such file or directory
$ sudo chroot /var/chroot/
chroot: /bin/bash: No such file or directory

有什么想法导致chroot无法在我的Mac上运行吗?

OSX版本10.8.4

编辑: 在CentOS上,当我运行ldd / bin / bash时,我得到了所有需要的库。如果我不复制它们,它说/ bin / bash:没有这样的文件或目录。所以我认为这意味着在mac上我缺少libs;只是不确定是哪一个。

2 个答案:

答案 0 :(得分:6)

当你说你做了一个递归的otool -L你的意思是什么?这项工作需要很多共享库,例如:

otool -L /bin/bash
/bin/bash:
    /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)

然后你必须对那些运行otool -L(然后otool -L运行那些):

otool -L /usr/lib/libSystem.B.dylib
/usr/lib/libSystem.B.dylib:
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)
    /usr/lib/system/libcache.dylib (compatibility version 1.0.0, current version 62.0.0)
    /usr/lib/system/libcommonCrypto.dylib (compatibility version 1.0.0, current version 60049.0.0)
    /usr/lib/system/libcompiler_rt.dylib (compatibility version 1.0.0, current version 35.0.0)
    /usr/lib/system/libcopyfile.dylib (compatibility version 1.0.0, current version 103.0.0)
    /usr/lib/system/libcorecrypto.dylib (compatibility version 1.0.0, current version 1.0.0)
    /usr/lib/system/libdispatch.dylib (compatibility version 1.0.0, current version 339.1.9)
    /usr/lib/system/libdyld.dylib (compatibility version 1.0.0, current version 239.3.0)
    /usr/lib/system/libkeymgr.dylib (compatibility version 1.0.0, current version 28.0.0)
    /usr/lib/system/liblaunch.dylib (compatibility version 1.0.0, current version 842.1.4)
    /usr/lib/system/libmacho.dylib (compatibility version 1.0.0, current version 845.0.0)
    /usr/lib/system/libquarantine.dylib (compatibility version 1.0.0, current version 71.0.0)
    /usr/lib/system/libremovefile.dylib (compatibility version 1.0.0, current version 33.0.0)
    /usr/lib/system/libsystem_asl.dylib (compatibility version 1.0.0, current version 217.1.4)
    /usr/lib/system/libsystem_blocks.dylib (compatibility version 1.0.0, current version 63.0.0)
    /usr/lib/system/libsystem_c.dylib (compatibility version 1.0.0, current version 997.1.1)
    /usr/lib/system/libsystem_configuration.dylib (compatibility version 1.0.0, current version 596.12.0)
    /usr/lib/system/libsystem_dnssd.dylib (compatibility version 1.0.0, current version 522.1.11)
    /usr/lib/system/libsystem_info.dylib (compatibility version 1.0.0, current version 449.1.3)
    /usr/lib/system/libsystem_kernel.dylib (compatibility version 1.0.0, current version 2422.1.72)
    /usr/lib/system/libsystem_m.dylib (compatibility version 1.0.0, current version 3047.16.0)
    /usr/lib/system/libsystem_malloc.dylib (compatibility version 1.0.0, current version 23.1.10)
    /usr/lib/system/libsystem_network.dylib (compatibility version 1.0.0, current version 241.3.0)
    /usr/lib/system/libsystem_notify.dylib (compatibility version 1.0.0, current version 121.0.0)
    /usr/lib/system/libsystem_platform.dylib (compatibility version 1.0.0, current version 24.1.4)
    /usr/lib/system/libsystem_pthread.dylib (compatibility version 1.0.0, current version 53.1.4)
    /usr/lib/system/libsystem_sandbox.dylib (compatibility version 1.0.0, current version 278.10.0)
    /usr/lib/system/libsystem_stats.dylib (compatibility version 1.0.0, current version 93.1.26)
    /usr/lib/system/libunc.dylib (compatibility version 1.0.0, current version 28.0.0)
    /usr/lib/system/libunwind.dylib (compatibility version 1.0.0, current version 35.3.0)
    /usr/lib/system/libxpc.dylib (compatibility version 1.0.0, current version 300.1.17)

我做了一个快速测试:

mkdir -p /Users/chroot/bin /Users/chroot/usr/lib/system
cp /bin/bash /Users/chroot/bin
cp /usr/lib/* /Users/chroot/usr/lib
cp /usr/lib/system/* /Users/chroot/usr/lib/system

chroot /Users/chroot /bin/bash

这很有效,所以我假设您缺少所需的共享库。您可以编写一个脚本来完全执行递归otool -L并获得所需的确切共享库,但只是进行批量复制可能更容易。

答案 1 :(得分:6)

您需要将/usr/lib/dyld复制到chroot jail以获取动态链接器。如果不存在,则尝试在chroot jail中执行任何操作将失败而没有Killed: 9以外的任何错误。复制/usr/lib/dyld后,如果您缺少任何其他库,则会收到错误,例如:

dyld: Library not loaded: /usr/lib/libncurses.5.4.dylib
  Referenced from: /bin/bash
  Reason: image not found

从我的文件列表中我看到你没有/usr/lib/dyld,所以我认为这很可能是你的问题。我不确定为什么你会因为这个问题得到No such file or directory而不是Killed: 9;可能这是操作系统版本的差异 - 我在Mac OS X 10.10.5上测试,你是(或者是)用Mac OS X 10.8.4进行测试。

hwatkins的回答当然有效,因为它复制了/usr/lib/dyld和每个必需的dylib来运行/bin/bash到监狱。但是,在设置监狱时,我更愿意只复制最低限度以使其正常工作。一旦你复制了 dyld和bash,您可以按照错误消息获取所需的所有其他库的路径。 (有点费力,但保证最低限度。)

递归调用otool -L的方法也适用于* .dylib文件,但它不会告诉您/usr/lib/dyld。这是因为-L打印使用的共享库,但/usr/lib/dyld并不完全是共享库。如果在可执行库上运行otool -l,您将看到-L输出与加载命令LC_LOAD_DYLIB(以及一些变体,如LC_REEXPORT_DYLIB)匹配,而参考至dyld的{​​{1}}位于LC_LOAD_DYLINKER,而-L未输出。