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

capability: can't raise ambient and drop bounding caps for other process #171

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

lifubang
Copy link
Contributor

There may be two ways to fix #168:

  1. Return an error when pid is not 0;
  2. Do raise and drop operation when pid is 0.

I think we should choose the second way, because besides the bug fix,
we can also have a compatibility with before.

@kolyshkin
Copy link
Collaborator

There may be two ways to fix #168:

  1. Return an error when pid is not 0;
  2. Do raise and drop operation when pid is 0.

I think we should choose the second way, because besides the bug fix, we can also have a compatibility with before.

There is a compatibility issue either way. With the second way, the behavior of this package has changed:

  • it used to ignore the pid argument and set capabilities for the current process;
  • now it doesn't do that.

So, for some code which does, for example, NewPid(os.Getpid()), this will be a breaking change. What's worse, they won't know about it, as no error is returned.

So I think the first way is better. If someone is doing something wrong, it's better to return an error so they know they are doing it wrong, rather than silently stop doing something.

@lifubang lifubang force-pushed the fix-capa-bounding-ambient-other branch 3 times, most recently from 5f831f3 to a782713 Compare October 13, 2024 09:29
@lifubang
Copy link
Contributor Author

So I think the first way is better. If someone is doing something wrong, it's better to return an error so they know they are doing it wrong, rather than silently stop doing something.

Agree, I have changed to return an error.

Comment on lines 67 to 68
ErrBoundingNotMine = errors.New("not support drop bounding cap of other process")
ErrAmbientNotMine = errors.New("not support modify ambient cap of other process")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need these errors as part of a public API? If yes, why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe someone use it to check the error type, like what to do in the test?
But maybe no, so I'll change it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe someone use it to check the error type, like what to do in the test?

For the test case, you can add export_test.go file having something like this:

package capability

// Make some internal details available for tests.

var ErrBoundingNotMine = errBoundingNotMine

@lifubang lifubang force-pushed the fix-capa-bounding-ambient-other branch from d85f137 to 0b9a879 Compare October 15, 2024 23:33
Signed-off-by: lifubang <lifubang@acmcoder.com>
@lifubang lifubang force-pushed the fix-capa-bounding-ambient-other branch from 0b9a879 to f298c15 Compare October 15, 2024 23:36
}
requirePCapSet(t)

cmd := exec.Command("sleep", "sleep", "infinity")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this should be

Suggested change
cmd := exec.Command("sleep", "sleep", "infinity")
cmd := exec.Command("sleep", "infinity")

Copy link
Collaborator

Choose a reason for hiding this comment

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

The error went unnoticed because this test is not being run, because the previous test (TestAmbientCapSet) removes CAP_SET_PCAP from the test process. Oh well

Copy link
Collaborator

Choose a reason for hiding this comment

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

See here

Copy link
Collaborator

@kolyshkin kolyshkin left a comment

Choose a reason for hiding this comment

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

We have a big problem

=== RUN   TestAmbientCapSet
--- PASS: TestAmbientCapSet (0.00s)
=== RUN   TestApplyCapsForOtherProcess
    capability_test.go:159: The test needs `CAP_SETPCAP`.
--- SKIP: TestApplyCapsForOtherProcess (0.00s)

TestAmbientCapSet removes some capabilities from the binary, and thus all other tests that require CAP_SETPCAP (or root) won't work.

Glad we found it now before it's too late.

The solution here is to spawn such test cases in a separate binary, so it won't affect others.

I will come up with a patch.

Comment on lines +67 to +68
errBoundingNotMine = errors.New("not support drop bounding cap of other process")
errAmbientNotMine = errors.New("not support modify ambient cap of other process")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
errBoundingNotMine = errors.New("not support drop bounding cap of other process")
errAmbientNotMine = errors.New("not support modify ambient cap of other process")
errBoundingNotMine = errors.New("unable to modify bounding capabilities of another process")
errAmbientNotMine = errors.New("unable to modify ambient capabilities of another process")

}
err = pid.Apply(BOUNDING)
if !errors.Is(err, errBoundingNotMine) {
t.Fatalf("expected not support error when drop bounding caps for other process, but got: %v", err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
t.Fatalf("expected not support error when drop bounding caps for other process, but got: %v", err)
t.Fatalf("want error %v, got %v", errBoundingNotMine, err)

}
err = pid.Apply(AMBIENT)
if !errors.Is(err, errAmbientNotMine) {
t.Fatalf("expected not support error when rasing ambient caps for other process, but got: %v", err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
t.Fatalf("expected not support error when rasing ambient caps for other process, but got: %v", err)
t.Fatalf("want error %v, got %v", errAmbientNotMine, err)

package capability_test
package capability
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I have added this comment already but it looks like it is lost.

I deliberately changed this in commit 7208f83 so these tests can change public API only, and I don't like changing it back.

You have a few options here:

  1. Just check for an error.
  2. export errors (as described in capability: can't raise ambient and drop bounding caps for other process #171 (comment)).
  3. create a different test file which will have package capability and thus would be able to test and access internals.

I would prefer way 2.

@kolyshkin
Copy link
Collaborator

I will come up with a patch.

See #173.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug] capability: some errors related to pid
2 participants