public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Lang Yu <lang.yu@amd.com>
To: Joe Perches <joe@perches.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J . Wysocki" <rafael@kernel.org>,
	<linux-kernel@vger.kernel.org>
Cc: Lang Yu <lang.yu@amd.com>
Subject: [PATCH] sysfs: Remove page boundary align limitation on sysfs_emit and sysfs_emit_at
Date: Wed, 8 Sep 2021 20:07:23 +0800	[thread overview]
Message-ID: <20210908120723.3920701-1-lang.yu@amd.com> (raw)

The key purpose of sysfs_emit and sysfs_emit_at is to
ensure that no overrun is done. Make them more equivalent
with scnprintf.

Signed-off-by: Lang Yu <lang.yu@amd.com>
---
 fs/sysfs/file.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index 9aefa7779b29..b754f2ef186f 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -732,14 +732,16 @@ EXPORT_SYMBOL_GPL(sysfs_change_owner);
 int sysfs_emit(char *buf, const char *fmt, ...)
 {
 	va_list args;
-	int len;
+	int len, offset;
 
-	if (WARN(!buf || offset_in_page(buf),
+	offset = offset_in_page(buf);
+
+	if (WARN(!buf,
 		 "invalid sysfs_emit: buf:%p\n", buf))
 		return 0;
 
 	va_start(args, fmt);
-	len = vscnprintf(buf, PAGE_SIZE, fmt, args);
+	len = vscnprintf(buf, PAGE_SIZE - offset, fmt, args);
 	va_end(args);
 
 	return len;
@@ -760,14 +762,16 @@ EXPORT_SYMBOL_GPL(sysfs_emit);
 int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
 {
 	va_list args;
-	int len;
+	int len, offset;
+
+	offset = offset_in_page(buf);
 
-	if (WARN(!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE,
+	if (WARN(!buf || at < 0 || at + offset >= PAGE_SIZE,
 		 "invalid sysfs_emit_at: buf:%p at:%d\n", buf, at))
 		return 0;
 
 	va_start(args, fmt);
-	len = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args);
+	len = vscnprintf(buf + at, PAGE_SIZE - at - offset, fmt, args);
 	va_end(args);
 
 	return len;
-- 
2.25.1


             reply	other threads:[~2021-09-08 12:08 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-08 12:07 Lang Yu [this message]
2021-09-08 12:32 ` [PATCH] sysfs: Remove page boundary align limitation on sysfs_emit and sysfs_emit_at Greg Kroah-Hartman
2021-09-08 12:52   ` Yu, Lang
2021-09-08 13:04     ` Greg Kroah-Hartman
2021-09-08 13:21       ` Yu, Lang
2021-09-08 13:49         ` Greg Kroah-Hartman
2021-09-08 15:33           ` Yu, Lang
2021-09-09  5:19             ` Greg Kroah-Hartman
2021-09-09  5:31               ` Yu, Lang
2021-09-09  6:05                 ` Greg Kroah-Hartman
2021-09-09  5:05 ` Joe Perches
2021-09-09  5:27   ` Yu, Lang
2021-09-09  5:34     ` Greg Kroah-Hartman
2021-09-09  5:59       ` Yu, Lang
2021-09-09  6:07         ` Greg Kroah-Hartman
2021-09-09  5:44     ` Joe Perches
2021-09-09  5:52       ` Yu, Lang
2021-09-09  6:07         ` Greg Kroah-Hartman
2021-09-09  6:22           ` Yu, Lang
2021-09-09  6:35             ` Greg Kroah-Hartman
2021-09-09  7:48               ` Yu, Lang
2021-09-09  7:59                 ` Greg Kroah-Hartman
2021-09-09  8:48                   ` Yu, Lang

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=20210908120723.3920701-1-lang.yu@amd.com \
    --to=lang.yu@amd.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@kernel.org \
    /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