Skip to contentSkip to conversation

possible use-after-free in amdgpu_dm

DISCUSSION Tom St Denis 5 participants 12 messages Oct 17, 2017 9 messages not in the archive

lore
Share & tools
lore
#0 lore raw

In this block of code:

void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)

{
	struct dm_connector_state *state =
		to_dm_connector_state(connector->state);

	kfree(state);

	state = kzalloc(sizeof(*state), GFP_KERNEL);

The value of state is never compared with NULL and moreso the value of connector->state is never written to if NULL. Wouldn't this mean the pointer points to freed memory?

Cheers,
Tom
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): f6968925-fe34-901e-1d86-32680620bcb0-5C7GfCeVMHo@public.gmane.org

#2 lore raw

Am 17.10.2017 um 16:10 schrieb Tom St Denis:

Quoted 12 lines

In this block of code:

void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)

{
    struct dm_connector_state *state =
        to_dm_connector_state(connector->state);

    kfree(state);

    state = kzalloc(sizeof(*state), GFP_KERNEL);
Quoted 1 line

The value of state is never compared with NULL and moreso the value of connector->state is never written to if NULL. Wouldn't this mean the pointer points to freed memory?

Why should we compare the value of state to NULL? What's done here is just to get the size of the type state points to.

Not sure if that is really covered by the C standard, but in practice it works fine even when state is NULL.

Christian.

Quoted 7 lines

Cheers,
Tom
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 3bd6a5e2-2c00-95f1-cfc0-828c03ecb028-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#4 lore raw
Quoted 10 lines · Christian König

Am 17.10.2017 um 16:10 schrieb Tom St Denis:

Quoted 9 lines

In this block of code:

void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector) {
    struct dm_connector_state *state = to_dm_connector_state(connector->state);

    kfree(state);

Quoted 8 lines
Quoted 4 lines

    state = kzalloc(sizeof(*state), GFP_KERNEL);

The value of state is never compared with NULL and moreso the value of connector->state is never written to if NULL. Wouldn't this mean the pointer points to freed memory?

Why should we compare the value of state to NULL? What's done here is just to get the size of the type state points to.

Not sure if that is really covered by the C standard, but in practice it works fine even when state is NULL.

Hi Christian,

Oh sorry no the issue isn't with the sizeof (that's perfectly fine since the standard says the pointer won't be dereferenced).

The issue is later on in the function there's this statement:

		connector->state = &state->base;

Where "base" is first item in the struct so it's effectively just "connector->state = &state".

This means that the value of "connector->state" is stale since the pointer was kfree'ed right (if the alloc fails) which could lead to a use-after-free error (I don't know where this function lies in the rest of the code paths but it seems wrong either way).

Tom
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): ce79890c-6d35-61f7-b3e4-fba9d004bafa-5C7GfCeVMHo@public.gmane.org

#6 lore raw
Quoted 25 lines · Tom St Denis
Quoted 10 lines · Christian König

Am 17.10.2017 um 16:10 schrieb Tom St Denis:

Quoted 9 lines

In this block of code:

void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector) {
    struct dm_connector_state *state = to_dm_connector_state(connector->state);

    kfree(state);

Quoted 8 lines
Quoted 4 lines

    state = kzalloc(sizeof(*state), GFP_KERNEL);

The value of state is never compared with NULL and moreso the value of connector->state is never written to if NULL. Wouldn't this mean the pointer points to freed memory?

Why should we compare the value of state to NULL? What's done here is just to get the size of the type state points to.

Not sure if that is really covered by the C standard, but in practice it works fine even when state is NULL.

Hi Christian,

Oh sorry no the issue isn't with the sizeof (that's perfectly fine since the standard says the pointer won't be dereferenced).

Quoted 7 lines

The issue is later on in the function there's this statement:

        connector->state = &state->base;

Where "base" is first item in the struct so it's effectively just "connector->state = &state".

This means that the value of "connector->state" is stale since the pointer was kfree'ed right (if the alloc fails) which could lead to a use-after-free error (I don't know where this function lies in the rest of the code paths but it seems wrong either way).

Sorry I think I might be explaining this poorly.

In the case the alloc succeeds the pointer is updated and everything is fine.

IF the alloc fails the pointer (connector->state) is not updated and the value points to freed memory.

Tom
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 152711af-98ec-1dde-6456-58a7ed0b887a-5C7GfCeVMHo@public.gmane.org

#8 lore raw
Quoted 32 lines · Tom St Denis
Quoted 31 lines · Tom St Denis
Quoted 10 lines · Christian König

Am 17.10.2017 um 16:10 schrieb Tom St Denis:

Quoted 9 lines

