net.c 5.8 KB

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