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 14 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions security/selinux/include/limits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Limits for various policy database elements.
*/

/*
* Maximum supported depth of conditional expressions.
*/
#define COND_EXPR_MAXDEPTH 10

/*
* Maximum supported depth for constraint expressions.
*/
#define CEXPR_MAXDEPTH 5

/*
* Maximum supported identifier value.
*
* Reasoning: The most used symbols are types and they need to fit into
* an u16 for the avtab entries. Keep U16_MAX as special value
* and U16_MAX-1 to avoid accidental overflows into U16_MAX.
*/
#define IDENTIFIER_MAXVALUE (U16_MAX - 2)

/*
* Maximum supported length of security context strings.
*
* Reasoning: The string must fir into a PAGE_SIZE.
*/
#define CONTEXT_MAXLENGTH 4000

/*
* Maximum supported boolean name length.
*/
#define BOOLEAN_NAME_MAXLENGTH 64

/*
* Maximum supported security class and common class name length.
*/
#define CLASS_NAME_MAXLENGTH 64

/*
* Maximum supported permission name length.
*/
#define PERMISSION_NAME_MAXLENGTH 64

/*
* Maximum supported user name length.
*/
#define USER_NAME_MAXLENGTH 64

/*
* Maximum supported role name length.
*/
#define ROLE_NAME_MAXLENGTH 64

/*
* Maximum supported type name length.
*/
#define TYPE_NAME_MAXLENGTH 1024

/*
* Maximum supported sensitivity name length.
*/
#define SENSITIVITY_NAME_MAXLENGTH 32

/*
* Maximum supported category name length.
*/
#define CATEGORY_NAME_MAXLENGTH 16

/*
* Maximum supported path name length for keys in filename transitions.
*/
#define FILETRANSKEY_NAME_MAXLENGTH 1024

/*
* Maximum supported filesystem name length.
*/
#define FILESYSTEM_NAME_MAXLENGTH 128

/*
* Maximum supported path prefix length for genfs statements.
*/
#define GENFS_PATH_MAXLENGTH 1024

/*
* Maximum supported Infiniband device name length.
*/
#define INFINIBAND_DEVNAME_MAXLENGTH 256
1 change: 1 addition & 0 deletions security/selinux/include/security.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,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