Skip to content

Commit 6601e13

Browse files
committed
apparmor: fix unprivileged local user can do privileged policy management
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>
1 parent 5df0c44 commit 6601e13

3 files changed

Lines changed: 43 additions & 9 deletions

File tree

security/apparmor/apparmorfs.c

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

419419
static ssize_t policy_update(u32 mask, const char __user *buf, size_t size,
420-
loff_t *pos, struct aa_ns *ns)
420+
loff_t *pos, struct aa_ns *ns,
421+
const struct cred *ocred)
421422
{
422423
struct aa_loaddata *data;
423424
struct aa_label *label;
@@ -428,7 +429,7 @@ static ssize_t policy_update(u32 mask, const char __user *buf, size_t size,
428429
/* high level check about policy management - fine grained in
429430
* below after unpack
430431
*/
431-
error = aa_may_manage_policy(current_cred(), label, ns, mask);
432+
error = aa_may_manage_policy(current_cred(), label, ns, ocred, mask);
432433
if (error)
433434
goto end_section;
434435

@@ -449,7 +450,8 @@ static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
449450
loff_t *pos)
450451
{
451452
struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
452-
int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns);
453+
int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns,
454+
f->f_cred);
453455

454456
aa_put_ns(ns);
455457

@@ -467,7 +469,7 @@ static ssize_t profile_replace(struct file *f, const char __user *buf,
467469
{
468470
struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
469471
int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY,
470-
buf, size, pos, ns);
472+
buf, size, pos, ns, f->f_cred);
471473
aa_put_ns(ns);
472474

473475
return error;
@@ -492,7 +494,7 @@ static ssize_t profile_remove(struct file *f, const char __user *buf,
492494
* below after unpack
493495
*/
494496
error = aa_may_manage_policy(current_cred(), label, ns,
495-
AA_MAY_REMOVE_POLICY);
497+
f->f_cred, AA_MAY_REMOVE_POLICY);
496498
if (error)
497499
goto out;
498500

@@ -1830,7 +1832,7 @@ static struct dentry *ns_mkdir_op(struct mnt_idmap *idmap, struct inode *dir,
18301832
int error;
18311833

18321834
label = begin_current_label_crit_section();
1833-
error = aa_may_manage_policy(current_cred(), label, NULL,
1835+
error = aa_may_manage_policy(current_cred(), label, NULL, NULL,
18341836
AA_MAY_LOAD_POLICY);
18351837
end_current_label_crit_section(label);
18361838
if (error)
@@ -1880,7 +1882,7 @@ static int ns_rmdir_op(struct inode *dir, struct dentry *dentry)
18801882
int error;
18811883

18821884
label = begin_current_label_crit_section();
1883-
error = aa_may_manage_policy(current_cred(), label, NULL,
1885+
error = aa_may_manage_policy(current_cred(), label, NULL, NULL,
18841886
AA_MAY_LOAD_POLICY);
18851887
end_current_label_crit_section(label);
18861888
if (error)

security/apparmor/include/policy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ bool aa_policy_admin_capable(const struct cred *subj_cred,
443443
struct aa_label *label, struct aa_ns *ns);
444444
int aa_may_manage_policy(const struct cred *subj_cred,
445445
struct aa_label *label, struct aa_ns *ns,
446-
u32 mask);
446+
const struct cred *ocred, u32 mask);
447447
bool aa_current_policy_view_capable(struct aa_ns *ns);
448448
bool aa_current_policy_admin_capable(struct aa_ns *ns);
449449

security/apparmor/policy.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,17 +942,44 @@ bool aa_current_policy_admin_capable(struct aa_ns *ns)
942942
return res;
943943
}
944944

945+
static bool is_subset_of_obj_privilege(const struct cred *cred,
946+
struct aa_label *label,
947+
const struct cred *ocred)
948+
{
949+
if (cred == ocred)
950+
return true;
951+
952+
if (!aa_label_is_subset(label, cred_label(ocred)))
953+
return false;
954+
/* don't allow crossing userns for now */
955+
if (cred->user_ns != ocred->user_ns)
956+
return false;
957+
if (!cap_issubset(cred->cap_inheritable, ocred->cap_inheritable))
958+
return false;
959+
if (!cap_issubset(cred->cap_permitted, ocred->cap_permitted))
960+
return false;
961+
if (!cap_issubset(cred->cap_effective, ocred->cap_effective))
962+
return false;
963+
if (!cap_issubset(cred->cap_bset, ocred->cap_bset))
964+
return false;
965+
if (!cap_issubset(cred->cap_ambient, ocred->cap_ambient))
966+
return false;
967+
return true;
968+
}
969+
970+
945971
/**
946972
* aa_may_manage_policy - can the current task manage policy
947973
* @subj_cred: subjects cred
948974
* @label: label to check if it can manage policy
949975
* @ns: namespace being managed by @label (may be NULL if @label's ns)
976+
* @ocred: object cred if request is coming from an open object
950977
* @mask: contains the policy manipulation operation being done
951978
*
952979
* Returns: 0 if the task is allowed to manipulate policy else error
953980
*/
954981
int aa_may_manage_policy(const struct cred *subj_cred, struct aa_label *label,
955-
struct aa_ns *ns, u32 mask)
982+
struct aa_ns *ns, const struct cred *ocred, u32 mask)
956983
{
957984
const char *op;
958985

@@ -968,6 +995,11 @@ int aa_may_manage_policy(const struct cred *subj_cred, struct aa_label *label,
968995
return audit_policy(label, op, NULL, NULL, "policy_locked",
969996
-EACCES);
970997

998+
if (ocred && !is_subset_of_obj_privilege(subj_cred, label, ocred))
999+
return audit_policy(label, op, NULL, NULL,
1000+
"not privileged for target profile",
1001+
-EACCES);
1002+
9711003
if (!aa_policy_admin_capable(subj_cred, label, ns))
9721004
return audit_policy(label, op, NULL, NULL, "not policy admin",
9731005
-EACCES);

0 commit comments

Comments
 (0)