Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable gdb plugin to work with enclave from buffer #902

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gilanghamidy
Copy link

  • Initially, the gdb requires the enclave file to load the symbol. However, if we create the enclave from sgx_create_enclave_from_buffer_ex, the file information is not populated by the URTS, which prevents the gdb plugin to load the symbols
  • This patch allows the gdb plugin to hook into the _create_enclave_from_buffer_ex function and perform necessary patching to allow the enclave not created from file to be debugged

This fixes the long-running issue of #482

The signature of the entry function:

sgx_status_t _create_enclave_from_buffer_ex(const bool debug, uint8_t *base_addr, uint64_t file_size, se_file_t& file, ...);

The idea is that the debugger internally stops in the entry point of _create_enclave_from_buffer_ex check the file parameter if the file.name is nullptr (pointer to file is stored in $ecx). If so, we create a temporary file in the /tmp directory, and dump the enclave buffer on the base_addr ($rsi). Then, we replace the file.name value from nullptr into a dynamically-allocated string that contains the path to the temporary file. During the breakpoint, we call malloc from gdb to obtain the dynamically-allocated buffer to store the string so that it can be stored in the normal way by the URTS.

The debugger also keeps track over the temporary buffer file and malloc-ed pointer, which after the enclave is destroyed and fini_enclave_debug is called, it deletes the temporary buffer file and the malloc-ed pointer by calling free.

In this way, no changes at all are required to the internal URTS, PSW, and API compatibility. However, it may only work as long as the _create_enclave_from_buffer_ex is present in the symbol table. I tested it using the normal build (non-debug PSW), and it looks like the compiler retains the local symbol so it can still be captured by the breakpoint.


# dump enclave to file
dump_name = "/tmp/enclave_dump_{0}.bin".format(time.time())
buffer_ptr = gdb.parse_and_eval("$rsi")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code assumes we are under 64-bit. Currently we still need to support 32-bit so we may need to add that support as well just like we do for several other internal breakpoints

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thank you for your review. Indeed I was thinking the same thing about the support for 32-bit as the SDK and SGX is also support 32-bit system. However, I am not sure how to test that since I am running the 64-bit system. I assume I will need to do either cross-compiling or install a 32-bit VM. Perhaps the difference is that the debugger now needs to inspect the stack as the function parameter is now spilled to the stack instead of register. But I guess we can do that. I'll see if I can help in the coming days :)

# This breakpoint is to handle enclave creation with buffer
class CreateBufferEnclaveBreakpoint(gdb.Breakpoint):
def __init__(self):
gdb.Breakpoint.__init__ (self, spec="_create_enclave_from_buffer_ex", internal=1)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you mentioned in PR description, we rely on the existence of this symbol _create_enclave_from_buffer_ex. I think this may not be a problem because if the user is actually doing the debugging, s/he needs to install the libsgx-urts-dbgsym package anyway, and the symbols will be there.

Copy link

@xxu36 xxu36 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general idea looks good to me, thanks for the contribution :)

- Initially, the gdb requires the enclave file to load the symbol. However, if we create the enclave from `sgx_create_enclave_from_buffer_ex`, the file information is not populated by the URTS, which prevents the gdb plugin to load the symbols
- This patch allows the gdb plugin to hook into the `_create_enclave_from_buffer_ex` function and perform necessary patching to allow enclave not from file to be debugged

Signed-off-by: Gilang Mentari Hamidy <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants