-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(libsinsp_e2e): add unix_udp_client_server_read test #2231
Changes from all commits
38e20c8
7809820
258aa7e
6498f92
600fefb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -776,7 +776,8 @@ 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); | ||
struct sockaddr_un usrsockaddr_un = {}; | ||
char *path = NULL; | ||
|
||
/* Pack the tuple info: | ||
|
@@ -787,18 +788,23 @@ 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); | ||
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); | ||
} | ||
} 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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid convoluted logic with multiple variables involved. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i have the suspect it was used to avoid some verifier madness, but let's see 🤞 |
||
/* Please note exceptions in the `sun_path`: | ||
* Taken from: https://man7.org/linux/man-pages/man7/unix.7.html | ||
* | ||
|
@@ -808,14 +814,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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -233,30 +233,24 @@ inline int sock_getname(struct socket *sock, struct sockaddr *sock_address, int | |
case AF_UNIX: { | ||
struct sockaddr_un *sunaddr = (struct sockaddr_un *)sock_address; | ||
struct unix_sock *u; | ||
struct unix_address *u_addr = NULL; | ||
|
||
if(peer) { | ||
if(peer) | ||
sk = ((struct unix_sock *)sk)->peer; | ||
if(!sk) { | ||
return -ENOTCONN; | ||
} | ||
} | ||
|
||
u = (struct unix_sock *)sk; | ||
u_addr = u->addr; | ||
if(!u_addr) { | ||
if(u && u->addr) { | ||
unsigned int len = u->addr->len; | ||
if(unlikely(len > sizeof(struct sockaddr_storage))) { | ||
len = sizeof(struct sockaddr_storage); | ||
} | ||
memcpy(sunaddr, u->addr->name, len); | ||
} else { | ||
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 = u_addr->len; | ||
if(unlikely(len > sizeof(struct sockaddr_storage))) { | ||
len = sizeof(struct sockaddr_storage); | ||
} | ||
memcpy(sunaddr, u_addr->name, len); | ||
} | ||
break; | ||
} | ||
|
@@ -861,6 +855,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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid de-referencing a possibly null ptr. |
||
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 | ||
|
@@ -999,8 +998,8 @@ uint16_t fd_to_socktuple(int fd, | |
struct sockaddr_in *usrsockaddr_in; | ||
struct sockaddr_in6 *usrsockaddr_in6; | ||
uint16_t size; | ||
struct sockaddr_storage sock_address; | ||
struct sockaddr_storage peer_address; | ||
struct sockaddr_storage sock_address = {}; | ||
struct sockaddr_storage peer_address = {}; | ||
struct socket *sock; | ||
char *dest; | ||
struct unix_sock *us; | ||
|
@@ -1173,7 +1172,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; | ||
} | ||
|
||
|
@@ -1182,7 +1182,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[0] == '\0' && us_name[1] == '\0')) && usrsockaddr != NULL) { | ||
usrsockaddr_un = (struct sockaddr_un *)usrsockaddr; | ||
|
||
/* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A rename.