Skip to content

Commit 01a92d5

Browse files
jrjohansentjaalton
authored andcommitted
UBUNTU: SAUCE: apparmor4.0.0 [67/99]: userns - make it so special unconfined profiles can mediate user namespaces
BugLink: https://bugs.launchpad.net/bugs/2028253 Currently unconfined profiles are entirely governed by the sysctl. However we want to allow for named unconfined profiles to treat user ns mediation like other profiles. Allow unconfined profiles to mediate user ns creation using the standard mediates() mechanisms. When these profiles choose not to unmediated user namespaces they behave like the system unconfined profile. That is the sysctl will determine whether unprivileged unconfined processes can create user namespaces. Other wise the profiles rules control the behavior. Signed-off-by: John Johansen <[email protected]> (cherry picked from https://gitlab.com/jjohansen/apparmor-kernel) Signed-off-by: Andrea Righi <[email protected]> Signed-off-by: Timo Aaltonen <[email protected]>
1 parent 229faa8 commit 01a92d5

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

security/apparmor/apparmorfs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2587,6 +2587,7 @@ static struct aa_sfs_entry aa_sfs_entry_domain[] = {
25872587

25882588
static struct aa_sfs_entry aa_sfs_entry_unconfined[] = {
25892589
AA_SFS_FILE_BOOLEAN("change_profile", 1),
2590+
AA_SFS_FILE_INTPTR("userns", aa_unprivileged_userns_restricted),
25902591
{ }
25912592
};
25922593

@@ -2609,7 +2610,7 @@ static struct aa_sfs_entry aa_sfs_entry_policy[] = {
26092610
AA_SFS_FILE_BOOLEAN("set_load", 1),
26102611
/* number of out of band transitions supported */
26112612
AA_SFS_FILE_U64("outofband", MAX_OOB_SUPPORTED),
2612-
AA_SFS_FILE_U64("permstable32_version", 1),
2613+
AA_SFS_FILE_U64("permstable32_version", 2),
26132614
AA_SFS_FILE_STRING("permstable32", PERMS32STR),
26142615
AA_SFS_DIR("unconfined_restrictions", aa_sfs_entry_unconfined),
26152616
{ }

security/apparmor/lsm.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,16 +1221,18 @@ static int apparmor_userns_create(const struct cred *cred)
12211221
struct aa_label *label;
12221222
struct aa_profile *profile;
12231223
int error = 0;
1224-
DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_TASK, AA_CLASS_NS,
1225-
OP_USERNS_CREATE);
1226-
1227-
ad.subj_cred = current_cred();
12281224

12291225
label = begin_current_label_crit_section();
1230-
if (aa_unprivileged_userns_restricted || !unconfined(label)) {
1226+
/* remove unprivileged_userns_restricted check when unconfined is updated */
1227+
if (aa_unprivileged_userns_restricted ||
1228+
LABEL_MEDIATES(label, AA_CLASS_NS)) {
1229+
DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_TASK, AA_CLASS_NS,
1230+
OP_USERNS_CREATE);
1231+
ad.subj_cred = current_cred();
1232+
12311233
error = fn_for_each(label, profile,
1232-
aa_profile_ns_perm(profile, &ad,
1233-
AA_USERNS_CREATE));
1234+
aa_profile_ns_perm(profile, &ad, AA_USERNS_CREATE));
1235+
end_current_label_crit_section(label);
12341236
}
12351237
end_current_label_crit_section(label);
12361238

security/apparmor/task.c

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -317,32 +317,40 @@ int aa_profile_ns_perm(struct aa_profile *profile,
317317
struct apparmor_audit_data *ad,
318318
u32 request)
319319
{
320+
struct aa_ruleset *rules = list_first_entry(&profile->rules,
321+
typeof(*rules), list);
320322
struct aa_perms perms = { };
323+
aa_state_t state;
321324

322325
ad->subj_label = &profile->label;
323326
ad->request = request;
324327

325-
if (profile_unconfined(profile)) {
326-
if (!aa_unprivileged_userns_restricted ||
327-
ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN))
328-
return 0;
329328

330-
ad->info = "User namespace creation restricted";
331-
/* don't just return: allow complain mode to override */
332-
} else {
333-
struct aa_ruleset *rules = list_first_entry(&profile->rules,
334-
typeof(*rules),
335-
list);
336-
aa_state_t state;
337-
338-
state = RULE_MEDIATES(rules, ad->class);
339-
if (!state && !aa_unprivileged_userns_restricted_force)
340-
/* TODO: add flag to complain about unmediated */
329+
/* TODO: rework unconfined profile/dfa to mediate user ns, then
330+
* we can drop the unconfined test
331+
*/
332+
state = RULE_MEDIATES(rules, ad->class);
333+
if (!state) {
334+
/* TODO: this gets replaced when the default unconfined
335+
* profile dfa gets updated to handle this
336+
*/
337+
if (profile_unconfined(profile) &&
338+
profile == profiles_ns(profile)->unconfined) {
339+
if (!aa_unprivileged_userns_restricted ||
340+
ns_capable_noaudit(current_user_ns(),
341+
CAP_SYS_ADMIN))
342+
return 0;
343+
ad->info = "User namespace creation restricted";
344+
/* unconfined unprivileged user */
345+
/* don't just return: allow complain mode to override */
346+
} else if (!aa_unprivileged_userns_restricted_force) {
341347
return 0;
342-
perms = *aa_lookup_perms(rules->policy, state);
343-
if (aa_unprivileged_userns_restricted_complain)
344-
perms.complain = ALL_PERMS_MASK;
348+
}
349+
/* continue to mediation */
345350
}
351+
perms = *aa_lookup_perms(rules->policy, state);
352+
if (aa_unprivileged_userns_restricted_complain)
353+
perms.complain = ALL_PERMS_MASK;
346354

347355
aa_apply_modes_to_perms(profile, &perms);
348356
return aa_check_perms(profile, &perms, request, ad, audit_ns_cb);

0 commit comments

Comments
 (0)