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
|
/*-
* Copyright (c) 2005 McAfee, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <sys/types.h>
#include <sys/mac.h>
#include <security/mac_bsdextended/mac_bsdextended.h>
#include <err.h>
#include <grp.h>
#include <pwd.h>
#include <ugidfw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Starting point for a regression test for mac_bsdextended(4) and the
* supporting libugidfw(3).
*/
void
usage(void)
{
fprintf(stderr, "test_ugidfw\n");
exit(-1);
}
/*
* This section of the regression test passes some test cases through the
* rule<->string routines to confirm they work approximately as desired.
*/
/*
* List of users and groups we must check exists before we can begin, since
* they are used in the string test rules. We use users and groups that will
* always exist in a default install used for regression testing.
*/
static const char *test_users[] = {
"root",
"daemon",
"operator",
"bin",
};
static const int test_users_len = sizeof(test_users) / sizeof(char *);
static const char *test_groups[] = {
"wheel",
"daemon",
"operator",
"bin",
};
static const int test_groups_len = sizeof(test_groups) / sizeof(char *);
/*
* List of test strings that must go in (and come out) of libugidfw intact.
*/
static const char *test_strings[] = {
/* Variations on subject and object uids. */
"subject uid root object uid root mode n",
"subject uid root object uid daemon mode n",
"subject uid daemon object uid root mode n",
"subject uid daemon object uid daemon mode n",
/* Variations on mode. */
"subject uid root object uid root mode a",
"subject uid root object uid root mode r",
"subject uid root object uid root mode s",
"subject uid root object uid root mode w",
"subject uid root object uid root mode x",
"subject uid root object uid root mode arswx",
/* Variations on subject and object gids. */
"subject gid wheel object gid wheel mode n",
"subject gid wheel object gid daemon mode n",
"subject gid daemon object gid wheel mode n",
"subject gid daemon object gid daemon mode n",
/* Subject uids and subject gids. */
"subject uid bin gid daemon object uid operator gid wheel mode n",
/* Not */
"subject not uid operator object uid bin mode n",
"subject uid bin object not uid operator mode n",
"subject not uid daemon object not uid operator mode n",
};
static const int test_strings_len = sizeof(test_strings) / sizeof(char *);
static void
test_libugidfw_strings(void)
{
struct mac_bsdextended_rule rule;
char errorstr[128];
char rulestr[128];
int i, error;
for (i = 0; i < test_users_len; i++) {
if (getpwnam(test_users[i]) == NULL)
err(-1, "test_libugidfw_strings: getpwnam: %s",
test_users[i]);
}
for (i = 0; i < test_groups_len; i++) {
if (getgrnam(test_groups[i]) == NULL)
err(-1, "test_libugidfw_strings: getgrnam: %s",
test_groups[i]);
}
for (i = 0; i < test_strings_len; i++) {
error = bsde_parse_rule_string(test_strings[i], &rule,
128, errorstr);
if (error == -1)
errx(-1, "bsde_parse_rule_string: '%s' (%d): %s",
test_strings[i], i, errorstr);
error = bsde_rule_to_string(&rule, rulestr, 128);
if (error < 0)
errx(-1, "bsde_rule_to_string: rule for '%s' "
"returned %d", test_strings[i], error);
if (strcmp(test_strings[i], rulestr) != 0)
errx(-1, "test_libugidfw: '%s' in, '%s' out",
test_strings[i], rulestr);
}
}
int
main(int argc, char *argv[])
{
char errorstr[128];
int count, slots;
if (argc != 1)
usage();
/* Print an error if a non-root user attemps to run the tests. */
if (getuid() != 0) {
fprintf(stderr, "Error! Only root may run this utility\n");
return (EXIT_FAILURE);
}
/*
* We can test some parts of the library without the MAC Framework
* and policy loaded, so run those tests before calling
* mac_is_present().
*/
test_libugidfw_strings();
switch (mac_is_present("bsdextended")) {
case -1:
err(-1, "mac_is_present");
case 1:
break;
case 0:
default:
errx(-1, "mac_bsdextended not loaded");
}
/*
* Some simple up-front checks to see if we're able to query the
* policy for basic state. We want the rule count to be 0 before
* starting, but "slots" is a property of prior runs and so we ignore
* the return value.
*/
count = bsde_get_rule_count(128, errorstr);
if (count == -1)
errx(-1, "bsde_get_rule_count: %s", errorstr);
if (count != 0)
errx(-1, "bsde_get_rule_count: %d rules", count);
slots = bsde_get_rule_slots(128, errorstr);
if (slots == -1)
errx(-1, "bsde_get_rule_slots: %s", errorstr);
return (0);
}
|