unixcreds_linux.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ttrpc
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "net"
  19. "os"
  20. "syscall"
  21. "golang.org/x/sys/unix"
  22. )
  23. type UnixCredentialsFunc func(*unix.Ucred) error
  24. func (fn UnixCredentialsFunc) Handshake(ctx context.Context, conn net.Conn) (net.Conn, interface{}, error) {
  25. uc, err := requireUnixSocket(conn)
  26. if err != nil {
  27. return nil, nil, fmt.Errorf("ttrpc.UnixCredentialsFunc: require unix socket: %w", err)
  28. }
  29. rs, err := uc.SyscallConn()
  30. if err != nil {
  31. return nil, nil, fmt.Errorf("ttrpc.UnixCredentialsFunc: (net.UnixConn).SyscallConn failed: %w", err)
  32. }
  33. var (
  34. ucred *unix.Ucred
  35. ucredErr error
  36. )
  37. if err := rs.Control(func(fd uintptr) {
  38. ucred, ucredErr = unix.GetsockoptUcred(int(fd), unix.SOL_SOCKET, unix.SO_PEERCRED)
  39. }); err != nil {
  40. return nil, nil, fmt.Errorf("ttrpc.UnixCredentialsFunc: (*syscall.RawConn).Control failed: %w", err)
  41. }
  42. if ucredErr != nil {
  43. return nil, nil, fmt.Errorf("ttrpc.UnixCredentialsFunc: failed to retrieve socket peer credentials: %w", err)
  44. }
  45. if err := fn(ucred); err != nil {
  46. return nil, nil, fmt.Errorf("ttrpc.UnixCredentialsFunc: credential check failed: %w", err)
  47. }
  48. return uc, ucred, nil
  49. }
  50. // UnixSocketRequireUidGid requires specific *effective* UID/GID, rather than the real UID/GID.
  51. //
  52. // For example, if a daemon binary is owned by the root (UID 0) with SUID bit but running as an
  53. // unprivileged user (UID 1001), the effective UID becomes 0, and the real UID becomes 1001.
  54. // So calling this function with uid=0 allows a connection from effective UID 0 but rejects
  55. // a connection from effective UID 1001.
  56. //
  57. // See socket(7), SO_PEERCRED: "The returned credentials are those that were in effect at the time of the call to connect(2) or socketpair(2)."
  58. func UnixSocketRequireUidGid(uid, gid int) UnixCredentialsFunc {
  59. return func(ucred *unix.Ucred) error {
  60. return requireUidGid(ucred, uid, gid)
  61. }
  62. }
  63. func UnixSocketRequireRoot() UnixCredentialsFunc {
  64. return UnixSocketRequireUidGid(0, 0)
  65. }
  66. // UnixSocketRequireSameUser resolves the current effective unix user and returns a
  67. // UnixCredentialsFunc that will validate incoming unix connections against the
  68. // current credentials.
  69. //
  70. // This is useful when using abstract sockets that are accessible by all users.
  71. func UnixSocketRequireSameUser() UnixCredentialsFunc {
  72. euid, egid := os.Geteuid(), os.Getegid()
  73. return UnixSocketRequireUidGid(euid, egid)
  74. }
  75. func requireRoot(ucred *unix.Ucred) error {
  76. return requireUidGid(ucred, 0, 0)
  77. }
  78. func requireUidGid(ucred *unix.Ucred, uid, gid int) error {
  79. if (uid != -1 && uint32(uid) != ucred.Uid) || (gid != -1 && uint32(gid) != ucred.Gid) {
  80. return fmt.Errorf("ttrpc: invalid credentials: %v", syscall.EPERM)
  81. }
  82. return nil
  83. }
  84. func requireUnixSocket(conn net.Conn) (*net.UnixConn, error) {
  85. uc, ok := conn.(*net.UnixConn)
  86. if !ok {
  87. return nil, errors.New("a unix socket connection is required")
  88. }
  89. return uc, nil
  90. }