net.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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/af_unix.h"
  11. #include "include/apparmor.h"
  12. #include "include/audit.h"
  13. #include "include/cred.h"
  14. #include "include/label.h"
  15. #include "include/net.h"
  16. #include "include/policy.h"
  17. #include "include/secid.h"
  18. #include "net_names.h"
  19. struct aa_sfs_entry aa_sfs_entry_network[] = {
  20. AA_SFS_FILE_STRING("af_mask", AA_SFS_AF_MASK),
  21. { }
  22. };
  23. struct aa_sfs_entry aa_sfs_entry_networkv9[] = {
  24. AA_SFS_FILE_STRING("af_mask", AA_SFS_AF_MASK),
  25. AA_SFS_FILE_BOOLEAN("af_unix", 1),
  26. { }
  27. };
  28. static const char * const net_mask_names[] = {
  29. "unknown",
  30. "send",
  31. "receive",
  32. "unknown",
  33. "create",
  34. "shutdown",
  35. "connect",
  36. "unknown",
  37. "setattr",
  38. "getattr",
  39. "setcred",
  40. "getcred",
  41. "chmod",
  42. "chown",
  43. "chgrp",
  44. "lock",
  45. "mmap",
  46. "mprot",
  47. "unknown",
  48. "unknown",
  49. "accept",
  50. "bind",
  51. "listen",
  52. "unknown",
  53. "setopt",
  54. "getopt",
  55. "unknown",
  56. "unknown",
  57. "unknown",
  58. "unknown",
  59. "unknown",
  60. "unknown",
  61. };
  62. static void audit_unix_addr(struct audit_buffer *ab, const char *str,
  63. struct sockaddr_un *addr, int addrlen)
  64. {
  65. int len = unix_addr_len(addrlen);
  66. if (!addr || len <= 0) {
  67. audit_log_format(ab, " %s=none", str);
  68. } else if (addr->sun_path[0]) {
  69. audit_log_format(ab, " %s=", str);
  70. audit_log_untrustedstring(ab, addr->sun_path);
  71. } else {
  72. audit_log_format(ab, " %s=\"@", str);
  73. if (audit_string_contains_control(&addr->sun_path[1], len - 1))
  74. audit_log_n_hex(ab, &addr->sun_path[1], len - 1);
  75. else
  76. audit_log_format(ab, "%.*s", len - 1,
  77. &addr->sun_path[1]);
  78. audit_log_format(ab, "\"");
  79. }
  80. }
  81. static void audit_unix_sk_addr(struct audit_buffer *ab, const char *str,
  82. const struct sock *sk)
  83. {
  84. const struct unix_sock *u = unix_sk(sk);
  85. if (u && u->addr) {
  86. int addrlen;
  87. struct sockaddr_un *addr = aa_sunaddr(u, &addrlen);
  88. audit_unix_addr(ab, str, addr, addrlen);
  89. } else {
  90. audit_unix_addr(ab, str, NULL, 0);
  91. }
  92. }
  93. /* audit callback for net specific fields */
  94. void audit_net_cb(struct audit_buffer *ab, void *va)
  95. {
  96. struct common_audit_data *sa = va;
  97. struct apparmor_audit_data *ad = aad(sa);
  98. if (address_family_names[ad->common.u.net->family])
  99. audit_log_format(ab, " family=\"%s\"",
  100. address_family_names[ad->common.u.net->family]);
  101. else
  102. audit_log_format(ab, " family=\"unknown(%d)\"",
  103. ad->common.u.net->family);
  104. if (sock_type_names[ad->net.type])
  105. audit_log_format(ab, " sock_type=\"%s\"",
  106. sock_type_names[ad->net.type]);
  107. else
  108. audit_log_format(ab, " sock_type=\"unknown(%d)\"",
  109. ad->net.type);
  110. audit_log_format(ab, " protocol=%d", ad->net.protocol);
  111. if (ad->request & NET_PERMS_MASK) {
  112. audit_log_format(ab, " requested_mask=");
  113. aa_audit_perm_mask(ab, ad->request, NULL, 0,
  114. net_mask_names, NET_PERMS_MASK);
  115. if (ad->denied & NET_PERMS_MASK) {
  116. audit_log_format(ab, " denied_mask=");
  117. aa_audit_perm_mask(ab, ad->denied, NULL, 0,
  118. net_mask_names, NET_PERMS_MASK);
  119. }
  120. }
  121. if (ad->common.u.net->family == PF_UNIX) {
  122. if (ad->net.addr || !ad->common.u.net->sk)
  123. audit_unix_addr(ab, "addr",
  124. unix_addr(ad->net.addr),
  125. ad->net.addrlen);
  126. else
  127. audit_unix_sk_addr(ab, "addr", ad->common.u.net->sk);
  128. if (ad->request & NET_PEER_MASK) {
  129. audit_unix_addr(ab, "peer_addr",
  130. unix_addr(ad->net.peer.addr),
  131. ad->net.peer.addrlen);
  132. }
  133. }
  134. if (ad->peer) {
  135. audit_log_format(ab, " peer=");
  136. aa_label_xaudit(ab, labels_ns(ad->subj_label), ad->peer,
  137. FLAGS_NONE, GFP_ATOMIC);
  138. }
  139. }
  140. /* standard permission lookup pattern - supports early bailout */
  141. int aa_do_perms(struct aa_profile *profile, struct aa_policydb *policy,
  142. aa_state_t state, u32 request,
  143. struct aa_perms *p, struct apparmor_audit_data *ad)
  144. {
  145. struct aa_perms perms;
  146. AA_BUG(!profile);
  147. AA_BUG(!policy);
  148. if (state || !p)
  149. p = aa_lookup_perms(policy, state);
  150. perms = *p;
  151. aa_apply_modes_to_perms(profile, &perms);
  152. return aa_check_perms(profile, &perms, request, ad,
  153. audit_net_cb);
  154. }
  155. /* only continue match if
  156. * insufficient current perms at current state
  157. * indicates there are more perms in later state
  158. * Returns: perms struct if early match
  159. */
  160. static struct aa_perms *early_match(struct aa_policydb *policy,
  161. aa_state_t state, u32 request)
  162. {
  163. struct aa_perms *p;
  164. p = aa_lookup_perms(policy, state);
  165. if (((p->allow & request) != request) && (p->allow & AA_CONT_MATCH))
  166. return NULL;
  167. return p;
  168. }
  169. static aa_state_t aa_dfa_match_be16(struct aa_dfa *dfa, aa_state_t state,
  170. u16 data)
  171. {
  172. __be16 buffer = cpu_to_be16(data);
  173. return aa_dfa_match_len(dfa, state, (char *) &buffer, 2);
  174. }
  175. /**
  176. * aa_match_to_prot - match the af, type, protocol triplet
  177. * @policy: policy being matched
  178. * @state: state to start in
  179. * @request: permissions being requested, ignored if @p == NULL
  180. * @af: socket address family
  181. * @type: socket type
  182. * @protocol: socket protocol
  183. * @p: output - pointer to permission associated with match
  184. * @info: output - pointer to string describing failure
  185. *
  186. * RETURNS: state match stopped in.
  187. *
  188. * If @(p) is assigned a value the returned state will be the
  189. * corresponding state. Will not set @p on failure or if match completes
  190. * only if an early match occurs
  191. */
  192. aa_state_t aa_match_to_prot(struct aa_policydb *policy, aa_state_t state,
  193. u32 request, u16 af, int type, int protocol,
  194. struct aa_perms **p, const char **info)
  195. {
  196. state = aa_dfa_match_be16(policy->dfa, state, (u16)af);
  197. if (!state) {
  198. *info = "failed af match";
  199. return state;
  200. }
  201. state = aa_dfa_match_be16(policy->dfa, state, (u16)type);
  202. if (state) {
  203. if (p)
  204. *p = early_match(policy, state, request);
  205. if (!p || !*p) {
  206. state = aa_dfa_match_be16(policy->dfa, state, (u16)protocol);
  207. if (!state)
  208. *info = "failed protocol match";
  209. }
  210. } else {
  211. *info = "failed type match";
  212. }
  213. return state;
  214. }
  215. /* Generic af perm */
  216. int aa_profile_af_perm(struct aa_profile *profile,
  217. struct apparmor_audit_data *ad, u32 request, u16 family,
  218. int type, int protocol)
  219. {
  220. struct aa_ruleset *rules = profile->label.rules[0];
  221. struct aa_perms *p = NULL;
  222. aa_state_t state;
  223. AA_BUG(family >= AF_MAX);
  224. AA_BUG(type < 0 || type >= SOCK_MAX);
  225. AA_BUG(profile_unconfined(profile));
  226. if (profile_unconfined(profile))
  227. return 0;
  228. state = RULE_MEDIATES_NET(rules);
  229. if (!state)
  230. return 0;
  231. state = aa_match_to_prot(rules->policy, state, request, family, type,
  232. protocol, &p, &ad->info);
  233. return aa_do_perms(profile, rules->policy, state, request, p, ad);
  234. }
  235. int aa_af_perm(const struct cred *subj_cred, struct aa_label *label,
  236. const char *op, u32 request, u16 family, int type, int protocol)
  237. {
  238. struct aa_profile *profile;
  239. DEFINE_AUDIT_NET(ad, op, subj_cred, NULL, family, type, protocol);
  240. return fn_for_each_confined(label, profile,
  241. aa_profile_af_perm(profile, &ad, request, family,
  242. type, protocol));
  243. }
  244. static int aa_label_sk_perm(const struct cred *subj_cred,
  245. struct aa_label *label,
  246. const char *op, u32 request,
  247. struct sock *sk)
  248. {
  249. struct aa_sk_ctx *ctx = aa_sock(sk);
  250. int error = 0;
  251. AA_BUG(!label);
  252. AA_BUG(!sk);
  253. if (rcu_access_pointer(ctx->label) != kernel_t && !unconfined(label)) {
  254. struct aa_profile *profile;
  255. DEFINE_AUDIT_SK(ad, op, subj_cred, sk);
  256. ad.subj_cred = subj_cred;
  257. error = fn_for_each_confined(label, profile,
  258. aa_profile_af_sk_perm(profile, &ad, request, sk));
  259. }
  260. return error;
  261. }
  262. int aa_sk_perm(const char *op, u32 request, struct sock *sk)
  263. {
  264. struct aa_label *label;
  265. int error;
  266. AA_BUG(!sk);
  267. AA_BUG(in_interrupt());
  268. /* TODO: switch to begin_current_label ???? */
  269. label = begin_current_label_crit_section();
  270. error = aa_label_sk_perm(current_cred(), label, op, request, sk);
  271. end_current_label_crit_section(label);
  272. return error;
  273. }
  274. int aa_sock_file_perm(const struct cred *subj_cred, struct aa_label *label,
  275. const char *op, u32 request, struct file *file)
  276. {
  277. struct socket *sock = (struct socket *) file->private_data;
  278. AA_BUG(!label);
  279. /* sock && sock->sk can be NULL for sockets being set up or torn down */
  280. if (!sock || !sock->sk)
  281. return 0;
  282. if (sock->sk->sk_family == PF_UNIX)
  283. return aa_unix_file_perm(subj_cred, label, op, request, file);
  284. return aa_label_sk_perm(subj_cred, label, op, request, sock->sk);
  285. }
  286. #ifdef CONFIG_NETWORK_SECMARK
  287. static int apparmor_secmark_init(struct aa_secmark *secmark)
  288. {
  289. struct aa_label *label;
  290. if (secmark->label[0] == '*') {
  291. secmark->secid = AA_SECID_WILDCARD;
  292. return 0;
  293. }
  294. label = aa_label_strn_parse(&root_ns->unconfined->label,
  295. secmark->label, strlen(secmark->label),
  296. GFP_ATOMIC, false, false);
  297. if (IS_ERR(label))
  298. return PTR_ERR(label);
  299. secmark->secid = label->secid;
  300. return 0;
  301. }
  302. static int aa_secmark_perm(struct aa_profile *profile, u32 request, u32 secid,
  303. struct apparmor_audit_data *ad)
  304. {
  305. int i, ret;
  306. struct aa_perms perms = { };
  307. struct aa_ruleset *rules = profile->label.rules[0];
  308. if (rules->secmark_count == 0)
  309. return 0;
  310. for (i = 0; i < rules->secmark_count; i++) {
  311. if (!rules->secmark[i].secid) {
  312. ret = apparmor_secmark_init(&rules->secmark[i]);
  313. if (ret)
  314. return ret;
  315. }
  316. if (rules->secmark[i].secid == secid ||
  317. rules->secmark[i].secid == AA_SECID_WILDCARD) {
  318. if (rules->secmark[i].deny)
  319. perms.deny = ALL_PERMS_MASK;
  320. else
  321. perms.allow = ALL_PERMS_MASK;
  322. if (rules->secmark[i].audit)
  323. perms.audit = ALL_PERMS_MASK;
  324. }
  325. }
  326. aa_apply_modes_to_perms(profile, &perms);
  327. return aa_check_perms(profile, &perms, request, ad, audit_net_cb);
  328. }
  329. int apparmor_secmark_check(struct aa_label *label, char *op, u32 request,
  330. u32 secid, const struct sock *sk)
  331. {
  332. struct aa_profile *profile;
  333. DEFINE_AUDIT_SK(ad, op, NULL, sk);
  334. return fn_for_each_confined(label, profile,
  335. aa_secmark_perm(profile, request, secid,
  336. &ad));
  337. }
  338. #endif