net.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "net_names.h"
  21. struct aa_sfs_entry aa_sfs_entry_network[] = {
  22. AA_SFS_FILE_STRING("af_mask", AA_SFS_AF_MASK),
  23. { }
  24. };
  25. static const char * const net_mask_names[] = {
  26. "unknown",
  27. "send",
  28. "receive",
  29. "unknown",
  30. "create",
  31. "shutdown",
  32. "connect",
  33. "unknown",
  34. "setattr",
  35. "getattr",
  36. "setcred",
  37. "getcred",
  38. "chmod",
  39. "chown",
  40. "chgrp",
  41. "lock",
  42. "mmap",
  43. "mprot",
  44. "unknown",
  45. "unknown",
  46. "accept",
  47. "bind",
  48. "listen",
  49. "unknown",
  50. "setopt",
  51. "getopt",
  52. "unknown",
  53. "unknown",
  54. "unknown",
  55. "unknown",
  56. "unknown",
  57. "unknown",
  58. };
  59. /* audit callback for net specific fields */
  60. void audit_net_cb(struct audit_buffer *ab, void *va)
  61. {
  62. struct common_audit_data *sa = va;
  63. audit_log_format(ab, " family=");
  64. if (address_family_names[sa->u.net->family])
  65. audit_log_string(ab, address_family_names[sa->u.net->family]);
  66. else
  67. audit_log_format(ab, "\"unknown(%d)\"", sa->u.net->family);
  68. audit_log_format(ab, " sock_type=");
  69. if (sock_type_names[aad(sa)->net.type])
  70. audit_log_string(ab, sock_type_names[aad(sa)->net.type]);
  71. else
  72. audit_log_format(ab, "\"unknown(%d)\"", aad(sa)->net.type);
  73. audit_log_format(ab, " protocol=%d", aad(sa)->net.protocol);
  74. if (aad(sa)->request & NET_PERMS_MASK) {
  75. audit_log_format(ab, " requested_mask=");
  76. aa_audit_perm_mask(ab, aad(sa)->request, NULL, 0,
  77. net_mask_names, NET_PERMS_MASK);
  78. if (aad(sa)->denied & NET_PERMS_MASK) {
  79. audit_log_format(ab, " denied_mask=");
  80. aa_audit_perm_mask(ab, aad(sa)->denied, NULL, 0,
  81. net_mask_names, NET_PERMS_MASK);
  82. }
  83. }
  84. if (aad(sa)->peer) {
  85. audit_log_format(ab, " peer=");
  86. aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
  87. FLAGS_NONE, GFP_ATOMIC);
  88. }
  89. }
  90. /* Generic af perm */
  91. int aa_profile_af_perm(struct aa_profile *profile, struct common_audit_data *sa,
  92. u32 request, u16 family, int type)
  93. {
  94. struct aa_perms perms = { };
  95. unsigned int state;
  96. __be16 buffer[2];
  97. AA_BUG(family >= AF_MAX);
  98. AA_BUG(type < 0 || type >= SOCK_MAX);
  99. if (profile_unconfined(profile))
  100. return 0;
  101. state = PROFILE_MEDIATES(profile, AA_CLASS_NET);
  102. if (!state)
  103. return 0;
  104. buffer[0] = cpu_to_be16(family);
  105. buffer[1] = cpu_to_be16((u16) type);
  106. state = aa_dfa_match_len(profile->policy.dfa, state, (char *) &buffer,
  107. 4);
  108. aa_compute_perms(profile->policy.dfa, state, &perms);
  109. aa_apply_modes_to_perms(profile, &perms);
  110. return aa_check_perms(profile, &perms, request, sa, audit_net_cb);
  111. }
  112. int aa_af_perm(struct aa_label *label, const char *op, u32 request, u16 family,
  113. int type, int protocol)
  114. {
  115. struct aa_profile *profile;
  116. DEFINE_AUDIT_NET(sa, op, NULL, family, type, protocol);
  117. return fn_for_each_confined(label, profile,
  118. aa_profile_af_perm(profile, &sa, request, family,
  119. type));
  120. }
  121. static int aa_label_sk_perm(struct aa_label *label, const char *op, u32 request,
  122. struct sock *sk)
  123. {
  124. int error = 0;
  125. AA_BUG(!label);
  126. AA_BUG(!sk);
  127. if (!unconfined(label)) {
  128. struct aa_profile *profile;
  129. DEFINE_AUDIT_SK(sa, op, sk);
  130. error = fn_for_each_confined(label, profile,
  131. aa_profile_af_sk_perm(profile, &sa, request, sk));
  132. }
  133. return error;
  134. }
  135. int aa_sk_perm(const char *op, u32 request, struct sock *sk)
  136. {
  137. struct aa_label *label;
  138. int error;
  139. AA_BUG(!sk);
  140. AA_BUG(in_interrupt());
  141. /* TODO: switch to begin_current_label ???? */
  142. label = begin_current_label_crit_section();
  143. error = aa_label_sk_perm(label, op, request, sk);
  144. end_current_label_crit_section(label);
  145. return error;
  146. }
  147. int aa_sock_file_perm(struct aa_label *label, const char *op, u32 request,
  148. struct socket *sock)
  149. {
  150. AA_BUG(!label);
  151. AA_BUG(!sock);
  152. AA_BUG(!sock->sk);
  153. return aa_label_sk_perm(label, op, request, sock->sk);
  154. }