-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Bound tar entry preallocation to available archive data to prevent disk-exhaustion #131794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -739,7 +739,7 @@ private FileStreamOptions CreateFileStreamOptions(bool isAsync) | |
| // (real) size, while the archive only contains the much smaller packed data. | ||
| // Preallocating to the expanded size would reserve disk space that bears no | ||
| // relation to the archive contents and can fail surprisingly on small volumes. | ||
| PreallocationSize = _header._gnuSparseDataStream is null ? Length : 0, | ||
| PreallocationSize = _header._gnuSparseDataStream is null ? GetPreallocationSize() : 0, | ||
| Options = isAsync ? FileOptions.Asynchronous : FileOptions.None | ||
| }; | ||
|
|
||
|
|
@@ -757,5 +757,39 @@ private FileStreamOptions CreateFileStreamOptions(bool isAsync) | |
|
|
||
| return fileStreamOptions; | ||
| } | ||
|
|
||
| // Determines the number of bytes to preallocate for the destination file. | ||
| // The entry's declared Length comes directly from the (potentially attacker-controlled) tar | ||
| // header's size field, so when the data section is backed by a SubReadStream over the archive | ||
| // stream, cap the preallocation to the number of bytes actually remaining in the archive stream. | ||
| // This prevents a crafted entry that declares a huge size but provides little or no actual data | ||
| // from causing an excessive up-front file preallocation (which can exhaust disk space or hang). | ||
| // Since extraction writes from the stream's current position, the remaining declared bytes | ||
| // (rather than the full Length) are compared against the remaining available archive data. | ||
| // When the archive stream isn't seekable, its true remaining length can't be determined without | ||
| // consuming it, so preallocation is skipped entirely rather than trusting the declared size. | ||
| // Non-SubReadStream data sources (e.g. a user-provided DataStream) are preallocated using the | ||
| // full remaining reported Length, since there is no archive stream to validate against. | ||
| private long GetPreallocationSize() | ||
| { | ||
| long length = Length; | ||
|
|
||
| if (length > 0 && _header._dataStream is SubReadStream subReadStream) | ||
| { | ||
| long remainingDeclared = length - subReadStream.Position; | ||
|
|
||
| long? availableLength = subReadStream.AvailableLengthInSuperStream; | ||
| if (!availableLength.HasValue) | ||
| { | ||
| // Unseekable archive stream: the declared size cannot be validated against the | ||
| // actual remaining data, so don't preallocate based on it at all. | ||
| return 0; | ||
|
Comment on lines
+784
to
+786
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might regress perf because it disables preallocation in cases where it was allowed before. The application already authorized writing |
||
| } | ||
|
|
||
| return Math.Min(remainingDeclared, availableLength.Value); | ||
| } | ||
|
|
||
| return length; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can put the _gnuSparseDataStream check also into GetPreallocationSize so that all logic is in one place