Skip to content

Commit 6a0ee60

Browse files
bpo-40121: Fixes audit event raised on creating a new socket (GH-19238)
(cherry picked from commit 63ba5cc) Co-authored-by: Steve Dower <[email protected]>
1 parent 6c9a2a8 commit 6a0ee60

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

Lib/test/audit-tests.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,28 @@ def hook(event, args):
327327
CloseKey(kv)
328328

329329

330+
def test_socket():
331+
import socket
332+
333+
def hook(event, args):
334+
if event.startswith("socket."):
335+
print(event, *args)
336+
337+
sys.addaudithook(hook)
338+
339+
socket.gethostname()
340+
341+
# Don't care if this fails, we just want the audit message
342+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
343+
try:
344+
# Don't care if this fails, we just want the audit message
345+
sock.bind(('127.0.0.1', 8080))
346+
except error:
347+
pass
348+
finally:
349+
sock.close()
350+
351+
330352
if __name__ == "__main__":
331353
from test.libregrtest.setup import suppress_msvcrt_asserts
332354

Lib/test/test_audit.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ def test_winreg(self):
118118
self.assertSequenceEqual(["winreg.EnumKey", " ", f"{expected} 10000"], events[3])
119119
self.assertSequenceEqual(["winreg.PyHKEY.Detach", " ", expected], events[4])
120120

121+
def test_socket(self):
122+
support.import_module("socket")
123+
returncode, events, stderr = self.run_python("test_socket")
124+
if returncode:
125+
self.fail(stderr)
126+
127+
if support.verbose:
128+
print(*events, sep='\n')
129+
self.assertEqual(events[0][0], "socket.gethostname")
130+
self.assertEqual(events[1][0], "socket.__new__")
131+
self.assertEqual(events[2][0], "socket.bind")
132+
self.assertTrue(events[2][2].endswith("('127.0.0.1', 8080)"))
121133

122134
if __name__ == "__main__":
123135
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes audit events raised on creating a new socket.

Modules/socketmodule.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5069,7 +5069,7 @@ sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
50695069

50705070
#ifdef MS_WINDOWS
50715071
/* In this case, we don't use the family, type and proto args */
5072-
if (fdobj != NULL && fdobj != Py_None)
5072+
if (fdobj == NULL || fdobj == Py_None)
50735073
#endif
50745074
{
50755075
if (PySys_Audit("socket.__new__", "Oiii",
@@ -5091,8 +5091,9 @@ sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
50915091
}
50925092
memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info));
50935093

5094-
if (PySys_Audit("socket()", "iii", info.iAddressFamily,
5095-
info.iSocketType, info.iProtocol) < 0) {
5094+
if (PySys_Audit("socket.__new__", "Oiii", s,
5095+
info.iAddressFamily, info.iSocketType,
5096+
info.iProtocol) < 0) {
50965097
return -1;
50975098
}
50985099

0 commit comments

Comments
 (0)