Skip to content

Commit

Permalink
Only allow access to the temporary file for the current user
Browse files Browse the repository at this point in the history
If the permissions for the targets of atomic writes are specified, k0s
should be conservative and not allow the contents of the temporary file
to be leaked. Do this by restricting access to the temporary file to
the current user. Otherwise, rely on the umask. On Windows, chmod isn't
implemented for file descriptors and would just toggle the read-only
attribute anyway, so it's useless to try to use it.

Signed-off-by: Tom Wieczorek <twieczorek@mirantis.com>
  • Loading branch information
twz123 committed Apr 24, 2024
1 parent 765095e commit 9ab39d1
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/pkg/file/atomic.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io/fs"
"os"
"path/filepath"
"runtime"
)

// The internal options for atomic file writes.
Expand Down Expand Up @@ -84,6 +85,26 @@ func (o *AtomicOpener) Open() (f *Atomic, err error) {
return nil, err
}

var opened bool
defer func() {
if !opened {
err = errors.Join(err, f.fd.Close(), remove(f.fd))
}
}()

// If the target's permissions are specified, k0s should be conservative and
// not allow the contents of the temporary file to be leaked. Do this by
// restricting access to the temporary file to the current user. Otherwise,
// rely on the umask. On Windows, chmod isn't implemented for file
// descriptors and would just toggle the read-only attribute anyway, so it's
// useless to try to use it.
if f.wantsChmod() && runtime.GOOS != "windows" {
if err := f.fd.Chmod(0600); err != nil {
return nil, err
}
}

opened = true
return f, nil
}

Expand Down

0 comments on commit 9ab39d1

Please sign in to comment.