Dr. Daniel, M.D.

Q: Security issue? Gaining root is ridiculously easy.

Okay, so I noticed the other day that an app (forget which) that usually prompts for my password to gain root privileges didn't do so — it just continued doing what it needed the privileges for.

 

I remembered that shortly before, I had used sudo in iTerm to run a command with root privileges, and I know that OS X doesn't ask for your password after the first time you run sudo unless a number of minutes have passed. I had assumed that this behavior would be local to the thread from which you initially provided the password, which would've been the zsh session in iTerm. However, it seemed that it was local to the zsh session, nor to zsh, nor to iTerm. A completely different app had apparently piggy-backed on my sudo "session" and gained root privileges without my approval.

 

I tested this by issuing a sudo command in zsh in iTerm, and then, after having provided my password, I opened up Terminal with bash and issued a sudo command there. No password prompt, instant root privileges.

 

Based on this, it's clear that any app which runs as a user who can run sudo to gain root privileges (which is any normal OS X user) can wait for the user to execute sudo, and immediately gain root access to the system. Knowing when the current user runs sudo is easy, as such an event is written to the syslog.

 

Proof of concept. Quick-n-dirty. Save as a script and run in a terminal window. Then, run sudo in a different terminal window. The script will catch the sudo event and write the empty file "kilroy-was-here", as root:wheel, to the root of the drive.

 

#!/bin/bash

 

tail -f -n 0 /var/log/system.log | grep -m 1 -E 'sudo\[[0-9]+\]:\s+'$USER

echo "Gonna play around with root privs ..."

sudo touch /kilroy-was-here

 

This seems... Wrong... Thoughts?

 

Daniel

MacBook Air, OS X El Capitan (10.11.6), null

Posted on Jul 26, 2016 2:42 AM

Close

Q: Security issue? Gaining root is ridiculously easy.

  • All replies
  • Helpful answers

