Skip to content

Tagged.Tags() sorts the shared slice under a read lock #2181

Closed

Description

@TerryHowe

What happened

model.Tagged.Tags() sorts the shared slice while holding only a read lock:

https://github.com/oras-project/oras/blob/main/cmd/oras/internal/display/metadata/model/tag.go#L37-L42

func (tag *Tagged) Tags() []string {
	tag.lock.RLock()
	defer tag.lock.RUnlock()
	slices.Sort(tag.tags)   // <-- writes to tag.tags under RLock
	return tag.tags
}

slices.Sort mutates the backing array in place, but RLock is shared — two concurrent Tags() calls both acquire it and sort the same array simultaneously. The mutex therefore does not provide the exclusion the type appears to promise.

Tags() also returns the internal slice directly, so a caller can mutate tag.tags after the lock is released.

Reproduction

Dropping this into cmd/oras/internal/display/metadata/model and running go test -race:

func TestTaggedRaceProbe(t *testing.T) {
	var tagged Tagged
	for _, s := range []string{"c", "a", "b"} {
		tagged.AddTag(s)
	}
	var wg sync.WaitGroup
	for i := 0; i < 8; i++ {
		wg.Add(1)
		go func() { defer wg.Done(); _ = tagged.Tags() }()
	}
	wg.Wait()
}
WARNING: DATA RACE
Read at 0x00c000180ae0 by goroutine 12:
  slices.insertionSortOrdered[go.shape.string]()
  slices.Sort[go.shape.[]string,go.shape.string]()
  ...model.(*Tagged).Tags()
      cmd/oras/internal/display/metadata/model/tag.go:41

Previous write at 0x00c000180ae0 by goroutine 10:
  slices.insertionSortOrdered[go.shape.string]()
  ...model.(*Tagged).Tags()
      cmd/oras/internal/display/metadata/model/tag.go:41

Impact

Latent, not currently reachable. The only two call sites each invoke Tags() once, from Render():

  • cmd/oras/internal/display/metadata/json/push.go:62
  • cmd/oras/internal/display/metadata/template/push.go:64

AddTag vs. Tags is correctly excluded (write lock vs. read lock), so no user-visible bug exists today. The problem is that Tagged is written as a concurrency-safe type — that is the entire reason it carries a sync.RWMutex — and it does not hold up if a second Tags() caller is ever added. That would be an easy and quiet regression to introduce.

Suggested fix

Either:

  1. Sort in AddTag under the write lock, leaving Tags() a pure read; or
  2. Have Tags() take the write lock and return slices.Clone(tag.tags).

(2) also fixes the returned-internal-slice aliasing. A concurrent Tags() subtest under -race would lock the behavior in.

Context

Noticed while reviewing #2176, which adds the first test coverage for Tagged and pins the sorting behavior. That PR is test-only and correct as-is; this is a separate production-code follow-up.

Activity

  1. added
    bug
    on Sep 21, 2026
  2. TanvirTian commented on Sep 21, 2026

    @TanvirTian
    Contributor

    I want to work on this issue. Can you assign it to me?

  3. TerryHowe commented on Sep 21, 2026

    @TerryHowe
    MemberAuthor

    We don't assign issues, but your post should work, thanks!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions