Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp9791683imu; Sun, 30 Dec 2018 05:31:26 -0800 (PST) X-Google-Smtp-Source: ALg8bN4z2ByHbfzbfIoksj0hcC2MNiz8gR8HbNi2uTZchL2CMNuObUvDMkNqNf/zskwd6yqfJaO5 X-Received: by 2002:a63:181c:: with SMTP id y28mr4387971pgl.75.1546176686715; Sun, 30 Dec 2018 05:31:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546176686; cv=none; d=google.com; s=arc-20160816; b=S3Qht3G/jlFFzoOIwDdH+HF5kVBJNKDKXfb6IPqmOkSsb0eoKuuxOnlm0RtKvq+D3S Uh76IcT52Ztwy+YQBaWPPCWTkkTOPN0pOkKdslCqIXMxooI3X63o3NemkeasCw+3edBh ODl0yMrfg5zlpuVBvfN2DB+FXt824ITwo6XTETx3IHATfarMGVcRuGWVi6vbITM5tqoY Zz6BEReyEbKz0lKXpwckWm99LavQZmWGSnHCNW1pGkeHGRxJ+kL+yuhhtUbZ5jtzLw68 Zt6a2lps11U5HTrLhQZx08PdQM0tylS4uriPJUy9g/Nbv68cIwCmDsXRBrWo1KCM9FoY eMbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=nWBrAT2ylqKn+nLRC8jRd1IkzOsVy3gKj5LQnJixJBo=; b=oDfAGxTgncouvI/6d4y9EDnbCu5Jw2LigjOHEOJahyEFfDq/HY82MIo13zxLdeNRre QSvwMOv3EXF6ygEzPiEkDRpBoQYH9zBvU/rCd3nMt8Qm7xOXVzcCn4qHxTQ8Mn0LyJKa 2TNz0BetCC2C63zwrONWw7GFVIvr0yM9XqJpGr/1vO6JnOv7HeRuJWL9h2TSiKbHLdl7 CzlBYyUFGz6S89rqBMt5GROZqaTWALq34mxHzOi2u/9JKDpn22Ipok31GJ99wIhXicMV TtljjlgFsE/g8tsEJsa19FxHoL0du/VGynrSAhX7cYYGZh/o0dpE1695ZpOr3fAe++Sm PRkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=vVDR7UL3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z11si45718314pfg.107.2018.12.30.05.31.10; Sun, 30 Dec 2018 05:31:26 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=vVDR7UL3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726280AbeL3N3g (ORCPT + 99 others); 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-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-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; -- 2.13.6