Debian Codegraph @

actions/context.go

raw
1 /*
2 * context.go - top-level interface to fscrypt packages
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 is the high-level interface to the fscrypt packages. The
21 // functions here roughly correspond with commands for the tool in cmd/fscrypt.
22 // All of the actions include a significant amount of logging, so that good
23 // output can be provided for cmd/fscrypt's verbose mode.
24 // The top-level actions currently include:
25 // - Creating a new config file
26 // - Creating a context on which to perform actions
27 // - Creating, unlocking, and modifying Protectors
28 // - Creating, unlocking, and modifying Policies
29 package actions
30
31 import (
32 "log"
33 "os/user"
34
35 "github.com/pkg/errors"
36
37 "github.com/google/fscrypt/filesystem"
38 "github.com/google/fscrypt/keyring"
39 "github.com/google/fscrypt/metadata"
40 "github.com/google/fscrypt/util"
41 )
42
43 // ErrLocked indicates that the key hasn't been unwrapped yet.
44 var ErrLocked = errors.New("key needs to be unlocked first")
45
46 // Context contains the necessary global state to perform most of fscrypt's
47 // actions.
48 type Context struct {
49 // Config is the struct loaded from the global config file. It can be
50 // modified after being loaded to customise parameters.
51 Config *metadata.Config
52 // Mount is the filesystem relative to which all Protectors and Policies
53 // are added, edited, removed, and applied, and to which policies using
54 // the filesystem keyring are provisioned.
55 Mount *filesystem.Mount
56 // TargetUser is the user for whom protectors are created, and to whose
57 // keyring policies using the user keyring are provisioned. It's also
58 // the user for whom the keys are claimed in the filesystem keyring when
59 // v2 policies are provisioned.
60 TargetUser *user.User
61 // TrustedUser is the user for whom policies and protectors are allowed
62 // to be read. Specifically, if TrustedUser is set, then only
63 // policies and protectors owned by TrustedUser or by root will be
64 // allowed to be read. If it's nil, then all policies and protectors
65 // the process has filesystem-level read access to will be allowed.
66 TrustedUser *user.User
67 }
68
69 // NewContextFromPath makes a context for the filesystem containing the
70 // specified path and whose Config is loaded from the global config file. On
71 // success, the Context contains a valid Config and Mount. The target user
72 // defaults to the current effective user if none is specified.
73 func NewContextFromPath(path string, targetUser *user.User) (*Context, error) {
74 ctx, err := newContextFromUser(targetUser)
75 if err != nil {
76 return nil, err
77 }
78 if ctx.Mount, err = filesystem.FindMount(path); err != nil {
79 return nil, err
80 }
81
82 log.Printf("%s is on %s filesystem %q (%s)", path,
83 ctx.Mount.FilesystemType, ctx.Mount.Path, ctx.Mount.Device)
84 return ctx, nil
85 }
86
87 // NewContextFromMountpoint makes a context for the filesystem at the specified
88 // mountpoint and whose Config is loaded from the global config file. On
89 // success, the Context contains a valid Config and Mount. The target user
90 // defaults to the current effective user if none is specified.
91 func NewContextFromMountpoint(mountpoint string, targetUser *user.User) (*Context, error) {
92 ctx, err := newContextFromUser(targetUser)
93 if err != nil {
94 return nil, err
95 }
96 if ctx.Mount, err = filesystem.GetMount(mountpoint); err != nil {
97 return nil, err
98 }
99
100 log.Printf("found %s filesystem %q (%s)", ctx.Mount.FilesystemType,
101 ctx.Mount.Path, ctx.Mount.Device)
102 return ctx, nil
103 }
104
105 // newContextFromUser makes a context with the corresponding target user, and
106 // whose Config is loaded from the global config file. If the target user is
107 // nil, the effective user is used.
108 func newContextFromUser(targetUser *user.User) (*Context, error) {
109 var err error
110 if targetUser == nil {
111 if targetUser, err = util.EffectiveUser(); err != nil {
112 return nil, err
113 }
114 }
115
116 ctx := &Context{TargetUser: targetUser}
117 if ctx.Config, err = getConfig(); err != nil {
118 return nil, err
119 }
120
121 // By default, when running as a non-root user we only read policies and
122 // protectors owned by the user or root. When running as root, we allow
123 // reading all policies and protectors.
124 if !ctx.Config.GetAllowCrossUserMetadata() && !util.IsUserRoot() {
125 ctx.TrustedUser, err = util.EffectiveUser()
126 if err != nil {
127 return nil, err
128 }
129 }
130
131 log.Printf("creating context for user %q", targetUser.Username)
132 return ctx, nil
133 }
134
135 // checkContext verifies that the context contains a valid config and a mount
136 // which is being used with fscrypt.
137 func (ctx *Context) checkContext() error {
138 if err := ctx.Config.CheckValidity(); err != nil {
139 return &ErrBadConfig{ctx.Config, err}
140 }
141 return ctx.Mount.CheckSetup(ctx.TrustedUser)
142 }
143
144 func (ctx *Context) getKeyringOptions() *keyring.Options {
145 return &keyring.Options{
146 Mount: ctx.Mount,
147 User: ctx.TargetUser,
148 UseFsKeyringForV1Policies: ctx.Config.GetUseFsKeyringForV1Policies(),
149 }
150 }
151
152 // getProtectorOption returns the ProtectorOption for the protector on the
153 // context's mountpoint with the specified descriptor.
154 func (ctx *Context) getProtectorOption(protectorDescriptor string) *ProtectorOption {
155 mnt, data, err := ctx.Mount.GetProtector(protectorDescriptor, ctx.TrustedUser)
156 if err != nil {
157 return &ProtectorOption{ProtectorInfo{}, nil, err}
158 }
159
160 info := ProtectorInfo{data}
161 // No linked path if on the same mountpoint
162 if mnt == ctx.Mount {
163 return &ProtectorOption{info, nil, nil}
164 }
165 return &ProtectorOption{info, mnt, nil}
166 }
167
168 // ProtectorOptions creates a slice of all the options for all of the Protectors
169 // on the Context's mountpoint.
170 func (ctx *Context) ProtectorOptions() ([]*ProtectorOption, error) {
171 if err := ctx.checkContext(); err != nil {
172 return nil, err
173 }
174 descriptors, err := ctx.Mount.ListProtectors(ctx.TrustedUser)
175 if err != nil {
176 return nil, err
177 }
178
179 options := make([]*ProtectorOption, len(descriptors))
180 for i, descriptor := range descriptors {
181 options[i] = ctx.getProtectorOption(descriptor)
182 }
183 return options, nil
184 }