2 * Copyright (c) 2003-2004 Networks Associates Technology, Inc.
5 * This software was developed for the FreeBSD Project 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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Developed by the TrustedBSD Project.
37 * Administratively limit access to local UDP/TCP ports for binding purposes.
38 * Intended to be combined with net.inet.ip.portrange.reservedhigh to allow
39 * specific uids and gids to bind specific ports for specific purposes,
40 * while not opening the door to any user replacing an "official" service
41 * while you're restarting it. This only affects ports explicitly bound by
42 * the user process (either for listen/outgoing socket for TCP, or send/
43 * receive for UDP). This module will not limit ports bound implicitly for
44 * out-going connections where the process hasn't explicitly selected a port:
45 * these are automatically selected by the IP stack.
47 * To use this module, security.mac.enforce_socket must be enabled, and
48 * you will probably want to twiddle the net.inet sysctl listed above.
49 * Then use sysctl(8) to modify the rules string:
51 * # sysctl security.mac.portacl.rules="uid:425:tcp:80,uid:425:tcp:79"
53 * This ruleset, for example, permits uid 425 to bind TCP ports 80 (http)
54 * and 79 (finger). User names and group names can't be used directly
55 * because the kernel only knows about uids and gids.
58 #include <sys/types.h>
59 #include <sys/param.h>
61 #include <sys/domain.h>
62 #include <sys/kernel.h>
63 #include <sys/libkern.h>
66 #include <sys/malloc.h>
67 #include <sys/mount.h>
68 #include <sys/mutex.h>
70 #include <sys/protosw.h>
71 #include <sys/queue.h>
72 #include <sys/systm.h>
73 #include <sys/sysproto.h>
74 #include <sys/sysent.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/sysctl.h>
81 #include <netinet/in.h>
82 #include <netinet/in_pcb.h>
86 #include <sys/mac_policy.h>
88 SYSCTL_DECL(_security_mac);
90 SYSCTL_NODE(_security_mac, OID_AUTO, portacl, CTLFLAG_RW, 0,
91 "TrustedBSD mac_portacl policy controls");
93 static int mac_portacl_enabled = 1;
94 SYSCTL_INT(_security_mac_portacl, OID_AUTO, enabled, CTLFLAG_RW,
95 &mac_portacl_enabled, 0, "Enforce portacl policy");
96 TUNABLE_INT("security.mac.portacl.enabled", &mac_portacl_enabled);
98 static int mac_portacl_suser_exempt = 1;
99 SYSCTL_INT(_security_mac_portacl, OID_AUTO, suser_exempt, CTLFLAG_RW,
100 &mac_portacl_suser_exempt, 0, "Privilege permits binding of any port");
101 TUNABLE_INT("security.mac.portacl.suser_exempt",
102 &mac_portacl_suser_exempt);
104 static int mac_portacl_autoport_exempt = 1;
105 SYSCTL_INT(_security_mac_portacl, OID_AUTO, autoport_exempt, CTLFLAG_RW,
106 &mac_portacl_autoport_exempt, 0, "Allow automatic allocation through "
107 "binding port 0 if not IP_PORTRANGELOW");
108 TUNABLE_INT("security.mac.portacl.autoport_exempt",
109 &mac_portacl_autoport_exempt);
111 static int mac_portacl_port_high = 1023;
112 SYSCTL_INT(_security_mac_portacl, OID_AUTO, port_high, CTLFLAG_RW,
113 &mac_portacl_port_high, 0, "Highest port to enforce for");
114 TUNABLE_INT("security.mac.portacl.port_high", &mac_portacl_port_high);
116 MALLOC_DEFINE(M_PORTACL, "portacl rule", "Rules for mac_portacl");
118 #define MAC_RULE_STRING_LEN 1024
122 #define RULE_PROTO_TCP 1
123 #define RULE_PROTO_UDP 2
130 TAILQ_ENTRY(rule) r_entries;
133 #define GID_STRING "gid"
134 #define TCP_STRING "tcp"
135 #define UID_STRING "uid"
136 #define UDP_STRING "udp"
139 * Text format for the rule string is that a rule consists of a
140 * comma-seperated list of elements. Each element is in the form
141 * idtype:id:protocol:portnumber, and constitutes granting of permission
142 * for the specified binding.
145 static struct mtx rule_mtx;
146 static TAILQ_HEAD(rulehead, rule) rule_head;
147 static char rule_string[MAC_RULE_STRING_LEN];
150 toast_rules(struct rulehead *head)
154 while ((rule = TAILQ_FIRST(head)) != NULL) {
155 TAILQ_REMOVE(head, rule, r_entries);
156 free(rule, M_PORTACL);
161 * Note that there is an inherent race condition in the unload of modules
162 * and access via sysctl.
165 destroy(struct mac_policy_conf *mpc)
168 mtx_destroy(&rule_mtx);
169 toast_rules(&rule_head);
173 init(struct mac_policy_conf *mpc)
176 mtx_init(&rule_mtx, "rule_mtx", NULL, MTX_DEF);
177 TAILQ_INIT(&rule_head);
181 * Note: parsing routines are destructive on the passed string.
184 parse_rule_element(char *element, struct rule **rule)
186 char *idtype, *id, *protocol, *portnumber, *p;
191 new = malloc(sizeof(*new), M_PORTACL, M_ZERO | M_WAITOK);
193 idtype = strsep(&element, ":");
194 if (idtype == NULL) {
198 id = strsep(&element, ":");
203 new->r_id = strtol(id, &p, 10);
208 if (strcmp(idtype, UID_STRING) == 0)
209 new->r_idtype = RULE_UID;
210 else if (strcmp(idtype, GID_STRING) == 0)
211 new->r_idtype = RULE_GID;
216 protocol = strsep(&element, ":");
217 if (protocol == NULL) {
221 if (strcmp(protocol, TCP_STRING) == 0)
222 new->r_protocol = RULE_PROTO_TCP;
223 else if (strcmp(protocol, UDP_STRING) == 0)
224 new->r_protocol = RULE_PROTO_UDP;
229 portnumber = element;
230 if (portnumber == NULL) {
234 new->r_port = strtol(portnumber, &p, 10);
242 free(new, M_PORTACL);
250 parse_rules(char *string, struct rulehead *head)
257 while ((element = strsep(&string, ",")) != NULL) {
258 if (strlen(element) == 0)
260 error = parse_rule_element(element, &new);
263 TAILQ_INSERT_TAIL(head, new, r_entries);
272 * rule_printf() and rules_to_string() are unused currently because they rely
273 * on sbufs with auto-extension, which may sleep while holding a mutex.
274 * Instead, the non-canonical user-generated rule string is returned to the
275 * user when the rules are queried, which is faster anyway.
279 rule_printf(struct sbuf *sb, struct rule *rule)
281 const char *idtype, *protocol;
283 switch(rule->r_idtype) {
291 panic("rule_printf: unknown idtype (%d)\n", rule->r_idtype);
294 switch (rule->r_protocol) {
296 protocol = TCP_STRING;
299 protocol = UDP_STRING;
302 panic("rule_printf: unknown protocol (%d)\n",
305 sbuf_printf(sb, "%s:%jd:%s:%d", idtype, (intmax_t)rule->r_id,
306 protocol, rule->r_port);
310 rules_to_string(void)
317 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
320 for (rule = TAILQ_FIRST(&rule_head); rule != NULL;
321 rule = TAILQ_NEXT(rule, r_entries)) {
325 sbuf_printf(sb, ",");
326 rule_printf(sb, rule);
328 mtx_unlock(&rule_mtx);
330 temp = strdup(sbuf_data(sb), M_PORTACL);
337 * Note: due to races, there is not a single serializable order
338 * between parallel calls to the sysctl.
341 sysctl_rules(SYSCTL_HANDLER_ARGS)
343 char *string, *copy_string, *new_string;
344 struct rulehead head, save_head;
348 if (req->newptr == NULL) {
349 new_string = malloc(MAC_RULE_STRING_LEN, M_PORTACL,
351 strcpy(new_string, rule_string);
354 string = rule_string;
356 error = sysctl_handle_string(oidp, string, MAC_RULE_STRING_LEN, req);
360 if (req->newptr != NULL) {
361 copy_string = strdup(string, M_PORTACL);
363 error = parse_rules(copy_string, &head);
364 free(copy_string, M_PORTACL);
368 TAILQ_INIT(&save_head);
370 TAILQ_CONCAT(&save_head, &rule_head, r_entries);
371 TAILQ_CONCAT(&rule_head, &head, r_entries);
372 strcpy(rule_string, string);
373 mtx_unlock(&rule_mtx);
374 toast_rules(&save_head);
377 if (new_string != NULL)
378 free(new_string, M_PORTACL);
382 SYSCTL_PROC(_security_mac_portacl, OID_AUTO, rules,
383 CTLTYPE_STRING|CTLFLAG_RW, 0, 0, sysctl_rules, "A", "Rules");
386 rules_check(struct ucred *cred, int family, int type, u_int16_t port)
392 printf("Check requested for euid %d, family %d, type %d, port %d\n",
393 cred->cr_uid, family, type, port);
396 if (port > mac_portacl_port_high)
401 for (rule = TAILQ_FIRST(&rule_head);
403 rule = TAILQ_NEXT(rule, r_entries)) {
404 if (type == SOCK_DGRAM && rule->r_protocol != RULE_PROTO_UDP)
406 if (type == SOCK_STREAM && rule->r_protocol != RULE_PROTO_TCP)
408 if (port != rule->r_port)
410 if (rule->r_idtype == RULE_UID) {
411 if (cred->cr_uid == rule->r_id) {
415 } else if (rule->r_idtype == RULE_GID) {
416 if (cred->cr_gid == rule->r_id) {
419 } else if (groupmember(rule->r_id, cred)) {
424 panic("rules_check: unknown rule type %d",
427 mtx_unlock(&rule_mtx);
429 if (error != 0 && mac_portacl_suser_exempt != 0)
430 error = suser_cred(cred, 0);
436 * Note, this only limits the ability to explicitly bind a port, it
437 * doesn't limit implicitly bound ports for outgoing connections where
438 * the source port is left up to the IP stack to determine automatically.
441 check_socket_bind(struct ucred *cred, struct socket *so,
442 struct label *socketlabel, struct sockaddr *sockaddr)
444 struct sockaddr_in *sin;
449 /* Only run if we are enabled. */
450 if (mac_portacl_enabled == 0)
453 /* Only interested in IPv4 and IPv6 sockets. */
454 if (so->so_proto->pr_domain->dom_family != PF_INET &&
455 so->so_proto->pr_domain->dom_family != PF_INET6)
458 /* Currently, we don't attempt to deal with SOCK_RAW, etc. */
459 if (so->so_type != SOCK_DGRAM &&
460 so->so_type != SOCK_STREAM)
463 /* Reject addresses we don't understand; fail closed. */
464 if (sockaddr->sa_family != AF_INET &&
465 sockaddr->sa_family != AF_INET6)
468 family = so->so_proto->pr_domain->dom_family;
470 sin = (struct sockaddr_in *) sockaddr;
471 port = ntohs(sin->sin_port);
474 * Sockets are frequently bound with a specific IP address but a port
475 * number of '0' to request automatic port allocation. This is often
476 * desirable as long as IP_PORTRANGELOW isn't set, which might permit
477 * automatic allocation of a "privileged" port. The autoport exempt
478 * flag exempts port 0 allocation from rule checking as long as a low
479 * port isn't required.
481 if (mac_portacl_autoport_exempt && port == 0) {
483 if ((inp->inp_flags & INP_LOWPORT) == 0)
487 return (rules_check(cred, family, type, port));
490 static struct mac_policy_ops mac_portacl_ops =
492 .mpo_destroy = destroy,
494 .mpo_check_socket_bind = check_socket_bind,
497 MAC_POLICY_SET(&mac_portacl_ops, trustedbsd_mac_portacl,
498 "TrustedBSD MAC/portacl", MPC_LOADTIME_FLAG_UNLOADOK, NULL);