From ba371f241116d2fcc1472047081d45a1f7431f25 Mon Sep 17 00:00:00 2001 From: moonD4rk Date: Wed, 18 Nov 2026 15:38:25 +0800 Subject: [PATCH 1/3] fix(masterkey): resolve Linux v11 keys via Secret Service Enumerating a collection and matching the label exactly missed keys: a GNOME keyring listing stale item paths aborted the whole read, and KWallet labels entries " Keys/ Safe Storage". Use SearchItems (live items only) and match the label by suffix. --- go.mod | 1 - go.sum | 3 - internal/masterkey/retriever_linux.go | 147 ++++++++++++++++++++------ 3 files changed, 117 insertions(+), 34 deletions(-) diff --git a/go.mod b/go.mod index ed54573..17a1c8f 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( github.com/moond4rk/binarycookies v1.0.3 github.com/moond4rk/keychainbreaker v0.2.6 github.com/moond4rk/plist v1.2.2 - github.com/ppacher/go-dbus-keyring v1.0.1 github.com/spf13/cobra v1.10.2 github.com/spf13/pflag v1.1.0 github.com/stretchr/testify v1.12.1 diff --git a/go.sum b/go.sum index d690e91..86bcf8d 100644 --- a/go.sum +++ b/go.sum @@ -2,7 +2,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6N github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= -github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.2.4 h1:F2gUCy++NMIxGmo9J+z5le/+ZC8W1OgeR51P4i9ZB/U= github.com/godbus/dbus/v5 v5.2.4/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -33,8 +32,6 @@ github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/ppacher/go-dbus-keyring v1.0.1 h1:dM4dMfP5w9MxY+foFHCQiN7izEGpFdKr3tZeMGmvqD0= -github.com/ppacher/go-dbus-keyring v1.0.1/go.mod h1:JEmkRwBVPBFkOHedAsoZALWmhNJxR/R/ykkFpbEHtGE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= diff --git a/internal/masterkey/retriever_linux.go b/internal/masterkey/retriever_linux.go index 08a771e..61ae4e9 100644 --- a/internal/masterkey/retriever_linux.go +++ b/internal/masterkey/retriever_linux.go @@ -6,9 +6,12 @@ import ( "context" "crypto/sha1" "fmt" + "strings" + "time" "github.com/godbus/dbus/v5" - keyring "github.com/ppacher/go-dbus-keyring" + + "github.com/moond4rk/hackbrowserdata/internal/log" ) // linuxParams follow https://source.chromium.org/chromium/chromium/src/+/main:components/os_crypt/os_crypt_linux.cc @@ -26,52 +29,136 @@ type dbusRetriever struct{} func (dbusRetriever) Version() Version { return V11 } -func (dbusRetriever) Retrieve(_ context.Context, locator Locator) ([]byte, error) { - storage := locator.SecretLabel +const ( + secretServiceName = "org.freedesktop.secrets" + secretServicePath = "/org/freedesktop/secrets" + secretServiceIntf = "org.freedesktop.Secret.Service" + secretItemIntf = "org.freedesktop.Secret.Item" + secretSessionIntf = "org.freedesktop.Secret.Session" + secretCallTimeout = 10 * time.Second +) + +// osCryptSchemas are the Secret Service xdg:schema values Chromium forks use: libsecret's own on GNOME, and the one +// KWallet tags its entries with when it answers the Secret Service on KDE. +var osCryptSchemas = []string{"chrome_libsecret_os_crypt_password_v2", "org.qt.keychain"} + +// dbusSecret is the org.freedesktop.Secret.Item.GetSecret reply; a plain session leaves Value unencrypted. +type dbusSecret struct { + Session dbus.ObjectPath + Parameters []byte + Value []byte + ContentType string +} + +// Retrieve reads the storage key through the Secret Service's SearchItems, which resolves only live items. The login +// collection can list item paths that no longer exist, and a whole-collection enumeration aborts on the first of them, +// so it misses the live key even while the collection is unlocked. KWallet exposes its entries here labeled +// " Keys/ Safe Storage", so the wanted label is matched by suffix as well. +func (dbusRetriever) Retrieve(ctx context.Context, locator Locator) ([]byte, error) { + ctx, cancel := context.WithTimeout(ctx, secretCallTimeout) + defer cancel() + connection, err := dbus.SessionBus() if err != nil { return nil, fmt.Errorf("dbus session: %w", err) } + service := connection.Object(secretServiceName, secretServicePath) - service, err := keyring.GetSecretService(connection) + session, err := openSecretSession(ctx, service) if err != nil { - return nil, fmt.Errorf("secret service: %w", err) + return nil, err } + defer closeSecretSession(connection, session) - session, err := service.OpenSession() - if err != nil { - return nil, fmt.Errorf("open session: %w", err) + for _, schema := range osCryptSchemas { + if key := findStorageSecret(ctx, connection, service, session, schema, locator.SecretLabel); key != nil { + return linuxParams.deriveKey(key), nil + } } - defer session.Close() + return nil, fmt.Errorf("%q: %w", locator.SecretLabel, errStorageNotFound) +} - collections, err := service.GetAllCollections() - if err != nil { - return nil, fmt.Errorf("get collections: %w", err) +func openSecretSession(ctx context.Context, service dbus.BusObject) (dbus.ObjectPath, error) { + call := service.CallWithContext(ctx, secretServiceIntf+".OpenSession", 0, "plain", dbus.MakeVariant("")) + if call.Err != nil { + return "", fmt.Errorf("open secret session: %w", call.Err) + } + var output dbus.Variant + var session dbus.ObjectPath + if err := call.Store(&output, &session); err != nil { + return "", fmt.Errorf("open secret session: %w", err) } + return session, nil +} + +func closeSecretSession(connection *dbus.Conn, session dbus.ObjectPath) { + if call := connection.Object(secretServiceName, session).Call(secretSessionIntf+".Close", 0); call.Err != nil { + log.Debugf("close secret session: %v", call.Err) + } +} - for _, collection := range collections { - items, err := collection.GetAllItems() - if err != nil { +// findStorageSecret returns the first live item of the schema whose label equals, or ends with "/"+want. +func findStorageSecret(ctx context.Context, conn *dbus.Conn, service dbus.BusObject, session dbus.ObjectPath, schema, want string) []byte { + call := service.CallWithContext(ctx, secretServiceIntf+".SearchItems", 0, map[string]string{"xdg:schema": schema}) + if call.Err != nil { + log.Debugf("secret service SearchItems (%s): %v", schema, call.Err) + return nil + } + var unlocked, locked []dbus.ObjectPath + if err := call.Store(&unlocked, &locked); err != nil { + return nil + } + if len(locked) > 0 { + unlocked = append(unlocked, unlockItems(ctx, service, locked)...) + } + for _, path := range unlocked { + if !labelMatches(conn, path, want) { continue } - for _, item := range items { - label, err := item.GetLabel() - if err != nil { - continue - } - if label == storage { - secret, err := item.GetSecret(session.Path()) - if err != nil { - return nil, fmt.Errorf("get secret for %s: %w", storage, err) - } - if len(secret.Value) > 0 { - return linuxParams.deriveKey(secret.Value), nil - } - } + if secret := itemSecret(ctx, conn, path, session); len(secret) > 0 { + return secret } } + return nil +} - return nil, fmt.Errorf("%q: %w", storage, errStorageNotFound) +// unlockItems unlocks only what opens without a prompt; a returned prompt path (not "/") means a GUI dialog is needed, +// which a non-interactive extraction skips rather than block on. +func unlockItems(ctx context.Context, service dbus.BusObject, locked []dbus.ObjectPath) []dbus.ObjectPath { + call := service.CallWithContext(ctx, secretServiceIntf+".Unlock", 0, locked) + if call.Err != nil { + return nil + } + var unlocked []dbus.ObjectPath + var prompt dbus.ObjectPath + if err := call.Store(&unlocked, &prompt); err != nil { + return nil + } + if prompt != "/" { + log.Debugf("secret service: %d item(s) stay locked behind a prompt", len(locked)-len(unlocked)) + } + return unlocked +} + +func labelMatches(conn *dbus.Conn, path dbus.ObjectPath, want string) bool { + value, err := conn.Object(secretServiceName, path).GetProperty(secretItemIntf + ".Label") + if err != nil { + return false + } + label, _ := value.Value().(string) + return label == want || strings.HasSuffix(label, "/"+want) +} + +func itemSecret(ctx context.Context, conn *dbus.Conn, path, session dbus.ObjectPath) []byte { + call := conn.Object(secretServiceName, path).CallWithContext(ctx, secretItemIntf+".GetSecret", 0, session) + if call.Err != nil { + return nil + } + var secret dbusSecret + if err := call.Store(&secret); err != nil { + return nil + } + return secret.Value } // posixRetriever covers hosts without a keyring (headless, Docker, CI), mirroring Chromium's PosixKeyProvider. From fd9f3b0de6753c29352e95953ad9543861e0e317 Mon Sep 17 00:00:00 2001 From: moonD4rk Date: Wed, 18 Nov 2026 17:21:58 +0800 Subject: [PATCH 2/3] fix(masterkey): locked error, bounded calls, user query A locked wallet now reports "locked" instead of "not found", every D-Bus call (label read, session close) is bounded by the timeout, and the lookup also searches by KWallet's user attribute so it no longer depends on the xdg:schema label. The label match is a tested pure func. --- internal/masterkey/retriever_linux.go | 87 ++++++++++++++-------- internal/masterkey/retriever_linux_test.go | 8 ++ 2 files changed, 66 insertions(+), 29 deletions(-) diff --git a/internal/masterkey/retriever_linux.go b/internal/masterkey/retriever_linux.go index 61ae4e9..1f12e3c 100644 --- a/internal/masterkey/retriever_linux.go +++ b/internal/masterkey/retriever_linux.go @@ -5,6 +5,7 @@ package masterkey import ( "context" "crypto/sha1" + "errors" "fmt" "strings" "time" @@ -35,12 +36,11 @@ const ( secretServiceIntf = "org.freedesktop.Secret.Service" secretItemIntf = "org.freedesktop.Secret.Item" secretSessionIntf = "org.freedesktop.Secret.Session" + propertiesGet = "org.freedesktop.DBus.Properties.Get" secretCallTimeout = 10 * time.Second ) -// osCryptSchemas are the Secret Service xdg:schema values Chromium forks use: libsecret's own on GNOME, and the one -// KWallet tags its entries with when it answers the Secret Service on KDE. -var osCryptSchemas = []string{"chrome_libsecret_os_crypt_password_v2", "org.qt.keychain"} +var errKeyringLocked = errors.New("keyring or wallet is locked") //nolint:unused // only used on linux // dbusSecret is the org.freedesktop.Secret.Item.GetSecret reply; a plain session leaves Value unencrypted. type dbusSecret struct { @@ -50,10 +50,19 @@ type dbusSecret struct { ContentType string } -// Retrieve reads the storage key through the Secret Service's SearchItems, which resolves only live items. The login -// collection can list item paths that no longer exist, and a whole-collection enumeration aborts on the first of them, -// so it misses the live key even while the collection is unlocked. KWallet exposes its entries here labeled -// " Keys/ Safe Storage", so the wanted label is matched by suffix as well. +// searchQueries find the storage item by libsecret's schema (GNOME), KWallet's schema, and KWallet's entry-name +// attribute, the last independent of the schema label, which KWallet versions set inconsistently. +func searchQueries(label string) []map[string]string { + return []map[string]string{ + {"xdg:schema": "chrome_libsecret_os_crypt_password_v2"}, + {"xdg:schema": "org.qt.keychain"}, + {"user": label}, + } +} + +// Retrieve looks the storage key up through the Secret Service's SearchItems, which resolves only live items, then +// reads each one by one so a stale path that errors is skipped (a whole-collection read aborts on it instead). A +// match hidden behind a locked wallet is reported as locked, not as not found. func (dbusRetriever) Retrieve(ctx context.Context, locator Locator) ([]byte, error) { ctx, cancel := context.WithTimeout(ctx, secretCallTimeout) defer cancel() @@ -70,10 +79,16 @@ func (dbusRetriever) Retrieve(ctx context.Context, locator Locator) ([]byte, err } defer closeSecretSession(connection, session) - for _, schema := range osCryptSchemas { - if key := findStorageSecret(ctx, connection, service, session, schema, locator.SecretLabel); key != nil { - return linuxParams.deriveKey(key), nil + sawLocked := false + for _, attrs := range searchQueries(locator.SecretLabel) { + secret, locked := findStorageSecret(ctx, connection, service, session, attrs, locator.SecretLabel) + if len(secret) > 0 { + return linuxParams.deriveKey(secret), nil } + sawLocked = sawLocked || locked + } + if sawLocked { + return nil, fmt.Errorf("%q: %w", locator.SecretLabel, errKeyringLocked) } return nil, fmt.Errorf("%q: %w", locator.SecretLabel, errStorageNotFound) } @@ -92,34 +107,42 @@ func openSecretSession(ctx context.Context, service dbus.BusObject) (dbus.Object } func closeSecretSession(connection *dbus.Conn, session dbus.ObjectPath) { - if call := connection.Object(secretServiceName, session).Call(secretSessionIntf+".Close", 0); call.Err != nil { + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + if call := connection.Object(secretServiceName, session).CallWithContext(ctx, secretSessionIntf+".Close", 0); call.Err != nil { log.Debugf("close secret session: %v", call.Err) } } -// findStorageSecret returns the first live item of the schema whose label equals, or ends with "/"+want. -func findStorageSecret(ctx context.Context, conn *dbus.Conn, service dbus.BusObject, session dbus.ObjectPath, schema, want string) []byte { - call := service.CallWithContext(ctx, secretServiceIntf+".SearchItems", 0, map[string]string{"xdg:schema": schema}) +// findStorageSecret returns the first live item matching attrs whose label fits want, and whether a locked item hid a +// possible match (so the caller can report "locked" rather than "not found"). +func findStorageSecret( + ctx context.Context, conn *dbus.Conn, service dbus.BusObject, session dbus.ObjectPath, attrs map[string]string, want string, +) ([]byte, bool) { + call := service.CallWithContext(ctx, secretServiceIntf+".SearchItems", 0, attrs) if call.Err != nil { - log.Debugf("secret service SearchItems (%s): %v", schema, call.Err) - return nil + log.Debugf("secret service SearchItems %v: %v", attrs, call.Err) + return nil, false } var unlocked, locked []dbus.ObjectPath if err := call.Store(&unlocked, &locked); err != nil { - return nil + return nil, false } + sawLocked := false if len(locked) > 0 { - unlocked = append(unlocked, unlockItems(ctx, service, locked)...) + opened := unlockItems(ctx, service, locked) + unlocked = append(unlocked, opened...) + sawLocked = len(opened) < len(locked) } for _, path := range unlocked { - if !labelMatches(conn, path, want) { + if !labelMatches(itemLabel(ctx, conn, path), want) { continue } if secret := itemSecret(ctx, conn, path, session); len(secret) > 0 { - return secret + return secret, sawLocked } } - return nil + return nil, sawLocked } // unlockItems unlocks only what opens without a prompt; a returned prompt path (not "/") means a GUI dialog is needed, @@ -134,19 +157,25 @@ func unlockItems(ctx context.Context, service dbus.BusObject, locked []dbus.Obje if err := call.Store(&unlocked, &prompt); err != nil { return nil } - if prompt != "/" { - log.Debugf("secret service: %d item(s) stay locked behind a prompt", len(locked)-len(unlocked)) - } return unlocked } -func labelMatches(conn *dbus.Conn, path dbus.ObjectPath, want string) bool { - value, err := conn.Object(secretServiceName, path).GetProperty(secretItemIntf + ".Label") - if err != nil { - return false +// labelMatches accepts the exact Secret Service label and KWallet's "/" form, where the entry is want. +func labelMatches(label, want string) bool { + return label == want || strings.HasSuffix(label, "/"+want) +} + +func itemLabel(ctx context.Context, conn *dbus.Conn, path dbus.ObjectPath) string { + call := conn.Object(secretServiceName, path).CallWithContext(ctx, propertiesGet, 0, secretItemIntf, "Label") + if call.Err != nil { + return "" + } + var value dbus.Variant + if err := call.Store(&value); err != nil { + return "" } label, _ := value.Value().(string) - return label == want || strings.HasSuffix(label, "/"+want) + return label } func itemSecret(ctx context.Context, conn *dbus.Conn, path, session dbus.ObjectPath) []byte { diff --git a/internal/masterkey/retriever_linux_test.go b/internal/masterkey/retriever_linux_test.go index 58ef6e3..fe7362b 100644 --- a/internal/masterkey/retriever_linux_test.go +++ b/internal/masterkey/retriever_linux_test.go @@ -51,3 +51,11 @@ func TestPlatformRetrievers_Linux(t *testing.T) { assert.Equal(t, V10, platformRetrievers[0].Version()) assert.Equal(t, V11, platformRetrievers[1].Version()) } + +func TestLabelMatches(t *testing.T) { + assert.True(t, labelMatches("Chrome Safe Storage", "Chrome Safe Storage"), "exact label") + assert.True(t, labelMatches("Chrome Keys/Chrome Safe Storage", "Chrome Safe Storage"), "KWallet folder/entry form") + assert.False(t, labelMatches("Chromium Safe Storage", "Chrome Safe Storage"), "a different browser must not match") + assert.False(t, labelMatches("Chromium Keys/Chromium Safe Storage", "Chrome Safe Storage"), "nor its folder/entry form") + assert.False(t, labelMatches("", "Chrome Safe Storage"), "an unreadable label must not match") +} From 9c3ea04f254413ef04ffda6d7c909ae6feaaa6e3 Mon Sep 17 00:00:00 2001 From: moonD4rk Date: Wed, 18 Nov 2026 21:40:46 +0800 Subject: [PATCH 3/3] chore(masterkey): drop needless nolint on errKeyringLocked --- internal/masterkey/retriever_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/masterkey/retriever_linux.go b/internal/masterkey/retriever_linux.go index 1f12e3c..0b5c3b8 100644 --- a/internal/masterkey/retriever_linux.go +++ b/internal/masterkey/retriever_linux.go @@ -40,7 +40,7 @@ const ( secretCallTimeout = 10 * time.Second ) -var errKeyringLocked = errors.New("keyring or wallet is locked") //nolint:unused // only used on linux +var errKeyringLocked = errors.New("keyring or wallet is locked") // dbusSecret is the org.freedesktop.Secret.Item.GetSecret reply; a plain session leaves Value unencrypted. type dbusSecret struct {