LinuxÄÚºËAF_PACKETÔÉúÌ×½Ó×ÖÎó²î£¨CVE-2020-14386£©ÆÊÎö
Ðû²¼Ê±¼ä 2020-09-22Îó²îÅä¾°
¿ËÈÕ£¬OpenwallÉçÇøÉϹûÕæÁËÒ»¸öLinuxÄÚºËAF_PACKETÔÉúÌ×½Ó×ÖÄÚ´æÆÆËðÎó²î¡£Æ¾Ö¤Ï¸½ÚÐÎò£¬¸ÃÎó²î·ºÆðÔÚnet/packet/af_packet.cÖУ¬ÓÉÕûÊýÒç³öµ¼ÖÂÔ½½çд£¬¿ÉÒÔͨ¹ýËü¾ÙÐÐȨÏÞÌáÉý¡£¸ÃÎó²îΣº¦ÆÀ¼¶Îª¸ß£¬±àºÅΪCVE-2020-14386¡£
ÊÜÓ°Ïì²úÆ·»ººÍ½â²½·¥
1¡¢ÊÜÓ°Ïì²úÆ·
¸ÃÎó²îÓ°ÏìLinux¿¯Ðаæ¸ßÓÚ4.6µÄÄں˰汾£¬°üÀ¨£º
Ubuntu Bionic (18.04) and newer
Debian 9
Debian 10
CentOS 8/RHEL 8
2¡¢»º½â²½·¥
£¨1£©ÐÞ²¹ÏµÍ³
ÉÏÓÎÄں˲¹¶¡ÈçÏ£º
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=acf69c946233259ab4d64f8869d4037a198c7f06
£¨2£©¹Ø±ÕCAP_NET_RAW¹¦Ð§
Õë¶ÔRHEL8£¬Ïêϸ¹Ø±Õ°ì·¨ÈçÏ£º
# sysctl -p/etc/sysctl.d/userns.conf
£¨3£©Õë¶ÔһЩÊÜÓ°ÏìµÄÈÝÆ÷²úÆ·£¬Í¬Ñù½ÓÄɹرÕCAP_NET_RAW¹¦Ð§¾ÙÐлº½â
Kubernetes PodÇå¾²Õ½ÂÔ£ºÉèÖÃPodÇå¾²Õ½ÂÔÒÔɾ³ýÔËÐÐÈÝÆ÷ÖеÄCAP_NET_RAW¹¦Ð§£¬²Î¿¼Á´½Ó£ºhttps://cloud.google.com/kubernetes-engine/docs/security-bulletins¡£
Ïà¹Ø¿´·¨
1¡¢AF_PACKETÌ×½Ó×Ö
ÍøÂçÐÒéÕ»ÖУ¬ÔʼÌ×½Ó×ÖÊÇÒ»¸öÌØÊâµÄÌ×½Ó×ÖÀàÐÍ£¬´ÓʵÏÖÉÏ¿ÉÒÔ·ÖΪÁ½À࣬һÀàΪÁ´Â·²ãÔʼÌ×½Ó×Ö£»ÁíÒ»ÀàΪÍøÂç²ãÔʼÌ×½Ó×Ö¡£Á´Â·²ãÔʼÌ×½Ó×Ö¿ÉÖ±½ÓÓÃÓÚÎüÊպͷ¢ËÍÁ´Â·²ãµÄMACÖ¡£¬ÔÚ·¢ËÍʱÐèҪŲÓÃÕß×ÔÐнṹºÍ·â×°MACÊײ¿¡£Á´Â·²ãÔʼÌ×½Ó×ÖŲÓÃsocket()º¯Êý½¨Éè¡£µÚÒ»¸ö²ÎÊýÖ¸¶¨µØµã´ØÀàÐÍΪAF_PACKET£¬µÚ¶þ¸ö²ÎÊýÌ×½Ó×ÖÀàÐÍΪSOCK_RAW»òSOCK_DGRAM£¬µ±ÀàÐÍÖ¸¶¨ÎªSOCK_RAWʱ£¬Ì×½Ó×ÖÎüÊպͷ¢Ë͵ÄÊý¾Ý¶¼ÊÇ´ÓMACÊײ¿×îÏȵġ£ÔÚ·¢ËÍʱÐèÒªÓÉŲÓÃÕß´ÓMACÊײ¿×îÏȽṹºÍ·â×°±¨ÎÄÊý¾Ý¡£
2¡¢PACKET_MMAP
½öÒÀÀµAF_PACKET¹ýÂËÊý¾Ý°üÊǺÜÊǵÍЧµÄ£¬ÄÚºËÓÖÌṩÁËPACKET_MMAPÖ§³Ö¡£PACKET_MMAPÔÚÄں˿ռäÖзÖÅÉÒ»¿é»·ÐÎÄں˻º³åÇø£¬Óû§¿Õ¼äͨ¹ýmmap½«¸ÃÄں˻º³åÇøÓ³Éä³öÀ´¡£ÊÕµ½µÄÊý¾Ý°ü¿½±´µ½»·ÐÎÄں˻º³åÇøÖУ¬Óû§²ã¿ÉÒÔÖ±½Ó²Ù×÷Êý¾Ý£¬Í¨¹ýÄں˿ռäºÍÓû§¿Õ¼ä¹²ÏíµÄ»º³åÇøÆðµ½ïÔÌÊý¾Ý¿½±´µÄ×÷Óã¬Ìá¸ß´¦Öóͷ£Ð§ÂÊ¡£
PACKET_MMAPʵÏÖÀú³Ì
ͨ¹ýsetsockopt()º¯ÊýÉèÖû·Ðλº³åÇø£¬option²ÎÊýÉèÖÃΪPACKET_RX_RING»òPACKET_TX_RING¡£ÎªÁËÀû±ãÄÚºËÓëÓû§²ãÖÎÀíºÍ½»»¥»·Ðλº³åÇøÖеÄÊý¾ÝÖ¡£¬Äں˽ç˵ÁËTPACKET_HEADER½á¹¹Ì壬¸Ã½á¹¹Ìå´æ´¢×ÅһЩԪÐÅÏ¢ÈçÌ×½Ó×ֵصãÐÅÏ¢¡¢Ê±¼ä´ÁÒÔ¼°»·Ðλº³åÇøÖÎÀíÐÅÏ¢µÈ¡£ÈôÊÇͨ¹ýsetsockopt()º¯ÊýÉèÖÃÁËPACKET_VNET_HDRÑ¡Ï»¹ÐèÌí¼ÓÒ»¸övirtio_net_hdr½á¹¹Ìå¡£Ò»¸öÊý¾ÝÖ¡°üÀ¨Á½¸ö²¿·Ö£¬µÚÒ»²¿·ÖΪTPACKET_HEADER£¬µÚ¶þ²¿·ÖΪData£¬²¢ÇÒÒª°ü¹ÜÒ³ÃæÁÙÆ룬ÈçÏÂͼËùʾ£º
²¶»ñµÄframe±»»®·ÖΪ¶à¸öblock£¬Ã¿¸öblockÊÇÒ»¿éÎïÀíÉÏÒ»Á¬µÄÄÚ´æÇøÓò£¬ÓÐtp_block_size/tp_frame_size¸öframe£¬blockµÄ×ÜÊýÊÇtp_block_nr¡£ÀýÈ磬tp_block_size = 4096£¬tp_frame_size = 2048£¬tp_block_nr = 4£¬tp_frame_nr = 8¡£»ñµÃµÄ»º³åÇø½á¹¹ÈçÏÂͼËùʾ£º
ÿ¸öframe±ØÐè·ÅÔÚÒ»¸öblockÖУ¬Ã¿¸öblockÉúÑÄÕûÊý¸öframe£¬Ò²¾ÍÊÇ˵һ¸öframe²»¿É¿çÔ½Á½¸öblock¡£ÔÚÓû§²ãÓ³Éä»·Ðλº³åÇø¿ÉÒÔÖ±½ÓʹÓÃmmap()º¯Êý¡£ËäÈ»»·Ðλº³åÇøÔÚÄÚºËÖÐÊÇÓɶà¸öblock×é³ÉµÄ£¬¿ÉÊÇÓ³ÉäºóËüÃÇÔÚÓû§¿Õ¼äÖÐÊÇÒ»Á¬µÄ¡£
Îó²îÆÊÎö
ÐÐ2226µ½ÐÐ2228£¬ÈôÊÇsk_typeΪSOCK_DGRAM£¬ÌåÏÖ²»ÐèÒª×ÔÐнṹMACÊײ¿£¬ÓÉÄÚºËÌî³ä£¬Ôòmacoff¼´ÊÇnetoff£¬¾ÞϸΪTPACKET_ALIGN(tp_hdr_len)+ 16 + tp_reserve¡£ÈôÊÇsk_typeΪSOCK_RAW£¬Ôò½øÈëÐÐ2230£¬ÌåÏÖÐèÒª×ÔÐнṹMACÊײ¿¡£ÐÐ2231µ½ÐÐ2233£¬Ê×ÏÈÅÌËãnetoff£¬¾ÞϸΪTPACKET_ALIGN(tp_hdrlen +(maclen < 16 ?16 : maclen)) + tp_reserve¡£ÐÐ2234µ½ÐÐ2237£¬ÈôÊÇÉèÖÃÁËPACKET_VNET_HDRÑ¡Ï»¹Ðè¼ÓÉÏÒ»¸övirtio_net_hdr½á¹¹ÌåµÄ¾Þϸ£¬È»ºóÉèÖÃdo_vnetΪÕæ¡£ÐÐ2238£¬ÅÌËãmacoff¡£
ÓÉÓÚmacoff¡¢netoffÒÔ¼°maclen±»½ç˵Ϊunsigned shortÀàÐÍ£¬×î´óֵΪ0xffff¡£¶øtp_reserve±»½ç˵Ϊunsigned intÀàÐÍ£¬×î´óֵΪ0xffffffff£¬²¢ÇÒ¾Þϸ¿ÉÒÔͨ¹ýsetsockopt()º¯Êý¾ÙÐÐÉèÖã¬ÈçÏ´úÂëËùʾ£º
Òò´Ë£¬ÔÚÅÌËãnetoffʱ£¬¿ÉÒÔͨ¹ý¿ØÖÆtp_reserveÔì³ÉÕûÊýÒç³ö£¬½ø¶øÅÌËãÍÉ»¯ÎóµÄmacoff¡£µ±Ö´Ðе½ÈçÏ´úÂëʱ£º
ÐÐ2287£¬Å²ÓÃvirtio_net_hdr_from_skb()º¯Êý´Ósk_buffÖп½±´Êý¾Ý£¬¸Ãº¯ÊýµÚ¶þ¸ö²ÎÊýΪh.raw + macoff ¨C sizeof(struct virtio_net_hdr)£¬h.rawΪtpacket_rcv_uhdrÀàÐ͵ÄÖ¸Õ룬ָÏò»·Ðλº³åÇøµÄframe£¬ÓÉÓÚmacoffÊǿɿصģ¬¿ÉÒÔÈÃmaoffСÓÚsizeof(struct virtio_net_hdr)£¬µ¼ÖÂÏòÇ°Ô½½çд£¬×î¶à¿ÉдÈësizeof(struct virtio_net_hdr)¸ö×Ö½Ú¡£Æ¾Ö¤ÌṩµÄPoC£¬µ÷ÊÔ´úÂëÈçÏÂͼËùʾ£º
rdxÖдæ·Å×ÅTPACKET_ALIGN(tp_hdrlen+(maclen < 16 ? 16 : maclen))£¬¾ÞϸΪ0x50¡£rbp+0x4e4´¦´æ·Å×Åpo->tp_reserve£¬¾ÞϸΪ0x0000ffb4¡£Ïà¼Óºó£¬ÕûÊýÉÏÒçºó£¬rdxΪ0x0004¡£µ±Ö´Ðе½Ô½½ç»á¼ûʱ£¬ÏêϸÈçÏ£º
R9´æ·Å×Åh.rawÖ¸Õ룬rdx´æ·Å×Åmacoff£¬virtio_net_hdr½á¹¹Ìå¾ÞϸΪ0xa¡£ÈçÏÂͼËùʾ£º
±¬·¢ÄÚ´æ»á¼û¹ýʧ£¬Ôì³ÉϵͳÍ߽⡣
²Î¿¼Á´½Ó£º
[1] https://blog.csdn.net/sinat_20184565/article/details/82788387
[2] https://www.openwall.com/lists/oss-security/2020/09/03/3
[3] https://elixir.bootlin.com/linux/v5.6/source/Documentation/networking/packet_mmap.txt
[4] https://sysdig.com/blog/cve-2020-14386-falco/
[5] https://bugzilla.redhat.com/show_bug.cgi?id=1875699#c9