Author: phater
Date: Sat Oct 29 18:22:22 2016
New Revision: 73068

URL: http://svn.reactos.org/svn/reactos?rev=73068&view=rev
Log:
[MSAFD][WS2_32] Better WSASocket parameters check
CORE-12104

Modified:
    trunk/reactos/dll/win32/msafd/misc/dllmain.c
    trunk/reactos/dll/win32/ws2_32/src/dcatalog.c

Modified: trunk/reactos/dll/win32/msafd/misc/dllmain.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/msafd/misc/dllmain.c?rev=73068&r1=73067&r2=73068&view=diff
==============================================================================
--- trunk/reactos/dll/win32/msafd/misc/dllmain.c        [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/msafd/misc/dllmain.c        [iso-8859-1] Sat Oct 29 
18:22:22 2016
@@ -95,13 +95,33 @@
         Protocol = SharedData->Protocol;
     }
 
-    if (AddressFamily == AF_UNSPEC && SocketType == 0 && Protocol == 0)
+    if (lpProtocolInfo)
+    {
+        if (lpProtocolInfo->iAddressFamily && AddressFamily <= 0)
+            AddressFamily = lpProtocolInfo->iAddressFamily;
+        if (lpProtocolInfo->iSocketType && SocketType <= 0)
+            SocketType = lpProtocolInfo->iSocketType;
+        if (lpProtocolInfo->iProtocol && Protocol <= 0)
+            Protocol = lpProtocolInfo->iProtocol;
+    }
+
+    /* FIXME: AF_NETDES should be AF_MAX */
+    if (AddressFamily < AF_UNSPEC || AddressFamily > AF_NETDES)
         return WSAEINVAL;
 
+    if (SocketType < 0 && SocketType > SOCK_SEQPACKET)
+        return WSAEINVAL;
+
+    if (Protocol < 0 && Protocol > IPPROTO_MAX)
+        return WSAEINVAL;
+
+    /* when no protocol and socket type are specified the first entry
+    * from WSAEnumProtocols that has the flag PFL_MATCHES_PROTOCOL_ZERO
+    * is returned */
+    if (SocketType == 0 && Protocol == 0 && lpProtocolInfo && 
(lpProtocolInfo->dwProviderFlags & PFL_MATCHES_PROTOCOL_ZERO) == 0)
+        return WSAEINVAL;
+
     /* Set the defaults */
-    if (AddressFamily == AF_UNSPEC)
-        AddressFamily = AF_INET;
-
     if (SocketType == 0)
     {
         switch (Protocol)
@@ -117,8 +137,7 @@
             break;
         default:
             TRACE("Unknown Protocol (%d). We will try SOCK_STREAM.\n", 
Protocol);
-            SocketType = SOCK_STREAM;
-            break;
+            return WSAEINVAL;
         }
     }
 
@@ -137,10 +156,12 @@
             break;
         default:
             TRACE("Unknown SocketType (%d). We will try IPPROTO_TCP.\n", 
SocketType);
-            Protocol = IPPROTO_TCP;
-            break;
-        }
-    }
+            return WSAEINVAL;
+        }
+    }
+
+    if (AddressFamily == AF_UNSPEC)
+        return WSAEINVAL;
 
     /* Get Helper Data and Transport */
     Status = SockGetTdiName (&AddressFamily,

Modified: trunk/reactos/dll/win32/ws2_32/src/dcatalog.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/ws2_32/src/dcatalog.c?rev=73068&r1=73067&r2=73068&view=diff
==============================================================================
--- trunk/reactos/dll/win32/ws2_32/src/dcatalog.c       [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/ws2_32/src/dcatalog.c       [iso-8859-1] Sat Oct 29 
18:22:22 2016
@@ -490,6 +490,20 @@
     /* Assume failure */
     *CatalogEntry = NULL;
 
+    /* Params can't be all wildcards */
+    if (af == AF_UNSPEC && type == 0 && protocol == 0)
+        return WSAEINVAL;
+
+    /* FIXME: AF_NETDES should be AF_MAX */
+    if (af < AF_UNSPEC || af > AF_NETDES)
+        return WSAEINVAL;
+
+    if (type < 0 && type > SOCK_SEQPACKET)
+        return WSAEINVAL;
+
+    if (protocol < 0 && protocol > IPPROTO_MAX)
+        return WSAEINVAL;
+
     /* Lock the catalog */
     WsTcLock();
 
@@ -527,6 +541,13 @@
                       Entry->ProtocolInfo.iProtocolMaxOffset) >= protocol)) ||
                     (protocol == 0))
                 {
+                    /* Check that if type and protocol are 0 provider entry 
has PFL_MATCHES_PROTOCOL_ZERO flag set */
+                    if (type == 0 && protocol == 0 && 
(Entry->ProtocolInfo.dwProviderFlags & PFL_MATCHES_PROTOCOL_ZERO) == 0)
+                    {
+                        ErrorCode = WSAEPROTONOSUPPORT;
+                        continue;
+                    }
+
                     /* Check if it doesn't already have a provider */
                     if (!Entry->Provider)
                     {
@@ -550,12 +571,14 @@
             } 
             else 
             {
-                ErrorCode = WSAESOCKTNOSUPPORT;
+                if (ErrorCode != WSAEPROTONOSUPPORT)
+                    ErrorCode = WSAESOCKTNOSUPPORT;
             }
         } 
         else 
         {
-            ErrorCode = WSAEAFNOSUPPORT;
+            if (ErrorCode != WSAEPROTONOSUPPORT && ErrorCode != 
WSAESOCKTNOSUPPORT)
+                ErrorCode = WSAEAFNOSUPPORT;
         }
     }
 


Reply via email to