/
sshutil.go
343 lines (324 loc) · 10.6 KB
/
sshutil.go
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package sshutil
import (
"bytes"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"github.com/coreos/go-semver/semver"
"github.com/lima-vm/lima/pkg/ioutilx"
"github.com/lima-vm/lima/pkg/lockutil"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/store/dirnames"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/sirupsen/logrus"
"golang.org/x/sys/cpu"
)
type PubKey struct {
Filename string
Content string
}
func readPublicKey(f string) (PubKey, error) {
entry := PubKey{
Filename: f,
}
content, err := os.ReadFile(f)
if err == nil {
entry.Content = strings.TrimSpace(string(content))
} else {
err = fmt.Errorf("failed to read ssh public key %q: %w", f, err)
}
return entry, err
}
// DefaultPubKeys returns the public key from $LIMA_HOME/_config/user.pub.
// The key will be created if it does not yet exist.
//
// When loadDotSSH is true, ~/.ssh/*.pub will be appended to make the VM accessible without specifying
// an identity explicitly.
func DefaultPubKeys(loadDotSSH bool) ([]PubKey, error) {
// Read $LIMA_HOME/_config/user.pub
configDir, err := dirnames.LimaConfigDir()
if err != nil {
return nil, err
}
_, err = os.Stat(filepath.Join(configDir, filenames.UserPrivateKey))
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, err
}
if err := os.MkdirAll(configDir, 0o700); err != nil {
return nil, fmt.Errorf("could not create %q directory: %w", configDir, err)
}
if err := lockutil.WithDirLock(configDir, func() error {
keygenCmd := exec.Command("ssh-keygen", "-t", "ed25519", "-q", "-N", "", "-f",
filepath.Join(configDir, filenames.UserPrivateKey))
logrus.Debugf("executing %v", keygenCmd.Args)
if out, err := keygenCmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to run %v: %q: %w", keygenCmd.Args, string(out), err)
}
return nil
}); err != nil {
return nil, err
}
}
entry, err := readPublicKey(filepath.Join(configDir, filenames.UserPublicKey))
if err != nil {
return nil, err
}
res := []PubKey{entry}
if !loadDotSSH {
return res, nil
}
// Append all of ~/.ssh/*.pub
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
files, err := filepath.Glob(filepath.Join(homeDir, ".ssh/*.pub"))
if err != nil {
panic(err) // Only possible error is ErrBadPattern, so this should be unreachable.
}
for _, f := range files {
if !strings.HasSuffix(f, ".pub") {
panic(fmt.Errorf("unexpected ssh public key filename %q", f))
}
entry, err := readPublicKey(f)
if err == nil {
if !detectValidPublicKey(entry.Content) {
logrus.Warnf("public key %q doesn't seem to be in ssh format", entry.Filename)
} else {
res = append(res, entry)
}
} else if !errors.Is(err, os.ErrNotExist) {
return nil, err
}
}
return res, nil
}
var sshInfo struct {
sync.Once
// aesAccelerated is set to true when AES acceleration is available.
// Available on almost all modern Intel/AMD processors.
aesAccelerated bool
// openSSHVersion is set to the version of OpenSSH, or semver.New("0.0.0") if the version cannot be determined.
openSSHVersion semver.Version
}
// CommonOpts returns ssh option key-value pairs like {"IdentityFile=/path/to/id_foo"}.
// The result may contain different values with the same key.
//
// The result always contains the IdentityFile option.
// The result never contains the Port option.
func CommonOpts(useDotSSH bool) ([]string, error) {
configDir, err := dirnames.LimaConfigDir()
if err != nil {
return nil, err
}
privateKeyPath := filepath.Join(configDir, filenames.UserPrivateKey)
_, err = os.Stat(privateKeyPath)
if err != nil {
return nil, err
}
var opts []string
if runtime.GOOS == "windows" {
privateKeyPath = ioutilx.CanonicalWindowsPath(privateKeyPath)
opts = []string{fmt.Sprintf(`IdentityFile='%s'`, privateKeyPath)}
} else {
opts = []string{fmt.Sprintf(`IdentityFile="%s"`, privateKeyPath)}
}
// Append all private keys corresponding to ~/.ssh/*.pub to keep old instances working
// that had been created before lima started using an internal identity.
if useDotSSH {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
files, err := filepath.Glob(filepath.Join(homeDir, ".ssh/*.pub"))
if err != nil {
panic(err) // Only possible error is ErrBadPattern, so this should be unreachable.
}
for _, f := range files {
if !strings.HasSuffix(f, ".pub") {
panic(fmt.Errorf("unexpected ssh public key filename %q", f))
}
privateKeyPath := strings.TrimSuffix(f, ".pub")
_, err = os.Stat(privateKeyPath)
if errors.Is(err, fs.ErrNotExist) {
// Skip .pub files without a matching private key. This is reasonably common,
// due to major projects like Vault recommending the ${name}-cert.pub format
// for SSH certificate files.
//
// e.g. https://www.vaultproject.io/docs/secrets/ssh/signed-ssh-certificates
continue
}
if err != nil {
// Fail on permission-related and other path errors
return nil, err
}
if runtime.GOOS == "windows" {
opts = append(opts, fmt.Sprintf(`IdentityFile='%s'`, privateKeyPath))
} else {
opts = append(opts, fmt.Sprintf(`IdentityFile="%s"`, privateKeyPath))
}
}
}
opts = append(opts,
"StrictHostKeyChecking=no",
"UserKnownHostsFile=/dev/null",
"NoHostAuthenticationForLocalhost=yes",
"GSSAPIAuthentication=no",
"PreferredAuthentications=publickey",
"Compression=no",
"BatchMode=yes",
"IdentitiesOnly=yes",
)
sshInfo.Do(func() {
sshInfo.aesAccelerated = detectAESAcceleration()
sshInfo.openSSHVersion = DetectOpenSSHVersion()
})
// Only OpenSSH version 8.1 and later support adding ciphers to the front of the default set
if !sshInfo.openSSHVersion.LessThan(*semver.New("8.1.0")) {
// By default, `ssh` choose chacha20-poly1305@openssh.com, even when AES accelerator is available.
// (OpenSSH_8.1p1, macOS 11.6, MacBookPro 2020, Core i7-1068NG7)
//
// We prioritize AES algorithms when AES accelerator is available.
if sshInfo.aesAccelerated {
logrus.Debugf("AES accelerator seems available, prioritizing aes128-gcm@openssh.com and aes256-gcm@openssh.com")
if runtime.GOOS == "windows" {
opts = append(opts, "Ciphers=^aes128-gcm@openssh.com,aes256-gcm@openssh.com")
} else {
opts = append(opts, "Ciphers=\"^aes128-gcm@openssh.com,aes256-gcm@openssh.com\"")
}
} else {
logrus.Debugf("AES accelerator does not seem available, prioritizing chacha20-poly1305@openssh.com")
if runtime.GOOS == "windows" {
opts = append(opts, "Ciphers=^chacha20-poly1305@openssh.com")
} else {
opts = append(opts, "Ciphers=\"^chacha20-poly1305@openssh.com\"")
}
}
}
return opts, nil
}
// SSHOpts adds the following options to CommonOptions: User, ControlMaster, ControlPath, ControlPersist
func SSHOpts(instDir string, useDotSSH, forwardAgent, forwardX11, forwardX11Trusted bool) ([]string, error) {
controlSock := filepath.Join(instDir, filenames.SSHSock)
if len(controlSock) >= osutil.UnixPathMax {
return nil, fmt.Errorf("socket path %q is too long: >= UNIX_PATH_MAX=%d", controlSock, osutil.UnixPathMax)
}
u, err := osutil.LimaUser(false)
if err != nil {
return nil, err
}
opts, err := CommonOpts(useDotSSH)
if err != nil {
return nil, err
}
controlPath := fmt.Sprintf(`ControlPath="%s"`, controlSock)
if runtime.GOOS == "windows" {
controlSock = ioutilx.CanonicalWindowsPath(controlSock)
controlPath = fmt.Sprintf(`ControlPath='%s'`, controlSock)
}
opts = append(opts,
fmt.Sprintf("User=%s", u.Username), // guest and host have the same username, but we should specify the username explicitly (#85)
"ControlMaster=auto",
controlPath,
"ControlPersist=yes",
)
if forwardAgent {
opts = append(opts, "ForwardAgent=yes")
}
if forwardX11 {
opts = append(opts, "ForwardX11=yes")
}
if forwardX11Trusted {
opts = append(opts, "ForwardX11Trusted=yes")
}
return opts, nil
}
// SSHArgsFromOpts returns ssh args from opts.
// The result always contains {"-F", "/dev/null} in addition to {"-o", "KEY=VALUE", ...}.
func SSHArgsFromOpts(opts []string) []string {
args := []string{"-F", "/dev/null"}
for _, o := range opts {
args = append(args, "-o", o)
}
return args
}
func ParseOpenSSHVersion(version []byte) *semver.Version {
regex := regexp.MustCompile(`^OpenSSH_(\d+\.\d+)(?:p(\d+))?\b`)
matches := regex.FindSubmatch(version)
if len(matches) == 3 {
if len(matches[2]) == 0 {
matches[2] = []byte("0")
}
return semver.New(fmt.Sprintf("%s.%s", matches[1], matches[2]))
}
return &semver.Version{}
}
func DetectOpenSSHVersion() semver.Version {
var (
v semver.Version
stderr bytes.Buffer
)
cmd := exec.Command("ssh", "-V")
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
logrus.Warnf("failed to run %v: stderr=%q", cmd.Args, stderr.String())
} else {
v = *ParseOpenSSHVersion(stderr.Bytes())
logrus.Debugf("OpenSSH version %s detected", v)
}
return v
}
// detectValidPublicKey returns whether content represent a public key.
// OpenSSH public key format have the structure of '<algorithm> <key> <comment>'.
// By checking 'algorithm' with signature format identifier in 'key' part,
// this function may report false positive but provide better compatibility.
func detectValidPublicKey(content string) bool {
if strings.ContainsRune(content, '\n') {
return false
}
spaced := strings.SplitN(content, " ", 3)
if len(spaced) < 2 {
return false
}
algo, base64Key := spaced[0], spaced[1]
decodedKey, err := base64.StdEncoding.DecodeString(base64Key)
if err != nil || len(decodedKey) < 4 {
return false
}
sigLength := binary.BigEndian.Uint32(decodedKey)
if uint32(len(decodedKey)) < sigLength {
return false
}
sigFormat := string(decodedKey[4 : 4+sigLength])
return algo == sigFormat
}
func detectAESAcceleration() bool {
if !cpu.Initialized {
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
// golang.org/x/sys/cpu supports darwin/amd64, linux/amd64, and linux/arm64,
// but apparently lacks support for darwin/arm64: https://github.com/golang/sys/blob/v0.5.0/cpu/cpu_arm64.go#L43-L60
//
// According to https://gist.github.com/voluntas/fd279c7b4e71f9950cfd4a5ab90b722b ,
// aes-128-gcm is faster than chacha20-poly1305 on Apple M1.
//
// So we return `true` here.
//
// This workaround will not be needed when https://go-review.googlesource.com/c/sys/+/332729 is merged.
logrus.Debug("Failed to detect CPU features. Assuming that AES acceleration is available on this Apple silicon.")
return true
}
logrus.Warn("Failed to detect CPU features. Assuming that AES acceleration is not available.")
return false
}
return cpu.ARM.HasAES || cpu.ARM64.HasAES || cpu.S390X.HasAES || cpu.X86.HasAES
}