xref: /freebsd/sys/security/mac/mac_sysv_sem.c (revision e1e636193db45630c7881246d25902e57c43d24e)
1 /*-
2  * Copyright (c) 2003-2004 Networks Associates Technology, Inc.
3  * Copyright (c) 2006 SPARTA, Inc.
4  * Copyright (c) 2009 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * This software was developed for the FreeBSD Project in part by Network
8  * Associates Laboratories, the Security Research Division of Network
9  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
10  * as part of the DARPA CHATS research program.
11  *
12  * This software was enhanced by SPARTA ISSO under SPAWAR contract
13  * N66001-04-C-6019 ("SEFOS").
14  *
15  * This software was developed at the University of Cambridge Computer
16  * Laboratory with support from a grant from Google, Inc.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 #include "opt_mac.h"
42 
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/sbuf.h>
49 #include <sys/systm.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/file.h>
53 #include <sys/namei.h>
54 #include <sys/sdt.h>
55 #include <sys/sysctl.h>
56 #include <sys/sem.h>
57 
58 #include <security/mac/mac_framework.h>
59 #include <security/mac/mac_internal.h>
60 #include <security/mac/mac_policy.h>
61 
62 static struct label *
63 mac_sysv_sem_label_alloc(void)
64 {
65 	struct label *label;
66 
67 	label = mac_labelzone_alloc(M_WAITOK);
68 	MAC_POLICY_PERFORM(sysvsem_init_label, label);
69 	return (label);
70 }
71 
72 void
73 mac_sysvsem_init(struct semid_kernel *semakptr)
74 {
75 
76 	if (mac_labeled & MPC_OBJECT_SYSVSEM)
77 		semakptr->label = mac_sysv_sem_label_alloc();
78 	else
79 		semakptr->label = NULL;
80 }
81 
82 static void
83 mac_sysv_sem_label_free(struct label *label)
84 {
85 
86 	MAC_POLICY_PERFORM_NOSLEEP(sysvsem_destroy_label, label);
87 	mac_labelzone_free(label);
88 }
89 
90 void
91 mac_sysvsem_destroy(struct semid_kernel *semakptr)
92 {
93 
94 	if (semakptr->label != NULL) {
95 		mac_sysv_sem_label_free(semakptr->label);
96 		semakptr->label = NULL;
97 	}
98 }
99 
100 void
101 mac_sysvsem_create(struct ucred *cred, struct semid_kernel *semakptr)
102 {
103 
104 	MAC_POLICY_PERFORM_NOSLEEP(sysvsem_create, cred, semakptr,
105 	    semakptr->label);
106 }
107 
108 void
109 mac_sysvsem_cleanup(struct semid_kernel *semakptr)
110 {
111 
112 	MAC_POLICY_PERFORM_NOSLEEP(sysvsem_cleanup, semakptr->label);
113 }
114 
115 MAC_CHECK_PROBE_DEFINE3(sysvsem_check_semctl, "struct ucred *",
116     "struct semid_kernel *", "int");
117 
118 int
119 mac_sysvsem_check_semctl(struct ucred *cred, struct semid_kernel *semakptr,
120     int cmd)
121 {
122 	int error;
123 
124 	MAC_POLICY_CHECK_NOSLEEP(sysvsem_check_semctl, cred, semakptr,
125 	    semakptr->label, cmd);
126 	MAC_CHECK_PROBE3(sysvsem_check_semctl, error, cred, semakptr, cmd);
127 
128 	return (error);
129 }
130 
131 MAC_CHECK_PROBE_DEFINE2(sysvsem_check_semget, "struct ucred *",
132     "struct semid_kernel *");
133 
134 int
135 mac_sysvsem_check_semget(struct ucred *cred, struct semid_kernel *semakptr)
136 {
137 	int error;
138 
139 	MAC_POLICY_CHECK_NOSLEEP(sysvsem_check_semget, cred, semakptr,
140 	    semakptr->label);
141 
142 	return (error);
143 }
144 
145 MAC_CHECK_PROBE_DEFINE3(sysvsem_check_semop, "struct ucred *",
146     "struct semid_kernel *", "size_t");
147 
148 int
149 mac_sysvsem_check_semop(struct ucred *cred, struct semid_kernel *semakptr,
150     size_t accesstype)
151 {
152 	int error;
153 
154 	MAC_POLICY_CHECK_NOSLEEP(sysvsem_check_semop, cred, semakptr,
155 	    semakptr->label, accesstype);
156 	MAC_CHECK_PROBE3(sysvsem_check_semop, error, cred, semakptr,
157 	    accesstype);
158 
159 	return (error);
160 }
161