Skip to content

Commit

Permalink
deb/extract: Extract with umask set to 0
Browse files Browse the repository at this point in the history
Permission bits passed to open(2) are AND-ed with current process umask.
Set umask to 0 at the beginning of extractData() and restore it at the
end, to extract files with desired permissions no matter what's the
current process umask.

How does dpkg do it? It invokes tar binary to extract data.tar.gz, and
tar sets current process umask to 0 if it's running as root. (See
--no-same-permissions in tar(1)).

Also fix expected permissions of /tmp from 01775 to 01777.
  • Loading branch information
woky committed Nov 28, 2022
1 parent 829d21f commit 69ef992
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions internal/deb/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"
"sort"
"strings"
"syscall"

"github.com/blakesmith/ar"
"github.com/klauspost/compress/zstd"
Expand Down Expand Up @@ -104,6 +105,11 @@ func Extract(pkgReader io.Reader, options *ExtractOptions) (err error) {

func extractData(dataReader io.Reader, options *ExtractOptions) error {

oldUmask := syscall.Umask(0)
defer func() {
syscall.Umask(oldUmask)
}()

shouldExtract := func(pkgPath string) (globPath string, ok bool) {
if pkgPath == "" {
return "", false
Expand Down
4 changes: 2 additions & 2 deletions internal/deb/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var extractTests = []extractTest{{
},
},
result: map[string]string{
"/tmp/": "dir 01775",
"/tmp/": "dir 01777",
"/usr/": "dir 0755",
"/usr/bin/": "dir 0755",
"/usr/bin/hello": "file 0775 eaf29575",
Expand Down Expand Up @@ -263,7 +263,7 @@ var extractTests = []extractTest{{
"/etc/": "dir 0755",
"/usr/": "dir 0755",
"/usr/bin/": "dir 0755",
"/tmp/": "dir 01775",
"/tmp/": "dir 01777",
},
}, {
summary: "Optional entries mixed in cannot be missing",
Expand Down

0 comments on commit 69ef992

Please sign in to comment.