-
Notifications
You must be signed in to change notification settings - Fork 0
/
reexec.go
522 lines (452 loc) · 15.8 KB
/
reexec.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
Copyright 2020 Gravitational, Inc.
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 srv
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"
"syscall"
"github.com/gravitational/trace"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/pam"
"github.com/gravitational/teleport/lib/shell"
"github.com/gravitational/teleport/lib/sshutils"
"github.com/gravitational/teleport/lib/utils"
log "github.com/sirupsen/logrus"
)
// execCommand contains the payload to "teleport exec" which will be used to
// construct and execute a shell.
type execCommand struct {
// Command is the command to execute. If an interactive session is being
// requested, will be empty.
Command string `json:"command"`
// DestinationAddress is the target address to dial to.
DestinationAddress string `json:"dst_addr"`
// Username is the username associated with the Teleport identity.
Username string `json:"username"`
// Login is the local *nix account.
Login string `json:"login"`
// Roles is the list of Teleport roles assigned to the Teleport identity.
Roles []string `json:"roles"`
// ClusterName is the name of the Teleport cluster.
ClusterName string `json:"cluster_name"`
// Terminal indicates if a TTY has been allocated for the session. This is
// typically set if either an shell was requested or a TTY was explicitly
// allocated for a exec request.
Terminal bool `json:"term"`
// RequestType is the type of request: either "exec" or "shell". This will
// be used to control where to connect std{out,err} based on the request
// type: "exec" or "shell".
RequestType string `json:"request_type"`
// PAM indicates if PAM support was requested by the node.
PAM bool `json:"pam"`
// ServiceName is the name of the PAM service requested if PAM is enabled.
ServiceName string `json:"service_name"`
// Environment is a list of environment variables to add to the defaults.
Environment []string `json:"environment"`
// PermitUserEnvironment is set to allow reading in ~/.tsh/environment
// upon login.
PermitUserEnvironment bool `json:"permit_user_environment"`
// IsTestStub is used by tests to mock the shell.
IsTestStub bool `json:"is_test_stub"`
}
// RunCommand reads in the command to run from the parent process (over a
// pipe) then constructs and runs the command.
func RunCommand() (io.Writer, int, error) {
// errorWriter is used to return any error message back to the client. By
// default it writes to stdout, but if a TTY is allocated, it will write
// to it instead.
errorWriter := os.Stdout
// Parent sends the command payload in the third file descriptor.
cmdfd := os.NewFile(uintptr(3), "/proc/self/fd/3")
if cmdfd == nil {
return errorWriter, teleport.RemoteCommandFailure, trace.BadParameter("command pipe not found")
}
contfd := os.NewFile(uintptr(4), "/proc/self/fd/4")
if contfd == nil {
return errorWriter, teleport.RemoteCommandFailure, trace.BadParameter("continue pipe not found")
}
// Read in the command payload.
var b bytes.Buffer
_, err := b.ReadFrom(cmdfd)
if err != nil {
return errorWriter, teleport.RemoteCommandFailure, trace.Wrap(err)
}
var c execCommand
err = json.Unmarshal(b.Bytes(), &c)
if err != nil {
return errorWriter, teleport.RemoteCommandFailure, trace.Wrap(err)
}
var tty *os.File
var pty *os.File
// If a terminal was requested, file descriptor 4 and 5 always point to the
// PTY and TTY. Extract them and set the controlling TTY. Otherwise, connect
// std{in,out,err} directly.
if c.Terminal {
pty = os.NewFile(uintptr(5), "/proc/self/fd/5")
tty = os.NewFile(uintptr(6), "/proc/self/fd/6")
if pty == nil || tty == nil {
return errorWriter, teleport.RemoteCommandFailure, trace.BadParameter("pty and tty not found")
}
errorWriter = tty
}
// If PAM is enabled, open a PAM context. This has to be done before anything
// else because PAM is sometimes used to create the local user used to
// launch the shell under.
var pamEnvironment []string
if c.PAM {
// Connect std{in,out,err} to the TTY if it's a shell request, otherwise
// discard std{out,err}. If this was not done, things like MOTD would be
// printed for "exec" requests.
var stdin io.Reader
var stdout io.Writer
var stderr io.Writer
if c.RequestType == sshutils.ShellRequest {
stdin = tty
stdout = tty
stderr = tty
} else {
stdin = os.Stdin
stdout = ioutil.Discard
stderr = ioutil.Discard
}
// Set Teleport specific environment variables that PAM modules like
// pam_script.so can pick up to potentially customize the account/session.
os.Setenv("TELEPORT_USERNAME", c.Username)
os.Setenv("TELEPORT_LOGIN", c.Login)
os.Setenv("TELEPORT_ROLES", strings.Join(c.Roles, " "))
// Open the PAM context.
pamContext, err := pam.Open(&pam.Config{
ServiceName: c.ServiceName,
Login: c.Login,
Stdin: stdin,
Stdout: stdout,
Stderr: stderr,
})
if err != nil {
return errorWriter, teleport.RemoteCommandFailure, trace.Wrap(err)
}
defer pamContext.Close()
// Save off any environment variables that come from PAM.
pamEnvironment = pamContext.Environment()
}
// Build the actual command that will launch the shell.
cmd, err := buildCommand(&c, tty, pty, pamEnvironment)
if err != nil {
return errorWriter, teleport.RemoteCommandFailure, trace.Wrap(err)
}
// Wait until the continue signal is received from Teleport signaling that
// the child process has been placed in a cgroup.
err = waitForContinue(contfd)
if err != nil {
return errorWriter, teleport.RemoteCommandFailure, trace.Wrap(err)
}
// Start the command.
err = cmd.Start()
if err != nil {
return errorWriter, teleport.RemoteCommandFailure, trace.Wrap(err)
}
// Wait for the command to exit. It doesn't make sense to print an error
// message here because the shell has successfully started. If an error
// occured during shell execution or the shell exits with an error (like
// running exit 2), the shell will print an error if appropriate and return
// an exit code.
err = cmd.Wait()
return ioutil.Discard, exitCode(err), trace.Wrap(err)
}
// RunForward reads in the command to run from the parent process (over a
// pipe) then port forwards.
func RunForward() (io.Writer, int, error) {
// errorWriter is used to return any error message back to the client.
errorWriter := os.Stdout
// Parent sends the command payload in the third file descriptor.
cmdfd := os.NewFile(uintptr(3), "/proc/self/fd/3")
if cmdfd == nil {
return errorWriter, teleport.RemoteCommandFailure, trace.BadParameter("command pipe not found")
}
// Read in the command payload.
var b bytes.Buffer
_, err := b.ReadFrom(cmdfd)
if err != nil {
return errorWriter, teleport.RemoteCommandFailure, trace.Wrap(err)
}
var c execCommand
err = json.Unmarshal(b.Bytes(), &c)
if err != nil {
return errorWriter, teleport.RemoteCommandFailure, trace.Wrap(err)
}
// If PAM is enabled, open a PAM context. This has to be done before anything
// else because PAM is sometimes used to create the local user used to
// launch the shell under.
if c.PAM {
// Set Teleport specific environment variables that PAM modules like
// pam_script.so can pick up to potentially customize the account/session.
os.Setenv("TELEPORT_USERNAME", c.Username)
os.Setenv("TELEPORT_LOGIN", c.Login)
os.Setenv("TELEPORT_ROLES", strings.Join(c.Roles, " "))
// Open the PAM context.
pamContext, err := pam.Open(&pam.Config{
ServiceName: c.ServiceName,
Login: c.Login,
Stdin: os.Stdin,
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
})
if err != nil {
return errorWriter, teleport.RemoteCommandFailure, trace.Wrap(err)
}
defer pamContext.Close()
}
// Connect to the target host.
conn, err := net.Dial("tcp", c.DestinationAddress)
if err != nil {
return errorWriter, teleport.RemoteCommandFailure, trace.Wrap(err)
}
defer conn.Close()
// Start copy routines that copy from channel to stdin pipe and from stdout
// pipe to channel.
errorCh := make(chan error, 2)
go func() {
defer os.Stdout.Close()
defer os.Stdin.Close()
_, err := io.Copy(os.Stdout, conn)
errorCh <- err
}()
go func() {
defer os.Stdout.Close()
defer os.Stdin.Close()
_, err := io.Copy(conn, os.Stdin)
errorCh <- err
}()
// Block until copy is complete and the child process is done executing.
var errs []error
for i := 0; i < 2; i++ {
select {
case err := <-errorCh:
if err != nil && err != io.EOF {
errs = append(errs, err)
}
}
}
return ioutil.Discard, teleport.RemoteCommandSuccess, trace.NewAggregate(errs...)
}
// RunAndExit will run the requested command and then exit. This wrapper
// allows Run{Command,Forward} to use defers and makes sure error messages
// are consistent across both.
func RunAndExit(commandType string) {
var w io.Writer
var code int
var err error
switch commandType {
case teleport.ExecSubCommand:
w, code, err = RunCommand()
case teleport.ForwardSubCommand:
w, code, err = RunForward()
default:
w, code, err = os.Stderr, teleport.RemoteCommandFailure, fmt.Errorf("unknown command type: %v", commandType)
}
if err != nil {
s := fmt.Sprintf("Failed to launch: %v.\r\n", err)
io.Copy(w, bytes.NewBufferString(s))
}
os.Exit(code)
}
// buildCommand constructs a command that will execute the users shell. This
// function is run by Teleport while it's re-executing.
func buildCommand(c *execCommand, tty *os.File, pty *os.File, pamEnvironment []string) (*exec.Cmd, error) {
var cmd exec.Cmd
// Lookup the UID and GID for the user.
localUser, err := user.Lookup(c.Login)
if err != nil {
return nil, trace.Wrap(err)
}
uid, err := strconv.Atoi(localUser.Uid)
if err != nil {
return nil, trace.Wrap(err)
}
gid, err := strconv.Atoi(localUser.Gid)
if err != nil {
return nil, trace.Wrap(err)
}
// Lookup supplementary groups for the user.
userGroups, err := localUser.GroupIds()
if err != nil {
return nil, trace.Wrap(err)
}
groups := make([]uint32, 0)
for _, sgid := range userGroups {
igid, err := strconv.Atoi(sgid)
if err != nil {
log.Warnf("Cannot interpret user group: '%v'", sgid)
} else {
groups = append(groups, uint32(igid))
}
}
if len(groups) == 0 {
groups = append(groups, uint32(gid))
}
// Get the login shell for the user (or fallback to the default).
shellPath, err := shell.GetLoginShell(c.Login)
if err != nil {
log.Debugf("Failed to get login shell for %v: %v. Using default: %v.",
c.Login, err, shell.DefaultShell)
}
if c.IsTestStub {
shellPath = "/bin/sh"
}
// If no command was given, configure a shell to run in 'login' mode.
// Otherwise, execute a command through the shell.
if c.Command == "" {
// Set the path to the path of the shell.
cmd.Path = shellPath
// Configure the shell to run in 'login' mode. From OpenSSH source:
// "If we have no command, execute the shell. In this case, the shell
// name to be passed in argv[0] is preceded by '-' to indicate that
// this is a login shell."
// https://github.com/openssh/openssh-portable/blob/master/session.c
cmd.Args = []string{"-" + filepath.Base(shellPath)}
} else {
// Execute commands like OpenSSH does:
// https://github.com/openssh/openssh-portable/blob/master/session.c
cmd.Path = shellPath
cmd.Args = []string{shellPath, "-c", c.Command}
}
// Create default environment for user.
cmd.Env = []string{
"LANG=en_US.UTF-8",
getDefaultEnvPath(localUser.Uid, defaultLoginDefsPath),
"HOME=" + localUser.HomeDir,
"USER=" + c.Login,
"SHELL=" + shellPath,
}
// Add in Teleport specific environment variables.
cmd.Env = append(cmd.Env, c.Environment...)
// If the server allows reading in of ~/.tsh/environment read it in
// and pass environment variables along to new session.
if c.PermitUserEnvironment {
filename := filepath.Join(localUser.HomeDir, ".tsh", "environment")
userEnvs, err := utils.ReadEnvironmentFile(filename)
if err != nil {
return nil, trace.Wrap(err)
}
cmd.Env = append(cmd.Env, userEnvs...)
}
// If any additional environment variables come from PAM, apply them as well.
cmd.Env = append(cmd.Env, pamEnvironment...)
// Set the home directory for the user.
cmd.Dir = localUser.HomeDir
// If a terminal was requested, connect std{in,out,err} to the TTY and set
// the controlling TTY. Otherwise, connect std{in,out,err} to
// os.Std{in,out,err}.
if c.Terminal {
cmd.Stdin = tty
cmd.Stdout = tty
cmd.Stderr = tty
cmd.SysProcAttr = &syscall.SysProcAttr{
Setsid: true,
Setctty: true,
Ctty: int(tty.Fd()),
}
} else {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Setsid: true,
}
}
// Only set process credentials if the UID/GID of the requesting user are
// different than the process (Teleport).
//
// Note, the above is important because setting the credentials struct
// triggers calling of the SETUID and SETGID syscalls during process start.
// If the caller does not have permission to call those two syscalls (for
// example, if Teleport is started from a shell), this will prevent the
// process from spawning shells with the error: "operation not permitted". To
// workaround this, the credentials struct is only set if the credentials
// are different from the process itself. If the credentials are not, simply
// pick up the ambient credentials of the process.
if strconv.Itoa(os.Getuid()) != localUser.Uid || strconv.Itoa(os.Getgid()) != localUser.Gid {
cmd.SysProcAttr.Credential = &syscall.Credential{
Uid: uint32(uid),
Gid: uint32(gid),
Groups: groups,
}
log.Debugf("Creating process with UID %v, GID: %v, and Groups: %v.",
uid, gid, groups)
} else {
log.Debugf("Creating process with ambient credentials UID %v, GID: %v, Groups: %v.",
uid, gid, groups)
}
return &cmd, nil
}
// ConfigureCommand creates a command fully configured to execute. This
// function is used by Teleport to re-execute itself and pass whatever data
// is need to the child to actually execute the shell.
func ConfigureCommand(ctx *ServerContext) (*exec.Cmd, error) {
// Marshal the parts needed from the *ServerContext into an *execCommand.
cmdmsg, err := ctx.ExecCommand()
if err != nil {
return nil, trace.Wrap(err)
}
cmdbytes, err := json.Marshal(cmdmsg)
if err != nil {
return nil, trace.Wrap(err)
}
// Write command bytes to pipe. The child process will read the command
// to execute from this pipe.
_, err = io.Copy(ctx.cmdw, bytes.NewReader(cmdbytes))
if err != nil {
return nil, trace.Wrap(err)
}
err = ctx.cmdw.Close()
if err != nil {
return nil, trace.Wrap(err)
}
// Set to nil so the close in the context doesn't attempt to re-close.
ctx.cmdw = nil
// Find the Teleport executable and its directory on disk.
executable, err := os.Executable()
if err != nil {
return nil, trace.Wrap(err)
}
executableDir, _ := filepath.Split(executable)
// The channel type determines the subcommand to execute (execution or
// port forwarding).
subCommand := teleport.ExecSubCommand
if ctx.ChannelType == teleport.ChanDirectTCPIP {
subCommand = teleport.ForwardSubCommand
}
// Build the list of arguments to have Teleport re-exec itself. The "-d" flag
// is appended if Teleport is running in debug mode.
args := []string{executable, subCommand}
// Build the "teleport exec" command.
return &exec.Cmd{
Path: executable,
Args: args,
Dir: executableDir,
ExtraFiles: []*os.File{
ctx.cmdr,
ctx.contr,
},
}, nil
}