@@ -5,9 +5,10 @@ import (
55 "errors"
66 "fmt"
77 "os"
8- "path/filepath "
8+ "strings "
99
1010 "github.com/cri-o/cri-o/internal/log"
11+ securejoin "github.com/cyphar/filepath-securejoin"
1112 "github.com/opencontainers/selinux/go-selinux/label"
1213 "golang.org/x/sys/unix"
1314 "k8s.io/apimachinery/pkg/util/validation"
@@ -28,12 +29,22 @@ func MountPodLogs(ctx context.Context, kubePodUID, emptyDirVolName, namespace, k
2829 if errs := validation .IsDNS1123Label (emptyDirVolName ); len (errs ) != 0 {
2930 return errors .New ("empty dir vol name is invalid" )
3031 }
31- emptyDirLoggingVolumePath := podEmptyDirPath (kubePodUID , emptyDirVolName )
32+
33+ emptyDirLoggingVolumePath , err := podEmptyDirPath (kubePodUID , emptyDirVolName )
34+ if err != nil {
35+ return fmt .Errorf ("failed to get empty dir path: %w" , err )
36+ }
37+
3238 if _ , err := os .Stat (emptyDirLoggingVolumePath ); err != nil {
3339 return fmt .Errorf ("failed to find %v: %w" , emptyDirLoggingVolumePath , err )
3440 }
3541 podLogsDirectory := namespace + "_" + kubeName + "_" + kubePodUID
36- podLogsPath := filepath .Join (kubeletPodLogsRootDir , podLogsDirectory )
42+
43+ podLogsPath , err := securejoin .SecureJoin (kubeletPodLogsRootDir , podLogsDirectory )
44+ if err != nil {
45+ return fmt .Errorf ("failed to join %v and %v: %w" , kubeletPodLogsRootDir , podLogsDirectory , err )
46+ }
47+
3748 log .Infof (ctx , "Mounting from %s to %s for linked logs" , podLogsPath , emptyDirLoggingVolumePath )
3849 if err := unix .Mount (podLogsPath , emptyDirLoggingVolumePath , "bind" , unix .MS_BIND | unix .MS_RDONLY , "" ); err != nil {
3950 return fmt .Errorf ("failed to mount %v to %v: %w" , podLogsPath , emptyDirLoggingVolumePath , err )
@@ -46,7 +57,11 @@ func MountPodLogs(ctx context.Context, kubePodUID, emptyDirVolName, namespace, k
4657
4758// UnmountPodLogs unmounts the pod log directory from the specified empty dir volume
4859func UnmountPodLogs (ctx context.Context , kubePodUID , emptyDirVolName string ) error {
49- emptyDirLoggingVolumePath := podEmptyDirPath (kubePodUID , emptyDirVolName )
60+ emptyDirLoggingVolumePath , err := podEmptyDirPath (kubePodUID , emptyDirVolName )
61+ if err != nil {
62+ return fmt .Errorf ("failed to get empty dir path: %w" , err )
63+ }
64+
5065 log .Infof (ctx , "Unmounting %s for linked logs" , emptyDirLoggingVolumePath )
5166 if _ , err := os .Stat (emptyDirLoggingVolumePath ); ! os .IsNotExist (err ) {
5267 if err := unix .Unmount (emptyDirLoggingVolumePath , unix .MNT_DETACH ); err != nil {
@@ -57,14 +72,30 @@ func UnmountPodLogs(ctx context.Context, kubePodUID, emptyDirVolName string) err
5772}
5873
5974func LinkContainerLogs (ctx context.Context , kubePodUID , emptyDirVolName , id string , metadata * types.ContainerMetadata ) error {
60- emptyDirLoggingVolumePath := podEmptyDirPath (kubePodUID , emptyDirVolName )
75+ emptyDirLoggingVolumePath , err := podEmptyDirPath (kubePodUID , emptyDirVolName )
76+ if err != nil {
77+ return fmt .Errorf ("failed to get empty dir path: %w" , err )
78+ }
79+
6180 // Symlink a relative path so the location is legitimate inside and outside the container.
6281 from := fmt .Sprintf ("%s/%d.log" , metadata .Name , metadata .Attempt )
63- to := filepath .Join (emptyDirLoggingVolumePath , id + ".log" )
82+
83+ to , err := securejoin .SecureJoin (emptyDirLoggingVolumePath , id + ".log" )
84+ if err != nil {
85+ return fmt .Errorf ("failed to join %v and %v: %w" , emptyDirLoggingVolumePath , id + ".log" , err )
86+ }
87+
6488 log .Infof (ctx , "Symlinking from %s to %s for linked logs" , from , to )
6589 return os .Symlink (from , to )
6690}
6791
68- func podEmptyDirPath (podUID , emptyDirVolName string ) string {
69- return filepath .Join (kubeletPodsRootDir , podUID , "volumes" , kubeletEmptyDirLogDir , emptyDirVolName )
92+ func podEmptyDirPath (podUID , emptyDirVolName string ) (string , error ) {
93+ relativePath := strings .Join ([]string {podUID , "volumes" , kubeletEmptyDirLogDir , emptyDirVolName }, "/" )
94+
95+ dirPath , err := securejoin .SecureJoin (kubeletPodsRootDir , relativePath )
96+ if err != nil {
97+ return "" , fmt .Errorf ("failed to join %v and %v: %w" , kubeletPodsRootDir , relativePath , err )
98+ }
99+
100+ return dirPath , err
70101}
0 commit comments