Skip to content
Access control Write-up Intermediate

Zimbra “nginx” Local Root Exploit

The zimbra user could run nginx as root via sudo with an arbitrary configuration file, allowing reading of any file such as /etc/shadow. By abusing nginx error logging to write to /etc/ld.so.preload, an attacker could load a malicious library as root and gain a root shell.

Darren Martyn Follow

Published on darrenmartyn.ie

Recently I decided to have a look at the somewhat popular email and collaboration platform, Zimbra, with the idea to go find some bugs in it.

Darren Martyn (@_darrenmartyn), on darrenmartyn.ie

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

Zimbra is an email and collaboration platform. The author describes it as largely a large body of Java web services, and chose not to look for remote vulnerabilities in it at that time, planning to return to that later. Instead, the target of this work is local privilege escalation: the assumption is that an attacker already has code execution as the zimbra user, for example by exploiting a hole in the web services, and wants to go from there to root.

The write-up is published as full disclosure. The author states that the Zimbra disclosure policy prohibits publication of exploit code, and that he disagrees with that policy.

Setup

The version tested is the latest available from the vendor website at the time: 8.8.15_GA, package zcs-8.8.15_GA_3869.UBUNTU18_64.20190918004220.tgz, MD5 [redacted]. It was installed on an Ubuntu 18.04 virtual machine.

The precondition for the whole chain is a shell as the zimbra user on that host.

Finding it

The first thing checked is what the zimbra user can run through sudo:

Text
zimbra@zimbratest:~$ sudo -l
Matching Defaults entries for zimbra on zimbratest:
    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin, !requiretty

User zimbra may run the following commands on zimbratest:
    (root) NOPASSWD: /opt/zimbra/libexec/zmstat-fd *
    (root) NOPASSWD: /opt/zimbra/libexec/zmunbound
    (root) NOPASSWD: /sbin/resolvconf *
    (root) NOPASSWD: /opt/zimbra/libexec/zmslapd
    (root) NOPASSWD: /opt/zimbra/common/sbin/postfix
    (root) NOPASSWD: /opt/zimbra/common/sbin/postalias
    (root) NOPASSWD: /opt/zimbra/common/sbin/qshape.pl
    (root) NOPASSWD: /opt/zimbra/common/sbin/postconf
    (root) NOPASSWD: /opt/zimbra/common/sbin/postsuper
    (root) NOPASSWD: /opt/zimbra/common/sbin/postcat
    (root) NOPASSWD: /opt/zimbra/libexec/zmqstat
    (root) NOPASSWD: /opt/zimbra/libexec/zmmtastatus
    (root) NOPASSWD: /opt/zimbra/common/sbin/amavis-mc
    (root) NOPASSWD: /opt/zimbra/common/sbin/nginx
    (root) NOPASSWD: /opt/zimbra/libexec/zmmailboxdmgr

The author notes a previous bug in zmstat-fd that allowed taking ownership of files and escalating to root that way, but says it was fixed at some point. He suspects there are further bugs in that file, but it is written in Perl and he was looking for an easy win.

The easy win is the entry for nginx: the zimbra user can run /opt/zimbra/common/sbin/nginx as root, with no password, and the wildcard means it can be given any arguments, including an arbitrary configuration file.

Exploitation

Step 1: read files as root with a custom nginx configuration

The first attempt is deliberately simple: a configuration that serves the whole filesystem and dumps /etc/shadow.

Bash
#!/bin/bash
echo "[+] making config"
cat <<EOF >/tmp/nginx.conf
user root;
worker_processes 4;
pid /tmp/nginx.pid;
events {
        worker_connections 768;
}
http {
server {
	listen 1337;
	root /;
	autoindex on;
}
}
EOF
echo "[+] Launching..."
sudo /opt/zimbra/common/sbin/nginx -c /tmp/nginx.conf
echo "[+] Reading /etc/shadow..."
curl http://localhost:1337/etc/shadow

The script writes a configuration to /tmp/nginx.conf that runs the workers as root, listens on port 1337, sets the document root to / and enables directory listing. It then starts nginx through sudo with -c pointing at that file, and requests /etc/shadow from the local listener.

