utils.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //go:build linux || freebsd || netbsd || openbsd || solaris || !windows
  2. package csplugin
  3. import (
  4. "fmt"
  5. "io/fs"
  6. "math"
  7. "os"
  8. "os/exec"
  9. "os/user"
  10. "path/filepath"
  11. "strconv"
  12. "strings"
  13. "syscall"
  14. "github.com/pkg/errors"
  15. )
  16. func CheckCredential(uid int, gid int) *syscall.SysProcAttr {
  17. return &syscall.SysProcAttr{
  18. Credential: &syscall.Credential{
  19. Uid: uint32(uid),
  20. Gid: uint32(gid),
  21. },
  22. }
  23. }
  24. func (pb *PluginBroker) CreateCmd(binaryPath string) (*exec.Cmd, error) {
  25. var err error
  26. cmd := exec.Command(binaryPath)
  27. if pb.pluginProcConfig.User != "" || pb.pluginProcConfig.Group != "" {
  28. if !(pb.pluginProcConfig.User != "" && pb.pluginProcConfig.Group != "") {
  29. return nil, errors.New("while getting process attributes: both plugin user and group must be set")
  30. }
  31. cmd.SysProcAttr, err = getProcessAttr(pb.pluginProcConfig.User, pb.pluginProcConfig.Group)
  32. if err != nil {
  33. return nil, errors.Wrap(err, "while getting process attributes")
  34. }
  35. cmd.SysProcAttr.Credential.NoSetGroups = true
  36. }
  37. return cmd, err
  38. }
  39. func getUID(username string) (uint32, error) {
  40. u, err := user.Lookup(username)
  41. if err != nil {
  42. return 0, err
  43. }
  44. uid, err := strconv.ParseInt(u.Uid, 10, 32)
  45. if err != nil {
  46. return 0, err
  47. }
  48. if uid < 0 || uid > math.MaxInt32 {
  49. return 0, fmt.Errorf("out of bound uid")
  50. }
  51. return uint32(uid), nil
  52. }
  53. func getGID(groupname string) (uint32, error) {
  54. g, err := user.LookupGroup(groupname)
  55. if err != nil {
  56. return 0, err
  57. }
  58. gid, err := strconv.ParseInt(g.Gid, 10, 32)
  59. if err != nil {
  60. return 0, err
  61. }
  62. if gid < 0 || gid > math.MaxInt32 {
  63. return 0, fmt.Errorf("out of bound gid")
  64. }
  65. return uint32(gid), nil
  66. }
  67. func getPluginTypeAndSubtypeFromPath(path string) (string, string, error) {
  68. pluginFileName := filepath.Base(path)
  69. parts := strings.Split(pluginFileName, "-")
  70. if len(parts) < 2 {
  71. return "", "", fmt.Errorf("plugin name %s is invalid. Name should be like {type-name}", path)
  72. }
  73. return strings.Join(parts[:len(parts)-1], "-"), parts[len(parts)-1], nil
  74. }
  75. func getProcessAttr(username string, groupname string) (*syscall.SysProcAttr, error) {
  76. u, err := user.Lookup(username)
  77. if err != nil {
  78. return nil, err
  79. }
  80. g, err := user.LookupGroup(groupname)
  81. if err != nil {
  82. return nil, err
  83. }
  84. uid, err := strconv.ParseInt(u.Uid, 10, 32)
  85. if err != nil {
  86. return nil, err
  87. }
  88. if uid < 0 && uid > math.MaxInt32 {
  89. return nil, fmt.Errorf("out of bound uid")
  90. }
  91. gid, err := strconv.ParseInt(g.Gid, 10, 32)
  92. if err != nil {
  93. return nil, err
  94. }
  95. if gid < 0 && gid > math.MaxInt32 {
  96. return nil, fmt.Errorf("out of bound gid")
  97. }
  98. return &syscall.SysProcAttr{
  99. Credential: &syscall.Credential{
  100. Uid: uint32(uid),
  101. Gid: uint32(gid),
  102. },
  103. }, nil
  104. }
  105. func pluginIsValid(path string) error {
  106. var details fs.FileInfo
  107. var err error
  108. // check if it exists
  109. if details, err = os.Stat(path); err != nil {
  110. return errors.Wrap(err, fmt.Sprintf("plugin at %s does not exist", path))
  111. }
  112. // check if it is owned by current user
  113. currentUser, err := user.Current()
  114. if err != nil {
  115. return errors.Wrap(err, "while getting current user")
  116. }
  117. currentUID, err := getUID(currentUser.Username)
  118. if err != nil {
  119. return errors.Wrap(err, "while looking up the current uid")
  120. }
  121. stat := details.Sys().(*syscall.Stat_t)
  122. if stat.Uid != currentUID {
  123. return fmt.Errorf("plugin at %s is not owned by user '%s'", path, currentUser.Username)
  124. }
  125. mode := details.Mode()
  126. perm := uint32(mode)
  127. if (perm & 00002) != 0 {
  128. return fmt.Errorf("plugin at %s is world writable, world writable plugins are invalid", path)
  129. }
  130. if (perm & 00020) != 0 {
  131. return fmt.Errorf("plugin at %s is group writable, group writable plugins are invalid", path)
  132. }
  133. if (mode & os.ModeSetgid) != 0 {
  134. return fmt.Errorf("plugin at %s has setgid permission, which is not allowed", path)
  135. }
  136. return nil
  137. }