| 1 |
/* |
| 2 |
* recovery.go - support for generating recovery passphrases |
| 3 |
* |
| 4 |
* Copyright 2019 Google LLC |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 7 |
* use this file except in compliance with the License. You may obtain a copy of |
| 8 |
* the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 |
* License for the specific language governing permissions and limitations under |
| 16 |
* the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package actions |
| 20 |
|
| 21 |
import ( |
| 22 |
"fmt" |
| 23 |
"os" |
| 24 |
"strconv" |
| 25 |
|
| 26 |
"golang.org/x/sys/unix" |
| 27 |
"google.golang.org/protobuf/proto" |
| 28 |
|
| 29 |
"github.com/google/fscrypt/crypto" |
| 30 |
"github.com/google/fscrypt/metadata" |
| 31 |
"github.com/google/fscrypt/util" |
| 32 |
) |
| 33 |
|
| 34 |
// modifiedContextWithSource returns a copy of ctx with the protector source |
| 35 |
// replaced by source. |
| 36 |
func modifiedContextWithSource(ctx *Context, source metadata.SourceType) *Context { |
| 37 |
modifiedConfig := proto.Clone(ctx.Config).(*metadata.Config) |
| 38 |
modifiedConfig.Source = source |
| 39 |
modifiedCtx := *ctx |
| 40 |
modifiedCtx.Config = modifiedConfig |
| 41 |
return &modifiedCtx |
| 42 |
} |
| 43 |
|
| 44 |
// AddRecoveryPassphrase randomly generates a recovery passphrase and adds it as |
| 45 |
// a custom_passphrase protector for the given Policy. |
| 46 |
func AddRecoveryPassphrase(policy *Policy, dirname string) (*crypto.Key, *Protector, error) { |
| 47 |
// 20 random characters in a-z is 94 bits of entropy, which is way more |
| 48 |
// than enough for a passphrase which still goes through the usual |
| 49 |
// passphrase hashing which makes it extremely costly to brute force. |
| 50 |
passphrase, err := crypto.NewRandomPassphrase(20) |
| 51 |
if err != nil { |
| 52 |
return nil, nil, err |
| 53 |
} |
| 54 |
defer func() { |
| 55 |
if err != nil { |
| 56 |
passphrase.Wipe() |
| 57 |
} |
| 58 |
}() |
| 59 |
getPassphraseFn := func(info ProtectorInfo, retry bool) (*crypto.Key, error) { |
| 60 |
// CreateProtector() wipes the passphrase, but in this case we |
| 61 |
// still need it for later, so make a copy. |
| 62 |
return passphrase.Clone() |
| 63 |
} |
| 64 |
var recoveryProtector *Protector |
| 65 |
customCtx := modifiedContextWithSource(policy.Context, metadata.SourceType_custom_passphrase) |
| 66 |
seq := 1 |
| 67 |
for { |
| 68 |
// Automatically generate a name for the recovery protector. |
| 69 |
name := "Recovery passphrase for " + dirname |
| 70 |
if seq != 1 { |
| 71 |
name += " (" + strconv.Itoa(seq) + ")" |
| 72 |
} |
| 73 |
recoveryProtector, err = CreateProtector(customCtx, name, getPassphraseFn, policy.ownerIfCreating) |
| 74 |
if err == nil { |
| 75 |
break |
| 76 |
} |
| 77 |
if _, ok := err.(*ErrProtectorNameExists); !ok { |
| 78 |
return nil, nil, err |
| 79 |
} |
| 80 |
seq++ |
| 81 |
} |
| 82 |
if err := policy.AddProtector(recoveryProtector); err != nil { |
| 83 |
recoveryProtector.Revert() |
| 84 |
return nil, nil, err |
| 85 |
} |
| 86 |
return passphrase, recoveryProtector, nil |
| 87 |
} |
| 88 |
|
| 89 |
// WriteRecoveryInstructions writes a recovery passphrase and instructions to a |
| 90 |
// file. This file should initially be located in the encrypted directory |
| 91 |
// protected by the passphrase itself. It's up to the user to store the |
| 92 |
// passphrase in a different location if they actually need it. |
| 93 |
func WriteRecoveryInstructions(recoveryPassphrase *crypto.Key, recoveryProtector *Protector, |
| 94 |
policy *Policy, path string) error { |
| 95 |
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL|unix.O_NOFOLLOW, 0600) |
| 96 |
if err != nil { |
| 97 |
return err |
| 98 |
} |
| 99 |
defer file.Close() |
| 100 |
str := fmt.Sprintf( |
| 101 |
`fscrypt automatically generated a recovery passphrase for this directory: |
| 102 |
|
| 103 |
%s |
| 104 |
|
| 105 |
It did this because you chose to protect this directory with your login |
| 106 |
passphrase, but this directory is not on the root filesystem. |
| 107 |
|
| 108 |
Copy this passphrase to a safe place if you want to still be able to unlock this |
| 109 |
directory if you re-install the operating system or connect this storage media |
| 110 |
to a different system (which would result in your login protector being lost). |
| 111 |
|
| 112 |
To unlock this directory using this recovery passphrase, run 'fscrypt unlock' |
| 113 |
and select the protector named %q. |
| 114 |
|
| 115 |
If you want to disable recovery passphrase generation (not recommended), |
| 116 |
re-create this directory and pass the --no-recovery option to 'fscrypt encrypt'. |
| 117 |
Alternatively, you can remove this recovery passphrase protector using: |
| 118 |
|
| 119 |
fscrypt metadata remove-protector-from-policy --force --protector=%s:%s --policy=%s:%s |
| 120 |
|
| 121 |
It is safe to keep it around though, as the recovery passphrase is high-entropy. |
| 122 |
`, recoveryPassphrase.Data(), recoveryProtector.data.Name, |
| 123 |
recoveryProtector.Context.Mount.Path, recoveryProtector.data.ProtectorDescriptor, |
| 124 |
policy.Context.Mount.Path, policy.data.KeyDescriptor) |
| 125 |
if _, err = file.WriteString(str); err != nil { |
| 126 |
return err |
| 127 |
} |
| 128 |
if recoveryProtector.ownerIfCreating != nil { |
| 129 |
if err = util.Chown(file, recoveryProtector.ownerIfCreating); err != nil { |
| 130 |
return err |
| 131 |
} |
| 132 |
} |
| 133 |
return file.Sync() |
| 134 |
} |