In this block of code:

void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)

{
    struct dm_connector_state *state =
        to_dm_connector_state(connector->state);

    kfree(state);
Quoted 8 lines
Quoted 4 lines
    state = kzalloc(sizeof(*state), GFP_KERNEL);

The value of state is never compared with NULL and moreso the value of connector->state is never written to if NULL. Wouldn't this mean the pointer points to freed memory?

Why should we compare the value of state to NULL? What's done here is just to get the size of the type state points to.

Not sure if that is really covered by the C standard, but in practice it works fine even when state is NULL.

Hi Christian,

Oh sorry no the issue isn't with the sizeof (that's perfectly fine since the standard says the pointer won't be dereferenced).

The issue is later on in the function there's this statement:

         connector->state = &state->base;

Where "base" is first item in the struct so it's effectively just "connector->state = &state".

Quoted 8 lines
Quoted 1 line

This means that the value of "connector->state" is stale since the pointer was kfree'ed right (if the alloc fails) which could lead to a use-after-free error (I don't know where this function lies in the rest of the code paths but it seems wrong either way).

Sorry I think I might be explaining this poorly.

In the case the alloc succeeds the pointer is updated and everything is fine.

IF the alloc fails the pointer (connector->state) is not updated and the value points to freed memory.

Indeed an issue, there should be a big WARN_ON or error here for such a case. In general this hook is called from drm_mode_config_reset which is used to reset SW and HW states on loading or resetting due to GPU reset or resume from suspend. I think we only use it on load.

Andrey

Quoted 6 lines

Tom
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 728b99a2-35f5-31f9-a73f-d6df2d94c6eb-5C7GfCeVMHo@public.gmane.org

#10 lore raw
Quoted 38 lines · Andrey Grodzovsky
Quoted 35 lines · Tom St Denis
Quoted 32 lines · Tom St Denis
Quoted 10 lines · Christian König

Am 17.10.2017 um 16:10 schrieb Tom St Denis:

Quoted 9 lines

In this block of code:

void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector) {
    struct dm_connector_state *state = to_dm_connector_state(connector->state);

    kfree(state);

Quoted 8 lines
Quoted 4 lines

    state = kzalloc(sizeof(*state), GFP_KERNEL);

The value of state is never compared with NULL and moreso the value of connector->state is never written to if NULL. Wouldn't this mean the pointer points to freed memory?

Why should we compare the value of state to NULL? What's done here is just to get the size of the type state points to.

Not sure if that is really covered by the C standard, but in practice it works fine even when state is NULL.

Hi Christian,

Oh sorry no the issue isn't with the sizeof (that's perfectly fine since the standard says the pointer won't be dereferenced).

The issue is later on in the function there's this statement:

         connector->state = &state->base;

Where "base" is first item in the struct so it's effectively just "connector->state = &state".

This means that the value of "connector->state" is stale since the pointer was kfree'ed right (if the alloc fails) which could lead to a use-after-free error (I don't know where this function lies in the rest of the code paths but it seems wrong either way).

Quoted 7 lines
Quoted 5 lines

Sorry I think I might be explaining this poorly.

In the case the alloc succeeds the pointer is updated and everything is fine.

IF the alloc fails the pointer (connector->state) is not updated and the value points to freed memory.

Indeed an issue, there should be a big WARN_ON or error here for such a case. In general this hook is called from drm_mode_config_reset which is used to reset SW and HW states on loading or resetting due to GPU reset or resume from suspend. I think we only use it on load.

I'm not sure if that's a WARN_ON or simply a BUG_ON since I don't get how the dm layer continues at this point.

Alternatively, we could put a WARN_ON and set the pointer to NULL and let it handle it higher in the call stack.

Would you like me to draft a commit for the latter?

Cheers,
Tom
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): a89ae0a1-86f9-02b9-e4a3-801cfbb0fdb4-5C7GfCeVMHo@public.gmane.org

#12 lore raw
Quoted 45 lines · Tom St Denis
Quoted 44 lines · Andrey Grodzovsky
Quoted 40 lines · Tom St Denis
Quoted 25 lines · Tom St Denis
Quoted 10 lines · Christian König

Am 17.10.2017 um 16:10 schrieb Tom St Denis:

Quoted 9 lines

In this block of code:

void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)