From the original on darrenmartyn.ie

The screenshot is included after the exploit code.

Reading hashes is not the goal, though; the author wants code execution and notes that cracking a hash is time consuming.

Step 2: turn the error log into /etc/ld.so.preload

After reading the documentation for a while, the author arrives at what he calls a delightfully inelegant solution. It relies on the Linux dynamic linker being very permissive when it parses /etc/ld.so.preload: it accepts any garbage as long as there is a string in there, separated with spaces, containing a valid library to load.

The plan is:

  1. Drop a setuid(0);execve("/bin/sh"...) rootshell to disk.
  2. Drop a library containing a constructor that does chown/chmod on said rootshell binary, along with unlinking /etc/ld.so.preload.
  3. Write an nginx config that runs as root, and has the error log set to /etc/ld.so.preload.
  4. Run nginx with our config file, using sudo.
  5. Request a URL containing some whitespace, and the path to our library file a couple of times. This will ensure that the string containing our library-path gets written out to the error log.
  6. Call sudo, or any setuid binary really, to trigger some library loading as root and get our root shell created.
  7. Run our root shell.

The author warns that it is a bit messy and might cause some temporary system instability, but describes it as extremely fast, reliable and works. Cleanup is built in. He also warns that it does break the whole Zimbra application, because it takes out nginx temporarily; this can be fixed by relaunching nginx with the correct configuration file.

From the original on darrenmartyn.ie

The exploit code is published at https://github.com/darrenmartyn/zimbra-hinginx.

Root cause

The sudoers entry grants the zimbra user passwordless root execution of nginx with arbitrary arguments. nginx takes a configuration file path on the command line, and that configuration controls, among other things, the user the workers run as, the document root, and the path of the error log. Because the configuration is attacker supplied, the error log can be pointed at /etc/ld.so.preload, and request paths that nginx writes into that log become content of the preload file. The dynamic linker then loads the attacker's library into any subsequently started setuid process, and the library's constructor prepares a root shell.

The design mistake is treating a program that reads an attacker-controlled configuration as safe to run as root through sudo. The wildcard in the sudoers rule is what allows the -c argument to be supplied.

Impact

An attacker who already has code execution as the zimbra user can obtain root on the host. The technique is fast and, according to the author, reliable, at the cost of temporary instability: nginx is taken out of service while the exploit runs, which breaks the Zimbra application until nginx is relaunched with the correct configuration file. The author notes this is not the only LPE he found in Zimbra, and that another, neater and more stable one would be published a couple of days later.

Fix

The report does not describe a fix. The measure implied by the analysis is to remove nginx, and any other program that accepts an attacker-controlled configuration file, from the passwordless sudo rules for the zimbra user, or to constrain the arguments it may be given.

Variants to test

  1. Run sudo -l as the low-privileged service account on other Zimbra installs and look for entries with a trailing wildcard, especially interpreters, mail tools and daemons that read a configuration file. A positive result is any entry that lets you pass a path to a file you control.
  2. For each such program, check whether its configuration can redirect a log file or another writable output to /etc/ld.so.preload. A positive result is the file being created or appended to with content you influence.
  3. Check whether request paths, hostnames or other attacker-influenced strings are written verbatim into that log. A positive result is your library path appearing in /etc/ld.so.preload after a request.
  4. Trigger a setuid binary afterwards and see whether your constructor runs as root. A positive result is a root shell or a file owned by root that you created.
  5. Revisit zmstat-fd, which the author suspects still contains further bugs, and the other binaries in the sudo list, for file ownership or argument handling issues.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 16 min read.

What happened, in plain words

Zimbra is a program that companies install on their own servers to handle email and team collaboration: mailboxes, calendars, shared documents and so on. It is a large piece of software made mostly of Java, a programming language that runs on a shared runtime rather than directly on the machine. The author of this write-up wanted to find security bugs in it.

