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

비밀번호

댓글 작성자
 




... 121  122  123  124  [125]  126  127  128  129  130  131  132  133  134  135  ...
NoWriterDateCnt.TitleFile(s)
10916정성태3/9/201634376.NET Framework: 557. 머신 바이트 배열로부터 역어셈블해주는 라이브러리 - Udis86 Assembler파일 다운로드2
10915정성태3/9/201628249오류 유형: 323. FatalExecutionEngineError was detected
10914정성태3/8/201631090오류 유형: 322. 정적 라이브러리 참조 시 "LNK2019 unresolved external symbol '...' referenced in function" 오류 발생파일 다운로드1
10913정성태3/7/201630595.NET Framework: 556. C#으로 다루는 MBR(Master Boot Record) [9]파일 다운로드1
10912정성태3/2/201626002.NET Framework: 555. List<T>의 Resize 메서드 구현 [2]파일 다운로드1
10911정성태2/29/201630387Math: 15. 그래프 그리기로 알아보는 뉴턴-랩슨(Newton-Raphson's method)법과 제곱근 구하기 - C#파일 다운로드1
10910정성태2/29/201632815Math: 14. HTML에서 수학 관련 기호/수식을 표현하기 위한 방법 - MathJax.js - 두 번째 이야기 [5]
10909정성태2/25/201630952기타: 56. ETW provider 목록 [3]
10908정성태2/25/201626907기타: 55. ETW man 파일 목록
10907정성태2/24/201626430.NET Framework: 554. 인터프리터 - 재귀적 하향 구문 분석 C# 예제파일 다운로드1
10906정성태2/24/201623058.NET Framework: 553. C# 관리 코드에서 IMetaDataDispenserEx, IMetaDataImport 관련 인터페이스를 얻는 방법파일 다운로드1
10905정성태2/24/201626950오류 유형: 321. Hyper-V The operation failed with error code '32791'.
10904정성태2/23/201625621.NET Framework: 552. 인터프리터 - 역폴란드 표기법을 이용한 식의 분석 - C# 예제파일 다운로드1
10903정성태2/22/201626389.NET Framework: 551. 인터프리터 어휘 분석 프로그램 - C# 예제파일 다운로드1
10902정성태2/22/201626150.NET Framework: 550. GetFunctionPointer 호출 시 System.InvalidProgramException 예외 발생
10901정성태2/20/201630126.NET Framework: 549. ContextBoundObject 상속 클래스와 System.Reflection.ReflectionTypeLoadException 예외 [4]파일 다운로드1
10900정성태2/19/201628687.NET Framework: 548. Linq는 결국 메서드 호출! [3]파일 다운로드1
10899정성태2/17/201629313개발 환경 구성: 282. kernel32.dll, kernel32legacy.dll, api-ms-win-core-sysinfo-l1-2-0.dll [1]
10898정성태2/17/201628173.NET Framework: 547. PerformanceCounter의 InstanceName 지정 시 주의 사항파일 다운로드1
10897정성태2/17/201627343디버깅 기술: 76. windbg 분석 사례 - 닷넷 프로파일러의 GC 콜백 부하
10896정성태2/17/201627494오류 유형: 320. FATAL: 28000: no pg_hba.conf entry for host "fe80::1970:8120:695:a41e%12"
10895정성태2/17/201626856.NET Framework: 546. System.AppDomain으로부터 .NET Profiler의 AppDomainID 구하는 방법 [1]
10894정성태2/17/201626065오류 유형: 319. Visual Studio에서 찾기는 성공하지만 해당 소스 코드 정보가 보이지 않는 경우
10893정성태2/16/201625903.NET Framework: 545. 닷넷 - 특정 클래스가 로드되었는지 여부를 알 수 있을까? - 두 번째 이야기
10892정성태2/16/201625581오류 유형: 318. 탐색기에서 폴더 생성/삭제 시 몇 초 동안 멈추는 현상
10891정성태2/16/201629806VC++: 95. 내 CPU가 MPX/SGX를 지원할까요? [1]
... 121  122  123  124  [125]  126  127  128  129  130  131  132  133  134  135  ...