1 /*-
2 * Copyright (c) 1999-2002, 2007 Robert N. M. Watson
3 * Copyright (c) 2001-2002 Networks Associates Technology, Inc.
4 * Copyright (c) 2006 SPARTA, Inc.
5 * All rights reserved.
6 *
7 * This software was developed by Robert Watson for the TrustedBSD Project.
8 *
9 * This software was developed for the FreeBSD Project in part by Network
10 * Associates Laboratories, the Security Research Division of Network
11 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
12 * as part of the DARPA CHATS research program.
13 *
14 * This software was enhanced by SPARTA ISSO under SPAWAR contract
15 * N66001-04-C-6019 ("SEFOS").
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 /*
40 * Developed by the TrustedBSD Project.
41 *
42 * Prevent processes owned by a particular uid from seeing various transient
43 * kernel objects associated with other uids.
44 */
45
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/module.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/systm.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sysctl.h>
55
56 #include <net/route.h>
57 #include <netinet/in.h>
58 #include <netinet/in_pcb.h>
59
60 #include <security/mac/mac_policy.h>
61
62 static SYSCTL_NODE(_security_mac, OID_AUTO, seeotheruids,
63 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
64 "TrustedBSD mac_seeotheruids policy controls");
65
66 static int seeotheruids_enabled = 1;
67 SYSCTL_INT(_security_mac_seeotheruids, OID_AUTO, enabled, CTLFLAG_RW,
68 &seeotheruids_enabled, 0, "Enforce seeotheruids policy");
69
70 /*
71 * Exception: allow credentials to be aware of other credentials with the
72 * same primary gid.
73 */
74 static int primarygroup_enabled = 0;
75 SYSCTL_INT(_security_mac_seeotheruids, OID_AUTO, primarygroup_enabled,
76 CTLFLAG_RW, &primarygroup_enabled, 0, "Make an exception for credentials "
77 "with the same real primary group id");
78
79 /*
80 * Exception: allow the root user to be aware of other credentials by virtue
81 * of privilege.
82 */
83 static int suser_privileged = 1;
84 SYSCTL_INT(_security_mac_seeotheruids, OID_AUTO, suser_privileged,
85 CTLFLAG_RW, &suser_privileged, 0, "Make an exception for superuser");
86
87 /*
88 * Exception: allow processes with a specific gid to be exempt from the
89 * policy. One sysctl enables this functionality; the other sets the
90 * exempt gid.
91 */
92 static int specificgid_enabled = 0;
93 SYSCTL_INT(_security_mac_seeotheruids, OID_AUTO, specificgid_enabled,
94 CTLFLAG_RW, &specificgid_enabled, 0, "Make an exception for credentials "
95 "with a specific gid as their real primary group id or group set");
96
97 static gid_t specificgid = 0;
98 SYSCTL_UINT(_security_mac_seeotheruids, OID_AUTO, specificgid, CTLFLAG_RW,
99 &specificgid, 0, "Specific gid to be exempt from seeotheruids policy");
100
101 static int
seeotheruids_check(struct ucred * cr1,struct ucred * cr2)102 seeotheruids_check(struct ucred *cr1, struct ucred *cr2)
103 {
104
105 if (!seeotheruids_enabled)
106 return (0);
107
108 if (primarygroup_enabled) {
109 if (cr1->cr_rgid == cr2->cr_rgid)
110 return (0);
111 }
112
113 if (specificgid_enabled) {
114 if (cr1->cr_rgid == specificgid ||
115 groupmember(specificgid, cr1))
116 return (0);
117 }
118
119 if (cr1->cr_ruid == cr2->cr_ruid)
120 return (0);
121
122 if (suser_privileged) {
123 if (priv_check_cred(cr1, PRIV_SEEOTHERUIDS) == 0)
124 return (0);
125 }
126
127 return (ESRCH);
128 }
129
130 static int
seeotheruids_proc_check_debug(struct ucred * cred,struct proc * p)131 seeotheruids_proc_check_debug(struct ucred *cred, struct proc *p)
132 {
133
134 return (seeotheruids_check(cred, p->p_ucred));
135 }
136
137 static int
seeotheruids_proc_check_sched(struct ucred * cred,struct proc * p)138 seeotheruids_proc_check_sched(struct ucred *cred, struct proc *p)
139 {
140
141 return (seeotheruids_check(cred, p->p_ucred));
142 }
143
144 static int
seeotheruids_proc_check_signal(struct ucred * cred,struct proc * p,int signum)145 seeotheruids_proc_check_signal(struct ucred *cred, struct proc *p,
146 int signum)
147 {
148
149 return (seeotheruids_check(cred, p->p_ucred));
150 }
151
152 static int
seeotheruids_cred_check_visible(struct ucred * cr1,struct ucred * cr2)153 seeotheruids_cred_check_visible(struct ucred *cr1, struct ucred *cr2)
154 {
155
156 return (seeotheruids_check(cr1, cr2));
157 }
158
159 static int
seeotheruids_inpcb_check_visible(struct ucred * cred,struct inpcb * inp,struct label * inplabel)160 seeotheruids_inpcb_check_visible(struct ucred *cred, struct inpcb *inp,
161 struct label *inplabel)
162 {
163
164 return (seeotheruids_check(cred, inp->inp_cred));
165 }
166
167 static int
seeotheruids_socket_check_visible(struct ucred * cred,struct socket * so,struct label * solabel)168 seeotheruids_socket_check_visible(struct ucred *cred, struct socket *so,
169 struct label *solabel)
170 {
171
172 return (seeotheruids_check(cred, so->so_cred));
173 }
174
175 static struct mac_policy_ops seeotheruids_ops =
176 {
177 .mpo_proc_check_debug = seeotheruids_proc_check_debug,
178 .mpo_proc_check_sched = seeotheruids_proc_check_sched,
179 .mpo_proc_check_signal = seeotheruids_proc_check_signal,
180 .mpo_cred_check_visible = seeotheruids_cred_check_visible,
181 .mpo_inpcb_check_visible = seeotheruids_inpcb_check_visible,
182 .mpo_socket_check_visible = seeotheruids_socket_check_visible,
183 };
184
185 MAC_POLICY_SET(&seeotheruids_ops, mac_seeotheruids,
186 "TrustedBSD MAC/seeotheruids", MPC_LOADTIME_FLAG_UNLOADOK, NULL);
187