|
| 1 | +package selinux |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/opencontainers/selinux/internal/impl" |
| 5 | +) |
| 6 | + |
| 7 | +const ( |
| 8 | + // Enforcing constant indicate SELinux is in enforcing mode |
| 9 | + Enforcing = impl.Enforcing |
| 10 | + // Permissive constant to indicate SELinux is in permissive mode |
| 11 | + Permissive = impl.Permissive |
| 12 | + // Disabled constant to indicate SELinux is disabled |
| 13 | + Disabled = impl.Disabled |
| 14 | + |
| 15 | + // DefaultCategoryRange is the default upper bound on the category range. |
| 16 | + // See [SetCategoryRange]. |
| 17 | + DefaultCategoryRange = impl.DefaultCategoryRange |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + // ErrMCSAlreadyExists is returned when trying to allocate a duplicate MCS. |
| 22 | + ErrMCSAlreadyExists = impl.ErrMCSAlreadyExists |
| 23 | + // ErrEmptyPath is returned when an empty path has been specified. |
| 24 | + ErrEmptyPath = impl.ErrEmptyPath |
| 25 | + |
| 26 | + // ErrInvalidLabel is returned when an invalid label is specified. |
| 27 | + ErrInvalidLabel = impl.ErrInvalidLabel |
| 28 | + |
| 29 | + // ErrIncomparable is returned two levels are not comparable |
| 30 | + ErrIncomparable = impl.ErrIncomparable |
| 31 | + // ErrLevelSyntax is returned when a sensitivity or category do not have correct syntax in a level |
| 32 | + ErrLevelSyntax = impl.ErrLevelSyntax |
| 33 | + |
| 34 | + // ErrContextMissing is returned if a requested context is not found in a file. |
| 35 | + ErrContextMissing = impl.ErrContextMissing |
| 36 | + // ErrVerifierNil is returned when a context verifier function is nil. |
| 37 | + ErrVerifierNil = impl.ErrVerifierNil |
| 38 | + |
| 39 | + // ErrNotTGLeader is returned by [SetKeyLabel] if the calling thread |
| 40 | + // is not the thread group leader. |
| 41 | + ErrNotTGLeader = impl.ErrNotTGLeader |
| 42 | +) |
| 43 | + |
| 44 | +// Context is a representation of the SELinux label broken into 4 parts |
| 45 | +type Context = impl.Context |
| 46 | + |
| 47 | +// SetDisabled disables SELinux support for the package |
| 48 | +func SetDisabled() { |
| 49 | + impl.SetDisabled() |
| 50 | +} |
| 51 | + |
| 52 | +// GetEnabled returns whether SELinux is currently enabled. |
| 53 | +func GetEnabled() bool { |
| 54 | + return impl.GetEnabled() |
| 55 | +} |
| 56 | + |
| 57 | +// SetCategoryRange allows to adjust the upper bound of the category range. |
| 58 | +// It affects subsequent calls to [KVMContainerLabel] and [InitContainerLabel]. |
| 59 | +func SetCategoryRange(upper uint32) error { |
| 60 | + return impl.SetCategoryRange(upper) |
| 61 | +} |
| 62 | + |
| 63 | +// ClassIndex returns the int index for an object class in the loaded policy, |
| 64 | +// or -1 and an error |
| 65 | +func ClassIndex(class string) (int, error) { |
| 66 | + return impl.ClassIndex(class) |
| 67 | +} |
| 68 | + |
| 69 | +// SetFileLabel sets the SELinux label for this path, following symlinks, |
| 70 | +// or returns an error. |
| 71 | +func SetFileLabel(fpath string, label string) error { |
| 72 | + return impl.SetFileLabel(fpath, label) |
| 73 | +} |
| 74 | + |
| 75 | +// LsetFileLabel sets the SELinux label for this path, not following symlinks, |
| 76 | +// or returns an error. |
| 77 | +func LsetFileLabel(fpath string, label string) error { |
| 78 | + return impl.LsetFileLabel(fpath, label) |
| 79 | +} |
| 80 | + |
| 81 | +// FileLabel returns the SELinux label for this path, following symlinks, |
| 82 | +// or returns an error. |
| 83 | +func FileLabel(fpath string) (string, error) { |
| 84 | + return impl.FileLabel(fpath) |
| 85 | +} |
| 86 | + |
| 87 | +// LfileLabel returns the SELinux label for this path, not following symlinks, |
| 88 | +// or returns an error. |
| 89 | +func LfileLabel(fpath string) (string, error) { |
| 90 | + return impl.LfileLabel(fpath) |
| 91 | +} |
| 92 | + |
| 93 | +// SetFSCreateLabel tells the kernel what label to use for all file system objects |
| 94 | +// created by this task. |
| 95 | +// Set the label to an empty string to return to the default label. Calls to SetFSCreateLabel |
| 96 | +// should be wrapped in runtime.LockOSThread()/runtime.UnlockOSThread() until file system |
| 97 | +// objects created by this task are finished to guarantee another goroutine does not migrate |
| 98 | +// to the current thread before execution is complete. |
| 99 | +func SetFSCreateLabel(label string) error { |
| 100 | + return impl.SetFSCreateLabel(label) |
| 101 | +} |
| 102 | + |
| 103 | +// FSCreateLabel returns the default label the kernel which the kernel is using |
| 104 | +// for file system objects created by this task. "" indicates default. |
| 105 | +func FSCreateLabel() (string, error) { |
| 106 | + return impl.ReadConThreadSelf("attr/fscreate") |
| 107 | +} |
| 108 | + |
| 109 | +// CurrentLabel returns the SELinux label of the current process thread, or an error. |
| 110 | +func CurrentLabel() (string, error) { |
| 111 | + return impl.ReadConThreadSelf("attr/current") |
| 112 | +} |
| 113 | + |
| 114 | +// PidLabel returns the SELinux label of the given pid, or an error. |
| 115 | +func PidLabel(pid int) (string, error) { |
| 116 | + return impl.PidLabel(pid) |
| 117 | +} |
| 118 | + |
| 119 | +// ExecLabel returns the SELinux label that the kernel will use for any programs |
| 120 | +// that are executed by the current process thread, or an error. |
| 121 | +func ExecLabel() (string, error) { |
| 122 | + return impl.ReadConThreadSelf("attr/exec") |
| 123 | +} |
| 124 | + |
| 125 | +// CanonicalizeContext takes a context string and writes it to the kernel |
| 126 | +// the function then returns the context that the kernel will use. Use this |
| 127 | +// function to check if two contexts are equivalent |
| 128 | +func CanonicalizeContext(val string) (string, error) { |
| 129 | + return impl.CanonicalizeContext(val) |
| 130 | +} |
| 131 | + |
| 132 | +// ComputeCreateContext requests the type transition from source to target for |
| 133 | +// class from the kernel. |
| 134 | +func ComputeCreateContext(source string, target string, class string) (string, error) { |
| 135 | + return impl.ComputeCreateContext(source, target, class) |
| 136 | +} |
| 137 | + |
| 138 | +// CalculateGlbLub computes the glb (greatest lower bound) and lub (least upper bound) |
| 139 | +// of a source and target range. |
| 140 | +// The glblub is calculated as the greater of the low sensitivities and |
| 141 | +// the lower of the high sensitivities and the and of each category bitset. |
| 142 | +func CalculateGlbLub(sourceRange, targetRange string) (string, error) { |
| 143 | + return impl.CalculateGlbLub(sourceRange, targetRange) |
| 144 | +} |
| 145 | + |
| 146 | +// SetExecLabel sets the SELinux label that the kernel will use for any programs |
| 147 | +// that are executed by the current process thread, or an error. Calls to SetExecLabel |
| 148 | +// should be wrapped in runtime.LockOSThread()/runtime.UnlockOSThread() until execution |
| 149 | +// of the program is finished to guarantee another goroutine does not migrate to the current |
| 150 | +// thread before execution is complete. |
| 151 | +func SetExecLabel(label string) error { |
| 152 | + return impl.WriteConThreadSelf("attr/exec", label) |
| 153 | +} |
| 154 | + |
| 155 | +// SetTaskLabel sets the SELinux label for the current thread, or an error. |
| 156 | +// This requires the dyntransition permission. Calls to SetTaskLabel should |
| 157 | +// be wrapped in runtime.LockOSThread()/runtime.UnlockOSThread() to guarantee |
| 158 | +// the current thread does not run in a new mislabeled thread. |
| 159 | +func SetTaskLabel(label string) error { |
| 160 | + return impl.WriteConThreadSelf("attr/current", label) |
| 161 | +} |
| 162 | + |
| 163 | +// SetSocketLabel takes a process label and tells the kernel to assign the |
| 164 | +// label to the next socket that gets created. Calls to SetSocketLabel |
| 165 | +// should be wrapped in runtime.LockOSThread()/runtime.UnlockOSThread() until |
| 166 | +// the socket is created to guarantee another goroutine does not migrate |
| 167 | +// to the current thread before execution is complete. |
| 168 | +func SetSocketLabel(label string) error { |
| 169 | + return impl.WriteConThreadSelf("attr/sockcreate", label) |
| 170 | +} |
| 171 | + |
| 172 | +// SocketLabel retrieves the current socket label setting |
| 173 | +func SocketLabel() (string, error) { |
| 174 | + return impl.ReadConThreadSelf("attr/sockcreate") |
| 175 | +} |
| 176 | + |
| 177 | +// PeerLabel retrieves the label of the client on the other side of a socket |
| 178 | +func PeerLabel(fd uintptr) (string, error) { |
| 179 | + return impl.PeerLabel(fd) |
| 180 | +} |
| 181 | + |
| 182 | +// SetKeyLabel takes a process label and tells the kernel to assign the |
| 183 | +// label to the next kernel keyring that gets created. |
| 184 | +// |
| 185 | +// Calls to SetKeyLabel should be wrapped in |
| 186 | +// runtime.LockOSThread()/runtime.UnlockOSThread() until the kernel keyring is |
| 187 | +// created to guarantee another goroutine does not migrate to the current |
| 188 | +// thread before execution is complete. |
| 189 | +// |
| 190 | +// Only the thread group leader can set key label. |
| 191 | +func SetKeyLabel(label string) error { |
| 192 | + return impl.SetKeyLabel(label) |
| 193 | +} |
| 194 | + |
| 195 | +// KeyLabel retrieves the current kernel keyring label setting |
| 196 | +func KeyLabel() (string, error) { |
| 197 | + return impl.KeyLabel() |
| 198 | +} |
| 199 | + |
| 200 | +// NewContext creates a new Context struct from the specified label |
| 201 | +func NewContext(label string) (Context, error) { |
| 202 | + return impl.NewContext(label) |
| 203 | +} |
| 204 | + |
| 205 | +// ClearLabels clears all reserved labels |
| 206 | +func ClearLabels() { |
| 207 | + impl.ClearLabels() |
| 208 | +} |
| 209 | + |
| 210 | +// ReserveLabel reserves the MLS/MCS level component of the specified label. |
| 211 | +// |
| 212 | +// Deprecated: use [ReserveLabelV2] instead. |
| 213 | +func ReserveLabel(label string) { |
| 214 | + _ = impl.ReserveLabel(label) |
| 215 | +} |
| 216 | + |
| 217 | +// ReserveLabelV2 reserves the MLS/MCS level component of the specified label. |
| 218 | +// Returns an error if the label can't be reserved. |
| 219 | +func ReserveLabelV2(label string) error { |
| 220 | + return impl.ReserveLabel(label) |
| 221 | +} |
| 222 | + |
| 223 | +// CheckLabel check the MLS/MCS level component of the specified label |
| 224 | +func CheckLabel(label string) error { |
| 225 | + return impl.CheckLabel(label) |
| 226 | +} |
| 227 | + |
| 228 | +// MLSEnabled checks if MLS is enabled. |
| 229 | +func MLSEnabled() bool { |
| 230 | + return impl.MLSEnabled() |
| 231 | +} |
| 232 | + |
| 233 | +// EnforceMode returns the current SELinux mode (one of [Enforcing], |
| 234 | +// [Permissive], or [Disabled]). |
| 235 | +func EnforceMode() int { |
| 236 | + return impl.EnforceMode() |
| 237 | +} |
| 238 | + |
| 239 | +// SetEnforceMode sets the current SELinux mode Enforcing, Permissive. |
| 240 | +// Disabled is not valid, since this needs to be set at boot time. |
| 241 | +func SetEnforceMode(mode int) error { |
| 242 | + return impl.SetEnforceMode(mode) |
| 243 | +} |
| 244 | + |
| 245 | +// DefaultEnforceMode returns the systems default SELinux mode Enforcing, |
| 246 | +// Permissive or Disabled. Note this is just the default at boot time. |
| 247 | +// EnforceMode tells you the systems current mode. |
| 248 | +func DefaultEnforceMode() int { |
| 249 | + return impl.DefaultEnforceMode() |
| 250 | +} |
| 251 | + |
| 252 | +// ReleaseLabel un-reserves the MLS/MCS Level field of the specified label, |
| 253 | +// allowing it to be used by another process. |
| 254 | +func ReleaseLabel(label string) { |
| 255 | + impl.ReleaseLabel(label) |
| 256 | +} |
| 257 | + |
| 258 | +// ROFileLabel returns the specified SELinux readonly file label. |
| 259 | +// |
| 260 | +// Deprecated: this (apparently) has no users and will be removed from the |
| 261 | +// future version of this package. Open a bug report if you use it. |
| 262 | +func ROFileLabel() string { |
| 263 | + return impl.ROFileLabel() |
| 264 | +} |
| 265 | + |
| 266 | +// KVMContainerLabels returns the default processLabel and mountLabel to be used |
| 267 | +// for kvm containers by the calling process. |
| 268 | +// |
| 269 | +// Deprecated: use [KVMContainerLabel] instead. |
| 270 | +func KVMContainerLabels() (string, string) { |
| 271 | + return impl.KVMContainerLabels() |
| 272 | +} |
| 273 | + |
| 274 | +// KVMContainerLabel returns the default process label to be used |
| 275 | +// for KVM containers by the calling process. |
| 276 | +func KVMContainerLabel() (string, error) { |
| 277 | + return impl.KVMContainerLabel() |
| 278 | +} |
| 279 | + |
| 280 | +// InitContainerLabels returns the default processLabel and file labels to be |
| 281 | +// used for containers running an init system like systemd by the calling process. |
| 282 | +// |
| 283 | +// Deprecated: use [InitContainerLabel] instead. |
| 284 | +func InitContainerLabels() (string, string) { |
| 285 | + return impl.InitContainerLabels() |
| 286 | +} |
| 287 | + |
| 288 | +// InitContainerLabel returns the default process label to be used |
| 289 | +// for containers running an init system like systemd by the calling process. |
| 290 | +func InitContainerLabel() (string, error) { |
| 291 | + return impl.InitContainerLabel() |
| 292 | +} |
| 293 | + |
| 294 | +// ContainerLabels returns an allocated processLabel and fileLabel to be used for |
| 295 | +// container labeling by the calling process. |
| 296 | +// |
| 297 | +// Deprecated: this (apparently) has no users and will be removed from the |
| 298 | +// future version of this package. Open a bug report if you use it. |
| 299 | +func ContainerLabels() (processLabel string, fileLabel string) { |
| 300 | + return impl.ContainerLabels() |
| 301 | +} |
| 302 | + |
| 303 | +// SecurityCheckContext validates that the SELinux label is understood by the kernel |
| 304 | +func SecurityCheckContext(val string) error { |
| 305 | + return impl.SecurityCheckContext(val) |
| 306 | +} |
| 307 | + |
| 308 | +// CopyLevel returns a label with the MLS/MCS level from src label replaced on |
| 309 | +// the dest label. |
| 310 | +func CopyLevel(src, dest string) (string, error) { |
| 311 | + return impl.CopyLevel(src, dest) |
| 312 | +} |
| 313 | + |
| 314 | +// Chcon changes the fpath file object to the SELinux label. |
| 315 | +// If fpath is a directory and recurse is true, then Chcon walks the |
| 316 | +// directory tree setting the label. |
| 317 | +// |
| 318 | +// The fpath itself is guaranteed to be relabeled last. |
| 319 | +func Chcon(fpath string, label string, recurse bool) error { |
| 320 | + return impl.Chcon(fpath, label, recurse) |
| 321 | +} |
| 322 | + |
| 323 | +// DupSecOpt takes an SELinux process label and returns security options that |
| 324 | +// can be used to set the SELinux Type and Level for future container processes. |
| 325 | +func DupSecOpt(src string) ([]string, error) { |
| 326 | + return impl.DupSecOpt(src) |
| 327 | +} |
| 328 | + |
| 329 | +// DisableSecOpt returns a security opt that can be used to disable SELinux |
| 330 | +// labeling support for future container processes. |
| 331 | +func DisableSecOpt() []string { |
| 332 | + return []string{"disable"} |
| 333 | +} |
| 334 | + |
| 335 | +// SEUserByName retrieves the SELinux username and security level for a given |
| 336 | +// Linux username. The username and security level is based on the |
| 337 | +// /etc/selinux/{SELINUXTYPE}/seusers file. |
| 338 | +func SEUserByName(username string) (seUser string, level string, err error) { |
| 339 | + return impl.SEUserByName(username) |
| 340 | +} |
| 341 | + |
| 342 | +// GetDefaultContextWithLevel gets a single context for the specified SELinux user |
| 343 | +// identity that is reachable from the specified scon context. The context is based |
| 344 | +// on the per-user /etc/selinux/{SELINUXTYPE}/contexts/users/<username> if it exists, |
| 345 | +// and falls back to the global /etc/selinux/{SELINUXTYPE}/contexts/default_contexts |
| 346 | +// file and finally the global /etc/selinux/{SELINUXTYPE}/contexts/failsafe_context |
| 347 | +// file if no match can be found anywhere else. |
| 348 | +func GetDefaultContextWithLevel(user, level, scon string) (string, error) { |
| 349 | + return impl.GetDefaultContextWithLevel(user, level, scon) |
| 350 | +} |
| 351 | + |
| 352 | +// PrivContainerMountLabel returns mount label for privileged containers |
| 353 | +func PrivContainerMountLabel() string { |
| 354 | + return impl.PrivContainerMountLabel() |
| 355 | +} |
0 commit comments