File: user_keyring.go

package info (click to toggle)
fscrypt 0.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,064 kB
  • sloc: sh: 970; makefile: 159; ansic: 84
file content (251 lines) | stat: -rw-r--r-- 8,736 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * user_keyring.go - Add/remove encryption policy keys to/from user keyrings.
 * This is the deprecated mechanism; see fs_keyring.go for the new mechanism.
 *
 * Copyright 2017 Google Inc.
 * Author: Joe Richey (joerichey@google.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package keyring

import (
	"os/user"
	"runtime"
	"unsafe"

	"github.com/pkg/errors"
	"golang.org/x/sys/unix"

	"fmt"
	"log"

	"github.com/google/fscrypt/crypto"
	"github.com/google/fscrypt/security"
	"github.com/google/fscrypt/util"
)

// ErrAccessUserKeyring indicates that a user's keyring cannot be
// accessed.
type ErrAccessUserKeyring struct {
	TargetUser      *user.User
	UnderlyingError error
}

func (err *ErrAccessUserKeyring) Error() string {
	return fmt.Sprintf("could not access user keyring for %q: %s",
		err.TargetUser.Username, err.UnderlyingError)
}

// ErrSessionUserKeyring indicates that a user's keyring is not linked
// into the session keyring.
type ErrSessionUserKeyring struct {
	TargetUser *user.User
}

func (err *ErrSessionUserKeyring) Error() string {
	return fmt.Sprintf("user keyring for %q is not linked into the session keyring",
		err.TargetUser.Username)
}

// KeyType is always logon as required by filesystem encryption.
const KeyType = "logon"

// userAddKey puts the provided policy key into the user keyring for the
// specified user with the provided description, and type logon.
func userAddKey(key *crypto.Key, description string, targetUser *user.User) error {
	runtime.LockOSThread() // ensure target user keyring remains possessed in thread keyring
	defer runtime.UnlockOSThread()

	// Create our payload (containing an FscryptKey)
	payload, err := crypto.NewBlankKey(int(unsafe.Sizeof(unix.FscryptKey{})))
	if err != nil {
		return err
	}
	defer payload.Wipe()

	// Cast the payload to an FscryptKey so we can initialize the fields.
	fscryptKey := (*unix.FscryptKey)(payload.UnsafePtr())
	// Mode is ignored by the kernel
	fscryptKey.Mode = 0
	fscryptKey.Size = uint32(key.Len())
	copy(fscryptKey.Raw[:], key.Data())

	keyringID, err := UserKeyringID(targetUser, true)
	if err != nil {
		return err
	}
	keyID, err := unix.AddKey(KeyType, description, payload.Data(), keyringID)
	log.Printf("KeyctlAddKey(%s, %s, <data>, %d) = %d, %v",
		KeyType, description, keyringID, keyID, err)
	if err != nil {
		return errors.Wrapf(err,
			"error adding key with description %s to user keyring for %q",
			description, targetUser.Username)
	}
	return nil
}

// userRemoveKey tries to remove a policy key from the user keyring with the
// provided description. An error is returned if the key does not exist.
func userRemoveKey(description string, targetUser *user.User) error {
	runtime.LockOSThread() // ensure target user keyring remains possessed in thread keyring
	defer runtime.UnlockOSThread()

	keyID, keyringID, err := userFindKey(description, targetUser)
	if err != nil {
		return ErrKeyNotPresent
	}

	_, err = unix.KeyctlInt(unix.KEYCTL_UNLINK, keyID, keyringID, 0, 0)
	log.Printf("KeyctlUnlink(%d, %d) = %v", keyID, keyringID, err)
	if err != nil {
		return errors.Wrapf(err,
			"error removing key with description %s from user keyring for %q",
			description, targetUser.Username)
	}
	return nil
}

// userFindKey tries to locate a key with the provided description in the user
// keyring for the target user. The key ID and keyring ID are returned if we can
// find the key. An error is returned if the key does not exist.
func userFindKey(description string, targetUser *user.User) (int, int, error) {
	runtime.LockOSThread() // ensure target user keyring remains possessed in thread keyring
	defer runtime.UnlockOSThread()

	keyringID, err := UserKeyringID(targetUser, false)
	if err != nil {
		return 0, 0, err
	}

	keyID, err := unix.KeyctlSearch(keyringID, KeyType, description, 0)
	log.Printf("KeyctlSearch(%d, %s, %s) = %d, %v", keyringID, KeyType, description, keyID, err)
	if err != nil {
		return 0, 0, errors.Wrapf(err,
			"error searching for key %s in user keyring for %q",
			description, targetUser.Username)
	}
	return keyID, keyringID, err
}

// UserKeyringID returns the key id of the target user's user keyring. We also
// ensure that the keyring will be accessible by linking it into the thread
// keyring and linking it into the root user keyring (permissions allowing). If
// checkSession is true, an error is returned if a normal user requests their
// user keyring, but it is not in the current session keyring.
func UserKeyringID(targetUser *user.User, checkSession bool) (int, error) {
	runtime.LockOSThread() // ensure target user keyring remains possessed in thread keyring
	defer runtime.UnlockOSThread()

	uid := util.AtoiOrPanic(targetUser.Uid)
	targetKeyring, err := userKeyringIDLookup(uid)
	if err != nil {
		return 0, &ErrAccessUserKeyring{targetUser, err}
	}

	if !util.IsUserRoot() {
		// Make sure the returned keyring will be accessible by checking
		// that it is in the session keyring.
		if checkSession && !isUserKeyringInSession(uid) {
			return 0, &ErrSessionUserKeyring{targetUser}
		}
		return targetKeyring, nil
	}

	// Make sure the returned keyring will be accessible by linking it into
	// the root user's user keyring (which will not be garbage collected).
	rootKeyring, err := userKeyringIDLookup(0)
	if err != nil {
		return 0, errors.Wrapf(err, "error looking up root's user keyring")
	}

	if rootKeyring != targetKeyring {
		if err = keyringLink(targetKeyring, rootKeyring); err != nil {
			return 0, errors.Wrapf(err,
				"error linking user keyring for %q into root's user keyring",
				targetUser.Username)
		}
	}
	return targetKeyring, nil
}

func userKeyringIDLookup(uid int) (keyringID int, err error) {

	// Our goals here are to:
	//    - Find the user keyring (for the provided uid)
	//    - Link it into the current thread keyring (so we can use it)
	//    - Make no permanent changes to the process privileges
	// Complicating this are the facts that:
	//    - The value of KEY_SPEC_USER_KEYRING is determined by the ruid
	//    - Keyring linking permissions use the euid
	// So we have to change both the ruid and euid to make this work,
	// setting the suid to 0 so that we can later switch back.
	ruid, euid, suid := security.GetUids()
	if ruid != uid || euid != uid {
		if err = security.SetUids(uid, uid, 0); err != nil {
			return
		}
		defer func() {
			resetErr := security.SetUids(ruid, euid, suid)
			if resetErr != nil {
				err = resetErr
			}
		}()
	}

	// We get the value of KEY_SPEC_USER_KEYRING. Note that this will also
	// trigger the creation of the uid keyring if it does not yet exist.
	keyringID, err = unix.KeyctlGetKeyringID(unix.KEY_SPEC_USER_KEYRING, true)
	log.Printf("keyringID(_uid.%d) = %d, %v", uid, keyringID, err)
	if err != nil {
		return 0, err
	}

	// We still want to use this keyring after our privileges are reset. So
	// we link it into the thread keyring, preventing a loss of access.
	//
	// We must be under LockOSThread() for this to work reliably.  Note that
	// we can't just use the process keyring, since it doesn't work reliably
	// in Go programs, due to the Go runtime creating threads before the
	// program starts and has a chance to create the process keyring.
	if err = keyringLink(keyringID, unix.KEY_SPEC_THREAD_KEYRING); err != nil {
		return 0, err
	}

	return keyringID, nil
}

// isUserKeyringInSession tells us if the user's uid keyring is in the current
// session keyring.
func isUserKeyringInSession(uid int) bool {
	// We cannot use unix.KEY_SPEC_SESSION_KEYRING directly as that might
	// create a session keyring if one does not exist.
	sessionKeyring, err := unix.KeyctlGetKeyringID(unix.KEY_SPEC_SESSION_KEYRING, false)
	log.Printf("keyringID(session) = %d, %v", sessionKeyring, err)
	if err != nil {
		return false
	}

	description := fmt.Sprintf("_uid.%d", uid)
	id, err := unix.KeyctlSearch(sessionKeyring, "keyring", description, 0)
	log.Printf("KeyctlSearch(%d, keyring, %s) = %d, %v", sessionKeyring, description, id, err)
	return err == nil
}

func keyringLink(keyID int, keyringID int) error {
	_, err := unix.KeyctlInt(unix.KEYCTL_LINK, keyID, keyringID, 0, 0)
	log.Printf("KeyctlLink(%d, %d) = %v", keyID, keyringID, err)
	return err
}