mirror of
https://github.com/restic/restic.git
synced 2026-11-23 13:52:39 +00:00
repository: save config before key file during init (#21944)
restic init generated and uploaded the key file first and only saved the config afterwards. If two clients ran init against the same empty repository location at nearly the same time, the backend could end up with two key files but only one config file, since backends that reject overwrites let only one of the two config writes succeed. The losing client's key file stayed in the repository without ever belonging to a valid config, so clients picking that key later would fail with a "config or key ... is damaged" error instead of a clear diagnostic. Reorder Repository.init so the config is saved before the key file is uploaded. To make that possible, AddKey no longer generates a random master key internally; callers now supply the master key to encrypt and store. The two other callers of AddKey (key add, key passwd) already had an existing key available and pass that through unchanged.
This commit is contained in:
4 files changed
+62
-20
No files matched your search
@@ -0,0 +1,12 @@
|
||||
Bugfix: Fix damaged repository after concurrent `init` on REST backend
|
||||
|
||||
When two `restic init` runs raced against the same empty repository on
|
||||
the REST backend, the repository could end up with a leftover key file
|
||||
that did not belong to the stored config. Clients that happened to pick
|
||||
that key failed with a "config or key ... is damaged" error.
|
||||
|
||||
Restic now stores the config file before the key file during `init`, so
|
||||
the losing run no longer leaves a key file behind.
|
||||
|
||||
https://github.com/restic/restic/issues/5717
|
||||
https://github.com/restic/restic/pull/21944
|
||||
@@ -59,12 +59,6 @@ const (
|
||||
KDFMemory = 60
|
||||
)
|
||||
|
||||
// createMasterKey creates a new master key in the given backend and encrypts
|
||||
// it with the password.
|
||||
func createMasterKey(ctx context.Context, s *Repository, password string) (*Key, error) {
|
||||
return AddKey(ctx, s, password, "", "", nil)
|
||||
}
|
||||
|
||||
// openKey tries do decrypt the key specified by name with the given password.
|
||||
func openKey(ctx context.Context, s *Repository, id restic.ID, password string) (*Key, error) {
|
||||
if key, ok := testKeyInjection.Load(id); ok {
|
||||
@@ -208,8 +202,9 @@ func LoadKey(ctx context.Context, s *Repository, id restic.ID) (k *Key, err erro
|
||||
return k, nil
|
||||
}
|
||||
|
||||
// AddKey adds a new key to an already existing repository.
|
||||
func AddKey(ctx context.Context, s *Repository, password, username, hostname string, template *crypto.Key) (*Key, error) {
|
||||
// AddKey adds a new key to an already existing repository. masterKey is the
|
||||
// master key encrypted and stored in the new key file; it must not be nil.
|
||||
func AddKey(ctx context.Context, s *Repository, password, username, hostname string, masterKey *crypto.Key) (*Key, error) {
|
||||
// make sure we have valid KDF parameters
|
||||
if params == nil {
|
||||
p, err := crypto.Calibrate(KDFTimeout, KDFMemory)
|
||||
@@ -257,13 +252,7 @@ func AddKey(ctx context.Context, s *Repository, password, username, hostname str
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if template == nil {
|
||||
// generate new random master keys
|
||||
newkey.master = crypto.NewRandomKey()
|
||||
} else {
|
||||
// copy master keys from old key
|
||||
newkey.master = template
|
||||
}
|
||||
newkey.master = masterKey
|
||||
|
||||
// encrypt master keys (as json) with user key
|
||||
buf, err := json.Marshal(newkey.master)
|
||||
|
||||
@@ -936,16 +936,27 @@ func (r *Repository) Init(ctx context.Context, version uint, password string, ch
|
||||
|
||||
// init creates a new master key with the supplied password and uses it to save
|
||||
// the config into the repo.
|
||||
//
|
||||
// The config is uploaded before the key file. On a backend that rejects
|
||||
// overwriting an existing file (e.g. rest), a losing concurrent init then
|
||||
// fails at the config upload and leaves no stray key file behind.
|
||||
func (r *Repository) init(ctx context.Context, password string, cfg restic.Config) error {
|
||||
key, err := createMasterKey(ctx, r, password)
|
||||
masterKey := crypto.NewRandomKey()
|
||||
// r.key must be set before SaveConfig, since saveUnpacked encrypts the
|
||||
// config file with it.
|
||||
r.key = masterKey
|
||||
|
||||
if err := restic.SaveConfig(ctx, &internalRepository{r}, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
r.setConfig(cfg)
|
||||
|
||||
key, err := AddKey(ctx, r, password, "", "", masterKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.key = key.master
|
||||
r.keyID = key.ID()
|
||||
r.setConfig(cfg)
|
||||
return restic.SaveConfig(ctx, &internalRepository{r}, cfg)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Key returns the current master key.
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"io"
|
||||
"math/rand"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -515,6 +516,35 @@ func TestNoDoubleInit(t *testing.T) {
|
||||
rtest.Assert(t, strings.Contains(err.Error(), "repository already contains snapshots"), "expected already contains snapshots error, got %q", err)
|
||||
}
|
||||
|
||||
// saveOrderBackend records the types of files in the order they were saved.
|
||||
type saveOrderBackend struct {
|
||||
backend.Backend
|
||||
order []backend.FileType
|
||||
}
|
||||
|
||||
func (be *saveOrderBackend) Save(ctx context.Context, h backend.Handle, rd backend.RewindReader) error {
|
||||
be.order = append(be.order, h.Type)
|
||||
return be.Backend.Save(ctx, h, rd)
|
||||
}
|
||||
|
||||
// TestInitSavesConfigBeforeKey makes sure that Init uploads the config file
|
||||
// before the key file, such that a failed config write also prevents the key
|
||||
// file upload.
|
||||
func TestInitSavesConfigBeforeKey(t *testing.T) {
|
||||
be := &saveOrderBackend{Backend: mem.New()}
|
||||
repo, err := repository.New(be, repository.Options{})
|
||||
rtest.OK(t, err)
|
||||
|
||||
rtest.OK(t, repo.Init(context.TODO(), restic.StableRepoVersion, rtest.TestPassword, nil))
|
||||
|
||||
keyIdx := slices.Index(be.order, backend.KeyFile)
|
||||
configIdx := slices.Index(be.order, backend.ConfigFile)
|
||||
|
||||
rtest.Assert(t, keyIdx != -1, "key file was never saved")
|
||||
rtest.Assert(t, configIdx != -1, "config file was never saved")
|
||||
rtest.Assert(t, configIdx < keyIdx, "expected config file (index %d) to be saved before key file (index %d)", configIdx, keyIdx)
|
||||
}
|
||||
|
||||
func TestSaveBlobAsync(t *testing.T) {
|
||||
repo, _, _ := repository.TestRepositoryWithVersion(t, 2)
|
||||
ctx := context.Background()
|
||||
|
||||
Reference in new issue
Block a user