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

Change inotify_watch event to security_path_notify #3913

Merged
merged 9 commits into from
Mar 19, 2024

Conversation

oshaked1
Copy link
Contributor

1. Explain what the PR does

Changed inotify_watch event to security_path_notify.
inotify_watch used the inotify_find_inode kernel function which is static and cannot be hooked on all kernels.
This replacement event hooks security_path_notify instead (using a kprobe), which is always available.
This function also handles notify requests from dnotify and fanotify, which are used for similar purposes.
The new event also includes the mask and object type fields,
which can be used to determine the exact characteristics of the watch.

2. Explain how to test it

Run tracee with --events security_path_notify, generate the event by registering a FS watch using any of the dnotify, inotify or fanotify APIs.

oshaked1 added 3 commits March 7, 2024 15:51
inotify_watch used the inotify_find_inode kernel function which is static and cannot be hooked on all kernels.
This replacement event hooks security_path_notify instead (using a kprobe), which is always available.
This function also handles notify requests from dnotify and fanotify, which are used for similar purposes.
The new event also includes the mask and object type fields,
which can be used to determine the exact characteristics of the watch.
@CLAassistant
Copy link

CLAassistant commented Mar 10, 2024

CLA assistant check
All committers have signed the CLA.

params: []trace.ArgMeta{
{Type: "const char*", Name: "pathname"},
{Type: "unsigned long", Name: "inode"},
{Type: "dev_t", Name: "dev"},
{Type: "u64", Name: "mask"},
Copy link
Contributor

Choose a reason for hiding this comment

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

You will need to support parsing this value, as it is a bit mask.
This is done in libbpfgo, you can have a look on a PR doing it, like the GUP flags parsing

@@ -12837,22 +12837,23 @@ var CoreEvents = map[ID]Definition{
},
},
},
InotifyWatch: {
id: InotifyWatch,
SecurityPathNotify: {
Copy link
Contributor

Choose a reason for hiding this comment

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

You will need to add a documentation page for the event.
The documentation pages reside in the "docs/docs/events/builtin/extra" directory.
Just copy the format.md file and fill it.

}

int main(void) {
mkdir_exist_ok(DNOTIFY_PATH);
Copy link
Contributor

Choose a reason for hiding this comment

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

Make sure to clean afterwards

@NDStrahilevitz
Copy link
Collaborator

@yanivagman Are we ok with straight up replacing the event and breaking users? I think we should start adding new events and deprecating older versions if needed instead as a practice.
Alternatively, we could switch the probes in the event to the new one, and not touch the event at all, besides adding the new argument.

@AlonZivony
Copy link
Contributor

@yanivagman Are we ok with straight up replacing the event and breaking users? I think we should start adding new events and deprecating older versions if needed instead as a practice. Alternatively, we could switch the probes in the event to the new one, and not touch the event at all, besides adding the new argument.

You are probably right.
We should keep the old event, and mark him as deprecated somehow.
Then we can in a future major release remove at once all deprecated mark events.

@oshaked1
Copy link
Contributor Author

@yanivagman Are we ok with straight up replacing the event and breaking users? I think we should start adding new events and deprecating older versions if needed instead as a practice. Alternatively, we could switch the probes in the event to the new one, and not touch the event at all, besides adding the new argument.

You are probably right. We should keep the old event, and mark him as deprecated somehow. Then we can in a future major release remove at once all deprecated mark events.

I added back the old event. How should it be marked as deprecated?

@yanivagman
Copy link
Collaborator

Since we're still in v0.x.y and the event is very specific and still new, I'm ok with removing it. In the future we should add the mechanism to mark events as deprecated

security_path_notify is now a separate event for backwards compatibility.
Copy link
Contributor

@AlonZivony AlonZivony left a comment

Choose a reason for hiding this comment

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

LGTM
A very high quality code :)

Comment on lines +283 to +295
case SecurityPathNotify:
if maskArg := GetArg(event, "mask"); maskArg != nil {
if mask, isUint64 := maskArg.Value.(uint64); isUint64 {
fsNotifyMaskArgument := helpers.ParseFsNotifyMask(mask)
parseOrEmptyString(maskArg, fsNotifyMaskArgument, nil)
}
}
if objTypeArg := GetArg(event, "obj_type"); objTypeArg != nil {
if objType, isUint := objTypeArg.Value.(uint32); isUint {
objTypeArgument, err := helpers.ParseFsNotifyObjType(uint64(objType))
parseOrEmptyString(objTypeArg, objTypeArgument, err)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please note that if you use a threat detection event which is based on this event you should not rely on the parsed arguments values but on the raw arguments values

Comment on lines 35 to 38
static void handle_dnotify_event(int sig, siginfo_t *si, void *ucontext)
{
return;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the use of that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Forgot to remove it, fixed.

Added should_trace() and should_submit() checks
and removed unused function from testing script.
Copy link
Collaborator

@yanivagman yanivagman left a comment

Choose a reason for hiding this comment

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

LGTM

@yanivagman yanivagman merged commit 084ad9e into aquasecurity:main Mar 19, 2024
32 checks passed
@oshaked1 oshaked1 deleted the fsnotify_event branch March 19, 2024 12:45
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.

6 participants