[PATCH 5/5] cifs: Fix buffer size in cifs_strncpy_to_host

Suresh Jayaraman <[email protected]>
Newsgroups gmane.linux.file-systems.cifs
Message-ID <[email protected]>
Fix insufficient buffer allocation and replace kmalloc() with
kzalloc() so that we ensure safe NULL termination always in
unicode case.

Signed-off-by: Suresh Jayaraman <[email protected]>
---
 fs/cifs/cifssmb.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

Index: cifs-2.6.git/fs/cifs/cifssmb.c
===================================================================
--- cifs-2.6.git.orig/fs/cifs/cifssmb.c
+++ cifs-2.6.git/fs/cifs/cifssmb.c
@@ -123,22 +123,24 @@ cifs_strncpy_to_host(char **dst, const c
 		 const bool is_unicode, const struct nls_table *nls_codepage)
 {
 	int plen;
+	size_t nbytes;
 
 	if (is_unicode) {
-		plen = UniStrnlen((wchar_t *)src, maxlen);
-		*dst = kmalloc(plen + 2, GFP_KERNEL);
+		nbytes = UniStrnlenBytes((wchar_t *)src, maxlen, &plen,
+					 nls_codepage);
+		*dst = kzalloc(nbytes + 2, GFP_KERNEL);
 		if (!*dst)
 			goto cifs_strncpy_to_host_ErrExit;
 		cifs_strfromUCS_le(*dst, (__le16 *)src, plen, nls_codepage);
+		/* kzalloc() ensures NULL termination */
 	} else {
 		plen = strnlen(src, maxlen);
 		*dst = kmalloc(plen + 2, GFP_KERNEL);
 		if (!*dst)
 			goto cifs_strncpy_to_host_ErrExit;
 		strncpy(*dst, src, plen);
+		(*dst)[plen] = 0;
 	}
-	(*dst)[plen] = 0;
-	(*dst)[plen+1] = 0; /* harmless for ASCII case, needed for Unicode */
 	return 0;
 
 cifs_strncpy_to_host_ErrExit:
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.