There are two broad kinds of bug you can look for in a server product. A remote bug is one an outsider can trigger over the network, without already being logged in to the machine. A local bug is one that only works once you already have some kind of foothold on the machine, for example a login as a low-privileged user. The author decided to skip the remote hunt for the moment and focus on the local kind, because the Java side was a lot of work and he wanted to keep his sanity.

The specific local bug he went looking for is called local privilege escalation, usually shortened to LPE. On a Linux machine, every running program runs as some user account, and each account has a set of permissions. The most powerful account is called root, also known as the superuser: it can read and change anything on the machine. A normal account, like the zimbra account that the Zimbra software runs under, can only touch its own files. Privilege escalation means going from a weak account to a stronger one, ideally all the way to root. The everyday comparison: you have a key to the staff car park but not to the manager's office; LPE is finding a way to get into the manager's office using only what the car park key gives you.

The assumption behind the whole exercise is that an attacker has already found some way to run commands as the zimbra user, for example by exploiting a bug in Zimbra's web services. From there, the author wants to reach root.

One note on how this was published. The author released it as full disclosure, meaning he published the details and the working exploit code publicly rather than quietly telling the vendor and waiting. He says the Zimbra disclosure policy forbids publishing exploit code, and that he disagrees with that policy. He also says he finds "responsible" disclosure in general to be a way for vendors to pressure researchers into silence. That is his stated position; the technical content below stands on its own.

Setup

The author installed the latest version of Zimbra available from the vendor's website at the time: version 8.8.15_GA, in a package file named zcs-8.8.15_GA_3869.UBUNTU18_64.20190918004220.tgz, with MD5 checksum [redacted]. An MD5 checksum is a short fingerprint of a file: if the file you downloaded has the same fingerprint as the one the vendor published, you know the download was not corrupted or swapped for something else. He installed it on a virtual machine running Ubuntu 18.04. A virtual machine is a computer simulated inside another computer, which is handy for testing because you can break it and throw it away.

The starting point for everything that follows is a command prompt (a shell) logged in as the zimbra user on that machine.

Finding it

On Linux, sudo is the standard tool for running a single command as another user, usually root, without logging in as that user. Which commands a given account is allowed to run this way is written down in a configuration file. The first thing the author did was ask the system what the zimbra user is allowed to run:

Text
zimbra@zimbratest:~$ sudo -l
Matching Defaults entries for zimbra on zimbratest:
    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin, !requiretty

User zimbra may run the following commands on zimbratest:
    (root) NOPASSWD: /opt/zimbra/libexec/zmstat-fd *
    (root) NOPASSWD: /opt/zimbra/libexec/zmunbound
    (root) NOPASSWD: /sbin/resolvconf *
    (root) NOPASSWD: /opt/zimbra/libexec/zmslapd
    (root) NOPASSWD: /opt/zimbra/common/sbin/postfix
    (root) NOPASSWD: /opt/zimbra/common/sbin/postalias
    (root) NOPASSWD: /opt/zimbra/common/sbin/qshape.pl
    (root) NOPASSWD: /opt/zimbra/common/sbin/postconf
    (root) NOPASSWD: /opt/zimbra/common/sbin/postsuper
    (root) NOPASSWD: /opt/zimbra/common/sbin/postcat
    (root) NOPASSWD: /opt/zimbra/libexec/zmqstat
    (root) NOPASSWD: /opt/zimbra/libexec/zmmtastatus
    (root) NOPASSWD: /opt/zimbra/common/sbin/amavis-mc
    (root) NOPASSWD: /opt/zimbra/common/sbin/nginx
    (root) NOPASSWD: /opt/zimbra/libexec/zmmailboxdmgr

Line by line:

  • zimbra@zimbratest:~$ sudo -l is the command being typed. The part before the $ is the prompt: user zimbra, machine zimbratest, current folder ~ (the home folder). sudo -l means "list what I am allowed to do with sudo".
  • Matching Defaults entries lists general settings that apply to this user. env_reset means the environment (the collection of variables a program inherits, such as where to find programs) is reset before running a sudo command. mail_badpass means the administrator gets an email if someone types a wrong password. secure_path=... is a fixed list of folders that sudo will search for programs, so a user cannot sneak in a fake program from a folder they control. !requiretty means sudo does not insist on being run from a real terminal.
  • User zimbra may run the following commands on zimbratest: introduces the list.
  • Each following line is one permission. (root) means the command runs as the root user. NOPASSWD: means no password is asked for. The path is the exact program. A trailing * means "with any arguments at all".

