Skip to content

Commit b0a25b1

Browse files
authored
Merge pull request #96 from superfly/jailer-improvements
Improves and fixes jailer
2 parents 8f8f22d + e58d10b commit b0a25b1

File tree

7 files changed

+67
-25
lines changed

7 files changed

+67
-25
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
firecracker
2+
jailer
23
firecracker-*
34
vmlinux

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# [ Unreleased ]
22

3+
* Fixes a bug where fifos were not working properly with jailer enabled (#96)
34
* Fixes bug where context was not being used at all during startVM (#86)
45
* Updates the jailer's socket path to point to the unix socket in the jailer's workspace (#86)
56
* Fixes bug where default socketpath would always be used when not using jailer (#84).

handlers.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
const (
2323
StartVMMHandlerName = "fcinit.StartVMM"
2424
BootstrapLoggingHandlerName = "fcinit.BootstrapLogging"
25+
CreateLogFilesHandlerName = "fcinit.CreateLogFilesHandler"
2526
CreateMachineHandlerName = "fcinit.CreateMachine"
2627
CreateBootSourceHandlerName = "fcinit.CreateBootSource"
2728
AttachDrivesHandlerName = "fcinit.AttachDrives"
@@ -107,6 +108,29 @@ var StartVMMHandler = Handler{
107108
},
108109
}
109110

111+
// CreateLogFilesHandler is a named handler that will create the fifo log files
112+
var CreateLogFilesHandler = Handler{
113+
Name: CreateLogFilesHandlerName,
114+
Fn: func(ctx context.Context, m *Machine) error {
115+
logFifoPath := m.cfg.LogFifo
116+
metricsFifoPath := m.cfg.MetricsFifo
117+
118+
if len(logFifoPath) == 0 || len(metricsFifoPath) == 0 {
119+
// logging is disabled
120+
return nil
121+
}
122+
123+
if err := createFifos(logFifoPath, metricsFifoPath); err != nil {
124+
m.logger.Errorf("Unable to set up logging: %s", err)
125+
return err
126+
}
127+
128+
m.logger.Debug("Created metrics and logging fifos.")
129+
130+
return nil
131+
},
132+
}
133+
110134
// BootstrapLoggingHandler is a named handler that will set up fifo logging of
111135
// firecracker process.
112136
var BootstrapLoggingHandler = Handler{
@@ -180,6 +204,7 @@ func NewSetMetadataHandler(metadata interface{}) Handler {
180204

181205
var defaultFcInitHandlerList = HandlerList{}.Append(
182206
StartVMMHandler,
207+
CreateLogFilesHandler,
183208
BootstrapLoggingHandler,
184209
CreateMachineHandler,
185210
CreateBootSourceHandler,

jailer.go

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,7 @@ func jail(ctx context.Context, m *Machine, cfg *Config) error {
309309
stderr = os.Stderr
310310
}
311311

312-
stdin := cfg.JailerCfg.Stdin
313-
if stdin == nil {
314-
stdin = os.Stdin
315-
}
316-
317-
m.cmd = NewJailerCommandBuilder().
312+
builder := NewJailerCommandBuilder().
318313
WithID(cfg.JailerCfg.ID).
319314
WithUID(*cfg.JailerCfg.UID).
320315
WithGID(*cfg.JailerCfg.GID).
@@ -324,9 +319,13 @@ func jail(ctx context.Context, m *Machine, cfg *Config) error {
324319
WithDaemonize(cfg.JailerCfg.Daemonize).
325320
WithSeccompLevel(cfg.JailerCfg.SeccompLevel).
326321
WithStdout(stdout).
327-
WithStderr(stderr).
328-
WithStdin(stdin).
329-
Build(ctx)
322+
WithStderr(stderr)
323+
324+
if stdin := cfg.JailerCfg.Stdin; stdin != nil {
325+
builder = builder.WithStdin(stdin)
326+
}
327+
328+
m.cmd = builder.Build(ctx)
330329

331330
if err := cfg.JailerCfg.ChrootStrategy.AdaptHandlers(&m.Handlers); err != nil {
332331
return err
@@ -375,6 +374,29 @@ func LinkFilesHandler(rootfs, kernelImageFileName string) Handler {
375374
}
376375

377376
m.cfg.KernelImagePath = kernelImageFileName
377+
378+
for _, fifoPath := range []*string{&m.cfg.LogFifo, &m.cfg.MetricsFifo} {
379+
if fifoPath == nil || *fifoPath == "" {
380+
continue
381+
}
382+
383+
fileName := filepath.Base(*fifoPath)
384+
if err := linkFileToRootFS(
385+
m.cfg.JailerCfg,
386+
filepath.Join(rootfs, fileName),
387+
*fifoPath,
388+
); err != nil {
389+
return err
390+
}
391+
392+
if err := os.Chown(filepath.Join(rootfs, fileName), *m.cfg.JailerCfg.UID, *m.cfg.JailerCfg.GID); err != nil {
393+
return err
394+
}
395+
396+
// update fifoPath as jailer works relative to the chroot dir
397+
*fifoPath = fileName
398+
}
399+
378400
return nil
379401
},
380402
}
@@ -395,18 +417,18 @@ func NewNaiveChrootStrategy(rootfs, kernelImagePath string) NaiveChrootStrategy
395417
}
396418
}
397419

398-
// ErrCreateMachineHandlerMissing occurs when the CreateMachineHandler is not
399-
// present in FcInit.
400-
var ErrCreateMachineHandlerMissing = fmt.Errorf("%s is missing from FcInit's list", CreateMachineHandlerName)
420+
// ErrRequiredHandlerMissing occurs when a required handler is not present in
421+
// the handler list.
422+
var ErrRequiredHandlerMissing = fmt.Errorf("required handler is missing from FcInit's list")
401423

402424
// AdaptHandlers will inject the LinkFilesHandler into the handler list.
403425
func (s NaiveChrootStrategy) AdaptHandlers(handlers *Handlers) error {
404-
if !handlers.FcInit.Has(CreateMachineHandlerName) {
405-
return ErrCreateMachineHandlerMissing
426+
if !handlers.FcInit.Has(CreateLogFilesHandlerName) {
427+
return ErrRequiredHandlerMissing
406428
}
407429

408430
handlers.FcInit = handlers.FcInit.AppendAfter(
409-
CreateMachineHandlerName,
431+
CreateLogFilesHandlerName,
410432
LinkFilesHandler(filepath.Join(s.Rootfs, rootfsFolderName), filepath.Base(s.KernelImagePath)),
411433
)
412434

jailer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func TestJailerBuilder(t *testing.T) {
111111
func TestJail(t *testing.T) {
112112
m := &Machine{
113113
Handlers: Handlers{
114-
FcInit: HandlerList{}.Append(CreateMachineHandler),
114+
FcInit: defaultFcInitHandlerList,
115115
},
116116
}
117117
cfg := &Config{

machine.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,6 @@ func (m *Machine) setupLogging(ctx context.Context) error {
417417
return nil
418418
}
419419

420-
if err := createFifos(m.cfg.LogFifo, m.cfg.MetricsFifo); err != nil {
421-
m.logger.Errorf("Unable to set up logging: %s", err)
422-
return err
423-
}
424-
425-
m.logger.Debug("Created metrics and logging fifos.")
426-
427420
l := models.Logger{
428421
LogFifo: String(m.cfg.LogFifo),
429422
Level: String(m.cfg.LogLevel),

machine_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ func TestJailerMicroVMExecution(t *testing.T) {
141141
os.MkdirAll(jailerTestPath, 0777)
142142

143143
socketPath := filepath.Join(jailerTestPath, "firecracker", "TestJailerMicroVMExecution.socket")
144-
logFifo := filepath.Join(testDataPath, "firecracker.log")
145-
metricsFifo := filepath.Join(testDataPath, "firecracker-metrics")
144+
logFifo := filepath.Join(tmpDir, "firecracker.log")
145+
metricsFifo := filepath.Join(tmpDir, "firecracker-metrics")
146146
defer func() {
147147
os.Remove(socketPath)
148148
os.Remove(logFifo)

0 commit comments

Comments
 (0)