Skip to content

Commit 4c36613

Browse files
committed
Configure the network namespace before executing jailer
Right now there is a bug when trying to start a Firecracker VM with jailer using a CNI where the VM is never joined to the correct network namespace. This is because in its current form, the CNI execution occurs when `fcinit.SetupNetwork` runs, which occurs _after_ the jailer has already created a chroot and dropped privleges. This fixes the problem by executing the `fcinit.SetupNetwork` call _before_ running jailer and removing that hook from the `FcInit` functions later on. It also passes through the UID and GID options to the `tc-redirect-tap` plugin and includes the `IgnoreUnknown` directive so that chained CNI plugins work. Signed-off-by: Dan Norris <[email protected]>
1 parent aa97886 commit 4c36613

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

Diff for: machine.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,26 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
376376

377377
if cfg.JailerCfg != nil {
378378
m.Handlers.Validation = m.Handlers.Validation.Append(JailerConfigValidationHandler)
379+
380+
if cfg.NetNS == "" && cfg.NetworkInterfaces.cniInterface() != nil {
381+
cfg.NetNS = filepath.Join(defaultNetNSDir, cfg.VMID)
382+
383+
// If the network namespace is set, we need to setup the network prior to running the jailer.
384+
err := cfg.ValidateNetwork()
385+
if err != nil {
386+
return nil, fmt.Errorf("failed to validate network configuration: %w", err)
387+
}
388+
m.Handlers.Validation = m.Handlers.Validation.Remove(ValidateNetworkCfgHandlerName)
389+
m.Handlers.FcInit = m.Handlers.FcInit.Remove(SetupNetworkHandlerName)
390+
391+
jailLog := log.New()
392+
err, cleanupFuncs := cfg.NetworkInterfaces.setupNetwork(ctx, cfg.VMID, cfg.NetNS, log.NewEntry(jailLog), cfg.JailerCfg.UID, cfg.JailerCfg.GID)
393+
m.cleanupFuncs = append(m.cleanupFuncs, cleanupFuncs...)
394+
if err != nil {
395+
return nil, fmt.Errorf("failed to setup network prior to jailing: %w", err)
396+
}
397+
}
398+
379399
if err := jail(ctx, m, &cfg); err != nil {
380400
return nil, err
381401
}
@@ -492,7 +512,7 @@ func (m *Machine) GetFirecrackerVersion(ctx context.Context) (string, error) {
492512
}
493513

494514
func (m *Machine) setupNetwork(ctx context.Context) error {
495-
err, cleanupFuncs := m.Cfg.NetworkInterfaces.setupNetwork(ctx, m.Cfg.VMID, m.Cfg.NetNS, m.logger)
515+
err, cleanupFuncs := m.Cfg.NetworkInterfaces.setupNetwork(ctx, m.Cfg.VMID, m.Cfg.NetNS, m.logger, nil, nil)
496516
m.cleanupFuncs = append(m.cleanupFuncs, cleanupFuncs...)
497517
return err
498518
}
@@ -649,7 +669,7 @@ func (m *Machine) startVMM(ctx context.Context) error {
649669
return nil
650670
}
651671

652-
//StopVMM stops the current VMM.
672+
// StopVMM stops the current VMM.
653673
func (m *Machine) StopVMM() error {
654674
return m.stopVMM()
655675
}

Diff for: network.go

+18
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ func (networkInterfaces NetworkInterfaces) setupNetwork(
9898
vmID string,
9999
netNSPath string,
100100
logger *log.Entry,
101+
uid *int,
102+
gid *int,
101103
) (error, []func() error) {
102104
var cleanupFuncs []func() error
103105

@@ -111,6 +113,22 @@ func (networkInterfaces NetworkInterfaces) setupNetwork(
111113
cniNetworkInterface.CNIConfiguration.containerID = vmID
112114
cniNetworkInterface.CNIConfiguration.netNSPath = netNSPath
113115
cniNetworkInterface.CNIConfiguration.setDefaults()
116+
if uid != nil && gid != nil {
117+
cniNetworkInterface.CNIConfiguration.Args = [][2]string{
118+
{
119+
"IgnoreUnknown",
120+
"true",
121+
},
122+
{
123+
"TC_REDIRECT_TAP_UID",
124+
fmt.Sprintf("%d", *uid),
125+
},
126+
{
127+
"TC_REDIRECT_TAP_GID",
128+
fmt.Sprintf("%d", *gid),
129+
},
130+
}
131+
}
114132

115133
// Make sure the netns is setup. If the path doesn't yet exist, it will be
116134
// initialized with a new empty netns.

0 commit comments

Comments
 (0)