LinuxÄÚºËAF_VSOCKÌ×½Ó×ÖÌõ¼þ¾ºÕùÎó²î£¨CVE-2021-26708£©ÆÊÎö
Ðû²¼Ê±¼ä 2021-03-10Îó²îÅä¾°
½üÆÚ£¬ÍâÑóÇå¾²Ñо¿Ö°Ô±ÔÚoss-securityÉÏÅû¶ÁËÒ»¸öAF_VSOCKÌ×½Ó×ÖÌõ¼þ¾ºÕù¸ßΣÎó²îCVE-2021-26708£¨CNVD-2021-10822¡¢CNNVD-202102-529£©¡£Æ¾Ö¤Åû¶ϸ½Ú£¬¸ÃÎó²îÊÇÓÉÓÚ¹ýʧ¼ÓËøµ¼Ö£¬¿ÉÒÔÔÚµÍȨÏÞÏ´¥·¢²¢×Ô¶¯¼ÓÔØÒ×Êܹ¥»÷Çý¶¯Ä£¿é½¨ÉèAF_VSOCKÌ×½Ó×Ö£¬½ø¶øµ¼ÖÂÍâµØȨÏÞÌáÉý¡£¸ÃÎó²î²¹¶¡ÒѾºÏ²¢µ½LinuxÄÚºËÖ÷ÏßÖС£
VSOCKÏÈÈݺͼܹ¹
VSOCKÏÈÈÝ
VMÌ×½Ó×Ö×îÔçÊÇÓÉVmware¿ª·¢²¢Ìá½»µ½LinuxÄÚºËÖ÷ÏßÖС£VMÌ×½Ó×ÖÔÊÐíÐéÄâ»úÓëÐéÄâ»úÖÎÀí³ÌÐòÖ®¼ä¾ÙÐÐͨѶ¡£ÐéÄâ»úºÍÖ÷»úÉϵÄÓû§¼¶Ó¦ÓóÌÐò¶¼¿ÉÒÔʹÓÃVM Ì×½Ó×ÖAPI£¬´Ó¶øÔö½øguestÐéÄâ»úÓëÆähostÖ®¼äµÄ¿ìËÙÓÐÓÃͨѶ¡£¸Ã»úÖÆÌṩÁËÒ»¸övsockÌ×½Ó×ֵصãϵÁм°Æävmci´«Ê䣬ּÔÚÓë½Ó¿Ú¼¶±ðµÄUDPºÍTCP¼æÈÝ¡£VSOCK»úÖÆËæ¼´»ñµÃLinuxÉçÇøµÄÏìÓ¦£¬RedhatÔÚVSOCKÖÐΪvsockÌí¼ÓÁËvirtio´«Ê䣬QEMU/KVMÐéÄâ»úÖÎÀíÌṩ֧³Ö£¬MicrosoftÌí¼ÓÁËHyperV´«Êä¡£
VSOCK¼Ü¹¹
VMÌ×½Ó×ÖÓëÆäËûÌ×½Ó×ÖÀàÐÍÀàËÆ£¬ÀýÈçBerkeley UNIXÌ×½Ó×Ö½Ó¿Ú¡£VMÌ×½Ó×ÖÄ£¿éÖ§³ÖÃæÏòÅþÁ¬µÄÁ÷Ì×½Ó×Ö£¨ÀýÈçTCP£©ºÍÎÞÅþÁ¬Êý¾Ý±¨Ì×½Ó×Ö£¨ÀýÈçUDP£©¡£VMÌ×½Ó×ÖÐÒéϵÁнç˵Ϊ¡°AF_VSOCK¡±£¬²¢ÇÒÌ×½Ó×Ö²Ù×÷·ÖΪSOCK_DGRAMºÍSOCK_STREAM¡£ÈçÏÂͼËùʾ£º
VSOCKÖ§³Ösocket API¡£AF_SOCKµØµã´Ø°üÀ¨Á½¸öÒªËØ£ºCIDºÍport¡£CIDΪContextIdentifier£¬ÉÏÏÂÎıêʶ·û£»portΪ¶Ë¿Ú¡£TCP/IPÓ¦ÓóÌÐòÏÕЩ²»ÐèÒª¸ü¸Ä¾Í¿ÉÒÔÊÊÅ䣬ÿһ¸öµØµãÌåÏÖΪ
Transportƾ֤´«ÊäÆ«Ïò·ÖΪÁ½ÖÖ£¨ÒÔSOCK_STREAMÀàÐÍΪÀý£©£¬Ò»ÖÖΪG2H transport£¬ÌåÏÖguestµ½hostµÄ´«ÊäÀàÐÍ£¬ÔËÐÐÔÚguestÖС£ÁíÒ»ÖÖΪH2G transport£¬ÌåÏÖhostµ½guestµÄ´«ÊäÀàÐÍ¡£ÒÔQEMU/KVM´«ÊäΪÀý£¬ÈçÏÂͼËùʾ£º
¸Ã´«ÊäÌṩÌ×½Ó×Ö²ã½Ó¿ÚµÄÇý¶¯·ÖΪÁ½¸ö²¿·Ö£ºÒ»¸öÊÇÔËÐÐÔÚguestÖеÄvirtio-transport£¬ÓÃÓÚÅäºÏguest¾ÙÐÐÊý¾Ý´«Ê䣻ÁíÒ»¸öÊÇÔËÐÐÔÚhostÖеÄvhost-transport£¬ÓÃÓÚÅäºÏhost¾ÙÐÐÊý¾Ý´«Êä¡£VSOCK transport»¹Ìṩ¶à´«ÊäͨµÀģʽ£¬¸Ã¹¦Ð§ÊÇΪÁËÖ§³ÖǶÌ×ÐéÄâ»úÖеÄVSOCK¹¦Ð§¡£ÈçÏÂͼËùʾ£º
Ö§³ÖL1ÐéÄâ»úͬʱ¼ÓÔØH2GºÍG2HÁ½¸ö´«ÊäͨµÀ£¬´ËʱL1ÐéÄâ»ú¼´ÊÇhostÒ²ÊÇguest£¬Í¨¹ýH2G´«ÊäͨµÀºÍL2ǶÌ×ÐéÄâ»úͨѶ£¬Í¨¹ýG2H´«ÊäͨµÀºÍL0 hostͨѶ¡£VSOCK transport»¹Ö§³ÖÍâµØ»·»Ø´«ÊäͨµÀģʽ£¬²»ÐèÒªÓÐÐéÄâ»ú¡£ÈçÏÂͼËùʾ£º
¸ÃģʽÓÃÓÚ²âÊԺ͵÷ÊÔ£¬ÓÉvsock-loopbackÌṩ֧³Ö£¬²¢¶ÔµØµã´ØÖеÄCID¾ÙÐÐÁË·ÖÀ࣬°üÀ¨Á½ÖÖÀàÐÍ£ºÒ»ÖÖÊÇVMADDR_CID_LOCAL£¬ÌåÏÖÍâµØ»·»Ø£»Ò»ÖÖΪVMADDR_CID_HOST£¬ÌåÏÖH2G´«ÊäͨµÀ¼ÓÔØ£¬G2H´«ÊäͨµÀδ¼ÓÔØ¡£
Îó²îÆÊÎöÓë´¥·¢Àú³Ì
Îó²îÆÊÎö
¸ÃÎó²î´¥·¢Ôµ¹ÊÔÓÉÊǹýʧ¼ÓËøµ¼ÖÂÌõ¼þ¾ºÕù£¬Æ¾Ö¤²¹¶¡¿ÉÖª£¬±£´æ¶à´¦¹ýʧ¼ÓËø£¬ÕâÀïÒÔvsock_stream_setsockopt()º¯Êý²¹¶¡ÎªÀý£¬ÈçÏÂͼËùʾ£º
²¹¶¡ºÜ¾«Á·£¬½«µÚ1564ÐдúÂëÒƶ¯µ½µÚ1571ÐУ¬ÖÐÐľ͸ô×ŵÚ1569ÐдúÂ룺lock_sock(sk)¡£¼ÓËøÇ°£¬vsk->transportÒѾ¸³Öµµ½transport±äÁ¿ÖУ¬ÕâÀﱬ·¢ÁËÒ»¸öÒýÓã¬È»ºó²Å¾ÙÐÐlock_sock(sk)½«skËø¶¨¡£¿ÉÊÇvsk->transport»áÔڶദ±»Å²ÓÃÉõÖÁ±»ÊÍ·Å£¬Õâ¾ÍÓпÉÄÜͨ¹ýÌõ¼þ¾ºÕùÔì³ÉUse After Free¡£
´¥·¢Àú³Ì
Ê×ÏÈÕÒµ½Ð޸ĻòÊÍ·Åvsk->transportµÄŲÓ÷¾¶£¬À´¿´Òªº¦º¯Êývsock_assign_transport()µÄʵÏÖ¡£¹ØÓڶഫÊäģʽ£¬¸Ãº¯ÊýÓÃÓÚƾ֤²î±ðCID·ÖÅɲî±ðµÄ´«ÊäͨµÀ¡£ÊµÏÖ´úÂëÈçÏÂͼËùʾ£º
ƾ֤sk->sk_type·ÖΪSOCK_DGRAMºÍSOCK_STREAM£¬ÔÚSOCK_STREAMÖУ¬·ÖΪÈýÖÖ´«ÊäͨµÀ¡£ÕâÀï¿ÉÒÔͨ¹ý½«CIDÉèÖÃΪÍâµØ»·»Øģʽ£¬»ñµÃtransport_local´«ÊäͨµÀ¡£½ÓÏÂÀ´ÈçÏÂͼËùʾ£º
ÈôÊÇvsk->transport²»Îª¿Õ£¬Ôò½øÈëifÓï¾ä¡£ÏÈÅжÏvsk->transportÊÇ·ñ¼´ÊÇnew_transport£¬ÈôÊǼ´ÊÇÖ±½Ó·µ»Ø£¬ÔÚ´¥·¢Àú³ÌÖУ¬Òª°ü¹ÜÄÜ×ßµ½vsock_deassign_transport()º¯Êý£¬¸Ãº¯ÊýÊÇÎö¹¹º¯Êý£¬ÓÃÓÚÊÍ·Åtransport¡£ÈçÏ´úÂëËùʾ£º
ÐÐ411£¬Å²ÓÃvsk->transport->destruct()£¬ÒªÃ÷ȷʹÓÃtransportÀàÐÍ£¬Ç°ÎÄÒѾȷ¶¨Ê¹ÓÃtransport_local¡£Transport_localΪȫ¾Ö±äÁ¿£¬»áÔÚvsock_core_register()º¯ÊýÖб»³õʼ»¯¡£¸Ãº¯Êý±»Å²ÓÃÇéÐÎÈçÏÂͼËùʾ£º
*_init()º¯ÊýÓÃÀ´³õʼ»¯transportµÄ»Øµ÷º¯Êý£¬Æ¾Ö¤µÚ¶þ²¿·ÖÏÈÈÝ£¬vhost_vsock_init()¡¢virtio_vsock_init()ºÍvsock_loopback_init()º¯ÊýΪQEMU/KVMÇéÐÎϵÄÖ§³Öº¯Êý¡£ÎÒÃÇ·¢Ã÷transport->destruct()º¯ÊýµÄ×îºóʵÏÖ¶¼ÊÇͳһ¸öº¯Êý¡£ÈçÏÂͼËùʾ£º
¸Ãdestruct()º¯ÊýÊÍ·Åvsk->trans£¬ÈçÏÂͼËùʾ£º
¶øvsk->transÖ¸ÕëÊÇÖ¸ÏòtransportµÄ¡£½á¹¹Ìåvsock_sock½ç˵ÈçÏÂËùʾ£º
×îÖÕ¿ÉÒԽṹһ¸öÊÍ·ÅtransportµÄº¯Êý·¾¶Îª£ºvsock_stream_connect-> vsock_assign_transport->virtio_transport_destrcut¡£
ÕÒµ½ÁËÊÍ·Å·¾¶£¬ÏÂÒ»²½ÕÒʹÓ÷¾¶£¬virtio_transport_notify_buffer_size()º¯Êý»áʹÓÃtransport¡£ÈçÏÂͼËùʾ£º
µÚ492ÐУ¬Í¨¹ývsk->trans»ñÈ¡Ö¸ÏòtransportµÄÖ¸Õ룬µÚ497ÐУ¬½âÒýÓÃvvsÖ¸Õ룬¶Ôvvs->buf_alloc¾ÙÐи³Öµ¡£¶øŲÓÃvirtio_transport_notify_buffer_size()º¯Êý×îÖջᱻvsock_stream_setsockopt()º¯ÊýŲÓá£×îÖÕ¿ÉÒԽṹһ¸öʹÓÃtransportµÄº¯Êý·¾¶Îª£ºvsock_stream_setsockopt-> vsock_update_buffer_size->virtio_transport_notify_buffer_size¡£
½ÓÏÂÀ´¾ÍÊÇÓªÔìÒ»¸öÇÀËøµÄÌõ¼þ¾ºÕùÇéÐΣ¬ºÜÏÔ×űØÐèÊÇconnect()ϵͳŲÓÃÏÈÇÀµ½Ëø¶Ôtransport¾ÙÐÐÊÍ·Å£¬È»ºóÔÙŲÓÃsetsockopt()²Å»ª´¥·¢Îó²î¡£Óпª·¢Ö°Ô±Ìá³öʹÓÃuserfaultfd»úÖÆÏȽ«lock_sockËø¶¨£¬È»ºóÔÚÈ¥ÊÍ·ÅËø£¬¾ÙÐÐÌõ¼þ¾ºÕù¡£Îó²î´¥·¢Àú³ÌÈçÏÂͼËùʾ£º
À¶¿òÖÐÊÇconnect()ŲÓÃÀú³Ì£¬×îºóŲÓÃvirtio_transport_destruct()º¯ÊýÊÍ·Åvsk->trans¡£ºì¿òÖÐÊÇsetsockopt()ŲÓÃÀú³Ì£¬Å²ÓÃvirtio_transport_notify_buffer_size()º¯ÊýʹÓÃvvs£¬¸ÃÖµÊÇ0xffff888107a74500£¬ÔÚ0xffff888107a74500+0x28´¦»áдÈë4×Ö½Ú¡£
²Î¿¼Á´½Ó£º
[1]https://github.com/torvalds/linux/commit/d021c344051af91f42c5ba9fdedc176740cbd238
[2]https://static.sched.com/hosted_files/devconfcz2020a/b1/DevConf.CZ_2020_vsock_v1.1.pdf
[3]https://github.com/jordan9001/vsock_poc
[4]https://terenceli.github.io/%E6%8A%80%E6%9C%AF/2020/04/18/vsock-internals
ÓÅ·¢¹ú¼ÊÍøÕ¾¹ÙÍøÆð¾¢·ÀÓùʵÑéÊÒ£¨ADLab£©
ADLab½¨ÉèÓÚ1999Ä꣬ÊÇÖйúÇå¾²ÐÐÒµ×îÔ罨ÉèµÄ¹¥·ÀÊÖÒÕÑо¿ÊµÑéÊÒÖ®Ò»£¬Î¢ÈíMAPPÍýÏë½¹µã³ÉÔ±£¬¡°ºÚȸ¹¥»÷¡±¿´·¨Ê×ÍÆÕß¡£×èÖ¹ÏÖÔÚ£¬ADLabÒÑͨ¹ýCVEÀÛ¼ÆÐû²¼Çå¾²Îó²î½ü1100¸ö£¬Í¨¹ý CNVD/CNNVDÀÛ¼ÆÐû²¼Çå¾²Îó²î1000Óà¸ö£¬Ò»Á¬¼á³Ö¹ú¼ÊÍøÂçÇå¾²ÁìÓòÒ»Á÷Ë®×¼¡£ÊµÑéÊÒÑо¿Æ«Ïòº¸Ç²Ù×÷ϵͳÓëÓ¦ÓÃϵͳÇå¾²Ñо¿¡¢ÖÇÄÜÖÕ¶ËÇå¾²Ñо¿¡¢ÎïÁªÍøÖÇÄÜ×°±¸Çå¾²Ñо¿¡¢WebÇå¾²Ñо¿¡¢¹¤¿ØϵͳÇå¾²Ñо¿¡¢ÔÆÇå¾²Ñо¿¡£Ñо¿Ð§¹ûÓ¦ÓÃÓÚ²úÆ·½¹µãÊÖÒÕÑо¿¡¢¹ú¼ÒÖصã¿Æ¼¼ÏîÄ¿¹¥¹Ø¡¢×¨ÒµÇå¾²·þÎñµÈ¡£