Skip to content
/ linux Public

Commit 0fc63dd

Browse files
jrjohansengregkh
authored andcommitted
apparmor: fix unprivileged local user can do privileged policy management
commit 6601e13 upstream. An unprivileged local user can load, replace, and remove profiles by opening the apparmorfs interfaces, via a confused deputy attack, by passing the opened fd to a privileged process, and getting the privileged process to write to the interface. This does require a privileged target that can be manipulated to do the write for the unprivileged process, but once such access is achieved full policy management is possible and all the possible implications that implies: removing confinement, DoS of system or target applications by denying all execution, by-passing the unprivileged user namespace restriction, to exploiting kernel bugs for a local privilege escalation. The policy management interface can not have its permissions simply changed from 0666 to 0600 because non-root processes need to be able to load policy to different policy namespaces. Instead ensure the task writing the interface has privileges that are a subset of the task that opened the interface. This is already done via policy for confined processes, but unconfined can delegate access to the opened fd, by-passing the usual policy check. Fixes: b7fd2c0 ("apparmor: add per policy ns .load, .replace, .remove interface files") Reported-by: Qualys Security Advisory <qsa@qualys.com> Tested-by: Salvatore Bonaccorso <carnil@debian.org> Reviewed-by: Georgia Garcia <georgia.garcia@canonical.com> Reviewed-by: Cengiz Can <cengiz.can@canonical.com> Signed-off-by: John Johansen <john.johansen@canonical.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 86feecc commit 0fc63dd

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

security/apparmor/apparmorfs.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf,
412412
}
413413

414414
static ssize_t policy_update(u32 mask, const char __user *buf, size_t size,
415-
loff_t *pos, struct aa_ns *ns)
415+
loff_t *pos, struct aa_ns *ns,
416+
const struct cred *ocred)
416417
{
417418
struct aa_loaddata *data;
418419
struct aa_label *label;
@@ -423,7 +424,7 @@ static ssize_t policy_update(u32 mask, const char __user *buf, size_t size,
423424
/* high level check about policy management - fine grained in
424425
* below after unpack
425426
*/
426-
error = aa_may_manage_policy(current_cred(), label, ns, mask);
427+
error = aa_may_manage_policy(current_cred(), label, ns, ocred, mask);
427428
if (error)
428429
goto end_section;
429430

@@ -444,7 +445,8 @@ static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
444445
loff_t *pos)
445446
{
446447
struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
447-
int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns);
448+
int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns,
449+
f->f_cred);
448450

449451
aa_put_ns(ns);
450452

@@ -462,7 +464,7 @@ static ssize_t profile_replace(struct file *f, const char __user *buf,
462464
{
463465
struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
464466
int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY,
465-
buf, size, pos, ns);
467+
buf, size, pos, ns, f->f_cred);
466468
aa_put_ns(ns);
467469

468470
return error;
@@ -487,7 +489,7 @@ static ssize_t profile_remove(struct file *f, const char __user *buf,
487489
* below after unpack
488490
*/
489491
error = aa_may_manage_policy(current_cred(), label, ns,
490-
AA_MAY_REMOVE_POLICY);
492+
f->f_cred, AA_MAY_REMOVE_POLICY);
491493
if (error)
492494
goto out;
493495

@@ -1813,7 +1815,7 @@ static int ns_mkdir_op(struct mnt_idmap *idmap, struct inode *dir,
18131815
int error;
18141816

18151817
label = begin_current_label_crit_section();
1816-
error = aa_may_manage_policy(current_cred(), label, NULL,
1818+
error = aa_may_manage_policy(current_cred(), label, NULL, NULL,
18171819
AA_MAY_LOAD_POLICY);
18181820
end_current_label_crit_section(label);
18191821
if (error)
@@ -1863,7 +1865,7 @@ static int ns_rmdir_op(struct inode *dir, struct dentry *dentry)
18631865
int error;
18641866

18651867
label = begin_current_label_crit_section();
1866-
error = aa_may_manage_policy(current_cred(), label, NULL,
1868+
error = aa_may_manage_policy(current_cred(), label, NULL, NULL,
18671869
AA_MAY_LOAD_POLICY);
18681870
end_current_label_crit_section(label);
18691871
if (error)

security/apparmor/include/policy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ bool aa_policy_admin_capable(const struct cred *subj_cred,
393393
struct aa_label *label, struct aa_ns *ns);
394394
int aa_may_manage_policy(const struct cred *subj_cred,
395395
struct aa_label *label, struct aa_ns *ns,
396-
u32 mask);
396+
const struct cred *ocred, u32 mask);
397397
bool aa_current_policy_view_capable(struct aa_ns *ns);
398398
bool aa_current_policy_admin_capable(struct aa_ns *ns);
399399

security/apparmor/policy.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,17 +894,44 @@ bool aa_current_policy_admin_capable(struct aa_ns *ns)
894894
return res;
895895
}
896896

897+
static bool is_subset_of_obj_privilege(const struct cred *cred,
898+
struct aa_label *label,
899+
const struct cred *ocred)
900+
{
901+
if (cred == ocred)
902+
return true;
903+
904+
if (!aa_label_is_subset(label, cred_label(ocred)))
905+
return false;
906+
/* don't allow crossing userns for now */
907+
if (cred->user_ns != ocred->user_ns)
908+
return false;
909+
if (!cap_issubset(cred->cap_inheritable, ocred->cap_inheritable))
910+
return false;
911+
if (!cap_issubset(cred->cap_permitted, ocred->cap_permitted))
912+
return false;
913+
if (!cap_issubset(cred->cap_effective, ocred->cap_effective))
914+
return false;
915+
if (!cap_issubset(cred->cap_bset, ocred->cap_bset))
916+
return false;
917+
if (!cap_issubset(cred->cap_ambient, ocred->cap_ambient))
918+
return false;
919+
return true;
920+
}
921+
922+
897923
/**
898924
* aa_may_manage_policy - can the current task manage policy
899925
* @subj_cred: subjects cred
900926
* @label: label to check if it can manage policy
901927
* @ns: namespace being managed by @label (may be NULL if @label's ns)
928+
* @ocred: object cred if request is coming from an open object
902929
* @mask: contains the policy manipulation operation being done
903930
*
904931
* Returns: 0 if the task is allowed to manipulate policy else error
905932
*/
906933
int aa_may_manage_policy(const struct cred *subj_cred, struct aa_label *label,
907-
struct aa_ns *ns, u32 mask)
934+
struct aa_ns *ns, const struct cred *ocred, u32 mask)
908935
{
909936
const char *op;
910937

@@ -920,6 +947,11 @@ int aa_may_manage_policy(const struct cred *subj_cred, struct aa_label *label,
920947
return audit_policy(label, op, NULL, NULL, "policy_locked",
921948
-EACCES);
922949

950+
if (ocred && !is_subset_of_obj_privilege(subj_cred, label, ocred))
951+
return audit_policy(label, op, NULL, NULL,
952+
"not privileged for target profile",
953+
-EACCES);
954+
923955
if (!aa_policy_admin_capable(subj_cred, label, ns))
924956
return audit_policy(label, op, NULL, NULL, "not policy admin",
925957
-EACCES);

0 commit comments

Comments
 (0)