Browse Source

[LibOS,Pal/Linux-SGX] Fix bugs for Unix domain sockets

This commit fixes trivial bugs in connect()/listen() syscalls
pertaining to Unix domain sockets. In particular, connect()
must not fail if socket name doesn't exist, and getsockname()
must be supplied with a legitimate length of the address.
Dmitrii Kuvaiskii 5 years ago
parent
commit
426b78946c
2 changed files with 9 additions and 4 deletions
  1. 7 2
      LibOS/shim/src/sys/shim_socket.c
  2. 2 2
      Pal/src/host/Linux-SGX/sgx_enclave.c

+ 7 - 2
LibOS/shim/src/sys/shim_socket.c

@@ -756,8 +756,13 @@ int shim_do_connect (int sockfd, struct sockaddr * addr, int addrlen)
         char * spath = saddr->sun_path;
         struct shim_dentry * dent;
 
-        if ((ret = path_lookupat(NULL, spath, LOOKUP_CREATE, &dent, NULL)) < 0)
-            goto out;
+        if ((ret = path_lookupat(NULL, spath, LOOKUP_CREATE, &dent, NULL)) < 0) {
+            // DEP 7/3/17: We actually want either 0 or -ENOENT, as the
+            // expected case is that the name is free (and we get the dent to
+            // populate the name)
+            if (ret != -ENOENT || !dent)
+                goto out;
+        }
 
         struct shim_unix_data * data = dent->data;
 

+ 2 - 2
Pal/src/host/Linux-SGX/sgx_enclave.c

@@ -301,7 +301,7 @@ static int sgx_ocall_sock_listen(void * pms)
         goto err_fd;
 
     if (ms->ms_addr) {
-        socklen_t addrlen;
+        socklen_t addrlen = ms->ms_addrlen;
         ret = INLINE_SYSCALL(getsockname, 3, fd, ms->ms_addr, &addrlen);
         if (IS_ERR(ret))
             goto err_fd;
@@ -394,7 +394,7 @@ static int sgx_ocall_sock_connect(void * pms)
     }
 
     if (ms->ms_bind_addr && !ms->ms_bind_addr->sa_family) {
-        socklen_t addrlen;
+        socklen_t addrlen = ms->ms_bind_addrlen;
         ret = INLINE_SYSCALL(getsockname, 3, fd, ms->ms_bind_addr,
                              &addrlen);
         if (IS_ERR(ret))