Skip to content

Commit

Permalink
checkpolicy: add not-self neverallow support
Browse files Browse the repository at this point in the history
Add support for using negated or complemented self in the target type of
neverallow rules.

Some refpolicy examples:

    neverallow * ~self:{ capability cap_userns capability2 cap2_userns } *;
    # no violations

    neverallow domain domain:file ~{ append read_file_perms write };

    libsepol.report_failure: neverallow on line 565 of policy/modules/kernel/kernel.te (or line 30300 of policy.conf) violated by allow sysadm_t httpd_bugzilla_script_t:file { create setattr relabelfrom relabelto unlink link rename };
    libsepol.report_failure: neverallow on line 565 of policy/modules/kernel/kernel.te (or line 30300 of policy.conf) violated by allow chromium_t chromium_t:file { create };
    libsepol.report_failure: neverallow on line 564 of policy/modules/kernel/kernel.te (or line 30299 of policy.conf) violated by allow sysadm_t httpd_bugzilla_script_t:dir { create };

    neverallow domain { domain -self }:file ~{ append read_file_perms write };

    libsepol.report_failure: neverallow on line 565 of policy/modules/kernel/kernel.te (or line 30300 of policy.conf) violated by allow sysadm_t httpd_bugzilla_script_t:file { create setattr relabelfrom relabelto unlink link rename };
    libsepol.report_failure: neverallow on line 564 of policy/modules/kernel/kernel.te (or line 30299 of policy.conf) violated by allow sysadm_t httpd_bugzilla_script_t:dir { create };

Using negated self in a complement `~{ domain -self }` is not supported.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>

---
v3:
   - mention both neverallow rule types when using -self within an
     unsupported rule type
v2:
   - fix neverallowxperm usage

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
  • Loading branch information
cgzones committed Jun 15, 2022
1 parent 912c49a commit d95d89f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
46 changes: 40 additions & 6 deletions checkpolicy/policy_define.c
Original file line number Diff line number Diff line change
Expand Up @@ -2080,12 +2080,17 @@ static int define_te_avtab_xperms_helper(int which, avrule_t ** rule)
while ((id = queue_remove(id_queue))) {
if (strcmp(id, "self") == 0) {
free(id);
if (add == 0) {
yyerror("-self is not supported");
if (add == 0 && which != AVRULE_XPERMS_NEVERALLOW) {
yyerror("-self is only supported in neverallow and neverallowxperm rules");
ret = -1;
goto out;
}
avrule->flags |= (add ? RULE_SELF : RULE_NOTSELF);
if ((avrule->flags & RULE_SELF) && (avrule->flags & RULE_NOTSELF)) {
yyerror("self and -self is not supported");
ret = -1;
goto out;
}
avrule->flags |= RULE_SELF;
continue;
}
if (set_types
Expand All @@ -2096,6 +2101,18 @@ static int define_te_avtab_xperms_helper(int which, avrule_t ** rule)
}
}

if ((avrule->ttypes.flags & TYPE_COMP)) {
if (avrule->flags & RULE_NOTSELF) {
yyerror("-self is not supported in complements");
ret = -1;
goto out;
}
if (avrule->flags & RULE_SELF) {
avrule->flags &= ~RULE_SELF;
avrule->flags |= RULE_NOTSELF;
}
}

ebitmap_init(&tclasses);
ret = read_classes(&tclasses);
if (ret)
Expand Down Expand Up @@ -2541,12 +2558,17 @@ static int define_te_avtab_helper(int which, avrule_t ** rule)
while ((id = queue_remove(id_queue))) {
if (strcmp(id, "self") == 0) {
free(id);
if (add == 0) {
yyerror("-self is not supported");
if (add == 0 && which != AVRULE_NEVERALLOW) {
yyerror("-self is only supported in neverallow and neverallowxperm rules");
ret = -1;
goto out;
}
avrule->flags |= (add ? RULE_SELF : RULE_NOTSELF);
if ((avrule->flags & RULE_SELF) && (avrule->flags & RULE_NOTSELF)) {
yyerror("self and -self is not supported");
ret = -1;
goto out;
}
avrule->flags |= RULE_SELF;
continue;
}
if (set_types
Expand All @@ -2557,6 +2579,18 @@ static int define_te_avtab_helper(int which, avrule_t ** rule)
}
}

if ((avrule->ttypes.flags & TYPE_COMP)) {
if (avrule->flags & RULE_NOTSELF) {
yyerror("-self is not supported in complements");
ret = -1;
goto out;
}
if (avrule->flags & RULE_SELF) {
avrule->flags &= ~RULE_SELF;
avrule->flags |= RULE_NOTSELF;
}
}

ebitmap_init(&tclasses);
ret = read_classes(&tclasses);
if (ret)
Expand Down
6 changes: 5 additions & 1 deletion checkpolicy/test/dismod.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static int display_type_set(type_set_t * set, uint32_t flags, policydb_t * polic
}

num_types = 0;
if (flags & RULE_SELF) {
if (flags & (RULE_SELF | RULE_NOTSELF)) {
num_types++;
}

Expand Down Expand Up @@ -169,6 +169,10 @@ static int display_type_set(type_set_t * set, uint32_t flags, policydb_t * polic
fprintf(fp, " self");
}

if (flags & RULE_NOTSELF) {
fprintf(fp, " -self");
}

if (num_types > 1)
fprintf(fp, " }");

Expand Down

0 comments on commit d95d89f

Please sign in to comment.