Skip to contentSkip to conversation

cifs: stop cifsd from exiting prematurely

PATCH In review Jeff Layton 2 participants 5 messages Sep 3, 2010 – Sep 5, 2010 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover cifs: stop cifsd from exiting prematurely

We've had some sporradic reports of problems recently where the ses->server pointers have been NULL. That pointer should only get set to NULL when cifs_demultiplex_thread exits. That should only happen when the last reference to it is put. If it gets cleaned up before then, other structures will hold pointers to it, and we'll get NULL pointer dereferences.

Unfortunately, there are some places in cifs_demuliplex_thread where this rule is broken. In certain error conditions, cifsd will exit even though the reference count hasn't gone to zero yet. This patchset is intended to close these holes. With it, cifsd should only exit when the last reference to it is put.

Exiting from the loop early is a serious error that's likely to cause an oops. I think we ought to consider this patchset for 2.6.36 and maybe for stable.

Thoughts?

Jeff Layton (3):
cifs: prevent cifsd from exiting prematurely
cifs: eliminate some more premature cifsd exits
cifs: prevent possible memory corruption in cifs_demultiplex_thread

 fs/cifs/cifsproto.h |    3 +-
 fs/cifs/connect.c   |   58 +++++++++++++++++---------------------------------
 fs/cifs/netmisc.c   |   22 +++++++++++--------
 3 files changed, 35 insertions(+), 48 deletions(-)

A message that is not in the archive (sent off-list or not yet received): 1283516711-15073-1-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org

#2 lore raw

PATCH 1/3 cifs: prevent cifsd from exiting prematurely

When cifs_demultiplex_thread exits, it does a number of cleanup tasks including freeing the TCP_Server_Info struct. Much of the existing code in cifs assumes that when there is a cisfSesInfo struct, that it holds a reference to a valid TCP_Server_Info struct.

We can never allow cifsd to exit when a cifsSesInfo struct is still holding a reference to the server. The server pointers will then point to freed memory.

This patch eliminates a couple of questionable conditions where it does this. The idea here is to make an -EINTR return from kernel_recvmsg behave the same way as -ERESTARTSYS or -EAGAIN. If the task was signalled from cifs_put_tcp_session, then tcpStatus will be CifsExiting, and the kernel_recvmsg call will return quickly.

There's also another condition where this can occur too -- if the tcpStatus is still in CifsNew, then it will also exit if the server closes the socket prematurely. I think we'll probably also need to fix that situation, but that requires a bit more consideration.

---
 fs/cifs/connect.c |   16 +++++++---------
 1 files changed, 7 insertions(+), 9 deletions(-)
fs/cifs/connect.c +7 -9
index ec0ea4a..f06d8f9 100644
@@ -400,7 +400,9 @@ incomplete_rcv:
 			cFYI(1, "call to reconnect done");
 			csocket = server->ssocket;
 			continue;
-		} else if ((length == -ERESTARTSYS) || (length == -EAGAIN)) {
+		} else if (length == -ERESTARTSYS ||
+			   length == -EAGAIN ||
+			   length == -EINTR) {
 			msleep(1); /* minimum sleep to prevent looping
 				allowing socket to clear and app threads to set
 				tcpStatus CifsNeedReconnect if server hung */
@@ -422,10 +424,6 @@ incomplete_rcv:
 				   and so simply return error to mount */
 				break;
 			}
-			if (!try_to_freeze() && (length == -EINTR)) {
-				cFYI(1, "cifsd thread killed");
-				break;
-			}
 			cFYI(1, "Reconnect after unexpected peek error %d",
 				length);
 			cifs_reconnect(server);
