Skip to contentSkip to conversation

settings: add l_settings_clone

PATCH In review James Prestwood 2 participants 6 messages Feb 4, 2022

lore
Share & tools
lore

James Prestwood

author
#0 lore raw

PATCH v2 1/5 settings: add l_settings_clone

Returns identical copy of settings

---
 ell/ell.sym    |  1 +
 ell/settings.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/settings.h |  2 ++
 3 files changed, 60 insertions(+)

v2:
* memdup the entire embedded_group_data structure rather than manually copying each part individually.

ell/ell.sym +1 -0
index 60c5525..827e68c 100644
@@ -452,6 +452,7 @@ global:
 	l_ringbuf_append;
 	/* settings */
 	l_settings_new;
+	l_settings_clone;
 	l_settings_free;
 	l_settings_load_from_data;
 	l_settings_to_data;
ell/settings.c +57 -0

63 lines not on this page · raw message

ell/settings.h +2 -0
index 9262ab9..f094ef5 100644
@@ -37,6 +37,8 @@ typedef void (*l_settings_debug_cb_t) (const char *str, void *user_data);
 typedef void (*l_settings_destroy_cb_t) (void *user_data);
 
 struct l_settings *l_settings_new(void);
+struct l_settings *l_settings_clone(const struct l_settings *settings);
+
 void l_settings_free(struct l_settings *settings);
 DEFINE_CLEANUP_FUNC(l_settings_free);
 
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v2 2/5 unit: add settings test for clone APIs

---
 unit/test-settings.c | 41 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)

v2:
* Fix 'settings' leak

unit/test-settings.c +40 -1
index a25197a..e506bd4 100644
@@ -559,6 +559,45 @@ static void test_invalid_data(const void *test_data)
 	l_settings_free(settings);
 }
 
+static void check_clone_data(struct l_settings *settings)
+{
+	const char *value;
+	const char *type = NULL;
+
+	assert(l_settings_has_group(settings, "normal"));
+	assert(l_settings_has_key(settings, "normal", "key"));
+	value = l_settings_get_value(settings, "normal", "key");
+	assert(value);
+	assert(!strcmp(value, "value"));
+
+	assert(l_settings_has_embedded_group(settings, "single_cert"));
+	value = l_settings_get_embedded_value(settings, "single_cert",
+								&type);
+	assert(value && type);
+	assert(!strcmp(type, "pem"));
+	assert(!strcmp(value, TEST_CERTIFICATE));
+}
+
+static void test_clone(const void *data)
+{
+	const char *raw_data =
+			"[normal]\n"
+			"key=value\n"
+			"[@pem(a)single_cert]\n"
+			TEST_CERTIFICATE;
+
+	struct l_settings *settings = l_settings_new();
+	struct l_settings *copy;
+
+	assert(l_settings_load_from_data(settings, raw_data, strlen(raw_data)));
+	check_clone_data(settings);
+
+	copy = l_settings_clone(settings);
+	check_clone_data(copy);
+	l_settings_free(copy);
+	l_settings_free(settings);
+}
+
 int main(int argc, char *argv[])
 {
 	l_test_init(&argc, &argv);
@@ -571,7 +610,7 @@ int main(int argc, char *argv[])
 	l_test_add("Invalid Data 2", test_invalid_data, key_before_group_data);
 	l_test_add("Test valid ext group", test_valid_extended_group, NULL);
 	l_test_add("Test invalid ext group", test_invalid_extended_group, NULL);
-
+	l_test_add("Test clone", test_clone, NULL);
 
 	return l_test_run();
 }
Signature · 1 line
2.31.1
author
#2 lore raw

PATCH v2 3/5 settings: add l_settings_remove_embedded_groups

In reply to James Prestwood

This removes all embedded groups from a l_settings object

---
 ell/ell.sym    |  1 +
 ell/settings.c | 10 ++++++++++
 ell/settings.h |  1 +
 3 files changed, 12 insertions(+)

v2:
* Use l_queue_clear(). Not sure how I didn't notice this API existed.

