linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andy Lutomirski <luto@amacapital.net>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Jann Horn <jannh@google.com>, joeyli <jlee@suse.com>,
	Andy Lutomirski <luto@kernel.org>,
	"Lee, Chun-Yi" <joeyli.kernel@gmail.com>,
	"Rafael J . Wysocki" <rafael.j.wysocki@intel.com>,
	Pavel Machek <pavel@ucw.cz>, Len Brown <len.brown@intel.com>,
	"Martin K . Petersen" <martin.petersen@oracle.com>,
	Randy Dunlap <rdunlap@infradead.org>,
	Joe Perches <joe@perches.com>,
	Bart Van Assche <bvanassche@acm.org>,
	kernel list <linux-kernel@vger.kernel.org>,
	linux-pm@vger.kernel.org, Chen Yu <yu.c.chen@intel.com>,
	Giovanni Gherdovich <ggherdovich@suse.cz>
Subject: Re: [PATCH 2/2] PM / Sleep: Check the file capability when writing wake lock interface
Date: Mon, 31 Dec 2018 08:31:05 -0700	[thread overview]
Message-ID: <09310D00-114A-4A51-8E98-0B11F9D9541E@amacapital.net> (raw)
In-Reply-To: <20181231123310.GA3038@kroah.com>



> On Dec 31, 2018, at 5:33 AM, Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote:
> 
>> On Mon, Dec 31, 2018 at 01:02:35PM +0100, Jann Horn wrote:
>> On Mon, Dec 31, 2018 at 11:41 AM Greg Kroah-Hartman
>> <gregkh@linuxfoundation.org> wrote:
>>> 
>>>> On Mon, Dec 31, 2018 at 05:38:51PM +0800, joeyli wrote:
>>>> Hi Greg,
>>>> 
>>>>> On Sun, Dec 30, 2018 at 03:48:35PM +0100, Greg Kroah-Hartman wrote:
>>>>>> On Sun, Dec 30, 2018 at 09:28:56PM +0800, Lee, Chun-Yi wrote:
>>>>>> The wake lock/unlock sysfs interfaces check that the writer must has
>>>>>> CAP_BLOCK_SUSPEND capability. But the checking logic can be bypassed
>>>>>> by opening sysfs file within an unprivileged process and then writing
>>>>>> the file within a privileged process. The tricking way has been exposed
>>>>>> by Andy Lutomirski in CVE-2013-1959.
>>>>> 
>>>>> Don't you mean "open by privileged and then written by unprivileged?"
>>>>> Or if not, exactly how is this a problem?  You check the capabilities
>>>>> when you do the write and if that is not allowed then, well
>>>>> 
>>>> 
>>>> Sorry for I didn't provide clear explanation.
>>>> 
>>>> The privileged means CAP_BLOCK_SUSPEND but not file permission. The file permission
>>>> has already relaxed for non-root user. Then the expected behavior is that non-root
>>>> process must has CAP_BLOCK_SUSPEND capability for writing wake_lock sysfs.
>>>> 
>>>> But, the CAP_BLOCK_SUSPEND restrict can be bypassed:
>>>> 
>>>> int main(int argc, char* argv[])
>>>> {
>>>>        int fd, ret = 0;
>>>> 
>>>>        fd = open("/sys/power/wake_lock", O_RDWR);
>>>>        if (fd < 0)
>>>>                err(1, "open wake_lock");
>>>> 
>>>>        if (dup2(fd, 1) != 1) // overwrite the stdout with wake_lock
>>>>                err(1, "dup2");
>>>>        sleep(1);
>>>>        execl("./string", "string");  //string has capability
>>>> 
>>>>        return ret;
>>>> }
>>>> 
>>>> This program is an unpriviledged process (has no CAP_BLOCK_SUSPEND), it opened
>>>> wake_lock sysfs and overwrited stdout. Then it executes the "string" program
>>>> that has CAP_BLOCK_SUSPEND.
>>> 
>>> That's the problem right there, do not give CAP_BLOCK_SUSPEND rights to
>>> "string".  If any user can run that program, there's nothing the kernel
>>> can do about this, right?  Just don't allow that program on the system :)
>>> 
>>>> The string program writes to stdout, which means that it writes to
>>>> wake_lock. So an unpriviledged opener can trick an priviledged writer
>>>> for writing sysfs.
>>> 
>>> That sounds like a userspace program that was somehow given incorrect
>>> rights by the admin, and a user is taking advantage of it.  That's not
>>> the kernel's fault.
>> 
>> Isn't it? Pretty much any setuid program will write to stdout or
>> stderr; even the glibc linker code does so if you set LD_DEBUG.
>> (Normally the output isn't entirely attacker-controlled, but it is in
>> the case of stuff like "procmail", which I think Debian still ships as
>> setuid root.) setuid programs should always be able to safely call
>> read() and write() on caller-provided file descriptors. Also, you're
>> supposed to be able to receive file descriptors over unix domain
>> sockets and then write to them without trusting the sender. Basically,
>> the ->read and ->write VFS handlers should never look at the caller's
>> credentials, only the opener's (with the exception of LSMs, which tend
>> to do weird things to the system's security model).
> 
> So a root program gets the file handle to the sysfs file and then passes
> it off to a setuid program and the kernel should somehow protect from
> this?

Yes, the kernel should.  If the kernel wants to check caps, it should do it right.

Calling capable() from a .write handler is wrong, even in sysfs.

> 
> I think that any sysfs file that is relying on the capable() check
> should just set their permissions properly first, and then it should be
> ok.
> 

Probably true. 


> thanks,
> 
> greg k-h

  reply	other threads:[~2018-12-31 15:31 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-30 13:28 [PATCH 0/2] [RFC] sysfs: Add hook for checking the file capability of opener Lee, Chun-Yi
2018-12-30 13:28 ` [PATCH 1/2] sysfs: Add hook for checking the file capable for opener Lee, Chun-Yi
2018-12-30 13:28 ` [PATCH 2/2] PM / Sleep: Check the file capability when writing wake lock interface Lee, Chun-Yi
2018-12-30 14:48   ` Greg Kroah-Hartman
2018-12-31  9:38     ` joeyli
2018-12-31 10:40       ` Greg Kroah-Hartman
2018-12-31 12:02         ` Jann Horn
2018-12-31 12:33           ` Greg Kroah-Hartman
2018-12-31 15:31             ` Andy Lutomirski [this message]
2018-12-30 14:45 ` [PATCH 0/2] [RFC] sysfs: Add hook for checking the file capability of opener Greg Kroah-Hartman
2018-12-31  9:41   ` joeyli
2018-12-31 10:38     ` Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=09310D00-114A-4A51-8E98-0B11F9D9541E@amacapital.net \
    --to=luto@amacapital.net \
    --cc=bvanassche@acm.org \
    --cc=ggherdovich@suse.cz \
    --cc=gregkh@linuxfoundation.org \
    --cc=jannh@google.com \
    --cc=jlee@suse.com \
    --cc=joe@perches.com \
    --cc=joeyli.kernel@gmail.com \
    --cc=len.brown@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=pavel@ucw.cz \
    --cc=rafael.j.wysocki@intel.com \
    --cc=rdunlap@infradead.org \
    --cc=yu.c.chen@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).