lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20210913054121.616001-8-hch@lst.de>
Date:   Mon, 13 Sep 2021 07:41:15 +0200
From:   Christoph Hellwig <hch@....de>
To:     Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
        "Rafael J. Wysocki" <rafael@...nel.org>,
        Alexander Viro <viro@...iv.linux.org.uk>
Cc:     Jens Axboe <axboe@...nel.dk>, Tejun Heo <tj@...nel.org>,
        linux-block@...r.kernel.org, linux-xfs@...r.kernel.org,
        linux-fsdevel@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: [PATCH 07/13] sysfs: add ->seq_show support to sysfs_ops

Allow attributes to directly use the seq_file method instead of
carving out a buffer that can easily lead to buffer overflows.

Signed-off-by: Christoph Hellwig <hch@....de>
---
 fs/sysfs/file.c       | 19 ++++++++++++++-----
 include/linux/sysfs.h |  9 +++++++--
 2 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index 42dcf96881b68..12e0bfe40a2b4 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -45,6 +45,9 @@ static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
 	ssize_t count;
 	char *buf;
 
+	if (ops->seq_show)
+		return ops->seq_show(kobj, of->kn->priv, sf);
+
 	if (WARN_ON_ONCE(!ops->show))
 		return -EINVAL;
 
@@ -268,6 +271,10 @@ int sysfs_add_file_mode_ns(struct kernfs_node *parent,
 		return -EINVAL;
 
 	if (mode & SYSFS_PREALLOC) {
+		if (WARN(sysfs_ops->seq_show, KERN_ERR
+				"seq_show not supported on prealloc file: %s\n",
+				kobject_name(kobj)))
+			return -EINVAL;
 		if (sysfs_ops->show && sysfs_ops->store)
 			ops = &sysfs_prealloc_kfops_rw;
 		else if (sysfs_ops->show)
@@ -275,12 +282,14 @@ int sysfs_add_file_mode_ns(struct kernfs_node *parent,
 		else if (sysfs_ops->store)
 			ops = &sysfs_prealloc_kfops_wo;
 	} else {
-		if (sysfs_ops->show && sysfs_ops->store)
-			ops = &sysfs_file_kfops_rw;
-		else if (sysfs_ops->show)
-			ops = &sysfs_file_kfops_ro;
-		else if (sysfs_ops->store)
+		if (sysfs_ops->seq_show || sysfs_ops->show) {
+			if (sysfs_ops->store)
+				ops = &sysfs_file_kfops_rw;
+			else
+				ops = &sysfs_file_kfops_ro;
+		} else if (sysfs_ops->store) {
 			ops = &sysfs_file_kfops_wo;
+		}
 	}
 
 	if (!ops)
diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index e3f1e8ac1f85b..e1ab4da716730 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -236,8 +236,13 @@ struct bin_attribute bin_attr_##_name = __BIN_ATTR_WO(_name, _size)
 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);
+	int	(*seq_show)(struct kobject *kobj, struct attribute *attr,
+			struct seq_file *sf);
+	ssize_t	(*store)(struct kobject *kobj, struct attribute *attr,
+			const char *buf, size_t size);
+
+	/* deprecated except for preallocated attributes: */
+	ssize_t	(*show)(struct kobject *kob, struct attribute *attr, char *buf);
 };
 
 #ifdef CONFIG_SYSFS
-- 
2.30.2

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