linux-nfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] nfs-utils: mount: fix snprintf return value handling in error formatting
@ 2026-08-25 18:53 Olga Kornievskaia
  2026-08-31 22:59 ` Steve Dickson
  0 siblings, 1 reply; 2+ messages in thread
From: Olga Kornievskaia @ 2026-08-25 18:53 UTC (permalink / raw)
  To: steved; +Cc: linux-nfs

snprintf() returns the number of bytes that would have been written
if sufficient space were available, not the number actually written.
When the formatted string exceeds PATH_MAX (e.g. due to a long
progname), the return value exceeds the buffer size. Using that
unclamped value as an index into errbuf produces an out-of-bounds
pointer, and subsequent snprintf() calls write past the buffer.

Add errbuf_clamp() to bound the position to the valid range before
it is used as a buffer index. Apply it in rpc_mount_errors(),
sys_mount_errors(), and rpc_strerror().

Fixes: 6e54f6179cb9 ("text-based mount.nfs: Add text-based error reporting function")
Signed-off-by: Olga Kornievskaia <okorniev@redhat.com>
---
 utils/mount/error.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/utils/mount/error.c b/utils/mount/error.c
index d6cbdea1..4dace8f5 100644
--- a/utils/mount/error.c
+++ b/utils/mount/error.c
@@ -54,6 +54,21 @@ extern char *progname;
 static char errbuf[PATH_MAX];
 static char *erreob = &errbuf[PATH_MAX];
 
+/*
+ * Clamp a buffer position to the valid range for errbuf.
+ * snprintf() returns the number of bytes that would have been written,
+ * which can exceed the buffer size on truncation.  Using that unclamped
+ * value as an index into errbuf produces an out-of-bounds pointer.
+ */
+static int errbuf_clamp(int pos)
+{
+	if (pos < 0)
+		return 0;
+	if (pos >= PATH_MAX)
+		return PATH_MAX - 1;
+	return pos;
+}
+
 /* Convert RPC errors into strings */
 static int rpc_strerror(int spos)
 {
@@ -62,6 +77,8 @@ static int rpc_strerror(int spos)
 	char *ptr, *estr = clnt_sperrno(cf_stat);
 	char *tmp;
 
+	spos = errbuf_clamp(spos);
+
 	if (estr) {
 		if ((ptr = strchr(estr, ':')))
 			estr = ++ptr;
@@ -72,9 +89,9 @@ static int rpc_strerror(int spos)
 					_("System Error: %s"),
 						strerror(cf_errno));
 		else {
-			if (cf_errno) 
+			if (cf_errno)
 				pos = snprintf(tmp, (erreob - tmp),
-					_("RPC Error:%s; errno = %s"), 
+					_("RPC Error:%s; errno = %s"),
 					estr, strerror(cf_errno));
 			else
 				pos = snprintf(tmp, (erreob - tmp),
@@ -109,6 +126,7 @@ void rpc_mount_errors(char *server, int will_retry, int bg)
 				_("%s: mount to NFS server '%s' failed: "),
 					progname, server);
 
+	pos = errbuf_clamp(pos);
 	tmp = &errbuf[pos];
 	if (rpc_createerr.cf_stat == RPC_TIMEDOUT) {
 		if (will_retry)
@@ -119,6 +137,7 @@ void rpc_mount_errors(char *server, int will_retry, int bg)
 					_("timed out, giving up"));
 	} else {
 		pos += rpc_strerror(pos);
+		pos = errbuf_clamp(pos);
 		tmp = &errbuf[pos];
 		if (bg) {
 			if (will_retry)
@@ -164,6 +183,7 @@ void sys_mount_errors(char *server, int error, int will_retry, int bg)
 				_("%s: mount to NFS server '%s' failed: "),
 					progname, server);
 
+	pos = errbuf_clamp(pos);
 	tmp = &errbuf[pos];
 	if (error == ETIMEDOUT) {
 		if (will_retry)
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-08-31 22:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-25 18:53 [PATCH 1/1] nfs-utils: mount: fix snprintf return value handling in error formatting Olga Kornievskaia
2026-08-31 22:59 ` Steve Dickson

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