So the zimbra user can run a set of Zimbra and mail-related programs as root without a password. The author mentions that one of them, zmstat-fd, used to have a bug that let an attacker take ownership of files and become root that way, but that this was fixed at some point. He suspects there are more bugs in that program, but it is written in Perl (a scripting language) and he was after an easy win.

The easy win is the nginx line. nginx is a web server: a program that listens for requests from browsers and sends back files or pages. It is configured by a text file, and the path to that file can be given on the command line with the -c option. Because the sudo rule ends in *, the zimbra user can pass any arguments, including -c pointing at a configuration file the user wrote themselves. That means a low-privileged user can tell a program that will run as root exactly how to behave. The everyday comparison: the building manager gives you their master key and says "only use it to open the front door", but nothing stops you from using it on any other door.

Exploitation

Step 1: read files as root with a custom nginx configuration

The first attempt is deliberately simple. If nginx runs as root and we control its configuration, we can make it serve the entire filesystem over HTTP, including files only root can normally read. The most famous such file on Linux is /etc/shadow, which stores the hashed passwords of all accounts. A hash is the result of scrambling a password with a one-way function: you cannot turn it back into the password, but you can guess passwords and scramble the guesses to see if they match.

Bash
#!/bin/bash
echo "[+] making config"
cat <<EOF >/tmp/nginx.conf
user root;
worker_processes 4;
pid /tmp/nginx.pid;
events {
        worker_connections 768;
}
http {
server {
	listen 1337;
	root /;
	autoindex on;
}
}
EOF
echo "[+] Launching..."
sudo /opt/zimbra/common/sbin/nginx -c /tmp/nginx.conf
echo "[+] Reading /etc/shadow..."
curl http://localhost:1337/etc/shadow

Line by line:

  • #!/bin/bash tells the operating system that this file is a script to be run by the bash shell.
  • echo "[+] making config" prints a progress message.
  • cat <<EOF >/tmp/nginx.conf starts writing a block of text into the file /tmp/nginx.conf. Everything until the line EOF is the content. /tmp is a folder every user can write to.
  • user root; tells nginx to run its worker processes as root.
  • worker_processes 4; asks for four worker processes, the parts of nginx that actually handle requests.
  • pid /tmp/nginx.pid; says where to write the file containing the process ID (the number the operating system uses to identify a running program).
  • events { worker_connections 768; } is a standard block setting how many connections each worker can handle.
  • http { server { ... } } starts the web-serving part of the configuration and defines one site.
  • listen 1337; makes nginx listen on port 1337. A port is a numbered channel on a machine; a browser talking to a web server uses port 80 or 443 by default, but any number can be used.
  • root /; sets the document root to /, the top of the filesystem. The document root is the folder from which nginx serves files, so this means "serve anything on the machine".
  • autoindex on; makes nginx show a list of files when a folder is requested, instead of refusing.
  • EOF ends the block.
  • echo "[+] Launching..." prints another progress message.
  • sudo /opt/zimbra/common/sbin/nginx -c /tmp/nginx.conf runs nginx as root (thanks to the sudo rule) with -c pointing at the configuration we just wrote.
  • echo "[+] Reading /etc/shadow..." prints a message.
  • curl http://localhost:1337/etc/shadow uses curl, a command-line tool for fetching URLs, to ask the local nginx on port 1337 for the file /etc/shadow. localhost means "this same machine".
From the original on darrenmartyn.ie

The screenshot is included after the exploit code.

Reading password hashes is not the goal, though. The author wants to run commands as root, and notes that cracking a hash (guessing the password behind it) takes time.

Step 2: turn the error log into /etc/ld.so.preload

After reading the documentation for a while, the author arrived at what he calls a delightfully inelegant solution. To follow it, you need to know about two Linux mechanisms.

