Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(475)

Side by Side Diff: Modules/posixmodule.c

Issue 13703: Hash collision security issue
Patch Set: Created 1 year, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Modules/main.c ('k') | Objects/bytesobject.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* POSIX module implementation */ 2 /* POSIX module implementation */
3 3
4 /* This file is also used for Windows NT/MS-Win and OS/2. In that case the 4 /* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few 5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source 6 functions are either unimplemented or implemented differently. The source
7 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent 7 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
8 of the compiler used. Different compilers define their own feature 8 of the compiler used. Different compilers define their own feature
9 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler 9 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default 10 independent macro PYOS_OS2 should be defined. On OS/2 the default
(...skipping 4004 matching lines...) Expand 10 before | Expand all | Expand 10 after
4015 PyObject *result = NULL; 4015 PyObject *result = NULL;
4016 4016
4017 #ifdef NGROUPS_MAX 4017 #ifdef NGROUPS_MAX
4018 #define MAX_GROUPS NGROUPS_MAX 4018 #define MAX_GROUPS NGROUPS_MAX
4019 #else 4019 #else
4020 /* defined to be 16 on Solaris7, so this should be a small number */ 4020 /* defined to be 16 on Solaris7, so this should be a small number */
4021 #define MAX_GROUPS 64 4021 #define MAX_GROUPS 64
4022 #endif 4022 #endif
4023 gid_t grouplist[MAX_GROUPS]; 4023 gid_t grouplist[MAX_GROUPS];
4024 4024
4025 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results 4025 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4026 * This is a helper variable to store the intermediate result when 4026 * This is a helper variable to store the intermediate result when
4027 * that happens. 4027 * that happens.
4028 * 4028 *
4029 * To keep the code readable the OSX behaviour is unconditional, 4029 * To keep the code readable the OSX behaviour is unconditional,
4030 * according to the POSIX spec this should be safe on all unix-y 4030 * according to the POSIX spec this should be safe on all unix-y
4031 * systems. 4031 * systems.
4032 */ 4032 */
4033 gid_t* alt_grouplist = grouplist; 4033 gid_t* alt_grouplist = grouplist;
4034 int n; 4034 int n;
4035 4035
(...skipping 2896 matching lines...) Expand 10 before | Expand all | Expand 10 after
6932 6932
6933 static PyObject * 6933 static PyObject *
6934 posix_getloadavg(PyObject *self, PyObject *noargs) 6934 posix_getloadavg(PyObject *self, PyObject *noargs)
6935 { 6935 {
6936 double loadavg[3]; 6936 double loadavg[3];
6937 if (getloadavg(loadavg, 3)!=3) { 6937 if (getloadavg(loadavg, 3)!=3) {
6938 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); 6938 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6939 return NULL; 6939 return NULL;
6940 } else 6940 } else
6941 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); 6941 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
6942 }
6943 #endif
6944
6945 #ifdef MS_WINDOWS
6946
6947 PyDoc_STRVAR(win32_urandom__doc__,
6948 "urandom(n) -> str\n\n\
6949 Return n random bytes suitable for cryptographic use.");
6950
6951 typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6952 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6953 DWORD dwFlags );
6954 typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6955 BYTE *pbBuffer );
6956
6957 static CRYPTGENRANDOM pCryptGenRandom = NULL;
6958 /* This handle is never explicitly released. Instead, the operating
6959 system will release it when the process terminates. */
6960 static HCRYPTPROV hCryptProv = 0;
6961
6962 static PyObject*
6963 win32_urandom(PyObject *self, PyObject *args)
6964 {
6965 int howMany;
6966 PyObject* result;
6967
6968 /* Read arguments */
6969 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6970 return NULL;
6971 if (howMany < 0)
6972 return PyErr_Format(PyExc_ValueError,
6973 "negative argument not allowed");
6974
6975 if (hCryptProv == 0) {
6976 HINSTANCE hAdvAPI32 = NULL;
6977 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
6978
6979 /* Obtain handle to the DLL containing CryptoAPI
6980 This should not fail */
6981 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6982 if(hAdvAPI32 == NULL)
6983 return win32_error("GetModuleHandle", NULL);
6984
6985 /* Obtain pointers to the CryptoAPI functions
6986 This will fail on some early versions of Win95 */
6987 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6988 hAdvAPI32,
6989 "CryptAcquireContextA");
6990 if (pCryptAcquireContext == NULL)
6991 return PyErr_Format(PyExc_NotImplementedError,
6992 "CryptAcquireContextA not found");
6993
6994 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6995 hAdvAPI32, "CryptGenRandom");
6996 if (pCryptGenRandom == NULL)
6997 return PyErr_Format(PyExc_NotImplementedError,
6998 "CryptGenRandom not found");
6999
7000 /* Acquire context */
7001 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7002 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7003 return win32_error("CryptAcquireContext", NULL);
7004 }
7005
7006 /* Allocate bytes */
7007 result = PyBytes_FromStringAndSize(NULL, howMany);
7008 if (result != NULL) {
7009 /* Get random data */
7010 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7011 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7012 PyBytes_AS_STRING(result))) {
7013 Py_DECREF(result);
7014 return win32_error("CryptGenRandom", NULL);
7015 }
7016 }
7017 return result;
7018 } 6942 }
7019 #endif 6943 #endif
7020 6944
7021 PyDoc_STRVAR(device_encoding__doc__, 6945 PyDoc_STRVAR(device_encoding__doc__,
7022 "device_encoding(fd) -> str\n\n\ 6946 "device_encoding(fd) -> str\n\n\
7023 Return a string describing the encoding of the device\n\ 6947 Return a string describing the encoding of the device\n\
7024 if the output is a terminal; else return None."); 6948 if the output is a terminal; else return None.");
7025 6949
7026 static PyObject * 6950 static PyObject *
7027 device_encoding(PyObject *self, PyObject *args) 6951 device_encoding(PyObject *self, PyObject *args)
(...skipping 20 matching lines...) Expand all
7048 { 6972 {
7049 char *codeset = nl_langinfo(CODESET); 6973 char *codeset = nl_langinfo(CODESET);
7050 if (codeset != NULL && codeset[0] != 0) 6974 if (codeset != NULL && codeset[0] != 0)
7051 return PyUnicode_FromString(codeset); 6975 return PyUnicode_FromString(codeset);
7052 } 6976 }
7053 #endif 6977 #endif
7054 Py_INCREF(Py_None); 6978 Py_INCREF(Py_None);
7055 return Py_None; 6979 return Py_None;
7056 } 6980 }
7057 6981
7058 #ifdef __VMS 6982 PyDoc_STRVAR(posix_urandom__doc__,
7059 /* Use openssl random routine */
7060 #include <openssl/rand.h>
7061 PyDoc_STRVAR(vms_urandom__doc__,
7062 "urandom(n) -> str\n\n\ 6983 "urandom(n) -> str\n\n\
7063 Return n random bytes suitable for cryptographic use."); 6984 Return n random bytes suitable for cryptographic use.");
7064 6985
7065 static PyObject* 6986 static PyObject *
7066 vms_urandom(PyObject *self, PyObject *args) 6987 posix_urandom(PyObject *self, PyObject *args)
7067 { 6988 {
7068 int howMany; 6989 Py_ssize_t size;
7069 PyObject* result; 6990 PyObject *result;
6991 int ret;
7070 6992
7071 /* Read arguments */ 6993 /* Read arguments */
7072 if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) 6994 if (!PyArg_ParseTuple(args, "n:urandom", &size))
7073 return NULL; 6995 return NULL;
7074 if (howMany < 0) 6996 if (size < 0)
7075 return PyErr_Format(PyExc_ValueError, 6997 return PyErr_Format(PyExc_ValueError,
7076 "negative argument not allowed"); 6998 "negative argument not allowed");
6999 result = PyBytes_FromStringAndSize(NULL, size);
7000 if (result == NULL)
7001 return NULL;
7077 7002
7078 /* Allocate bytes */ 7003 ret = _PyOS_URandom(PyBytes_AS_STRING(result),
7079 result = PyBytes_FromStringAndSize(NULL, howMany); 7004 PyBytes_GET_SIZE(result));
7080 if (result != NULL) { 7005 if (ret == -1) {
7081 /* Get random data */ 7006 Py_DECREF(result);
7082 if (RAND_pseudo_bytes((unsigned char*) 7007 return NULL;
7083 PyBytes_AS_STRING(result),
7084 howMany) < 0) {
7085 Py_DECREF(result);
7086 return PyErr_Format(PyExc_ValueError,
7087 "RAND_pseudo_bytes");
7088 }
7089 } 7008 }
7090 return result; 7009 return result;
7091 } 7010 }
7092 #endif
7093 7011
7094 static PyMethodDef posix_methods[] = { 7012 static PyMethodDef posix_methods[] = {
7095 {"access", posix_access, METH_VARARGS, posix_access__doc__}, 7013 {"access", posix_access, METH_VARARGS, posix_access__doc__},
7096 #ifdef HAVE_TTYNAME 7014 #ifdef HAVE_TTYNAME
7097 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, 7015 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
7098 #endif 7016 #endif
7099 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, 7017 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
7100 #ifdef HAVE_CHFLAGS 7018 #ifdef HAVE_CHFLAGS
7101 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, 7019 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
7102 #endif /* HAVE_CHFLAGS */ 7020 #endif /* HAVE_CHFLAGS */
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
7367 #ifdef HAVE_PATHCONF 7285 #ifdef HAVE_PATHCONF
7368 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, 7286 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
7369 #endif 7287 #endif
7370 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, 7288 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
7371 #ifdef MS_WINDOWS 7289 #ifdef MS_WINDOWS
7372 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, 7290 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
7373 #endif 7291 #endif
7374 #ifdef HAVE_GETLOADAVG 7292 #ifdef HAVE_GETLOADAVG
7375 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, 7293 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
7376 #endif 7294 #endif
7377 #ifdef MS_WINDOWS 7295 {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__},
7378 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
7379 #endif
7380 #ifdef __VMS
7381 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
7382 #endif
7383 {NULL, NULL} /* Sentinel */ 7296 {NULL, NULL} /* Sentinel */
7384 }; 7297 };
7385 7298
7386 7299
7387 static int 7300 static int
7388 ins(PyObject *module, char *symbol, long value) 7301 ins(PyObject *module, char *symbol, long value)
7389 { 7302 {
7390 return PyModule_AddIntConstant(module, symbol, value); 7303 return PyModule_AddIntConstant(module, symbol, value);
7391 } 7304 }
7392 7305
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
7786 7699
7787 7700
7788 #endif /* __APPLE__ */ 7701 #endif /* __APPLE__ */
7789 return m; 7702 return m;
7790 7703
7791 } 7704 }
7792 7705
7793 #ifdef __cplusplus 7706 #ifdef __cplusplus
7794 } 7707 }
7795 #endif 7708 #endif
OLDNEW
« no previous file with comments | « Modules/main.c ('k') | Objects/bytesobject.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7