net.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor network mediation
  5. *
  6. * Copyright (C) 1998-2008 Novell/SUSE
  7. * Copyright 2009-2017 Canonical Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2 of the
  12. * License.
  13. */
  14. #include "include/apparmor.h"
  15. #include "include/audit.h"
  16. #include "include/cred.h"
  17. #include "include/label.h"
  18. #include "include/net.h"
  19. #include "include/policy.h"
  20. #include "include/secid.h"
  21. #include "net_names.h"
  22. struct aa_sfs_entry aa_sfs_entry_network[] = {
  23. AA_SFS_FILE_STRING("af_mask", AA_SFS_AF_MASK),
  24. { }
  25. };
  26. static const char * const net_mask_names[] = {
  27. "unknown",
  28. "send",
  29. "receive",
  30. "unknown",
  31. "create",
  32. "shutdown",
  33. "connect",
  34. "unknown",
  35. "setattr",
  36. "getattr",
  37. "setcred",
  38. "getcred",
  39. "chmod",
  40. "chown",
  41. "chgrp",
  42. "lock",
  43. "mmap",
  44. "mprot",
  45. "unknown",
  46. "unknown",
  47. "accept",
  48. "bind",
  49. "listen",
  50. "unknown",
  51. "setopt",
  52. "getopt",
  53. "unknown",
  54. "unknown",
  55. "unknown",
  56. "unknown",
  57. "unknown",
  58. "unknown",
  59. };
  60. /* audit callback for net specific fields */
  61. void audit_net_cb(struct audit_buffer *ab, void *va)
  62. {
  63. struct common_audit_data *sa = va;
  64. audit_log_format(ab, " family=");
  65. if (address_family_names[sa->u.net->family])
  66. audit_log_string(ab, address_family_names[sa->u.net->family]);
  67. else
  68. audit_log_format(ab, "\"unknown(%d)\"", sa->u.net->family);
  69. audit_log_format(ab, " sock_type=");
  70. if (sock_type_names[aad(sa)->net.type])
  71. audit_log_string(ab, sock_type_names[aad(sa)->net.type]);
  72. else
  73. audit_log_format(ab, "\"unknown(%d)\"", aad(sa)->net.type);
  74. audit_log_format(ab, " protocol=%d", aad(sa)->net.protocol);
  75. if (aad(sa)->request & NET_PERMS_MASK) {
  76. audit_log_format(ab, " requested_mask=");
  77. aa_audit_perm_mask(ab, aad(sa)->request, NULL, 0,
  78. net_mask_names, NET_PERMS_MASK);
  79. if (aad(sa)->denied & NET_PERMS_MASK) {
  80. audit_log_format(ab, " denied_mask=");
  81. aa_audit_perm_mask(ab, aad(sa)->denied, NULL, 0,
  82. net_mask_names, NET_PERMS_MASK);
  83. }
  84. }
  85. if (aad(sa)->peer) {
  86. audit_log_format(ab, " peer=");
  87. aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
  88. FLAGS_NONE, GFP_ATOMIC);
  89. }
  90. }
  91. /* Generic af perm */
  92. int aa_profile_af_perm(struct aa_profile *profile, struct common_audit_data *sa,
  93. u32 request, u16 family, int type)
  94. {
  95. struct aa_perms perms = { };
  96. unsigned int state;
  97. __be16 buffer[2];
  98. AA_BUG(family >= AF_MAX);
  99. AA_BUG(type < 0 || type >= SOCK_MAX);
  100. if (profile_unconfined(profile))
  101. return 0;
  102. state = PROFILE_MEDIATES(profile, AA_CLASS_NET);
  103. if (!state)
  104. return 0;
  105. buffer[0] = cpu_to_be16(family);
  106. buffer[1] = cpu_to_be16((u16) type);
  107. state = aa_dfa_match_len(profile->policy.dfa, state, (char *) &buffer,
  108. 4);
  109. aa_compute_perms(profile->policy.dfa, state, &perms);
  110. aa_apply_modes_to_perms(profile, &perms);
  111. return aa_check_perms(profile, &perms, request, sa, audit_net_cb);
  112. }
  113. int aa_af_perm(struct aa_label *label, const char *op, u32 request, u16 family,
  114. int type, int protocol)
  115. {
  116. struct aa_profile *profile;
  117. DEFINE_AUDIT_NET(sa, op, NULL, family, type, protocol);
  118. return fn_for_each_confined(label, profile,
  119. aa_profile_af_perm(profile, &sa, request, family,
  120. type));
  121. }
  122. static int aa_label_sk_perm(struct aa_label *label, const char *op, u32 request,
  123. struct sock *sk)
  124. {
  125. int error = 0;
  126. AA_BUG(!label);
  127. AA_BUG(!sk);
  128. if (!unconfined(label)) {
  129. struct aa_profile *profile;
  130. DEFINE_AUDIT_SK(sa, op, sk);
  131. error = fn_for_each_confined(label, profile,
  132. aa_profile_af_sk_perm(profile, &sa, request, sk));
  133. }
  134. return error;
  135. }
  136. int aa_sk_perm(const char *op, u32 request, struct sock *sk)
  137. {
  138. struct aa_label *label;
  139. int error;
  140. AA_BUG(!sk);
  141. AA_BUG(in_interrupt());
  142. /* TODO: switch to begin_current_label ???? */
  143. label = begin_current_label_crit_section();
  144. error = aa_label_sk_perm(label, op, request, sk);
  145. end_current_label_crit_section(label);
  146. return error;
  147. }
  148. int aa_sock_file_perm(struct aa_label *label, const char *op, u32 request,
  149. struct socket *sock)
  150. {
  151. AA_BUG(!label);
  152. AA_BUG(!sock);
  153. AA_BUG(!sock->sk);
  154. return aa_label_sk_perm(label, op, request, sock->sk);
  155. }
  156. #ifdef CONFIG_NETWORK_SECMARK
  157. static int apparmor_secmark_init(struct aa_secmark *secmark)
  158. {
  159. struct aa_label *label;
  160. if (secmark->label[0] == '*') {
  161. secmark->secid = AA_SECID_WILDCARD;
  162. return 0;
  163. }
  164. label = aa_label_strn_parse(&root_ns->unconfined->label,
  165. secmark->label, strlen(secmark->label),
  166. GFP_ATOMIC, false, false);
  167. if (IS_ERR(label))
  168. return PTR_ERR(label);
  169. secmark->secid = label->secid;
  170. return 0;
  171. }
  172. static int aa_secmark_perm(struct aa_profile *profile, u32 request, u32 secid,
  173. struct common_audit_data *sa, struct sock *sk)
  174. {
  175. int i, ret;
  176. struct aa_perms perms = { };
  177. if (profile->secmark_count == 0)
  178. return 0;
  179. for (i = 0; i < profile->secmark_count; i++) {
  180. if (!profile->secmark[i].secid) {
  181. ret = apparmor_secmark_init(&profile->secmark[i]);
  182. if (ret)
  183. return ret;
  184. }
  185. if (profile->secmark[i].secid == secid ||
  186. profile->secmark[i].secid == AA_SECID_WILDCARD) {
  187. if (profile->secmark[i].deny)
  188. perms.deny = ALL_PERMS_MASK;
  189. else
  190. perms.allow = ALL_PERMS_MASK;
  191. if (profile->secmark[i].audit)
  192. perms.audit = ALL_PERMS_MASK;
  193. }
  194. }
  195. aa_apply_modes_to_perms(profile, &perms);
  196. return aa_check_perms(profile, &perms, request, sa, audit_net_cb);
  197. }
  198. int apparmor_secmark_check(struct aa_label *label, char *op, u32 request,
  199. u32 secid, struct sock *sk)
  200. {
  201. struct aa_profile *profile;
  202. DEFINE_AUDIT_SK(sa, op, sk);
  203. return fn_for_each_confined(label, profile,
  204. aa_secmark_perm(profile, request, secid,
  205. &sa, sk));
  206. }
  207. #endif