1 // SPDX-License-Identifier: GPL-2.0-only
3 * AppArmor security module
5 * This file contains AppArmor network mediation
7 * Copyright (C) 1998-2008 Novell/SUSE
8 * Copyright 2009-2017 Canonical Ltd.
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"
19 #include "net_names.h"
22 struct aa_sfs_entry aa_sfs_entry_network[] = {
23 AA_SFS_FILE_STRING("af_mask", AA_SFS_AF_MASK),
27 static const char * const net_mask_names[] = {
70 /* audit callback for net specific fields */
71 void audit_net_cb(struct audit_buffer *ab, void *va)
73 struct common_audit_data *sa = va;
75 if (address_family_names[sa->u.net->family])
76 audit_log_format(ab, " family=\"%s\"",
77 address_family_names[sa->u.net->family]);
79 audit_log_format(ab, " family=\"unknown(%d)\"",
81 if (sock_type_names[aad(sa)->net.type])
82 audit_log_format(ab, " sock_type=\"%s\"",
83 sock_type_names[aad(sa)->net.type]);
85 audit_log_format(ab, " sock_type=\"unknown(%d)\"",
87 audit_log_format(ab, " protocol=%d", aad(sa)->net.protocol);
89 if (aad(sa)->request & NET_PERMS_MASK) {
90 audit_log_format(ab, " requested_mask=");
91 aa_audit_perm_mask(ab, aad(sa)->request, NULL, 0,
92 net_mask_names, NET_PERMS_MASK);
94 if (aad(sa)->denied & NET_PERMS_MASK) {
95 audit_log_format(ab, " denied_mask=");
96 aa_audit_perm_mask(ab, aad(sa)->denied, NULL, 0,
97 net_mask_names, NET_PERMS_MASK);
101 audit_log_format(ab, " peer=");
102 aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
103 FLAGS_NONE, GFP_ATOMIC);
107 /* Generic af perm */
108 int aa_profile_af_perm(struct aa_profile *profile, struct common_audit_data *sa,
109 u32 request, u16 family, int type)
111 struct aa_ruleset *rules = list_first_entry(&profile->rules,
112 typeof(*rules), list);
113 struct aa_perms perms = { };
117 AA_BUG(family >= AF_MAX);
118 AA_BUG(type < 0 || type >= SOCK_MAX);
120 if (profile_unconfined(profile))
122 state = RULE_MEDIATES(rules, AA_CLASS_NET);
126 buffer[0] = cpu_to_be16(family);
127 buffer[1] = cpu_to_be16((u16) type);
128 state = aa_dfa_match_len(rules->policy.dfa, state, (char *) &buffer,
130 perms = *aa_lookup_perms(&rules->policy, state);
131 aa_apply_modes_to_perms(profile, &perms);
133 return aa_check_perms(profile, &perms, request, sa, audit_net_cb);
136 int aa_af_perm(struct aa_label *label, const char *op, u32 request, u16 family,
137 int type, int protocol)
139 struct aa_profile *profile;
140 DEFINE_AUDIT_NET(sa, op, NULL, family, type, protocol);
142 return fn_for_each_confined(label, profile,
143 aa_profile_af_perm(profile, &sa, request, family,
147 static int aa_label_sk_perm(struct aa_label *label, const char *op, u32 request,
150 struct aa_sk_ctx *ctx = SK_CTX(sk);
156 if (ctx->label != kernel_t && !unconfined(label)) {
157 struct aa_profile *profile;
158 DEFINE_AUDIT_SK(sa, op, sk);
160 error = fn_for_each_confined(label, profile,
161 aa_profile_af_sk_perm(profile, &sa, request, sk));
167 int aa_sk_perm(const char *op, u32 request, struct sock *sk)
169 struct aa_label *label;
173 AA_BUG(in_interrupt());
175 /* TODO: switch to begin_current_label ???? */
176 label = begin_current_label_crit_section();
177 error = aa_label_sk_perm(label, op, request, sk);
178 end_current_label_crit_section(label);
184 int aa_sock_file_perm(struct aa_label *label, const char *op, u32 request,
191 return aa_label_sk_perm(label, op, request, sock->sk);
194 #ifdef CONFIG_NETWORK_SECMARK
195 static int apparmor_secmark_init(struct aa_secmark *secmark)
197 struct aa_label *label;
199 if (secmark->label[0] == '*') {
200 secmark->secid = AA_SECID_WILDCARD;
204 label = aa_label_strn_parse(&root_ns->unconfined->label,
205 secmark->label, strlen(secmark->label),
206 GFP_ATOMIC, false, false);
209 return PTR_ERR(label);
211 secmark->secid = label->secid;
216 static int aa_secmark_perm(struct aa_profile *profile, u32 request, u32 secid,
217 struct common_audit_data *sa)
220 struct aa_perms perms = { };
221 struct aa_ruleset *rules = list_first_entry(&profile->rules,
222 typeof(*rules), list);
224 if (rules->secmark_count == 0)
227 for (i = 0; i < rules->secmark_count; i++) {
228 if (!rules->secmark[i].secid) {
229 ret = apparmor_secmark_init(&rules->secmark[i]);
234 if (rules->secmark[i].secid == secid ||
235 rules->secmark[i].secid == AA_SECID_WILDCARD) {
236 if (rules->secmark[i].deny)
237 perms.deny = ALL_PERMS_MASK;
239 perms.allow = ALL_PERMS_MASK;
241 if (rules->secmark[i].audit)
242 perms.audit = ALL_PERMS_MASK;
246 aa_apply_modes_to_perms(profile, &perms);
248 return aa_check_perms(profile, &perms, request, sa, audit_net_cb);
251 int apparmor_secmark_check(struct aa_label *label, char *op, u32 request,
252 u32 secid, const struct sock *sk)
254 struct aa_profile *profile;
255 DEFINE_AUDIT_SK(sa, op, sk);
257 return fn_for_each_confined(label, profile,
258 aa_secmark_perm(profile, request, secid,