Skip to content

Commit 49a8488

Browse files
committed
fix: correct bugs found during codebase audit
- Use annotHypervisor instead of annotType in isRunning() - Remove nil error wrapping in run.go - Fix potential nil-deref on consoleFile in rootfs.go - Use errors.Is() for sentinel error comparison - Propagate errors in getInitPid instead of swallowing - Propagate error in deleteAllTCFilters Signed-off-by: vinayakjeet <vinayakjeetog@gmail.com>
1 parent 5711284 commit 49a8488

File tree

5 files changed

+9
-10
lines changed

5 files changed

+9
-10
lines changed

cmd/urunc/run.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package main
1616

1717
import (
1818
"context"
19-
"fmt"
2019
"os"
2120

2221
"github.com/sirupsen/logrus"
@@ -75,6 +74,6 @@ var runCommand = &cli.Command{
7574
if err := startUnikontainer(cmd); err != nil {
7675
return err
7776
}
78-
return fmt.Errorf("urunc run failed: %w", nil)
77+
return nil
7978
},
8079
}

pkg/network/network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ func deleteAllTCFilters(device netlink.Link) error {
387387
parent := uint32(netlink.HANDLE_ROOT)
388388
tapFilters, err := netlink.FilterList(device, parent)
389389
if err != nil {
390-
return nil
390+
return err
391391
}
392392
allFilters = append(allFilters, tapFilters...)
393393
device, err = discoverContainerIface()

pkg/unikontainers/rootfs.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,16 +446,16 @@ func prepareMonRootfs(monRootfs string, monitorPath string, monitorDataPath stri
446446

447447
// Create /dev/console file
448448
consolePath := filepath.Join(monRootfs, "/dev/console")
449-
consoleFile, err := os.Create(consolePath)
450-
if err != nil && !os.IsExist(err) {
449+
consoleFile, err := os.OpenFile(consolePath, os.O_CREATE|os.O_WRONLY, 0o666)
450+
if err != nil {
451451
return fmt.Errorf("failed to create /dev/console: %w", err)
452452
}
453+
defer consoleFile.Close()
454+
453455
// Ensure correct permissions
454456
if err := consoleFile.Chmod(0o666); err != nil {
455-
consoleFile.Close()
456457
return fmt.Errorf("failed to chmod /dev/console: %w", err)
457458
}
458-
consoleFile.Close()
459459

460460
return nil
461461
}

pkg/unikontainers/unikontainers.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ func (u *Unikontainer) Exec(metrics m.Writer) error {
443443

444444
// unikernel
445445
err = unikernel.Init(unikernelParams)
446-
if err == unikernels.ErrUndefinedVersion || err == unikernels.ErrVersionParsing {
446+
if errors.Is(err, unikernels.ErrUndefinedVersion) ||
447+
errors.Is(err, unikernels.ErrVersionParsing) {
447448
uniklog.WithError(err).Error("an error occurred while initializing the unikernel")
448449
} else if err != nil {
449450
return err
@@ -1146,7 +1147,7 @@ func (u *Unikontainer) SendMessage(message IPCMessage) error {
11461147

11471148
// isRunning returns true if the PID is alive or hedge.ListVMs returns our containerID
11481149
func (u *Unikontainer) isRunning() bool {
1149-
vmmType := hypervisors.VmmType(u.State.Annotations[annotType])
1150+
vmmType := hypervisors.VmmType(u.State.Annotations[annotHypervisor])
11501151
if vmmType != hypervisors.HedgeVmm {
11511152
return syscall.Kill(u.State.Pid, syscall.Signal(0)) == nil
11521153
}

pkg/unikontainers/utils.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ func getInitPid(filePath string) (float64, error) {
5757
decoder := json.NewDecoder(file)
5858
if err := decoder.Decode(&jsonData); err != nil {
5959
return 0, nil
60-
6160
}
6261

6362
// Extract the specific value "init_process_pid"

0 commit comments

Comments
 (0)