Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure the network namespace before executing jailer #489

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,26 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)

if cfg.JailerCfg != nil {
m.Handlers.Validation = m.Handlers.Validation.Append(JailerConfigValidationHandler)

if cfg.NetNS == "" && cfg.NetworkInterfaces.cniInterface() != nil {
cfg.NetNS = filepath.Join(defaultNetNSDir, cfg.VMID)

// If the network namespace is set, we need to setup the network prior to running the jailer.
err := cfg.ValidateNetwork()
if err != nil {
return nil, fmt.Errorf("failed to validate network configuration: %w", err)
}
m.Handlers.Validation = m.Handlers.Validation.Remove(ValidateNetworkCfgHandlerName)
m.Handlers.FcInit = m.Handlers.FcInit.Remove(SetupNetworkHandlerName)

jailLog := log.New()
err, cleanupFuncs := cfg.NetworkInterfaces.setupNetwork(ctx, cfg.VMID, cfg.NetNS, log.NewEntry(jailLog), cfg.JailerCfg.UID, cfg.JailerCfg.GID)
m.cleanupFuncs = append(m.cleanupFuncs, cleanupFuncs...)
if err != nil {
return nil, fmt.Errorf("failed to setup network prior to jailing: %w", err)
}
}

if err := jail(ctx, m, &cfg); err != nil {
return nil, err
}
Expand Down Expand Up @@ -492,7 +512,7 @@ func (m *Machine) GetFirecrackerVersion(ctx context.Context) (string, error) {
}

func (m *Machine) setupNetwork(ctx context.Context) error {
err, cleanupFuncs := m.Cfg.NetworkInterfaces.setupNetwork(ctx, m.Cfg.VMID, m.Cfg.NetNS, m.logger)
err, cleanupFuncs := m.Cfg.NetworkInterfaces.setupNetwork(ctx, m.Cfg.VMID, m.Cfg.NetNS, m.logger, nil, nil)
m.cleanupFuncs = append(m.cleanupFuncs, cleanupFuncs...)
return err
}
Expand Down
18 changes: 18 additions & 0 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ func (networkInterfaces NetworkInterfaces) setupNetwork(
vmID string,
netNSPath string,
logger *log.Entry,
uid *int,
gid *int,
) (error, []func() error) {
var cleanupFuncs []func() error

Expand All @@ -111,6 +113,22 @@ func (networkInterfaces NetworkInterfaces) setupNetwork(
cniNetworkInterface.CNIConfiguration.containerID = vmID
cniNetworkInterface.CNIConfiguration.netNSPath = netNSPath
cniNetworkInterface.CNIConfiguration.setDefaults()
if uid != nil && gid != nil {
cniNetworkInterface.CNIConfiguration.Args = [][2]string{
{
"IgnoreUnknown",
"true",
},
{
"TC_REDIRECT_TAP_UID",
fmt.Sprintf("%d", *uid),
},
{
"TC_REDIRECT_TAP_GID",
fmt.Sprintf("%d", *gid),
},
}
}

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