Skip to content

Commit b2d09ae

Browse files
committed
apparmor: move ptrace checks to using labels
Signed-off-by: John Johansen <[email protected]>
1 parent ca916e8 commit b2d09ae

File tree

5 files changed

+58
-80
lines changed

5 files changed

+58
-80
lines changed

security/apparmor/domain.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@ void aa_free_domain_entries(struct aa_domain *domain)
5151

5252
/**
5353
* may_change_ptraced_domain - check if can change profile on ptraced task
54-
* @to_profile: profile to change to (NOT NULL)
54+
* @to_label: profile to change to (NOT NULL)
55+
* @info: message if there is an error
5556
*
5657
* Check if current is ptraced and if so if the tracing task is allowed
5758
* to trace the new domain
5859
*
5960
* Returns: %0 or error if change not allowed
6061
*/
61-
static int may_change_ptraced_domain(struct aa_profile *to_profile)
62+
static int may_change_ptraced_domain(struct aa_label *to_label,
63+
const char **info)
6264
{
6365
struct task_struct *tracer;
6466
struct aa_label *tracerl = NULL;
@@ -74,13 +76,14 @@ static int may_change_ptraced_domain(struct aa_profile *to_profile)
7476
if (!tracer || unconfined(tracerl))
7577
goto out;
7678

77-
error = aa_may_ptrace(labels_profile(tracerl), to_profile,
78-
PTRACE_MODE_ATTACH);
79+
error = aa_may_ptrace(tracerl, to_label, PTRACE_MODE_ATTACH);
7980

8081
out:
8182
rcu_read_unlock();
8283
aa_put_label(tracerl);
8384

85+
if (error)
86+
*info = "ptrace prevents transition";
8487
return error;
8588
}
8689

@@ -477,7 +480,7 @@ int apparmor_bprm_set_creds(struct linux_binprm *bprm)
477480
}
478481

479482
if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
480-
error = may_change_ptraced_domain(new_profile);
483+
error = may_change_ptraced_domain(&new_profile->label, &info);
481484
if (error)
482485
goto audit;
483486
}
@@ -661,7 +664,7 @@ int aa_change_hat(const char *hats[], int count, u64 token, int flags)
661664
}
662665
}
663666

664-
error = may_change_ptraced_domain(hat);
667+
error = may_change_ptraced_domain(&hat->label, &info);
665668
if (error) {
666669
info = "ptraced";
667670
error = -EPERM;
@@ -782,7 +785,7 @@ int aa_change_profile(const char *fqname, int flags)
782785
}
783786

784787
/* check if tracing task is allowed to trace target domain */
785-
error = may_change_ptraced_domain(target);
788+
error = may_change_ptraced_domain(&target->label, &info);
786789
if (error) {
787790
info = "ptrace prevents transition";
788791
goto audit;

security/apparmor/include/apparmor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define AA_CLASS_NET 4
2828
#define AA_CLASS_RLIMITS 5
2929
#define AA_CLASS_DOMAIN 6
30+
#define AA_CLASS_PTRACE 9
3031
#define AA_CLASS_LABEL 16
3132

3233
#define AA_CLASS_LAST AA_CLASS_LABEL

security/apparmor/include/ipc.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This file contains AppArmor ipc mediation function definitions.
55
*
66
* Copyright (C) 1998-2008 Novell/SUSE
7-
* Copyright 2009-2010 Canonical Ltd.
7+
* Copyright 2009-2017 Canonical Ltd.
88
*
99
* This program is free software; you can redistribute it and/or
1010
* modify it under the terms of the GNU General Public License as
@@ -19,10 +19,10 @@
1919

2020
struct aa_profile;
2121

22-
int aa_may_ptrace(struct aa_profile *tracer, struct aa_profile *tracee,
23-
unsigned int mode);
22+
#define AA_PTRACE_TRACE MAY_WRITE
23+
#define AA_PTRACE_READ MAY_READ
2424

25-
int aa_ptrace(struct task_struct *tracer, struct task_struct *tracee,
26-
unsigned int mode);
25+
int aa_may_ptrace(struct aa_label *tracer, struct aa_label *tracee,
26+
u32 request);
2727

2828
#endif /* __AA_IPC_H */

security/apparmor/ipc.c

Lines changed: 21 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This file contains AppArmor ipc mediation
55
*
66
* Copyright (C) 1998-2008 Novell/SUSE
7-
* Copyright 2009-2010 Canonical Ltd.
7+
* Copyright 2009-2017 Canonical Ltd.
88
*
99
* This program is free software; you can redistribute it and/or
1010
* modify it under the terms of the GNU General Public License as
@@ -25,88 +25,43 @@
2525
static void audit_ptrace_cb(struct audit_buffer *ab, void *va)
2626
{
2727
struct common_audit_data *sa = va;
28+
2829
audit_log_format(ab, " peer=");
2930
aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
3031
FLAGS_NONE, GFP_ATOMIC);
3132
}
3233

