Skip to contentSkip to conversation

Memory leak in transfer_memory_block()?

DISCUSSION Markus Armbruster 2 participants 3 messages Jun 18, 2020 – Jun 22, 2020

lore
Share & tools
lore
#0 lore raw

We appear to leak an Error object when ga_read_sysfs_file() fails with errno != ENOENT unless caller passes true @sys2memblk:

    static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memblk,
                                      GuestMemoryBlockResponse *result,
                                      Error **errp)
    {
        [...]
        if (local_err) {

We have an Error object.

            /* treat with sysfs file that not exist in old kernel */
            if (errno == ENOENT) {

Case 1: ENOENT; we free it. Good.

                error_free(local_err);
                if (sys2memblk) {
                    mem_blk->online = true;
                    mem_blk->can_offline = false;
                } else if (!mem_blk->online) {
                    result->response =
                        GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
                }
            } else {

Case 2: other than ENOENT

                if (sys2memblk) {

Case 2a: sys2memblk; we pass it to the caller. Good.

                    error_propagate(errp, local_err);
                } else {

Case 2b: !sys2memblk; ???

                    result->response =
                        GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
                }
            }
            goto out2;
        }
        [...]
    out2:
        g_free(status);
        close(dirfd);
    out1:
        if (!sys2memblk) {
            result->has_error_code = true;
            result->error_code = errno;
        }
    }

What is supposed to be done with @local_err in case 2b?

#1 lore raw
Quoted 39 lines

-----Original Message-----
From: Markus Armbruster [mailto:[email protected]]
Sent: Thursday, June 18, 2020 1:36 PM
To: Zhanghailiang <[email protected]>
Cc: [email protected]; Michael Roth <[email protected]>
Subject: Memory leak in transfer_memory_block()?

We appear to leak an Error object when ga_read_sysfs_file() fails with errno != ENOENT unless caller passes true @sys2memblk:

static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool
sys2memblk,
GuestMemoryBlockResponse
*result,

                                      Error **errp)
    {
        [...]
        if (local_err) {

We have an Error object.

            /* treat with sysfs file that not exist in old kernel */
            if (errno == ENOENT) {

Case 1: ENOENT; we free it. Good.

                error_free(local_err);
                if (sys2memblk) {
                    mem_blk->online = true;
                    mem_blk->can_offline = false;
                } else if (!mem_blk->online) {
                    result->response =
GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
                }
            } else {

Case 2: other than ENOENT

Quoted 9 lines
                if (sys2memblk) {

Case 2a: sys2memblk; we pass it to the caller. Good.

                    error_propagate(errp, local_err);
                } else {

Case 2b: !sys2memblk; ???

Good catch! I think we should pass the error info back to the caller, Let's record this error for debug when it happens.

Quoted 19 lines
                    result->response =
GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
                }
            }
            goto out2;
        }
        [...]
    out2:
        g_free(status);
        close(dirfd);
    out1:
        if (!sys2memblk) {
            result->has_error_code = true;
            result->error_code = errno;
        }
    }

What is supposed to be done with @local_err in case 2b?

#2 lore raw
Quoted 43 lines · Zhanghailiang
Quoted 43 lines

-----Original Message-----
From: Markus Armbruster [mailto:[email protected]]
Sent: Thursday, June 18, 2020 1:36 PM
To: Zhanghailiang <[email protected]>
Cc: [email protected]; Michael Roth <[email protected]>
Subject: Memory leak in transfer_memory_block()?

We appear to leak an Error object when ga_read_sysfs_file() fails with errno != ENOENT unless caller passes true @sys2memblk:

static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool
sys2memblk,
GuestMemoryBlockResponse
*result,

                                      Error **errp)
    {
        [...]
        if (local_err) {

We have an Error object.

            /* treat with sysfs file that not exist in old kernel */
            if (errno == ENOENT) {

Case 1: ENOENT; we free it. Good.

                error_free(local_err);
                if (sys2memblk) {
                    mem_blk->online = true;
                    mem_blk->can_offline = false;
                } else if (!mem_blk->online) {
                    result->response =
GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
                }
            } else {

Case 2: other than ENOENT

                if (sys2memblk) {

Case 2a: sys2memblk; we pass it to the caller. Good.

Quoted 8 lines
Quoted 5 lines
                    error_propagate(errp, local_err);
                } else {

Case 2b: !sys2memblk; ???

Good catch! I think we should pass the error info back to the caller, Let's record this error for debug when it happens.

I'll post a minimal fix to plug the leak, cc'ing you. I'll gladly replace it by a patch that does more. Thanks!

Quoted 19 lines
Quoted 19 lines
                    result->response =
GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
                }
            }
            goto out2;
        }
        [...]
    out2:
        g_free(status);
        close(dirfd);
    out1:
        if (!sys2memblk) {
            result->has_error_code = true;
            result->error_code = errno;
        }
    }

What is supposed to be done with @local_err in case 2b?