No description
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Reposyncer 4386fcb857
All checks were successful
Go check / check-go (push) Successful in 41s
chore(reposyncer): update repo baseline (#38)
This PR was automatically created by the reposyncer script via https://git.foxden.network/FoxDen/maid

Reviewed-on: #38
2026-07-17 07:59:21 -07:00
.forgejo/workflows chore(reposyncer): update repo baseline (#38) 2026-07-17 07:59:21 -07:00
go.mod fix(deps): update module golang.org/x/sys to v0.47.0 (#37) 2026-07-08 11:23:09 -07:00
go.sum fix(deps): update module golang.org/x/sys to v0.47.0 (#37) 2026-07-08 11:23:09 -07:00
LICENSE initial commit 2020-09-27 13:37:27 -04:00
peercred.go initial commit 2020-09-27 13:37:27 -04:00
peercred_linux.go initial commit 2020-09-27 13:37:27 -04:00
peercred_linux_test.go Fix lint 2025-11-14 12:18:56 -08:00
peercred_unsupported.go Fix lint 2025-11-14 12:18:56 -08:00
peercred_unsupported_test.go Fix lint 2025-11-14 12:18:56 -08:00
README.md initial commit 2020-09-27 13:37:27 -04:00
renovate.json chore(reposyncer): update repo baseline (#23) 2025-11-16 20:48:21 -08:00

peercred

peercred is a Go package that wraps usage of the Linux SO_PEERCRED socket option on Unix domain sockets.

From the unix(7) man page:

   SO_PEERCRED
          This read-only socket option returns the credentials of the
          peer process connected to this socket.  The returned creden
          tials are those that were in effect at the time of the call to
          connect(2) or socketpair(2).

          The argument to getsockopt(2) is a pointer to a ucred struc
          ture; define the _GNU_SOURCE feature test macro to obtain the
          definition of that structure from <sys/socket.h>.

          The use of this option is possible only for connected AF_UNIX
          stream sockets and for AF_UNIX stream and datagram socket
          pairs created using socketpair(2).

On Linux systems, the raw functionality is provided through the built-in syscall package and the golang.org/x/sys/unix package. These packages, however, are not stable across operating systems and the usage of socket options is pretty low level. This package encapsulates the functionality and returns errors on unsupported operating systems through an easy Read function.

The returned value provides the process ID, user ID, and group ID of the process on the other side of the Unix domain socket. These values are populated by the Linux kernel and cannot be spoofed. (However, these values are set at the time of socket creation and will not take into account privileges dropped afterward.)

Usage


conn, err := net.Dial("unix", "/var/run/somesocket")
if err != nil {
    log.Fatal(err)
}

cred, err := peercred.Read(conn.(*net.UnixConn))
if err != nil {
    log.Fatal(err)
}

fmt.Printf("%+v\n", cred)
// => &{PID:2002 UID:1000 GID:1000}

License

Copyright 2020 Joe Shaw

peercred is licensed under the MIT license. See the LICENSE file for details.