Is it safe (no security implications) to chown -R user /opt/local/, so that I don't sudo port install with MacPorts?

I don't like the idea of sudoing with MacPorts as it can write things in /Library, for example, without my permission. I know I could use homebrew instead of MacPorts but I had problems with third-party software writing in /usr/local. I could also compile MacPorts with a custom prefix in my home directory but I guess that is the same with /opt/local/, as /opt/local/ is not a system directory.

  • 2
    brew chowns /usr/local and uses it like this. – Kevin Sep 3 '13 at 19:27
  • I used homebrew like this but brew doctor would always complain about some third party program's configs, libraries etc. I wish all third party programs would install in /opt/ as this problem would be solved for good (plus /opt/ is designed to be used for this kind of thing, /usr/local/ is not). – heaptobesquare Sep 3 '13 at 20:08

If you want to use a non-root MacPorts installation you will have to configure your installation to do that by installing from source and specifying --with-no-root-privileges as argument to ./configure. Note that you might have to set --prefix, too, if you want to do that.

I would encourage you not to do that, for a couple of reasons, though:

  • Directories listed in multiple users' $PATH that are writable without superuser privileges can be used for attacks (e.g., by placing a sudo binary that will log the password there). The same can be done by malicious software running as your user in order to get your password – although one might argue it's already too late when this happens.
  • MacPorts can not use privilege separation without root privileges. Normally, MacPorts will drop privileges to an unprivileged "macports" user while building software to avoid rogue Makefiles to wreak havoc (or even read files) in your $HOME.
  • MacPorts as of version 2.2 uses OS X sandboxing capabilities to prevent ports from installing files outside of the locations MacPorts encourages. These default locations are:
    • /opt/local
    • /Applications/MacPorts
    • /Library/LaunchDaemons/
    • some locations necessary for some special ports to work, e.g., /usr/lib/pam for PAM modules.

So, in conclusion, I think a MacPorts installing using root privileges is actually safer and more secure than one not using them.

Disclaimer: Since I'm a MacPorts Developer, I might be biased.  

  • Thank you for your thorough answer. I was not aware of MacPorts usage of sandboxing capabilities. – heaptobesquare Nov 7 '13 at 19:21

This is safe up to the point where you choose to install anything inside /opt

At that point, it's more up to what programs you install there and what permissions and sticky bits you have set. In general, this is a fair tradeoff as long as you otherwise restrict access to the computer, but any time you let a user write directly to a place where programs will be stored, you risk privilege escalation and security issues if the programs can be exploited or have back doors or are just plain destructive due to their design.

  • So another program could easily write in /opt/local/ since access is not restricted. What if I use another prefix only for macports, say ~/opt/local? Again a third party program could write in there but it is unlikely that there are third party programs that would write in a user-defined directory. – heaptobesquare Sep 3 '13 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .