|
205 | 205 | _TCP_ACCEPT_v6 = 403, |
206 | 206 |
|
207 | 207 | // UDP_MSG |
208 | | - _UDP_SENDMSG = 10000 |
| 208 | + _UDP_SENDMSG = 10000, |
| 209 | + _UDP_SEND_SKB = 10001 |
209 | 210 | }; |
210 | 211 |
|
211 | 212 | // forward declartaion for CWD |
@@ -2529,6 +2530,83 @@ int kretprobe__inet_csk_accept(struct pt_regs *ctx) |
2529 | 2530 | return 0; |
2530 | 2531 | } |
2531 | 2532 |
|
| 2533 | + |
| 2534 | +#define UDPHDR_LEN 8 |
| 2535 | + |
| 2536 | +SEC("kprobe/udp_send_skb") |
| 2537 | +int kprobe__udp_send_skb(struct pt_regs *ctx){ |
| 2538 | + |
| 2539 | + if (skip_syscall()) |
| 2540 | + return 0; |
| 2541 | + |
| 2542 | + if (get_kubearmor_config(_ENFORCER_BPFLSM) && drop_syscall(_DNS_PROBE)) |
| 2543 | + return 0; |
| 2544 | + |
| 2545 | + struct sk_buff *skb = (struct sk_buff *)PT_REGS_PARM1(ctx); |
| 2546 | + struct flowi4 *fl4 = (struct flowi4 *)PT_REGS_PARM2(ctx); |
| 2547 | + if (skb == NULL || fl4 == NULL) |
| 2548 | + return 0; |
| 2549 | + |
| 2550 | + struct sock *sk = NULL; |
| 2551 | + bpf_probe_read(&sk, sizeof(sk), &skb->sk); |
| 2552 | + if (sk == NULL) |
| 2553 | + return 0; |
| 2554 | + |
| 2555 | + __u16 dport = 0; |
| 2556 | + bpf_probe_read(&dport, sizeof(dport), &fl4->uli.ports.dport); |
| 2557 | + dport = ntohs(dport); |
| 2558 | + if (dport != 53) |
| 2559 | + return 0; |
| 2560 | + |
| 2561 | + __u32 skb_len = 0; |
| 2562 | + bpf_probe_read(&skb_len, sizeof(skb_len), &skb->len); |
| 2563 | + if (skb_len > 512 + UDPHDR_LEN) // MAX_DNS_SIZE + udp header |
| 2564 | + return 0; |
| 2565 | + |
| 2566 | + unsigned char *head = NULL; |
| 2567 | + __u16 trans_off = 0; |
| 2568 | + bpf_probe_read(&head, sizeof(head), &skb->head); |
| 2569 | + bpf_probe_read(&trans_off, sizeof(trans_off), &skb->transport_header); |
| 2570 | + void *data = head + trans_off + UDPHDR_LEN; |
| 2571 | + |
| 2572 | + sys_context_t context = {}; |
| 2573 | + args_t args = {}; |
| 2574 | + u64 types = 0; |
| 2575 | + init_context(&context); |
| 2576 | + context.argnum = 3; |
| 2577 | + context.retval = 0; |
| 2578 | + context.event_id = _UDP_SEND_SKB; |
| 2579 | + |
| 2580 | + if (context.retval >= 0 && drop_syscall(_DNS_PROBE)) |
| 2581 | + return 0; |
| 2582 | + |
| 2583 | + if (context.retval < 0 && !get_kubearmor_config(_ENFORCER_BPFLSM) && |
| 2584 | + get_kubearmor_config(_ALERT_THROTTLING) && |
| 2585 | + should_drop_alerts_per_container(&context, ctx, types, &args)) |
| 2586 | + return 0; |
| 2587 | + |
| 2588 | + set_buffer_offset(DNS_BUF_TYPE, sizeof(sys_context_t)); |
| 2589 | + bufs_t *bufs_p = get_buffer(DNS_BUF_TYPE); |
| 2590 | + if (bufs_p == NULL) |
| 2591 | + return 0; |
| 2592 | + save_context_to_buffer(bufs_p, (void *)&context); |
| 2593 | + |
| 2594 | + |
| 2595 | + struct sock_common conn = READ_KERN(sk->__sk_common); |
| 2596 | + struct sockaddr_in sockv4 = {}; |
| 2597 | + sockv4.sin_family = conn.skc_family; |
| 2598 | + bpf_probe_read(&sockv4.sin_addr.s_addr, sizeof(sockv4.sin_addr.s_addr), &fl4->daddr); |
| 2599 | + sockv4.sin_port = dport; |
| 2600 | + |
| 2601 | + save_to_buffer(bufs_p, DNS_BUF_TYPE, (void *)&sockv4, sizeof(struct sockaddr_in), SOCKADDR_T); |
| 2602 | + save_dns_data_to_dns_buffer(bufs_p, data, UDP_MSG); |
| 2603 | + events_perf_submit(ctx, DNS_BUF_TYPE); |
| 2604 | + return 0; |
| 2605 | +} |
| 2606 | + |
| 2607 | +// This probe is currently not attached by the system monitor. |
| 2608 | +// The decision to disable attaching this probe was taken for performance reasons. |
| 2609 | + |
2532 | 2610 | SEC("kprobe/udp_sendmsg") |
2533 | 2611 | int kprobe__udp_sendmsg(struct pt_regs *ctx) |
2534 | 2612 | { |
|
0 commit comments