{
    struct dm_connector_state *state =
        to_dm_connector_state(connector->state);

    kfree(state);
Quoted 8 lines
Quoted 4 lines
    state = kzalloc(sizeof(*state), GFP_KERNEL);

The value of state is never compared with NULL and moreso the value of connector->state is never written to if NULL. Wouldn't this mean the pointer points to freed memory?

Why should we compare the value of state to NULL? What's done here is just to get the size of the type state points to.

Not sure if that is really covered by the C standard, but in practice it works fine even when state is NULL.

Hi Christian,

Oh sorry no the issue isn't with the sizeof (that's perfectly fine since the standard says the pointer won't be dereferenced).

Quoted 7 lines

The issue is later on in the function there's this statement:

         connector->state = &state->base;

Where "base" is first item in the struct so it's effectively just "connector->state = &state".

This means that the value of "connector->state" is stale since the pointer was kfree'ed right (if the alloc fails) which could lead to a use-after-free error (I don't know where this function lies in the rest of the code paths but it seems wrong either way).

Sorry I think I might be explaining this poorly.

In the case the alloc succeeds the pointer is updated and everything is fine.

IF the alloc fails the pointer (connector->state) is not updated and the value points to freed memory.

Quoted 8 lines
Quoted 1 line

Indeed an issue, there should be a big WARN_ON or error here for such a case. In general this hook is called from drm_mode_config_reset which is used to reset SW and HW states on loading or resetting due to GPU reset or resume from suspend. I think we only use it on load.

I'm not sure if that's a WARN_ON or simply a BUG_ON since I don't get how the dm layer continues at this point.

Alternatively, we could put a WARN_ON and set the pointer to NULL and let it handle it higher in the call stack.

Would you like me to draft a commit for the latter?

IMHO It's more of a BUG_ON situation, this failure is not recoverable since the framework assumes a connector has a fresh state to start working on.

Andrey

Quoted 3 lines

Cheers,
Tom

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#13 lore raw
In reply to a message not in the archive
Quoted 20 lines · Tom St Denis
Quoted 11 lines · Tom St Denis
Quoted 10 lines · Christian König

Am 17.10.2017 um 16:10 schrieb Tom St Denis:

Quoted 9 lines

In this block of code:

void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector) {
    struct dm_connector_state *state = to_dm_connector_state(connector->state);

    kfree(state);

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

    state = kzalloc(sizeof(*state), GFP_KERNEL);

The value of state is never compared with NULL and moreso the value of connector->state is never written to if NULL. Wouldn't this mean the pointer points to freed memory?

Quoted 7 lines

Sorry I think I might be explaining this poorly.

In the case the alloc succeeds the pointer is updated and everything is fine.

IF the alloc fails the pointer (connector->state) is not updated and the value points to freed memory.

I'm wondering why the function frees, and then reallocates the memory. Does its size change? If not, why not just memset it to 0?

Regards,
  Felix

Quoted 6 lines

Tom
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 27bba3a3-1f03-fa58-95a7-72069822e412-5C7GfCeVMHo@public.gmane.org

#15 lore raw
Quoted 21 lines · Felix Kuehling
Quoted 20 lines · Tom St Denis
Quoted 9 lines · Tom St Denis
Quoted 8 lines · Christian König

Am 17.10.2017 um 16:10 schrieb Tom St Denis:

Quoted 7 lines

In this block of code:

void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector) {
struct dm_connector_state *state =
to_dm_connector_state(connector->state);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
     kfree(state);

     state = kzalloc(sizeof(*state), GFP_KERNEL);

The value of state is never compared with NULL and moreso the value of connector->state is never written to if NULL. Wouldn't this mean the pointer points to freed memory?

Sorry I think I might be explaining this poorly.

Quoted 7 lines
Quoted 5 lines

In the case the alloc succeeds the pointer is updated and everything is fine.

IF the alloc fails the pointer (connector->state) is not updated and the value points to freed memory.

I'm wondering why the function frees, and then reallocates the memory. Does its size change? If not, why not just memset it to 0?

I don't have an answer for why not just memset, this hook is a an older relic and not implemented properly, dm_drm_plane_reset is a more recent example of how it should be implemented (calling atomic_destroy_state hook inside) and i think we do kfree there just because DRM's helpers (e.g. drm_atomic_helper_plane_destroy_state) do the same.

Thanks,
Andrey

Quoted 9 lines

Regards,
Felix

Quoted 5 lines

Tom
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#16 lore raw
In reply to a message not in the archive

Am 17.10.2017 um 19:58 schrieb Felix Kuehling:

Quoted 21 lines · Tom St Denis
Quoted 20 lines · Tom St Denis
Quoted 9 lines · Christian König
Quoted 8 lines

Am 17.10.2017 um 16:10 schrieb Tom St Denis:

Quoted 7 lines

In this block of code:

void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector) {
struct dm_connector_state *state =
to_dm_connector_state(connector->state);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
     kfree(state);

     state = kzalloc(sizeof(*state), GFP_KERNEL);

The value of state is never compared with NULL and moreso the value of connector->state is never written to if NULL. Wouldn't this mean the pointer points to freed memory?

Sorry I think I might be explaining this poorly.

Quoted 7 lines
Quoted 5 lines

In the case the alloc succeeds the pointer is updated and everything is fine.

IF the alloc fails the pointer (connector->state) is not updated and the value points to freed memory.

I'm wondering why the function frees, and then reallocates the memory. Does its size change? If not, why not just memset it to 0?

Yeah, I was just thinking the same thing.

And no from the code snippet Tom posted it just frees and reallocated the same structure.

I think that should rather look like this:

if (!state)

     state = kzalloc(...);
else
    memset(...);

Regards,
Christian.

Quoted 9 lines

Regards,
Felix

Quoted 5 lines

Tom
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): eb41bb8c-acb1-40ed-4cf1-57d143292e52-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#18 lore raw
Quoted 32 lines · Christian König