33-
/**
34-
* aa_audit_ptrace - do auditing for ptrace
35-
* @profile: profile being enforced (NOT NULL)
36-
* @target: profile being traced (NOT NULL)
37-
* @error: error condition
38-
*
39-
* Returns: %0 or error code
40-
*/
41-
static int aa_audit_ptrace(struct aa_profile *profile,
42-
struct aa_profile *target, int error)
34+
static int cross_ptrace_perm(struct aa_profile *tracer,
35+
struct aa_profile *tracee, u32 request,
36+
struct common_audit_data *sa)
4337
{
44-
DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_PTRACE);
38+
/* policy uses the old style capability check for ptrace */
39+
if (profile_unconfined(tracer) || tracer == tracee)
40+
return 0;
4541

46-
aad(&sa)->peer = &target->label;
47-
aad(&sa)->error = error;
42+
aad(sa)->label = &tracer->label;
43+
aad(sa)->peer = &tracee->label;
44+
aad(sa)->request = 0;
45+
aad(sa)->error = aa_capable(&tracer->label, CAP_SYS_PTRACE, 1);
4846

49-
return aa_audit(AUDIT_APPARMOR_AUTO, profile, &sa, audit_ptrace_cb);
47+
return aa_audit(AUDIT_APPARMOR_AUTO, tracer, sa, audit_ptrace_cb);
5048
}
5149

5250
/**
5351
* aa_may_ptrace - test if tracer task can trace the tracee
54-
* @tracer: profile of the task doing the tracing (NOT NULL)
55-
* @tracee: task to be traced
56-
* @mode: whether PTRACE_MODE_READ || PTRACE_MODE_ATTACH
52+
* @tracer: label of the task doing the tracing (NOT NULL)
53+
* @tracee: task label to be traced
54+
* @request: permission request
5755
*
5856
* Returns: %0 else error code if permission denied or error
5957
*/
60-
int aa_may_ptrace(struct aa_profile *tracer, struct aa_profile *tracee,
61-
unsigned int mode)
58+
int aa_may_ptrace(struct aa_label *tracer, struct aa_label *tracee,
59+
u32 request)
6260
{
63-
/* TODO: currently only based on capability, not extended ptrace
64-
* rules,
65-
* Test mode for PTRACE_MODE_READ || PTRACE_MODE_ATTACH
66-
*/
61+
DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_PTRACE);
6762

68-
if (profile_unconfined(tracer) || tracer == tracee)
69-
return 0;
70-
/* log this capability request */
71-
return aa_capable(&tracer->label, CAP_SYS_PTRACE, 1);
63+
return xcheck_labels_profiles(tracer, tracee, cross_ptrace_perm,
64+
request, &sa);
7265
}
7366

74-
/**
75-
* aa_ptrace - do ptrace permission check and auditing
76-
* @tracer: task doing the tracing (NOT NULL)
77-
* @tracee: task being traced (NOT NULL)
78-
* @mode: ptrace mode either PTRACE_MODE_READ || PTRACE_MODE_ATTACH
79-
*
80-
* Returns: %0 else error code if permission denied or error
81-
*/
82-
int aa_ptrace(struct task_struct *tracer, struct task_struct *tracee,
83-
unsigned int mode)
84-
{
85-
/*
86-
* tracer can ptrace tracee when
87-
* - tracer is unconfined ||
88-
* - tracer is in complain mode
89-
* - tracer has rules allowing it to trace tracee currently this is:
90-
* - confined by the same profile ||
91-
* - tracer profile has CAP_SYS_PTRACE
92-
*/
9367

94-
struct aa_label *tracer_l = aa_get_task_label(tracer);
95-
int error = 0;
96-
97-
if (!unconfined(tracer_l)) {
98-
struct aa_label *tracee_l = aa_get_task_label(tracee);
99-
100-
error = aa_may_ptrace(labels_profile(tracer_l),
101-
labels_profile(tracee_l),
102-
mode);
103-
error = aa_audit_ptrace(labels_profile(tracer_l),
104-
labels_profile(tracee_l),
105-
error);
106-
107-
aa_put_label(tracee_l);
108-
}
109-
aa_put_label(tracer_l);
110-
111-
return error;
112-
}

security/apparmor/lsm.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,31 @@ static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
104104
static int apparmor_ptrace_access_check(struct task_struct *child,
105105
unsigned int mode)
106106
{
107-
return aa_ptrace(current, child, mode);
107+
struct aa_label *tracer, *tracee;
108+
int error;
109+
110+
tracer = begin_current_label_crit_section();
111+
tracee = aa_get_task_label(child);
112+
error = aa_may_ptrace(tracer, tracee,
113+
mode == PTRACE_MODE_READ ? AA_PTRACE_READ : AA_PTRACE_TRACE);
114+
aa_put_label(tracee);
115+
end_current_label_crit_section(tracer);
116+
117+
return error;
108118
}
109119

110120
static int apparmor_ptrace_traceme(struct task_struct *parent)
111121
{
112-
return aa_ptrace(parent, current, PTRACE_MODE_ATTACH);
122+
struct aa_label *tracer, *tracee;
123+
int error;
124+
125+
tracee = begin_current_label_crit_section();
126+
tracer = aa_get_task_label(parent);
127+
error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE);
128+
aa_put_label(tracer);
129+
end_current_label_crit_section(tracee);
130+
131+
return error;
113132
}
114133

115134
/* Derived from security/commoncap.c:cap_capget */

0 commit comments

Comments
 (0)