-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradm_misc.c
312 lines (265 loc) · 7.01 KB
/
gradm_misc.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* 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"
extern FILE *grlearn_configin;
extern int grlearn_configparse(void);
void check_pam_auth(const unsigned char *rolename)
{
struct stat fstat;
int pid;
if (stat(GRPAM_PATH, &fstat)) {
fprintf(stderr, "PAM authentication support has been disabled "
"in this install. Please reinstall gradm with PAM "
"authentication support.\n");
exit(EXIT_FAILURE);
}
pid = fork();
if (pid == 0) {
execl(GRPAM_PATH, GRPAM_PATH, rolename, (char *)NULL);
exit(EXIT_FAILURE);
} else if (pid > 0) {
int status = 0;
wait(&status);
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
return;
} else {
fprintf(stderr, "Error forking.\n");
exit(EXIT_FAILURE);
}
fprintf(stderr, "PAM authentication failed.\n");
exit(EXIT_FAILURE);
return;
}
void parse_learn_config(void)
{
struct stat st;
grlearn_configin = fopen(GR_LEARN_CONFIG_PATH, "r");
if (grlearn_configin == NULL) {
fprintf(stderr, "Unable to open %s: %s\n", GR_LEARN_CONFIG_PATH, strerror(errno));
exit(EXIT_FAILURE);
}
grlearn_configparse();
fclose(grlearn_configin);
if (high_protected_paths == NULL) {
fprintf(stderr, "Invalid learn_config detected. Please use the learn_config "
"provided with gradm as a starting point, otherwise learning will not "
"produce desired results.\n");
exit(EXIT_FAILURE);
}
return;
}
FILE *
open_acl_file(const char *filename)
{
FILE *aclfile;
if ((aclfile = fopen(filename, "r")) == NULL) {
fprintf(stderr, "Unable to open %s for reading.\n", filename);
failure("fopen");
}
return aclfile;
}
int
transmit_to_kernel(struct gr_arg_wrapper *buf)
{
int fd;
int err = 0;
if ((fd = open(GRDEV_PATH, O_WRONLY)) < 0) {
fprintf(stderr, "Could not open %s.\n", GRDEV_PATH);
failure("open");
}
if (write(fd, buf, sizeof(struct gr_arg_wrapper)) != sizeof(struct gr_arg_wrapper)) {
err = 1;
switch (errno) {
case EFAULT:
fprintf(stderr, "Error copying structures to the "
"kernel.\n");
break;
case ENOMEM:
fprintf(stderr, "Out of memory.\n");
break;
case EBUSY:
fprintf(stderr, "You have attempted to authenticate "
"while authentication was locked, try "
"again later.\n");
break;
case EAGAIN:
fprintf(stderr, "Your request was ignored, "
"please check the kernel logs for more "
"info.\n");
break;
case EPERM:
if (buf->arg->mode != GRADM_UNSPROLE)
fprintf(stderr, "Invalid password.\n");
else
fprintf(stderr, "You are not in a special role.\n");
break;
case EINVAL:
default:
fprintf(stderr, "You are using incompatible "
"versions of gradm and grsecurity.\n"
"Please update both versions to the "
"ones available on the website.\n"
"Make sure your gradm has been compiled "
"for the kernel you are currently running.\n");
}
}
close(fd);
ioctl(0, TIOCNXCL);
if (buf->arg->mode != GRADM_DISABLE) {
memset(buf->arg, 0, sizeof(struct gr_arg));
if (err)
exit(EXIT_FAILURE);
} else {
memset(buf->arg, 0, sizeof(struct gr_arg));
if (err)
exit(EXIT_FAILURE);
else
stop_grlearn();
}
return err;
}
void ctrl_sighandler(int sig)
{
struct termios term;
signal(sig, SIG_IGN);
tcgetattr(STDIN_FILENO, &term);
term.c_lflag |= ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &term);
printf("\n");
fflush(stdout);
ioctl(0, TIOCNXCL);
exit(EXIT_FAILURE);
}
void check_acl_status(u_int16_t reqmode)
{
int fd;
int retval;
struct gr_arg arg;
struct gr_arg_wrapper wrapper;
signal(SIGINT, ctrl_sighandler);
ioctl(0, TIOCEXCL);
wrapper.version = GRADM_VERSION;
wrapper.size = sizeof(struct gr_arg);
wrapper.arg = &arg;
arg.mode = GRADM_STATUS;
if ((fd = open(GRDEV_PATH, O_WRONLY)) < 0) {
fprintf(stderr, "Could not open %s.\n", GRDEV_PATH);
failure("open");
}
retval = write(fd, &wrapper, sizeof(struct gr_arg_wrapper));
close(fd);
switch (reqmode) {
case GRADM_PASSSET:
if (retval == 3) {
printf("The terminal you are using is unsafe for this operation. Use another terminal.\n");
ioctl(0, TIOCNXCL);
exit(EXIT_FAILURE);
}
break;
case GRADM_STATUS:
ioctl(0, TIOCNXCL);
if (retval == 1) {
printf("The RBAC system is currently enabled.\n");
exit(0);
} else if (retval == 2) {
printf("The RBAC system is currently disabled.\n");
exit(1);
} else if (retval == 3) {
printf("The terminal you are using is unsafe. Use another terminal.\n");
exit(2);
} else {
printf("The /dev/grsec device is not properly installed on your system or you are not using a grsecurity kernel.\n");
exit(-1);
}
break;
case GRADM_ENABLE:
ioctl(0, TIOCNXCL);
if (retval == 1) {
printf("The operation you requested cannot be performed "
"because the RBAC system is currently enabled.\n");
exit(EXIT_FAILURE);
}
break;
case GRADM_RELOAD:
case GRADM_DISABLE:
case GRADM_SPROLE:
case GRADM_UNSPROLE:
case GRADM_MODSEGV:
if (retval == 2) {
printf("The operation you requested cannot be performed "
"because the RBAC system is currently disabled.\n");
ioctl(0, TIOCNXCL);
exit(EXIT_FAILURE);
} else if (retval == 3 && reqmode != GRADM_UNSPROLE) {
printf("The terminal you are using is unsafe for this operation. Use another terminal.\n");
ioctl(0, TIOCNXCL);
exit(EXIT_FAILURE);
}
break;
}
return;
}
void
init_variables(void)
{
extern struct ip_acl ip;
lineno = 1;
current_acl_file = NULL;
current_role = NULL;
current_subject = NULL;
num_roles = 0;
num_subjects = 0;
num_objects = 0;
num_pointers = 0;
dont_reduce_dirs = NULL;
always_reduce_dirs = NULL;
protected_paths = NULL;
read_protected_paths = NULL;
high_reduce_dirs = NULL;
high_protected_paths = NULL;
add_to_string_array(&high_protected_paths, GRSEC_DIR);
add_to_string_array(&high_protected_paths, GRDEV_PATH);
memset(&ip, 0, sizeof (ip));
return;
}
void
change_current_acl_file(const char *filename)
{
char *p;
if ((p = (char *) calloc(strlen(filename) + 1, sizeof (char))) == NULL)
failure("calloc");
strcpy(p, filename);
current_acl_file = p;
return;
}
int
parent_dir(const char *filename, char *parent_dirent[])
{
int i;
if ((strlen(*parent_dirent) <= 1) || (strlen(filename) <= 1))
return 0;
for (i = strlen(*parent_dirent) - 1; i >= 0; i--) {
if (i)
(*parent_dirent)[i] = '\0';
if (filename[i] == '/')
return 1;
}
return 0;
}