Some checks failed
gokrazy CI / CI (macos-latest) (push) Has been cancelled
gokrazy CI / CI (ubuntu-latest) (push) Has been cancelled
gokrazy CI / CI (windows-latest) (push) Has been cancelled
125 lines
3.1 KiB
Go
125 lines
3.1 KiB
Go
// Package cap
|
|
// Copyright (c) 2019-21 Andrew G. Morgan <morgan@kernel.org>
|
|
//
|
|
// The cap and psx packages are licensed with a (you choose) BSD
|
|
// 3-clause or GPL2. See LICENSE file for details.
|
|
// [the Fully Capable site]: https://sites.google.com/site/fullycapable/
|
|
package cap
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
)
|
|
|
|
// Value is the type of a single capability (or permission) bit.
|
|
type Value uint
|
|
|
|
// Flag is the type of one of the three Value dimensions held in a
|
|
// Set. It is also used in the (*IAB).Fill() method for changing the
|
|
// Bounding and Ambient Vectors.
|
|
type Flag uint
|
|
|
|
// Effective, Permitted, Inheritable are the three Flags of Values
|
|
// held in a Set.
|
|
const (
|
|
Effective Flag = iota
|
|
Permitted
|
|
Inheritable
|
|
)
|
|
|
|
// String identifies a Flag value by its conventional "e", "p" or "i"
|
|
// string abbreviation.
|
|
func (f Flag) String() string {
|
|
switch f {
|
|
case Effective:
|
|
return "e"
|
|
case Permitted:
|
|
return "p"
|
|
case Inheritable:
|
|
return "i"
|
|
default:
|
|
return "<Error>"
|
|
}
|
|
}
|
|
|
|
// data holds a 32-bit slice of the compressed bitmaps of capability
|
|
// sets as understood by the kernel.
|
|
type data [Inheritable + 1]uint32
|
|
|
|
// Set is an opaque capabilities container for a set of system
|
|
// capbilities. It holds individually addressable capability Value's
|
|
// for the three capability Flag's. See GetFlag() and SetFlag() for
|
|
// how to adjust them individually, and Clear() and ClearFlag() for
|
|
// how to do bulk operations.
|
|
//
|
|
// For admin tasks associated with managing namespace specific file
|
|
// capabilities, Set can also support a namespace-root-UID value which
|
|
// defaults to zero. See GetNSOwner() and SetNSOwner().
|
|
type Set struct {
|
|
// mu protects all other members of a Set.
|
|
mu sync.RWMutex
|
|
|
|
// flat holds Flag Value bitmaps for all capabilities
|
|
// associated with this Set.
|
|
flat []data
|
|
|
|
// Linux specific
|
|
nsRoot int
|
|
}
|
|
|
|
// Various known kernel magic values.
|
|
const (
|
|
kv1 = 0x19980330 // First iteration of process capabilities (32 bits).
|
|
kv2 = 0x20071026 // First iteration of process and file capabilities (64 bits) - deprecated.
|
|
kv3 = 0x20080522 // Most recently supported process and file capabilities (64 bits).
|
|
)
|
|
|
|
var (
|
|
// startUp protects setting of the following values: magic,
|
|
// words, maxValues.
|
|
startUp sync.Once
|
|
|
|
// magic holds the preferred magic number for the kernel ABI.
|
|
magic uint32
|
|
|
|
// words holds the number of uint32's associated with each
|
|
// capability Flag for this session.
|
|
words int
|
|
|
|
// maxValues holds the number of bit values that are named by
|
|
// the running kernel. This is generally expected to match
|
|
// ValueCount which is autogenerated at packaging time.
|
|
maxValues uint
|
|
)
|
|
|
|
type header struct {
|
|
magic uint32
|
|
pid int32
|
|
}
|
|
|
|
// defines from uapi/linux/prctl.h
|
|
const (
|
|
prCapBSetRead = 23
|
|
prCapBSetDrop = 24
|
|
)
|
|
|
|
// NewSet returns an empty capability set.
|
|
func NewSet() *Set {
|
|
startUp.Do(cInit)
|
|
return &Set{
|
|
flat: make([]data, words),
|
|
}
|
|
}
|
|
|
|
// ErrBadSet indicates a nil pointer was used for a *Set, or the
|
|
// request of the Set is invalid in some way.
|
|
var ErrBadSet = errors.New("bad capability set")
|
|
|
|
// good confirms that c looks valid.
|
|
func (c *Set) good() error {
|
|
if c == nil || len(c.flat) == 0 {
|
|
return ErrBadSet
|
|
}
|
|
return nil
|
|
}
|