-
Notifications
You must be signed in to change notification settings - Fork 336
fix: double free in checkout_tree_at_recurse #3515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Hi @HastD. Thanks for your PR. I'm waiting for a ostreedev member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request correctly fixes a double-free vulnerability by changing the type of modified_xattrs from g_autoptr(GVariant) to a raw GVariant*. This prevents the automatic cleanup mechanism from attempting to free the same memory twice. The fix is applied consistently in both create_file_copy_from_input_at and checkout_tree_at_recurse. I've added a couple of suggestions to make the code slightly more concise by removing the temporary modified_xattrs variable.
|
Let's try to push this through soon |
|
Copilot reviews have been closed without comments, why use a temporary variable at all ? |
|
@champtar The called function has signature |
|
Nop that's ok with me, I'll leave it to someone else to merge if anyone has stronger opinions on this |
Not...quite. I believe there's a bug here, but my initial take is this patch is changing a double free to a memory leak. This merits deeper investigation. Let's look at this code pattern: A really important thing to understand is that in the C programming language, the assignment of It will print |
|
This pattern of "constant pointer and owned pointer" is used in many other places in the code. It is safe if used correctly - and again I could totally believe there's a bug here. (Insert obligatory "this wouldn't happen in Rust" thing and I am so looking forward to migrating most logic here to bootc/composefs which are all in Rust) It's going to take me a bit but let me investigate before we merge please (cc @champtar ). (Or of course anyone else can feel free to do so, but this just needs a careful analysis) |
|
What seems most likely here to me is us handling floating references with GVariant incorrectly; it wouldn't be the first time. The thing is floating references were created to make reference counting easier in C but that was before we popularized In a nutshell what can easily go wrong is when one passes a floating reference into a function which consumes it. The value of I tried to do some manual auditing here but yeah, high level of tedium, running an AI agent on this to do an audit, we'll see. |
|
TL;DR can you retest with reverting just the first hunk? |
This comment was marked as outdated.
This comment was marked as outdated.
|
I think the sole change we need is |
Both `xattrs` and `modified_xattrs` are declared with `g_autoptr`, but `xattrs` is later simply assigned to be equal to `modified_xattrs`, meaning the automatic cleanup is a double-free. This is fixed by instead using `g_steal_pointer` to assign the old value of `xattrs` to a temporary variable, which is used to create the new value. I believe this is the cause of issue ostreedev#3303, and this should fix ostreedev#3303. (I can consistently reproduce the issue by attempting to deploy a rechunked image with bootc, and with this patch, the issue no longer occurs and the deployment succeeds.) Signed-off-by: Daniel Hast <hast.daniel@protonmail.com>
3e53298 to
a7e2650
Compare
|
I just retested the revised version (which I've now pushed to this PR) in the same VM, and it does indeed fix the bug. Thanks @cgwalters for catching that memory leak in the earlier version. |
cgwalters
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for finally finding this bug!!
|
/override ci/prow/fcos-e2e |
|
@cgwalters: Overrode contexts on behalf of cgwalters: ci/prow/fcos-e2e DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
Both
xattrsandmodified_xattrsare declared withg_autoptr, butxattrsis later simply assigned to be equal tomodified_xattrs, meaning the automatic cleanup is a double-free.This is fixed by instead using
g_steal_pointerto assign the old value ofxattrsto a temporary variable, which is used to create the new value.I believe this is the cause of issue #3303, and this should fix #3303. (I can consistently reproduce the issue by attempting to deploy a rechunked image with bootc, and with this patch, the issue no longer occurs and the deployment succeeds.)