@@ -522,8 +520,7 @@ incomplete_rcv:
 		     total_read += length) {
 			length = kernel_recvmsg(csocket, &smb_msg, &iov, 1,
 						pdu_length - total_read, 0);
-			if ((server->tcpStatus == CifsExiting) ||
-			    (length == -EINTR)) {
+			if (server->tcpStatus == CifsExiting) {
 				/* then will exit */
 				reconnect = 2;
 				break;
@@ -534,8 +531,9 @@ incomplete_rcv:
 				/* Now we will reread sock */
 				reconnect = 1;
 				break;
-			} else if ((length == -ERESTARTSYS) ||
-				   (length == -EAGAIN)) {
+			} else if (length == -ERESTARTSYS ||
+				   length == -EAGAIN ||
+				   length == -EINTR) {
 				msleep(1); /* minimum sleep to prevent looping,
 					      allowing socket to clear and app
 					      threads to set tcpStatus
Signature · 1 line
1.7.1
#3 lore raw

PATCH 2/3 cifs: eliminate some more premature cifsd exits

In reply to a message not in the archive

If the tcpStatus is still CifsNew, the main cifs_demultiplex_loop can break out prematurely in some cases. This is wrong as we will almost always have other structures with pointers to the TCP_Server_Info. If the main loop breaks under any other condition other than tcpStatus == CifsExiting, then it'll face a use-after-free situation.

I don't see any reason to treat a CifsNew tcpStatus differently than CifsGood. I believe we'll still want to attempt to reconnect in either case. What should happen in those situations is that the MIDs get marked as MID_RETRY_NEEDED. This will make CIFSSMBNegotiate return -EAGAIN, and then the caller can retry the whole thing on a newly reconnected socket. If that fails again in the same way, the caller of cifs_get_smb_ses should tear down the TCP_Server_Info struct.

---
 fs/cifs/connect.c |   41 ++++++++++++-----------------------------
 1 files changed, 12 insertions(+), 29 deletions(-)
fs/cifs/connect.c +12 -29
index f06d8f9..a071358 100644
@@ -416,14 +416,6 @@ incomplete_rcv:
 			} else
 				continue;
 		} else if (length <= 0) {
-			if (server->tcpStatus == CifsNew) {
-				cFYI(1, "tcp session abend after SMBnegprot");
-				/* some servers kill the TCP session rather than
-				   returning an SMB negprot error, in which
-				   case reconnecting here is not going to help,
-				   and so simply return error to mount */
-				break;
-			}
 			cFYI(1, "Reconnect after unexpected peek error %d",
 				length);
 			cifs_reconnect(server);
@@ -464,27 +456,18 @@ incomplete_rcv:
 			   an error on SMB negprot response */
 			cFYI(1, "Negative RFC1002 Session Response Error 0x%x)",
 				pdu_length);
-			if (server->tcpStatus == CifsNew) {
-				/* if nack on negprot (rather than
-				ret of smb negprot error) reconnecting
-				not going to help, ret error to mount */
-				break;
-			} else {
-				/* give server a second to
-				clean up before reconnect attempt */
-				msleep(1000);
-				/* always try 445 first on reconnect
-				since we get NACK on some if we ever
-				connected to port 139 (the NACK is
-				since we do not begin with RFC1001
-				session initialize frame) */
-				server->addr.sockAddr.sin_port =
-					htons(CIFS_PORT);
-				cifs_reconnect(server);
-				csocket = server->ssocket;
-				wake_up(&server->response_q);
-				continue;
-			}
+			/* give server a second to clean up  */
+			msleep(1000);
+			/* always try 445 first on reconnect since we get NACK
+			 * on some if we ever connected to port 139 (the NACK
+			 * is since we do not begin with RFC1001 session
+			 * initialize frame)
+			 */
+			server->addr.sockAddr.sin_port = htons(CIFS_PORT);
+			cifs_reconnect(server);
+			csocket = server->ssocket;
+			wake_up(&server->response_q);
+			continue;
 		} else if (temp != (char) 0) {
 			cERROR(1, "Unknown RFC 1002 frame");
 			cifs_dump_mem(" Received Data: ", (char *)smb_buffer,
Signature · 1 line
1.7.1
#4 lore raw

PATCH 3/3 cifs: prevent possible memory corruption in cifs_demultiplex_thread

In reply to a message not in the archive

cifs_demultiplex_thread sets the addr.sockAddr.sin_port without any regard for the socket family. While it may be that the error in question here never occurs on an IPv6 socket, it's probably best to be safe and set the port properly if it ever does.

Break the port setting code out of cifs_fill_sockaddr and into a new function, and call that from cifs_demultiplex_thread.

---
 fs/cifs/cifsproto.h |    3 ++-
 fs/cifs/connect.c   |    3 ++-
 fs/cifs/netmisc.c   |   22 +++++++++++++---------
 3 files changed, 17 insertions(+), 11 deletions(-)
fs/cifs/cifsproto.h +2 -1
index 1378d91..c3d3472 100644
@@ -87,8 +87,9 @@ extern unsigned int smbCalcSize_LE(struct smb_hdr *ptr);
 extern int decode_negTokenInit(unsigned char *security_blob, int length,
 			struct TCP_Server_Info *server);
 extern int cifs_convert_address(struct sockaddr *dst, const char *src, int len);
+extern int cifs_set_port(struct sockaddr *addr, const unsigned short int port);
 extern int cifs_fill_sockaddr(struct sockaddr *dst, const char *src, int len,
-				unsigned short int port);
+				const unsigned short int port);
 extern int map_smb_to_linux_error(struct smb_hdr *smb, int logErr);
 extern void header_assemble(struct smb_hdr *, char /* command */ ,
 			    const struct cifsTconInfo *, int /* length of
fs/cifs/connect.c +2 -1
index a071358..dee5762 100644
@@ -463,7 +463,8 @@ incomplete_rcv:
 			 * is since we do not begin with RFC1001 session
 			 * initialize frame)
 			 */
-			server->addr.sockAddr.sin_port = htons(CIFS_PORT);
+			cifs_set_port((struct sockaddr *)
+					&server->addr.sockAddr, CIFS_PORT);
 			cifs_reconnect(server);
 			csocket = server->ssocket;
 			wake_up(&server->response_q);
fs/cifs/netmisc.c +13 -9
index f978511..9aad47a 100644
@@ -206,26 +206,30 @@ cifs_convert_address(struct sockaddr *dst, const char *src, int len)
 }
 
 int
-cifs_fill_sockaddr(struct sockaddr *dst, const char *src, int len,
-		   const unsigned short int port)
+cifs_set_port(struct sockaddr *addr, const unsigned short int port)
 {
-	if (!cifs_convert_address(dst, src, len))
-		return 0;
-
-	switch (dst->sa_family) {
+	switch (addr->sa_family) {
 	case AF_INET:
-		((struct sockaddr_in *)dst)->sin_port = htons(port);
+		((struct sockaddr_in *)addr)->sin_port = htons(port);
 		break;
 	case AF_INET6:
-		((struct sockaddr_in6 *)dst)->sin6_port = htons(port);
+		((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
 		break;
 	default:
 		return 0;
 	}
-
 	return 1;
 }
 
+int
+cifs_fill_sockaddr(struct sockaddr *dst, const char *src, int len,
+		   const unsigned short int port)
+{
+	if (!cifs_convert_address(dst, src, len))
+		return 0;
+	return cifs_set_port(dst, port);
+}
+
 /*****************************************************************************
 convert a NT status code to a dos class/code
  *****************************************************************************/
Signature · 1 line
1.7.1
#5 lore raw
In reply to a message not in the archive

Hi Jeff,

Ive put your patches on the system which had the issue once. cifs and mounts are working ok. As I can't find a way to reliably reproduce it, I'll just leave it running and check its logs daily.

Quoted 30 lines · Jeff Layton

We've had some sporradic reports of problems recently where the ses->server pointers have been NULL. That pointer should only get set to NULL when cifs_demultiplex_thread exits. That should only happen when the last reference to it is put. If it gets cleaned up before then, other structures will hold pointers to it, and we'll get NULL pointer dereferences.

Unfortunately, there are some places in cifs_demuliplex_thread where this rule is broken. In certain error conditions, cifsd will exit even though the reference count hasn't gone to zero yet. This patchset is intended to close these holes. With it, cifsd should only exit when the last reference to it is put.

Exiting from the loop early is a serious error that's likely to cause an oops. I think we ought to consider this patchset for 2.6.36 and maybe for stable.

Thoughts?

Jeff Layton (3):
 cifs: prevent cifsd from exiting prematurely
 cifs: eliminate some more premature cifsd exits
 cifs: prevent possible memory corruption in cifs_demultiplex_thread

 fs/cifs/cifsproto.h |    3 +-
 fs/cifs/connect.c   |   58 +++++++++++++++++--------------------------------- fs/cifs/netmisc.c   |   22 +++++++++++--------

 3 files changed, 35 insertions(+), 48 deletions(-)