Am 17.10.2017 um 19:58 schrieb Felix Kuehling:

Quoted 21 lines · Tom St Denis
Quoted 20 lines · Tom St Denis
Quoted 9 lines · Christian König
Quoted 8 lines

Am 17.10.2017 um 16:10 schrieb Tom St Denis:

Quoted 7 lines

In this block of code:

void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector) {
     struct dm_connector_state *state = to_dm_connector_state(connector->state);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

     kfree(state);

     state = kzalloc(sizeof(*state), GFP_KERNEL);

The value of state is never compared with NULL and moreso the value of connector->state is never written to if NULL. Wouldn't this mean the pointer points to freed memory?

Sorry I think I might be explaining this poorly.

Quoted 7 lines
Quoted 5 lines

In the case the alloc succeeds the pointer is updated and everything is fine.

IF the alloc fails the pointer (connector->state) is not updated and the value points to freed memory.

I'm wondering why the function frees, and then reallocates the memory. Does its size change? If not, why not just memset it to 0?

Yeah, I was just thinking the same thing.

Quoted 9 lines

And no from the code snippet Tom posted it just frees and reallocated the same structure.

I think that should rather look like this:

if (!state)
     state = kzalloc(...);
else
    memset(...);

I don't expect connector->state to ever be NULL here which seems to be confirmed by code that would have blown up if it ever was. How about we do a BUG_ON on this condition as has been suggested?

Otherwise I like the memset idea. No need for free/alloc here.

Harry

Quoted 18 lines

Regards,
Christian.

Quoted 9 lines

Regards,
   Felix

Quoted 5 lines

Tom
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 5262112b-476e-71b4-5253-eb1d66a02a10-5C7GfCeVMHo@public.gmane.org

#20 lore raw
Quoted 36 lines · Harry Wentland
Quoted 35 lines · Christian König

Am 17.10.2017 um 19:58 schrieb Felix Kuehling:

Quoted 21 lines · Tom St Denis
Quoted 20 lines · Tom St Denis
Quoted 9 lines · Christian König
Quoted 8 lines

Am 17.10.2017 um 16:10 schrieb Tom St Denis:

Quoted 7 lines

In this block of code:

void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector) {
struct dm_connector_state *state =
to_dm_connector_state(connector->state);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
      kfree(state);

      state = kzalloc(sizeof(*state), GFP_KERNEL);

The value of state is never compared with NULL and moreso the value of connector->state is never written to if NULL. Wouldn't this mean the pointer points to freed memory?

Sorry I think I might be explaining this poorly.

Quoted 7 lines
Quoted 5 lines

In the case the alloc succeeds the pointer is updated and everything is fine.

IF the alloc fails the pointer (connector->state) is not updated and the value points to freed memory.

I'm wondering why the function frees, and then reallocates the memory. Does its size change? If not, why not just memset it to 0?

Yeah, I was just thinking the same thing.

And no from the code snippet Tom posted it just frees and reallocated the same structure.

I think that should rather look like this:

Quoted 8 lines
Quoted 5 lines

if (!state)

      state = kzalloc(...);
else
     memset(...);

I don't expect connector->state to ever be NULL here which seems to be confirmed by code that would have blown up if it ever was. How about we do a BUG_ON on this condition as has been suggested?

Tom already sent a patch for that and i rb it.

Quoted 2 lines

Otherwise I like the memset idea. No need for free/alloc here.

I suggest first to align this hook with the dm_plane and dm_crtc hooks.

Thanks,
Andrey

Quoted 19 lines

Harry

Quoted 16 lines

Regards,
Christian.

Quoted 12 lines

Regards,
Felix

Quoted 5 lines

Tom
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Quoted 8 lines
Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx