]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Nest: Fix memory alignment in attribute cache
authorOndrej Zajicek <santiago@crfreenet.org>
Wed, 13 Dec 2023 16:46:16 +0000 (17:46 +0100)
committerOndrej Zajicek <santiago@crfreenet.org>
Wed, 13 Dec 2023 16:46:16 +0000 (17:46 +0100)
In attribute cache, adata structures were stored densely in one memory
block, without regard to alignment. Let's force at least u32 alignment.

nest/route.h
nest/rt-attr.c

index 3e1340fa307279aa20d669bd35143f2895147c7e..d26a4b8c485427386c4fc4224c468dfc9f01c30a 100644 (file)
@@ -542,6 +542,7 @@ const char *ea_custom_name(uint ea);
 #define EA_ALLOW_UNDEF 0x10000         /* ea_find: allow EAF_TYPE_UNDEF */
 #define EA_BIT(n) ((n) << 24)          /* Used in bitfield accessors */
 #define EA_BIT_GET(ea) ((ea) >> 24)
+#define EA_DATA_ALIGN 4                        /* Alignment of adata in attribute cache */
 
 #define EAF_TYPE_MASK 0x1f             /* Mask with this to get type */
 #define EAF_TYPE_INT 0x01              /* 32-bit unsigned integer number */
index 25936d81d16ea757083c8ac74e3fd7b4f4a103d3..7f3645ee90ddcf27c2bfec35731fc9865b5cfdfc 100644 (file)
@@ -766,7 +766,7 @@ ea_list_copy(ea_list *o)
     {
       eattr *a = &o->attrs[i];
       if (!(a->type & EAF_EMBEDDED))
-       elen += sizeof(struct adata) + a->u.ptr->length;
+       elen += BIRD_ALIGN(sizeof(struct adata) + a->u.ptr->length, EA_DATA_ALIGN);
     }
 
   n = mb_alloc(rta_pool, elen);
@@ -777,11 +777,12 @@ ea_list_copy(ea_list *o)
       eattr *a = &n->attrs[i];
       if (!(a->type & EAF_EMBEDDED))
        {
-         unsigned size = sizeof(struct adata) + a->u.ptr->length;
+         uint size_u = sizeof(struct adata) + a->u.ptr->length;
+         uint size = BIRD_ALIGN(size_u, EA_DATA_ALIGN);
          ASSERT_DIE(adpos + size <= elen);
 
          struct adata *d = ((void *) n) + adpos;
-         memcpy(d, a->u.ptr, size);
+         memcpy(d, a->u.ptr, size_u);
          a->u.ptr = d;
 
          adpos += size;