diff --git a/driver/bpf/filler_helpers.h b/driver/bpf/filler_helpers.h index 56d9d57fda..958853bc86 100644 --- a/driver/bpf/filler_helpers.h +++ b/driver/bpf/filler_helpers.h @@ -420,14 +420,22 @@ static __always_inline bool bpf_getsockname(struct socket *sock, struct unix_sock *u; struct unix_address *addr; - if(peer) + if(peer) { sk = _READ(((struct unix_sock *)sk)->peer); + if(!sk) { + return false; + } + } u = (struct unix_sock *)sk; addr = _READ(u->addr); if(!addr) { sunaddr->sun_family = AF_UNIX; sunaddr->sun_path[0] = 0; + // The first byte to 0 can be confused with an `abstract socket address` for this reason + // we put also the second byte to 0 to comunicate to the caller that the address is not + // valid. + sunaddr->sun_path[1] = 0; } else { unsigned int len = _READ(addr->len); @@ -897,13 +905,11 @@ static __always_inline long bpf_fd_to_socktuple(struct filler_data *data, us_name = ((struct sockaddr_un *)peer_address)->sun_path; } - size = 1 + 8 + 8; int res = unix_socket_path( &data->buf[(data->state->tail_ctx.curoff + 1 + 8 + 8) & SCRATCH_SIZE_HALF], us_name, UNIX_PATH_MAX); - size += res; - + size = 1 + 8 + 8 + res; break; } } diff --git a/driver/modern_bpf/helpers/store/auxmap_store_params.h b/driver/modern_bpf/helpers/store/auxmap_store_params.h index c181012e3b..005fc4584a 100644 --- a/driver/modern_bpf/helpers/store/auxmap_store_params.h +++ b/driver/modern_bpf/helpers/store/auxmap_store_params.h @@ -776,7 +776,7 @@ static __always_inline void auxmap__store_socktuple_param(struct auxiliary_map * case AF_UNIX: { struct unix_sock *socket_local = (struct unix_sock *)sk; - struct unix_sock *socket_remote = (struct unix_sock *)BPF_CORE_READ(socket_local, peer); + struct unix_sock *socket_peer = (struct unix_sock *)BPF_CORE_READ(socket_local, peer); char *path = NULL; /* Pack the tuple info: @@ -787,18 +787,16 @@ static __always_inline void auxmap__store_socktuple_param(struct auxiliary_map * */ push__u8(auxmap->data, &auxmap->payload_pos, socket_family_to_scap(socket_family)); if(direction == OUTBOUND) { - push__u64(auxmap->data, &auxmap->payload_pos, (uint64_t)socket_remote); + push__u64(auxmap->data, &auxmap->payload_pos, (uint64_t)socket_peer); push__u64(auxmap->data, &auxmap->payload_pos, (uint64_t)socket_local); - path = BPF_CORE_READ(socket_remote, addr, name[0].sun_path); + path = BPF_CORE_READ(socket_peer, addr, name[0].sun_path); // empty } else { push__u64(auxmap->data, &auxmap->payload_pos, (uint64_t)socket_local); - push__u64(auxmap->data, &auxmap->payload_pos, (uint64_t)socket_remote); + push__u64(auxmap->data, &auxmap->payload_pos, (uint64_t)socket_peer); path = BPF_CORE_READ(socket_local, addr, name[0].sun_path); } - unsigned long start_reading_point; - char first_path_byte = *(char *)path; - if(first_path_byte == '\0') { + if(path[0] == '\0') { /* Please note exceptions in the `sun_path`: * Taken from: https://man7.org/linux/man-pages/man7/unix.7.html * @@ -808,14 +806,12 @@ static __always_inline void auxmap__store_socktuple_param(struct auxiliary_map * * * So in this case, we need to skip the initial `\0`. */ - start_reading_point = (unsigned long)path + 1; - } else { - start_reading_point = (unsigned long)path; + path++; } uint16_t written_bytes = push__charbuf(auxmap->data, &auxmap->payload_pos, - start_reading_point, + (unsigned long)path, MAX_UNIX_SOCKET_PATH, KERNEL); final_param_len = FAMILY_SIZE + KERNEL_POINTER + KERNEL_POINTER + written_bytes; diff --git a/driver/ppm_events.c b/driver/ppm_events.c index f1bc33af4a..731bd448bd 100644 --- a/driver/ppm_events.c +++ b/driver/ppm_events.c @@ -861,6 +861,11 @@ static struct socket *ppm_sockfd_lookup_light(int fd, int *err, int *fput_needed */ static void unix_socket_path(char *dest, const char *path, size_t size) { + if(path == NULL) { + dest[0] = '\0'; + return; + } + if(path[0] == '\0') { /* Please note exceptions in the `sun_path`: * Taken from: https://man7.org/linux/man-pages/man7/unix.7.html @@ -1173,7 +1178,8 @@ uint16_t fd_to_socktuple(int fd, } else { *(uint64_t *)(targetbuf + 1) = (uint64_t)(unsigned long)speer; *(uint64_t *)(targetbuf + 1 + 8) = (uint64_t)(unsigned long)us; - sock_getname(sock, (struct sockaddr *)&peer_address, 1); + err = sock_getname(sock, (struct sockaddr *)&peer_address, 1); + ASSERT(err == 0); us_name = ((struct sockaddr_un *)&peer_address)->sun_path; } diff --git a/test/libsinsp_e2e/unix_udp_client_server.cpp b/test/libsinsp_e2e/unix_udp_client_server.cpp index 13a62b47d5..b76a7f3d8d 100644 --- a/test/libsinsp_e2e/unix_udp_client_server.cpp +++ b/test/libsinsp_e2e/unix_udp_client_server.cpp @@ -57,8 +57,8 @@ class unix_udp_server { void run() { int sock; - struct sockaddr_un name; - struct sockaddr_un caddr; + struct sockaddr_un name = {}; + struct sockaddr_un caddr = {}; socklen_t address_length = sizeof(struct sockaddr_un); char buf[1024]; @@ -122,7 +122,7 @@ class unix_udp_client { public: void run() { int sock; - struct sockaddr_un name; + struct sockaddr_un name = {}; /* Create socket on which to send. */ sock = socket(AF_UNIX, SOCK_DGRAM, 0);