linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Dewar <alex.dewar90@gmail.com>
To: unlisted-recipients:; (no To-header on input)
Cc: Alex Dewar <alex.dewar90@gmail.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	Christian Brauner <christian.brauner@ubuntu.com>,
	Nayna Jain <nayna@linux.ibm.com>,
	Dan Williams <dan.j.williams@intel.com>,
	Mauro Carvalho Chehab <mchehab+huawei@kernel.org>,
	Sourabh Jain <sourabhjain@linux.ibm.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH RFC 1/2] sysfs: add helpers for safely showing simple strings
Date: Sun, 30 Aug 2020 00:37:16 +0100	[thread overview]
Message-ID: <20200829233720.42640-2-alex.dewar90@gmail.com> (raw)
In-Reply-To: <20200829233720.42640-1-alex.dewar90@gmail.com>

Add a helper, sysfs_strscpy(), which simply copies a string and
appends a newline and NUL char to the end, making sure not to overflow
the destination buffer, which MUST be PAGE_SIZE bytes (which is true for
buffers in this context). It includes a compile time check for the
specified destination buffer size.

Also add a helper macro, sysfs_strcpy(), which calls sysfs_strscpy() with
count == PAGE_SIZE, for use with regular NUL-terminated strings. If the
src buffer is a fixed-size array, guarantee that we don't copy beyond its
end by only copying a maximum of sizeof(src) bytes.

Signed-off-by: Alex Dewar <alex.dewar90@gmail.com>
---
 fs/sysfs/file.c       | 14 ++++++++++++++
 include/linux/sysfs.h | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index eb6897ab78e7..2a60e5c6392d 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -707,3 +707,17 @@ int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid)
 	return 0;
 }
 EXPORT_SYMBOL_GPL(sysfs_change_owner);
+
+ssize_t __sysfs_strscpy(char *dest, const char *src, size_t count)
+{
+	ssize_t written;
+
+	if (count > PAGE_SIZE)
+		return -EINVAL;
+
+	written = strscpy(dest, src, count - 1);
+	dest[written++] = '\n';
+	dest[written] = '\0';
+	return written;
+}
+EXPORT_SYMBOL_GPL(__sysfs_strscpy);
diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index 34e84122f635..26e7d9f69dfd 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -162,6 +162,41 @@ static const struct attribute_group _name##_group = {		\
 };								\
 __ATTRIBUTE_GROUPS(_name)
 
+/**
+ *	sysfs_strscpy - return a string from a show method with terminating
+ *	newline to a maximum of count bytes
+ *	@dest: destination buffer
+ *	@src: string to be emitted
+ *	@count: maximum number of bytes to be written to dest
+ */
+#define sysfs_strscpy(dest, src, count)                                       \
+({                                                                            \
+	BUILD_BUG_ON(__builtin_constant_p(count) && (count) > PAGE_SIZE);      \
+	__sysfs_strscpy((dest), (src), (count));                               \
+})
+
+ssize_t __sysfs_strscpy(char *dest, const char *src, size_t count);
+
+/**
+ *	sysfs_strcpy - return a string from a show method with terminating
+ *	newline
+ *	@dest: destination buffer
+ *	@src: string to be emitted
+ *
+ *	This method will only write a maximum of PAGE_SIZE bytes to dest,
+ *	ensuring that the output buffer is not overflown. If src is a
+ *	fixed-size array, a maximum of sizeof(src) bytes will be copied,
+ *	ensuring that memory is not read beyond the end of the array.
+ */
+#define sysfs_strcpy(dest, src)                                      \
+sysfs_strscpy((dest), (src),                                         \
+		__builtin_choose_expr(                                \
+			__same_type((src), &(src)[0]),                \
+			PAGE_SIZE,                                    \
+			min(sizeof(src) + 2, PAGE_SIZE)               \
+		)                                                     \
+)
+
 struct file;
 struct vm_area_struct;
 
-- 
2.28.0


  reply	other threads:[~2020-08-29 23:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-29 23:37 [PATCH RFC 0/2] simple sysfs wrappers for single values Alex Dewar
2020-08-29 23:37 ` Alex Dewar [this message]
2020-08-29 23:37 ` [PATCH RFC 2/2] sysfs: add helper macro for showing simple integer values Alex Dewar
2020-08-30  9:13   ` Greg Kroah-Hartman
2020-09-02 15:31     ` Alex Dewar
2020-08-30  0:28 ` [PATCH RFC 0/2] simple sysfs wrappers for single values Joe Perches
2020-08-30  1:23   ` Joe Perches
2020-09-02 15:29     ` Alex Dewar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200829233720.42640-2-alex.dewar90@gmail.com \
    --to=alex.dewar90@gmail.com \
    --cc=christian.brauner@ubuntu.com \
    --cc=dan.j.williams@intel.com \
    --cc=davem@davemloft.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mchehab+huawei@kernel.org \
    --cc=nayna@linux.ibm.com \
    --cc=rafael@kernel.org \
    --cc=sourabhjain@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).