Skip to content

Commit b25b60d

Browse files
tititiou36chucklever
authored andcommitted
SUNRPC: Fix a potential buffer overflow in 'svc_print_xprts()'
'maxlen' is the total size of the destination buffer. There is only one caller and this value is 256. When we compute the size already used and what we would like to add in the buffer, the trailling NULL character is not taken into account. However, this trailling character will be added by the 'strcat' once we have checked that we have enough place. So, there is a off-by-one issue and 1 byte of the stack could be erroneously overwridden. Take into account the trailling NULL, when checking if there is enough place in the destination buffer. While at it, also replace a 'sprintf' by a safer 'snprintf', check for output truncation and avoid a superfluous 'strlen'. Fixes: dc9a16e ("svc: Add /proc/sys/sunrpc/transport files") Signed-off-by: Christophe JAILLET <[email protected]> [ cel: very minor fix to documenting comment Signed-off-by: Chuck Lever <[email protected]>
1 parent 9a81ef4 commit b25b60d

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

net/sunrpc/svc_xprt.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,17 @@ void svc_unreg_xprt_class(struct svc_xprt_class *xcl)
104104
}
105105
EXPORT_SYMBOL_GPL(svc_unreg_xprt_class);
106106

107-
/*
108-
* Format the transport list for printing
107+
/**
108+
* svc_print_xprts - Format the transport list for printing
109+
* @buf: target buffer for formatted address
110+
* @maxlen: length of target buffer
111+
*
112+
* Fills in @buf with a string containing a list of transport names, each name
113+
* terminated with '\n'. If the buffer is too small, some entries may be
114+
* missing, but it is guaranteed that all lines in the output buffer are
115+
* complete.
116+
*
117+
* Returns positive length of the filled-in string.
109118
*/
110119
int svc_print_xprts(char *buf, int maxlen)
111120
{
@@ -118,9 +127,9 @@ int svc_print_xprts(char *buf, int maxlen)
118127
list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
119128
int slen;
120129

121-
sprintf(tmpstr, "%s %d\n", xcl->xcl_name, xcl->xcl_max_payload);
122-
slen = strlen(tmpstr);
123-
if (len + slen > maxlen)
130+
slen = snprintf(tmpstr, sizeof(tmpstr), "%s %d\n",
131+
xcl->xcl_name, xcl->xcl_max_payload);
132+
if (slen >= sizeof(tmpstr) || len + slen >= maxlen)
124133
break;
125134
len += slen;
126135
strcat(buf, tmpstr);

0 commit comments

Comments
 (0)