summaryrefslogtreecommitdiffstats
path: root/sys/security/lomac/kernel_mediate.c
blob: 472dea6c258dd49598937b6bdbd1e0bdda002d9c (plain)
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
/*-
 * Copyright (c) 2001 Networks Associates Technologies, Inc.
 * All rights reserved.
 *
 * This software was developed for the FreeBSD Project by NAI Labs, the
 * Security Research Division of Network Associates, Inc. under
 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
 * CHATS research program.
 *
 * 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.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * 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.
 *
 * $Id$
 * $FreeBSD$
 */

/*
 * This file contains functions that make access control decisions
 * concerning wether or not given system calls should be allowed
 * or denied.  This activity is called "mediation".  These functions
 * generally consider both the parameters passed to a system call
 * and the current internal state of LOMAC in the course of making
 * a decision.  However, they do not modify these parameters or
 * LOMAC's internal state.  Functions for modifying LOMAC's internal
 * state can be found in lomac_monitor.c.
 *
 */

#include "kernel_interface.h"
#include "kernel_mediate.h"
#if 0
#include "lomac_plm.h"
#endif
#include "kernel_log.h"

/* mediate_subject_level_subject()
 *
 * in:     op_s          - name of operation to mediate
 *         p_subject_one - subject one (for informational purposes only)
 *         level_one     - already-known level of the first subject
 *         p_subject_two - subject two
 * out:    nothing
 * return: value   condition
 *         -----   ---------
 *           0     caller should deny operation
 *           1     caller should allow operation
 *
 *     This function returns 1 if `p_subject_one's level is at least
 * as great as `p_subject_two's level.  Otherwise, it logs a permission
 * failure on operation `op_s' and returns 0.
 *
 * This function is used to mediate pgrp changes.
 *
 */

int
mediate_subject_level_subject(const char *op_s,
    const lomac_subject_t *p_subject_one, level_t level_one,
    lomac_subject_t *p_subject_two) {

	lattr_t lattr_two;     /* lattr of `p_subject_two' */
	int ret_val;           /* result to return to caller */

#ifdef NO_MEDIATION
	ret_val = 1;        /* no denials, just logging */
#else
	ret_val = 0;        /* pessimistically assume deny */
#endif

	get_subject_lattr(p_subject_two, &lattr_two);

	if (lattr_two.level <= level_one) {
		ret_val = 1;      /* OK, allow */
	} else if (lomac_verbose_demote_deny) {
		lomac_log_t *logmsg = log_start();

		log_append_string(logmsg, "LOMAC: denied level-");
		log_append_int(logmsg, level_one);
		log_append_string(logmsg, " proc ");
		log_append_subject_id(logmsg, p_subject_one);
		log_append_string(logmsg, " ");
		log_append_string(logmsg, op_s);
		log_append_string(logmsg, " to level-");
		log_append_int(logmsg, lattr_two.level);
		log_append_string(logmsg, " proc ");
		log_append_subject_id(logmsg, p_subject_two);
		log_print(logmsg);
	}
	return (ret_val);
} /* mediate_subject_subject() */

/* mediate_subject_object()
 *
 * in:     op_s      - string describing operation, like "write" or "writev"
 *         p_subject - subject trying to operate on `p_object'.
 *         p_object  - object that `p_subject' is trying to operate on.
 * out:    nothing
 * return: value    condition
 *         -----    ---------
 *           0      Caller should prevent operation
 *           1      Caller should permit operation
 * 
 * This function returns 1 if the level of `p_object' is less than or
 * equal to the level of `p_subject'.  Otherwise, it returns 0 and
 * logs a permission denial on `op_s'.
 *
 * This function allows LOMAC to mediate write and writev system calls.
 *
 */

int
mediate_subject_object(const char *op_s, lomac_subject_t *p_subject,
    const lomac_object_t *p_object) {
	lattr_t subject_lattr;     /* lattr of `p_subject' */
	lattr_t object_lattr;      /* lattr of `p_object' */
	int ret_val;               /* value to return to caller */

#ifdef NO_MEDIATION
	ret_val = 1;           /* allow operation regardless of decision */
#else
	ret_val = 0;           /* pessimistically assume deny */
#endif

	/* Get the lattrs of `p_subject' and `p_object' so we can compare them. */
	get_subject_lattr(p_subject, &subject_lattr);
	get_object_lattr(p_object,  &object_lattr);

	/*
	 * If `p_subject's level is less than `p_object's level,
	 * we indicate that the operation must not be allowed.
	 */

	if (!lomac_must_deny(&subject_lattr, &object_lattr) ||
	    object_lattr.flags & LOMAC_ATTR_LOWWRITE) {
		ret_val = 1;         /* allow operation */
	} else if (lomac_verbose_demote_deny) {
		lomac_log_t *logmsg = log_start();
		log_append_string(logmsg, "LOMAC: level-");
		log_append_int(logmsg, subject_lattr.level);
		log_append_string(logmsg, " proc ");
		log_append_subject_id(logmsg, p_subject);
		log_append_string(logmsg, " denied ");
		log_append_string(logmsg, op_s);
		log_append_string(logmsg, " to level-");
		log_append_int(logmsg, object_lattr.level);
		log_append_string(logmsg, " object ");
		log_append_object_id(logmsg, p_object);
		log_append_string(logmsg, "\n");
		log_print(logmsg);
	}
	return (ret_val);
} /* mediate_subject_object() */


