Skip to content

Conversation

@Fidget-Spinner
Copy link
Member

Co-Authored-By: chilaxan [email protected]
Fixes #92888

@Fidget-Spinner Fidget-Spinner added needs backport to 3.10 only security fixes needs backport to 3.11 only security fixes labels May 19, 2022
@Fidget-Spinner Fidget-Spinner added type-crash A hard crash of the interpreter, possibly with a core dump needs backport to 3.8 and removed needs backport to 3.7 type-security A security issue labels May 19, 2022
@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented May 23, 2022

I have other approach, but your approach is more compatible, so it is safer to use it backports. I'll experiment with other approach in the developing branch only.

You can use the following test as a base:

    def test_use_released_memory(self):
        size = 128
        def release():
            m.release()
            nonlocal ba
            ba = bytearray(size)
        class MyIndex:
            def __index__(self):
                release()
                return 4
        class MyFloat:
            def __float__(self):
                release()
                return 4.25
        class MyBool:
            def __bool__(self):
                release()
                return True

        ba = None
        m = memoryview(bytearray(b'\xff'*size))
        with self.assertRaises(ValueError):
            m[MyIndex()]

        ba = None
        m = memoryview(bytearray(b'\xff'*size))
        self.assertEqual(list(m[:MyIndex()]), [255] * 4)

        ba = None
        m = memoryview(bytearray(b'\xff'*size))
        self.assertEqual(list(m[MyIndex():8]), [255] * 4)

        ba = None
        m = memoryview(bytearray(b'\xff'*size))
        m[MyIndex()] = 42
        self.assertEqual(ba[:8], b'\0'*8)

        ba = None
        m = memoryview(bytearray(b'\xff'*size))
        m[:MyIndex()] = b'spam'
        self.assertEqual(ba[:8], b'\0'*8)

        ba = None
        m = memoryview(bytearray(b'\xff'*size))
        m[MyIndex():8] = b'spam'
        self.assertEqual(ba[:8], b'\0'*8)

        ba = None
        m = memoryview(bytearray(b'\xff'*size))
        m[0] = MyIndex()
        self.assertEqual(ba[:8], b'\0'*8)

        for fmt in 'bhilqnBHILQN':
            ba = None
            m = memoryview(bytearray(b'\xff'*size)).cast(fmt)
            m[0] = MyIndex()
            self.assertEqual(ba[:8], b'\0'*8)

        for fmt in 'fd':
            ba = None
            m = memoryview(bytearray(b'\xff'*size)).cast(fmt)
            m[0] = MyFloat()
            self.assertEqual(ba[:8], b'\0'*8)

        ba = None
        m = memoryview(bytearray(b'\xff'*size)).cast('?')
        m[0] = MyBool()
        self.assertEqual(ba[:8], b'\0'*8)

@Fidget-Spinner
Copy link
Member Author

Fidget-Spinner commented May 23, 2022

@serhiy-storchaka thanks taking the time to review and writing those really comprehensive tests. One part of your test fails on my branch: specifically m[MyIndex()] = 42 raises ValueError. That's supposed to happen right? (Actually, most of the asserts seem to fail on my branch :()

@serhiy-storchaka
Copy link
Member

Yes, it is expected that some lines raise exceptions with your changes. Just add assertRaises() if needed.

@Fidget-Spinner
Copy link
Member Author

Great, thanks to your tests I found that my fix didn't cover slices. It now does.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yet few tests.

Fidget-Spinner and others added 2 commits May 23, 2022 21:49
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@serhiy-storchaka
Copy link
Member

I am sorry for being so pedantic. 🤦‍♂️

@Fidget-Spinner
Copy link
Member Author

I am sorry for being so pedantic. man_facepalming

I didn't feel that you were. Those are errors I ought to have caught anyways. Thanks for the detailed review as always.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your approach to fix memory_ass_sub(). Here is a first review.

copy_single(const Py_buffer *dest, const Py_buffer *src)
copy_single(PyMemoryViewObject *self, const Py_buffer *dest, const Py_buffer *src)
{
CHECK_RELEASED_INT(self); /* See gh-92888 for why we need this here */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the check after the equiv_structure() test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would not access to Py_buffer of the released object cause reading after free?

@@ -0,0 +1,2 @@
Fix ``memoryview`` use after free when accessing the backing buffer in certain cases.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to mention more explicitly that the protection is about released views:

If a memoryview is released while getting or setting an item, raise an exception.
For example, converting an object to an index can execute arbitrary Python
code which can indirectly release the memoryview.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not always an exception is raised.

The bug was in reading or wring the freed memory. Now it is prevented -- you either get an exception or free the memory after reading. @Fidget-Spinner's description is more correct.

I am going to address such inconsistency in a separate issue.

def release():
m.release()
nonlocal ba
ba = bytearray(size)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's useless, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we need it for tests below that tests indexing into ba[].

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We allocate a bytearray of the same size as the bytearray just released in memoryview in hope that it will be allocated at the same memory. It helps to check that we do nor read/write a freed memory.

return True

ba = None
m = memoryview(bytearray(b'\xff'*size))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my PR, I tried to make the code more generic to test more cases: https://github.com/python/cpython/pull/93127/files#diff-d41c6bb40a1e03fea5a20d15c4077413e0ddde65651147922b625b03a66a2f16R399:

        tp = self.rw_type
        b = self.rw_type(self._source)
        view = self._view(b)

ba = None
m = memoryview(bytearray(b'\xff'*size))
with self.assertRaises(ValueError):
m[MyIndex()]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is very long. Can you try to factorize similar code and use loop with subTest(), and put pack operations in one test method and unpack in another test method?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we will need to duplicate the definitions of internal classes.

The tested code is so different, that it is difficult to use a loop. And I think that the result will be more complicated.

with self.assertRaises(ValueError):
m[MyIndex()]

ba = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is necessary. The old bytearray should be deallocated before creating a new bytearray, so all bytearrays will be allocated at the same place.

@vstinner
Copy link
Member

I closed my PR #93127 since this one seems to be more complete.

@Fidget-Spinner
Copy link
Member Author

@vstinner do you have any major objections? Otherwise, I plan to merge.

@vstinner
Copy link
Member

My reviews on the tests were just remarks, not objections.

@Fidget-Spinner Fidget-Spinner merged commit 11190c4 into python:main Jun 17, 2022
@miss-islington
Copy link
Contributor

Thanks @Fidget-Spinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.11.
🐍🍒⛏🤖

@Fidget-Spinner Fidget-Spinner deleted the fix_memoryview_auf branch June 17, 2022 15:14
@miss-islington
Copy link
Contributor

Sorry, @Fidget-Spinner, I could not cleanly backport this to 3.10 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 11190c4ad0d3722b8d263758ac802985131a5462 3.10

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Jun 17, 2022
@bedevere-bot
Copy link

GH-93948 is a backport of this pull request to the 3.11 branch.

@bedevere-bot
Copy link

GH-93950 is a backport of this pull request to the 3.10 branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-crash A hard crash of the interpreter, possibly with a core dump

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use After Free when assigning into a memoryview

5 participants