From 814eee55faa81252e50b7e4108d93bbb67aa4c3a Mon Sep 17 00:00:00 2001 From: Nathan Skrzypczak Date: Fri, 7 May 2021 19:39:07 +0200 Subject: libmemif: Fix abstract sockets This fixes size computation when using abstract sockets with libmemif Type: fix Change-Id: I3a686e4ff2132b9fb295bbe30633958dcfec672b Signed-off-by: Nathan Skrzypczak --- extras/libmemif/src/main.c | 33 +++++++++++++++++++++------------ extras/libmemif/src/socket.c | 2 +- 2 files changed, 22 insertions(+), 13 deletions(-) (limited to 'extras/libmemif') diff --git a/extras/libmemif/src/main.c b/extras/libmemif/src/main.c index 6a8e8b942dd..c1ab3b804b2 100644 --- a/extras/libmemif/src/main.c +++ b/extras/libmemif/src/main.c @@ -500,9 +500,6 @@ memif_create_socket (memif_socket_handle_t *sock, memif_socket_args_t *args, /* copy arguments to internal struct */ memcpy (&ms->args, args, sizeof (*args)); - /* Handle abstract socket by converting '@' -> '\0' */ - if (ms->args.path[0] == '@') - ms->args.path[0] = '\0'; ms->private_ctx = private_ctx; if (ms->args.alloc == NULL) @@ -723,6 +720,12 @@ error: return err; } +static inline int +memif_path_is_abstract (const char *filename) +{ + return (filename[0] == '@'); +} + int memif_request_connection (memif_conn_handle_t c) { @@ -736,6 +739,7 @@ memif_request_connection (memif_conn_handle_t c) memif_control_channel_t *cc = NULL; memif_fd_event_t fde; memif_fd_event_data_t *fdata = NULL; + int sunlen = sizeof (un); void *ctx; if (conn == NULL) @@ -773,6 +777,16 @@ memif_request_connection (memif_conn_handle_t c) goto error; } + if (memif_path_is_abstract (ms->args.path)) + { + /* Ensure the string is NULL terminated */ + un.sun_path[sizeof (un.sun_path) - 1] = '\0'; + /* sunlen is strlen(un.sun_path) + sizeof(un.sun_family) */ + sunlen = strlen (un.sun_path) + (sizeof (un) - sizeof (un.sun_path)); + /* Handle abstract socket by converting '@' -> '\0' */ + un.sun_path[0] = '\0'; + } + if (conn->args.is_master != 0) { /* Configure socket optins */ @@ -781,7 +795,7 @@ memif_request_connection (memif_conn_handle_t c) err = memif_syscall_error_handler (errno); goto error; } - if (bind (sockfd, (struct sockaddr *) &un, sizeof (un)) < 0) + if (bind (sockfd, (struct sockaddr *) &un, sunlen) < 0) { err = memif_syscall_error_handler (errno); goto error; @@ -791,7 +805,7 @@ memif_request_connection (memif_conn_handle_t c) err = memif_syscall_error_handler (errno); goto error; } - if (ms->args.path[0] != '\0') + if (!memif_path_is_abstract (ms->args.path)) { /* Verify that the socket was created */ if (stat ((char *) ms->args.path, &file_stat) < 0) @@ -815,8 +829,7 @@ memif_request_connection (memif_conn_handle_t c) err = MEMIF_ERR_NOMEM; goto error; } - if (connect (sockfd, (struct sockaddr *) &un, - sizeof (struct sockaddr_un)) != 0) + if (connect (sockfd, (struct sockaddr *) &un, sunlen) != 0) { err = MEMIF_ERR_CONNREFUSED; goto error; @@ -1739,7 +1752,7 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, if ((data_offset < 0) || ((data_offset + b0->len) > c->run_args.buffer_size)) { - DBG ("slot: %d, data_offset: %d, length: %d", + DBG ("slot: %d, data_offset: %ld, length: %d", b0->desc_index & mask, data_offset, b0->len); err = MEMIF_ERR_INVAL_ARG; goto done; @@ -1947,10 +1960,6 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md, if (l0 + l1 < buflen) { md->socket_path = (uint8_t *) memcpy (buf + l0, ms->args.path, 108); - if (md->socket_path[0] == '\0') - { - md->socket_path[0] = '@'; - } l0 += l1; } else diff --git a/extras/libmemif/src/socket.c b/extras/libmemif/src/socket.c index a9db7705fe9..eaf9d2f7ca2 100644 --- a/extras/libmemif/src/socket.c +++ b/extras/libmemif/src/socket.c @@ -274,7 +274,7 @@ memif_msg_enq_disconnect (memif_control_channel_t *cc, uint8_t *err_string, uint16_t l = sizeof (d->string); if (l > 96) { - DBG ("Disconnect string too long. Sending the first %d characters.", + DBG ("Disconnect string too long. Sending the first %ld characters.", sizeof (d->string) - 1); } strlcpy ((char *) d->string, (char *) err_string, sizeof (d->string)); -- cgit 1.2.3-korg