libmemif: Fix abstract sockets

This fixes size computation when using
abstract sockets with libmemif

Type: fix

Change-Id: I3a686e4ff2132b9fb295bbe30633958dcfec672b
Signed-off-by: Nathan Skrzypczak <nathan.skrzypczak@gmail.com>
This commit is contained in:
Nathan Skrzypczak
2021-05-07 19:39:07 +02:00
parent 754cffbe16
commit 814eee55fa
2 changed files with 22 additions and 13 deletions

View File

@ -500,9 +500,6 @@ memif_create_socket (memif_socket_handle_t *sock, memif_socket_args_t *args,
/* copy arguments to internal struct */ /* copy arguments to internal struct */
memcpy (&ms->args, args, sizeof (*args)); 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; ms->private_ctx = private_ctx;
if (ms->args.alloc == NULL) if (ms->args.alloc == NULL)
@ -723,6 +720,12 @@ error:
return err; return err;
} }
static inline int
memif_path_is_abstract (const char *filename)
{
return (filename[0] == '@');
}
int int
memif_request_connection (memif_conn_handle_t c) 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_control_channel_t *cc = NULL;
memif_fd_event_t fde; memif_fd_event_t fde;
memif_fd_event_data_t *fdata = NULL; memif_fd_event_data_t *fdata = NULL;
int sunlen = sizeof (un);
void *ctx; void *ctx;
if (conn == NULL) if (conn == NULL)
@ -773,6 +777,16 @@ memif_request_connection (memif_conn_handle_t c)
goto error; 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) if (conn->args.is_master != 0)
{ {
/* Configure socket optins */ /* Configure socket optins */
@ -781,7 +795,7 @@ memif_request_connection (memif_conn_handle_t c)
err = memif_syscall_error_handler (errno); err = memif_syscall_error_handler (errno);
goto error; 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); err = memif_syscall_error_handler (errno);
goto error; goto error;
@ -791,7 +805,7 @@ memif_request_connection (memif_conn_handle_t c)
err = memif_syscall_error_handler (errno); err = memif_syscall_error_handler (errno);
goto error; goto error;
} }
if (ms->args.path[0] != '\0') if (!memif_path_is_abstract (ms->args.path))
{ {
/* Verify that the socket was created */ /* Verify that the socket was created */
if (stat ((char *) ms->args.path, &file_stat) < 0) 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; err = MEMIF_ERR_NOMEM;
goto error; goto error;
} }
if (connect (sockfd, (struct sockaddr *) &un, if (connect (sockfd, (struct sockaddr *) &un, sunlen) != 0)
sizeof (struct sockaddr_un)) != 0)
{ {
err = MEMIF_ERR_CONNREFUSED; err = MEMIF_ERR_CONNREFUSED;
goto error; goto error;
@ -1739,7 +1752,7 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
if ((data_offset < 0) || if ((data_offset < 0) ||
((data_offset + b0->len) > c->run_args.buffer_size)) ((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); b0->desc_index & mask, data_offset, b0->len);
err = MEMIF_ERR_INVAL_ARG; err = MEMIF_ERR_INVAL_ARG;
goto done; goto done;
@ -1947,10 +1960,6 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
if (l0 + l1 < buflen) if (l0 + l1 < buflen)
{ {
md->socket_path = (uint8_t *) memcpy (buf + l0, ms->args.path, 108); md->socket_path = (uint8_t *) memcpy (buf + l0, ms->args.path, 108);
if (md->socket_path[0] == '\0')
{
md->socket_path[0] = '@';
}
l0 += l1; l0 += l1;
} }
else else

View File

@ -274,7 +274,7 @@ memif_msg_enq_disconnect (memif_control_channel_t *cc, uint8_t *err_string,
uint16_t l = sizeof (d->string); uint16_t l = sizeof (d->string);
if (l > 96) 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); sizeof (d->string) - 1);
} }
strlcpy ((char *) d->string, (char *) err_string, sizeof (d->string)); strlcpy ((char *) d->string, (char *) err_string, sizeof (d->string));