Skip to content

Commit 4314812

Browse files
committed
fix: validate Linux spec early to prevent nil dereference panics
Signed-off-by: Rishi Jat <rishijat098@gmail.com>
1 parent 11e894a commit 4314812

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

pkg/unikontainers/unikontainers.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ var uniklog = logrus.WithField("subsystem", "unikontainers")
5151
var ErrQueueProxy = errors.New("this a queue proxy container")
5252
var ErrNotUnikernel = errors.New("this is not a unikernel container")
5353
var ErrNotExistingNS = errors.New("the namespace does not exist")
54+
var ErrMissingLinuxSpec = errors.New("invalid OCI spec: linux section is required")
5455

5556
// Unikontainer holds the data necessary to create, manage and delete unikernel containers
5657
type Unikontainer struct {
@@ -70,6 +71,10 @@ func New(bundlePath string, containerID string, rootDir string, cfg *UruncConfig
7071
return nil, err
7172
}
7273

74+
if spec == nil || spec.Linux == nil {
75+
return nil, ErrMissingLinuxSpec
76+
}
77+
7378
containerName := spec.Annotations["io.kubernetes.cri.container-name"]
7479
if containerName == "queue-proxy" {
7580
uniklog.Warn("This is a queue-proxy container. Adding IP env.")
@@ -125,6 +130,9 @@ func Get(containerID string, rootDir string) (*Unikontainer, error) {
125130
if err != nil {
126131
return nil, err
127132
}
133+
if spec == nil || spec.Linux == nil {
134+
return nil, ErrMissingLinuxSpec
135+
}
128136
u.BaseDir = containerDir
129137
u.RootDir = rootDir
130138
u.Spec = spec
@@ -357,7 +365,7 @@ func (u *Unikontainer) Exec(metrics m.Writer) error {
357365

358366
// ExecArgs
359367
// If memory limit is set in spec, use it instead of the config default value
360-
if u.Spec.Linux.Resources.Memory != nil {
368+
if u.Spec.Linux.Resources != nil && u.Spec.Linux.Resources.Memory != nil {
361369
if u.Spec.Linux.Resources.Memory.Limit != nil {
362370
if *u.Spec.Linux.Resources.Memory.Limit > 0 {
363371
vmmArgs.MemSizeB = uint64(*u.Spec.Linux.Resources.Memory.Limit) // nolint:gosec

0 commit comments

Comments
 (0)