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
62SYSCTL_DECL(_security_mac);
63
64static SYSCTL_NODE(_security_mac, OID_AUTO, seeotheruids,
65    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
66    "TrustedBSD mac_seeotheruids policy controls");
67
68static int	seeotheruids_enabled = 1;
69SYSCTL_INT(_security_mac_seeotheruids, OID_AUTO, enabled, CTLFLAG_RW,
70    &seeotheruids_enabled, 0, "Enforce seeotheruids policy");
71
72/*
73 * Exception: allow credentials to be aware of other credentials with the
74 * same primary gid.
75 */
76static int	primarygroup_enabled = 0;
77SYSCTL_INT(_security_mac_seeotheruids, OID_AUTO, primarygroup_enabled,
78    CTLFLAG_RW, &primarygroup_enabled, 0, "Make an exception for credentials "
79    "with the same real primary group id");
80
81/*
82 * Exception: allow the root user to be aware of other credentials by virtue
83 * of privilege.
84 */
85static int	suser_privileged = 1;
86SYSCTL_INT(_security_mac_seeotheruids, OID_AUTO, suser_privileged,
87    CTLFLAG_RW, &suser_privileged, 0, "Make an exception for superuser");
88
89/*
90 * Exception: allow processes with a specific gid to be exempt from the
91 * policy.  One sysctl enables this functionality; the other sets the
92 * exempt gid.
93 */
94static int	specificgid_enabled = 0;
95SYSCTL_INT(_security_mac_seeotheruids, OID_AUTO, specificgid_enabled,
96    CTLFLAG_RW, &specificgid_enabled, 0, "Make an exception for credentials "
97    "with a specific gid as their real primary group id or group set");
98
99static gid_t	specificgid = 0;
100SYSCTL_UINT(_security_mac_seeotheruids, OID_AUTO, specificgid, CTLFLAG_RW,
101    &specificgid, 0, "Specific gid to be exempt from seeotheruids policy");
102
103static int
104seeotheruids_check(struct ucred *cr1, struct ucred *cr2)
105{
106
107	if (!seeotheruids_enabled)
108		return (0);
109
110	if (primarygroup_enabled) {
111		if (cr1->cr_rgid == cr2->cr_rgid)
112			return (0);
113	}
114
115	if (specificgid_enabled) {
116		if (cr1->cr_rgid == specificgid ||
117		    groupmember(specificgid, cr1))
118			return (0);
119	}
120
121	if (cr1->cr_ruid == cr2->cr_ruid)
122		return (0);
123
124	if (suser_privileged) {
125		if (priv_check_cred(cr1, PRIV_SEEOTHERUIDS) == 0)
126			return (0);
127	}
128
129	return (ESRCH);
130}
131
132static int
133seeotheruids_proc_check_debug(struct ucred *cred, struct proc *p)
134{
135
136	return (seeotheruids_check(cred, p->p_ucred));
137}
138
139static int
140seeotheruids_proc_check_sched(struct ucred *cred, struct proc *p)
141{
142
143	return (seeotheruids_check(cred, p->p_ucred));
144}
145
146static int
147seeotheruids_proc_check_signal(struct ucred *cred, struct proc *p,
148    int signum)
149{
150
151	return (seeotheruids_check(cred, p->p_ucred));
152}
153
154static int
155seeotheruids_cred_check_visible(struct ucred *cr1, struct ucred *cr2)
156{
157
158	return (seeotheruids_check(cr1, cr2));
159}
160
161static int
162seeotheruids_inpcb_check_visible(struct ucred *cred, struct inpcb *inp,
163    struct label *inplabel)
164{
165
166	return (seeotheruids_check(cred, inp->inp_cred));
167}
168
169static int
170seeotheruids_socket_check_visible(struct ucred *cred, struct socket *so,
171    struct label *solabel)
172{
173
174	return (seeotheruids_check(cred, so->so_cred));
175}
176
177static struct mac_policy_ops seeotheruids_ops =
178{
179	.mpo_proc_check_debug = seeotheruids_proc_check_debug,
180	.mpo_proc_check_sched = seeotheruids_proc_check_sched,
181	.mpo_proc_check_signal = seeotheruids_proc_check_signal,
182	.mpo_cred_check_visible = seeotheruids_cred_check_visible,
183	.mpo_inpcb_check_visible = seeotheruids_inpcb_check_visible,
184	.mpo_socket_check_visible = seeotheruids_socket_check_visible,
185};
186
187MAC_POLICY_SET(&seeotheruids_ops, mac_seeotheruids,
188    "TrustedBSD MAC/seeotheruids", MPC_LOADTIME_FLAG_UNLOADOK, NULL);
189