agent.go 4.4 KB
Newer Older
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
package libenclave // import "github.com/opencontainers/runc/libenclave"

import (
	"fmt"
	"github.com/opencontainers/runc/libcontainer/utils"
	pb "github.com/opencontainers/runc/libenclave/proto"
	"github.com/sirupsen/logrus"
	"golang.org/x/sys/unix"
	"net"
	"os"
	"path/filepath"
	"strings"
)

const agentServicePath = "agent.sock"

var instanceId int

func CreateParentAgentPipe(root string, uid, gid int) (lnFile *os.File, err error) {
	// The Linux kernel only allows unix domain socket paths less than
	// 108 bytes, so chdir() to avoid errors caused by excessively long
	// path.
	cwd, err := os.Getwd()
	if err != nil {
		return nil, err
	}
	if err = os.Chdir(root); err != nil {
		return nil, err
	}
	defer os.Chdir(cwd)

	ln, err := net.Listen("unix", agentServicePath)
	if err != nil {
		return nil, err
	}
	defer func() {
		if err != nil {
			ln.Close()
		}
	}()
	unixListener, ok := ln.(*net.UnixListener)
	if !ok {
		return nil, fmt.Errorf("casting to UnixListener failed")
	}
	// By default the underlying socket file is removed when the
	// listener is closed, no matter what the opened file count is.
	// This behavior will cause the connection failure when the
	// incoming child runelet attempts to connect to agent service.
	unixListener.SetUnlinkOnClose(false)
	defer func() {
		if err != nil {
			unixListener.SetUnlinkOnClose(true)
		}
	}()
	lnFile, err = unixListener.File()
	if err != nil {
		return nil, err
	}
	defer func() {
		if err != nil {
			lnFile.Close()
		}
	}()

	err = unix.Chmod(agentServicePath, 0622)
	if err != nil {
		return nil, err
	}
	err = os.Chown(agentServicePath, uid, gid)
	if err != nil {
		return nil, err
	}

	return lnFile, nil
}

func CreateChildAgentPipe(root string) (*os.File, error) {
	path := filepath.Join(root, "agent.sock")
	conn, err := net.Dial("unix", path)
	if err != nil {
		return nil, err
	}
	defer conn.Close()

	unixConn, ok := conn.(*net.UnixConn)
	if !ok {
		return nil, fmt.Errorf("casting to UnixConn failed")
	}

	connFile, err := unixConn.File()
	if err != nil {
		return nil, err
	}
	return connFile, nil
}

func startAgentService(agentPipe *os.File, notifyExit <-chan struct{}) <-chan struct{} {
	isDead := make(chan struct{})
	go func() {
		defer close(isDead)

		ln, err := net.FileListener(agentPipe)
		if err != nil {
			return
		}
		defer ln.Close()
		uln, ok := ln.(*net.UnixListener)
		if !ok {
			return
		}

		for {
			select {
			case <-notifyExit:
				logrus.Debug("TODO: notifying exit event")
				return
			default:
			}

			client, err := uln.Accept()
			if err != nil {
				logrus.Debugf("accept() failed with %v", err)
				return
			}
			instanceId += 1
			go handleRequest(client, instanceId)
		}
	}()

	return isDead
}

func handleRequest(conn net.Conn, id int) {
	defer conn.Close()

	if enclaveRuntime == nil {
		logrus.Fatal("race with parent runelet")
		return
	}

	var err error
	resp := &pb.AgentServiceResponse{}
	resp.Exec = &pb.AgentServiceResponse_Execute{}
	exitCode := int32(1)
	defer func() {
		resp.Exec.ExitCode = exitCode
		if err != nil {
			resp.Exec.Error = fmt.Sprint(err)
		}
		protoBufWrite(conn, resp)
	}()

	req := &pb.AgentServiceRequest{}
	if err = protoBufRead(conn, req); err != nil {
		return
	}

	c, ok := conn.(*net.UnixConn)
	if !ok {
		err = fmt.Errorf("casting to UnixConn failed")
		return
	}
	connFile, err := c.File()
	if err != nil {
		return
	}
	defer connFile.Close()

	// Retrieve signal pipe.
	signalPipe, err := utils.RecvFd(connFile)
	if err != nil {
		return
	}
	go relaySignal(signalPipe, id)

	// Retrieve stdio fds.
	stdin, err := utils.RecvFd(connFile)
	if err != nil {
		return
	}
	defer stdin.Close()
	stdout, err := utils.RecvFd(connFile)
	if err != nil {
		return
	}
	defer stdout.Close()
	stderr, err := utils.RecvFd(connFile)
	if err != nil {
		return
	}
	defer stderr.Close()
	stdio := [3]*os.File{
		stdin, stdout, stderr,
	}

	cmd := req.Exec.GetArgv()
	envp := req.Exec.GetEnvp()
	exitCode, err = enclaveRuntime.ExecutePayload(strings.Split(cmd, " "), strings.Split(envp, " "), stdio)
	if err != nil {
		return
	}

	// TODO: sync up with relaySignal()

	logrus.Debug("remote exec normally exits")
}

func relaySignal(signalPipe *os.File, id int) {
	defer signalPipe.Close()

	for {
		req := &pb.AgentServiceRequest{}
213
		if err := protoBufRead(signalPipe, req); err != nil {
214 215 216
			return
		}

217
		err := enclaveRuntime.KillPayload(id, int(req.Kill.Sig))
218 219 220 221 222 223
		if err != nil {
			logrus.Errorf("unable to kill payload with sig %d by %d: %v\n", int(req.Kill.Sig), id, err)
			return
		}
	}
}