Previous Page 2 of 3 last Next
  • by Dr. Daniel, M.D.,

    Dr. Daniel, M.D. Dr. Daniel, M.D. Jul 28, 2016 6:29 AM in response to Tech198
    Level 1 (5 points)
    Mac OS X
    Jul 28, 2016 6:29 AM in response to Tech198

    Tech198 wrote:

     

    haha... u call that "easy" ?  I would have a hard time doing that syntax, and i like to know more than the normal user.

     

    If i can't understand that, how do u expect a average user to know ? This is why Apple does things via Terminal

     

    to prevent most users getting access, but its there it they need it..

     

    I wouldn't call a "smart user" who can get "easily get root access" easy on the Mac ... Easy for you yes.. but hard for others.. if they must figure it out on themselves.

     

    Perfectly fine that you couldn't do that as a regular user, don't get me wrong But I'm assuming you're not worried about just some other random person when we're talking about computer security, right? Fact of the matter is that I'm not a blackhat or something like that. I'm a web developer. Nothing fancy. And that snippet right there isn't even code, it's just a couple of commands. The point is that if you ran any program that I made — as yourself — and I had that snippet of code in there, I could do anything with your computer and all user accounts on it. If, of course, and that's the catch, you ever use apps that prompt for your password to do system-level stuff. I just confirmed that with the TrueCrypt app. I've made a mental note to see if something like a Java update's elevation prompt can be exploited as easily. I'm tempted to say I'd put money on that it will.

  • by BobHarris,

    BobHarris BobHarris Jul 28, 2016 6:31 AM in response to Dr. Daniel, M.D.
    Level 6 (19,432 points)
    Mac OS X
    Jul 28, 2016 6:31 AM in response to Dr. Daniel, M.D.

    ...Once you do that, I can steal root privileges from my script running in Terminal in the background without providing the password

    You did not steal anything.  You are within the sudo facilities 5 minute time out for all processes running as "You"

     

    If you feel this is a serious problem (which to date has not been), then please tell Apple

    BugReporter (Free ADC (Apple Developer Connection) account needed for BugReporter)

    <http://bugreporter.apple.com>

    Anyone can get a free ADC account at:

    <https://developer.apple.com/register/index.action>

    And/Or
  • by Dr. Daniel, M.D.,

    Dr. Daniel, M.D. Dr. Daniel, M.D. Jul 28, 2016 6:38 AM in response to BobHarris
    Level 1 (5 points)
    Mac OS X
    Jul 28, 2016 6:38 AM in response to BobHarris

    BobHarris wrote:

     

    When you issued the 'sudo' command, in your terminal session, you started a 5 minute timer for your 'admin' account to be able to issue the 'sudo' command again without a password.

     

    And when I say your 'admin' account, I mean every instance of your account.  So any other terminal sessions, your GUI login, etc... have 5 minutes to use the 'sudo' facilities without needing to specify a password.

     

    And the first account created on your Mac is always an 'admin' account.

     

    GUI applications running in your 'admin' account that need elevated privileges are using 'sudo' facilities under the covers.

     

    So your Terminal session's use of 'sudo' enabled your GUI apps use of 'sudo' because it is all you.  And you are the 'admin'

     

    If you wish the 'sudo' command to have a shorter timeout (not advised), you can change the /etc/sudoers file to include the 'passwd_timeout' parameter (see "man sudoers").  Note: mess up the /etc/sudoers file and you will not be able to get elevated privileges again, without restoring it, so be very careful about any changes you make to this file.

     

    Hey I missed this reply

     

    If this confirmation is in fact true (and I believe it is), then this is really, really bad. That means the defaults leave you wide open to having an app wait around to get root access to your box. I'll have to try a Linux box when I get home to see if it behaves similarly. From using Lubuntu earlier this week, I'm almost positive that consecutive sudos in one tab in lxterm used the auto-grant, while opening a new tab would require password on the first run. If it does behave like on OS X, I'll weep

     

    It seems utterly insane to me not to restrict this five-minute "auto-grant" period to the process which triggered it. Or at least the app (which I actually thought sudo/OS X was doing before this). I mean, I realize that "convenience" is a pretty high priority with Apple, but this doesn't seem like a well thought through design.

  • by Dr. Daniel, M.D.,

    Dr. Daniel, M.D. Dr. Daniel, M.D. Jul 28, 2016 6:45 AM in response to BobHarris
    Level 1 (5 points)
    Mac OS X
    Jul 28, 2016 6:45 AM in response to BobHarris

    BobHarris wrote:

     

    ...Once you do that, I can steal root privileges from my script running in Terminal in the background without providing the password

    You did not steal anything.  You are within the sudo facilities 5 minute time out for all processes running as "You"

     

    If you feel this is a serious problem (which to date has not been), then please tell Apple

    BugReporter (Free ADC (Apple Developer Connection) account needed for BugReporter)

    <http://bugreporter.apple.com>

    Anyone can get a free ADC account at:

    <https://developer.apple.com/register/index.action>

    I'm sorry to be so blunt, but if you don't think this is a problem at all then I'm quite frankly shocked beyond belief.

     

    Either way, I didn't actually intend for the question to be a debate about the ramifications of the issue so much as try to establish whether it actually was an issue that existed outside my (apparently not "technically" broken) install. Seems like it is

     

    Well, thanks for chipping in! It was very helpful

     

    Cheers,

    Daniel

  • by BobHarris,

    BobHarris BobHarris Jul 28, 2016 7:16 AM in response to Dr. Daniel, M.D.
    Level 6 (19,432 points)
    Mac OS X
    Jul 28, 2016 7:16 AM in response to Dr. Daniel, M.D.

    I'll have to try a Linux box when I get home to see if it behaves similarly.

    This is the same behavior I have on RedHat Enterprise Linux and Oracle Linux distributions.  I do not know if every Linux distribution keeps the default /etc/sudoers settings.

     

    And I have frequently used the ability to issue sudo on one terminal session, and within the 5 minute timeout, issued sudo commands on other terminal sessions.

     

    As for apps waiting around to issue 'sudo' commands.  If you install malware from any source, it can do damage.  It could pretend to be a FlashPlayer update, or someother system install that many people know they need to enter their password for, and trick you into giving you not only sudo access, but grab your password.

     

    This is why Apple has signed apps, and can revoke the developer's signature if it is discovered that they are distributing malware with their signature.

     

    It is also why XProtect runs as part of OS X to look for known issues, and it is updated as needed when new issues crop up, to disable them once Apple knows about them.

     

    There are lots of ways to fool a user.  The sudo 5 minute timeout is the least of the problems.  And since this has been the behavior since the first release of OS X way back in the early 2000's, and it has not become a problem in all that time, plus it is the behavior of just about every Unix implementation I have worked on (Digital Equipment Corporation/Compaq/HP's Tru64 UNIX, HP-UX, Solaris, AIX, RedHat Linux, Oracle Linux, and of course OS X), and the 5 minute timeout has not been a problem.

     

    Since you have "MD" after your name, it is possible you are concerned about HIPPA Compliance, and I do not think this forum is qualified to offer an opinion about HIPPA.

  • by dialabrain,

    dialabrain dialabrain Jul 28, 2016 7:23 AM in response to BobHarris
    Level 5 (6,211 points)
    Mac App Store
    Jul 28, 2016 7:23 AM in response to BobHarris

    Thank you Bob for bringing up Linux's behavior. I thought it behaved the same in Ubuntu based distros but I wasn't positive and didn't feel like checking at the moment.

  • by Király,

    Király Király Jul 28, 2016 7:33 AM in response to Dr. Daniel, M.D.
    Level 6 (9,819 points)
    Mac OS X
    Jul 28, 2016 7:33 AM in response to Dr. Daniel, M.D.

    Dr. Daniel, M.D. wrote:

     

    Okay, first of all, let's get real here. I know of zero Mac users (including everyone at every place I've ever worked) who have created an extra, underprivileged user after having set up their Mac and logged in with the sudo-enabled user that is created by default, and is currently using that user to log in with. The majority of people don't know how to do that, let alone why they might want to.

    I do that, in fact Apple advises us to to exactly that. Running all the time as an admin user hands admin privileges to every app and process you run; privileges those apps don't need and shouldn't have.

     

     

     

     

    I know how to do that, but seriously can't be bothered having to deal with user switching throughout the day while developing and whatnot. And I shouldn't have to to avoid the issue I'm talking about right here. We can agree all day that your using a less privileged user for your sessions is more secure. But the reality is, that pretty much no one is. And if I'm interested in exploiting a vulnerability to get access to sensitive data en masse, I'm much more interested in the majority and the defaults than I am about seeking out people utilizing better practices.

     

    Why do you think you need to switch users? I do admin and root tasks from my non-admin account all the time simply by supplying the admin user's username and password when prompted. No switching to the admin account is necessary. It's probably been at least a year since I actually logged in to the admin account.

     

    Switching is easy; just create a new admin account and change your previous account from admin to standard. It takes about 20 seconds.

     

    Yes I agree about your security concerns with sudo, but that's what you get when you run all the time as an admin user. The solution is just to stop unnecessarily handing admin rights to apps and processes that don't need them; i.e. stop running all the time as admin.

  • by etresoft,

    etresoft etresoft Jul 28, 2016 9:07 AM in response to Dr. Daniel, M.D.
    Level 7 (29,198 points)
    Mac OS X
    Jul 28, 2016 9:07 AM in response to Dr. Daniel, M.D.

    Dr. Daniel, M.D. wrote:

     

    The sudo command isn't just for advanced users. It is also indirectly called by apps that require root access to do stuff (e.g. write images to USB sticks, mount encrypted partitions, etc.). The fact that it does this via a graphical user interface doesn't change the fact that it triggers a sudo session in the background which can be hijacked very easily (if you're logged in the way that arguably 99+% of Mac users are).

    No. The sudo command is only for shell use. It cannot be used from any GUI apps. Other kinds of authentication use other methods and are specific to those apps that employ them. There are normally lots of hoops to jump through. By using sudo, you acknowledge the risks of bypassing all of those extra security hoops.

     

    Also, I don't agree with you that, "the expectation is that they know what else is running on their system at all times." That would just be a blanket excuse someone could apply anywhere to not bother dealing with security in one's own code. I don't remember sudo behaving this way when I used to run Linux as a desktop OS.

    Well, yes. It is a blanket excuse that people could use to avoid proper security. But that is the way the world works. If you go outside the mainstream, you are on your own. Terminal and especially sudo are outside that mainstream.

     

    Don't know what you mean by, "There are timestamp directories in /var/db/sudo as expected. There just isn't any $username variable."?

    I was referred to the comment above by Jesse Trucks. The sudo command is working as documented.

  • by Dr. Daniel, M.D.,

    Dr. Daniel, M.D. Dr. Daniel, M.D. Jul 29, 2016 12:01 PM in response to Király
    Level 1 (5 points)
    Mac OS X
    Jul 29, 2016 12:01 PM in response to Király

    Király wrote:

     

    The majority of people don't know how to do that, let alone why they might want to.

    I do that, in fact Apple advises us to to exactly that. Running all the time as an admin user hands admin privileges to every app and process you run; privileges those apps don't need and shouldn't have.

     

    I don't know that Apple advices us to do that, a quick Google search doesn't find anything immediately suggesting this. The knowledge base articles that do turn up, such as this one, all seem to mention that you shouldn't use auto-login for an administrator account, though, but that's far from that advice.

     

    Even if they do advise using this setup somewhere, then how does that possibly make any difference when

    1. You'd have to go look for this advice yourself
    2. The setup wizard that runs on your Mac does not set this up, nor does it mention that you might want to

    Why do you think you need to switch users? I do admin and root tasks from my non-admin account all the time simply by supplying the admin user's username and password when prompted. No switching to the admin account is necessary. It's probably been at least a year since I actually logged in to the admin account.

     

    Well, for me, I the shell more than I use the GUI. I do tons of stuff that needs root access, and having to do that by first su'ing to my administrator user and then sudo'ing on from there would just plain suck. Then there's the issue of file permissions while I'd be shelling as either daniel:staff or standarddaniel:whatevergroupthatwouldbe. Neither would be able to mess with each other's files, so combining coding in the GUI as the standard user with having to be the admin user in an elevated shell prompt (where if I didn't want to be constantly switching users, I'd have to have duplicate setups of zsh and all other terminal-y stuff like brew and go and tools and credentials for accessing rackspace and AWS, ssh keys for logging in to our corporate servers, etc. ad nauseum), I'd have to deal with file permissions all day long as well.

     

    Even if that setup could be made semi-trivial, it doesn't change the fact that it doesn't have to be like that. Every Linux distro I've used (and tested, including those two falsely claimed to behave otherwise by that other fella) don't have this insecure configuration and work just splendidly in terms of security out-of-the-box.

     

    And again, even if you using that setup isn't a hassle for you, that's not the point: It's just not the setup that practically every Mac user is using. Sorry if I sound pedantic (still a little worked up about the other guy just lying to me to win some kind of argument I wasn't actually trying to make), but it's just irrelevant that it's not a really big hassle to reconfigure your Mac to not be vulnerable to attacks exploiting this ill-thought-of default configuration. It's like asking someone using Windows when a zero-day exploit in the networking stack is discovered, "Why aren't you just using OS X instead? It's just as easy, if not easier to use, and it has all the apps you use?" There are valid points in that question, it's just not relevant to a discussion about the exploit itself.

     

     

    Yes I agree about your security concerns with sudo, but that's what you get when you run all the time as an admin user. The solution is just to stop unnecessarily handing admin rights to apps and processes that don't need them; i.e. stop running all the time as admin.

    No. That's what you get when you run all the time as an admin user on OS X. No other OS that I've tried, including Windows, has an exploitable configuration like this.

     

    And again, how is this relevant? Is anyone in your family running OS X? Your mother or father, perhaps? Your friends? Have they set up their systems like you? Does it matter to you that they're vulnerable to this unfortunate exploitable default configuration? I'm not worried about me (well, a little bit, I'm worried that it might already have been exploited) — I'm worried about my friends, family and colleagues. This is the way the default user session runs on every Mac out there.

     

    I totally agree with the points that running as a different user than the default would exempt you from being vulnerable to attacks exploiting this behavior, I just don't see how to get from that to "then this isn't a problem."

     

    Cheers,

    Daniel

  • by dialabrain,

    dialabrain dialabrain Jul 29, 2016 12:15 PM in response to Dr. Daniel, M.D.
    Level 5 (6,211 points)
    Mac App Store
    Jul 29, 2016 12:15 PM in response to Dr. Daniel, M.D.

    Dr. Daniel, M.D. wrote:

    Does it matter to you that they're vulnerable to this unfortunate exploitable default configuration?

    Not really.

  • by Dr. Daniel, M.D.,

    Dr. Daniel, M.D. Dr. Daniel, M.D. Jul 29, 2016 12:22 PM in response to etresoft
    Level 1 (5 points)
    Mac OS X
    Jul 29, 2016 12:22 PM in response to etresoft

    etresoft wrote:

     

    No. The sudo command is only for shell use. It cannot be used from any GUI apps. Other kinds of authentication use other methods and are specific to those apps that employ them. There are normally lots of hoops to jump through. By using sudo, you acknowledge the risks of bypassing all of those extra security hoops.

     

    The sudo command is not just for shell use. That there are other methods for obtaining elevated privileges in no way changes the fact that software already being used on OS X (as I demonstrated) does in fact use sudo and allows any other app running as the same user to piggy-back on the root access granted by them.

     

    I don't know what you mean by there normally being "lots of hoops to jump through," or how using sudo is "bypassing all of those extra security hoops" — you'd need to demonstrate that before it's more than just an assumption. By using sudo on any sanely configured system I've used, you do not acknowledge any security wall to just disappear, least of all that using sudo to elevate a command effectively allows every process running under your user account to automatically be able to get root privileges. I really don't know how I'm even having to stress this fact.

     

    Well, yes. It is a blanket excuse that people could use to avoid proper security. But that is the way the world works. If you go outside the mainstream, you are on your own. Terminal and especially sudo are outside that mainstream.

     

    And, as illustrated, you actively need to use neither Terminal, nor sudo, to be vulnerable. Just be logged in and use software.

     

    I know I'm getting bitchy here, and I'll stop shortly since right now we're not even remotely on topic, and the OQ has already been answered, but seriously, "that is the way the world works" is the most depressing statement uttered by people who couldn't give a rat's *** about anything and need an excuse to why they just don't bother participating anymore. I know that's not why you said it, so let me instead point out that, no, that's not the way the world works, unless you use OS X right now, because sudo doesn't normally work like this, and it was actually created to improve the security of the average POSIXy box running out there.

  • by etresoft,

    etresoft etresoft Jul 29, 2016 2:29 PM in response to Dr. Daniel, M.D.
    Level 7 (29,198 points)
    Mac OS X
    Jul 29, 2016 2:29 PM in response to Dr. Daniel, M.D.

    Dr. Daniel, M.D. wrote:

     

    The sudo command is not just for shell use. That there are other methods for obtaining elevated privileges in no way changes the fact that software already being used on OS X (as I demonstrated) does in fact use sudo and allows any other app running as the same user to piggy-back on the root access granted by them.

    The sudo command is a shell command. That is all it is.

     

    I don't know what you mean by there normally being "lots of hoops to jump through," or how using sudo is "bypassing all of those extra security hoops" — you'd need to demonstrate that before it's more than just an assumption.

    OK. This is how all other apps elevate privileges: https://developer.apple.com/library/mac/documentation/Security/Conceptual/Secure CodingGuide/Articles/AccessControl.html

     

    By using sudo on any sanely configured system I've used, you do not acknowledge any security wall to just disappear, least of all that using sudo to elevate a command effectively allows every process running under your user account to automatically be able to get root privileges. I really don't know how I'm even having to stress this fact.

    Probably because it isn't a fact. The sudo command on the Mac works the same way it always has. It works the same way the sudo command works on Linux. I installed Ubuntu just now to verify. In fact, Ubuntu (at least the Ubuntu Server 16.04 distro) has even less security than the Mac. It didn't even have that initial wall of warning about the sudo command. It took my password and dumped me into root. And after that, it didn't bother asking for my password again.

     

    And, as illustrated, you actively need to use neither Terminal, nor sudo, to be vulnerable. Just be logged in and use software.

    I have not seen that demonstrated anywhere. Sudo will allow another sudo, but not anything else.

     

    To be clear, if you consider only the sudo command, you do have a valid point. The exploit you describe is possible, it just isn't anything new or specific to OS X. It also relies on the user running sudo. All in all, it doesn't seem like a very good mass-market attack strategy. But if you knew someone you wanted to hack and knew they used sudo, it might be effective.

     

    But again, no one else in the world is really concerned about this. If you are concerned, then you can do as both Király and I have suggested and use a standard account. Or you can change the sudo configuration to your liking. Or you can use one of the "-k" options to invalidate the timestamp. It really isn't that much of a hassle. I can't imagine what you are doing that requires you to go in and out of an admin account that much. I certainly don't do that. Nothing that you have described above requires root. You don't need duplicate shell setups. You only need root for operations that require root on your local machine and there are very few of those. If you run something like Docker then there will be even fewer still.

  • by BobHarris,

    BobHarris BobHarris Jul 29, 2016 4:23 PM in response to Dr. Daniel, M.D.
    Level 6 (19,432 points)
    Mac OS X
    Jul 29, 2016 4:23 PM in response to Dr. Daniel, M.D.

    ...I mean, even using one of these distros is rare. Using both? Seriously weird...

    My day job is writing software that runs on multiple platforms, so every day, I have access to RedHat Linux, Oracle Linux, Oracle Sun's Solaris, IBM's AIX, Microsoft Windows, and my own OS X systems.  And since I've been working in the computer industry since the 70's, I've worked with many other Unix distributions, such a ULTRIX, Digital UNIX, Tru64 UNIX, HP-UX, SCO UNIX, BSD UNIX, FreeBSD UNIX, AT&T System V UNIX, and I may have forgotten a few (and I've got a lot of years on Digital Equipment Corporation's OpenVMS, not to mention my hardware diagnostic days when I wrote diagnostics that ran on bare metal without an operating system).

     

    While it may be rare for someone not in the industry, I've been doing this professionally for 45+ years.

  • by etresoft,

    etresoft etresoft Jul 29, 2016 4:45 PM in response to BobHarris
    Level 7 (29,198 points)
    Mac OS X
    Jul 29, 2016 4:45 PM in response to BobHarris

    This thread must have been edited because I don't see the quoted line anywhere.

     

    What's really weird is that the OP is using zsh. I thought I was the only one weird enough to use zsh.

  • by cdhw,

    cdhw cdhw Jul 29, 2016 5:02 PM in response to Jesse Trucks
    Level 4 (2,653 points)
    Servers Enterprise
    Jul 29, 2016 5:02 PM in response to Jesse Trucks

    I believe that the directory modification date for

     

         /var/db/sudo/$username

     

    is the timestamp you are looking for.

     

    IME, it's common, but not universal, Mac users to have a non-admin 'user' account for day-to-day use separate from the original 'admin' account used to setup the machine. It's uncommon for employer-owned Macs to be set up any other way and folk tend to replicate the setup they are familiar with at work on their home machines.

     

    C.

Previous Page 2 of 3 last Next