Skip to content

Commit

Permalink
fix(driver): properly add back fallback to user data when peer socket…
Browse files Browse the repository at this point in the history
… data is missing.

Signed-off-by: Federico Di Pierro <[email protected]>

Co-authored-by: Andrea Terzolo <[email protected]>
  • Loading branch information
FedeDP and Andreagit97 committed Jan 14, 2025
1 parent 78aa54d commit fa11f82
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
9 changes: 7 additions & 2 deletions driver/bpf/filler_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,7 @@ static __always_inline long bpf_fd_to_socktuple(struct filler_data *data,
*/
struct unix_sock *us = (struct unix_sock *)sk;
struct sock *speer = _READ(us->peer);
struct sockaddr_un *usrsockaddr_un;
char *us_name = NULL;

data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF] = socket_family_to_scap(family);
Expand All @@ -901,8 +902,12 @@ static __always_inline long bpf_fd_to_socktuple(struct filler_data *data,
} else {
memcpy(&data->buf[(data->state->tail_ctx.curoff + 1) & SCRATCH_SIZE_HALF], &speer, 8);
memcpy(&data->buf[(data->state->tail_ctx.curoff + 1 + 8) & SCRATCH_SIZE_HALF], &us, 8);
bpf_getsockname(sock, peer_address, 1);
us_name = ((struct sockaddr_un *)peer_address)->sun_path;
if(bpf_getsockname(sock, peer_address, 1)) {
us_name = ((struct sockaddr_un *)peer_address)->sun_path;
} else if(usrsockaddr != NULL) {
usrsockaddr_un = (struct sockaddr_un *)usrsockaddr;
us_name = usrsockaddr_un->sun_path;
}
}

int res = unix_socket_path(
Expand Down
10 changes: 9 additions & 1 deletion driver/modern_bpf/helpers/store/auxmap_store_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,15 @@ static __always_inline void auxmap__store_socktuple_param(struct auxiliary_map *
if(direction == OUTBOUND) {
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_peer, addr, name[0].sun_path); // empty
struct sockaddr_un usrsockaddr_un = {};
if(socket_peer == NULL && usrsockaddr != NULL) {
bpf_probe_read_user(&usrsockaddr_un,
bpf_core_type_size(struct sockaddr_un),
(void *)usrsockaddr);
path = usrsockaddr_un.sun_path;
} else {
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_peer);
Expand Down
2 changes: 1 addition & 1 deletion driver/ppm_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ uint16_t fd_to_socktuple(int fd,
// Note that we check the second byte of `us_name`, see `sock_getname` for more details.
// Some times `usrsockaddr` is provided as a NULL pointer, checking `use_userdata` should be
// enough but just to be sure we check also `usrsockaddr != NULL`
if(us_name && us_name[1] == '\0' && use_userdata && usrsockaddr != NULL) {
if((!us_name || us_name[1] == '\0') && usrsockaddr != NULL) {
usrsockaddr_un = (struct sockaddr_un *)usrsockaddr;

/*
Expand Down

0 comments on commit fa11f82

Please sign in to comment.