Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
 

(시리즈 글이 3개 있습니다.)
Linux: 124. eBPF - __sk_buff / sk_buff 구조체
; https://www.sysnet.pe.kr/2/0/14019

Linux: 130. eBPF - bpf_skb_load_bytes를 이용한 __sk_buff.data 영역의 IP/TCP 헤더 해석
; https://www.sysnet.pe.kr/2/0/14038

Linux: 131. eBPF - bpf_skb_load_bytes를 이용한 __sk_buff.data 영역의 TCP payload 접근
; https://www.sysnet.pe.kr/2/0/14039




eBPF - bpf_skb_load_bytes를 이용한 __sk_buff.data 영역의 TCP payload 접근

지난 글에서,

eBPF - bpf_skb_load_bytes를 이용한 __sk_buff.data 영역의 IP/TCP 헤더 해석
; https://www.sysnet.pe.kr/2/0/14038

__sk_buff.data의 IP/TCP 헤더를 접근해 봤는데요, 이번엔 TCP Header를 넘어 사용자가 Socket에서 send/receive한 데이터를 접근해 보겠습니다. 이를 위해 기존의 print_sk_buff 소스 코드에 약간의 코드를 추가해 볼 수 있습니다.

// TCP header, TCP header size, TCP checksum mechanism, TCP header structure, options, and format
// https://www.noction.com/blog/tcp-header
//
// L7 Tracing with eBPF: HTTP and Beyond via Socket Filters and Syscall Tracepoints
// ; https://eunomia.dev/en/tutorials/23-http/

static void print_sk_buff(char* title, struct __sk_buff *skb) {
    struct iphdr iph;
    long result = bpf_skb_load_bytes_relative(skb, 0, &iph, sizeof(struct iphdr), BPF_HDR_START_NET);
    if (result != 0) {
        bpf_printk("[%s]: unexpected-packet = %d", title, result);
        return;
    }

    if (iph.protocol != IPPROTO_TCP) {
        bpf_printk("[%s]: !tcp_packet(protocol = %d)", title, iph.protocol);
        return;
    }

    __u8 ip_header_length = iph.ihl * 4;

    struct tcphdr tcph;
    result = bpf_skb_load_bytes_relative(skb, ip_header_length, &tcph, sizeof(struct tcphdr), BPF_HDR_START_NET);
    __u8 tcp_header_length = tcph.doff * 4;

    __u32 ip_tcp_header_legnth = ip_header_length + tcp_header_length;

    __u32 total_packet_length = __bpf_ntohs(iph.tot_len);
    __u32 tcp_payload_length = total_packet_length - ip_tcp_header_legnth;

    bpf_printk("[%s]: len(IPHeader) = %d, len(TCPHeader) = %d, len(TCPPayload) = %d", title, ip_header_length, tcp_header_length, tcp_payload_length);

    if (tcp_payload_length >= 4) {
        __u8 packet_data[4];
        result = bpf_skb_load_bytes_relative(skb, ip_tcp_header_legnth, &packet_data, 4, BPF_HDR_START_NET); // (EFAULT 14 Bad address)
        bpf_printk("[%s]: pakcet: result = %d, sk_buff = %p", title, result, skb);
        bpf_printk("First-4bytes: %c, %c, %c, %c", packet_data[0], packet_data[1], packet_data[2], packet_data[3]);
    }
}

SEC("cgroup_skb/ingress")
int test_ingress_packets(struct __sk_buff *skb) {
    print_sk_buff("ingress", skb);
    return 1;
}

SEC("cgroup_skb/egress")
int test_egress_packets(struct __sk_buff *skb) {
    print_sk_buff("egress", skb);
    return 1;
}

그런데 막상 실행해 보면 마지막 bpf_skb_load_bytes_relative에서 -14, 즉 "EFAULT(Bad address)" 오류가 발생하는 것을 볼 수 있습니다. 재미있는 것은, skb->len의 값을 조사해 보면, IP 헤더 + TCP 헤더 + TCP payload를 모두 더한 값과 일치합니다.

bpf_printk("[%s]: len(IPHeader) = %d, len(TCPHeader) = %d, len(TCPPayload) = %d", title, ip_header_length, tcp_header_length, tcp_payload_length);
bpf_printk("[%s]: skb->len: %d", title, skb->len);

/* 출력 예
[egress]: len(IPHeader) = 20, len(TCPHeader) = 40, len(TCPPayload) = 0
[egress]: skb->len: 60

[egress]: len(IPHeader) = 20, len(TCPHeader) = 32, len(TCPPayload) = 57
[egress]: skb->len: 109

[egress]: len(IPHeader) = 20, len(TCPHeader) = 32, len(TCPPayload) = 0
[egress] skb->len: 52
*/

그렇다면 분명히 egress 문맥에서도 payload 데이터가 존재할 텐데도... 접근은 안 되는 것입니다.

다른 방법으로, 혹시나 싶어 bpf_skb_pull_data를 호출해 봤는데요,

// non-linear 대비: 필요한 부분을 head로 당겨오기
//                  (tcp_data_offset + 4) 바이트가 선형으로 보장되도록 요청
if (bpf_skb_pull_data(skb, ip_tcp_header_legnth + 4) < 0) {
    bpf_printk("failed to pull data len = %d, at = %d", want_bytes, ip_tcp_header_legnth);
    return 1;
}

아예 eBPF 프로그램 로딩 단계에서 오류가 발생해 실행조차 하지 못합니다.

load program: invalid argument: program of this type cannot use helper bpf_skb_pull_data#39 (73 line(s) omitted)

