xref: /freebsd/sys/security/mac/mac_posix_shm.c (revision ea906c4152774dff300bb26fbfc1e4188351c89a)
1 /*-
2  * Copyright (c) 2003-2006 SPARTA, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project in part by Network
6  * Associates Laboratories, the Security Research Division of Network
7  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
8  * as part of the DARPA CHATS research program.
9  *
10  * This software was enhanced by SPARTA ISSO under SPAWAR contract
11  * N66001-04-C-6019 ("SEFOS").
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include "opt_mac.h"
39 
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/mman.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/systm.h>
46 #include <sys/sysctl.h>
47 
48 #include <security/mac/mac_framework.h>
49 #include <security/mac/mac_internal.h>
50 #include <security/mac/mac_policy.h>
51 
52 static struct label *
53 mac_posixshm_label_alloc(void)
54 {
55 	struct label *label;
56 
57 	label = mac_labelzone_alloc(M_WAITOK);
58 	MAC_PERFORM(posixshm_init_label, label);
59 	return (label);
60 }
61 
62 void
63 mac_posixshm_init(struct shmfd *shmfd)
64 {
65 
66 	shmfd->shm_label = mac_posixshm_label_alloc();
67 }
68 
69 static void
70 mac_posixshm_label_free(struct label *label)
71 {
72 
73 	MAC_PERFORM(posixshm_destroy_label, label);
74 	mac_labelzone_free(label);
75 }
76 
77 void
78 mac_posixshm_destroy(struct shmfd *shmfd)
79 {
80 
81 	mac_posixshm_label_free(shmfd->shm_label);
82 	shmfd->shm_label = NULL;
83 }
84 
85 void
86 mac_posixshm_create(struct ucred *cred, struct shmfd *shmfd)
87 {
88 
89 	MAC_PERFORM(posixshm_create, cred, shmfd, shmfd->shm_label);
90 }
91 
92 int
93 mac_posixshm_check_mmap(struct ucred *cred, struct shmfd *shmfd, int prot,
94     int flags)
95 {
96 	int error;
97 
98 	MAC_CHECK(posixshm_check_mmap, cred, shmfd, shmfd->shm_label, prot,
99 	    flags);
100 
101 	return (error);
102 }
103 
104 int
105 mac_posixshm_check_open(struct ucred *cred, struct shmfd *shmfd)
106 {
107 	int error;
108 
109 	MAC_CHECK(posixshm_check_open, cred, shmfd, shmfd->shm_label);
110 
111 	return (error);
112 }
113 
114 int
115 mac_posixshm_check_stat(struct ucred *active_cred, struct ucred *file_cred,
116     struct shmfd *shmfd)
117 {
118 	int error;
119 
120 	MAC_CHECK(posixshm_check_stat, active_cred, file_cred, shmfd,
121 	    shmfd->shm_label);
122 
123 	return (error);
124 }
125 
126 int
127 mac_posixshm_check_truncate(struct ucred *active_cred, struct ucred *file_cred,
128     struct shmfd *shmfd)
129 {
130 	int error;
131 
132 	MAC_CHECK(posixshm_check_truncate, active_cred, file_cred, shmfd,
133 	    shmfd->shm_label);
134 
135 	return (error);
136 }
137 
138 int
139 mac_posixshm_check_unlink(struct ucred *cred, struct shmfd *shmfd)
140 {
141 	int error;
142 
143 	MAC_CHECK(posixshm_check_unlink, cred, shmfd, shmfd->shm_label);
144 
145 	return (error);
146 }
147