Facebook Google Plus Twitter LinkedIn YouTube RSS Menu Search Resource - BlogResource - WebinarResource - ReportResource - Eventicons_066 icons_067icons_068icons_069icons_070

CODESYS V3 Unauthenticated Remote Heap Overflow

Critical

Synopsis

A heap overflow vulnerability exists in CmpWebServerHandlerV3.dll (file version 3.5.15.20) due to improper validation of user-supplied data sent to the CODESYS V3 web server URL endpoint /WebVisuV3.

The flaw is due to the fact that the MemGCGetSize function adds 0x5c bytes to the requested allocation size during memory allocation operation:

__wibu00:004BF8C0 MemGCGetSize proc near             ; CODE XREF: SysMemAllocCode+39↓p
__wibu00:004BF8C0                                    ; SysMemAllocData+1B↓p
__wibu00:004BF8C0                                    ; SysMemReallocData+3C↓p
__wibu00:004BF8C0                                    ; DATA XREF: __wibu01:00813B28↓o
__wibu00:004BF8C0
__wibu00:004BF8C0 arg_size  = dword ptr  8
__wibu00:004BF8C0
__wibu00:004BF8C0           push    ebp
__wibu00:004BF8C1           mov     ebp, esp
__wibu00:004BF8C3           mov     eax, [ebp+arg_size]; attacker-controlled
__wibu00:004BF8C6           add     eax, 5Ch ; '\'   ; int32 overflow!
__wibu00:004BF8C6                                    ; i.e., size can be 0xffffffff
__wibu00:004BF8C9           pop     ebp
__wibu00:004BF8CA           retn
__wibu00:004BF8CA MemGCGetSize endp

The extra 0x5c bytes appears to be used for memory garbage collection purposes. The MemGCGetSize function is called within the SysMemAllocData function, which is used by many CODESYS components to allocate memory from the heap.

An unauthenticated, remote attacker can request a very large memory allocation size (i.e., 0xffffffff) via a WEB_CLIENT_OPENCONNECTION message sent to the CmpWebServerHandlerV3 component:

|foo|-1|true|

The CmpWebServerHandlerV3 component (when in state 0) attempts to allocate -1 (0xffffffff) bytes for the communication buffer. When the SysMemAllocData function is called, the memory allocation size gets overflowed and a small (0xffffffff + 0x5c = 0x5b) heap buffer is actually allocated.

The attacker then sends a WEB_CLIENT_RUN_SERVICE message to overflow the small communication buffer:

.text:100039F4           call    HandleVisuService
.text:100039FA           add     esp, 14h
.text:100039FD           mov     [ebp+err], eax
.text:10003A00           cmp     [ebp+err], 0
.text:10003A04           jnz     short err_10003A54
.text:10003A06           mov     ecx, [ebp+HdrSizePlus4] ; attacker-controlled
.text:10003A09           push    ecx
.text:10003A0A           mov     edx, [ebp+pbLayer7] ; attacker-controlled
.text:10003A0D           push    edx
.text:10003A0E           mov     eax, [ebp+cbCommBuf2] ; ffffffff
.text:10003A11           push    eax
.text:10003A12           mov     ecx, [ebp+pbCommBuf2] ; very small buf => heap buf overflow!
.text:10003A15           push    ecx
.text:10003A16           call    CMUtlSafeMemCpy

The following windbg output shows 0x4014 bytes of attacker-controlled data is being copied to a 0x3-byte (0x5b - (0x0028c3d8 - 0x0028c380) = 3) user buffer on the heap:

CmpWebServerHandlerV3!ComponentEntry+0x1d66:
*** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\Program Files\CODESYS 3.5.15.20\GatewayPLC\CODESYSControlService.exe - 
01b13a16 ff152c85b101    call    dword ptr [CmpWebServerHandlerV3!ComponentEntry+0x687c (01b1852c)] ds:0023:01b1852c=00566110
0:013> dd esp L4
0460fddc  0028c3d8 ffffffff 023107ac 00004014
0:013> !heap -p -a 0028c3d8 
    address 0028c3d8 found in
    _HEAP @ 260000
      HEAP_ENTRY Size Prev Flags    UserPtr UserSize - state
        0028c378 000d 0000  [00]   0028c380    0005b - (busy)

The attached PoC can be used to terminate a 32-bit CODESYSControlService.exe:

   python codesys_v3_webserver_int32_overflow.py  8080

Note that when running the PoC, it's important that the CmpWebServerHandlerV3 component must be in 'state' 0. When CODESYSControlService.exe starts, CmpWebServerHandlerV3 is in state 0.

