label.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package label
  2. import (
  3. "fmt"
  4. "github.com/opencontainers/selinux/go-selinux"
  5. )
  6. // Deprecated: use selinux.ROFileLabel
  7. var ROMountLabel = selinux.ROFileLabel
  8. // SetProcessLabel takes a process label and tells the kernel to assign the
  9. // label to the next program executed by the current process.
  10. // Deprecated: use selinux.SetExecLabel
  11. var SetProcessLabel = selinux.SetExecLabel
  12. // ProcessLabel returns the process label that the kernel will assign
  13. // to the next program executed by the current process. If "" is returned
  14. // this indicates that the default labeling will happen for the process.
  15. // Deprecated: use selinux.ExecLabel
  16. var ProcessLabel = selinux.ExecLabel
  17. // SetSocketLabel takes a process label and tells the kernel to assign the
  18. // label to the next socket that gets created
  19. // Deprecated: use selinux.SetSocketLabel
  20. var SetSocketLabel = selinux.SetSocketLabel
  21. // SocketLabel retrieves the current default socket label setting
  22. // Deprecated: use selinux.SocketLabel
  23. var SocketLabel = selinux.SocketLabel
  24. // SetKeyLabel takes a process label and tells the kernel to assign the
  25. // label to the next kernel keyring that gets created
  26. // Deprecated: use selinux.SetKeyLabel
  27. var SetKeyLabel = selinux.SetKeyLabel
  28. // KeyLabel retrieves the current default kernel keyring label setting
  29. // Deprecated: use selinux.KeyLabel
  30. var KeyLabel = selinux.KeyLabel
  31. // FileLabel returns the label for specified path
  32. // Deprecated: use selinux.FileLabel
  33. var FileLabel = selinux.FileLabel
  34. // PidLabel will return the label of the process running with the specified pid
  35. // Deprecated: use selinux.PidLabel
  36. var PidLabel = selinux.PidLabel
  37. // Init initialises the labeling system
  38. func Init() {
  39. _ = selinux.GetEnabled()
  40. }
  41. // ClearLabels will clear all reserved labels
  42. // Deprecated: use selinux.ClearLabels
  43. var ClearLabels = selinux.ClearLabels
  44. // ReserveLabel will record the fact that the MCS label has already been used.
  45. // This will prevent InitLabels from using the MCS label in a newly created
  46. // container
  47. // Deprecated: use selinux.ReserveLabel
  48. func ReserveLabel(label string) error {
  49. selinux.ReserveLabel(label)
  50. return nil
  51. }
  52. // ReleaseLabel will remove the reservation of the MCS label.
  53. // This will allow InitLabels to use the MCS label in a newly created
  54. // containers
  55. // Deprecated: use selinux.ReleaseLabel
  56. func ReleaseLabel(label string) error {
  57. selinux.ReleaseLabel(label)
  58. return nil
  59. }
  60. // DupSecOpt takes a process label and returns security options that
  61. // can be used to set duplicate labels on future container processes
  62. // Deprecated: use selinux.DupSecOpt
  63. var DupSecOpt = selinux.DupSecOpt
  64. // FormatMountLabel returns a string to be used by the mount command. Using
  65. // the SELinux `context` mount option. Changing labels of files on mount
  66. // points with this option can never be changed.
  67. // FormatMountLabel returns a string to be used by the mount command.
  68. // The format of this string will be used to alter the labeling of the mountpoint.
  69. // The string returned is suitable to be used as the options field of the mount command.
  70. // If you need to have additional mount point options, you can pass them in as
  71. // the first parameter. Second parameter is the label that you wish to apply
  72. // to all content in the mount point.
  73. func FormatMountLabel(src, mountLabel string) string {
  74. return FormatMountLabelByType(src, mountLabel, "context")
  75. }
  76. // FormatMountLabelByType returns a string to be used by the mount command.
  77. // Allow caller to specify the mount options. For example using the SELinux
  78. // `fscontext` mount option would allow certain container processes to change
  79. // labels of files created on the mount points, where as `context` option does
  80. // not.
  81. // FormatMountLabelByType returns a string to be used by the mount command.
  82. // The format of this string will be used to alter the labeling of the mountpoint.
  83. // The string returned is suitable to be used as the options field of the mount command.
  84. // If you need to have additional mount point options, you can pass them in as
  85. // the first parameter. Second parameter is the label that you wish to apply
  86. // to all content in the mount point.
  87. func FormatMountLabelByType(src, mountLabel, contextType string) string {
  88. if mountLabel != "" {
  89. switch src {
  90. case "":
  91. src = fmt.Sprintf("%s=%q", contextType, mountLabel)
  92. default:
  93. src = fmt.Sprintf("%s,%s=%q", src, contextType, mountLabel)
  94. }
  95. }
  96. return src
  97. }