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
Milestone

Comments

@masiulaniec
Copy link

@masiulaniec masiulaniec commented Dec 29, 2019

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
@opennota
Copy link

@opennota opennota commented Dec 31, 2019

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

@masiulaniec
Copy link
Author

@masiulaniec masiulaniec commented Jan 1, 2020

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

@toothrot toothrot added this to the Backlog milestone Jan 7, 2020
@toothrot
Copy link
Contributor

@toothrot toothrot commented Jan 7, 2020

@masiulaniec What version of Go are you using?

/cc @dsnet

@masiulaniec
Copy link
Author

@masiulaniec masiulaniec commented Jan 9, 2020

go1.13.4 darwin/amd64

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

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.