tailscale/ssh/tailssh/auditd_linux.go
Will Norris 3ec5be3f51 all: remove AUTHORS file and references to it
This file was never truly necessary and has never actually been used in
the history of Tailscale's open source releases.

A Brief History of AUTHORS files
---

The AUTHORS file was a pattern developed at Google, originally for
Chromium, then adopted by Go and a bunch of other projects. The problem
was that Chromium originally had a copyright line only recognizing
Google as the copyright holder. Because Google (and most open source
projects) do not require copyright assignemnt for contributions, each
contributor maintains their copyright. Some large corporate contributors
then tried to add their own name to the copyright line in the LICENSE
file or in file headers. This quickly becomes unwieldy, and puts a
tremendous burden on anyone building on top of Chromium, since the
license requires that they keep all copyright lines intact.

The compromise was to create an AUTHORS file that would list all of the
copyright holders. The LICENSE file and source file headers would then
include that list by reference, listing the copyright holder as "The
Chromium Authors".

This also become cumbersome to simply keep the file up to date with a
high rate of new contributors. Plus it's not always obvious who the
copyright holder is. Sometimes it is the individual making the
contribution, but many times it may be their employer. There is no way
for the proejct maintainer to know.

Eventually, Google changed their policy to no longer recommend trying to
keep the AUTHORS file up to date proactively, and instead to only add to
it when requested: https://opensource.google/docs/releasing/authors.
They are also clear that:

> Adding contributors to the AUTHORS file is entirely within the
> project's discretion and has no implications for copyright ownership.

It was primarily added to appease a small number of large contributors
that insisted that they be recognized as copyright holders (which was
entirely their right to do). But it's not truly necessary, and not even
the most accurate way of identifying contributors and/or copyright
holders.

In practice, we've never added anyone to our AUTHORS file. It only lists
Tailscale, so it's not really serving any purpose. It also causes
confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header
in other open source repos which don't actually have an AUTHORS file, so
it's ambiguous what that means.

Instead, we just acknowledge that the contributors to Tailscale (whoever
they are) are copyright holders for their individual contributions. We
also have the benefit of using the DCO (developercertificate.org) which
provides some additional certification of their right to make the
contribution.

The source file changes were purely mechanical with:

    git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g'

Updates #cleanup

Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d
Signed-off-by: Will Norris <will@tailscale.com>
2026-01-23 15:49:45 -08:00

