-
Notifications
You must be signed in to change notification settings - Fork 10
/
privileges.c
129 lines (111 loc) · 3.25 KB
/
privileges.c
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Handle privilage management
*
* Copyright (C) 2014 - Brian Caswell <bmc@lungetech.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#define _GNU_SOURCE
#include <grp.h>
#include <pwd.h>
#include <unistd.h>
#include "privileges.h"
#include "utils.h"
extern int insecure_flag;
void setup_chroot(const char *directory) {
VERIFY(chdir, directory);
if (insecure_flag == 1) {
return;
}
VERIFY(chroot, directory);
VERIFY(chdir, "/");
}
uid_t get_unused_uid(void) {
struct passwd *pd;
uid_t uid;
unsigned int count = 0;
for (count = 0; count <= 1000; count++) {
get_random((void *) &uid, sizeof(uid));
pd = getpwuid(uid);
if (pd == NULL)
return uid;
}
err(-1, "unable to get an unused uid");
}
gid_t get_unused_gid(void) {
struct group *gd;
gid_t gid;
unsigned int count = 0;
for (count = 0; count <= 1000; count++) {
get_random((void *) &gid, sizeof(gid));
gd = getgrgid(gid);
if (gd == NULL)
return gid;
}
err(-1, "unable to get an unused gid");
}
void drop_privileges(const uid_t uid, const gid_t gid) {
VERIFY(setsid);
if (insecure_flag == 1) {
return;
}
#ifndef RANDOM_UID
{
struct passwd *pd;
pd = getpwuid(uid);
VERIFY_EXP(pd != NULL);
setup_chroot(pd->pw_dir);
}
#endif
#ifdef HAVE_SETRESGID
VERIFY(setresgid, gid, gid, gid);
#else
VERIFY(setregid, gid, gid);
#endif
VERIFY(setgroups, 1, &gid);
VERIFY(setgid, gid);
VERIFY(setegid, gid);
#ifdef HAVE_SETRESGID
VERIFY(setresuid, uid, uid, uid);
#else
VERIFY(setreuid, UID_MAX, uid);
#endif
VERIFY(setuid, uid);
VERIFY(seteuid, uid);
#ifdef HAVE_SETRESGID
{
uid_t real;
uid_t effective;
uid_t saved;
VERIFY(getresuid, &real, &effective, &saved);
VERIFY_EXP(uid == real);
VERIFY_EXP(uid == effective);
VERIFY_EXP(uid == saved);
VERIFY(getresgid, &real, &effective, &saved);
VERIFY_EXP(gid == real);
VERIFY_EXP(gid == effective);
VERIFY_EXP(gid == saved);
}
#else
VERIFY_EXP(geteuid() == uid);
VERIFY_EXP(getuid() == uid);
VERIFY_EXP(getegid() == gid);
VERIFY_EXP(getgid() == gid);
#endif
}