Skip to content

Commit

Permalink
capability: add some api for ambient cap
Browse files Browse the repository at this point in the history
Signed-off-by: lifubang <lifubang@acmcoder.com>
  • Loading branch information
lifubang committed Oct 13, 2024
1 parent f99b7fb commit d3949d0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
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()
}
26 changes: 24 additions & 2 deletions capability/capability_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ func (c *capsV3) Apply(kind CapType) error {
}

if kind&AMBS == AMBS {
err = ignoreEINVAL(prctl(pr_CAP_AMBIENT, pr_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0))
// Ignore EINVAL as not supported on kernels before 4.3
err = ignoreEINVAL(ambientClearAll())
if err != nil {
return err
}
Expand All @@ -377,7 +377,7 @@ func (c *capsV3) Apply(kind CapType) error {
continue
}
// Ignore EINVAL as not supported on kernels before 4.3
err = ignoreEINVAL(prctl(pr_CAP_AMBIENT, pr_CAP_AMBIENT_RAISE, uintptr(i), 0, 0))
err = ignoreEINVAL(ambientRaise(i))
if err != nil {
return err
}
Expand All @@ -387,6 +387,28 @@ func (c *capsV3) Apply(kind CapType) error {
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_RAISE, 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) {
c = &capsFile{path: path}
return
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
}

0 comments on commit d3949d0

Please sign in to comment.