Skip to content
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

Avoid Path.GetTempFileName() due to CWE 377. #537

Closed
tdhintz opened this issue Nov 10, 2020 · 7 comments · Fixed by #539
Closed

Avoid Path.GetTempFileName() due to CWE 377. #537

tdhintz opened this issue Nov 10, 2020 · 7 comments · Fixed by #539

Comments

@tdhintz
Copy link

tdhintz commented Nov 10, 2020

Steps to reproduce

  1. Static code security scan. Example:
		private static string GetTempFileName(string original, bool makeTempFile)
		{
			string result = null;

			if (original == null)
			{
				result = Path.GetTempFileName();
			}

Expected behavior

Use sufficiently random names for temporary files to prevent attacks which can predict the name of the file. For example, a Guid or crypto generated random number.

Actual behavior

Security scan failure.

Version of SharpZipLib

1.3.0

Obtained from (only keep the relevant lines)

  • Package installed using NuGet
@piksel
Copy link
Member

piksel commented Nov 10, 2020

The referenced code is

private static string GetTempFileName(string original, bool makeTempFile)

The suggested solution does not seem to corelate with the CWE. What static code analysis tool was used to produce these?
Instead I would suggest either using Path.GetRandomFileName() which doesn't create the file until we actually create the handle. The optimal solution would probably be to just not use any temporary files at all. It shouldn't be necessary really.

@tdhintz
Copy link
Author

tdhintz commented Nov 10, 2020

Veracode. Yes, avoiding temporary files altogether is a solution.

@Numpsy
Copy link
Contributor

Numpsy commented Nov 10, 2020

The optimal solution would probably be to just not use any temporary files at all. It shouldn't be necessary really.

If that's the 'safe' update mode code, then using temp/internediate files to do the update is the point? (not sure if just doing it in memory would work, given the attempts to roll back changes on error and such?)

@piksel
Copy link
Member

piksel commented Nov 10, 2020

@Numpsy I don't see how using a MemoryStream and then replacing the file when done would be any less safe... All the operations are just done on a Stream anyway.

@piksel
Copy link
Member

piksel commented Nov 10, 2020

I went with the easy fix for now anyway, making sure that the file updates work with a MemoryStream might need some work.

@Numpsy
Copy link
Contributor

Numpsy commented Nov 11, 2020

I was just looking at all the file copying logic in

public override Stream ConvertTemporaryToFinal()
and thinking of the case where it tries to revert changes to the original file in case of error - if it was done without any temporary files at all by just opening the file and streaming the completed/updated data into it, it could be more open to corruption if it fell over hald way though the write?
Other than that, it could potentially require a lot more memory when doing small updates to massive files?

@tdhintz
Copy link
Author

tdhintz commented Nov 22, 2020

Code scan now passes for 2 of the 3 risk areas. This one is still unresolved at line 827 in TarArchive.cs.

			if (asciiTranslate && !entry.IsDirectory)
			{
				if (!IsBinary(entryFilename))
				{
					tempFileName = Path.GetTempFileName();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants