Skip to content

Commit

Permalink
libsepol: Use new module binary format for avrule
Browse files Browse the repository at this point in the history
Implement new internal avrule structure in the module binary format.
This simpler format makes future improvements easier and also reduces
loading time by eliminating the need to convert between two different
representations.

This patch bumps the maximum module policy version and implements
reading/writing functions such that module binary policy structure
matches its internal representation, namely, object name attribute used
for filename transition rules.

Signed-off-by: Juraj Marcin <[email protected]>
  • Loading branch information
JurajMarcin committed Oct 12, 2022
1 parent be887c0 commit f0e8994
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
3 changes: 2 additions & 1 deletion libsepol/include/sepol/policydb/policydb.h
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,10 @@ extern int policydb_set_target_platform(policydb_t *p, int platform);
#define MOD_POLICYDB_VERSION_INFINIBAND 19
#define MOD_POLICYDB_VERSION_GLBLUB 20
#define MOD_POLICYDB_VERSION_SELF_TYPETRANS 21
#define MOD_POLICYDB_VERSION_AVRULE_FTRANS 22

#define MOD_POLICYDB_VERSION_MIN MOD_POLICYDB_VERSION_BASE
#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_SELF_TYPETRANS
#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_AVRULE_FTRANS

#define POLICYDB_CONFIG_MLS 1

Expand Down
28 changes: 28 additions & 0 deletions libsepol/src/policydb.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@ static const struct policydb_compat_info policydb_compat[] = {
.ocon_num = OCON_IBENDPORT + 1,
.target_platform = SEPOL_TARGET_SELINUX,
},
{
.type = POLICY_BASE,
.version = MOD_POLICYDB_VERSION_AVRULE_FTRANS,
.sym_num = SYM_NUM,
.ocon_num = OCON_IBENDPORT + 1,
.target_platform = SEPOL_TARGET_SELINUX,
},
{
.type = POLICY_MOD,
.version = MOD_POLICYDB_VERSION_BASE,
Expand Down Expand Up @@ -467,6 +474,13 @@ static const struct policydb_compat_info policydb_compat[] = {
.ocon_num = 0,
.target_platform = SEPOL_TARGET_SELINUX,
},
{
.type = POLICY_MOD,
.version = MOD_POLICYDB_VERSION_AVRULE_FTRANS,
.sym_num = SYM_NUM,
.ocon_num = 0,
.target_platform = SEPOL_TARGET_SELINUX,
},
};

#if 0
Expand Down Expand Up @@ -3203,6 +3217,19 @@ static avrule_t *avrule_read(policydb_t * p, struct policy_file *fp)
tail = cur;
}

if (p->policyvers >= MOD_POLICYDB_VERSION_AVRULE_FTRANS &&
avrule->specified & AVRULE_TRANSITION) {
rc = next_entry(buf, fp, sizeof(uint32_t));
if (rc < 0)
goto bad;
len = le32_to_cpu(*buf);
if (len) {
rc = str_read(&avrule->object_name, fp, len);
if (rc < 0)
goto bad;
}
}

if (avrule->specified & AVRULE_XPERMS) {
uint8_t buf8;
size_t nel = ARRAY_SIZE(avrule->xperms->perms);
Expand Down Expand Up @@ -3639,6 +3666,7 @@ static int avrule_decl_read(policydb_t * p, avrule_decl_t * decl,
}

if (p->policyvers >= MOD_POLICYDB_VERSION_FILENAME_TRANS &&
p->policyvers < MOD_POLICYDB_VERSION_AVRULE_FTRANS &&
filename_trans_rule_read(p, &decl->avrules, fp))
return -1;

Expand Down
24 changes: 21 additions & 3 deletions libsepol/src/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -2004,8 +2004,9 @@ static int avrule_write(policydb_t *p, avrule_t * avrule,
uint32_t buf[32], len;
class_perm_node_t *cur;

/* skip filename transitions for now */
if (avrule->specified & AVRULE_TRANSITION && avrule->object_name)
/* skip filename transitions if writing older version without name */
if (p->policyvers < MOD_POLICYDB_VERSION_AVRULE_FTRANS &&
avrule->specified & AVRULE_TRANSITION && avrule->object_name)
return POLICYDB_SUCCESS;

if (p->policyvers < MOD_POLICYDB_VERSION_SELF_TYPETRANS &&
Expand Down Expand Up @@ -2052,6 +2053,21 @@ static int avrule_write(policydb_t *p, avrule_t * avrule,
cur = cur->next;
}

if (p->policyvers >= MOD_POLICYDB_VERSION_AVRULE_FTRANS &&
avrule->specified & AVRULE_TRANSITION) {
len = avrule->object_name ? strlen(avrule->object_name) : 0;
*buf = cpu_to_le32(len);
items = put_entry(buf, sizeof(uint32_t), 1, fp);
if (items != 1)
return POLICYDB_ERROR;
if (avrule->object_name) {
items = put_entry(avrule->object_name, sizeof(char),
len, fp);
if (items != len)
return POLICYDB_ERROR;
}
}

if (avrule->specified & AVRULE_XPERMS) {
size_t nel = ARRAY_SIZE(avrule->xperms->perms);
uint32_t buf32[nel];
Expand Down Expand Up @@ -2101,7 +2117,8 @@ static int avrule_write_list(policydb_t *p, avrule_t * avrules,
avrule = avrules;
len = 0;
while (avrule) {
if (!(avrule->specified & AVRULE_TRANSITION &&
if (p->policyvers >= MOD_POLICYDB_VERSION_AVRULE_FTRANS ||
!(avrule->specified & AVRULE_TRANSITION &&
avrule->object_name))
len++;
avrule = avrule->next;
Expand Down Expand Up @@ -2336,6 +2353,7 @@ static int avrule_decl_write(avrule_decl_t * decl, int num_scope_syms,
}

if (p->policyvers >= MOD_POLICYDB_VERSION_FILENAME_TRANS &&
p->policyvers < MOD_POLICYDB_VERSION_AVRULE_FTRANS &&
filename_trans_rule_write(p, decl->avrules, fp))
return POLICYDB_ERROR;

Expand Down

0 comments on commit f0e8994

Please sign in to comment.