| 1 |
/* |
| 2 |
* privileges.go - Functions for managing users and privileges. |
| 3 |
* |
| 4 |
* Copyright 2017 Google Inc. |
| 5 |
* Author: Joe Richey ([email protected]) |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 8 |
* use this file except in compliance with the License. You may obtain a copy of |
| 9 |
* the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, software |
| 14 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 |
* License for the specific language governing permissions and limitations under |
| 17 |
* the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
// Package security manages: |
| 21 |
// - Cache clearing (cache.go) |
| 22 |
// - Privilege manipulation (privileges.go) |
| 23 |
package security |
| 24 |
|
| 25 |
// Use the libc versions of setreuid, setregid, and setgroups instead of the |
| 26 |
// "sys/unix" versions. The "sys/unix" versions use the raw syscalls which |
| 27 |
// operate on the calling thread only, whereas the libc versions operate on the |
| 28 |
// whole process. And we need to operate on the whole process, firstly for |
| 29 |
// pam_fscrypt to prevent the privileges of Go worker threads from diverging |
| 30 |
// from the PAM stack's "main" thread, violating libc's assumption and causing |
| 31 |
// an abort() later in the PAM stack; and secondly because Go code may migrate |
| 32 |
// between OS-level threads while it's running. |
| 33 |
// |
| 34 |
// See also: https://github.com/golang/go/issues/1435 |
| 35 |
|
| 36 |
/* |
| 37 |
#define _GNU_SOURCE // for getresuid and setresuid |
| 38 |
#include <sys/types.h> |
| 39 |
#include <unistd.h> // getting and setting uids and gids |
| 40 |
#include <grp.h> // setgroups |
| 41 |
*/ |
| 42 |
import "C" |
| 43 |
|
| 44 |
import ( |
| 45 |
"log" |
| 46 |
"os/user" |
| 47 |
"syscall" |
| 48 |
|
| 49 |
"github.com/pkg/errors" |
| 50 |
|
| 51 |
"github.com/google/fscrypt/util" |
| 52 |
) |
| 53 |
|
| 54 |
// Privileges encapsulate the effective uid/gid and groups of a process. |
| 55 |
type Privileges struct { |
| 56 |
euid C.uid_t |
| 57 |
egid C.gid_t |
| 58 |
groups []C.gid_t |
| 59 |
} |
| 60 |
|
| 61 |
// ProcessPrivileges returns the process's current effective privileges. |
| 62 |
func ProcessPrivileges() (*Privileges, error) { |
| 63 |
ruid := C.getuid() |
| 64 |
euid := C.geteuid() |
| 65 |
rgid := C.getgid() |
| 66 |
egid := C.getegid() |
| 67 |
|
| 68 |
var groups []C.gid_t |
| 69 |
n, err := C.getgroups(0, nil) |
| 70 |
if n < 0 { |
| 71 |
return nil, err |
| 72 |
} |
| 73 |
// If n == 0, the user isn't in any groups, so groups == nil is fine. |
| 74 |
if n > 0 { |
| 75 |
groups = make([]C.gid_t, n) |
| 76 |
n, err = C.getgroups(n, &groups[0]) |
| 77 |
if n < 0 { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
groups = groups[:n] |
| 81 |
} |
| 82 |
log.Printf("Current privs (real, effective): uid=(%d,%d) gid=(%d,%d) groups=%v", |
| 83 |
ruid, euid, rgid, egid, groups) |
| 84 |
return &Privileges{euid, egid, groups}, nil |
| 85 |
} |
| 86 |
|
| 87 |
// UserPrivileges returns the default privileges for the specified user. |
| 88 |
func UserPrivileges(user *user.User) (*Privileges, error) { |
| 89 |
privs := &Privileges{ |
| 90 |
euid: C.uid_t(util.AtoiOrPanic(user.Uid)), |
| 91 |
egid: C.gid_t(util.AtoiOrPanic(user.Gid)), |
| 92 |
} |
| 93 |
userGroups, err := user.GroupIds() |
| 94 |
if err != nil { |
| 95 |
return nil, util.SystemError(err.Error()) |
| 96 |
} |
| 97 |
privs.groups = make([]C.gid_t, len(userGroups)) |
| 98 |
for i, group := range userGroups { |
| 99 |
privs.groups[i] = C.gid_t(util.AtoiOrPanic(group)) |
| 100 |
} |
| 101 |
return privs, nil |
| 102 |
} |
| 103 |
|
| 104 |
// SetProcessPrivileges sets the privileges of the current process to have those |
| 105 |
// specified by privs. The original privileges can be obtained by first saving |
| 106 |
// the output of ProcessPrivileges, calling SetProcessPrivileges with the |
| 107 |
// desired privs, then calling SetProcessPrivileges with the saved privs. |
| 108 |
func SetProcessPrivileges(privs *Privileges) error { |
| 109 |
log.Printf("Setting euid=%d egid=%d groups=%v", privs.euid, privs.egid, privs.groups) |
| 110 |
|
| 111 |
// If setting privs as root, we need to set the euid to 0 first, so that |
| 112 |
// we will have the necessary permissions to make the other changes to |
| 113 |
// the groups/egid/euid, regardless of our original euid. |
| 114 |
C.seteuid(0) |
| 115 |
|
| 116 |
// Separately handle the case where the user is in no groups. |
| 117 |
numGroups := C.size_t(len(privs.groups)) |
| 118 |
groupsPtr := (*C.gid_t)(nil) |
| 119 |
if numGroups > 0 { |
| 120 |
groupsPtr = &privs.groups[0] |
| 121 |
} |
| 122 |
|
| 123 |
if res, err := C.setgroups(numGroups, groupsPtr); res < 0 { |
| 124 |
return errors.Wrapf(err.(syscall.Errno), "setting groups") |
| 125 |
} |
| 126 |
if res, err := C.setegid(privs.egid); res < 0 { |
| 127 |
return errors.Wrapf(err.(syscall.Errno), "setting egid") |
| 128 |
} |
| 129 |
if res, err := C.seteuid(privs.euid); res < 0 { |
| 130 |
return errors.Wrapf(err.(syscall.Errno), "setting euid") |
| 131 |
} |
| 132 |
ProcessPrivileges() |
| 133 |
return nil |
| 134 |
} |
| 135 |
|
| 136 |
// SetUids sets the process's real, effective, and saved UIDs. |
| 137 |
func SetUids(ruid, euid, suid int) error { |
| 138 |
log.Printf("Setting ruid=%d euid=%d suid=%d", ruid, euid, suid) |
| 139 |
// We elevate all the privs before setting them. This prevents issues |
| 140 |
// with (ruid=1000,euid=1000,suid=0), where just a single call to |
| 141 |
// setresuid might fail with permission denied. |
| 142 |
if res, err := C.setresuid(0, 0, 0); res < 0 { |
| 143 |
return errors.Wrapf(err.(syscall.Errno), "setting uids") |
| 144 |
} |
| 145 |
if res, err := C.setresuid(C.uid_t(ruid), C.uid_t(euid), C.uid_t(suid)); res < 0 { |
| 146 |
return errors.Wrapf(err.(syscall.Errno), "setting uids") |
| 147 |
} |
| 148 |
return nil |
| 149 |
} |
| 150 |
|
| 151 |
// GetUids gets the process's real, effective, and saved UIDs. |
| 152 |
func GetUids() (int, int, int) { |
| 153 |
var ruid, euid, suid C.uid_t |
| 154 |
C.getresuid(&ruid, &euid, &suid) |
| 155 |
return int(ruid), int(euid), int(suid) |
| 156 |
} |