Unveiling the Sudo Heap Overflow Vulnerability (CVE-2021-3156): A Critical Security Flaw Reappears

本文详细介绍了Sudo堆溢出漏洞(CVE-2021-3156)的细节。该漏洞允许攻击者通过构造恶意payload,导致sudo堆溢出,从而实现本地权限提升。漏洞存在于sudo解析命令行参数时的判断错误。利用该漏洞,执行特定命令会导致malloc():无效大小的提示,这是堆溢出的典型异常。
摘要由CSDN通过智能技术生成

​​​​​​​​

Introduction

In this blog post, we dive into the intricacies of the Sudo Heap Overflow Vulnerability (CVE-2021-3156). On January 26, 2021, Qualys Research Labs discovered a flaw in sudo. When sudo parses the command line parameters, the truncation character is wrongly judged, which leads to the attacker maliciously constructing a payload, causing sudo to overflow the heap. This vulnerability can cause Local privilege escalation.

Environment

environment version

• ubuntu 20.04

• sudo-1.8.31p2

Use the following command to compile and install

cd ./sudo-SUDO_1_8_31p2 mkdir build ./configure --prefix = /home/pwn/sudo CFLAGS = "-O0 -g " make && make install

Vulnerability

#poc
 ./sudoedit -s  '\' 11111111111111111111111111111111111111111111111111111111111111

Executing the above POC and executing sudoedit will display the words malloc(): invalid size, which is a typical exception caused by a heap overflow.

​​​​​​​​

 Source:- https://tutorialboy24.blogspot.com/2023/07/unveiling-sudo-heap-overflow.htmlicon-default.png?t=N6B9https://tutorialboy24.blogspot.com/2023/07/unveiling-sudo-heap-overflow.html

Image description

Vulnerability Analysis

The Source code analysis

set_cmnd function 
File : plugins\sudoers\sudoers . c 
800 : static  int 
801 : set_cmnd ( void ) 
802 : { ... 819 :      if ( sudo_mode & ( MODE_RUN | MODE_EDIT | MODE_CHECK )) { //Need to meet the setting of the flag bit To enter the escape process ... 845 : 846 : /* set user_args */ 847 : if ( NewArgc > 1 ) { 848 :



       char  * to , * from , ** av ; 
849 : size_t size , n ;         
850 : 
851 : /* Alloc and build up user_args. */        
852 : for ( size = 0 , av = NewArgv + 1 ; * av ; av ++ ) //Traverse each parameter              
853 : size += strlen ( * av ) +          1 ; // Calculate the length of each parameter 
854 : if ( size == 0 || ( user_args = malloc ( size )) == NULL ) { // Dynamically allocate a section of memory through malloc to store parameter content              
855 : sudo_warnx ( U_ ( "%s: %s" ), __func__ , U_ ( "unable to allocate memory" ));        
856 : debug_return_int ( - 1 );        
857 :        } 
858 : if       ( ISSET ( sudo_mode , MODE_SHELL | MODE_LOGIN_SHELL )) { //The setting of the flag bit needs to be satisfied to enter the escape process 
859 : /*        
860: * When running a command via a shell, the sudo front-end        
861: * escapes potential meta chars. We unescape non-spaces        
862: * for sudoers matching and logging purposes.        
863: */        
864 : for ( to = user_args , av = NewArgv + 1 ; ( from = * av ); av ++               ) { //Traverse each environment variable and copy the content to the memory 
865 : while ( * from ) {            /*

The vulnerability point, when scanning the parameter content, encounter \ needs to be escaped, such as '\t' , '\n', etc., so sudo only judges whether \ is followed by a space character, that is, the isspace function is used to judge . characters included in isspace are as follows: ' ' (0x20) space (SPC) space character '\t' (0x09) horizontal tab (TAB) horizontal tab character '\n' (0x0a) newline ( LF) newline character '\v' (0x0b) vertical tab (VT ) vertical tab character '\f' (0x0c) feed (FF) form feed character '\r' (0x0d) carriage return (CR) does not include '' above carriage return . The parameters are separated by '', so when '\' is followed by '', from++ will cause the next parameter to be copied in,and finally cause the heap block to overflow.*/ 866 :

           ==  '\\'  &&  ! isspace (( unsigned  char ) from [ 1 ])) 
867 : from ++ ;                
868 : * to ++ = * from ++ ;              
869 :     }        
870 : * to ++ = ' ' ;              
871 : }        
872 : *-- to = '' ;        

Therefore, the vulnerability lies in the need to escape the escape character when entering the set_cmnd function, but the function does not judge the escape character as the end of the parameter, that is, \ + \x00

parse_args function

The parse_args function is used to reverse escaping, that is, if there are escape characters in the parameter, a \ will be added before each escape character

File : src\parse_args . c 
592 :      if ( ISSET ( mode , MODE_RUN ) &&  ISSET ( flags , MODE_SHELL )) { //The setting of the flag bit needs to be satisfied before entering the reverse escape process 
593 : char ** av , * cmnd = NULL ;       
594 : int ac = 1 ;       
595 : 
596 : if ( argc != 0 ) {      
597: /* shell -c "command" */        
598 : char * src , * dst ;         
599 : size_t cmnd_size = ( size_t ) ( argv [ argc - 1 ] - argv [ 0 ]) +             
600 : strlen 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值