-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradm_cap.c
194 lines (166 loc) · 4.65 KB
/
gradm_cap.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright (C) 2002-2014 Bradley Spengler, Open Source Security, Inc.
* http://www.grsecurity.net [email protected]
*
* This file is part of gradm.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "gradm.h"
struct capability_set capability_list[] = {
{"CAP_CHOWN", 0},
{"CAP_DAC_OVERRIDE", 1},
{"CAP_DAC_READ_SEARCH", 2},
{"CAP_FOWNER", 3},
{"CAP_FSETID", 4},
{"CAP_KILL", 5},
{"CAP_SETGID", 6},
{"CAP_SETUID", 7},
{"CAP_SETPCAP", 8},
{"CAP_LINUX_IMMUTABLE", 9},
{"CAP_NET_BIND_SERVICE", 10},
{"CAP_NET_BROADCAST", 11},
{"CAP_NET_ADMIN", 12},
{"CAP_NET_RAW", 13},
{"CAP_IPC_LOCK", 14},
{"CAP_IPC_OWNER", 15},
{"CAP_SYS_MODULE", 16},
{"CAP_SYS_RAWIO", 17},
{"CAP_SYS_CHROOT", 18},
{"CAP_SYS_PTRACE", 19},
{"CAP_SYS_PACCT", 20},
{"CAP_SYS_ADMIN", 21},
{"CAP_SYS_BOOT", 22},
{"CAP_SYS_NICE", 23},
{"CAP_SYS_RESOURCE", 24},
{"CAP_SYS_TIME", 25},
{"CAP_SYS_TTY_CONFIG", 26},
{"CAP_MKNOD", 27},
{"CAP_LEASE", 28},
{"CAP_AUDIT_WRITE", 29},
{"CAP_AUDIT_CONTROL", 30},
{"CAP_SETFCAP", 31},
{"CAP_MAC_OVERRIDE", 32},
{"CAP_MAC_ADMIN", 33},
{"CAP_SYSLOG", 34},
{"CAP_WAKE_ALARM", 35},
{"CAP_BLOCK_SUSPEND", 36},
{"CAP_AUDIT_READ", 37},
{"CAP_ALL", ~0}
};
gr_cap_t cap_combine(gr_cap_t a, gr_cap_t b)
{
int i;
gr_cap_t ret;
for (i = 0; i < 2; i++)
ret.cap[i] = a.cap[i] | b.cap[i];
return ret;
}
gr_cap_t cap_drop(gr_cap_t a, gr_cap_t b)
{
int i;
gr_cap_t ret;
for (i = 0; i < 2; i++)
ret.cap[i] = a.cap[i] &~ b.cap[i];
return ret;
}
gr_cap_t cap_intersect(gr_cap_t a, gr_cap_t b)
{
int i;
gr_cap_t ret;
for (i = 0; i < 2; i++)
ret.cap[i] = a.cap[i] & b.cap[i];
return ret;
}
gr_cap_t cap_invert(gr_cap_t a)
{
int i;
gr_cap_t ret;
for (i = 0; i < 2; i++)
ret.cap[i] = ~a.cap[i];
return ret;
}
int cap_isclear(gr_cap_t a)
{
if (a.cap[0] || a.cap[1])
return 0;
return 1;
}
int cap_same(gr_cap_t a, gr_cap_t b)
{
if (a.cap[0] == b.cap[0] && a.cap[1] == b.cap[1])
return 1;
return 0;
}
gr_cap_t
cap_conv(const char *cap)
{
gr_cap_t retcap = {{ 0, 0 }};
int i;
for (i = 0;
i < sizeof (capability_list) / sizeof (struct capability_set); i++)
if (!strcmp(cap, capability_list[i].cap_name)) {
if (i == (sizeof (capability_list) /
sizeof (struct capability_set) - 1)) {
retcap.cap[0] = ~0;
retcap.cap[1] = ~0; /* CAP_ALL */
} else
cap_raise(retcap, capability_list[i].cap_val);
return retcap;
}
fprintf(stderr, "Invalid capability name \"%s\" on line %lu of %s.\n"
"The RBAC system will not load until this"
" error is fixed.\n", cap, lineno, current_acl_file);
exit(EXIT_FAILURE);
return retcap;
}
void
add_cap_acl(struct proc_acl *subject, const char *cap, const char *audit)
{
gr_cap_t kcap = cap_conv(cap + 1);
if (!subject) {
fprintf(stderr, "Error on line %lu of %s. Attempt to "
"add a capability without a subject declaration.\n"
"The RBAC system will not load until this "
"error is fixed.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
if (*cap == '+') {
subject->cap_drop = cap_drop(subject->cap_drop, kcap);
subject->cap_mask = cap_combine(subject->cap_mask, kcap);
if (audit && !strcmp(audit, "audit"))
subject->cap_invert_audit = cap_combine(subject->cap_invert_audit, kcap);
else if (audit) {
fprintf(stderr, "Error on line %lu of %s. \"suppress\" can only be applied to denied capabilities. The RBAC system will not load until this error is fixed.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
} else {
subject->cap_drop = cap_combine(subject->cap_drop, kcap);
subject->cap_mask = cap_combine(subject->cap_mask, kcap);
if (audit && !strcmp(audit, "suppress"))
subject->cap_invert_audit = cap_combine(subject->cap_invert_audit, kcap);
else if (audit) {
fprintf(stderr, "Error on line %lu of %s. \"audit\" can only be applied to permitted capabilities. The RBAC system will not load until this error is fixed.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
}
return;
}
void
modify_caps(struct proc_acl *proc, int cap)
{
cap_lower(proc->cap_drop, cap);
cap_raise(proc->cap_mask, cap);
return;
}