mac_cred.c revision 189503
1/*-
2 * Copyright (c) 1999-2002, 2008-2009 Robert N. M. Watson
3 * Copyright (c) 2001 Ilmar S. Habibulin
4 * Copyright (c) 2001-2003 Networks Associates Technology, Inc.
5 * Copyright (c) 2005 Samy Al Bahra
6 * Copyright (c) 2006 SPARTA, Inc.
7 * Copyright (c) 2008 Apple Inc.
8 * All rights reserved.
9 *
10 * This software was developed by Robert Watson and Ilmar Habibulin for the
11 * TrustedBSD Project.
12 *
13 * This software was developed for the FreeBSD Project in part by Network
14 * Associates Laboratories, the Security Research Division of Network
15 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
16 * as part of the DARPA CHATS research program.
17 *
18 * This software was enhanced by SPARTA ISSO under SPAWAR contract
19 * N66001-04-C-6019 ("SEFOS").
20 *
21 * This software was developed at the University of Cambridge Computer
22 * Laboratory with support from a grant from Google, Inc.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 *    notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 *    notice, this list of conditions and the following disclaimer in the
31 *    documentation and/or other materials provided with the distribution.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 */
45
46#include <sys/cdefs.h>
47__FBSDID("$FreeBSD: head/sys/security/mac/mac_cred.c 189503 2009-03-08 00:50:37Z rwatson $");
48
49#include "opt_kdtrace.h"
50#include "opt_mac.h"
51
52#include <sys/param.h>
53#include <sys/condvar.h>
54#include <sys/imgact.h>
55#include <sys/kernel.h>
56#include <sys/lock.h>
57#include <sys/malloc.h>
58#include <sys/mutex.h>
59#include <sys/mac.h>
60#include <sys/proc.h>
61#include <sys/sbuf.h>
62#include <sys/sdt.h>
63#include <sys/systm.h>
64#include <sys/vnode.h>
65#include <sys/mount.h>
66#include <sys/file.h>
67#include <sys/namei.h>
68#include <sys/sysctl.h>
69
70#include <vm/vm.h>
71#include <vm/pmap.h>
72#include <vm/vm_map.h>
73#include <vm/vm_object.h>
74
75#include <security/mac/mac_framework.h>
76#include <security/mac/mac_internal.h>
77#include <security/mac/mac_policy.h>
78
79struct label *
80mac_cred_label_alloc(void)
81{
82	struct label *label;
83
84	label = mac_labelzone_alloc(M_WAITOK);
85	MAC_PERFORM(cred_init_label, label);
86	return (label);
87}
88
89void
90mac_cred_init(struct ucred *cred)
91{
92
93	if (mac_labeled & MPC_OBJECT_CRED)
94		cred->cr_label = mac_cred_label_alloc();
95	else
96		cred->cr_label = NULL;
97}
98
99void
100mac_cred_label_free(struct label *label)
101{
102
103	MAC_PERFORM(cred_destroy_label, label);
104	mac_labelzone_free(label);
105}
106
107void
108mac_cred_destroy(struct ucred *cred)
109{
110
111	if (cred->cr_label != NULL) {
112		mac_cred_label_free(cred->cr_label);
113		cred->cr_label = NULL;
114	}
115}
116
117/*
118 * When a thread becomes an NFS server daemon, its credential may need to be
119 * updated to reflect this so that policies can recognize when file system
120 * operations originate from the network.
121 *
122 * At some point, it would be desirable if the credential used for each NFS
123 * RPC could be set based on the RPC context (i.e., source system, etc) to
124 * provide more fine-grained access control.
125 */
126void
127mac_cred_associate_nfsd(struct ucred *cred)
128{
129
130	MAC_PERFORM(cred_associate_nfsd, cred);
131}
132
133/*
134 * Initialize MAC label for the first kernel process, from which other kernel
135 * processes and threads are spawned.
136 */
137void
138mac_cred_create_swapper(struct ucred *cred)
139{
140
141	MAC_PERFORM(cred_create_swapper, cred);
142}
143
144/*
145 * Initialize MAC label for the first userland process, from which other
146 * userland processes and threads are spawned.
147 */
148void
149mac_cred_create_init(struct ucred *cred)
150{
151
152	MAC_PERFORM(cred_create_init, cred);
153}
154
155int
156mac_cred_externalize_label(struct label *label, char *elements,
157    char *outbuf, size_t outbuflen)
158{
159	int error;
160
161	MAC_EXTERNALIZE(cred, label, elements, outbuf, outbuflen);
162
163	return (error);
164}
165
166int
167mac_cred_internalize_label(struct label *label, char *string)
168{
169	int error;
170
171	MAC_INTERNALIZE(cred, label, string);
172
173	return (error);
174}
175
176/*
177 * When a new process is created, its label must be initialized.  Generally,
178 * this involves inheritence from the parent process, modulo possible deltas.
179 * This function allows that processing to take place.
180 */
181void
182mac_cred_copy(struct ucred *src, struct ucred *dest)
183{
184
185	MAC_PERFORM(cred_copy_label, src->cr_label, dest->cr_label);
186}
187
188/*
189 * When the subject's label changes, it may require revocation of privilege
190 * to mapped objects.  This can't be done on-the-fly later with a unified
191 * buffer cache.
192 */
193void
194mac_cred_relabel(struct ucred *cred, struct label *newlabel)
195{
196
197	MAC_PERFORM(cred_relabel, cred, newlabel);
198}
199
200MAC_CHECK_PROBE_DEFINE2(cred_check_relabel, "struct ucred *",
201    "struct label *");
202
203int
204mac_cred_check_relabel(struct ucred *cred, struct label *newlabel)
205{
206	int error;
207
208	MAC_CHECK(cred_check_relabel, cred, newlabel);
209	MAC_CHECK_PROBE2(cred_check_relabel, error, cred, newlabel);
210
211	return (error);
212}
213
214MAC_CHECK_PROBE_DEFINE2(cred_check_visible, "struct ucred *",
215    "struct ucred *");
216
217int
218mac_cred_check_visible(struct ucred *cr1, struct ucred *cr2)
219{
220	int error;
221
222	MAC_CHECK(cred_check_visible, cr1, cr2);
223	MAC_CHECK_PROBE2(cred_check_visible, error, cr1, cr2);
224
225	return (error);
226}
227