/** * remove_comment() - Truncate all chars following START_COMMENT in a string. * * @line: Supplies a policy line string for preprocessing.
*/ staticvoid remove_comment(char *line)
{
line = strchr(line, START_COMMENT);
if (line)
*line = '\0';
}
/** * remove_trailing_spaces() - Truncate all trailing spaces in a string. * * @line: Supplies a policy line string for preprocessing. * * Return: The length of truncated string.
*/ static size_t remove_trailing_spaces(char *line)
{
size_t i = 0;
i = strlen(line); while (i > 0 && isspace(line[i - 1]))
i--;
line[i] = '\0';
return i;
}
/** * parse_version() - Parse policy version. * @ver: Supplies a version string to be parsed. * @p: Supplies the partial parsed policy. * * Return: * * %0 - Success * * %-EBADMSG - Version string is invalid * * %-ERANGE - Version number overflow * * %-EINVAL - Parsing error
*/ staticint parse_version(char *ver, struct ipe_parsed_policy *p)
{
u16 *const cv[] = { &p->version.major, &p->version.minor, &p->version.rev };
size_t sep_count = 0; char *token; int rc = 0;
while ((token = strsep(&ver, ".")) != NULL) { /* prevent overflow */ if (sep_count >= ARRAY_SIZE(cv)) return -EBADMSG;
rc = kstrtou16(token, 10, cv[sep_count]); if (rc) return rc;
++sep_count;
}
/* prevent underflow */ if (sep_count != ARRAY_SIZE(cv)) return -EBADMSG;
switch (token) { case IPE_HEADER_POLICY_NAME:
p->name = match_strdup(&args[0]); if (!p->name)
rc = -ENOMEM; break; case IPE_HEADER_POLICY_VERSION:
ver = match_strdup(&args[0]); if (!ver) {
rc = -ENOMEM; break;
}
rc = parse_version(ver, p); break; default:
rc = -EBADMSG;
} if (rc) goto out;
++idx;
}
if (idx != __IPE_HEADER_MAX)
rc = -EBADMSG;
out:
kfree(ver); return rc;
}
/** * token_default() - Determine if the given token is "DEFAULT". * @token: Supplies the token string to be compared. * * Return: * * %false - The token is not "DEFAULT" * * %true - The token is "DEFAULT"
*/ staticbool token_default(char *token)
{ return !strcmp(token, "DEFAULT");
}
/** * free_rule() - Free the supplied ipe_rule struct. * @r: Supplies the ipe_rule struct to be freed. * * Free a ipe_rule struct @r. Note @r must be removed from any lists before * calling this function.
*/ staticvoid free_rule(struct ipe_rule *r)
{ struct ipe_prop *p, *t;
/** * parse_operation() - Parse the operation type given a token string. * @t: Supplies the token string to be parsed. * * Return: The parsed operation type.
*/ staticenum ipe_op_type parse_operation(char *t)
{
substring_t args[MAX_OPT_ARGS];
/** * parse_action() - Parse the action type given a token string. * @t: Supplies the token string to be parsed. * * Return: The parsed action type.
*/ staticenum ipe_action_type parse_action(char *t)
{
substring_t args[MAX_OPT_ARGS];
/** * parse_property() - Parse a rule property given a token string. * @t: Supplies the token string to be parsed. * @r: Supplies the ipe_rule the parsed property will be associated with. * * This function parses and associates a property with an IPE rule based * on a token string. * * Return: * * %0 - Success * * %-ENOMEM - Out of memory (OOM) * * %-EBADMSG - The supplied token cannot be parsed
*/ staticint parse_property(char *t, struct ipe_rule *r)
{
substring_t args[MAX_OPT_ARGS]; struct ipe_prop *p = NULL; int rc = 0; int token; char *dup = NULL;
p = kzalloc(sizeof(*p), GFP_KERNEL); if (!p) return -ENOMEM;
token = match_token(t, property_tokens, args);
switch (token) { case IPE_PROP_DMV_ROOTHASH: case IPE_PROP_FSV_DIGEST:
dup = match_strdup(&args[0]); if (!dup) {
rc = -ENOMEM; goto err;
}
p->value = ipe_digest_parse(dup); if (IS_ERR(p->value)) {
rc = PTR_ERR(p->value); goto err;
}
fallthrough; case IPE_PROP_BOOT_VERIFIED_FALSE: case IPE_PROP_BOOT_VERIFIED_TRUE: case IPE_PROP_DMV_SIG_FALSE: case IPE_PROP_DMV_SIG_TRUE: case IPE_PROP_FSV_SIG_FALSE: case IPE_PROP_FSV_SIG_TRUE:
p->type = token; break; default:
rc = -EBADMSG; break;
} if (rc) goto err;
list_add_tail(&p->next, &r->props);
if (rc) goto err; if (!is_default_rule)
list_add_tail(&r->next, &p->rules[op].rules); else
free_rule(r);
return rc;
err:
free_rule(r); return rc;
}
/** * ipe_free_parsed_policy() - free a parsed policy structure. * @p: Supplies the parsed policy.
*/ void ipe_free_parsed_policy(struct ipe_parsed_policy *p)
{ struct ipe_rule *pp, *t;
size_t i = 0;
if (IS_ERR_OR_NULL(p)) return;
for (i = 0; i < ARRAY_SIZE(p->rules); ++i)
list_for_each_entry_safe(pp, t, &p->rules[i].rules, next) {
list_del(&pp->next);
free_rule(pp);
}
kfree(p->name);
kfree(p);
}
/** * validate_policy() - validate a parsed policy. * @p: Supplies the fully parsed policy. * * Given a policy structure that was just parsed, validate that all * operations have their default rules or a global default rule is set. * * Return: * * %0 - Success * * %-EBADMSG - Policy is invalid
*/ staticint validate_policy(conststruct ipe_parsed_policy *p)
{
size_t i = 0;
if (p->global_default_action != IPE_ACTION_INVALID) return 0;
for (i = 0; i < ARRAY_SIZE(p->rules); ++i) { if (p->rules[i].default_action == IPE_ACTION_INVALID) return -EBADMSG;
}
return 0;
}
/** * ipe_parse_policy() - Given a string, parse the string into an IPE policy. * @p: partially filled ipe_policy structure to populate with the result. * it must have text and textlen set. * * Return: * * %0 - Success * * %-EBADMSG - Policy is invalid * * %-ENOMEM - Out of Memory * * %-ERANGE - Policy version number overflow * * %-EINVAL - Policy version parsing error
*/ int ipe_parse_policy(struct ipe_policy *p)
{ struct ipe_parsed_policy *pp = NULL; char *policy = NULL, *dup = NULL; bool header_parsed = false; char *line = NULL;
size_t len; int rc = 0;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.