Debian Codegraph @

actions/protector.go

raw
1 /*
2 * protector.go - functions for dealing with protectors
3 *
4 * Copyright 2017 Google Inc.
5 * Author: Joe Richey ([email protected])
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8 * use this file except in compliance with the License. You may obtain a copy of
9 * the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations under
17 * the License.
18 */
19
20 package actions
21
22 import (
23 "fmt"
24 "log"
25 "os/user"
26
27 "github.com/google/fscrypt/crypto"
28 "github.com/google/fscrypt/metadata"
29 "github.com/google/fscrypt/util"
30 )
31
32 // LoginProtectorMountpoint is the mountpoint where login protectors are stored.
33 // This can be overridden by the user of this package.
34 var LoginProtectorMountpoint = "/"
35
36 // ErrLoginProtectorExists indicates that a user already has a login protector.
37 type ErrLoginProtectorExists struct {
38 User *user.User
39 }
40
41 func (err *ErrLoginProtectorExists) Error() string {
42 return fmt.Sprintf("user %q already has a login protector", err.User.Username)
43 }
44
45 // ErrLoginProtectorName indicates that a name was given for a login protector.
46 type ErrLoginProtectorName struct {
47 Name string
48 User *user.User
49 }
50
51 func (err *ErrLoginProtectorName) Error() string {
52 return fmt.Sprintf(`cannot assign name %q to new login protector for
53 user %q because login protectors are identified by user, not by name.`,
54 err.Name, err.User.Username)
55 }
56
57 // ErrMissingProtectorName indicates that a protector name is needed.
58 type ErrMissingProtectorName struct {
59 Source metadata.SourceType
60 }
61
62 func (err *ErrMissingProtectorName) Error() string {
63 return fmt.Sprintf("%s protectors must be named", err.Source)
64 }
65
66 // ErrProtectorNameExists indicates that a protector name already exists.
67 type ErrProtectorNameExists struct {
68 Name string
69 }
70
71 func (err *ErrProtectorNameExists) Error() string {
72 return fmt.Sprintf("there is already a protector named %q", err.Name)
73 }
74
75 // checkForProtectorWithName returns an error if there is already a protector
76 // on the filesystem with a specific name (or if we cannot read the necessary
77 // data).
78 func checkForProtectorWithName(ctx *Context, name string) error {
79 options, err := ctx.ProtectorOptions()
80 if err != nil {
81 return err
82 }
83 for _, option := range options {
84 if option.Name() == name {
85 return &ErrProtectorNameExists{name}
86 }
87 }
88 return nil
89 }
90
91 // checkIfUserHasLoginProtector returns an error if there is already a login
92 // protector on the filesystem for a specific user (or if we cannot read the
93 // necessary data).
94 func checkIfUserHasLoginProtector(ctx *Context, uid int64) error {
95 options, err := ctx.ProtectorOptions()
96 if err != nil {
97 return err
98 }
99 for _, option := range options {
100 if option.Source() == metadata.SourceType_pam_passphrase && option.UID() == uid {
101 return &ErrLoginProtectorExists{ctx.TargetUser}
102 }
103 }
104 return nil
105 }
106
107 // Protector represents an unlocked protector, so it contains the ProtectorData
108 // as well as the actual protector key. These unlocked Protectors are necessary
109 // to unlock policies and create new polices. As with the key struct, a
110 // Protector should be wiped after use.
111 type Protector struct {
112 Context *Context
113 data *metadata.ProtectorData
114 key *crypto.Key
115 created bool
116 ownerIfCreating *user.User
117 }
118
119 // CreateProtector creates an unlocked protector with a given name (name only
120 // needed for custom and raw protector types). The keyFn provided to create the
121 // Protector key will only be called once. If an error is returned, no data has
122 // been changed on the filesystem.
123 func CreateProtector(ctx *Context, name string, keyFn KeyFunc, owner *user.User) (*Protector, error) {
124 if err := ctx.checkContext(); err != nil {
125 return nil, err
126 }
127 // Sanity checks for names
128 if ctx.Config.Source == metadata.SourceType_pam_passphrase {
129 // login protectors don't need a name (we use the username instead)
130 if name != "" {
131 return nil, &ErrLoginProtectorName{name, ctx.TargetUser}
132 }
133 } else {
134 // non-login protectors need a name (so we can distinguish between them)
135 if name == "" {
136 return nil, &ErrMissingProtectorName{ctx.Config.Source}
137 }
138 // we don't want to duplicate naming
139 if err := checkForProtectorWithName(ctx, name); err != nil {
140 return nil, err
141 }
142 }
143
144 var err error
145 protector := &Protector{
146 Context: ctx,
147 data: &metadata.ProtectorData{
148 Name: name,
149 Source: ctx.Config.Source,
150 },
151 created: true,
152 ownerIfCreating: owner,
153 }
154
155 // Extra data is needed for some SourceTypes
156 switch protector.data.Source {
157 case metadata.SourceType_pam_passphrase:
158 // As the pam passphrases are user specific, we also store the
159 // UID for this kind of source.
160 protector.data.Uid = int64(util.AtoiOrPanic(ctx.TargetUser.Uid))
161 // Make sure we aren't duplicating protectors
162 if err = checkIfUserHasLoginProtector(ctx, protector.data.Uid); err != nil {
163 return nil, err
164 }
165 fallthrough
166 case metadata.SourceType_custom_passphrase:
167 // Our passphrase sources need costs and a random salt.
168 if protector.data.Salt, err = crypto.NewRandomBuffer(metadata.SaltLen); err != nil {
169 return nil, err
170 }
171
172 protector.data.Costs = ctx.Config.HashCosts
173 }
174
175 // Randomly create the underlying protector key (and wipe if we fail)
176 if protector.key, err = crypto.NewRandomKey(metadata.InternalKeyLen); err != nil {
177 return nil, err
178 }
179 protector.data.ProtectorDescriptor, err = crypto.ComputeKeyDescriptor(protector.key, 1)
180 if err != nil {
181 protector.Lock()
182 return nil, err
183 }
184
185 if err = protector.Rewrap(keyFn); err != nil {
186 protector.Lock()
187 return nil, err
188 }
189
190 return protector, nil
191 }
192
193 // GetProtector retrieves a Protector with a specific descriptor. The Protector
194 // is still locked in this case, so it must be unlocked before using certain
195 // methods.
196 func GetProtector(ctx *Context, descriptor string) (*Protector, error) {
197 log.Printf("Getting protector %s", descriptor)
198 err := ctx.checkContext()
199 if err != nil {
200 return nil, err
201 }
202
203 protector := &Protector{Context: ctx}
204 protector.data, err = ctx.Mount.GetRegularProtector(descriptor, ctx.TrustedUser)
205 return protector, err
206 }
207
208 // GetProtectorFromOption retrieves a protector based on a protector option.
209 // If the option had a load error, this function returns that error. The
210 // Protector is still locked in this case, so it must be unlocked before using
211 // certain methods.
212 func GetProtectorFromOption(ctx *Context, option *ProtectorOption) (*Protector, error) {
213 log.Printf("Getting protector %s from option", option.Descriptor())
214 if err := ctx.checkContext(); err != nil {
215 return nil, err
216 }
217 if option.LoadError != nil {
218 return nil, option.LoadError
219 }
220
221 // Replace the context if this is a linked protector
222 if option.LinkedMount != nil {
223 ctx = &Context{ctx.Config, option.LinkedMount, ctx.TargetUser, ctx.TrustedUser}
224 }
225 return &Protector{Context: ctx, data: option.data}, nil
226 }
227
228 // Descriptor returns the protector descriptor.
229 func (protector *Protector) Descriptor() string {
230 return protector.data.ProtectorDescriptor
231 }
232
233 // Destroy removes a protector from the filesystem. The internal key should
234 // still be wiped with Lock().
235 func (protector *Protector) Destroy() error {
236 return protector.Context.Mount.RemoveProtector(protector.Descriptor())
237 }
238
239 // Revert destroys a protector if it was created, but does nothing if it was
240 // just queried from the filesystem.
241 func (protector *Protector) Revert() error {
242 if !protector.created {
243 return nil
244 }
245 return protector.Destroy()
246 }
247
248 func (protector *Protector) String() string {
249 return fmt.Sprintf("Protector: %s\nMountpoint: %s\nSource: %s\nName: %s\nCosts: %v\nUID: %d",
250 protector.Descriptor(), protector.Context.Mount, protector.data.Source,
251 protector.data.Name, protector.data.Costs, protector.data.Uid)
252 }
253
254 // Unlock unwraps the Protector's internal key. The keyFn provided to unwrap the
255 // Protector key will be retried as necessary to get the correct key. Lock()
256 // should be called after use. Does nothing if protector is already unlocked.
257 func (protector *Protector) Unlock(keyFn KeyFunc) (err error) {
258 if protector.key != nil {
259 return
260 }
261 protector.key, err = unwrapProtectorKey(ProtectorInfo{protector.data}, keyFn)
262 return
263 }
264
265 // Lock wipes a Protector's internal Key. It should always be called after using
266 // an unlocked Protector. This is often done with a defer statement. There is
267 // no effect if called multiple times.
268 func (protector *Protector) Lock() error {
269 err := protector.key.Wipe()
270 protector.key = nil
271 return err
272 }
273
274 // Rewrap updates the data that is wrapping the Protector Key. This is useful if
275 // a user's password has changed, for example. The keyFn provided to rewrap
276 // the Protector key will only be called once. Requires unlocked Protector.
277 func (protector *Protector) Rewrap(keyFn KeyFunc) error {
278 if protector.key == nil {
279 return ErrLocked
280 }
281 wrappingKey, err := getWrappingKey(ProtectorInfo{protector.data}, keyFn, false)
282 if err != nil {
283 return err
284 }
285
286 // Revert change to wrapped key on failure
287 oldWrappedKey := protector.data.WrappedKey
288 defer func() {
289 wrappingKey.Wipe()
290 if err != nil {
291 protector.data.WrappedKey = oldWrappedKey
292 }
293 }()
294
295 if protector.data.WrappedKey, err = crypto.Wrap(wrappingKey, protector.key); err != nil {
296 return err
297 }
298
299 return protector.Context.Mount.AddProtector(protector.data, protector.ownerIfCreating)
300 }