forked from tianon/gosu
/
setup-user.go
114 lines (96 loc) · 2.55 KB
/
setup-user.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"os"
"syscall"
"github.com/LTD-Beget/libcontainer/seccomp"
"github.com/LTD-Beget/libcontainer/system"
"github.com/LTD-Beget/libcontainer/user"
)
func initSeccomp() {
context := seccomp.New()
// limit minimum uid
args_uid := make([][]seccomp.Arg, 1)
args_uid[0] = make([]seccomp.Arg, 1)
args_uid[0][0] = seccomp.Arg{
Index: 0,
Op: seccomp.LessThan,
Value: MIN_UID,
}
setuid := seccomp.Syscall{
Value: syscall.SYS_SETUID,
Action: seccomp.Errno,
Args: args_uid,
}
// limit minimum gid
args_gid := make([][]seccomp.Arg, 1)
args_gid[0] = make([]seccomp.Arg, 1)
args_gid[0][0] = seccomp.Arg{
Index: 0,
Op: seccomp.LessThan,
Value: MIN_GID,
}
setgid := seccomp.Syscall{
Value: syscall.SYS_SETGID,
Action: seccomp.Errno,
Args: args_gid,
}
// apply seccomp
context.Add(&setuid)
context.Add(&setgid)
context.Load()
}
// this function comes from libcontainer/init_linux.go
// we don't use that directly because we don't want the whole namespaces package imported here
// SetupUser changes the groups, gid, and uid for the user inside the container
func SetupUser(u string) error {
// Set up defaults.
defaultExecUser := user.ExecUser{
Uid: syscall.Getuid(),
Gid: syscall.Getgid(),
Home: "/",
}
passwdPath, err := user.GetPasswdPath()
if err != nil {
return err
}
groupPath, err := user.GetGroupPath()
if err != nil {
return err
}
execUser, err := user.GetExecUserPath(u, &defaultExecUser, passwdPath, groupPath)
if err != nil {
return fmt.Errorf("get supplementary groups %s", err)
}
// if not root - check uid/gid by hand if seccomp is not working
if syscall.Geteuid() > 0 && (execUser.Uid <= MIN_UID || execUser.Gid <= MIN_GID) {
return fmt.Errorf("Invalid UID or GID")
}
// set supplementary groups
if err := syscall.Setgroups(execUser.Sgids); err != nil {
return fmt.Errorf("setgroups %s", err)
}
// set gid
if err := system.Setgid(execUser.Gid); err != nil {
return fmt.Errorf("setgid %s", err)
}
// check if setgid is successfull
if syscall.Getgid() != execUser.Gid {
return fmt.Errorf("setgid failed")
}
// set uid
if err := system.Setuid(execUser.Uid); err != nil {
return fmt.Errorf("setuid %s", err)
}
// check if setuid is successful
if syscall.Getuid() != execUser.Uid {
return fmt.Errorf("setuid failed")
}
// if we didn't get HOME already, set it based on the user's HOME
if envHome := os.Getenv("HOME"); envHome == "" {
if err := os.Setenv("HOME", execUser.Home); err != nil {
return fmt.Errorf("set HOME %s", err)
}
}
return nil
}