gramineproject / graphene

Graphene / Graphene-SGX - a library OS for Linux multi-process applications, with Intel SGX support
https://grapheneproject.io
GNU Lesser General Public License v3.0
771 stars 260 forks source link

[LibOS] `bind` Unix socket, `addrlen` parameter checking is different from Linux, and causes error in Python #2609

Closed llly closed 3 years ago

llly commented 3 years ago

Description of the problem

Graphene shim_do_bind function return EINVAL for "addrlen is wrong" when addrlen is not same as sizeof(struct sockaddr_un), which is 110. https://github.com/oscarlab/graphene/blob/20156c6e23ea6570614197a97daf77dcd764153b/LibOS/shim/src/sys/shim_socket.c#L467-L469 But smaller size can work on Linux.

Steps to reproduce

Copy example from man bind and change

 if (bind(sfd, (struct sockaddr *) &my_addr,  sizeof(struct sockaddr_un)) == -1)

to

 if (bind(sfd, (struct sockaddr *) &my_addr, strlen(MY_SOCK_PATH) + 4) == -1)

Compile and create Graphene manifest.

Expected results

App with and without Graphene both can run without error.

Actual results

App without Graphene can run without error. App with Graphene returns bind: Invalid argument.

Additional information

Bind Unix socket using Python can work on Linux, but fails with Graphene. https://hg.python.org/cpython/file/3.6/Modules/socketmodule.c#l2700

res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);

addrlen is calculated at https://hg.python.org/cpython/file/3.6/Modules/socketmodule.c#l1581

*len_ret = path.len + offsetof(struct sockaddr_un, sun_path);
dimakuv commented 3 years ago

@llly So just replacing https://github.com/oscarlab/graphene/blob/20156c6e23ea6570614197a97daf77dcd764153b/LibOS/shim/src/sys/shim_socket.c#L468

with if (addrlen > sizeof(struct sockaddr_un)) should work?

llly commented 3 years ago

Yes. I think so.

dimakuv commented 3 years ago

Please try https://github.com/oscarlab/graphene/pull/2620