From e87076a378050488c8ce47a8b09f7fb33349e4bd Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 16 Sep 2026 04:21:16 +0000 Subject: [PATCH] ipn/auditlog: store the audit log in the backend's state directory The store path was hardcoded to %ProgramData%\Tailscale\audit-log.json on Windows, so a tailscaled run by a regular user with its own --statedir logged "[unexpected] failed to create audit log store ... Access is denied" on every profile switch. Use the backend's TailscaleVarRoot when it is known, falling back to the platform default otherwise. For the Windows service the state directory is %ProgramData%\Tailscale, so its path is unchanged; an explicit SetStoreFilePath still wins. Updates #2791 Signed-off-by: Brad Fitzpatrick Change-Id: I5d7f9b1c3e5a7c9e1b3d5f7a9c1e3b5d7f9a1c3e --- ipn/auditlog/extension.go | 14 +++++++++++--- ipn/auditlog/store.go | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/ipn/auditlog/extension.go b/ipn/auditlog/extension.go index e403f145d..eedfec58c 100644 --- a/ipn/auditlog/extension.go +++ b/ipn/auditlog/extension.go @@ -37,6 +37,11 @@ func init() { type extension struct { logf logger.Logf + // sb is the backend, consulted for its state directory when the + // store is created. The directory is not known yet when the + // extension is constructed. + sb ipnext.SafeBackend + // store is the log store shared by all loggers. // It is created when the first logger is started. store lazy.SyncValue[LogStore] @@ -53,8 +58,11 @@ type extension struct { // newExtension is an [ipnext.NewExtensionFn] that creates a new audit log extension. // It is registered with [ipnext.RegisterExtension] if the package is imported. -func newExtension(logf logger.Logf, _ ipnext.SafeBackend) (ipnext.Extension, error) { - return &extension{logf: logger.WithPrefix(logf, featureName+": ")}, nil +func newExtension(logf logger.Logf, sb ipnext.SafeBackend) (ipnext.Extension, error) { + return &extension{ + logf: logger.WithPrefix(logf, featureName+": "), + sb: sb, + }, nil } // Name implements [ipnext.Extension]. @@ -86,7 +94,7 @@ func (e *extension) startNewLogger(cc controlclient.Client, profileID ipn.Profil // Create a new log store if this is the first logger. // Otherwise, get the existing log store. store, err := e.store.GetErr(func() (LogStore, error) { - return newDefaultLogStore(e.logf) + return newDefaultLogStore(e.logf, e.sb.TailscaleVarRoot()) }) if err != nil { return nil, fmt.Errorf("failed to create audit log store: %w", err) diff --git a/ipn/auditlog/store.go b/ipn/auditlog/store.go index 07c971772..10075e5ef 100644 --- a/ipn/auditlog/store.go +++ b/ipn/auditlog/store.go @@ -41,9 +41,20 @@ func DefaultStoreFilePath() (string, error) { } } -// newDefaultLogStore returns a new [LogStore] for the current platform. -func newDefaultLogStore(logf logger.Logf) (LogStore, error) { - path, err := storeFilePath.GetErr(DefaultStoreFilePath) +// newDefaultLogStore returns a new [LogStore] for the current platform. Its +// file is the path set with [SetStoreFilePath] if any, else audit-log.json in +// varRoot (the backend's state directory) if that is known, else the platform +// default from [DefaultStoreFilePath]. For the Windows service the state +// directory is the platform default's directory anyway; the difference is for +// a tailscaled run by a user with their own --statedir, which cannot write +// under %ProgramData%. +func newDefaultLogStore(logf logger.Logf, varRoot string) (LogStore, error) { + path, err := storeFilePath.GetErr(func() (string, error) { + if varRoot != "" { + return filepath.Join(varRoot, "audit-log.json"), nil + } + return DefaultStoreFilePath() + }) if err != nil { // This indicates that the auditlog package was not omitted from the build // on a platform without a default store path and that [SetStoreFilePath]