@@ -24,6 +24,8 @@ func InstallFile(filename string, r io.Reader, perm os.FileMode) error {
2424
2525// InstallFileWithLimitedSize streams content to local file with limited size and specified permissions.
2626// It ensures that the target directory exists and handles the file writing atomically.
27+ // The temp file is created in the same directory as the destination to preserve the
28+ // correct SELinux label (avoiding the user_tmp_t label from /tmp).
2729//
2830// NOTE: we assume the filename is trusted and cleaned without path traversal characters.
2931func InstallFileWithLimitedSize (filename string , r io.Reader , perm os.FileMode , maxBytes int64 ) error {
@@ -34,7 +36,7 @@ func InstallFileWithLimitedSize(filename string, r io.Reader, perm os.FileMode,
3436 return err
3537 }
3638
37- pf , err := renameio .NewPendingFile (filename , renameio .WithPermissions (perm ))
39+ pf , err := renameio .NewPendingFile (filename , renameio .WithPermissions (perm ), renameio . WithTempDir ( filepath . Dir ( filename )) )
3840 if err != nil {
3941 return err
4042 }
@@ -57,12 +59,14 @@ func InstallFileWithLimitedSize(filename string, r io.Reader, perm os.FileMode,
5759
5860// WriteFile writes the provided content to a local file with specified permissions.
5961// It ensures that the target directory exists and handles the file writing atomically.
62+ // The temp file is created in the same directory as the destination to preserve the
63+ // correct SELinux label (avoiding the user_tmp_t label from /tmp).
6064//
6165// NOTE: we assume the filename is trusted and cleaned without path traversal characters.
6266func WriteFile (filename string , content []byte , perm os.FileMode ) error {
6367 if err := os .MkdirAll (filepath .Dir (filename ), 0750 ); err != nil {
6468 return err
6569 }
6670
67- return renameio .WriteFile (filename , content , perm )
71+ return renameio .WriteFile (filename , content , perm , renameio . WithTempDir ( filepath . Dir ( filename )) )
6872}
0 commit comments