Page MenuHomeFreeBSD

ktrace: Record cpuset violations with KTR_CAPFAIL
ClosedPublic

Authored by jfree on Jun 20 2023, 10:04 PM.
Referenced Files
Unknown Object (File)
Mon, Apr 8, 3:36 PM
Unknown Object (File)
Mon, Apr 8, 12:35 AM
Unknown Object (File)
Mar 13 2024, 7:28 PM
Unknown Object (File)
Dec 20 2023, 5:39 AM
Unknown Object (File)
Dec 10 2023, 6:27 PM
Unknown Object (File)
Oct 10 2023, 7:42 PM
Unknown Object (File)
Aug 27 2023, 2:50 PM
Unknown Object (File)
Aug 15 2023, 11:54 PM
Subscribers

Details

Summary
Report Capsicum violations in the cpuset namespace with CAPFAIL_CPUSET.

Programs that are not yet Capsicumized may be traced to discover
potential capability failures. With `ktrace -t p` and kdump, you
are given a list of syscalls as a starting point for program
Capsicumization.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

sys/kern/kern_cpuset.c
1803

Rather than duplicating all of these conditions, I'd suggest lifting them into a function bool cpuset_capmode_allowed(level, which, id) and call that from this function.

It feels wrong to return inside of a macro, but I did not see any comments about it in style(9). This seems to be the solution that is most elegant in minimizing code duplication. Let me know your thoughts.

Actually, scratch that. I just understood your comment and this is a better solution.

As a side note though... Is returning in macros usually frowned upon?

jfree marked an inline comment as done.Jan 7 2024, 8:24 AM

Actually, scratch that. I just understood your comment and this is a better solution.

As a side note though... Is returning in macros usually frowned upon?

I would frown at that. :) It introduces hidden control flow which makes it hard to see quickly what a function does.

Consider the common case where a function allocates some memory and is supposed to free it before returning: if I'm reviewing the code and want to verify it behaves properly with respect to that free() call, it's much easier to check if I just have to look for the "return" keyword in the function.

Once in a while it's handy, but it should be avoided if possible.

sys/kern/kern_cpuset.c
1793

Note that you've factored the caller such that cpuset_capmode_allowed() isn't really required now: you could inline it into the caller without introducing code duplication.

But I would keep this as it is: cpuset_capmode_allowed() is a pure function, and IMO it's good style to factor those out when possible. (cpuset_check_capabilities() itself is not a pure function because it may call ktrcapfail(), which logs to a ktrace buffer.)

This revision is now accepted and ready to land.Jan 8 2024, 2:55 PM

I would frown at that. :) It introduces hidden control flow which makes it hard to see quickly what a function does.

Consider the common case where a function allocates some memory and is supposed to free it before returning: if I'm reviewing the code and want to verify it behaves properly with respect to that free() call, it's much easier to check if I just have to look for the "return" keyword in the function.

Once in a while it's handy, but it should be avoided if possible.

Thanks for the explanation. These things seem obvious once you know them, but it is sometimes hard to reason if you don't. I think in most cases, it is possible to rearrange a function to remove a macro that returns anyway. That's what lead me to explore other solutions.