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);