Skip to content

Commit

Permalink
fix(driver): round of small fixes and improvements around unix socket…
Browse files Browse the repository at this point in the history
… paths handling in drivers.

Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Jan 14, 2025
1 parent 38e20c8 commit 78aa54d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
14 changes: 10 additions & 4 deletions driver/bpf/filler_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
}
Expand Down
18 changes: 7 additions & 11 deletions driver/modern_bpf/helpers/store/auxmap_store_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
*
Expand All @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion driver/ppm_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions test/libsinsp_e2e/unix_udp_client_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 78aa54d

Please sign in to comment.