Skip to content

RFE: policydb read validation #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: dev
Choose a base branch
from
1 change: 1 addition & 0 deletions security/selinux/include/security.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ int security_read_policy(void **data, size_t *len);
int security_read_state_kernel(void **data, size_t *len);
int security_policycap_supported(unsigned int req_cap);

/* Maximum supported number of permissions per class */
#define SEL_VEC_MAX 32
struct av_decision {
u32 allowed;
Expand Down
49 changes: 42 additions & 7 deletions security/selinux/ss/avtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ int avtab_read_item(struct avtab *a, struct policy_file *fp, struct policydb *po
struct avtab_extended_perms xperms;
__le32 buf32[ARRAY_SIZE(xperms.perms.p)];
int rc;
unsigned int set, vers = pol->policyvers;
unsigned int vers = pol->policyvers;

memset(&key, 0, sizeof(struct avtab_key));
memset(&datum, 0, sizeof(struct avtab_datum));
Expand All @@ -360,9 +360,12 @@ int avtab_read_item(struct avtab *a, struct policy_file *fp, struct policydb *po
pr_err("SELinux: avtab: truncated entry\n");
return rc;
}
/* Read five or more items: source type, target type,
* target class, AV type, and at least one datum.
*/
items2 = le32_to_cpu(buf32[0]);
if (items2 > ARRAY_SIZE(buf32)) {
pr_err("SELinux: avtab: entry overflow\n");
if (items2 < 5 || items2 > ARRAY_SIZE(buf32)) {
pr_err("SELinux: avtab: invalid item count\n");
return -EINVAL;
}
rc = next_entry(buf32, fp, sizeof(u32) * items2);
Expand Down Expand Up @@ -391,6 +394,13 @@ int avtab_read_item(struct avtab *a, struct policy_file *fp, struct policydb *po
return -EINVAL;
}

if (!policydb_type_isvalid(pol, key.source_type) ||
!policydb_type_isvalid(pol, key.target_type) ||
!policydb_class_isvalid(pol, key.target_class)) {
pr_err("SELinux: avtab: invalid type or class\n");
return -EINVAL;
}

val = le32_to_cpu(buf32[items++]);
enabled = (val & AVTAB_ENABLED_OLD) ? AVTAB_ENABLED : 0;

Expand All @@ -409,8 +419,20 @@ int avtab_read_item(struct avtab *a, struct policy_file *fp, struct policydb *po

for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
if (val & spec_order[i]) {
if (items >= items2) {
pr_err("SELinux: avtab: entry has too many items (%d/%d)\n",
items + 1, items2);
return -EINVAL;
}
key.specified = spec_order[i] | enabled;
datum.u.data = le32_to_cpu(buf32[items++]);

if ((key.specified & AVTAB_TYPE) &&
!policydb_simpletype_isvalid(pol, datum.u.data)) {
pr_err("SELinux: avtab: invalid type\n");
return -EINVAL;
}

rc = insertf(a, &key, &datum, p);
if (rc)
return rc;
Expand Down Expand Up @@ -444,9 +466,13 @@ int avtab_read_item(struct avtab *a, struct policy_file *fp, struct policydb *po
return -EINVAL;
}

set = hweight16(key.specified & (AVTAB_XPERMS | AVTAB_TYPE | AVTAB_AV));
if (!set || set > 1) {
pr_err("SELinux: avtab: more than one specifier\n");
if (hweight16(key.specified & ~AVTAB_ENABLED) != 1) {
pr_err("SELinux: avtab: not exactly one specifier\n");
return -EINVAL;
}

if (key.specified & ~AVTAB_SPECIFIER_MASK) {
pr_err("SELinux: avtab: invalid specifier\n");
return -EINVAL;
}

Expand All @@ -471,6 +497,10 @@ int avtab_read_item(struct avtab *a, struct policy_file *fp, struct policydb *po
pr_err("SELinux: avtab: truncated entry\n");
return rc;
}
if (!avtab_is_valid_xperm_specified(xperms.specified))
pr_warn_once_policyload(pol,
"SELinux: avtab: unsupported xperm specifier %#x\n",
xperms.specified);
rc = next_entry(&xperms.driver, fp, sizeof(u8));
if (rc) {
pr_err("SELinux: avtab: truncated entry\n");
Expand All @@ -494,7 +524,7 @@ int avtab_read_item(struct avtab *a, struct policy_file *fp, struct policydb *po
datum.u.data = le32_to_cpu(*buf32);
}
if ((key.specified & AVTAB_TYPE) &&
!policydb_type_isvalid(pol, datum.u.data)) {
!policydb_simpletype_isvalid(pol, datum.u.data)) {
pr_err("SELinux: avtab: invalid type\n");
return -EINVAL;
}
Expand Down Expand Up @@ -525,6 +555,11 @@ int avtab_read(struct avtab *a, struct policy_file *fp, struct policydb *pol)
goto bad;
}

/* avtab_read_item() reads at least 96 bytes for any valid entry */
rc = size_check(3 * sizeof(u32), nel, fp);
if (rc)
goto bad;

rc = avtab_alloc(a, nel);
if (rc)
goto bad;
Expand Down
13 changes: 13 additions & 0 deletions security/selinux/ss/avtab.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct avtab_key {
AVTAB_XPERMS_DONTAUDIT)
#define AVTAB_ENABLED_OLD 0x80000000 /* reserved for used in cond_avtab */
#define AVTAB_ENABLED 0x8000 /* reserved for used in cond_avtab */
#define AVTAB_SPECIFIER_MASK (AVTAB_AV | AVTAB_TYPE | AVTAB_XPERMS | AVTAB_ENABLED)
u16 specified; /* what field is specified */
};

Expand All @@ -68,6 +69,18 @@ struct avtab_extended_perms {
struct extended_perms_data perms;
};

static inline bool avtab_is_valid_xperm_specified(u8 specified)
{
switch (specified) {
case AVTAB_XPERMS_IOCTLFUNCTION:
case AVTAB_XPERMS_IOCTLDRIVER:
case AVTAB_XPERMS_NLMSG:
return true;
default:
return false;
}
}

struct avtab_datum {
union {
u32 data; /* access vector or type value */
Expand Down
Loading