2 * security/tomoyo/network.c
4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
8 #include <linux/slab.h>
10 /* Structure for holding inet domain socket's address. */
11 struct tomoyo_inet_addr_info {
12 __be16 port; /* In network byte order. */
13 const __be32 *address; /* In network byte order. */
17 /* Structure for holding unix domain socket's address. */
18 struct tomoyo_unix_addr_info {
19 u8 *addr; /* This may not be '\0' terminated string. */
20 unsigned int addr_len;
23 /* Structure for holding socket address. */
24 struct tomoyo_addr_info {
27 struct tomoyo_inet_addr_info inet;
28 struct tomoyo_unix_addr_info unix0;
31 /* String table for socket's protocols. */
32 const char * const tomoyo_proto_keyword[TOMOYO_SOCK_MAX] = {
33 [SOCK_STREAM] = "stream",
34 [SOCK_DGRAM] = "dgram",
36 [SOCK_SEQPACKET] = "seqpacket",
37 [0] = " ", /* Dummy for avoiding NULL pointer dereference. */
38 [4] = " ", /* Dummy for avoiding NULL pointer dereference. */
42 * tomoyo_parse_ipaddr_union - Parse an IP address.
44 * @param: Pointer to "struct tomoyo_acl_param".
45 * @ptr: Pointer to "struct tomoyo_ipaddr_union".
47 * Returns true on success, false otherwise.
49 bool tomoyo_parse_ipaddr_union(struct tomoyo_acl_param *param,
50 struct tomoyo_ipaddr_union *ptr)
52 u8 * const min = ptr->ip[0].in6_u.u6_addr8;
53 u8 * const max = ptr->ip[1].in6_u.u6_addr8;
54 char *address = tomoyo_read_token(param);
57 if (!strchr(address, ':') &&
58 in4_pton(address, -1, min, '-', &end) > 0) {
61 ptr->ip[1].s6_addr32[0] = ptr->ip[0].s6_addr32[0];
62 else if (*end++ != '-' ||
63 in4_pton(end, -1, max, '\0', &end) <= 0 || *end)
67 if (in6_pton(address, -1, min, '-', &end) > 0) {
70 memmove(max, min, sizeof(u16) * 8);
71 else if (*end++ != '-' ||
72 in6_pton(end, -1, max, '\0', &end) <= 0 || *end)
80 * tomoyo_print_ipv4 - Print an IPv4 address.
82 * @buffer: Buffer to write to.
83 * @buffer_len: Size of @buffer.
84 * @min_ip: Pointer to __be32.
85 * @max_ip: Pointer to __be32.
89 static void tomoyo_print_ipv4(char *buffer, const unsigned int buffer_len,
90 const __be32 *min_ip, const __be32 *max_ip)
92 snprintf(buffer, buffer_len, "%pI4%c%pI4", min_ip,
93 *min_ip == *max_ip ? '\0' : '-', max_ip);
97 * tomoyo_print_ipv6 - Print an IPv6 address.
99 * @buffer: Buffer to write to.
100 * @buffer_len: Size of @buffer.
101 * @min_ip: Pointer to "struct in6_addr".
102 * @max_ip: Pointer to "struct in6_addr".
106 static void tomoyo_print_ipv6(char *buffer, const unsigned int buffer_len,
107 const struct in6_addr *min_ip,
108 const struct in6_addr *max_ip)
110 snprintf(buffer, buffer_len, "%pI6c%c%pI6c", min_ip,
111 !memcmp(min_ip, max_ip, 16) ? '\0' : '-', max_ip);
115 * tomoyo_print_ip - Print an IP address.
117 * @buf: Buffer to write to.
118 * @size: Size of @buf.
119 * @ptr: Pointer to "struct ipaddr_union".
123 void tomoyo_print_ip(char *buf, const unsigned int size,
124 const struct tomoyo_ipaddr_union *ptr)
127 tomoyo_print_ipv6(buf, size, &ptr->ip[0], &ptr->ip[1]);
129 tomoyo_print_ipv4(buf, size, &ptr->ip[0].s6_addr32[0],
130 &ptr->ip[1].s6_addr32[0]);
134 * Mapping table from "enum tomoyo_network_acl_index" to
135 * "enum tomoyo_mac_index" for inet domain socket.
137 static const u8 tomoyo_inet2mac
138 [TOMOYO_SOCK_MAX][TOMOYO_MAX_NETWORK_OPERATION] = {
140 [TOMOYO_NETWORK_BIND] = TOMOYO_MAC_NETWORK_INET_STREAM_BIND,
141 [TOMOYO_NETWORK_LISTEN] =
142 TOMOYO_MAC_NETWORK_INET_STREAM_LISTEN,
143 [TOMOYO_NETWORK_CONNECT] =
144 TOMOYO_MAC_NETWORK_INET_STREAM_CONNECT,
147 [TOMOYO_NETWORK_BIND] = TOMOYO_MAC_NETWORK_INET_DGRAM_BIND,
148 [TOMOYO_NETWORK_SEND] = TOMOYO_MAC_NETWORK_INET_DGRAM_SEND,
151 [TOMOYO_NETWORK_BIND] = TOMOYO_MAC_NETWORK_INET_RAW_BIND,
152 [TOMOYO_NETWORK_SEND] = TOMOYO_MAC_NETWORK_INET_RAW_SEND,
157 * Mapping table from "enum tomoyo_network_acl_index" to
158 * "enum tomoyo_mac_index" for unix domain socket.
160 static const u8 tomoyo_unix2mac
161 [TOMOYO_SOCK_MAX][TOMOYO_MAX_NETWORK_OPERATION] = {
163 [TOMOYO_NETWORK_BIND] = TOMOYO_MAC_NETWORK_UNIX_STREAM_BIND,
164 [TOMOYO_NETWORK_LISTEN] =
165 TOMOYO_MAC_NETWORK_UNIX_STREAM_LISTEN,
166 [TOMOYO_NETWORK_CONNECT] =
167 TOMOYO_MAC_NETWORK_UNIX_STREAM_CONNECT,
170 [TOMOYO_NETWORK_BIND] = TOMOYO_MAC_NETWORK_UNIX_DGRAM_BIND,
171 [TOMOYO_NETWORK_SEND] = TOMOYO_MAC_NETWORK_UNIX_DGRAM_SEND,
174 [TOMOYO_NETWORK_BIND] =
175 TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_BIND,
176 [TOMOYO_NETWORK_LISTEN] =
177 TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_LISTEN,
178 [TOMOYO_NETWORK_CONNECT] =
179 TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_CONNECT,
184 * tomoyo_same_inet_acl - Check for duplicated "struct tomoyo_inet_acl" entry.
186 * @a: Pointer to "struct tomoyo_acl_info".
187 * @b: Pointer to "struct tomoyo_acl_info".
189 * Returns true if @a == @b except permission bits, false otherwise.
191 static bool tomoyo_same_inet_acl(const struct tomoyo_acl_info *a,
192 const struct tomoyo_acl_info *b)
194 const struct tomoyo_inet_acl *p1 = container_of(a, typeof(*p1), head);
195 const struct tomoyo_inet_acl *p2 = container_of(b, typeof(*p2), head);
197 return p1->protocol == p2->protocol &&
198 tomoyo_same_ipaddr_union(&p1->address, &p2->address) &&
199 tomoyo_same_number_union(&p1->port, &p2->port);
203 * tomoyo_same_unix_acl - Check for duplicated "struct tomoyo_unix_acl" entry.
205 * @a: Pointer to "struct tomoyo_acl_info".
206 * @b: Pointer to "struct tomoyo_acl_info".
208 * Returns true if @a == @b except permission bits, false otherwise.
210 static bool tomoyo_same_unix_acl(const struct tomoyo_acl_info *a,
211 const struct tomoyo_acl_info *b)
213 const struct tomoyo_unix_acl *p1 = container_of(a, typeof(*p1), head);
214 const struct tomoyo_unix_acl *p2 = container_of(b, typeof(*p2), head);
216 return p1->protocol == p2->protocol &&
217 tomoyo_same_name_union(&p1->name, &p2->name);
221 * tomoyo_merge_inet_acl - Merge duplicated "struct tomoyo_inet_acl" entry.
223 * @a: Pointer to "struct tomoyo_acl_info".
224 * @b: Pointer to "struct tomoyo_acl_info".
225 * @is_delete: True for @a &= ~@b, false for @a |= @b.
227 * Returns true if @a is empty, false otherwise.
229 static bool tomoyo_merge_inet_acl(struct tomoyo_acl_info *a,
230 struct tomoyo_acl_info *b,
231 const bool is_delete)
234 &container_of(a, struct tomoyo_inet_acl, head)->perm;
236 const u8 b_perm = container_of(b, struct tomoyo_inet_acl, head)->perm;
247 * tomoyo_merge_unix_acl - Merge duplicated "struct tomoyo_unix_acl" entry.
249 * @a: Pointer to "struct tomoyo_acl_info".
250 * @b: Pointer to "struct tomoyo_acl_info".
251 * @is_delete: True for @a &= ~@b, false for @a |= @b.
253 * Returns true if @a is empty, false otherwise.
255 static bool tomoyo_merge_unix_acl(struct tomoyo_acl_info *a,
256 struct tomoyo_acl_info *b,
257 const bool is_delete)
260 &container_of(a, struct tomoyo_unix_acl, head)->perm;
262 const u8 b_perm = container_of(b, struct tomoyo_unix_acl, head)->perm;
273 * tomoyo_write_inet_network - Write "struct tomoyo_inet_acl" list.
275 * @param: Pointer to "struct tomoyo_acl_param".
277 * Returns 0 on success, negative value otherwise.
279 * Caller holds tomoyo_read_lock().
281 int tomoyo_write_inet_network(struct tomoyo_acl_param *param)
283 struct tomoyo_inet_acl e = { .head.type = TOMOYO_TYPE_INET_ACL };
286 const char *protocol = tomoyo_read_token(param);
287 const char *operation = tomoyo_read_token(param);
289 for (e.protocol = 0; e.protocol < TOMOYO_SOCK_MAX; e.protocol++)
290 if (!strcmp(protocol, tomoyo_proto_keyword[e.protocol]))
292 for (type = 0; type < TOMOYO_MAX_NETWORK_OPERATION; type++)
293 if (tomoyo_permstr(operation, tomoyo_socket_keyword[type]))
295 if (e.protocol == TOMOYO_SOCK_MAX || !e.perm)
297 if (param->data[0] == '@') {
300 tomoyo_get_group(param, TOMOYO_ADDRESS_GROUP);
301 if (!e.address.group)
304 if (!tomoyo_parse_ipaddr_union(param, &e.address))
307 if (!tomoyo_parse_number_union(param, &e.port) ||
308 e.port.values[1] > 65535)
310 error = tomoyo_update_domain(&e.head, sizeof(e), param,
311 tomoyo_same_inet_acl,
312 tomoyo_merge_inet_acl);
314 tomoyo_put_group(e.address.group);
315 tomoyo_put_number_union(&e.port);
320 * tomoyo_write_unix_network - Write "struct tomoyo_unix_acl" list.
322 * @param: Pointer to "struct tomoyo_acl_param".
324 * Returns 0 on success, negative value otherwise.
326 int tomoyo_write_unix_network(struct tomoyo_acl_param *param)
328 struct tomoyo_unix_acl e = { .head.type = TOMOYO_TYPE_UNIX_ACL };
331 const char *protocol = tomoyo_read_token(param);
332 const char *operation = tomoyo_read_token(param);
334 for (e.protocol = 0; e.protocol < TOMOYO_SOCK_MAX; e.protocol++)
335 if (!strcmp(protocol, tomoyo_proto_keyword[e.protocol]))
337 for (type = 0; type < TOMOYO_MAX_NETWORK_OPERATION; type++)
338 if (tomoyo_permstr(operation, tomoyo_socket_keyword[type]))
340 if (e.protocol == TOMOYO_SOCK_MAX || !e.perm)
342 if (!tomoyo_parse_name_union(param, &e.name))
344 error = tomoyo_update_domain(&e.head, sizeof(e), param,
345 tomoyo_same_unix_acl,
346 tomoyo_merge_unix_acl);
347 tomoyo_put_name_union(&e.name);
352 * tomoyo_audit_net_log - Audit network log.
354 * @r: Pointer to "struct tomoyo_request_info".
355 * @family: Name of socket family ("inet" or "unix").
356 * @protocol: Name of protocol in @family.
357 * @operation: Name of socket operation.
358 * @address: Name of address.
360 * Returns 0 on success, negative value otherwise.
362 static int tomoyo_audit_net_log(struct tomoyo_request_info *r,
363 const char *family, const u8 protocol,
364 const u8 operation, const char *address)
366 return tomoyo_supervisor(r, "network %s %s %s %s\n", family,
367 tomoyo_proto_keyword[protocol],
368 tomoyo_socket_keyword[operation], address);
372 * tomoyo_audit_inet_log - Audit INET network log.
374 * @r: Pointer to "struct tomoyo_request_info".
376 * Returns 0 on success, negative value otherwise.
378 static int tomoyo_audit_inet_log(struct tomoyo_request_info *r)
382 const __be32 *address = r->param.inet_network.address;
384 if (r->param.inet_network.is_ipv6)
385 tomoyo_print_ipv6(buf, sizeof(buf), (const struct in6_addr *)
386 address, (const struct in6_addr *) address);
388 tomoyo_print_ipv4(buf, sizeof(buf), address, address);
390 snprintf(buf + len, sizeof(buf) - len, " %u",
391 r->param.inet_network.port);
392 return tomoyo_audit_net_log(r, "inet", r->param.inet_network.protocol,
393 r->param.inet_network.operation, buf);
397 * tomoyo_audit_unix_log - Audit UNIX network log.
399 * @r: Pointer to "struct tomoyo_request_info".
401 * Returns 0 on success, negative value otherwise.
403 static int tomoyo_audit_unix_log(struct tomoyo_request_info *r)
405 return tomoyo_audit_net_log(r, "unix", r->param.unix_network.protocol,
406 r->param.unix_network.operation,
407 r->param.unix_network.address->name);
411 * tomoyo_check_inet_acl - Check permission for inet domain socket operation.
413 * @r: Pointer to "struct tomoyo_request_info".
414 * @ptr: Pointer to "struct tomoyo_acl_info".
416 * Returns true if granted, false otherwise.
418 static bool tomoyo_check_inet_acl(struct tomoyo_request_info *r,
419 const struct tomoyo_acl_info *ptr)
421 const struct tomoyo_inet_acl *acl =
422 container_of(ptr, typeof(*acl), head);
423 const u8 size = r->param.inet_network.is_ipv6 ? 16 : 4;
425 if (!(acl->perm & (1 << r->param.inet_network.operation)) ||
426 !tomoyo_compare_number_union(r->param.inet_network.port,
429 if (acl->address.group)
430 return tomoyo_address_matches_group
431 (r->param.inet_network.is_ipv6,
432 r->param.inet_network.address, acl->address.group);
433 return acl->address.is_ipv6 == r->param.inet_network.is_ipv6 &&
434 memcmp(&acl->address.ip[0],
435 r->param.inet_network.address, size) <= 0 &&
436 memcmp(r->param.inet_network.address,
437 &acl->address.ip[1], size) <= 0;
441 * tomoyo_check_unix_acl - Check permission for unix domain socket operation.
443 * @r: Pointer to "struct tomoyo_request_info".
444 * @ptr: Pointer to "struct tomoyo_acl_info".
446 * Returns true if granted, false otherwise.
448 static bool tomoyo_check_unix_acl(struct tomoyo_request_info *r,
449 const struct tomoyo_acl_info *ptr)
451 const struct tomoyo_unix_acl *acl =
452 container_of(ptr, typeof(*acl), head);
454 return (acl->perm & (1 << r->param.unix_network.operation)) &&
455 tomoyo_compare_name_union(r->param.unix_network.address,
460 * tomoyo_inet_entry - Check permission for INET network operation.
462 * @address: Pointer to "struct tomoyo_addr_info".
464 * Returns 0 on success, negative value otherwise.
466 static int tomoyo_inet_entry(const struct tomoyo_addr_info *address)
468 const int idx = tomoyo_read_lock();
469 struct tomoyo_request_info r;
471 const u8 type = tomoyo_inet2mac[address->protocol][address->operation];
473 if (type && tomoyo_init_request_info(&r, NULL, type)
474 != TOMOYO_CONFIG_DISABLED) {
475 r.param_type = TOMOYO_TYPE_INET_ACL;
476 r.param.inet_network.protocol = address->protocol;
477 r.param.inet_network.operation = address->operation;
478 r.param.inet_network.is_ipv6 = address->inet.is_ipv6;
479 r.param.inet_network.address = address->inet.address;
480 r.param.inet_network.port = ntohs(address->inet.port);
482 tomoyo_check_acl(&r, tomoyo_check_inet_acl);
483 error = tomoyo_audit_inet_log(&r);
484 } while (error == TOMOYO_RETRY_REQUEST);
486 tomoyo_read_unlock(idx);
491 * tomoyo_check_inet_address - Check permission for inet domain socket's operation.
493 * @addr: Pointer to "struct sockaddr".
494 * @addr_len: Size of @addr.
495 * @port: Port number.
496 * @address: Pointer to "struct tomoyo_addr_info".
498 * Returns 0 on success, negative value otherwise.
500 static int tomoyo_check_inet_address(const struct sockaddr *addr,
501 const unsigned int addr_len,
503 struct tomoyo_addr_info *address)
505 struct tomoyo_inet_addr_info *i = &address->inet;
507 switch (addr->sa_family) {
509 if (addr_len < SIN6_LEN_RFC2133)
512 i->address = (__be32 *)
513 ((struct sockaddr_in6 *) addr)->sin6_addr.s6_addr;
514 i->port = ((struct sockaddr_in6 *) addr)->sin6_port;
517 if (addr_len < sizeof(struct sockaddr_in))
520 i->address = (__be32 *)
521 &((struct sockaddr_in *) addr)->sin_addr;
522 i->port = ((struct sockaddr_in *) addr)->sin_port;
527 if (address->protocol == SOCK_RAW)
528 i->port = htons(port);
529 return tomoyo_inet_entry(address);
535 * tomoyo_unix_entry - Check permission for UNIX network operation.
537 * @address: Pointer to "struct tomoyo_addr_info".
539 * Returns 0 on success, negative value otherwise.
541 static int tomoyo_unix_entry(const struct tomoyo_addr_info *address)
543 const int idx = tomoyo_read_lock();
544 struct tomoyo_request_info r;
546 const u8 type = tomoyo_unix2mac[address->protocol][address->operation];
548 if (type && tomoyo_init_request_info(&r, NULL, type)
549 != TOMOYO_CONFIG_DISABLED) {
550 char *buf = address->unix0.addr;
551 int len = address->unix0.addr_len - sizeof(sa_family_t);
557 len = strnlen(buf, len);
559 buf = tomoyo_encode2(buf, len);
561 struct tomoyo_path_info addr;
564 tomoyo_fill_path_info(&addr);
565 r.param_type = TOMOYO_TYPE_UNIX_ACL;
566 r.param.unix_network.protocol = address->protocol;
567 r.param.unix_network.operation = address->operation;
568 r.param.unix_network.address = &addr;
570 tomoyo_check_acl(&r, tomoyo_check_unix_acl);
571 error = tomoyo_audit_unix_log(&r);
572 } while (error == TOMOYO_RETRY_REQUEST);
577 tomoyo_read_unlock(idx);
582 * tomoyo_check_unix_address - Check permission for unix domain socket's operation.
584 * @addr: Pointer to "struct sockaddr".
585 * @addr_len: Size of @addr.
586 * @address: Pointer to "struct tomoyo_addr_info".
588 * Returns 0 on success, negative value otherwise.
590 static int tomoyo_check_unix_address(struct sockaddr *addr,
591 const unsigned int addr_len,
592 struct tomoyo_addr_info *address)
594 struct tomoyo_unix_addr_info *u = &address->unix0;
596 if (addr->sa_family != AF_UNIX)
598 u->addr = ((struct sockaddr_un *) addr)->sun_path;
599 u->addr_len = addr_len;
600 return tomoyo_unix_entry(address);
604 * tomoyo_kernel_service - Check whether I'm kernel service or not.
606 * Returns true if I'm kernel service, false otherwise.
608 static bool tomoyo_kernel_service(void)
610 /* Nothing to do if I am a kernel service. */
611 return segment_eq(get_fs(), KERNEL_DS);
615 * tomoyo_sock_family - Get socket's family.
617 * @sk: Pointer to "struct sock".
619 * Returns one of PF_INET, PF_INET6, PF_UNIX or 0.
621 static u8 tomoyo_sock_family(struct sock *sk)
625 if (tomoyo_kernel_service())
627 family = sk->sk_family;
639 * tomoyo_socket_listen_permission - Check permission for listening a socket.
641 * @sock: Pointer to "struct socket".
643 * Returns 0 on success, negative value otherwise.
645 int tomoyo_socket_listen_permission(struct socket *sock)
647 struct tomoyo_addr_info address;
648 const u8 family = tomoyo_sock_family(sock->sk);
649 const unsigned int type = sock->type;
650 struct sockaddr_storage addr;
653 if (!family || (type != SOCK_STREAM && type != SOCK_SEQPACKET))
656 const int error = sock->ops->getname(sock, (struct sockaddr *)
657 &addr, &addr_len, 0);
662 address.protocol = type;
663 address.operation = TOMOYO_NETWORK_LISTEN;
664 if (family == PF_UNIX)
665 return tomoyo_check_unix_address((struct sockaddr *) &addr,
667 return tomoyo_check_inet_address((struct sockaddr *) &addr, addr_len,
672 * tomoyo_socket_connect_permission - Check permission for setting the remote address of a socket.
674 * @sock: Pointer to "struct socket".
675 * @addr: Pointer to "struct sockaddr".
676 * @addr_len: Size of @addr.
678 * Returns 0 on success, negative value otherwise.
680 int tomoyo_socket_connect_permission(struct socket *sock,
681 struct sockaddr *addr, int addr_len)
683 struct tomoyo_addr_info address;
684 const u8 family = tomoyo_sock_family(sock->sk);
685 const unsigned int type = sock->type;
689 address.protocol = type;
693 address.operation = TOMOYO_NETWORK_SEND;
697 address.operation = TOMOYO_NETWORK_CONNECT;
702 if (family == PF_UNIX)
703 return tomoyo_check_unix_address(addr, addr_len, &address);
704 return tomoyo_check_inet_address(addr, addr_len, sock->sk->sk_protocol,
709 * tomoyo_socket_bind_permission - Check permission for setting the local address of a socket.
711 * @sock: Pointer to "struct socket".
712 * @addr: Pointer to "struct sockaddr".
713 * @addr_len: Size of @addr.
715 * Returns 0 on success, negative value otherwise.
717 int tomoyo_socket_bind_permission(struct socket *sock, struct sockaddr *addr,
720 struct tomoyo_addr_info address;
721 const u8 family = tomoyo_sock_family(sock->sk);
722 const unsigned int type = sock->type;
731 address.protocol = type;
732 address.operation = TOMOYO_NETWORK_BIND;
737 if (family == PF_UNIX)
738 return tomoyo_check_unix_address(addr, addr_len, &address);
739 return tomoyo_check_inet_address(addr, addr_len, sock->sk->sk_protocol,
744 * tomoyo_socket_sendmsg_permission - Check permission for sending a datagram.
746 * @sock: Pointer to "struct socket".
747 * @msg: Pointer to "struct msghdr".
750 * Returns 0 on success, negative value otherwise.
752 int tomoyo_socket_sendmsg_permission(struct socket *sock, struct msghdr *msg,
755 struct tomoyo_addr_info address;
756 const u8 family = tomoyo_sock_family(sock->sk);
757 const unsigned int type = sock->type;
759 if (!msg->msg_name || !family ||
760 (type != SOCK_DGRAM && type != SOCK_RAW))
762 address.protocol = type;
763 address.operation = TOMOYO_NETWORK_SEND;
764 if (family == PF_UNIX)
765 return tomoyo_check_unix_address((struct sockaddr *)
767 msg->msg_namelen, &address);
768 return tomoyo_check_inet_address((struct sockaddr *) msg->msg_name,
770 sock->sk->sk_protocol, &address);