Skip to contentSkip to conversation

settings: add l_settings_clone

PATCH Superseded James Prestwood 2 participants 11 messages Feb 4, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

James Prestwood

author
#0 lore raw

PATCH 1/5 settings: add l_settings_clone

Returns identical copy of settings

---
 ell/ell.sym    |  1 +
 ell/settings.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/settings.h |  2 ++
 3 files changed, 65 insertions(+)
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 +62 -0

68 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 2/5 unit: add settings test for clone APIs

---
 unit/test-settings.c | 40 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
unit/test-settings.c +39 -1
index a25197a..d9f25f9 100644
@@ -559,6 +559,44 @@ 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);
+}
+
 int main(int argc, char *argv[])
 {
 	l_test_init(&argc, &argv);
@@ -571,7 +609,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
#2 lore raw

Re: [PATCH 2/5] unit: add settings test for clone APIs

Hi James,

Quoted 4 lines · James Prestwood
---
  unit/test-settings.c | 40 +++++++++++++++++++++++++++++++++++++++-
  1 file changed, 39 insertions(+), 1 deletion(-)

There seems to be a leak somewhere?

TEST: Test clone
==22270==
==22270== HEAP SUMMARY:
==22270== in use at exit: 1,860 bytes in 14 blocks
==22270== total heap usage: 599 allocs, 585 frees, 25,152 bytes allocated
==22270==
==22270== 1,860 (40 direct, 1,820 indirect) bytes in 1 blocks are definitely lost in loss record 14 of 14
==22270== at 0x483F7B5: malloc (in
/usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)

==22270==    by 0x10D88D: l_malloc (util.c:62)
==22270==    by 0x10FCDB: l_settings_new (settings.c:112)
==22270==    by 0x10D565: test_clone (test-settings.c:589)
==22270==    by 0x10E438: l_test_run (test.c:84)
==22270==    by 0x10B5D9: main (test-settings.c:614)

Regards,
-Denis

author
#3 lore raw

PATCH 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 | 11 +++++++++++
 ell/settings.h |  1 +
 3 files changed, 13 insertions(+)
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 +11 -0
index a553ec0..6187a46 100644
@@ -1566,3 +1566,14 @@ 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_destroy(settings->embedded_groups, embedded_group_destroy);
+	settings->embedded_groups = l_queue_new();
+
+	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
#4 lore raw

Re: [PATCH 3/5] settings: add l_settings_remove_embedded_groups

Hi James,

<snip>

ell/settings.c LIB_EXPORT const char *l_settings_get_embedded_value( · line 1576

Quoted 7 lines
ell/settings.c +7 -0
+LIB_EXPORT bool l_settings_remove_embedded_groups(struct l_settings *settings)
+{
+	if (unlikely(!settings))
+		return false;
+
+	l_queue_destroy(settings->embedded_groups, embedded_group_destroy);
+	settings->embedded_groups = l_queue_new();

l_queue_clear() might be cleaner here.

ell/settings.c LIB_EXPORT const char *l_settings_get_embedded_value( · line 1579

Quoted 3 lines
ell/settings.c +3 -0
+
+	return true;
+}

Regards,
-Denis

author
#5 lore raw

PATCH 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
#6 lore raw

Re: [PATCH 4/5] util: add l_secure_select_byte

Hi James,

Quoted 19 lines · 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 +5 -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.

ell/util.h static inline void l_secure_select(bool select_left, · line 442

Quoted 10 lines
ell/util.h +10 -0
+ */
+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);
+}
+

Why not use secure_select from ell/useful.h ?

ell/util.h static inline void l_secure_select(bool select_left, · line 445

Quoted 4 lines
ell/util.h +0 -0
  #ifdef __cplusplus
  }
  #endif

Regards,
-Denis

author
#7 lore raw

Re: [PATCH 4/5] util: add l_secure_select_byte

Quoted 30 lines · Denis Kenzior

Hi James,

Quoted 27 lines · 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 +0 -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)

Quoted 8 lines

ell/util.h static inline void l_secure_select(bool select_left, · line 442

Quoted 6 lines
ell/util.h +6 -0
+{
+       uint8_t mask = -(!!select_true);
+
+       return (mask & true_value) | (~mask & false_value);
+}
+

Why not use secure_select from ell/useful.h ?

Ugh, we should keep these things in one place, unless there was a reason for doing it this way?

Quoted 8 lines
Quoted 5 lines

  #ifdef __cplusplus
  }
  #endif

Regards,
-Denis

#8 lore raw

Re: [PATCH 4/5] util: add l_secure_select_byte

Hi James,

Quoted 6 lines
Quoted 2 lines

Why not use secure_select from ell/useful.h ?

Ugh, we should keep these things in one place, unless there was a reason for doing it this way?

It was needed for l_ecc_point_from_sswu() (SAE-H2E). I don't remember why I didn't switch iwd over. We should try to do so.

In general, ell/useful.h would be the place for such utilities.

Regards,
-Denis

author
#9 lore raw

PATCH 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
#10 lore raw
In reply to James Prestwood

Hi James,

Quoted 7 lines · James Prestwood

Returns identical copy of settings

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

<snip>

Quoted 5 lines
ell/settings.c +5 -0
+
+static void copy_embedded_foreach(void *data, void *user_data)
+{
+	struct embedded_group_data *embedded = data;
+	struct l_queue *groups = user_data;

ell/settings.c LIB_EXPORT struct l_settings *l_settings_new(void) · line 159

Quoted 10 lines
ell/settings.c +10 -0
+	struct embedded_group_data *copy;
+
+	copy = l_malloc(sizeof(struct embedded_group_data) +
+					embedded->len + 1);
+	copy->name = l_strdup(embedded->name);
+
+	memcpy(copy->type, embedded->type, sizeof(embedded->type));
+	copy->len = embedded->len;
+	memcpy(copy->data, embedded->data, embedded->len);
+	copy->data[embedded->len] = '\0';

Any reason not to just memcpy the entire structure and fix up copy->name to invoke l_strdup later?

ell/settings.c LIB_EXPORT struct l_settings *l_settings_new(void) · line 162

Quoted 3 lines
ell/settings.c +3 -0
+
+	l_queue_push_tail(groups, copy);
+}

Regards,
-Denis