-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathinit.go
More file actions
570 lines (461 loc) · 17.8 KB
/
Copy pathinit.go
File metadata and controls
570 lines (461 loc) · 17.8 KB
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package api
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"maps"
"net/http"
"net/netip"
"os/exec"
"slices"
"strings"
"time"
"github.com/awnumar/memguard"
"github.com/rs/zerolog"
"github.com/txn2/txeh"
"golang.org/x/sync/errgroup"
"github.com/e2b-dev/infra/packages/envd/internal/host"
"github.com/e2b-dev/infra/packages/envd/internal/logs"
"github.com/e2b-dev/infra/packages/envd/internal/logs/ratelimit"
"github.com/e2b-dev/infra/packages/envd/internal/services/cgroups"
"github.com/e2b-dev/infra/packages/shared/pkg/keys"
)
// /init is hammered by the orchestrator's infinite retry loop, so a
// persistent pin failure would otherwise flood the log.
var pinMMDSWarnLimit = ratelimit.New(10 * time.Second)
var (
ErrAccessTokenMismatch = errors.New("access token validation failed")
ErrAccessTokenResetNotAuthorized = errors.New("access token reset not authorized")
)
const (
maxTimeInPast = 50 * time.Millisecond
maxTimeInFuture = 5 * time.Second
)
// validateInitAccessToken validates the access token for /init requests.
// Token is valid if it matches the existing token OR the MMDS hash.
// If neither exists, first-time setup is allowed.
func (a *API) validateInitAccessToken(ctx context.Context, requestToken *SecureToken) error {
requestTokenSet := requestToken.IsSet()
// Fast path: token matches existing
if a.accessToken.IsSet() && requestTokenSet && a.accessToken.EqualsSecure(requestToken) {
return nil
}
// Check MMDS only if token didn't match existing
matchesMMDS, mmdsExists := a.checkMMDSHash(ctx, requestToken)
switch {
case matchesMMDS:
return nil
case !a.accessToken.IsSet() && !mmdsExists:
return nil // first-time setup
case !requestTokenSet:
return ErrAccessTokenResetNotAuthorized
default:
return ErrAccessTokenMismatch
}
}
// checkMMDSHash checks if the request token matches the MMDS hash.
// Returns (matches, mmdsExists).
//
// The MMDS hash is set by the orchestrator during Resume:
// - hash(token): requires this specific token
// - hash(""): explicitly allows nil token (token reset authorized)
// - "": MMDS not properly configured, no authorization granted
func (a *API) checkMMDSHash(ctx context.Context, requestToken *SecureToken) (bool, bool) {
if a.isNotFC {
return false, false
}
mmdsHash, err := a.mmdsClient.GetAccessTokenHash(ctx)
if err != nil {
// Self-heal: a user-installed PREROUTING/OUTPUT redirect on
// 169.254.169.254:80 in the same netns can shadow our route.
// Re-pin our RETURN rule at position 1 of nat PREROUTING and
// OUTPUT, then retry once.
if pinErr := host.PinMMDSRoute(ctx); pinErr != nil {
if ok, suppressed := pinMMDSWarnLimit.Allow(); ok {
a.logger.Warn().Err(pinErr).Int64("suppressed", suppressed).Msg("failed to pin MMDS iptables route")
}
}
mmdsHash, err = a.mmdsClient.GetAccessTokenHash(ctx)
}
if err != nil {
return false, false
}
if mmdsHash == "" {
return false, false
}
if !requestToken.IsSet() {
return mmdsHash == keys.HashAccessToken(""), true
}
tokenBytes, err := requestToken.Bytes()
if err != nil {
return false, true
}
defer memguard.WipeBytes(tokenBytes)
return keys.HashAccessTokenBytes(tokenBytes) == mmdsHash, true
}
func (a *API) PostInit(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
ctx := r.Context()
operationID := logs.AssignOperationID()
logger := a.logger.With().Str(string(logs.OperationIDKey), operationID).Logger()
if r.Body != nil {
// Read raw body so we can wipe it after parsing
body, err := io.ReadAll(r.Body)
// Ensure body is wiped after we're done
defer memguard.WipeBytes(body)
if err != nil {
logger.Error().Msgf("Failed to read request body: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
var initRequest PostInitJSONBody
if len(body) > 0 {
err = json.Unmarshal(body, &initRequest)
if err != nil {
logger.Error().Msgf("Failed to decode request: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
}
// Ensure request token is destroyed if not transferred via TakeFrom.
// This handles: validation failures, timestamp-based skips, and any early returns.
// Safe because Destroy() is nil-safe and TakeFrom clears the source.
defer initRequest.AccessToken.Destroy()
if err := a.initLock.Acquire(ctx, 1); err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
defer a.initLock.Release(1)
// Validate auth before installing the unfreeze defer or running SetData,
// so stale/replayed but unauthorized requests can't thaw cgroups.
if err := a.validateInitAccessToken(ctx, initRequest.AccessToken); err != nil {
writeInitError(w, logger, err)
return
}
// Run on every /init regardless of the Timestamp guard, so stale/replayed
// requests still thaw cgroups after pre-pause freeze.
defer a.unfreezeUserCgroups(ctx, logger)
// Update data only if the request is newer or if there's no timestamp at all
if initRequest.Timestamp == nil || a.lastSetTime.SetToGreater(initRequest.Timestamp.UnixNano()) {
if err := a.SetData(ctx, logger, initRequest); err != nil {
writeInitError(w, logger, err)
return
}
}
}
go func() { //nolint:contextcheck // MMDS re-poll must outlive the /init HTTP response, so it can't derive from r.Context
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
host.PollForMMDSOpts(ctx, a.mmdsChan, a.defaults.EnvVars)
}()
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(http.StatusNoContent)
}
func (a *API) SetData(ctx context.Context, logger zerolog.Logger, data PostInitJSONBody) error {
if data.Timestamp != nil {
// Check if current time differs significantly from the received timestamp
if shouldSetSystemTime(time.Now(), *data.Timestamp) {
logger.Debug().Msgf("Setting sandbox start time to: %v", *data.Timestamp)
if err := setSystemTime(*data.Timestamp); err != nil {
logger.Error().Msgf("Failed to set system time: %v", err)
}
} else {
logger.Debug().Msgf("Current time is within acceptable range of timestamp %v, not setting system time", *data.Timestamp)
}
}
if data.EnvVars != nil {
keys := slices.Collect(maps.Keys(*data.EnvVars))
logger.Debug().Msgf("Setting %d env vars: %s", len(keys), strings.Join(keys, ", "))
a.defaults.EnvVars.ReplaceUserVars(*data.EnvVars)
}
if data.AccessToken.IsSet() {
logger.Debug().Msg("Setting access token")
a.accessToken.TakeFrom(data.AccessToken)
} else if a.accessToken.IsSet() {
logger.Debug().Msg("Clearing access token")
a.accessToken.Destroy()
}
if data.HyperloopIP != nil {
go a.SetupHyperloop(*data.HyperloopIP)
}
if data.DefaultUser != nil && *data.DefaultUser != "" {
logger.Debug().Msgf("Setting default user to: %s", *data.DefaultUser)
a.defaults.User = *data.DefaultUser
}
if data.DefaultWorkdir != nil && *data.DefaultWorkdir != "" {
logger.Debug().Msgf("Setting default workdir to: %s", *data.DefaultWorkdir)
a.defaults.Workdir = data.DefaultWorkdir
}
if data.CaBundle != nil && *data.CaBundle != "" {
if err := a.caCertInstaller.Install(ctx, *data.CaBundle); err != nil {
return fmt.Errorf("failed to install CA bundle: %w", err)
}
}
if data.VolumeMounts != nil {
if err := a.setupNFS(ctx, logger, data.LifecycleID, *data.VolumeMounts); err != nil {
return fmt.Errorf("failed to setup NFS volumes: %w", err)
}
}
return nil
}
// userCgroupsToFreeze is the cgroup set frozen pre-pause and thawed on /init.
var userCgroupsToFreeze = []cgroups.ProcessType{
cgroups.ProcessTypeUser,
cgroups.ProcessTypePTY,
}
// PostFreeze freezes user/pty cgroups directly (no Process.Start / shell).
// Orchestrator calls this just before pause; the frozen state persists into the
// snapshot and /init thaws on resume. Best-effort: tries every cgroup even if
// one fails so we freeze as many as possible before the snapshot.
func (a *API) PostFreeze(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
logger := a.logger.With().Str(string(logs.OperationIDKey), logs.AssignOperationID()).Logger()
if err := a.freezeLock.Acquire(r.Context(), 1); err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
defer a.freezeLock.Release(1)
var errs []error
for _, pt := range userCgroupsToFreeze {
if err := a.cgroupManager.Freeze(pt); err != nil {
logger.Error().Err(err).Msgf("freeze %s cgroup", pt)
errs = append(errs, fmt.Errorf("freeze %s cgroup: %w", pt, err))
}
}
if len(errs) > 0 {
jsonError(w, http.StatusInternalServerError, errors.Join(errs...))
return
}
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(http.StatusNoContent)
}
// PostUnfreeze thaws user/pty cgroups directly. Exists ONLY for the
// orchestrator's pause-failure rollback path; the resume thaw runs via /init's
// deferred unfreeze and must not be replaced by this endpoint. Best-effort: tries
// every cgroup even if one fails so a partial failure cannot leave the rest frozen.
func (a *API) PostUnfreeze(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
ctx := r.Context()
logger := a.logger.With().Str(string(logs.OperationIDKey), logs.AssignOperationID()).Logger()
if err := a.freezeLock.Acquire(context.WithoutCancel(ctx), 1); err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
defer a.freezeLock.Release(1)
var errs []error
for _, pt := range userCgroupsToFreeze {
if err := a.cgroupManager.Unfreeze(pt); err != nil {
logger.Error().Err(err).Msgf("unfreeze %s cgroup", pt)
errs = append(errs, fmt.Errorf("unfreeze %s cgroup: %w", pt, err))
}
}
if len(errs) > 0 {
jsonError(w, http.StatusInternalServerError, errors.Join(errs...))
return
}
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(http.StatusNoContent)
}
// unfreezeUserCgroups unfreezes user/pty cgroups (idempotent if not frozen).
// Wraps the context with WithoutCancel so the unfreeze always completes.
func (a *API) unfreezeUserCgroups(ctx context.Context, logger zerolog.Logger) {
_ = a.freezeLock.Acquire(context.WithoutCancel(ctx), 1)
defer a.freezeLock.Release(1)
for _, pt := range userCgroupsToFreeze {
if err := a.cgroupManager.Unfreeze(pt); err != nil {
logger.Warn().Err(err).Msgf("unfreeze %s cgroup", pt)
}
}
}
func writeInitError(w http.ResponseWriter, logger zerolog.Logger, err error) {
switch {
case errors.Is(err, ErrAccessTokenMismatch), errors.Is(err, ErrAccessTokenResetNotAuthorized):
w.WriteHeader(http.StatusUnauthorized)
default:
logger.Error().Msgf("Failed to set data: %v", err)
w.WriteHeader(http.StatusBadRequest)
}
w.Write([]byte(err.Error()))
}
var nfsOptions = strings.Join([]string{
// wait for data to be sent to proxy server before returning.
// async might cause issues if the sandbox is shut down suddenly.
"sync",
"rsize=1048576", // 1 MB read buffer
"wsize=1048576", // 1 MB write buffer
"mountproto=tcp", // nfs proxy only supports tcp
"mountport=2049", // nfs proxy only supports mounting on port 2049
"proto=tcp", // nfs proxy only supports tcp
"port=2049", // nfs proxy only supports mounting on port 2049
"nfsvers=3", // nfs proxy is nfs version 3
"noacl", // no reason for acl in the sandbox
// disable caching so that pause/resume works correctly
"noac",
"lookupcache=none",
}, ",")
const nfsMountTimeout = 10 * time.Second
func (a *API) setupNFS(ctx context.Context, logger zerolog.Logger, lifecycleID *string, mounts []VolumeMount) (e error) {
// Prevent concurrent mounting attempts
if !a.isMountingNFS.CompareAndSwap(false, true) {
logger.Debug().Msg("NFS volumes already mounting")
return e
}
defer a.isMountingNFS.Store(false)
logger.Debug().Msg("Setting up NFS volumes")
ctx = context.WithoutCancel(ctx) // don't allow request context cancellation to propagate
ctx, cancel := context.WithTimeout(ctx, nfsMountTimeout) // don't let the nfs mount run forever
defer cancel()
wg, wgCtx := errgroup.WithContext(ctx)
requestLifecycleID := derefString(lifecycleID)
for _, volume := range mounts {
// Check if this path is already mounted for the current lifecycle
mountedLifecycle, isMounted := a.mountedPaths.Load(volume.Path)
mountedLifecycleID := asString(mountedLifecycle)
if !shouldRemountNFS(isMounted, mountedLifecycleID, requestLifecycleID) {
logger.Debug().Msgf("Skipping %q, already mounted for lifecycle %q", volume.Path, requestLifecycleID)
continue
}
if isMounted {
logger.Debug().Msgf("Lifecycle changed for %q: %q -> %q", volume.Path, mountedLifecycleID, requestLifecycleID)
}
logger.Debug().Msgf("Setting up %s at %q", volume.NfsTarget, volume.Path)
wg.Go(func() error {
// Unmount if currently mounted (handles stale mounts from previous lifecycle)
if err := a.unmountNFS(wgCtx, logger, volume.Path); err != nil {
return fmt.Errorf("failed to unmount stale NFS mount at %q: %w", volume.Path, err)
}
if err := a.mountNFS(wgCtx, volume.NfsTarget, volume.Path); err != nil {
return fmt.Errorf("failed to mount NFS at %q: %w", volume.Path, err)
}
a.mountedPaths.Store(volume.Path, requestLifecycleID)
return nil
})
}
return wg.Wait()
}
func (a *API) unmountNFS(ctx context.Context, logger zerolog.Logger, path string) error {
// Check if actually mounted before trying to unmount.
// findmnt returns exit code 1 when path is not a mount point - that's not an error.
data, err := exec.CommandContext(ctx, "findmnt", "--noheadings", "--output", "SOURCE", path).CombinedOutput()
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 {
// Not mounted - nothing to unmount
return nil
}
return fmt.Errorf("failed to check if %q is mounted: %w", path, err)
}
source := strings.TrimSpace(string(data))
if source == "" {
return nil // already unmounted
}
logger.Debug().Msgf("Unmounting stale NFS mount at %q (was: %s)", path, source)
if data, err = exec.CommandContext(ctx, "umount", "--force", path).CombinedOutput(); err != nil {
logger.Warn().Err(err).Str("path", path).Str("output", string(data)).Msg("Forced NFS umount failed, falling back to lazy")
// Fresh ctx so the forced umount running out of budget doesn't kill the fallback.
lazyCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 2*time.Second)
defer cancel()
if data, err = exec.CommandContext(lazyCtx, "umount", "--lazy", path).CombinedOutput(); err != nil {
return fmt.Errorf("failed to unmount stale NFS mount at %q: %w\n%s", path, err, string(data))
}
}
a.mountedPaths.Delete(path)
return nil
}
func (a *API) mountNFS(ctx context.Context, nfsTarget, path string) error {
commands := [][]string{
{"mkdir", "-p", path},
{"mount", "-v", "-t", "nfs", "-o", "fg,hard," + nfsOptions, nfsTarget, path},
}
for _, command := range commands {
data, err := exec.CommandContext(ctx, command[0], command[1:]...).CombinedOutput()
if err != nil {
return fmt.Errorf("`%s` failed: %w\n%s", strings.Join(command, " "), err, string(data))
}
}
return nil
}
// shouldRemountNFS determines if an NFS volume should be remounted based on lifecycle IDs.
// Returns true if remount is needed, false if we should skip (already mounted for this lifecycle).
//
// Truth table (treating nil/empty as equivalent):
// - mounted="" + request="" → false (no remount - would cause infinite loop)
// - mounted="abc" + request="" → true (remount - lifecycle cleared)
// - mounted="" + request="abc" → true (remount - new lifecycle)
// - mounted="abc" + request="abc" → false (no remount - same lifecycle)
// - mounted="abc" + request="xyz" → true (remount - different lifecycle)
func shouldRemountNFS(isMounted bool, mountedLifecycleID, requestLifecycleID string) bool {
if !isMounted {
return true // not mounted yet, need to mount
}
return mountedLifecycleID != requestLifecycleID
}
func derefString(p *string) string {
if p == nil {
return ""
}
return *p
}
func asString(v any) string {
if v == nil {
return ""
}
s, _ := v.(string)
return s
}
func (a *API) SetupHyperloop(address string) {
a.hyperloopLock.Lock()
defer a.hyperloopLock.Unlock()
if err := rewriteHostsFile(address, "/etc/hosts"); err != nil {
a.logger.Error().Err(err).Msg("failed to modify hosts file")
} else {
a.defaults.EnvVars.Store("E2B_EVENTS_ADDRESS", fmt.Sprintf("http://%s", address))
}
}
const eventsHost = "events.e2b.local"
func rewriteHostsFile(address, path string) error {
hosts, err := txeh.NewHosts(&txeh.HostsConfig{
ReadFilePath: path,
WriteFilePath: path,
})
if err != nil {
return fmt.Errorf("failed to create hosts: %w", err)
}
// Update /etc/hosts to point events.e2b.local to the hyperloop IP
// This will remove any existing entries for events.e2b.local first
ipFamily, err := getIPFamily(address)
if err != nil {
return fmt.Errorf("failed to get ip family: %w", err)
}
if ok, current, _ := hosts.HostAddressLookup(eventsHost, ipFamily); ok && current == address {
return nil // nothing to be done
}
hosts.AddHost(address, eventsHost)
return hosts.Save()
}
var (
ErrInvalidAddress = errors.New("invalid IP address")
ErrUnknownAddressFormat = errors.New("unknown IP address format")
)
func getIPFamily(address string) (txeh.IPFamily, error) {
addressIP, err := netip.ParseAddr(address)
if err != nil {
return txeh.IPFamilyV4, fmt.Errorf("failed to parse IP address: %w", err)
}
switch {
case addressIP.Is4():
return txeh.IPFamilyV4, nil
case addressIP.Is6():
return txeh.IPFamilyV6, nil
default:
return txeh.IPFamilyV4, fmt.Errorf("%w: %s", ErrUnknownAddressFormat, address)
}
}
// shouldSetSystemTime returns true if the current time differs significantly from the received timestamp,
// indicating the system clock should be adjusted. Returns true when the sandboxTime is more than
// maxTimeInPast before the hostTime or more than maxTimeInFuture after the hostTime.
func shouldSetSystemTime(sandboxTime, hostTime time.Time) bool {
return sandboxTime.Before(hostTime.Add(-maxTimeInPast)) || sandboxTime.After(hostTime.Add(maxTimeInFuture))
}