Skip to content

Commit

Permalink
fix(initrd): Do not follow symlinks when creating archives (#751)
Browse files Browse the repository at this point in the history
Reviewed-by: Alexander Jung <alex@unikraft.io>
Approved-by: Alexander Jung <alex@unikraft.io>
  • Loading branch information
nderjung authored Aug 29, 2023
2 parents cfcd31f + e146c6b commit 31e48fb
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions initrd/initrd.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,26 @@ func NewFromMapping(workdir, output string, maps ...string) (*InitrdConfig, erro

initrd.Files[path] = internal

data, err := os.ReadFile(path)
if err != nil {
return err
var data []byte
targetLink := ""
if info, err := d.Info(); err == nil && info.Mode()&os.ModeSymlink != 0 {
targetLink, err = os.Readlink(path)
if err != nil {
return err
}
data = []byte(targetLink)
} else if d.Type().IsRegular() {
data, err = os.ReadFile(path)
if err != nil {
return err
}
}

if err := writer.WriteHeader(&cpio.Header{
Name: internal,
Mode: cpio.FileMode(d.Type().Perm()),
Size: info.Size(),
Name: internal,
Linkname: targetLink,
Mode: cpio.FileMode(d.Type().Perm()),
Size: info.Size(),
}); err != nil {
return err
}
Expand Down

0 comments on commit 31e48fb

Please sign in to comment.