]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MEDIUM: Make sure stksess is properly aligned.
authorOlivier Houchard <ohouchard@haproxy.com>
Wed, 14 Nov 2018 16:54:36 +0000 (17:54 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 15 Nov 2018 13:24:05 +0000 (14:24 +0100)
When we allocate struct stksess, we also allocate memory to store the
associated data before the struct itself.
As the data can be of different types, they can have different size. However,
we need the struct stksess to be properly aligned, as it can do 64bits
load/store (including atomic load/stores) on 64bits platforms, and some of
them doesn't support unaligned access.
So, when allocating the struct stksess, round the size up to the next
multiple of sizeof(void *), and make sure the struct stksess itself is
properly aligned.
Many thanks to Paul Martin for investigating and reporting that bug.

This should be backported to earlier releases.

src/stick_table.c

index 7f14163105d1148b6d434c4f2f376bdda6daceb9..5e55b7a9b90ebf99348c70a3c4093c2dee964090 100644 (file)
@@ -45,6 +45,7 @@
 /* structure used to return a table key built from a sample */
 static THREAD_LOCAL struct stktable_key static_table_key;
 
+#define round_ptr_size(i) (((i) + (sizeof(void *) - 1)) &~ (sizeof(void *) - 1))
 /*
  * Free an allocated sticky session <ts>, and decrease sticky sessions counter
  * in table <t>.
@@ -52,7 +53,7 @@ static THREAD_LOCAL struct stktable_key static_table_key;
 void __stksess_free(struct stktable *t, struct stksess *ts)
 {
        t->current--;
-       pool_free(t->pool, (void *)ts - t->data_size);
+       pool_free(t->pool, (void *)ts - round_ptr_size(t->data_size));
 }
 
 /*
@@ -230,7 +231,7 @@ struct stksess *__stksess_new(struct stktable *t, struct stktable_key *key)
        ts = pool_alloc(t->pool);
        if (ts) {
                t->current++;
-               ts = (void *)ts + t->data_size;
+               ts = (void *)ts + round_ptr_size(t->data_size);
                __stksess_init(t, ts);
                if (key)
                        stksess_setkey(t, ts, key);
@@ -598,7 +599,7 @@ int stktable_init(struct stktable *t)
                t->updates = EB_ROOT_UNIQUE;
                HA_SPIN_INIT(&t->lock);
 
-               t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
+               t->pool = create_pool("sticktables", sizeof(struct stksess) + round_ptr_size(t->data_size) + t->key_size, MEM_F_SHARED);
 
                t->exp_next = TICK_ETERNITY;
                if ( t->expire ) {