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

Update(prlimit&setrlimit): Add resource arg for exit event #1348

Merged
merged 7 commits into from
Nov 21, 2023

Conversation

Rohith-Raju
Copy link
Contributor

@Rohith-Raju Rohith-Raju commented Sep 12, 2023

What type of PR is this?

/kind feature

Any specific area of the project related to this PR?

/area driver-kmod

/area driver-bpf

/area driver-modern-bpf

/area tests

What this PR does / why we need it:
Adding the resource argument to the exit events of setrlimit and prlimit will detect and provide useful alerts.

Which issue(s) this PR fixes:

Fixes #1327

Special notes for your reviewer:

Does this PR introduce a user-facing change?: No

Update(`prlimit`&`setrlimit`):  Add `resource` arg for exit event

@github-actions
Copy link

github-actions bot commented Sep 12, 2023

Please double check driver/API_VERSION file. See versioning.

/hold

@Rohith-Raju Rohith-Raju marked this pull request as draft September 12, 2023 09:29
@Rohith-Raju Rohith-Raju force-pushed the prlimit branch 4 times, most recently from b8e9130 to bef3ef4 Compare September 13, 2023 07:45
@Rohith-Raju Rohith-Raju marked this pull request as ready for review September 13, 2023 07:45
@poiana poiana requested a review from LucaGuerra September 13, 2023 07:45
@Andreagit97
Copy link
Member

Thank you for this, we will take a look soon! :)

@Andreagit97 Andreagit97 added this to the 0.14.0 milestone Sep 15, 2023
@Rohith-Raju Rohith-Raju force-pushed the prlimit branch 3 times, most recently from a630808 to b5ccaca Compare October 17, 2023 11:33
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.

Thank you for this! Left some comments
@Biagio-Dipalma after this PR it would be possible to access the resource field in setrlimit and prlimit syscalls through evt.arg.resource. Does it sound good to you?

Copy link
Member

Choose a reason for hiding this comment

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

Here we need a minor, so 2.13.0.
Moreover, we need also a major bump in the API_VERSION since we removed some old fillers

/*
* Copy the user structure and extract cur and max
*/
if(retval >= 0 || data->state->tail_ctx.evt_type == PPME_SYSCALL_SETRLIMIT_X)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if(retval >= 0 || data->state->tail_ctx.evt_type == PPME_SYSCALL_SETRLIMIT_X)
if(retval >= 0)

Copy link
Member

Choose a reason for hiding this comment

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

Same in the setrlimit filler, now that we split them there is no more need for this if

max = -1;
}

/* Parameter 2: cur (type: PT_ERRNO) */
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/* Parameter 2: cur (type: PT_ERRNO) */
/* Parameter 2: cur (type: PT_INT64) */

Copy link
Member

Choose a reason for hiding this comment

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

Same in all other places

CHECK_RES(res);

/* Parameter 4: resource (type: PT_ERRNO) */
unsigned long resource = bpf_syscall_get_argument(data, 0);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
unsigned long resource = bpf_syscall_get_argument(data, 0);
uint32_t resource = bpf_syscall_get_argument(data, 0);

Copy link
Member

Choose a reason for hiding this comment

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

Same in all other drivers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have a doubt: #1348 (comment)

driver/bpf/fillers.h Show resolved Hide resolved
*/
if(retval >= 0 || args->event_type == PPME_SYSCALL_SETRLIMIT_X)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if(retval >= 0 || args->event_type == PPME_SYSCALL_SETRLIMIT_X)
if(retval >= 0)

res = val_to_ring(args, newcur, 0, false, 0);
CHECK_RES(res);
CHECK_RES(res);
Copy link
Member

Choose a reason for hiding this comment

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

probably we can remove this, same below

@@ -68,6 +68,10 @@ int BPF_PROG(setrlimit_x,
/* Parameter 3: max (type: PT_INT64)*/
ringbuf__store_s64(&ringbuf, rl.rlim_max);

/* Parameter 4: resource (type: PT_ENUMFLAGS8) */
unsigned long resource = extract__syscall_argument(regs, 0);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
unsigned long resource = extract__syscall_argument(regs, 0);
uint32_t resource = extract__syscall_argument(regs, 0);

Copy link
Member

Choose a reason for hiding this comment

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

we could add a test both in setrlimit and prlimit in which we use a valid resource, for example RLIMIT_MEMLOCK, and an invalid pointer to a struct rlimit, so something like syscall(__NR_setrlimit, RLIMIT_MEMLOCK, NULL), WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, Sounds good!!


val = bpf_syscall_get_argument(data, 1);
if(bpf_probe_read_user(&rl, sizeof(rl), (void *)val))
return PPM_FAILURE_INVALID_USER_MEMORY;
Copy link
Member

Choose a reason for hiding this comment

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

Instead of returning this code probably here we want to return cur = 0; and max = 0; See the modern bpf implementation. Same for the kmod

Copy link
Member

Choose a reason for hiding this comment

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

We can do something like this

		struct rlimit rl = {0};
		val = bpf_syscall_get_argument(data, 1);
		bpf_probe_read_user(&rl, sizeof(rl), (void *)val);
		cur = rl.rlim_cur;
		max = rl.rlim_max;

instead of returning PPM_FAILURE_INVALID_USER_MEMORY if we fail in bpf_probe_read_user

@Andreagit97
Copy link
Member

The PR is great, thank you!
Since I was there I just added a commit to increase consistency between drivers, feel free to revert it if you don't like it :)

@poiana poiana added size/XXL and removed size/XL labels Nov 21, 2023
Copy link
Member

Choose a reason for hiding this comment

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

unfortunately, due to an inconsistency (#1283) we need a new major API version every time we add a new filler in the old probe

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Andreagit97 Understood!!

Copy link
Contributor

@FedeDP FedeDP 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 Nov 21, 2023

LGTM label has been added.

Git tree hash: 1730c644404b29a94e3f7e48a796c58cf6938783

@poiana
Copy link
Contributor

poiana commented Nov 21, 2023

[APPROVALNOTIFIER] This PR is APPROVED

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

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 [FedeDP,jasondellaluce]

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

@FedeDP
Copy link
Contributor

FedeDP commented Nov 21, 2023

/unhold

@poiana poiana merged commit 4e9a3cc into falcosecurity:master Nov 21, 2023
38 checks passed
@Rohith-Raju Rohith-Raju deleted the prlimit branch December 11, 2023 09:19
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.

Update args in prlimit exit event
5 participants