Skip to content

Commit 69c9b07

Browse files
jeromegnxibz
authored andcommitted
Fixes jailer to handle fifos correctly
This fix includes proper handling of fifos as before the SDK was not adding the fifo files to the correct root path during jailing. We now have a new handler, CreateLogFilesHandler, that will now create the fifos in the appropriate location and ensure that the fifos have correct permissions. Signed-off-by: xibz <[email protected]>
1 parent 8f8f22d commit 69c9b07

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

handlers.go

Lines changed: 20 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,24 @@ 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 err := createFifos(logFifoPath, metricsFifoPath); err != nil {
119+
m.logger.Errorf("Unable to set up logging: %s", err)
120+
return err
121+
}
122+
123+
m.logger.Debug("Created metrics and logging fifos.")
124+
125+
return nil
126+
},
127+
}
128+
110129
// BootstrapLoggingHandler is a named handler that will set up fifo logging of
111130
// firecracker process.
112131
var BootstrapLoggingHandler = Handler{
@@ -180,6 +199,7 @@ func NewSetMetadataHandler(metadata interface{}) Handler {
180199

181200
var defaultFcInitHandlerList = HandlerList{}.Append(
182201
StartVMMHandler,
202+
CreateLogFilesHandler,
183203
BootstrapLoggingHandler,
184204
CreateMachineHandler,
185205
CreateBootSourceHandler,

jailer.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,24 @@ func LinkFilesHandler(rootfs, kernelImageFileName string) Handler {
375375
}
376376

377377
m.cfg.KernelImagePath = kernelImageFileName
378+
379+
for _, fifoPath := range []string{m.cfg.LogFifo, m.cfg.MetricsFifo} {
380+
if fifoPath == "" {
381+
continue
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+
if err := os.Chown(filepath.Join(rootfs, fileName), *m.cfg.JailerCfg.UID, *m.cfg.JailerCfg.GID); err != nil {
392+
return err
393+
}
394+
}
395+
378396
return nil
379397
},
380398
}
@@ -406,7 +424,7 @@ func (s NaiveChrootStrategy) AdaptHandlers(handlers *Handlers) error {
406424
}
407425

408426
handlers.FcInit = handlers.FcInit.AppendAfter(
409-
CreateMachineHandlerName,
427+
CreateLogFilesHandlerName,
410428
LinkFilesHandler(filepath.Join(s.Rootfs, rootfsFolderName), filepath.Base(s.KernelImagePath)),
411429
)
412430

machine.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ func (m *Machine) Logger() *log.Entry {
152152
return m.logger.WithField("subsystem", userAgent)
153153
}
154154

155+
// PID returns the machine's process ID
156+
func (m *Machine) PID() (int, error) {
157+
if m.cmd == nil || m.cmd.Process == nil {
158+
return 0, errors.New("firecracker process is not running")
159+
}
160+
return m.cmd.Process.Pid, nil
161+
}
162+
155163
// NetworkInterface represents a Firecracker microVM's network interface.
156164
type NetworkInterface struct {
157165
// MacAddress defines the MAC address that should be assigned to the network
@@ -417,13 +425,6 @@ func (m *Machine) setupLogging(ctx context.Context) error {
417425
return nil
418426
}
419427

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-
427428
l := models.Logger{
428429
LogFifo: String(m.cfg.LogFifo),
429430
Level: String(m.cfg.LogLevel),

0 commit comments

Comments
 (0)