The first is the dynamic linker, usually called ld.so. Most programs on Linux do not contain all their code in one file. They use shared libraries: separate files of reusable code that are loaded when the program starts. The dynamic linker is the small program that does this loading. It reads a list of standard places to look, and it also reads a special file, /etc/ld.so.preload, if it exists. Every library listed in that file is loaded into every program that starts, before the program's own code runs. That file is normally only writable by root, which is what makes it interesting: if you can write to it, you can make your own code run inside every program on the machine.

The second is a constructor. In a shared library, a constructor is a function that runs automatically as soon as the library is loaded, before the main program does anything. So a library with a constructor is a way to get code executed inside any program that loads it.

The trick relies on the fact that the dynamic linker is very permissive when it parses /etc/ld.so.preload: it accepts any garbage, as long as somewhere in there, separated by spaces, is a string containing a valid library to load. It does not require the file to be clean or well formed.

Now, nginx has an error log: a file where it writes messages about problems, including the URLs that caused them. The path of that log is set in the configuration file. Since we control the configuration, we can point the error log at /etc/ld.so.preload. Then, if we can make nginx write our library path into that log, the library path ends up inside /etc/ld.so.preload, and the dynamic linker will load our library into every program started afterwards.

The plan is:

  1. Drop a setuid(0);execve("/bin/sh"...) rootshell to disk.
  2. Drop a library containing a constructor that does chown/chmod on said rootshell binary, along with unlinking /etc/ld.so.preload.
  3. Write an nginx config that runs as root, and has the error log set to /etc/ld.so.preload.
  4. Run nginx with our config file, using sudo.
  5. Request a URL containing some whitespace, and the path to our library file a couple of times. This will ensure that the string containing our library-path gets written out to the error log.
  6. Call sudo, or any setuid binary really, to trigger some library loading as root and get our root shell created.
  7. Run our root shell.

In plain words, step by step:

  1. A setuid program is one that runs with the permissions of its owner rather than of the person who started it. A "rootshell" is a shell (a command prompt) running as root. setuid(0) asks the operating system to switch to user ID 0, which is root; execve("/bin/sh", ...) then replaces the current program with a shell. So this step puts a small program on disk that, when run, tries to become root and give a shell.
  2. chown changes who owns a file, and chmod changes its permissions. The constructor in the library will make the rootshell program owned by root and mark it setuid, so that running it gives root. It also unlinks /etc/ld.so.preload, which means deleting that file.
  3. The nginx configuration is written so that nginx runs as root and its error log is /etc/ld.so.preload.
  4. nginx is started through sudo, so it really does run as root and can write to that file.
  5. A request is sent whose URL contains whitespace and the path to the library. nginx writes the offending URL into its error log, so the library path lands in /etc/ld.so.preload, surrounded by junk. The dynamic linker tolerates the junk and finds the path.
  6. Any program that starts as root and uses shared libraries will now load the library, run its constructor, and the constructor prepares the root shell. sudo itself is such a program, so simply calling sudo is enough.
  7. Finally, the rootshell program is run and gives a root command prompt.

The author warns that this is a bit messy and might cause some temporary system instability, but describes it as extremely fast, reliable and works. Cleanup is built in. He also warns that it breaks the whole Zimbra application, because it takes nginx out of service temporarily; this can be fixed by relaunching nginx with the correct configuration file.

From the original on darrenmartyn.ie

The exploit code is published at https://github.com/darrenmartyn/zimbra-hinginx.

Root cause

The root cause is the sudoers entry. The sudoers file is the configuration that says who may run what through sudo. This entry grants the zimbra user passwordless root execution of nginx with arbitrary arguments. nginx takes a configuration file path on the command line, and that configuration controls, among other things, the user the workers run as, the document root, and the path of the error log. Because the configuration is supplied by the attacker, the error log can be pointed at /etc/ld.so.preload, and request paths that nginx writes into that log become content of the preload file. The dynamic linker then loads the attacker's library into any subsequently started setuid process, and the library's constructor prepares a root shell.