/* mediate_subject_object_open()
 *
 * in:     p_subject - subject trying to operate on `p_object'.
 *         p_object  - object that `p_subject' is trying to operate on.
 * out:    nothing
 * return: value    condition
 *         -----    ---------
 *           0      Caller should prevent operation
 *           1      Caller should permit operation
 * 
 * This function returns 1 if the level of `p_object' is less than or
 * equal to the level of `p_subject'.  Otherwise, it returns 0 and
 * logs a permission denial on `op_s'.
 *
 * This function allows LOMAC to mediate open system calls.
 *
 */

int
mediate_subject_object_open(lomac_subject_t *p_subject, 
    const lomac_object_t *p_object) {
	lattr_t subject_lattr;     /* lattr of `p_subject' */
	lattr_t object_lattr;      /* lattr of `p_object' */
	int ret_val;               /* value to return to caller */

#ifdef NO_MEDIATION
	ret_val = 1;           /* allow operation regardless of decision */
#else
	ret_val = 0;           /* pessimistically assume deny */
#endif

	/* Get the lattrs of `p_subject' and `p_object' so we can compare them. */
	get_subject_lattr(p_subject, &subject_lattr);
	get_object_lattr(p_object,  &object_lattr);

	/*
	 * If `p_subject's level is less than `p_object's level,
	 * we must indicate that the operation should not be allowed.
	 */
	if (lomac_must_deny(&subject_lattr, &object_lattr) &&
	    object_lattr.flags & LOMAC_ATTR_LOWNOOPEN) {
		if (lomac_verbose_demote_deny) {
			lomac_log_t *logmsg = log_start();

			log_append_string(logmsg, "LOMAC: level-");
			log_append_int(logmsg, subject_lattr.level);
			log_append_string(logmsg, " proc ");
			log_append_subject_id(logmsg, p_subject);
			log_append_string(logmsg, " denied open to level-");
			log_append_int(logmsg, object_lattr.level);
			log_append_string(logmsg, " object ");
			log_append_object_id(logmsg, p_object);
			log_append_string(logmsg, "\n");
			log_print(logmsg);
		}
	} else {
		ret_val = 1;         /* allow operation */
	} /* if/else allow/deny */
	return (ret_val);
} /* mediate_subject_object() */


/* mediate_subject_at_level()
 *
 * in:     op_s         - name of operation being mediated
 *         p_subject    - subject whose level we want to check
 *         target_level - level to compare to `p_subject's level
 *         
 * out:    nothing
 * return: value   condition
 *         -----   ---------
 *           0     `p_subject' is not at `target_level'
 *           1     `p_subject' is at `target_level'
 *
 * This function provides a predicate for determining whether or not
 * `p_subject' is at the level specified by `target_level'.  This
 * function compares `p_subject's level to `target_level'.  If the
 * levels match, it retruns 1.  Otherwise, it logs a permission denial
 * on `op_s' and returns 0.
 *
 */

int
mediate_subject_at_level(const char *op_s, lomac_subject_t *p_subject,
    const level_t target_level) {
	lattr_t subject_lattr;   /* lattr of `p_subject' */
	int ret_val;             /* value returned to caller */

#ifdef NO_MEDIATION
	ret_val = 1;           /* allow operation regardless of decision */
#else
	ret_val = 0;           /* pessimistically assume deny */
#endif

	/* Make `subject_lattr' the lattr of `p_subject'. */
	get_subject_lattr(p_subject, &subject_lattr);

	/* compare with `target_lattr */
	if (subject_lattr.level == target_level) {
		ret_val = 1;    /* allow operation */
	} else if (lomac_verbose_demote_deny) {
		lomac_log_t *logmsg = log_start();

		log_append_string(logmsg, "LOMAC: denied level-");
		log_append_int(logmsg, subject_lattr.level);
		log_append_string(logmsg, " proc ");
		log_append_subject_id(logmsg, p_subject);
		log_append_string(logmsg, "'s ");
		log_append_string(logmsg, op_s);
		log_append_string(logmsg, ".\n");
		log_print(logmsg);
	}
	return (ret_val);
} /* mediate_subject_at_level() */
OpenPOWER on IntegriCloud