Skip to content

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

Closed

Description

@tdhintz

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

Activity

  1. piksel commented on Nov 10, 2020

    @piksel
    Member

    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.

  2. tdhintz commented on Nov 10, 2020

    @tdhintz
    Author

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

  3. Numpsy commented on Nov 10, 2020

    @Numpsy
    Contributor

    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?)

  4. piksel commented on Nov 10, 2020

    @piksel
    Member

    @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.

  5. piksel commented on Nov 10, 2020

    @piksel
    Member

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

  6. Numpsy commented on Nov 11, 2020

    @Numpsy
    Contributor

    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?

  7. tdhintz commented on Nov 22, 2020

    @tdhintz
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions