Skip to content

Conversation

@pradishmp
Copy link
Contributor

@pradishmp pradishmp commented Jul 3, 2024

Fixes #8351

What changed ?
This is a corner case scenario where there is a potential for a memory leak. For this leak to occur, two things must happen:

  1. The arguments passed to the service should be wide char strings.
  2. The function call toMBString, which is used to convert the wide char strings to multibyte strings, should fail, returning a nullptr.

When a nullptr is returned, as part of cleaning up the heap memory allocated, the cleanArgs() function is called. However, this function will only clean up the memory if the Boolean variable owns_argv_ptrs is set to true. In this case, as the variable is still false, the function will only remove all elements from the vector.

Later, when the Boolean variable owns_argv_ptrs is set to true and the destructor gets a chance to clean up, it cannot do anything as the elements are already removed and the vector size is zero.

Therefore, the fix is to remove the call to the function clearArgs() and leave it to the destructor to clean up the memory.

@pradishmp pradishmp requested review from a team as code owners July 3, 2024 14:40
@zwass
Copy link
Member

zwass commented Jul 3, 2024

Reopen for CI fix

@zwass zwass closed this Jul 3, 2024
@zwass zwass reopened this Jul 3, 2024
Copy link
Member

@directionless directionless left a comment

Choose a reason for hiding this comment

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

I think this is okay -- there's a destructer that clears the args. @Smjert @zwass either of you?

@michael-myers
Copy link
Contributor

I think this is okay -- there's a destructer that clears the args. @Smjert @zwass either of you?

Both before and after this PR, the same private method, cleanArgs will free the memory, only, it needs to be called only after owns_argv_ptrs_ = true;. Another way to fix this, maybe how I would do it, would be to set owns_argv_ptrs_ = true; immediately after the CommandLineToArgvW returns successfully. So, before the for loop instead of right after it. But this PR also fixes the issue.

@pradishmp
Copy link
Contributor Author

i was trying to remove some of the older commits which are not related to this issue..ended up closing the PR, Apologies for the inconvenience caused. i can create a new PR and submit for review , please let me know your thoughts

@pradishmp
Copy link
Contributor Author

I think this is okay -- there's a destructer that clears the args. @Smjert @zwass either of you?

Another way to fix this, maybe how I would do it, would be to set owns_argv_ptrs_ = true; immediately after the CommandLineToArgvW returns successfully. So, before the for loop instead of right after it.

i too agree with this approach, freeing memory immediately rather than wait for the destructor also make sence,

@Smjert
Copy link
Member

Smjert commented Jul 4, 2024

i was trying to remove some of the older commits which are not related to this issue..ended up closing the PR, Apologies for the inconvenience caused. i can create a new PR and submit for review , please let me know your thoughts

Feel free to open a new PR, I suggest not using the master branch on your repo to submit the PR from, but a separate PR specific branch. This way you can track osquery master branch, work on your bugfix branch and also sync it against master with a rebase (don't use merge or be aware that git pull by default merges if it cannot do a fast forward merge, which can be changed).

@directionless
Copy link
Member

@pradishmp We are likely to cut a release in the next few days, I don't know if the timing will line up on your next PR, but there will always be another release.

@pradishmp
Copy link
Contributor Author

@pradishmp We are likely to cut a release in the next few days, I don't know if the timing will line up on your next PR, but there will always be another release.

ok i will see if i can fix this today itself or we can always have this in next release, whichever works best for the product.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential memory leak in class ServiceArgumentParser's Constructor

5 participants