c - libsgx_capable.so : cannot open shared object file: No such file or directory

标签 c shared-libraries sgx

我正在尝试运行以下代码以在我的机器(BIOS:软件控制)中启用 SGX:

//enable_device.c
#include "stdio.h"
#include "sgx_capable.h"
#include "sgx_eid.h"
#include "sgx_urts.h"
#include "sgx_error.h"

int main(void) {
        sgx_device_status_t sgx_device_status;
        sgx_status_t ret;

        ret = sgx_cap_enable_device(&sgx_device_status);

        if(ret == SGX_SUCCESS) {
                switch(sgx_device_status) {
                        case SGX_ENABLED:
                                printf("The platform is enabled for Intel SGX.\n$
                                break;
                        case SGX_DISABLED_REBOOT_REQUIRED:
                                printf("This platform is disabled for Intel SGX.$
                                break;
                        case SGX_DISABLED_MANUAL_ENABLE:
                                printf("The platform is disabled for Intel SGX b$
                                break;
//                      case SGX_DISABLED_HYPERV_ENABLED:
//                              printf("The detected version of Windows* 10 is i$
//                              break;
                        case SGX_DISABLED_LEGACY_OS:
                                printf("The operating system does not support UE$
                                break;
                        case SGX_DISABLED_UNSUPPORTED_CPU:
                                printf("Intel SGX is not supported by this proce$
                                break;
                        case SGX_DISABLED:
                                printf("This platform is disabled for Intel SGX.$
                                break;
                        default:
                                printf("UNKNOWN RESPONSE\n");
                }
        } else {
                switch(ret) {
                        case SGX_ERROR_INVALID_PARAMETER:
                                printf("The sgx_device_status pointer is invalid$
                                break;
                        case SGX_ERROR_NO_PRIVILEGE:
                                printf("The application does not have the requir$
                                break;
//                      case SGX_ERROR_HYPERV_ENABLED:
//                              printf("The detected version of Windows* 10 is i$
//                              break;
                        default:
                                printf("An unexpected error is detected.\n");
                }
        }

        return 0;
}

这是我正在使用的 Makefile:

######### SGX TOOLS ######################
SGX_SDK := /usr/local/lib/intel/sgxsdk
SGX_LIBRARY_PATH := $(SGX_SDK)/lib64

######## App Settings ########
App_C_Files := enable_device.c
App_C_Flags := -fPIC -Wno-attributes -IInclude -IApp -I$(SGX_SDK)/include
App_Cpp_Flags := $(App_C_Flags) -std=c++11
App_Link_Flags := -L$(SGX_LIBRARY_PATH) -lsgx_capable
App_C_Objects := $(App_C_Files:.c=.o)
App_Name := app

.PHONY: all run

all: $(App_Name)

run: all

######## App Objects ########

enable_device.o: enable_device.c
        @$(CC) $(App_C_Flags) -c $< -o $@

$(App_Name): enable_device.o
        @$(CC) $^ -o $@ $(App_Link_Flags)

.PHONY: clean

clean:
        @rm -f $(App_Name) $(App_C_Objects)

当我运行应用程序时,我收到此消息:

The application does not have the required privileges to read an EFI variable. Run the application with the administrator privileges to enable the Intel SGX device status.

然后,我运行 sudo ./app 并收到以下错误:

./app: error while loading shared libraries: libsgx_capable.so: cannot open shared object file: No such file or directory

奇怪的是编译的时候找不到这个库:

$ldd app 
linux-vdso.so.1 (0x00007ffe065bc000)
libsgx_capable.so => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f15a060d000)
/lib64/ld-linux-x86-64.so.2 (0x00007f15a0c01000)

然后,我使用:

export LD_LIBRARY_PATH=/usr/local/lib/intel/sgxsdk/lib64/

我再次运行并返回相同的错误消息。谁能告诉我我错过了什么?

最佳答案

The strange thing is that this library is not being found when compiling.

在编译时(实际上是在链接时)被发现。否则 app 的链接会失败,它不会存在。 ldd 应用程序 输出:

libsgx_capable.so => not found

表明链接器找到了libsgx_capable.so,并适本地编写了一个NEEDEDapp.dynamic 部分为其输入,ldd - 调用 运行时加载程序 - 尝试解析为实际文件,但无法解析,因为 loader 现在无法在它始终查找的任何目录中找到 libsgx_capable.so:

  1. LD_LIBRARY_PATH 目录
  2. ldconfig /etc/ld.so.conf
  3. 中列出的缓存目录
  4. 可信目录 /lib/usr/lib

那是因为 /usr/local/lib/intel/sgxsdk/lib64/ 不是目录 23 并且在您运行 ldd app 时不是目录 1 之一。

你说 app 需要 root 权限,你用 sudo ./app 运行它, 但即使你执行:

$ export LD_LIBRARY_PATH=/usr/local/lib/intel/sgxsdk/lib64/
$ sudo ./app

你还会遇到:

./app: error while loading shared libraries: libsgx_capable.so: cannot open shared object file: No such file or directory

发生这种情况是因为 sudo 命令在默认情况下不会传递它所在的环境 被调用到它作为 root 执行的命令。见:

$ cat main.c
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char const *foobar = getenv("FOOBAR");
    printf("FOOBAR=[%s]\n",foobar ? foobar : "<null>");
    return 0;
}

$ gcc -Wall -o prog main.c
$ export FOOBAR=foobar
$ ./prog
FOOBAR=[foobar]
$ sudo ./prog
FOOBAR=[<null>]

要通过 sudo 传输环境设置,您可以将它们作为参数传递 到 sudo:

$ sudo FOOBAR=barfoo ./prog
FOOBAR=[barfoo]
$ echo $FOOBAR
foobar

或者你可以给sudo选项-E:

$ sudo -E ./prog
FOOBAR=[foobar]

所以要么:

$ sudo LD_LIBRARY_PATH=/usr/local/lib/intel/sgxsdk/lib64/ ./app

或:

$ export LD_LIBRARY_PATH=/usr/local/lib/intel/sgxsdk/lib64/
$ sudo -E ./app

会起作用。但是,您可能更愿意不依赖于 LD_LIBRARY_PATH 来 让 app 在运行时加载 libsgx_capable.so。在这种情况下,您可以将其放入 通过运行 ldconfig 缓存:

$ sudo ldconfig /usr/local/lib/intel/sgxsdk/lib64/

这将在 /usr/local/lib/intel/sgxsdk/lib64/ 中缓存任何共享库 用于装载机。

或者,如果您希望保留发行版定义的 ldconfig 缓存, 你可以改变:

App_Link_Flags := -L$(SGX_LIBRARY_PATH) -lsgx_capable

到:

App_Link_Flags := -L$(SGX_LIBRARY_PATH) -lsgx_capable -Wl,-rpath=$(SGX_LIBRARY_PATH)

在您的生成文件中。这将使链接器向 .dynamic 添加一个 RUNPATH 条目 app 中的部分提示加载程序搜索 /usr/local/lib/intel/sgxsdk/lib64/ 对于目录 1 之后和目录 23 之前的所需库。

关于c - libsgx_capable.so : cannot open shared object file: No such file or directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54695846/

相关文章:

c - 如何用新行替换空格?

c++ - 在 C++ 中使用数组调用方法

c++ - CUDA:堆栈和堆

c++ - Libxl + 窗口 + QT

c++ - 如何在 sgx ecall 中返回未知大小的指针?

c - 如何避免在使用此配置的每个函数中传递配置变量

java - VS2015 - 在 Android 项目中引用 .so 文件

linux - Linux 中包含多版本库导致的版本冲突如何解决

sgx - 英特尔 SGX 中的本地证明如何确保 Enclave 中的代码安全?