Skip to content

Commit aa9aeea

Browse files
committed
apparmor: add gerneric permissions struct and support fns
Signed-off-by: John Johansen <john.johansen@canonical.com>
1 parent b5b2557 commit aa9aeea

4 files changed

Lines changed: 153 additions & 17 deletions

File tree

‎security/apparmor/file.c‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ static void file_audit_cb(struct audit_buffer *ab, void *va)
5656
struct common_audit_data *sa = va;
5757
kuid_t fsuid = current_fsuid();
5858

59-
if (aad(sa)->fs.request & AA_AUDIT_FILE_MASK) {
59+
if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
6060
audit_log_format(ab, " requested_mask=");
61-
audit_file_mask(ab, aad(sa)->fs.request);
61+
audit_file_mask(ab, aad(sa)->request);
6262
}
63-
if (aad(sa)->fs.denied & AA_AUDIT_FILE_MASK) {
63+
if (aad(sa)->denied & AA_AUDIT_FILE_MASK) {
6464
audit_log_format(ab, " denied_mask=");
65-
audit_file_mask(ab, aad(sa)->fs.denied);
65+
audit_file_mask(ab, aad(sa)->denied);
6666
}
67-
if (aad(sa)->fs.request & AA_AUDIT_FILE_MASK) {
67+
if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
6868
audit_log_format(ab, " fsuid=%d",
6969
from_kuid(&init_user_ns, fsuid));
7070
audit_log_format(ab, " ouid=%d",
@@ -100,7 +100,7 @@ int aa_audit_file(struct aa_profile *profile, struct file_perms *perms,
100100
DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_TASK, op);
101101

102102
sa.u.tsk = NULL;
103-
aad(&sa)->fs.request = request;
103+
aad(&sa)->request = request;
104104
aad(&sa)->name = name;
105105
aad(&sa)->fs.target = target;
106106
aad(&sa)->fs.ouid = ouid;
@@ -115,30 +115,30 @@ int aa_audit_file(struct aa_profile *profile, struct file_perms *perms,
115115
mask = 0xffff;
116116

117117
/* mask off perms that are not being force audited */
118-
aad(&sa)->fs.request &= mask;
118+
aad(&sa)->request &= mask;
119119

120-
if (likely(!aad(&sa)->fs.request))
120+
if (likely(!aad(&sa)->request))
121121
return 0;
122122
type = AUDIT_APPARMOR_AUDIT;
123123
} else {
124124
/* only report permissions that were denied */
125-
aad(&sa)->fs.request = aad(&sa)->fs.request & ~perms->allow;
126-
AA_BUG(!aad(&sa)->fs.request);
125+
aad(&sa)->request = aad(&sa)->request & ~perms->allow;
126+
AA_BUG(!aad(&sa)->request);
127127

128-
if (aad(&sa)->fs.request & perms->kill)
128+
if (aad(&sa)->request & perms->kill)
129129
type = AUDIT_APPARMOR_KILL;
130130

131131
/* quiet known rejects, assumes quiet and kill do not overlap */
132-
if ((aad(&sa)->fs.request & perms->quiet) &&
132+
if ((aad(&sa)->request & perms->quiet) &&
133133
AUDIT_MODE(profile) != AUDIT_NOQUIET &&
134134
AUDIT_MODE(profile) != AUDIT_ALL)
135-
aad(&sa)->fs.request &= ~perms->quiet;
135+
aad(&sa)->request &= ~perms->quiet;
136136

137-
if (!aad(&sa)->fs.request)
137+
if (!aad(&sa)->request)
138138
return COMPLAIN_MODE(profile) ? 0 : aad(&sa)->error;
139139
}
140140

141-
aad(&sa)->fs.denied = aad(&sa)->fs.request & ~perms->allow;
141+
aad(&sa)->denied = aad(&sa)->request & ~perms->allow;
142142
return aa_audit(type, profile, &sa, file_audit_cb);
143143
}
144144

‎security/apparmor/include/audit.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ struct apparmor_audit_data {
107107
void *profile;
108108
const char *name;
109109
const char *info;
110+
u32 request;
111+
u32 denied;
110112
union {
111113
/* these entries require a custom callback fn */
112114
struct {
113115
struct aa_profile *peer;
114116
struct {
115117
const char *target;
116-
u32 request;
117-
u32 denied;
118118
kuid_t ouid;
119119
} fs;
120120
};

‎security/apparmor/include/perms.h‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,40 @@
6666
extern const char aa_file_perm_chrs[];
6767
extern const char *aa_file_perm_names[];
6868

69+
struct aa_perms {
70+
u32 allow;
71+
u32 audit; /* set only when allow is set */
72+
73+
u32 deny; /* explicit deny, or conflict if allow also set */
74+
u32 quiet; /* set only when ~allow | deny */
75+
u32 kill; /* set only when ~allow | deny */
76+
u32 stop; /* set only when ~allow | deny */
77+
78+
u32 complain; /* accumulates only used when ~allow & ~deny */
79+
u32 cond; /* set only when ~allow and ~deny */
80+
81+
u32 hide; /* set only when ~allow | deny */
82+
u32 prompt; /* accumulates only used when ~allow & ~deny */
83+
84+
/* Reserved:
85+
* u32 subtree; / * set only when allow is set * /
86+
*/
87+
u16 xindex;
88+
};
89+
90+
#define ALL_PERMS_MASK 0xffffffff
91+
92+
extern struct aa_perms allperms;
93+
94+
struct aa_profile;
95+
6996
void aa_perm_mask_to_str(char *str, const char *chrs, u32 mask);
97+
void aa_audit_perm_names(struct audit_buffer *ab, const char **names, u32 mask);
98+
void aa_audit_perm_mask(struct audit_buffer *ab, u32 mask, const char *chrs,
99+
u32 chrsmask, const char **names, u32 namesmask);
100+
void aa_apply_modes_to_perms(struct aa_profile *profile,
101+
struct aa_perms *perms);
102+
void aa_compute_perms(struct aa_dfa *dfa, unsigned int state,
103+
struct aa_perms *perms);
70104

71105
#endif /* __AA_PERM_H */

‎security/apparmor/lib.c‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#include "include/perms.h"
2525
#include "include/policy.h"
2626

27+
struct aa_perms allperms = { .allow = ALL_PERMS_MASK,
28+
.quiet = ALL_PERMS_MASK,
29+
.hide = ALL_PERMS_MASK };
30+
2731
/**
2832
* aa_split_fqname - split a fqname into a profile and namespace name
2933
* @fqname: a full qualified name in namespace profile format (NOT NULL)
@@ -188,6 +192,104 @@ void aa_perm_mask_to_str(char *str, const char *chrs, u32 mask)
188192
*str = '\0';
189193
}
190194

195+
void aa_audit_perm_names(struct audit_buffer *ab, const char **names, u32 mask)
196+
{
197+
const char *fmt = "%s";
198+
unsigned int i, perm = 1;
199+
bool prev = false;
200+
201+
for (i = 0; i < 32; perm <<= 1, i++) {
202+
if (mask & perm) {
203+
audit_log_format(ab, fmt, names[i]);
204+
if (!prev) {
205+
prev = true;
206+
fmt = " %s";
207+
}
208+
}
209+
}
210+
}
211+
212+
void aa_audit_perm_mask(struct audit_buffer *ab, u32 mask, const char *chrs,
213+
u32 chrsmask, const char **names, u32 namesmask)
214+
{
215+
char str[33];
216+
217+
audit_log_format(ab, "\"");
218+
if ((mask & chrsmask) && chrs) {
219+
aa_perm_mask_to_str(str, chrs, mask & chrsmask);
220+
mask &= ~chrsmask;
221+
audit_log_format(ab, "%s", str);
222+
if (mask & namesmask)
223+
audit_log_format(ab, " ");
224+
}
225+
if ((mask & namesmask) && names)
226+
aa_audit_perm_names(ab, names, mask & namesmask);
227+
audit_log_format(ab, "\"");
228+
}
229+
230+
/**
231+
* aa_apply_modes_to_perms - apply namespace and profile flags to perms
232+
* @profile: that perms where computed from
233+
* @perms: perms to apply mode modifiers to
234+
*
235+
* TODO: split into profile and ns based flags for when accumulating perms
236+
*/
237+
void aa_apply_modes_to_perms(struct aa_profile *profile, struct aa_perms *perms)
238+
{
239+
switch (AUDIT_MODE(profile)) {
240+
case AUDIT_ALL:
241+
perms->audit = ALL_PERMS_MASK;
242+
/* fall through */
243+
case AUDIT_NOQUIET:
244+
perms->quiet = 0;
245+
break;
246+
case AUDIT_QUIET:
247+
perms->audit = 0;
248+
/* fall through */
249+
case AUDIT_QUIET_DENIED:
250+
perms->quiet = ALL_PERMS_MASK;
251+
break;
252+
}
253+
254+
if (KILL_MODE(profile))
255+
perms->kill = ALL_PERMS_MASK;
256+
else if (COMPLAIN_MODE(profile))
257+
perms->complain = ALL_PERMS_MASK;
258+
/*
259+
* TODO:
260+
* else if (PROMPT_MODE(profile))
261+
* perms->prompt = ALL_PERMS_MASK;
262+
*/
263+
}
264+
265+
static u32 map_other(u32 x)
266+
{
267+
return ((x & 0x3) << 8) | /* SETATTR/GETATTR */
268+
((x & 0x1c) << 18) | /* ACCEPT/BIND/LISTEN */
269+
((x & 0x60) << 19); /* SETOPT/GETOPT */
270+
}
271+
272+
void aa_compute_perms(struct aa_dfa *dfa, unsigned int state,
273+
struct aa_perms *perms)
274+
{
275+
perms->deny = 0;
276+
perms->kill = perms->stop = 0;
277+
perms->complain = perms->cond = 0;
278+
perms->hide = 0;
279+
perms->prompt = 0;
280+
perms->allow = dfa_user_allow(dfa, state);
281+
perms->audit = dfa_user_audit(dfa, state);
282+
perms->quiet = dfa_user_quiet(dfa, state);
283+
284+
/* for v5 perm mapping in the policydb, the other set is used
285+
* to extend the general perm set
286+
*/
287+
perms->allow |= map_other(dfa_other_allow(dfa, state));
288+
perms->audit |= map_other(dfa_other_audit(dfa, state));
289+
perms->quiet |= map_other(dfa_other_quiet(dfa, state));
290+
// perms->xindex = dfa_user_xindex(dfa, state);
291+
}
292+
191293
/**
192294
* aa_policy_init - initialize a policy structure
193295
* @policy: policy to initialize (NOT NULL)

0 commit comments

Comments
 (0)