Skip to content

Conversation

@rdimitrov
Copy link
Contributor

The following PR updates the permissions used for cache directories

Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
@rdimitrov rdimitrov merged commit f400bf4 into master Jan 21, 2026
23 checks passed
@rdimitrov rdimitrov deleted the fix/directory-permissions branch January 21, 2026 08:32
Comment on lines -87 to +92
if err := os.MkdirAll(path, os.ModePerm); err != nil {
// Use 0700 for cache directories: only the owner can read, write, and
// access the directory. This prevents other users on shared systems from
// reading or writing to the TUF cache, which could be a security risk.
// If different permissions are needed, pre-create the directories with
// the desired permissions before calling this function.
if err := os.MkdirAll(path, 0700); err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this patch when updating our dependencies; not sure how important, but note that os.MkDirAll only creates missing directories, and ignores existing ones, and won't fix ownership (if anything else);

https://go.dev/play/p/6qrYT_RVhaL

package main

import (
	"os"
	"path/filepath"
	"syscall"
	"testing"
)

func TestPerms(t *testing.T) {
	tmpDir := t.TempDir()

	cacheDir := filepath.Join(tmpDir, "foo", "cache")
	err := os.MkdirAll(cacheDir, 0o755)
	if err != nil {
		t.Fatal(err)
	}

	// Chown requires root
	if os.Getuid() == 0 {
		err = os.Chown(cacheDir, 123, 456)
		if err != nil {
			t.Fatal(err)
		}
	}
	printMode(t, cacheDir)

	err = os.MkdirAll(cacheDir, 0o700)
	if err != nil {
		t.Fatal(err)
	}
	printMode(t, cacheDir)

	err = os.Chmod(cacheDir, 0o700)
	if err != nil {
		t.Fatal(err)
	}
	printMode(t, cacheDir)
}

func printMode(t *testing.T, path string) {
	t.Helper()
	fi, err := os.Stat(path)
	if err != nil {
		t.Fatal(err)
	}
	var uid, gid uint32
	if stat, ok := fi.Sys().(*syscall.Stat_t); ok && stat != nil {
		uid = stat.Uid
		gid = stat.Gid
	}

	t.Logf("fileMode: %s, (uid=%d, gid=%d)", fi.Mode().String(), uid, gid)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's stated in the comment (this permission mode only applies if new directories are created):

  // If different permissions are needed, pre-create the directories with
  // the desired permissions before calling this function.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 Doh! I read it wrong, and thought that was describing what the code tried to achieve.

I saw the change and thought "right, but this won't fix if it's there!" 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants