Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp9791050imu; Sun, 30 Dec 2018 05:30:38 -0800 (PST) X-Google-Smtp-Source: ALg8bN6IX9cOnwoBpOIyXuYQK4Pd68oCjAy0mYsDRcg1z5RN0uPfqsx+uJ7d3+HjXSDUWDpqZaWE X-Received: by 2002:a63:e915:: with SMTP id i21mr4409058pgh.409.1546176638799; Sun, 30 Dec 2018 05:30:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546176638; cv=none; d=google.com; s=arc-20160816; b=iG5xiUzgZ32MsVmOxO1GUR24Sq9rKK1HyXHqhlPN6YxkDgQsKPWqs6hOg5vvGP0Dtf TplFZnFEwC8EC3lrXBQbAqePpzMk2gIhYmkRs882qC3syVcyih6rx0A1Gty9iiyDHkOn iFKb607U921gD5Gf2AJszlMLPSewCMWww8KeHbs9UyoNxLRCqinAtZYlUcTems+BcT0D BQjoIFl87B95bdZXJrP81sY3Ec5qN2FetSpZVhifPac2bKep5HSbr1wuqco5TUHV+ceR zR5hH3x7Rh66Y6TjdjOYvmoNX8LtXEHyhCGLg0AlAVYAufbFF96bnqzpWYhQdnlSll3V 7CIg== 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=0IW3MD0sYWaz4TIRd5CQO1Z+JMNuO2rCUnDjCUF1N/I=; b=VXx8yjzfjwj6OyK23Ynd/4Htq9YOUkY7DfA4x7zCr4IaCdoUf/9YWxE8O7b33AGIPL qbfy++dQA0HXbwLSHxMX/hScZi7jrerHTEm4zwP7oM6vTv9DWFyiEpYsBoegqTYYwayc +aZ8/pH3o48KaB4m7IR4iYCbJj3fAJQBSINayuY49Xr2Nhni+EXXIqQV6+PKOKBsqnsD mJ8ZKgxdm4l6L8ZFpdnYu1/BW41chTKrYBMrvxRaHWnDUifoyXXVe0dqMt616w1TcHtm fO2aAHaJB8thgggDmiPtH3kqQm6UVerYQmohW4w7mRxxERSOhmFmbsmS/fIgkPlOjbwh R/kQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Wu9fw03y; 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 u72si41821974pgc.360.2018.12.30.05.30.23; Sun, 30 Dec 2018 05:30:38 -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=Wu9fw03y; 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 S1726229AbeL3N33 (ORCPT + 99 others); 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-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-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); -- 2.13.6