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 £¬Ïêϸ¹Ø±Õ°ì·¨ÈçÏ£º

# echo"user.max_user_namespaces=0" > /etc/sysctl.d/userns.conf

# 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 £¬²¢ÇÒÒª°ü¹ÜÒ³ÃæÁÙÆë £¬ÈçÏÂͼËùʾ£º

ÏÖÔÚTPACKET_HEADER±£´æÈý¸ö°æ±¾ £¬Ã¿¸ö°æ±¾³¤¶ÈÂÔÓвî±ð¡£¹ØÓÚv1ºÍv2 £¬ÊÕ·¢»·Ðλº³åÇøÓÃtpacket_req½á¹¹ÌåÖÎÀí £¬¸Ã½á¹¹Ìå°üÀ¨ËĸöÊý¾ÝÓò£º»®·ÖΪÄÚ´æ¿éµÄ¾ÞϸºÍÊýÄ¿¡¢Ã¿¸öÊý¾ÝÖ¡µÄ¾ÞϸºÍÊý¾ÝÖ¡×ÜÊý¡£ÈçÏÂͼËùʾ£º



²¶»ñµÄ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×é³ÉµÄ £¬¿ÉÊÇÓ³ÉäºóËüÃÇÔÚÓû§¿Õ¼äÖÐÊÇÒ»Á¬µÄ¡£


Îó²îÆÊÎö


¸ÃÎó²îÏêϸ·ºÆðÔÚtpacket_rcv()º¯ÊýÖÐ £¬¸Ãº¯ÊýÊÇ»ùÓÚPACKET_MMAPµÄÊý¾Ý°üÎüÊÕº¯Êý¡£Ïêϸ¹¦Ð§ÊµÏÖÈçÏ´úÂëËùʾ£º



ÐÐ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