Skip to content
Permalink
Browse files

net: socket: Add userspace support to getsockopt()

Allow userspace application to call getsockopt() without crashing.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
  • Loading branch information...
jukkar authored and andrewboie committed Jun 19, 2019
1 parent ee84982 commit 6f32f17cdaf92cce81b89b5ad5c3519d04f5357c
Showing with 32 additions and 4 deletions.
  1. +2 −2 include/net/socket.h
  2. +30 −2 subsys/net/lib/sockets/sockets.c
@@ -349,8 +349,8 @@ __syscall int zsock_poll(struct zsock_pollfd *fds, int nfds, int timeout);
* if :option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
* @endrst
*/
int zsock_getsockopt(int sock, int level, int optname,
void *optval, socklen_t *optlen);
__syscall int zsock_getsockopt(int sock, int level, int optname,
void *optval, socklen_t *optlen);

/**
* @brief Set various socket options
@@ -1108,12 +1108,40 @@ int zsock_getsockopt_ctx(struct net_context *ctx, int level, int optname,
return -1;
}

int zsock_getsockopt(int sock, int level, int optname,
void *optval, socklen_t *optlen)
int z_impl_zsock_getsockopt(int sock, int level, int optname,
void *optval, socklen_t *optlen)
{
VTABLE_CALL(getsockopt, sock, level, optname, optval, optlen);
}

#ifdef CONFIG_USERSPACE
Z_SYSCALL_HANDLER(zsock_getsockopt, sock, level, optname, optval, optlen)
{
socklen_t kernel_optlen = *(socklen_t *)optlen;
void *kernel_optval;
int ret;

if (Z_SYSCALL_MEMORY_WRITE(optval, kernel_optlen)) {
errno = -EPERM;
return -1;
}

kernel_optval = z_thread_malloc(kernel_optlen);
Z_OOPS(!kernel_optval);

ret = z_impl_zsock_getsockopt(sock, level, optname,
kernel_optval, &kernel_optlen);

Z_OOPS(z_user_to_copy((void *)optval, kernel_optval, kernel_optlen));
Z_OOPS(z_user_to_copy((void *)optlen, &kernel_optlen,
sizeof(socklen_t)));

k_free(kernel_optval);

return ret;
}
#endif /* CONFIG_USERSPACE */

int zsock_setsockopt_ctx(struct net_context *ctx, int level, int optname,
const void *optval, socklen_t optlen)
{

0 comments on commit 6f32f17

Please sign in to comment.
You can’t perform that action at this time.