The design mistake is treating a program that reads an attacker-controlled configuration as safe to run as root through sudo. The wildcard in the sudoers rule is what allows the -c argument to be supplied.

Impact

An attacker who already has code execution as the zimbra user can obtain root on the host. The technique is fast and, according to the author, reliable, at the cost of temporary instability: nginx is taken out of service while the exploit runs, which breaks the Zimbra application until nginx is relaunched with the correct configuration file. The author notes this is not the only LPE he found in Zimbra, and that another, neater and more stable one would be published a couple of days later.

Fix

The report does not describe a fix. The measure implied by the analysis is to remove nginx, and any other program that accepts an attacker-controlled configuration file, from the passwordless sudo rules for the zimbra user, or to constrain the arguments it may be given.

Variants to test

If you want to look for the same kind of problem on other systems, here is a checklist in plain terms:

  1. Run sudo -l as the low-privileged service account on other Zimbra installs and look for entries with a trailing wildcard, especially interpreters, mail tools and daemons that read a configuration file. A positive result is any entry that lets you pass a path to a file you control.
  2. For each such program, check whether its configuration can redirect a log file or another writable output to /etc/ld.so.preload. A positive result is the file being created or appended to with content you influence.
  3. Check whether request paths, hostnames or other attacker-influenced strings are written verbatim into that log. A positive result is your library path appearing in /etc/ld.so.preload after a request.
  4. Trigger a setuid binary afterwards and see whether your constructor runs as root. A positive result is a root shell or a file owned by root that you created.
  5. Revisit zmstat-fd, which the author suspects still contains further bugs, and the other binaries in the sudo list, for file ownership or argument handling issues.

Glossary

  • Zimbra: an email and collaboration platform that organisations install on their own servers.
  • Java: a programming language whose programs run on a shared runtime rather than directly on the machine.
  • Remote vulnerability: a bug an outsider can trigger over the network without already having a foothold on the machine.
  • Local vulnerability: a bug that only works once you already have some access to the machine.
  • Local privilege escalation (LPE): going from a low-privileged account on a machine to a more powerful one, ideally root.
  • root: the superuser account on Linux, which can read and change anything.
  • Shell: a command prompt where you type commands for the operating system to run.
  • Full disclosure: publishing vulnerability details and exploit code publicly rather than privately notifying the vendor first.
  • MD5 checksum: a short fingerprint of a file, used to check that a download is intact and genuine.
  • Virtual machine: a computer simulated inside another computer.
  • sudo: the Linux tool for running a single command as another user, usually root.
  • sudoers: the configuration file that says who may run what through sudo.
  • NOPASSWD: a sudo setting meaning no password is asked for that command.
  • Wildcard (*): in a sudo rule, a symbol meaning "any arguments at all".
  • nginx: a web server program that listens for requests and sends back files or pages.
  • Web server: a program that answers requests from browsers and other clients over the network.
  • Port: a numbered channel on a machine that a program can listen on.
  • Document root: the folder from which a web server serves files.
  • Hash: the result of scrambling a password with a one-way function, so it cannot be turned back but can be compared against guesses.
  • curl: a command-line tool for fetching URLs.
  • Dynamic linker (ld.so): the small program that loads shared libraries into a program when it starts.
  • Shared library: a separate file of reusable code that programs load at start-up.
  • /etc/ld.so.preload: a file listing libraries that the dynamic linker loads into every program; normally only writable by root.
  • Constructor: a function in a library that runs automatically as soon as the library is loaded.
  • Error log: a file where a program such as nginx writes messages about problems, including the URLs that caused them.
  • setuid: a property of a program that makes it run with the permissions of its owner rather than of the person who started it.
  • Rootshell: a shell running as root.
  • setuid(0): a system call asking the operating system to switch to user ID 0, which is root.
  • execve: a system call that replaces the current program with another one.
  • chown: a command that changes who owns a file.
  • chmod: a command that changes a file's permissions.
  • Unlink: deleting a file.
  • Perl: a scripting language, mentioned because zmstat-fd is written in it.

More by Darren Martyn

View all

1 more disclosed report, best first.

More from Zimbra

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.