Skip to content

Commit 740079a

Browse files
authored
Remove MLIB from PP header (#118)
* pp mlib changes * format * strcpy security fix w #define * strnlen * apply format * add bslp to define * docs + formatting * fix symbol * docs * format * off by 1 * test policyrule description limit * format
1 parent 4a9fc76 commit 740079a

5 files changed

Lines changed: 73 additions & 41 deletions

File tree

src/policy_provider/SamplePolicyProvider.c

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,24 @@
2727
*/
2828
#include <stddef.h>
2929
#include <stdlib.h>
30-
31-
#include <BPSecLib_Private.h>
30+
#include <string.h>
3231
#include <sys/types.h>
32+
#include <m-array.h>
3333

34+
#include <BPSecLib_Private.h>
3435
#include "SamplePolicyProvider.h"
3536

37+
/** @struct BSLP_SecOperPtrList_t
38+
* Defines a basic list of ::BSL_SecOper_t pointers.
39+
*/
40+
/// @cond Doxygen_Suppress
41+
// NOLINTBEGIN
42+
// GCOV_EXCL_START
43+
M_ARRAY_DEF(BSLP_SecOperPtrList, BSL_SecOper_t *, M_PTR_OPLIST)
44+
// GCOV_EXCL_STOP
45+
// NOLINTEND
46+
/// @endcond
47+
3648
static bool BSLP_PolicyProvider_IsConsistent(const BSLP_PolicyProvider_t *self)
3749
{
3850
ASSERT_ARG_NONNULL(self);
@@ -164,15 +176,15 @@ int BSLP_QueryPolicy(const void *user_data, BSL_SecurityActionSet_t *output_acti
164176
const BSLP_PolicyRule_t *rule = &self->rules[index];
165177
if (!BSLP_PolicyRule_IsConsistent(rule))
166178
{
167-
BSL_LOG_ERR("Rule `%s` is not consistent", m_string_get_cstr(rule->description));
179+
BSL_LOG_ERR("Rule `%s` is not consistent", rule->description);
168180
continue;
169181
}
170-
BSL_LOG_DEBUG("Evaluating against rule `%s`", m_string_get_cstr(rule->description));
182+
BSL_LOG_DEBUG("Evaluating against rule `%s`", rule->description);
171183

172184
if (!BSLP_PolicyPredicate_IsMatch(rule->predicate, location, primary_block.field_src_node_id,
173185
primary_block.field_dest_eid))
174186
{
175-
BSL_LOG_DEBUG("Rule `%s` not a match", m_string_get_cstr(rule->description));
187+
BSL_LOG_DEBUG("Rule `%s` not a match", rule->description);
176188
continue;
177189
}
178190

@@ -258,7 +270,7 @@ int BSLP_QueryPolicy(const void *user_data, BSL_SecurityActionSet_t *output_acti
258270
BSL_LOG_INFO("append to end");
259271
BSLP_SecOperPtrList_push_back(secops, sec_oper);
260272
}
261-
BSL_LOG_INFO("Created sec operation for rule `%s`", m_string_get_cstr(rule->description));
273+
BSL_LOG_INFO("Created sec operation for rule `%s`", rule->description);
262274
}
263275
BSL_PrimaryBlock_deinit(&primary_block);
264276

@@ -408,7 +420,12 @@ int BSLP_PolicyRule_Init(BSLP_PolicyRule_t *self, const char *desc, BSLP_PolicyP
408420
{
409421
ASSERT_ARG_NONNULL(self);
410422
memset(self, 0, sizeof(*self));
411-
string_init_set_str(self->description, desc);
423+
424+
size_t desc_sz = strnlen(desc, BSLP_POLICYPREDICATE_ARRAY_CAPACITY);
425+
self->description = BSL_MALLOC(desc_sz + 1);
426+
strncpy(self->description, desc, desc_sz);
427+
self->description[desc_sz] = '\0';
428+
412429
self->sec_block_type = sec_block_type;
413430
self->target_block_type = target_block_type;
414431
self->predicate = predicate;
@@ -424,9 +441,8 @@ int BSLP_PolicyRule_Init(BSLP_PolicyRule_t *self, const char *desc, BSLP_PolicyP
424441
void BSLP_PolicyRule_Deinit(BSLP_PolicyRule_t *self)
425442
{
426443
ASSERT_ARG_EXPR(BSLP_PolicyRule_IsConsistent(self));
427-
BSL_LOG_INFO("BSLP_PolicyRule_Deinit: %s, nparams=%zu", m_string_get_cstr(self->description),
428-
BSLB_SecParamList_size(self->params));
429-
string_clear(self->description);
444+
BSL_LOG_INFO("BSLP_PolicyRule_Deinit: %s, nparams=%zu", self->description, BSLB_SecParamList_size(self->params));
445+
BSL_FREE(self->description);
430446
BSLB_SecParamList_clear(self->params);
431447
memset(self, 0, sizeof(*self));
432448
}
@@ -486,7 +502,7 @@ int BSLP_PolicyRule_EvaluateAsSecOper(const BSLP_PolicyRule_t *self, BSL_SecOper
486502
const BSL_SecParam_t *param = BSLB_SecParamList_cref(pit);
487503
BSL_SecOper_AppendParam(sec_oper, param);
488504
}
489-
BSL_LOG_INFO("Created sec operation for rule `%s`", m_string_get_cstr(self->description));
505+
BSL_LOG_INFO("Created sec operation for rule `%s`", self->description);
490506

491507
return BSL_SUCCESS;
492508
}

src/policy_provider/SamplePolicyProvider.h

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,9 @@
2929
#define BSLP_SAMPLE_POLICY_PROVIDER_H
3030

3131
#include <stdint.h>
32-
#include <m-array.h>
33-
#include <m-string.h>
3432
#include <BPSecLib_Private.h>
3533
#include <backend/SecParam.h>
3634

37-
/** @struct BSLP_SecOperPtrList_t
38-
* Defines a basic list of ::BSL_SecOper_t pointers.
39-
*/
40-
/// @cond Doxygen_Suppress
41-
// NOLINTBEGIN
42-
// GCOV_EXCL_START
43-
M_ARRAY_DEF(BSLP_SecOperPtrList, BSL_SecOper_t *, M_PTR_OPLIST)
44-
// GCOV_EXCL_STOP
45-
// NOLINTEND
46-
/// @endcond
47-
4835
/**
4936
* THE key function that matches a bundle against a rule to provide the output action and specific parameters to use for
5037
* the security operation.
@@ -90,6 +77,12 @@ void BSLP_PolicyPredicate_Deinit(BSLP_PolicyPredicate_t *self);
9077
bool BSLP_PolicyPredicate_IsMatch(const BSLP_PolicyPredicate_t *self, BSL_PolicyLocation_e location,
9178
BSL_HostEID_t src_eid, BSL_HostEID_t dst_eid);
9279

80+
/**
81+
* Maximum string length of a policy rule description;
82+
* Affects ::BSLP_PolicyRule_Init `desc` parameter
83+
*/
84+
#define BSLP_POLICY_RULE_DESCRIPTION_MAX_STRLEN 100
85+
9386
/**
9487
* @brief Represents a policy rule
9588
*
@@ -105,7 +98,7 @@ bool BSLP_PolicyPredicate_IsMatch(const BSLP_PolicyPredicate_t *self, BSL_Policy
10598
*/
10699
typedef struct BSLP_PolicyRule_s
107100
{
108-
string_t description;
101+
char *description;
109102
BSLP_PolicyPredicate_t *predicate;
110103
BSL_SecRole_e role;
111104
BSL_BundleBlockTypeCode_e target_block_type;
@@ -119,7 +112,8 @@ typedef struct BSLP_PolicyRule_s
119112
* @brief Initialize this policy rule
120113
*
121114
* @param[in] self This policy rule
122-
* @param[in] dest Description of this rule (C-string)
115+
* @param[in] dest Description of this rule (C-string). Will copy characters of parameter from index 0 to
116+
* ::BSLP_POLICY_RULE_DESCRIPTION_MAX_STRLEN - 1.
123117
* @param[in] predicate Predicate used to identify which bundles apply
124118
* @param[in] context_id Security context ID
125119
* @param[in] role Such as source, acceptor, etc
@@ -173,7 +167,6 @@ int BSLP_PolicyRule_EvaluateAsSecOper(const BSLP_PolicyRule_t *self, BSL_SecOper
173167
/// @brief Concrete definition of a policy provider
174168
typedef struct BSLP_PolicyProvider_s
175169
{
176-
string_t name;
177170
BSLP_PolicyPredicate_t predicates[BSLP_POLICYPREDICATE_ARRAY_CAPACITY];
178171
size_t predicate_count;
179172
BSLP_PolicyRule_t rules[BSLP_POLICYPREDICATE_ARRAY_CAPACITY];

test/test_BackendPolicyProvider.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ void tearDown(void)
8383
*/
8484
void test_PolicyProvider_InspectEmptyRuleset(void)
8585
{
86-
BSLP_PolicyProvider_t *policy = BSL_PolicyDict_get(LocalTestCtx.bsl.policy_reg, BSL_SAMPLE_PP_ID)->user_data;
87-
string_init_set_str(policy->name, "Unit Test Policy Provider!");
8886
TEST_ASSERT_EQUAL(0,
8987
BSL_TestUtils_LoadBundleFromCBOR(&LocalTestCtx, RFC9173_TestVectors_AppendixA1.cbor_bundle_bib));
9088

@@ -98,7 +96,6 @@ void test_PolicyProvider_InspectEmptyRuleset(void)
9896
TEST_ASSERT_EQUAL(0, BSL_SecurityAction_CountSecOpers(act));
9997

10098
BSL_SecurityActionSet_Deinit(&action_set);
101-
string_clear(policy->name);
10299
}
103100

104101
/**
@@ -110,7 +107,6 @@ void test_PolicyProvider_InspectEmptyRuleset(void)
110107
void test_PolicyProvider_InspectSingleBIBRuleset(void)
111108
{
112109
BSLP_PolicyProvider_t *policy = BSL_PolicyDict_get(LocalTestCtx.bsl.policy_reg, BSL_SAMPLE_PP_ID)->user_data;
113-
string_init_set_str(policy->name, "Unit Test Policy Provider!");
114110

115111
BSLP_PolicyPredicate_t *predicate = &policy->predicates[policy->predicate_count++];
116112
BSLP_PolicyPredicate_Init(predicate, BSL_POLICYLOCATION_APPIN, BSL_TestUtils_GetEidPatternFromText("*:**"),
@@ -131,7 +127,6 @@ void test_PolicyProvider_InspectSingleBIBRuleset(void)
131127
TEST_ASSERT_EQUAL(1, BSL_SecurityAction_CountSecOpers(BSL_SecurityActionSet_GetActionAtIndex(&action_set, 0)));
132128

133129
BSL_SecurityActionSet_Deinit(&action_set);
134-
string_clear(policy->name);
135130
}
136131

137132
/**
@@ -140,7 +135,6 @@ void test_PolicyProvider_InspectSingleBIBRuleset(void)
140135
void test_PolicyProvider_Inspect_RFC9173_BIB(void)
141136
{
142137
BSLP_PolicyProvider_t *policy = BSL_PolicyDict_get(LocalTestCtx.bsl.policy_reg, BSL_SAMPLE_PP_ID)->user_data;
143-
string_init_set_str(policy->name, "Unit Test Policy Provider!");
144138

145139
BSLP_PolicyPredicate_t *predicate = &policy->predicates[policy->predicate_count++];
146140
BSLP_PolicyPredicate_Init(predicate, BSL_POLICYLOCATION_APPIN, BSL_TestUtils_GetEidPatternFromText("*:**"),
@@ -167,7 +161,6 @@ void test_PolicyProvider_Inspect_RFC9173_BIB(void)
167161
TEST_ASSERT_EQUAL(3, BSL_SecOper_CountParams(BSL_SecurityAction_GetSecOperAtIndex(act, 0)));
168162

169163
BSL_SecurityActionSet_Deinit(&action_set);
170-
string_clear(policy->name);
171164
}
172165

173166
// TODO - test with also setting sec pararms and other things and test the RFC 9173 things.
@@ -187,11 +180,9 @@ void test_MultiplePolicyProviders(void)
187180

188181
BSLP_PolicyProvider_t *policy = BSL_PolicyDict_get(LocalTestCtx.bsl.policy_reg, BSL_SAMPLE_PP_ID)->user_data;
189182
policy->pp_id = BSL_SAMPLE_PP_ID;
190-
string_init_set_str(policy->name, "Unit Test Policy Provider 1!");
191183

192184
BSLP_PolicyProvider_t *policy2 = BSL_PolicyDict_get(LocalTestCtx.bsl.policy_reg, BSL_SAMPLE_PP_ID_2)->user_data;
193185
policy2->pp_id = BSL_SAMPLE_PP_ID_2;
194-
string_init_set_str(policy2->name, "Unit Test Policy Provider 2!");
195186

196187
BSLP_PolicyPredicate_t *predicate = &policy->predicates[policy->predicate_count++];
197188
BSLP_PolicyPredicate_Init(predicate, BSL_POLICYLOCATION_APPIN, BSL_TestUtils_GetEidPatternFromText("*:**"),
@@ -254,6 +245,4 @@ void test_MultiplePolicyProviders(void)
254245

255246
BSL_SecurityActionSet_Deinit(&action_set);
256247
BSL_FREE(response_set);
257-
string_clear(policy->name);
258-
string_clear(policy2->name);
259248
}

test/test_PublicInterfaceImpl.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ void setUp(void)
135135
TEST_ASSERT_EQUAL(0, BSL_API_RegisterPolicyProvider(&LocalTestCtx.bsl, BSL_SAMPLE_PP_ID, policy_desc));
136136

137137
BSLP_PolicyProvider_t *policy = BSL_PolicyDict_get(LocalTestCtx.bsl.policy_reg, BSL_SAMPLE_PP_ID)->user_data;
138-
string_init_set_str(policy->name, "Unit Test Policy Provider!");
139138

140139
policy->pp_id = 1;
141140

@@ -569,8 +568,6 @@ void setUp(void)
569568
void tearDown(void)
570569
{
571570
BSL_SecurityActionSet_Deinit(&action_set);
572-
BSLP_PolicyProvider_t *policy = BSL_PolicyDict_get(LocalTestCtx.bsl.policy_reg, BSL_SAMPLE_PP_ID)->user_data;
573-
string_clear(policy->name);
574571
mock_bpa_ctr_deinit(&LocalTestCtx.mock_bpa_ctr);
575572
BSL_CryptoDeinit();
576573
TEST_ASSERT_EQUAL(0, BSL_API_DeinitLib(&LocalTestCtx.bsl));

test/test_SamplePolicyProvider.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,41 @@ void test_SamplePolicyProvider_WildcardPolicyRuleVerifiesBIB(void)
122122
BSLP_PolicyPredicate_Deinit(&predicate);
123123
}
124124

125+
TEST_CASE("")
126+
TEST_CASE("1")
127+
TEST_CASE(
128+
"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789") // 100 char
129+
TEST_CASE(
130+
"01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890") // 101 char
131+
void test_SamplePolicyProvider_PolicyRuleInit_Description(const char *description)
132+
{
133+
BSLP_PolicyPredicate_t predicate;
134+
BSLP_PolicyPredicate_Init(&predicate, BSL_POLICYLOCATION_APPIN, BSL_TestUtils_GetEidPatternFromText("*:**"),
135+
BSL_TestUtils_GetEidPatternFromText("*:**"), BSL_TestUtils_GetEidPatternFromText("*:**"));
136+
137+
BSLP_PolicyRule_t rule;
138+
BSLP_PolicyRule_Init(&rule, description, &predicate, 1, BSL_SECROLE_VERIFIER, BSL_SECBLOCKTYPE_BIB,
139+
BSL_BLOCK_TYPE_PAYLOAD, BSL_POLICYACTION_DROP_BUNDLE);
140+
141+
TEST_ASSERT_LESS_OR_EQUAL(BSLP_POLICY_RULE_DESCRIPTION_MAX_STRLEN, strlen(rule.description));
142+
143+
if (strlen(description) <= BSLP_POLICY_RULE_DESCRIPTION_MAX_STRLEN)
144+
{
145+
TEST_ASSERT_EQUAL(strlen(description), strlen(rule.description));
146+
}
147+
else
148+
{
149+
TEST_ASSERT_EQUAL(BSLP_POLICY_RULE_DESCRIPTION_MAX_STRLEN, strlen(rule.description));
150+
}
151+
152+
// unity doesn't like TEST_ASSERT_EQUAL_MEMORY call on 0 length buffer
153+
if (strlen(description) > 0)
154+
{
155+
TEST_ASSERT_EQUAL_MEMORY(description, rule.description, strlen(rule.description));
156+
}
157+
158+
BSLP_PolicyRule_Deinit(&rule);
159+
BSLP_PolicyPredicate_Deinit(&predicate);
160+
}
161+
125162
// TODO(bvb) more tests with more granular predicates and rules

0 commit comments

Comments
 (0)