-
Notifications
You must be signed in to change notification settings - Fork 251
/
perms_posix.go
62 lines (51 loc) · 1.64 KB
/
perms_posix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// +build linux darwin
package perms
import (
"fmt"
"io/ioutil"
"os"
"syscall"
)
// Make a private file that is only readable by the current user.
func WritePrivateFile(filename string, content []byte) error {
if filename == "" {
// regression check for
// https://bugzilla.mozilla.org/show_bug.cgi?id=1594353
panic("empty filename passed to WritePrivateFile")
}
// remove any existing file if it already exists, ignoring errors
_ = os.Remove(filename)
// 0600 permissions actually mean what they say on POSIX (unlike Windows)
err := ioutil.WriteFile(filename, content, 0600)
if err != nil {
return fmt.Errorf("Could not write to %s: %w", filename, err)
}
return verifyPrivateToOwner(filename)
}
// Read a file, first verifying that it can only be read by the current user.
func ReadPrivateFile(filename string) ([]byte, error) {
err := verifyPrivateToOwner(filename)
if err != nil {
return []byte{}, err
}
return ioutil.ReadFile(filename)
}
// verifyPrivateToOwner verifies that the given file can only be read by the
// file's owner, returning an error if this is not the case, or cannot be
// determined. Returns an error satisfying os.IsNotExist when the file does
// not exist.
func verifyPrivateToOwner(filename string) error {
stat, err := os.Stat(filename)
if err != nil {
return err
}
uid := int(stat.Sys().(*syscall.Stat_t).Uid)
if uid != os.Getuid() {
return fmt.Errorf("%s has incorrect owner id %d", filename, uid)
}
// (note: we don't check gid since the group has no permission to the file)
if stat.Mode() != os.FileMode(0600) {
return fmt.Errorf("%s has mode %#o, not 0600", filename, stat.Mode())
}
return nil
}