All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Lee, Chun-Yi" <joeyli.kernel@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"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>
Cc: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, "Lee,
	Chun-Yi" <jlee@suse.com>, Chen Yu <yu.c.chen@intel.com>,
	Giovanni Gherdovich <ggherdovich@suse.cz>,
	Jann Horn <jannh@google.com>, Andy Lutomirski <luto@kernel.org>
Subject: [PATCH 2/2] PM / Sleep: Check the file capability when writing wake lock interface
Date: Sun, 30 Dec 2018 21:28:56 +0800	[thread overview]
Message-ID: <20181230132856.24095-3-jlee@suse.com> (raw)
In-Reply-To: <20181230132856.24095-1-jlee@suse.com>

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.

Here is an example that it's a simplified version from CVE-2013-1959
to bypass the capability checking of wake_lock sysfs:

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)
                err(1, "dup2");
        sleep(1);
        execl("./string", "string");

        return ret;
}

The string is a privileged program that it can be used to write
string to wake_lock interface. The main unprivileged process opens
the sysfs interface and overwrites stdout. So the privileged
process will write to wake_lock.

This patch implemented the callback of file capable checking hook
in kobject attribute level. It will check the opener's capability.

Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
Cc: Chen Yu <yu.c.chen@intel.com>
Cc: Giovanni Gherdovich <ggherdovich@suse.cz>
Cc: Jann Horn <jannh@google.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Pavel Machek <pavel@ucw.cz>
Cc: Len Brown <len.brown@intel.com>
Cc: "Martin K. Petersen" <martin.petersen@oracle.com>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Joe Perches <joe@perches.com>
Cc: Bart Van Assche <bvanassche@acm.org>
Signed-off-by: "Lee, Chun-Yi" <jlee@suse.com>
---
 kernel/power/main.c     | 14 ++++++++++++++
 kernel/power/wakelock.c |  6 ------
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/kernel/power/main.c b/kernel/power/main.c
index 98e76cad128b..265199efedc1 100644
--- a/kernel/power/main.c
+++ b/kernel/power/main.c
@@ -661,6 +661,11 @@ static ssize_t wake_lock_store(struct kobject *kobj,
 	return error ? error : n;
 }
 
+static bool wake_lock_store_file_capable(const struct file *file)
+{
+	return file_ns_capable(file, &init_user_ns, CAP_BLOCK_SUSPEND);
+}
+
 power_attr(wake_lock);
 
 static ssize_t wake_unlock_show(struct kobject *kobj,
@@ -678,6 +683,11 @@ static ssize_t wake_unlock_store(struct kobject *kobj,
 	return error ? error : n;
 }
 
+static bool wake_unlock_store_file_capable(const struct file *file)
+{
+	return file_ns_capable(file, &init_user_ns, CAP_BLOCK_SUSPEND);
+}
+
 power_attr(wake_unlock);
 
 #endif /* CONFIG_PM_WAKELOCKS */
@@ -803,6 +813,10 @@ static int __init pm_init(void)
 	power_kobj = kobject_create_and_add("power", NULL);
 	if (!power_kobj)
 		return -ENOMEM;
+#ifdef CONFIG_PM_WAKELOCKS
+	wake_lock_attr.store_file_capable = wake_lock_store_file_capable;
+	wake_unlock_attr.store_file_capable = wake_unlock_store_file_capable;
+#endif
 	error = sysfs_create_group(power_kobj, &attr_group);
 	if (error)
 		return error;
diff --git a/kernel/power/wakelock.c b/kernel/power/wakelock.c
index 4210152e56f0..52a4cfe55eb5 100644
--- a/kernel/power/wakelock.c
+++ b/kernel/power/wakelock.c
@@ -205,9 +205,6 @@ int pm_wake_lock(const char *buf)
 	size_t len;
 	int ret = 0;
 
-	if (!capable(CAP_BLOCK_SUSPEND))
-		return -EPERM;
-
 	while (*str && !isspace(*str))
 		str++;
 
@@ -251,9 +248,6 @@ int pm_wake_unlock(const char *buf)
 	size_t len;
 	int ret = 0;
 
-	if (!capable(CAP_BLOCK_SUSPEND))
-		return -EPERM;
-
 	len = strlen(buf);
 	if (!len)
 		return -EINVAL;
-- 
2.13.6


  parent reply	other threads:[~2018-12-30 13:29 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 ` Lee, Chun-Yi [this message]
2018-12-30 14:48   ` [PATCH 2/2] PM / Sleep: Check the file capability when writing wake lock interface 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
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=20181230132856.24095-3-jlee@suse.com \
    --to=joeyli.kernel@gmail.com \
    --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=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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.