Skip to content
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

fix(userspace/libsinsp): avoid a possible source of segfault in sinsp::next #1423

Merged
merged 3 commits into from
Oct 19, 2023

Conversation

FedeDP
Copy link
Contributor

@FedeDP FedeDP commented Oct 18, 2023

What type of PR is this?

/kind bug

Any specific area of the project related to this PR?

/area libsinsp

What this PR does / why we need it:

Avoid returning SCAP_SUCCESS without setting *puevt pointed data.

Which issue(s) this PR fixes:

Refs falcosecurity/falco#2878

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

fix(userspace/libsinsp): avoid possible source of segfault in sinsp::next

…nsp next.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
@@ -1223,6 +1223,8 @@ int32_t sinsp::next(OUT sinsp_evt **puevt)
sinsp_evt* evt;
int32_t res;

*puevt = NULL;
Copy link
Contributor Author

@FedeDP FedeDP Oct 18, 2023

Choose a reason for hiding this comment

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

Force set puevt to NULL at start of function.

@@ -1297,8 +1299,6 @@ int32_t sinsp::next(OUT sinsp_evt **puevt)
{
m_external_event_processor->process_event(NULL, libsinsp::EVENT_RETURN_TIMEOUT);
}
*puevt = NULL;
return res;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Avoid multiple returns here; just use the return at L1339 that was already there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, *puevt is now always NULL here.

}

for(uint32_t j = 0; j < nfdr; j++)
if(ptinfo)
Copy link
Contributor Author

@FedeDP FedeDP Oct 18, 2023

Choose a reason for hiding this comment

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

Here return res would return SCAP_SUCCESS but *puevt was not set. Instead, avoid dropping an event altogether, since here it was not even parsed/processed.

Copy link
Contributor

Choose a reason for hiding this comment

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

nice catch! :)

@FedeDP
Copy link
Contributor Author

FedeDP commented Oct 18, 2023

/hold

@FedeDP
Copy link
Contributor Author

FedeDP commented Oct 18, 2023

/cc @gnosek @Andreagit97

@poiana poiana requested review from Andreagit97 and gnosek October 18, 2023 08:10
@FedeDP
Copy link
Contributor Author

FedeDP commented Oct 18, 2023

/milestone 0.14.0

But of course this can become a 0.13.3 if needed.

@poiana poiana added this to the 0.14.0 milestone Oct 18, 2023
Copy link
Contributor

@gnosek gnosek 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 this, definitely a nice cleanup, though I'd go a step further. I'd change:

if(res != SCAP_SUCCESS)
{
    if(res == SCAP_FOO)
   {
   // ... and so on
   }

    return res;
}

// much much later
*puevt = evt;
return res;

to a switch:

*puevt = NULL;
switch(res)
{
    case SCAP_SUCCESS:
        *puevt = evt;
        break;

    case SCAP_FILTERED_EVENT:
        *puevt = evt; // we do have an event so why not, I guess?
        // ...
        return res;

    // etc.
}

// here res == SCAP_SUCCESS and *puevt is valid so we can return whenever

}

for(uint32_t j = 0; j < nfdr; j++)
if(ptinfo)
Copy link
Contributor

Choose a reason for hiding this comment

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

nice catch! :)

userspace/libsinsp/sinsp.cpp Show resolved Hide resolved
userspace/libsinsp/sinsp.cpp Outdated Show resolved Hide resolved
@FedeDP
Copy link
Contributor Author

FedeDP commented Oct 18, 2023

I like this, definitely a nice cleanup, though I'd go a step further. I'd change:

But then in this case #1423 (comment) we'd need to set *puevt to NULL if we want to return its correct (ie: non SCAP_SUCCESS related) value.
I'd prefer to keep it as is :)

@gnosek
Copy link
Contributor

gnosek commented Oct 18, 2023

I like this, definitely a nice cleanup, though I'd go a step further. I'd change:

But then in this case #1423 (comment) we'd need to set *puevt to NULL if we want to return its correct (ie: non SCAP_SUCCESS related) value. I'd prefer to keep it as is :)

... or just don't set it in the switch. Either way, I'm not going to fight too much over it :D

…o matching `ptinfo` is found.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>

Co-authored-by: Grzegorz Nosek <grzegorz.nosek@sysdig.com>
@FedeDP
Copy link
Contributor Author

FedeDP commented Oct 18, 2023

/unhold

…out.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Copy link
Member

@Andreagit97 Andreagit97 left a comment

Choose a reason for hiding this comment

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

/approve

@poiana
Copy link
Contributor

poiana commented Oct 19, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Andreagit97, FedeDP, jasondellaluce

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [Andreagit97,FedeDP,jasondellaluce]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@poiana poiana merged commit b48ca3f into master Oct 19, 2023
20 checks passed
@poiana poiana deleted the fix/avoid_segfault_sinsp_next branch October 19, 2023 15:32
@FedeDP FedeDP mentioned this pull request Oct 23, 2023
@FedeDP
Copy link
Contributor Author

FedeDP commented Oct 23, 2023

/milestone 0.13.3

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.

5 participants