]> granicus.if.org Git - python/commitdiff
fix issue 9129: adds proper error handling on accept() when smtpd accepts new incomin...
authorGiampaolo Rodolà <g.rodola@gmail.com>
Mon, 23 Aug 2010 22:28:13 +0000 (22:28 +0000)
committerGiampaolo Rodolà <g.rodola@gmail.com>
Mon, 23 Aug 2010 22:28:13 +0000 (22:28 +0000)
Lib/smtpd.py
Misc/NEWS

index b4082786fa147b7771238f4d5e3b5578bfa0fff5..179a1b9d63a8e7bfedcf85ae2aa976f198ec9b50 100755 (executable)
@@ -121,7 +121,15 @@ class SMTPChannel(asynchat.async_chat):
         self.rcpttos = []
         self.received_data = ''
         self.fqdn = socket.getfqdn()
-        self.peer = conn.getpeername()
+        try:
+            self.peer = conn.getpeername()
+        except socket.error as err:
+            # a race condition  may occur if the other end is closing
+            # before we can get the peername
+            self.close()
+            if err.args[0] != errno.ENOTCONN:
+                raise
+            return
         print('Peer:', repr(self.peer), file=DEBUGSTREAM)
         self.push('220 %s %s' % (self.fqdn, __version__))
         self.set_terminator(b'\r\n')
@@ -414,7 +422,20 @@ class SMTPServer(asyncore.dispatcher):
                 localaddr, remoteaddr), file=DEBUGSTREAM)
 
     def handle_accept(self):
-        conn, addr = self.accept()
+        try:
+            conn, addr = self.accept()
+        except TypeError:
+            # sometimes accept() might return None
+            return
+        except socket.error as err:
+            # ECONNABORTED might be thrown
+            if err.args[0] != errno.ECONNABORTED:
+                raise
+            return
+        else:
+            # sometimes addr == None instead of (ip, port)
+            if addr == None:
+                return
         print('Incoming connection from %s' % repr(addr), file=DEBUGSTREAM)
         channel = self.channel_class(self, conn, addr)
 
index e8dcca2c12a71bf42b708a90d06aa37b2d2578d8..944df931fb08d3277b36856635d8883141c71fb2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -123,6 +123,9 @@ Extensions
 Library
 -------
 
+- Issue #9129: smtpd.py is vulnerable to DoS attacks deriving from missing 
+  error handling when accepting a new connection.
+
 - Issue #9601: ftplib now provides a workaround for non-compliant 
   implementations such as IIS shipped with Windows server 2003 returning invalid 
   response codes for MKD and PWD commands.