Search code examples
c++osx-elcapitanclang++

"Operation not permitted" running hello world binary compiled w/ clang++ on El Capitan


Background

I created a simple Hello World C++ program:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello World!" << endl;
    return 0;
}

And compiled it with clang++ like so (g++ points to clang++ on OS X apparently):

g++ helloworld-cpp.cpp

This produces an executable, a.out. Running it at the prompt causes bash to throw the error Operation not permitted, as shown:

$ ./a.out
-bash: ./a.out: Operation not permitted

Things I've Tried

  1. Verifying the file has execute permissions, and no attributes or flags that would prevent it from running, using ls -leO:

    -rwxr-xr-x 1 monarch staff - 15212 Jan 1 13:51 a.out

  2. Disabling "System Integrity Protection" using csrutil disable from the Recovery OS terminal, rebooting, recompiling, and running a.out. The same error messages results.

Question

Are there any other restrictions that could prevent binaries I compile on Mac OS X from running?


Solution

  • Figured it out.

    My code was on an encrypted sparseimage, which had the quarantined attribute set on it. I checked this by running mount like so (see attributes on /Volumes/work):

    $ mount
    /dev/disk0s2 on / (hfs, local, journaled)
    /dev/disk2s2 on /Volumes/work (hfs, local, nodev, nosuid, journaled, noowners, quarantine, mounted by monarch)
    

    The actual sparseimage is located in my home folder, titled work.sparseimage. I removed the quarantine attribute like so:

    $ xattr -d com.apple.quarantine work_personal.sparseimage
    

    I then unmounted (ejected) the image, then re-mounted it, recompiled the file and it executed without the error.

    Special thanks to @Mark Setchell for asking me in the question's comments if noexec was set on the drive, and to everyone else for their suggestions.