summaryrefslogtreecommitdiffstats
path: root/sys/security/lomac/kernel_log.c
blob: a43e70c8f2a2244640557a803e266c78d1d5eb29 (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
/*-
 * 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$
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/sysctl.h>
#include <sys/syslog.h>

#include "lomacfs.h"

#include "kernel_interface.h"

/* The following definition sets the global log verbosity level. */
SYSCTL_NODE(_kern, OID_AUTO, lomac, CTLFLAG_RW, 0, "LOMAC");
SYSCTL_NODE(_kern_lomac, OID_AUTO, verbose, CTLFLAG_RW, 0, "LOMAC verbosity");
#define	VERBOSITY_SETTING(level) 					\
	unsigned int lomac_verbose_##level## = 1;			\
	SYSCTL_UINT(_kern_lomac_verbose, OID_AUTO, level,		\
	    CTLFLAG_RW, &lomac_verbose_##level##, 1, "")
#include "kernel_log.h"

/* sbuf_start()
 *
 * in:     nothing
 * out:    nothing
 * return: struct sbuf * to pass to later callers
 *
 */

lomac_log_t *
log_start(void) {
	struct sbuf *s;

	s = sbuf_new(NULL, NULL, PATH_MAX * 2, 0);
	KASSERT(s != NULL, ("sbuf uses M_WAITOK -- must not return NULL!"));
	return (s);
} /* log_start() */


/* log_append_string()
 *
 * in:     s - a struct sbuf *
 * in:     data_s - null-terminated string to append to log
 * out:    nothing, see description for side-effects
 * return: nothing
 *
 *     This function appends `data_s' to `log_s', being careful to ensure
 * that there is sufficient room in `log_s' for the data and a null 
 * terminator.  If there is insufficient room in `log_s' for the entire
 * `data_s' string, this function will append only the prefix of `data_s'
 * which fits.
 *
 */

void	
log_append_string(lomac_log_t *s, const char *data_s) {

	(void)sbuf_cat(s, data_s);
} /* log_append_string */


/* log_append_int()
 *
 * in:     data - integer value to append to log
 * out:    nothing, see description for side-effects
 * return: nothing
 *
 *     This function determines the ASCII representation of the integer
 * value in `data' and, if there is sufficient room, appends this
 * ASCII representation to `log_s'.  If there is insufficient room,
 * this function behaves as log_append_string().
 *
 */

void
log_append_int(lomac_log_t *s, int data) {

	(void)sbuf_printf(s, "%d", data);
} /* log_append_int() */


/* log_append_subject_id()
 *
 * in:     p_subject - subject whose ID we want to append to the log message
 * out:    nothing, see description for side-effects
 * return: nothing
 *
 *    This function appends a string describing the identity of `p_subject'
 * to `log_s'.  If there is insufficient room in `log_s' for the entire
 * ID string, only a (possibly empty) prefix of the ID string will be
 * appended.
 *
 */

void
log_append_subject_id(lomac_log_t *s, const lomac_subject_t *p_subject) {

	(void)sbuf_printf(s, "p%dg%du%d:%s", p_subject->p_pid,
	    p_subject->p_pgrp->pg_id, p_subject->p_ucred->cr_uid,
	    p_subject->p_comm);
} /* log_append_subject_id() */

/* log_append_object_id()
 *
 * in:     p_object - object whose ID we want to append to the log message
 * out:    nothing, see description for side-effects
 * return: nothing
 *
 *    This function appends a string describing the identity of `p_object'
 * to `log_s'.  If there is insufficient room in `log_s' for the entire
 * ID string, only a (possibly empty) prefix of the ID string will be
 * appended.
 *
 */

void
log_append_object_id(lomac_log_t *s, const lomac_object_t *p_object) {
	struct lomac_node *ln;

	switch (p_object->lo_type) {
	case LO_TYPE_UVNODE:
		(void)sbuf_printf(s, "vp %p", p_object->lo_object.vnode);
		break;
	case LO_TYPE_LVNODE:
		ln = VTOLOMAC(p_object->lo_object.vnode);
#ifdef LOMAC_DEBUG_INCNAME
		(void)sbuf_printf(s, "named \"%s\"", ln->ln_name);
#else
		if (ln->ln_entry != NULL)
			(void)sbuf_printf(s, "named \"%s\"",
			    ln->ln_entry->ln_path);
		else
			(void)sbuf_printf(s, "under \"%s\"",
			    ln->ln_underpolicy->ln_path);
#endif
		break;
	case LO_TYPE_PIPE:
		(void)sbuf_printf(s, "pipe %p", p_object->lo_object.pipe);
		break;
	case LO_TYPE_SOCKETPAIR:
		(void)sbuf_printf(s, "socket %p", p_object->lo_object.socket);
		break;
	default:
		panic("invalid LOMAC object type");
	}
} /* log_append_object_id() */

/* log_print()
 *
 * in:     nothing
 * out:    nothing
 * return: nothing
 *
 *     This function prints `log_s' to the system log.
 *
 */

void
log_print(lomac_log_t *s) {

	sbuf_finish(s);
	log(LOG_INFO, "%s", sbuf_data(s));
	sbuf_delete(s);
} /* log_print() */
OpenPOWER on IntegriCloud