Skip to content

Commit a996c98

Browse files
csweichelroboquat
authored andcommitted
[workspacekit] Add ring2 enclave support
1 parent 264331a commit a996c98

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

components/common-go/nsenter/utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func Run(pid int, args []string, addFD []*os.File, enterNamespace ...Namespace)
7575

7676
cmd.Stdout = os.Stdout
7777
cmd.Stderr = os.Stderr
78+
cmd.Stdin = os.Stdin
7879
err := cmd.Run()
7980
if err != nil {
8081
return xerrors.Errorf("cannot run handler: %w", err)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package cmd
6+
7+
import (
8+
"log"
9+
"os"
10+
11+
"github.com/spf13/cobra"
12+
"golang.org/x/sys/unix"
13+
14+
"github.com/gitpod-io/gitpod/common-go/nsenter"
15+
)
16+
17+
var nsenterOpts struct {
18+
Target int
19+
MountNS bool
20+
}
21+
22+
var nsenterCmd = &cobra.Command{
23+
Use: "nsenter <cmd> <args ...>",
24+
Short: "enters namespaces and executes the arg",
25+
Args: cobra.MinimumNArgs(1),
26+
Aliases: []string{"handler"},
27+
Run: func(_ *cobra.Command, args []string) {
28+
if os.Getenv("_LIBNSENTER_INIT") != "" {
29+
err := unix.Exec(args[0], args, os.Environ())
30+
if err != nil {
31+
log.Fatalf("cannot exec: %v", err)
32+
}
33+
return
34+
}
35+
36+
var ns []nsenter.Namespace
37+
if nsenterOpts.MountNS {
38+
ns = append(ns, nsenter.NamespaceMount)
39+
}
40+
err := nsenter.Run(nsenterOpts.Target, args, nil, ns...)
41+
if err != nil {
42+
log.Fatal(err)
43+
}
44+
},
45+
}
46+
47+
func init() {
48+
rootCmd.AddCommand(nsenterCmd)
49+
50+
nsenterCmd.Flags().IntVar(&nsenterOpts.Target, "target", 0, "target PID")
51+
nsenterCmd.Flags().BoolVar(&nsenterOpts.MountNS, "mount", false, "enter mount namespace")
52+
}

components/workspacekit/cmd/rings.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"os/signal"
1919
"path/filepath"
2020
"runtime"
21+
"strconv"
2122
"strings"
2223
"syscall"
2324
"time"
@@ -479,6 +480,18 @@ var ring1Cmd = &cobra.Command{
479480
}()
480481
}
481482

483+
if enclave := os.Getenv("WORKSPACEKIT_RING2_ENCLAVE"); enclave != "" {
484+
ecmd := exec.Command("/proc/self/exe", append([]string{"nsenter", "--target", strconv.Itoa(cmd.Process.Pid), "--mount"}, strings.Fields(enclave)...)...)
485+
ecmd.Stdout = os.Stdout
486+
ecmd.Stderr = os.Stderr
487+
488+
err := ecmd.Start()
489+
if err != nil {
490+
log.WithError(err).WithField("cmd", enclave).Error("cannot run enclave")
491+
return
492+
}
493+
}
494+
482495
go func() {
483496
err := lift.ServeLift(ctx, lift.DefaultSocketPath)
484497
if err != nil {

0 commit comments

Comments
 (0)