From e146c6bfe0a860469268c3f564cc74321db18922 Mon Sep 17 00:00:00 2001 From: Cezar Craciunoiu Date: Mon, 28 Aug 2023 17:34:38 +0300 Subject: [PATCH] fix(initrd): Do not follow symlinks when creating archives Signed-off-by: Cezar Craciunoiu --- initrd/initrd.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/initrd/initrd.go b/initrd/initrd.go index e541397d8..c0adbab55 100644 --- a/initrd/initrd.go +++ b/initrd/initrd.go @@ -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 }