From patchwork Sun Dec 30 13:28:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chun-Yi Lee X-Patchwork-Id: 10745097 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D4EBD13AD for ; Sun, 30 Dec 2018 13:29:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C4080287AA for ; Sun, 30 Dec 2018 13:29:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B6E7E289C9; Sun, 30 Dec 2018 13:29:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 95F11287AA for ; Sun, 30 Dec 2018 13:29:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726202AbeL3N33 (ORCPT ); Sun, 30 Dec 2018 08:29:29 -0500 Received: from mail-pf1-f195.google.com ([209.85.210.195]:43964 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725914AbeL3N32 (ORCPT ); Sun, 30 Dec 2018 08:29:28 -0500 Received: by mail-pf1-f195.google.com with SMTP id w73so12312743pfk.10; Sun, 30 Dec 2018 05:29:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=0IW3MD0sYWaz4TIRd5CQO1Z+JMNuO2rCUnDjCUF1N/I=; b=Wu9fw03yUOJpguQhWgfuQX3gorTwS0o9exZglnAQ5/oVJkZZC+oIInh9vKtuc+JEgO qKhdSucETcwl/A91H20qqkDWWtItPzT0gtlgGSiU3yRtOgIn+v2q65xROQxh8NACNTiG dLYbGuL8fhY+R/TRJPueqMFsNiBst3Odiv+eaxTDl/cDPeGSquRveCuJeyn3JOfIOgR0 hTXSaur2Qw3cWTRYGP3kDBeMcU6lOOwF7AXnLLo2RBu4HDbFvkavCl+gdosSOKR/6UFa fgXPZcP/f7KhKW110lPeIPkLJhBiPXq2T6S2sbVbAHDDZoXDmwO6oQXzWKxD4IMMW3+e ivNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=0IW3MD0sYWaz4TIRd5CQO1Z+JMNuO2rCUnDjCUF1N/I=; b=Jnltg1wKM2f9ALRDG47fHzrC3oaqUpKitI8LJICZtiUVde5P4k5qNJDTl6NQqtltYz 0WXZhxYLfrv+KfL5Divgbxr9bHlt1+ZQARHCd45r5XOEvVAcOkJ3kjayvj00shbtSyq6 izbjr0c30XzPCGQi4e3K46/U9EoamsSpI8TZCC6LzS3pkBSIQKMhHF4tzaATgvWjlfaE NHySqOVFOFp/hUDl4ov5JqKUClneIruaJCnC6r1HBNTYG0Kfc4pj3iSx++feH+0f1Yb9 i2/x5f3HMDaRYX3DB3SxCvVKnRIoKvc8lOT0dDMVJHem9eWwXN2LtAchqib1Gflujr2g /kmA== X-Gm-Message-State: AJcUukeLsUYdTjAzu0W6pQb1V0V2ZqJP/0xvrtAAscj2226ECdr2oPUX +yTOxzIxB/YgroWCyKm2sCU= X-Google-Smtp-Source: ALg8bN6DutXMUmJBmsA3bXkGNGxf/awssKXSXBaS5jn9AwoJjHWSgbvQPTpgXD8Q4BXUoUxDK4uQOA== X-Received: by 2002:a63:eb52:: with SMTP id b18mr31839679pgk.213.1546176567694; Sun, 30 Dec 2018 05:29:27 -0800 (PST) Received: from linux-l9pv.suse ([124.11.22.254]) by smtp.gmail.com with ESMTPSA id n73sm74103091pfj.148.2018.12.30.05.29.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 30 Dec 2018 05:29:27 -0800 (PST) From: "Lee, Chun-Yi" X-Google-Original-From: "Lee, Chun-Yi" To: Greg Kroah-Hartman , "Rafael J . Wysocki" , Pavel Machek , Len Brown , "Martin K . Petersen" , Randy Dunlap , Joe Perches , Bart Van Assche Cc: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, "Lee, Chun-Yi" , Chen Yu , Giovanni Gherdovich , Jann Horn , Andy Lutomirski Subject: [PATCH 1/2] sysfs: Add hook for checking the file capable for opener Date: Sun, 30 Dec 2018 21:28:55 +0800 Message-Id: <20181230132856.24095-2-jlee@suse.com> X-Mailer: git-send-email 2.12.3 In-Reply-To: <20181230132856.24095-1-jlee@suse.com> References: <20181230132856.24095-1-jlee@suse.com> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Base on the discussion in the following mail loop about checking capability in sysfs write handler: https://lkml.org/lkml/2018/9/13/978 Some sysfs write handler are checking the writer's capability by using capable(). Base on CVE-2013-1959, those code should use file_ns_capable() to check the opener's capability. Otherwise the capability checking logic can be bypassed. This patch adds hook to sysfs_ops that it allows different implementation in object and attribute levels for checking file capable before accessing sysfs interfaces. The callback function in kobject sysfs_ops is the first implementation of new hook. It casts attribute to kobj_attribute and calls the file capability callback function in kobject attribute level. Cc: Greg Kroah-Hartman Cc: "Rafael J. Wysocki" Cc: Chen Yu Cc: Giovanni Gherdovich Cc: Jann Horn Cc: Andy Lutomirski Cc: Pavel Machek Cc: Len Brown Cc: "Martin K. Petersen" Cc: Randy Dunlap Cc: Joe Perches Cc: Bart Van Assche Signed-off-by: "Lee, Chun-Yi" --- fs/sysfs/file.c | 8 ++++++++ include/linux/kobject.h | 2 ++ include/linux/sysfs.h | 2 ++ lib/kobject.c | 26 ++++++++++++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c index bb71db63c99c..cf98957babd0 100644 --- a/fs/sysfs/file.c +++ b/fs/sysfs/file.c @@ -58,6 +58,10 @@ static int sysfs_kf_seq_show(struct seq_file *sf, void *v) * if @ops->show() isn't implemented. */ if (ops->show) { + if (ops->show_file_capable && + !ops->show_file_capable(of->file, of->kn->priv)) + return -EPERM; + count = ops->show(kobj, of->kn->priv, buf); if (count < 0) return count; @@ -136,6 +140,10 @@ static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf, if (!count) return 0; + if (ops->store_file_capable && + !ops->store_file_capable(of->file, of->kn->priv)) + return -EPERM; + return ops->store(kobj, of->kn->priv, buf, count); } diff --git a/include/linux/kobject.h b/include/linux/kobject.h index 1ab0d624fb36..f89fd692e812 100644 --- a/include/linux/kobject.h +++ b/include/linux/kobject.h @@ -166,6 +166,8 @@ struct kobj_attribute { char *buf); ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count); + bool (*show_file_capable)(const struct file *); + bool (*store_file_capable)(const struct file *); }; extern const struct sysfs_ops kobj_sysfs_ops; diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index 786816cf4aa5..0b107795bee4 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -214,6 +214,8 @@ struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size) struct sysfs_ops { ssize_t (*show)(struct kobject *, struct attribute *, char *); ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t); + bool (*show_file_capable)(const struct file *, struct attribute *); + bool (*store_file_capable)(const struct file *, struct attribute *); }; #ifdef CONFIG_SYSFS diff --git a/lib/kobject.c b/lib/kobject.c index b72e00fd7d09..81699b2b7f72 100644 --- a/lib/kobject.c +++ b/lib/kobject.c @@ -800,6 +800,18 @@ static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr, return ret; } +static bool kobj_attr_show_file_capable(const struct file *file, + struct attribute *attr) +{ + struct kobj_attribute *kattr; + + kattr = container_of(attr, struct kobj_attribute, attr); + if (kattr->show_file_capable) + return kattr->show_file_capable(file); + + return true; +} + static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t count) { @@ -812,9 +824,23 @@ static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr, return ret; } +static bool kobj_attr_store_file_capable(const struct file *file, + struct attribute *attr) +{ + struct kobj_attribute *kattr; + + kattr = container_of(attr, struct kobj_attribute, attr); + if (kattr->store_file_capable) + return kattr->store_file_capable(file); + + return true; +} + const struct sysfs_ops kobj_sysfs_ops = { .show = kobj_attr_show, .store = kobj_attr_store, + .show_file_capable = kobj_attr_show_file_capable, + .store_file_capable = kobj_attr_store_file_capable, }; EXPORT_SYMBOL_GPL(kobj_sysfs_ops); From patchwork Sun Dec 30 13:28:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chun-Yi Lee X-Patchwork-Id: 10745099 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 093FB746 for ; Sun, 30 Dec 2018 13:29:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EC94C287AA for ; Sun, 30 Dec 2018 13:29:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E0BE328A4A; Sun, 30 Dec 2018 13:29:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C034C287AA for ; Sun, 30 Dec 2018 13:29:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725974AbeL3N3g (ORCPT ); Sun, 30 Dec 2018 08:29:36 -0500 Received: from mail-pf1-f196.google.com ([209.85.210.196]:38051 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725914AbeL3N3g (ORCPT ); Sun, 30 Dec 2018 08:29:36 -0500 Received: by mail-pf1-f196.google.com with SMTP id q1so12318331pfi.5; Sun, 30 Dec 2018 05:29:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=nWBrAT2ylqKn+nLRC8jRd1IkzOsVy3gKj5LQnJixJBo=; b=vVDR7UL33exP6gkPgMUAzVpDkTW9B5OkhP4+/Gakpd7uCwNoGz6wFYPJCXr09Q1KC0 5Qr6f3ybdgEJNYhLG4HBUTbwn21xcAR4yT7qD6a/8juniWu6U6To2emcqFK1sJN3cME5 38JBqNePcBcsDWXfzKwVvZEe/6hSV35fOppj4aeEhSlqwuVfP0T1YRZ9GyywYgvqbXYa +kDIC5F0Lzlo1pexqbOmYSkq+cRxm728BKLJth5d9BbhdCHRdO33TbwJjO77DdOjX0wu FzuzGdFLnjvYwfCd3zY2XWyJSzpctH3DpT5KDfglDcqrPyWf0cQ+42X0gPK73+Dj95J5 8HbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=nWBrAT2ylqKn+nLRC8jRd1IkzOsVy3gKj5LQnJixJBo=; b=MUbG/MWfvWSlDda0BQ4GNeCllA/2XHHPCmQzNwkbOAwrIrp2IjPDS+VVeBFpIDem9m iG1apSeLuZaqyvsKNJo/j0wgmRGzzycxDFUKbWjDiBvaowl4yYQDWkNjd2qB42FnytvL t7eog/FmVPBc14ygskH/qPsjKw08sKXSbZxqY91g3TGByPcpEnijQUTTJRgAPv/LogEt wS35KfzhWbum1CTxnnr/6Pv1jijbSYOLzNa+A3kyBgdBSN0fxLkiiybudkhTlJN9ewAA 2CEwFy4yynmW3cdD8/YlNF8dM0orXW1O6SDxXvOgaDkdD2hZeA00vSuhp8nhjQ40JpoP YHFQ== X-Gm-Message-State: AA+aEWbktlL7j+lM0kNcb0fCzdp+vkBz22bJavHatszGj7u2IhOkRTEJ ploZOKWopqDywYaEauA7myI= X-Google-Smtp-Source: AFSGD/XZGc9ldQuUk2sVOd4tjypodmefDFyGtTeW5TY41a7xrZ5CQU+55/hQWGrE06E+ogcegnHccg== X-Received: by 2002:a62:e704:: with SMTP id s4mr35326229pfh.124.1546176575317; Sun, 30 Dec 2018 05:29:35 -0800 (PST) Received: from linux-l9pv.suse ([124.11.22.254]) by smtp.gmail.com with ESMTPSA id n73sm74103091pfj.148.2018.12.30.05.29.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 30 Dec 2018 05:29:34 -0800 (PST) From: "Lee, Chun-Yi" X-Google-Original-From: "Lee, Chun-Yi" To: Greg Kroah-Hartman , "Rafael J . Wysocki" , Pavel Machek , Len Brown , "Martin K . Petersen" , Randy Dunlap , Joe Perches , Bart Van Assche Cc: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, "Lee, Chun-Yi" , Chen Yu , Giovanni Gherdovich , Jann Horn , Andy Lutomirski Subject: [PATCH 2/2] PM / Sleep: Check the file capability when writing wake lock interface Date: Sun, 30 Dec 2018 21:28:56 +0800 Message-Id: <20181230132856.24095-3-jlee@suse.com> X-Mailer: git-send-email 2.12.3 In-Reply-To: <20181230132856.24095-1-jlee@suse.com> References: <20181230132856.24095-1-jlee@suse.com> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Cc: "Rafael J. Wysocki" Cc: Chen Yu Cc: Giovanni Gherdovich Cc: Jann Horn Cc: Andy Lutomirski Cc: Pavel Machek Cc: Len Brown Cc: "Martin K. Petersen" Cc: Randy Dunlap Cc: Joe Perches Cc: Bart Van Assche Signed-off-by: "Lee, Chun-Yi" --- 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;