Skip to content
This repository was archived by the owner on Jan 28, 2020. It is now read-only.

Commit 7e8dc08

Browse files
committed
Fix offset calculation for session entries.
The session entry size isn't known on compile time, so we must actually calculate the offset at runtime. This fixes a nasty bug where we would randomly overwrite session entries.
1 parent d29de0b commit 7e8dc08

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

auth_mellon_cache.c

+31-11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@
2121

2222
#include "auth_mellon.h"
2323

24+
/* Calculate the pointer to a cache entry.
25+
*
26+
* Parameters:
27+
* am_mod_cfg_rec *mod_cfg The module configuration.
28+
* void *table The base pointer for the table.
29+
* apr_size_t index The index we are looking for.
30+
*
31+
* Returns:
32+
* The session entry with the given index.
33+
*/
34+
static inline am_cache_entry_t *am_cache_entry_ptr(am_mod_cfg_rec *mod_cfg,
35+
void *table, apr_size_t index)
36+
{
37+
uint8_t *table_calc;
38+
table_calc = table;
39+
return (am_cache_entry_t *)&table_calc[mod_cfg->init_entry_size * index];
40+
}
41+
2442
/* Initialize the session table.
2543
*
2644
* Parameters:
@@ -31,13 +49,14 @@
3149
*/
3250
void am_cache_init(am_mod_cfg_rec *mod_cfg)
3351
{
34-
am_cache_entry_t *table;
35-
int i;
52+
void *table;
53+
apr_size_t i;
3654
/* Initialize the session table. */
3755
table = apr_shm_baseaddr_get(mod_cfg->cache);
3856
for (i = 0; i < mod_cfg->cache_size; i++) {
39-
table[i].key[0] = '\0';
40-
table[i].access = 0;
57+
am_cache_entry_t *e = am_cache_entry_ptr(mod_cfg, table, i);
58+
e->key[0] = '\0';
59+
e->access = 0;
4160
}
4261
}
4362

@@ -59,8 +78,8 @@ am_cache_entry_t *am_cache_lock(server_rec *s,
5978
const char *key)
6079
{
6180
am_mod_cfg_rec *mod_cfg;
62-
am_cache_entry_t *table;
63-
int i;
81+
void *table;
82+
apr_size_t i;
6483
int rv;
6584
char buffer[512];
6685

@@ -96,20 +115,21 @@ am_cache_entry_t *am_cache_lock(server_rec *s,
96115

97116

98117
for(i = 0; i < mod_cfg->init_cache_size; i++) {
118+
am_cache_entry_t *e = am_cache_entry_ptr(mod_cfg, table, i);
99119
const char *tablekey;
100120

101-
if (table[i].key[0] == '\0') {
121+
if (e->key[0] == '\0') {
102122
/* This entry is empty. Skip it. */
103123
continue;
104124
}
105125

106126
switch (type) {
107127
case AM_CACHE_SESSION:
108-
tablekey = table[i].key;
128+
tablekey = e->key;
109129
break;
110130
case AM_CACHE_NAMEID:
111131
/* tablekey may be NULL */
112-
tablekey = am_cache_env_fetch_first(&table[i], "NAME_ID");
132+
tablekey = am_cache_env_fetch_first(e, "NAME_ID");
113133
break;
114134
default:
115135
tablekey = NULL;
@@ -121,9 +141,9 @@ am_cache_entry_t *am_cache_lock(server_rec *s,
121141

122142
if(strcmp(tablekey, key) == 0) {
123143
/* We found the entry. */
124-
if(table[i].expires > apr_time_now()) {
144+
if(e->expires > apr_time_now()) {
125145
/* And it hasn't expired. */
126-
return &table[i];
146+
return e;
127147
}
128148
}
129149
}

0 commit comments

Comments
 (0)