Solution

Upgrade to V3.5.15.40.

Proof of Concept

https://github.com/tenable/poc/blob/master/codesys/codesys_v3_webserver_int32_overflow_tra_2020_16.py

Disclosure Timeline

12/02/2019 - Vulnerability discovered
12/11/2019 - Vendor Informed, 90 Days is 3/10/2020
12/11/2019 - CODESYS acknowledges. Asks how we would like to be acknowledged.
12/11/2019 - "Tenable, Inc."
12/11/2019 - CODESYS asks for clarification on 90-day date.
12/11/2019 - Tenable clarifies.
01/28/2020 - Vendor informs that they plan to release the patch in the middle of March.
03/18/2020 - CODESYS informs us that due to COVID-19, they will need to postpone their patch and advisory by a few days. They however do plan to release version 3.5.15.40 in mid March.
03/25/2020 - CODESYS has released an advisory and bug fix.

All information within TRA advisories is provided “as is”, without warranty of any kind, including the implied warranties of merchantability and fitness for a particular purpose, and with no guarantee of completeness, accuracy, or timeliness. Individuals and organizations are responsible for assessing the impact of any actual or potential security vulnerability.

Tenable takes product security very seriously. If you believe you have found a vulnerability in one of our products, we ask that you please work with us to quickly resolve it in order to protect customers. Tenable believes in responding quickly to such reports, maintaining communication with researchers, and providing a solution in short order.

For more details on submitting vulnerability information, please see our Vulnerability Reporting Guidelines page.

If you have questions or corrections about this advisory, please email [email protected]

Try for Free Buy Now

Try Tenable.io

FREE FOR 30 DAYS

Enjoy full access to a modern, cloud-based vulnerability management platform that enables you to see and track all of your assets with unmatched accuracy. Sign up now.

Buy Tenable.io

Enjoy full access to a modern, cloud-based vulnerability management platform that enables you to see and track all of your assets with unmatched accuracy. Purchase your annual subscription today.

65 assets

$2,275

Buy Now

Try for Free Buy Now

Try Nessus Professional Free

FREE FOR 7 DAYS

Nessus® is the most comprehensive vulnerability scanner on the market today. Nessus Professional will help automate the vulnerability scanning process, save time in your compliance cycles and allow you to engage your IT team.

Buy Nessus Professional

Nessus® is the most comprehensive vulnerability scanner on the market today. Nessus Professional will help automate the vulnerability scanning process, save time in your compliance cycles and allow you to engage your IT team.

Buy a multi-year license and save. Add Advanced Support for access to phone, email, community and chat support 24 hours a day, 365 days a year. Full details here.

Get FREE Advanced Support

with purchase of Nessus Professional

Try for Free Buy Now

Try Tenable.io Web Application Scanning

FREE FOR 30 DAYS

Enjoy full access to our latest web application scanning offering designed for modern applications as part of the Tenable.io platform. Safely scan your entire online portfolio for vulnerabilities with a high degree of accuracy without heavy manual effort or disruption to critical web applications. Sign up now.

Buy Tenable.io Web Application Scanning

Enjoy full access to a modern, cloud-based vulnerability management platform that enables you to see and track all of your assets with unmatched accuracy. Purchase your annual subscription today.

5 FQDNs

$3,578

Buy Now

Try for Free Contact Sales

Try Tenable.io Container Security

FREE FOR 30 DAYS

Enjoy full access to the only container security offering integrated into a vulnerability management platform. Monitor container images for vulnerabilities, malware and policy violations. Integrate with continuous integration and continuous deployment (CI/CD) systems to support DevOps practices, strengthen security and support enterprise policy compliance.

Buy Tenable.io Container Security

Tenable.io Container Security seamlessly and securely enables DevOps processes by providing visibility into the security of container images – including vulnerabilities, malware and policy violations – through integration with the build process.

Learn More about Industrial Security

Get a Demo of Tenable.sc

Please fill out the form below with your contact information and a sales representative will contact you shortly to schedule a demo. You may also include a short comment (limited to 255 characters). Please note that fields with asterisks (*) are mandatory.

Try for Free Contact Sales

Try Tenable Lumin

FREE FOR 30 DAYS

Visualize and explore your Cyber Exposure, track risk reduction over time and benchmark against your peers with Tenable Lumin.

Buy Tenable Lumin

Contact a Sales Representative to see how Lumin can help you gain insight across your entire organization and manage cyber risk.

Request a demo of Tenable.ot

Get the Operational Technology Security You Need.
Reduce the Risk You Don’t.