utils.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. uid, err := getUID(username)
  77. if err != nil {
  78. return nil, err
  79. }
  80. gid, err := getGID(groupname)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return &syscall.SysProcAttr{
  85. Credential: &syscall.Credential{
  86. Uid: uid,
  87. Gid: gid,
  88. },
  89. }, nil
  90. }
  91. func pluginIsValid(path string) error {
  92. var details fs.FileInfo
  93. var err error
  94. // check if it exists
  95. if details, err = os.Stat(path); err != nil {
  96. return errors.Wrap(err, fmt.Sprintf("plugin at %s does not exist", path))
  97. }
  98. // check if it is owned by current user
  99. currentUser, err := user.Current()
  100. if err != nil {
  101. return errors.Wrap(err, "while getting current user")
  102. }
  103. currentUID, err := getUID(currentUser.Username)
  104. if err != nil {
  105. return errors.Wrap(err, "while looking up the current uid")
  106. }
  107. stat := details.Sys().(*syscall.Stat_t)
  108. if stat.Uid != currentUID {
  109. return fmt.Errorf("plugin at %s is not owned by user '%s'", path, currentUser.Username)
  110. }
  111. mode := details.Mode()
  112. perm := uint32(mode)
  113. if (perm & 00002) != 0 {
  114. return fmt.Errorf("plugin at %s is world writable, world writable plugins are invalid", path)
  115. }
  116. if (perm & 00020) != 0 {
  117. return fmt.Errorf("plugin at %s is group writable, group writable plugins are invalid", path)
  118. }
  119. if (mode & os.ModeSetgid) != 0 {
  120. return fmt.Errorf("plugin at %s has setgid permission, which is not allowed", path)
  121. }
  122. return nil
  123. }