@@ -1404,101 +1404,89 @@ FILLER(sys_setrlimit_x, true) {
14041404}
14051405
14061406FILLER (sys_connect_e , true) {
1407- struct sockaddr * usrsockaddr ;
1408- unsigned long val ;
1409- long size = 0 ;
1410- long retval ;
1411- int err ;
1412- int res ;
1413- int fd ;
1414-
1415- fd = bpf_syscall_get_argument (data , 0 );
1416- res = bpf_push_s64_to_ring (data , fd );
1407+ /* Parameter 1: fd (type: PT_FD) */
1408+ int64_t fd = (int64_t )(int32_t )bpf_syscall_get_argument (data , 0 );
1409+ int res = bpf_push_s64_to_ring (data , fd );
14171410 CHECK_RES (res );
14181411
1419- if (fd >= 0 ) {
1420- usrsockaddr = (struct sockaddr * )bpf_syscall_get_argument (data , 1 );
1421- val = bpf_syscall_get_argument (data , 2 );
1412+ /* Get the sockaddr pointer and its length. */
1413+ struct sockaddr __user * usrsockaddr =
1414+ (struct sockaddr __user * )bpf_syscall_get_argument (data , 1 );
1415+ unsigned long usrsockaddr_len = bpf_syscall_get_argument (data , 2 );
14221416
1423- if (usrsockaddr && val != 0 ) {
1424- /*
1425- * Copy the address
1426- */
1427- err = bpf_addr_to_kernel (usrsockaddr , val , (struct sockaddr * )data -> tmp_scratch );
1428- if (err >= 0 ) {
1429- /*
1430- * Convert the fd into socket endpoint information
1431- */
1432- size = bpf_pack_addr (data , (struct sockaddr * )data -> tmp_scratch , val );
1433- }
1417+ long addr_size = 0 ;
1418+ if (usrsockaddr != NULL && usrsockaddr_len != 0 ) {
1419+ struct sockaddr * ksockaddr = (struct sockaddr * )data -> tmp_scratch ;
1420+ /* Copy the address into kernel memory. */
1421+ res = bpf_addr_to_kernel (usrsockaddr , usrsockaddr_len , ksockaddr );
1422+ if (likely (res >= 0 )) {
1423+ /* Convert the fd into socket endpoint information. */
1424+ addr_size = bpf_pack_addr (data , ksockaddr , usrsockaddr_len );
14341425 }
14351426 }
14361427
1437- /*
1438- * Copy the endpoint info into the ring
1439- */
1428+ /* Parameter 2: addr (type: PT_SOCKADDR) */
14401429 data -> curarg_already_on_frame = true;
1441- res = bpf_val_to_ring_len (data , 0 , size );
1442-
1443- return res ;
1430+ return bpf_val_to_ring_len (data , 0 , addr_size );
14441431}
14451432
14461433FILLER (sys_connect_x , true) {
1447- struct sockaddr * usrsockaddr ;
1448- unsigned long val ;
1449- long size = 0 ;
1450- long retval ;
1451- int err ;
1452- int res ;
1453- int fd ;
1454-
1455- /*
1456- * Push the result
1457- */
1458- retval = bpf_syscall_get_retval (data -> ctx );
1459- res = bpf_push_s64_to_ring (data , retval );
1434+ /* Parameter 1: res (type: PT_ERRNO) */
1435+ long retval = bpf_syscall_get_retval (data -> ctx );
1436+ int res = bpf_push_s64_to_ring (data , retval );
14601437 CHECK_RES (res );
14611438
1462- /*
1463- * Retrieve the fd and push it to the ring.
1464- * Note that, even if we are in the exit callback, the arguments are still
1465- * in the stack, and therefore we can consume them.
1466- */
1467- fd = bpf_syscall_get_argument (data , 0 );
1468- if (fd >= 0 ) {
1469- usrsockaddr = (struct sockaddr * )bpf_syscall_get_argument (data , 1 );
1470- val = bpf_syscall_get_argument (data , 2 );
1439+ int64_t fd = (int64_t )(int32_t )bpf_syscall_get_argument (data , 0 );
14711440
1472- if (usrsockaddr && val != 0 ) {
1473- /*
1474- * Copy the address
1475- */
1476- err = bpf_addr_to_kernel (usrsockaddr , val , (struct sockaddr * )data -> tmp_scratch );
1477- if (err >= 0 ) {
1478- /*
1479- * Convert the fd into socket endpoint information
1480- */
1481- size = bpf_fd_to_socktuple (data ,
1482- fd ,
1483- (struct sockaddr * )data -> tmp_scratch ,
1484- val ,
1485- true,
1486- false,
1487- data -> tmp_scratch + sizeof (struct sockaddr_storage ));
1488- }
1441+ if (retval != 0 && retval != - EINPROGRESS ) {
1442+ /* Parameter 2: tuple (type: PT_SOCKTUPLE) */
1443+ res = bpf_push_empty_param (data );
1444+ CHECK_RES (res );
1445+
1446+ /* Parameter 3: fd (type: PT_FD) */
1447+ return bpf_push_s64_to_ring (data , fd );
1448+ }
1449+
1450+ /* Get the sockaddr pointer and length. */
1451+ struct sockaddr __user * usrsockaddr =
1452+ (struct sockaddr __user * )bpf_syscall_get_argument (data , 1 );
1453+ unsigned long usrsockaddr_len = bpf_syscall_get_argument (data , 2 );
1454+
1455+ /* Evaluate socktuple, leveraging the user-provided sockaddr if possible */
1456+ struct sockaddr * ksockaddr = (struct sockaddr * )data -> tmp_scratch ;
1457+ bool use_sockaddr_user_data = false;
1458+ bool push_socktuple = true;
1459+ if (usrsockaddr != NULL && usrsockaddr_len != 0 ) {
1460+ /* Copy the address into kernel memory. */
1461+ res = bpf_addr_to_kernel (usrsockaddr , usrsockaddr_len , ksockaddr );
1462+ if (likely (res >= 0 )) {
1463+ /* Convert the fd into socket endpoint information. */
1464+ use_sockaddr_user_data = true;
1465+ } else {
1466+ /* Do not send any socket endpoint information. */
1467+ push_socktuple = false;
14891468 }
14901469 }
14911470
1492- /*
1493- * Copy the endpoint info into the ring
1494- */
1471+ uint32_t tuple_size = 0 ;
1472+ if (push_socktuple ) {
1473+ /* Convert the fd into socket endpoint information */
1474+ tuple_size = bpf_fd_to_socktuple (data ,
1475+ fd ,
1476+ ksockaddr ,
1477+ usrsockaddr_len ,
1478+ use_sockaddr_user_data ,
1479+ false,
1480+ data -> tmp_scratch + sizeof (struct sockaddr_storage ));
1481+ }
1482+
1483+ /* Parameter 2: tuple (type: PT_SOCKTUPLE) */
14951484 data -> curarg_already_on_frame = true;
1496- res = bpf_val_to_ring_len (data , 0 , size );
1485+ res = bpf_val_to_ring_len (data , 0 , tuple_size );
14971486 CHECK_RES (res );
14981487
1499- /* Parameter 3: fd (type: PT_FD)*/
1500- res = bpf_push_s64_to_ring (data , fd );
1501- return res ;
1488+ /* Parameter 3: fd (type: PT_FD) */
1489+ return bpf_push_s64_to_ring (data , fd );
15021490}
15031491
15041492FILLER (sys_socketpair_x , true) {
@@ -1905,7 +1893,6 @@ FILLER(sys_sendto_e, true) {
19051893 /* Get the address len */
19061894 unsigned long usrsockaddr_len = bpf_syscall_get_argument (data , 5 );
19071895
1908- /* Evaluate socktuple, leveraging the user-provided sockaddr if possible */
19091896 struct sockaddr * ksockaddr = (struct sockaddr * )data -> tmp_scratch ;
19101897 bool use_sockaddr_user_data = false;
19111898 bool push_socktuple = true;
0 commit comments