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: add separate API for ambient capabilities #165

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions capability/capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,18 @@ func NewFile2(path string) (Capabilities, error) {
func LastCap() (Cap, error) {
return lastCap()
}

// AmbientRaise raises specified ambient capabilities for the calling process.
func AmbientRaise(cap ...Cap) error {
return ambientRaise(cap...)
}

// AmbientLower lowers specified ambient capabilities for the calling process.
func AmbientLower(cap ...Cap) error {
return ambientLower(cap...)
}

// AmbientClearAll lowers all ambient capabilities for the calling process.
func AmbientClearAll() error {
return ambientClearAll()
}
65 changes: 43 additions & 22 deletions capability/capability_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ func newPid(pid int) (c Capabilities, retErr error) {
return
}

func ignoreEINVAL(err error) error {
if errors.Is(err, syscall.EINVAL) {
err = nil
}
return err
}

type capsV3 struct {
hdr capHeader
data [2]capData
Expand Down Expand Up @@ -327,7 +334,7 @@ func (c *capsV3) Load() (err error) {
return
}

func (c *capsV3) Apply(kind CapType) (err error) {
func (c *capsV3) Apply(kind CapType) error {
last, err := LastCap()
if err != nil {
return err
Expand All @@ -336,21 +343,17 @@ func (c *capsV3) Apply(kind CapType) (err error) {
var data [2]capData
err = capget(&c.hdr, &data[0])
if err != nil {
return
return err
}
if (1<<uint(CAP_SETPCAP))&data[0].effective != 0 {
for i := Cap(0); i <= last; i++ {
if c.Get(BOUNDING, i) {
continue
}
err = prctl(syscall.PR_CAPBSET_DROP, uintptr(i), 0, 0, 0)
// Ignore EINVAL since the capability may not be supported in this system.
err = ignoreEINVAL(prctl(syscall.PR_CAPBSET_DROP, uintptr(i), 0, 0, 0))
if err != nil {
// Ignore EINVAL since the capability may not be supported in this system.
kolyshkin marked this conversation as resolved.
Show resolved Hide resolved
if err == syscall.EINVAL { //nolint:errorlint // Errors from syscall are bare.
err = nil
continue
}
return
return err
}
}
}
Expand All @@ -359,33 +362,51 @@ func (c *capsV3) Apply(kind CapType) (err error) {
if kind&CAPS == CAPS {
err = capset(&c.hdr, &c.data[0])
if err != nil {
return
return err
}
}

if kind&AMBS == AMBS {
err = prctl(pr_CAP_AMBIENT, pr_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0)
if err != nil && err != syscall.EINVAL { //nolint:errorlint // Errors from syscall are bare.
// Ignore EINVAL as not supported on kernels before 4.3
return
// Ignore EINVAL as not supported on kernels before 4.3
err = ignoreEINVAL(ambientClearAll())
if err != nil {
return err
}
for i := Cap(0); i <= last; i++ {
if !c.Get(AMBIENT, i) {
continue
}
err = prctl(pr_CAP_AMBIENT, pr_CAP_AMBIENT_RAISE, uintptr(i), 0, 0)
// Ignore EINVAL as not supported on kernels before 4.3
err = ignoreEINVAL(ambientRaise(i))
if err != nil {
// Ignore EINVAL as not supported on kernels before 4.3
kolyshkin marked this conversation as resolved.
Show resolved Hide resolved
if err == syscall.EINVAL { //nolint:errorlint // Errors from syscall are bare.
err = nil
continue
}
return
return err
}
}
}

return
return nil
}

func setAmbient(op uintptr, cap ...Cap) error {
for _, val := range cap {
err := prctl(pr_CAP_AMBIENT, op, uintptr(val), 0, 0)
if err != nil {
return err
}
}
return nil
}

func ambientRaise(cap ...Cap) error {
return setAmbient(pr_CAP_AMBIENT_RAISE, cap...)
}

func ambientLower(cap ...Cap) error {
return setAmbient(pr_CAP_AMBIENT_LOWER, cap...)
}

func ambientClearAll() error {
return prctl(pr_CAP_AMBIENT, pr_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0)
}

func newFile(path string) (c Capabilities, err error) {
Expand Down
12 changes: 12 additions & 0 deletions capability/capability_noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ func newFile(_ string) (Capabilities, error) {
func lastCap() (Cap, error) {
return -1, errNotSup
}

func ambientRaise(cap ...Cap) error {
return errNotSup
}

func ambientLower(cap ...Cap) error {
return errNotSup
}

func ambientClearAll() error {
return errNotSup
}
71 changes: 70 additions & 1 deletion capability/capability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestNewPid2Load(t *testing.T) {
}
}

func TestAmbientCapSet(t *testing.T) {
func TestApplyCaps(t *testing.T) {
if runtime.GOOS != "linux" {
return
}
Expand Down Expand Up @@ -193,3 +193,72 @@ func childAmbientCapSet() {
}
os.Exit(0)
}

func TestAmbientCapAPI(t *testing.T) {
if runtime.GOOS != "linux" {
return
}
requirePCapSet(t)

out := testInChild(t, childAmbientCapSetByAPI)

t.Logf("output from child:\n%s", out)
}

func childAmbientCapSetByAPI() {
log.SetFlags(log.Lshortfile)

pid, err := NewPid2(0)
if err != nil {
log.Fatal(err)
}

list := []Cap{CAP_KILL, CAP_CHOWN, CAP_SYS_CHROOT}
pid.Set(CAPS|AMBIENT, list...)
if err = pid.Apply(CAPS); err != nil {
log.Fatal(err)
}
if err = AmbientRaise(list...); err != nil {
log.Fatal(err)
}

// Check if ambient caps were raised.
if err = pid.Load(); err != nil {
log.Fatal(err)
}
for _, cap := range list {
want := true
if got := pid.Get(AMBIENT, cap); want != got {
log.Fatalf("Get(AMBIENT, %s): want %v, got %v", cap, want, got)
}
}

// Lower one ambient cap.
const unsetIdx = 1
if err = AmbientLower(list[unsetIdx]); err != nil {
log.Fatal(err)
}
if err = pid.Load(); err != nil {
log.Fatal(err)
}
for i, cap := range list {
want := i != unsetIdx
if got := pid.Get(AMBIENT, cap); want != got {
log.Fatalf("Get(AMBIENT, %s): want %v, got %v", cap, want, got)
}
}

// Lower all ambient caps
if err = AmbientClearAll(); err != nil {
log.Fatal(err)
}
if err = pid.Load(); err != nil {
log.Fatal(err)
}
for _, cap := range list {
if got := pid.Get(AMBIENT, cap); got {
log.Fatalf("Get(AMBIENT, %s): want false, got %v", cap, got)
}
}
os.Exit(0)
}