// SPDX-License-Identifier: GPL-2.0 /* * Copyright (C) 2020-2024 Microsoft Corporation. All rights reserved. */ #include <linux/err.h> #include <linux/slab.h> #include <linux/parser.h> #include <linux/types.h> #include <linux/ctype.h> #include "policy.h" #include "policy_parser.h" #include "digest.h" #define START_COMMENT … #define IPE_POLICY_DELIM … #define IPE_LINE_DELIM … /** * new_parsed_policy() - Allocate and initialize a parsed policy. * * Return: * * a pointer to the ipe_parsed_policy structure - Success * * %-ENOMEM - Out of memory (OOM) */ static struct ipe_parsed_policy *new_parsed_policy(void) { … } /** * remove_comment() - Truncate all chars following START_COMMENT in a string. * * @line: Supplies a policy line string for preprocessing. */ static void remove_comment(char *line) { … } /** * 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) { … } /** * 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 */ static int parse_version(char *ver, struct ipe_parsed_policy *p) { … } enum header_opt { … }; static const match_table_t header_tokens = …; /** * parse_header() - Parse policy header information. * @line: Supplies header line to be parsed. * @p: Supplies the partial parsed policy. * * Return: * * %0 - Success * * %-EBADMSG - Header string is invalid * * %-ENOMEM - Out of memory (OOM) * * %-ERANGE - Version number overflow * * %-EINVAL - Version parsing error */ static int parse_header(char *line, struct ipe_parsed_policy *p) { … } /** * 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" */ static bool token_default(char *token) { … } /** * 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. */ static void free_rule(struct ipe_rule *r) { … } static const match_table_t operation_tokens = …; /** * parse_operation() - Parse the operation type given a token string. * @t: Supplies the token string to be parsed. * * Return: The parsed operation type. */ static enum ipe_op_type parse_operation(char *t) { … } static const match_table_t action_tokens = …; /** * parse_action() - Parse the action type given a token string. * @t: Supplies the token string to be parsed. * * Return: The parsed action type. */ static enum ipe_action_type parse_action(char *t) { … } static const match_table_t property_tokens = …; /** * 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 */ static int parse_property(char *t, struct ipe_rule *r) { … } /** * parse_rule() - parse a policy rule line. * @line: Supplies rule line to be parsed. * @p: Supplies the partial parsed policy. * * Return: * * 0 - Success * * %-ENOMEM - Out of memory (OOM) * * %-EBADMSG - Policy syntax error */ static int parse_rule(char *line, struct ipe_parsed_policy *p) { … } /** * ipe_free_parsed_policy() - free a parsed policy structure. * @p: Supplies the parsed policy. */ void ipe_free_parsed_policy(struct ipe_parsed_policy *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 */ static int validate_policy(const struct ipe_parsed_policy *p) { … } /** * 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) { … }