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

archive/zip: FileHeader.SetMode: character files encoded as regular files #36301

Open
masiulaniec opened this issue Dec 29, 2019 · 4 comments
Open
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@masiulaniec
Copy link

package main

import (
        "archive/zip"
        "log"
        "os"
)

const charDeviceFile = "/dev/null"

func main() {
        fi, err := os.Stat(charDeviceFile)
        if err != nil {
                panic(err)
        }
        fh, err := zip.FileInfoHeader(fi)
        if err != nil {
                panic(err)
        }
        if isChar(fi) != isChar(fh.FileInfo()) {
                log.Printf("file mode inconsistent after zip.FileInfoHeader: got %q, want %q", fh.FileInfo().Mode(), fi.Mode())
        }
}

func isChar(fi os.FileInfo) bool { return fi.Mode() & os.ModeCharDevice != 0 }

This reproducer program fails unexpectedly with the error:

file mode inconsistent after zip.FileInfoHeader: got "-rw-rw-rw-", want "Dcrw-rw-rw-"

The data loss is due to a logically impossible bit match in fileModeToUnixMode. The fix:

--- src/archive/zip/struct.go   2019-12-29 02:26:54.000000000 +0100
+++ src/archive/zip/struct.go   2019-12-29 02:27:05.000000000 +0100
@@ -337,12 +337,10 @@
                m = s_IFIFO
        case os.ModeSocket:
                m = s_IFSOCK
+       case os.ModeDevice | os.ModeCharDevice:
+               m = s_IFCHR
        case os.ModeDevice:
-               if mode&os.ModeCharDevice != 0 {
-                       m = s_IFCHR
-               } else {
-                       m = s_IFBLK
-               }
+               m = s_IFBLK
        }
        if mode&os.ModeSetuid != 0 {
                m |= s_ISUID
@masiulaniec masiulaniec changed the title archive/zip: FileHeader.SetMode: special files encoded as regular files archive/zip: FileHeader.SetMode: character files encoded as regular files Dec 29, 2019
@ghost
Copy link

ghost commented Dec 31, 2019

FWIW, the zip utility doesn't allow to add these to an archive at all.

@masiulaniec
Copy link
Author

Since the zip package is encoding block devices today, I would say encoding of character devices should be fixed to match.

@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 7, 2020
@toothrot toothrot added this to the Backlog milestone Jan 7, 2020
@toothrot
Copy link
Contributor

toothrot commented Jan 7, 2020

@masiulaniec What version of Go are you using?

/cc @dsnet

@masiulaniec
Copy link
Author

go1.13.4 darwin/amd64

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

2 participants