Skip to content

Commit

Permalink
unixcreds: use euid instead of uid
Browse files Browse the repository at this point in the history
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Dec 1, 2017
1 parent 45d16b4 commit 3e442f3
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions unixcreds.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (fn UnixCredentialsFunc) Handshake(ctx context.Context, conn net.Conn) (net
}
defer fp.Close() // this gets duped and must be closed when this method is complete.

// socket(7): "The returned credentials are those that were in effect at the time of the call to connect(2) or socketpair(2)."
ucred, err := unix.GetsockoptUcred(int(fp.Fd()), unix.SOL_SOCKET, unix.SO_PEERCRED)
if err != nil {
return nil, nil, errors.Wrapf(err, "ttrpc.UnixCredentialsFunc: failed to retrieve socket peer credentials")
Expand All @@ -51,14 +52,20 @@ func UnixSocketRequireRoot() UnixCredentialsFunc {
return UnixSocketRequireUidGid(0, 0)
}

// UnixSocketRequireSameUser resolves the current unix user and returns a
// UnixSocketRequireSameUser resolves the current effective unix user and returns a
// UnixCredentialsFunc that will validate incoming unix connections against the
// current credentials.
//
// This is useful when using abstract sockets that are accessible by all users.
//
// This function validates the *effective* UID/GID rather than the real UID/GID.
// For example, if a daemon binary is owned by the root (UID 0) with SUID bit but running as an
// unprivileged user (UID 1001), the effective UID becomes 0, and the real UID becomes 1001.
// So UnixSocketRequireSameUser() allows a connection from effective UID 0 but rejects
// a connection from effective UID 1001.
func UnixSocketRequireSameUser() UnixCredentialsFunc {
uid, gid := os.Getuid(), os.Getgid()
return UnixSocketRequireUidGid(uid, gid)
euid, egid := os.Geteuid(), os.Getegid()
return UnixSocketRequireUidGid(euid, egid)
}

func requireRoot(ucred *unix.Ucred) error {
Expand Down

0 comments on commit 3e442f3

Please sign in to comment.