SSD Advisory – NCurses 5.9 Local Privilege Escalation

Vulnerability Summary
The following advisory describes an Local Privilege Escalation vulnerability in NCurses, version 5.9.

Credit
An independent security researcher Dawid Golunski (https://legalhackers.com/) has reported this vulnerability to Beyond Security’s SecuriTeam Secure Disclosure program

Vendor Responses
NCurses has released a patch to address the vulnerability.

Thomas Dickey has also added the following statement “I don’t know of any actual packages which have traces enabled by default”.

Vulnerability Details
When NCurses, downloaded from official mirror

And NCurses is compiled with trace option:

It allows for NCURSES_TRACE environment variable as explained at:

When the environment variable is set , a program that makes use of NCurses library writes certain tracing information to ./trace file in the current directory.

The library Fails to check for an existence of a symlink and set appropriate umask settings.

This could be abused by attackers to exploit root suid binaries that make use of a vulnerable ncurses library to achieve privilege escalation.

Proof of Concept
For simplicity we can assign SUID root privileges on clear binary:

Which uses NCurses:

Although programs such as htop or ntop could be used as an alternative as these are often recommended to have root suid on their binaries.

Ncurses will then write to the ‘trace‘ file and follow the symlink when clear (having root suid permissions) is run:

As the result, the target file /etc/ldo.so.preload will be created:

If we repeat the test and run:

Before calling clear. Then the file will be created with world-writable permissions:

This would let attacker add arbitrary system libraries and gain code execution with root privileges.

https://blogs.securiteam.com/index.php/feed