ell/ell.sym +1 -0
index 827e68c..96b3a7d 100644
@@ -490,6 +490,7 @@ global:
 	l_settings_has_embedded_group;
 	l_settings_get_embedded_groups;
 	l_settings_get_embedded_value;
+	l_settings_remove_embedded_groups;
 	/* signal */
 	l_signal_create;
 	l_signal_remove;
ell/settings.c +10 -0
index 31d23fe..ac4259c 100644
@@ -1561,3 +1561,13 @@ LIB_EXPORT const char *l_settings_get_embedded_value(
 
 	return group->data;
 }
+
+LIB_EXPORT bool l_settings_remove_embedded_groups(struct l_settings *settings)
+{
+	if (unlikely(!settings))
+		return false;
+
+	l_queue_clear(settings->embedded_groups, embedded_group_destroy);
+
+	return true;
+}
ell/settings.h +1 -0
index f094ef5..519014d 100644
@@ -137,6 +137,7 @@ bool l_settings_remove_key(struct l_settings *settings, const char *group_name,
 bool l_settings_remove_group(struct l_settings *settings,
 				const char *group_name);
 
+bool l_settings_remove_embedded_groups(struct l_settings *settings);
 char **l_settings_get_embedded_groups(struct l_settings *settings);
 bool l_settings_has_embedded_group(struct l_settings *settings,
 					const char *group);
Signature · 1 line
2.31.1
author
#3 lore raw

PATCH v2 4/5 util: add l_secure_select_byte

In reply to James Prestwood

This was taken from IWD and modified slightly to act off a boolean rather than a 0xff/0x00 byte to stay consistent with l_secure_select

---
 ell/util.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
ell/util.h +15 -0
index 9d4e36c..d390d27 100644
@@ -425,6 +425,21 @@ static inline void l_secure_select(bool select_left,
 		o[i] = r[i] ^ ((l[i] ^ r[i]) & mask);
 }
 
+/*
+ * Returns either true_value or false_value (depending if mask is 0xFF or 0x00
+ * respectively).
+ * This constant time selection method allows to keep an identical memory
+ * access pattern.
+ */
+static inline uint8_t l_secure_select_byte(bool select_true,
+					const uint8_t true_value,
+					const uint8_t false_value)
+{
+	uint8_t mask = -(!!select_true);
+
+	return (mask & true_value) | (~mask & false_value);
+}
+
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.31.1
author
#4 lore raw

PATCH v2 5/5 ecc: make l_ecc_point_from_data fully constant time

In reply to James Prestwood

This was modified earlier to be constant time but there was one minor problem with how 'sub' was determined. That logic could short circuit which could alter the timing. The change in timing would be extremely small, especially compared to the previous fix making _vli_mod_sub() execute always but in any case there would be a small difference.

This patch uses l_secure_select_byte to select either of the two subtraction conditions which depend on the point type.

---
 ell/ecc.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
ell/ecc.c +3 -4
index eba9aef..d59b900 100644
@@ -562,10 +562,9 @@ LIB_EXPORT struct l_ecc_point *l_ecc_point_from_data(
 		if (!_ecc_compute_y(curve, p->y, p->x))
 			goto failed;
 
-		sub = ((type == L_ECC_POINT_TYPE_COMPRESSED_BIT0 &&
-				!(p->y[0] & 1)) ||
-				(type == L_ECC_POINT_TYPE_COMPRESSED_BIT1 &&
-				(p->y[0] & 1)));
+		sub = l_secure_select_byte(
+				type == L_ECC_POINT_TYPE_COMPRESSED_BIT0,
+				!(p->y[1] & 1), p->y[0] & 1);
 
 		_vli_mod_sub(tmp, curve->p, p->y, curve->p, curve->ndigits);
 
Signature · 1 line
2.31.1
#5 lore raw
In reply to James Prestwood

Hi James,

Quoted 11 lines · James Prestwood

Returns identical copy of settings

---
  ell/ell.sym    |  1 +
  ell/settings.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++
  ell/settings.h |  2 ++
  3 files changed, 60 insertions(+)

v2:
* memdup the entire embedded_group_data structure rather than manually copying each part individually.

Patches 1-3 applied, thanks.

Regards,
-Denis