8000 [3.11] gh-115886: Handle embedded null characters in shared memory na… · python/cpython@f2b1f6b · GitHub
[go: up one dir, main page]

Skip to content

Commit f2b1f6b

Browse files
[3.11] gh-115886: Handle embedded null characters in shared memory name (GH-115887) (GH-115907)
shm_open() and shm_unlink() now check for embedded null characters in the name and raise an error instead of silently truncating it. (cherry picked from commit 79811ed)
1 parent c6455ff commit f2b1f6b

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

Lib/test/_test_multiprocessing.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3932,6 +3932,21 @@ def _new_shm_name(self, prefix):
39323932
# test_multiprocessing_spawn, etc) in parallel.
39333933
return prefix + str(os.getpid())
39343934

3935+
def test_shared_memory_name_with_embedded_null(self):
3936+
name_tsmb = self._new_shm_name('test01_null')
3937+
sms = shared_memory.SharedMemory(name_tsmb, create=True, size=512)
3938+
self.addCleanup(sms.unlink)
3939+
with self.assertRaises(ValueError):
3940+
shared_memory.SharedMemory(name_tsmb + '\0a', create=False, size=512)
3941+
if shared_memory._USE_POSIX:
3942+
orig_name = sms._name
3943+
try:
3944+
sms._name = orig_name + '\0a'
3945+
with self.assertRaises(ValueError):
3946+
sms.unlink()
3947+
finally:
3948+
sms._name = orig_name
3949+
39353950
def test_shared_memory_basics(self):
39363951
name_tsmb = self._new_shm_name('test01_tsmb')
39373952
sms = shared_memory.SharedMemory(name_tsmb, create=True, size=512)
@@ -4066,7 +4081,7 @@ def test_shared_memory_recreate(self):
40664081
self.addCleanup(shm2.unlink)
40674082
self.assertEqual(shm2._name, names[1])
40684083

4069-
def test_invalid_shared_memory_cration(self):
4084+
def test_invalid_shared_memory_creation(self):
40704085
# Test creating a shared memory segment with negative size
40714086
with self.assertRaises(ValueError):
40724087
sms_invalid = shared_memory.SharedMemory(create=True, size=-1)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix silent truncation of the name with an embedded null character in
2+
:class:`multiprocessing.shared_memory.SharedMemory`.

Modules/_multiprocessing/posixshmem.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@ _posixshmem_shm_open_impl(PyObject *module, PyObject *path, int flags,
4242
{
4343
int fd;
4444
int async_err = 0;
45-
const char *name = PyUnicode_AsUTF8(path);
45+
Py_ssize_t name_size;
46+
const char *name = PyUnicode_AsUTF8AndSize(path, &name_size);
4647
if (name == NULL) {
4748
return -1;
4849
}
50+
if (strlen(name) != (size_t)name_size) {
51+
PyErr_SetString(PyExc_ValueError, "embedded null character");
52+
return -1;
53+
}
4954
do {
5055
Py_BEGIN_ALLOW_THREADS
5156
fd = shm_open(name, flags, mode);
@@ -81,10 +86,15 @@ _posixshmem_shm_unlink_impl(PyObject *module, PyObject *path)
8186
{
8287
int rv;
8388
int async_err = 0;
84-
const char *name = PyUnicode_AsUTF8(path);
89+
Py_ssize_t name_size;
90+
const char *name = PyUnicode_AsUTF8AndSize(path, &name_size);
8591
if (name == NULL) {
8692
return NULL;
8793
}
94+
if (strlen(name) != (size_t)name_size) {
95+
PyErr_SetString(PyExc_ValueError, "embedded null character");
96+
return NULL;
97+
}
8898
do {
8999
Py_BEGIN_ALLOW_THREADS
90100
rv = shm_unlink(name);

0 commit comments

Comments
 (0)
0