177 lines
4.7 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build linux && !android
package tailssh
import (
"bytes"
"encoding/binary"
"fmt"
"os"
"syscall"
"golang.org/x/sys/unix"
"tailscale.com/types/logger"
)
const (
auditUserLogin = 1112 // audit message type for user login (from linux/audit.h)
netlinkAudit = 9 // AF_NETLINK protocol number for audit (from linux/netlink.h)
nlmFRequest = 0x01 // netlink message flag: request (from linux/netlink.h)
// maxAuditMessageLength is the maximum length of an audit message payload.
// This is derived from MAX_AUDIT_MESSAGE_LENGTH (8970) in the Linux kernel
// (linux/audit.h), minus overhead for the netlink header and safety margin.
maxAuditMessageLength = 8192
)
// hasAuditWriteCap checks if the process has CAP_AUDIT_WRITE in its effective capability set.
func hasAuditWriteCap() bool {
var hdr unix.CapUserHeader
var data [2]unix.CapUserData
hdr.Version = unix.LINUX_CAPABILITY_VERSION_3
hdr.Pid = int32(os.Getpid())
if err := unix.Capget(&hdr, &data[0]); err != nil {
return false
}
const capBit = uint32(1 << (unix.CAP_AUDIT_WRITE % 32))
const capIdx = unix.CAP_AUDIT_WRITE / 32
return (data[capIdx].Effective & capBit) != 0
}
// buildAuditNetlinkMessage constructs a netlink audit message.
// This is separated from sendAuditMessage to allow testing the message format
// without requiring CAP_AUDIT_WRITE or a netlink socket.
func buildAuditNetlinkMessage(msgType uint16, message string) ([]byte, error) {
msgBytes := []byte(message)
if len(msgBytes) > maxAuditMessageLength {
msgBytes = msgBytes[:maxAuditMessageLength]
}
msgLen := len(msgBytes)
totalLen := syscall.NLMSG_HDRLEN + msgLen
alignedLen := (totalLen + syscall.NLMSG_ALIGNTO - 1) & ^(syscall.NLMSG_ALIGNTO - 1)
nlh := syscall.NlMsghdr{
Len: uint32(totalLen),
Type: msgType,
Flags: nlmFRequest,
Seq: 1,
Pid: uint32(os.Getpid()),
}
buf := bytes.NewBuffer(make([]byte, 0, alignedLen))
if err := binary.Write(buf, binary.NativeEndian, nlh); err != nil {
return nil, err
}
buf.Write(msgBytes)
for buf.Len() < alignedLen {
buf.WriteByte(0)
}
return buf.Bytes(), nil
}
// sendAuditMessage sends a message to the audit subsystem using raw netlink.
// It logs errors but does not return them.
func sendAuditMessage(logf logger.Logf, msgType uint16, message string) {
if !hasAuditWriteCap() {
return
}
fd, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW, netlinkAudit)
if err != nil {
logf("auditd: failed to create netlink socket: %v", err)
return
}
defer syscall.Close(fd)
bindAddr := &syscall.SockaddrNetlink{
Family: syscall.AF_NETLINK,
Pid: uint32(os.Getpid()),
Groups: 0,
}
if err := syscall.Bind(fd, bindAddr); err != nil {
logf("auditd: failed to bind netlink socket: %v", err)
return
}
kernelAddr := &syscall.SockaddrNetlink{
Family: syscall.AF_NETLINK,
Pid: 0,
Groups: 0,
}
msgBytes, err := buildAuditNetlinkMessage(msgType, message)
if err != nil {
logf("auditd: failed to build audit message: %v", err)
return
}
if err := syscall.Sendto(fd, msgBytes, 0, kernelAddr); err != nil {
logf("auditd: failed to send audit message: %v", err)
return
}
}
// logSSHLogin logs an SSH login event to auditd with whois information.
func logSSHLogin(logf logger.Logf, c *conn) {
if c == nil || c.info == nil || c.localUser == nil {
return
}
exePath := c.srv.tailscaledPath
if exePath == "" {
exePath = "tailscaled"
}
srcIP := c.info.src.Addr().String()
srcPort := c.info.src.Port()
dstIP := c.info.dst.Addr().String()
dstPort := c.info.dst.Port()
tailscaleUser := c.info.uprof.LoginName
tailscaleUserID := c.info.uprof.ID
tailscaleDisplayName := c.info.uprof.DisplayName
nodeName := c.info.node.Name()
nodeID := c.info.node.ID()
localUser := c.localUser.Username
localUID := c.localUser.Uid
localGID := c.localUser.Gid
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
// use principally the same format as ssh / PAM, which come from the audit userspace, i.e.
// https://github.com/linux-audit/audit-userspace/blob/b6f8c208435038df113a9795e3e202720aee6b70/lib/audit_logging.c#L515
msg := fmt.Sprintf(
"op=login acct=%s uid=%s gid=%s "+
"src=%s src_port=%d dst=%s dst_port=%d "+
"hostname=%q exe=%q terminal=ssh res=success "+
"ts_user=%q ts_user_id=%d ts_display_name=%q ts_node=%q ts_node_id=%d",
localUser, localUID, localGID,
srcIP, srcPort, dstIP, dstPort,
hostname, exePath,
tailscaleUser, tailscaleUserID, tailscaleDisplayName, nodeName, nodeID,
)
sendAuditMessage(logf, auditUserLogin, msg)
logf("audit: SSH login: user=%s uid=%s from=%s ts_user=%s node=%s",
localUser, localUID, srcIP, tailscaleUser, nodeName)
}
func init() {
hookSSHLoginSuccess.Set(logSSHLogin)
}