Skip to content

Commit 0dda0b3

Browse files
committed
apparmor: fix ptrace label match when matching stacked labels
Given a label with a profile stack of A//&B or A//&C ... A ptrace rule should be able to specify a generic trace pattern with a rule like ptrace trace A//&**, however this is failing because while the correct label match routine is called, it is being done post label decomposition so it is always being done against a profile instead of the stacked label. To fix this refactor the cross check to pass the full peer label in to the label_match. Fixes: 290f458 ("apparmor: allow ptrace checks to be finer grained than just capability") Cc: Stable <stable@vger.kernel.org> Reported-by: Matthew Garrett <mjg59@google.com> Tested-by: Matthew Garrett <mjg59@google.com> Signed-off-by: John Johansen <john.johansen@canonical.com>
1 parent c92a9a4 commit 0dda0b3

2 files changed

Lines changed: 35 additions & 21 deletions

File tree

‎security/apparmor/include/perms.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ extern struct aa_perms allperms;
133133
#define xcheck_labels_profiles(L1, L2, FN, args...) \
134134
xcheck_ns_labels((L1), (L2), xcheck_ns_profile_label, (FN), args)
135135

136+
#define xcheck_labels(L1, L2, P, FN1, FN2) \
137+
xcheck(fn_for_each((L1), (P), (FN1)), fn_for_each((L2), (P), (FN2)))
138+
136139

137140
void aa_perm_mask_to_str(char *str, const char *chrs, u32 mask);
138141
void aa_audit_perm_names(struct audit_buffer *ab, const char **names, u32 mask);

‎security/apparmor/ipc.c‎

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,40 +64,48 @@ static void audit_ptrace_cb(struct audit_buffer *ab, void *va)
6464
FLAGS_NONE, GFP_ATOMIC);
6565
}
6666

67+
/* assumes check for PROFILE_MEDIATES is already done */
6768
/* TODO: conditionals */
6869
static int profile_ptrace_perm(struct aa_profile *profile,
69-
struct aa_profile *peer, u32 request,
70-
struct common_audit_data *sa)
70+
struct aa_label *peer, u32 request,
71+
struct common_audit_data *sa)
7172
{
7273
struct aa_perms perms = { };
7374

74-
/* need because of peer in cross check */
75-
if (profile_unconfined(profile) ||
76-
!PROFILE_MEDIATES(profile, AA_CLASS_PTRACE))
77-
return 0;
78-
79-
aad(sa)->peer = &peer->label;
80-
aa_profile_match_label(profile, &peer->label, AA_CLASS_PTRACE, request,
75+
aad(sa)->peer = peer;
76+
aa_profile_match_label(profile, peer, AA_CLASS_PTRACE, request,
8177
&perms);
8278
aa_apply_modes_to_perms(profile, &perms);
8379
return aa_check_perms(profile, &perms, request, sa, audit_ptrace_cb);
8480
}
8581

86-
static int cross_ptrace_perm(struct aa_profile *tracer,
87-
struct aa_profile *tracee, u32 request,
88-
struct common_audit_data *sa)
82+
static int profile_tracee_perm(struct aa_profile *tracee,
83+
struct aa_label *tracer, u32 request,
84+
struct common_audit_data *sa)
8985
{
86+
if (profile_unconfined(tracee) || unconfined(tracer) ||
87+
!PROFILE_MEDIATES(tracee, AA_CLASS_PTRACE))
88+
return 0;
89+
90+
return profile_ptrace_perm(tracee, tracer, request, sa);
91+
}
92+
93+
static int profile_tracer_perm(struct aa_profile *tracer,
94+
struct aa_label *tracee, u32 request,
95+
struct common_audit_data *sa)
96+
{
97+
if (profile_unconfined(tracer))
98+
return 0;
99+
90100
if (PROFILE_MEDIATES(tracer, AA_CLASS_PTRACE))
91-
return xcheck(profile_ptrace_perm(tracer, tracee, request, sa),
92-
profile_ptrace_perm(tracee, tracer,
93-
request << PTRACE_PERM_SHIFT,
94-
sa));
95-
/* policy uses the old style capability check for ptrace */
96-
if (profile_unconfined(tracer) || tracer == tracee)
101+
return profile_ptrace_perm(tracer, tracee, request, sa);
102+
103+
/* profile uses the old style capability check for ptrace */
104+
if (&tracer->label == tracee)
97105
return 0;
98106

99107
aad(sa)->label = &tracer->label;
100-
aad(sa)->peer = &tracee->label;
108+
aad(sa)->peer = tracee;
101109
aad(sa)->request = 0;
102110
aad(sa)->error = aa_capable(&tracer->label, CAP_SYS_PTRACE, 1);
103111

@@ -115,10 +123,13 @@ static int cross_ptrace_perm(struct aa_profile *tracer,
115123
int aa_may_ptrace(struct aa_label *tracer, struct aa_label *tracee,
116124
u32 request)
117125
{
126+
struct aa_profile *profile;
127+
u32 xrequest = request << PTRACE_PERM_SHIFT;
118128
DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_PTRACE);
119129

120-
return xcheck_labels_profiles(tracer, tracee, cross_ptrace_perm,
121-
request, &sa);
130+
return xcheck_labels(tracer, tracee, profile,
131+
profile_tracer_perm(profile, tracee, request, &sa),
132+
profile_tracee_perm(profile, tracer, xrequest, &sa));
122133
}
123134

124135

0 commit comments

Comments
 (0)