오류 메시지에서 알려 주듯이, bpf_skb_pull_data 함수의 지원 프로그램은,

  • BPF_PROG_TYPE_LWT_IN
  • BPF_PROG_TYPE_LWT_OUT
  • BPF_PROG_TYPE_LWT_SEG6LOCAL
  • BPF_PROG_TYPE_LWT_XMIT
  • BPF_PROG_TYPE_SCHED_ACT
  • BPF_PROG_TYPE_SCHED_CLS
  • BPF_PROG_TYPE_SK_SKB

유형에서만 사용할 수 있기 때문에 BPF_PROG_TYPE_CGROUP_SKB에서는 eBPF verifier가 저렇게 거부를 하는 것입니다.

다시 말해, (제가 아는 수준에서는) BPF_PROG_TYPE_CGROUP_SKB 프로그램의 경우 TCP payload를 읽어올 수 없습니다.




재미있게도, 동일한 소스 코드를 BPF_PROG_TYPE_SOCKET_FILTER 유형에서 실행하면 정상적으로 TCP payload 영역을 접근할 수 있습니다.

SEC("socket")
int socket_handler(struct __sk_buff *skb) {
    print_sk_buff("socket_handler", skb);
    return skb->len;
}

예제에서는 최초 4바이트를 출력하게 했는데요, BPF_PROG_TYPE_SOCKET_FILTER가 ingoing 패킷만 다루므로 외부로 요청한 HTTP 호출에 대해 응답에 해당하는 "H, T, T, P"가 찍히는 것을 확인할 수 있습니다.

결국, 동일하게 "struct __sk_buff *skb" 구조체를 인자로 받는 eBPF 함수라고 해도, 프로그램 문맥에 따라 1) bpf_skb_load_bytes/bpf_skb_load_bytes_relative의 반환값도 다르고, 2) payload 접근 유무도 다릅니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 11/8/2025]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
14039정성태11/8/202543Linux: 131. eBPF - bpf_skb_load_bytes를 이용한 __sk_buff.data 영역의 TCP payload 접근
14038정성태11/6/2025854Linux: 130. eBPF - bpf_skb_load_bytes를 이용한 __sk_buff.data 영역의 IP/TCP 헤더 해석
14037정성태11/3/2025674Linux: 129. eBPF (bpf2go) - BPF_PROG_TYPE_CGROUP_SKB 예제 - "cgroup_skb/egress", "cgroup_skb/egress"
14036정성태10/25/20251666디버깅 기술: 224. Visual Studio - 디버깅 시 다른 함수의 소스 코드를 보여주는 사례 (Enable COMDAT Folding 옵션)파일 다운로드1
14035정성태10/24/20251657C/C++: 189. Visual C++ - 디버그 코드에서 빌드 옵션 조정으로 최적화 코드로의 전환파일 다운로드1
14034정성태10/22/20251631닷넷: 2375. C# - dynamic 사용 시 internal 멤버에 대한 RuntimeBinderException 예외가 발생하는 문제파일 다운로드1
14033정성태10/22/20252086닷넷: 2374. C# - dynamic과 "Explicit Interface Implementation"의 문제파일 다운로드1
14032정성태10/21/20251732닷넷: 2373. C# - dynamic 예약어 사용 시 런타임에 "Microsoft.CSharp.RuntimeBinder.RuntimeBinderException" 예외가 발생하는 경우파일 다운로드1
14031정성태10/20/20251665Linux: 128. "USER ..." 설정이 된 Docker 컨테이너의 호스트 측 볼륨 권한 (2)
14030정성태10/20/20251934Linux: 127. "USER ..." 설정이 된 Docker 컨테이너의 호스트 측 볼륨 권한
14029정성태10/17/20252713닷넷: 2372. C# - ssh-ed25519 유형의 Public Key 파일 해석파일 다운로드1
14028정성태10/17/20252886오류 유형: 985. openssh - ssh_host_ed25519_key 파일을 로드하지 못하는 문제
14027정성태10/15/20252724닷넷: 2371. C# - CRC64 (System.IO.Hashing의 약식 버전)파일 다운로드1
14026정성태10/15/20252972닷넷: 2370. 닷넷 지원 정보의 "package-provided" 의미
14025정성태10/14/20252858Linux: 126. eBPF (bpf2go) - tcp_sendmsg 예제
14024정성태10/14/20253452오류 유형: 984. Whisper.net - System.Exception: 'Cannot dispose while processing, please use DisposeAsync instead.'
14023정성태10/12/20253550닷넷: 2369. C# / Whisper 모델 - 동영상의 음성을 인식해 자동으로 SRT 자막 파일을 생성 [1]파일 다운로드1
14022정성태10/10/20253896닷넷: 2368. C# / NAudio - (AI 학습을 위해) 무음 구간을 반영한 오디오 파일 분할파일 다운로드1
14021정성태10/6/20254344닷넷: 2367. C# - Youtube 동영상 다운로드 (YoutubeExplode 패키지) [1]파일 다운로드1
14020정성태10/2/20253533Linux: 125. eBPF - __attribute__((preserve_access_index)) 활용 사례
14019정성태10/1/20253869Linux: 124. eBPF - __sk_buff / sk_buff 구조체
14018정성태9/30/20252903닷넷: 2366. C# - UIAutomationClient를 이용해 시스템 트레이의 아이콘을 열거하는 방법파일 다운로드1
14017정성태9/29/20253398Linux: 123. eBPF (bpf2go) - BPF_PROG_TYPE_SOCKET_FILTER 예제 - SEC("socket")
14016정성태9/28/20253936Linux: 122. eBPF - __attribute__((preserve_access_index)) 사용법
14015정성태9/22/20253032닷넷: 2365. C# - FFMpegCore를 이용한 MP4 동영상으로부터 MP3 음원 추출 예제파일 다운로드1
[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...