FileStatus.IsModeReadOnlyCore compares group with user where its comment says other: an extra getgroups per entry, and a wrong ReadOnly for some modes

Aperta Adatta ai principianti
#138,417 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp, macos

Direzione di ricerca

Leggi src/libraries/System.Private.CoreLib/src/System/IO/FileStatus.Unix.cs nella parte relativa a IsModeReadOnlyCore, in particolare il confronto dei permessi e il relativo commento. Controlla i test esistenti per la gestione dei permessi dei file Unix, quindi aggiungi test per verificare che i permessi del gruppo e degli altri utenti coincidano e per la modalità 0446 su un file di proprietà di un altro utente. Il lavoro è completato quando il confronto segue il commento e vengono corretti sia il risultato errato di sola lettura sia la ricerca del gruppo non necessaria.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

area-System.IO untriaged
Description

FileStatus.IsModeReadOnlyCore (Unix) skips the group check when group and user agree, where its comment speaks of group and other:

https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/IO/FileStatus.Unix.cs#L112-L116

// System files often have the same permissions for group and other (umask 022).
if (isGroupReadOnly == isUserReadOnly)
{
    return isGroupReadOnly;
}

This has two effects for a file or folder another user owns:

  1. Speed. For the common 0755 or 0644 owned by root, group and other agree (both read-only) but user and group don't, so the shortcut is missed and IsMemberOfGroup runs. On macOS that calls getgroups, which asks opendirectoryd and takes about a millisecond each time. Reading FileSystemInfo.Attributes or FileSystemEntry.Attributes over a tree another user owns pays it for every entry. Walking the 1,163 folders of /usr/local/share/dotnet/packs (root-owned, as the macOS installer leaves it) and reading each one's attributes took 644 ms cold and 274 ms warm. Telling links apart there with LinkTarget took 34 ms.
  2. Correctness. Where group and user agree but other differs, a caller who is neither the owner nor in the group gets the group's answer instead of other's. For example, mode 0446 owned by another user reports ReadOnly to someone outside the group, who may write it.
Reproduction Steps

On macOS, as a user other than root:

var watch = System.Diagnostics.Stopwatch.StartNew();
int count = 0;
foreach (string folder in Directory.EnumerateDirectories("/usr/local/share/dotnet/packs", "*", SearchOption.AllDirectories))
{
    _ = new DirectoryInfo(folder).Attributes;
    count++;
}
Console.WriteLine($"{count} folders, {watch.ElapsedMilliseconds} ms");
Expected behavior

The group check is skipped when group and other agree, as the comment says. Folders owned by root with mode 0755 then need no getgroups call.

Actual behavior

Every such folder calls IsMemberOfGroup. For mode 0446 owned by another user, a caller outside the group is told the file is read-only.

Regression?

Not known.

Known Workarounds

Avoid reading Attributes where only a link check is needed, for example with LinkTarget.

Configuration

.NET 11.0.0, macOS 27 on ARM64 (Apple M2 Max). The code is the same on main.

Other information

The likely fix is if (isGroupReadOnly == isOtherReadOnly).

Lingua principale
C#
Stelle
18.5k
Fork
5.8k
Merge medio
2g 8h
PR unite (30g)
597

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dotnet/runtime

Tutte le issue di dotnet/runtime

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.