Skip to content

Commit 585b177

Browse files
committed
Fixed TALOS-2019-0844 - XPM image colorhash parsing Code Execution Vulnerability
The table entry in the color_hash is created in the create_colorhash function based on the number of colors passed into the function. The size of the color_hash table is the first value in the powers of 2 larger than the passed in number of colors [2]. The size of the allocation is this calculated value * 8 (sizeof(struct hash_entry **)) [3]. This multiplication can cause an overflow, resulting in a very small allocation.
1 parent 52b9d17 commit 585b177

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

IMG_xpm.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static struct color_hash *create_colorhash(int maxnum)
101101

102102
/* we know how many entries we need, so we can allocate
103103
everything here */
104-
hash = (struct color_hash *)SDL_malloc(sizeof *hash);
104+
hash = (struct color_hash *)SDL_calloc(1, sizeof(*hash));
105105
if (!hash)
106106
return NULL;
107107

@@ -110,15 +110,29 @@ static struct color_hash *create_colorhash(int maxnum)
110110
;
111111
hash->size = s;
112112
hash->maxnum = maxnum;
113+
113114
bytes = hash->size * sizeof(struct hash_entry **);
114-
hash->entries = NULL; /* in case malloc fails */
115-
hash->table = (struct hash_entry **)SDL_malloc(bytes);
115+
/* Check for overflow */
116+
if ((bytes / sizeof(struct hash_entry **)) != hash->size) {
117+
IMG_SetError("memory allocation overflow");
118+
SDL_free(hash);
119+
return NULL;
120+
}
121+
hash->table = (struct hash_entry **)SDL_calloc(1, bytes);
116122
if (!hash->table) {
117123
SDL_free(hash);
118124
return NULL;
119125
}
120-
SDL_memset(hash->table, 0, bytes);
121-
hash->entries = (struct hash_entry *)SDL_malloc(maxnum * sizeof(struct hash_entry));
126+
127+
bytes = maxnum * sizeof(struct hash_entry);
128+
/* Check for overflow */
129+
if ((bytes / sizeof(struct hash_entry)) != maxnum) {
130+
IMG_SetError("memory allocation overflow");
131+
SDL_free(hash->table);
132+
SDL_free(hash);
133+
return NULL;
134+
}
135+
hash->entries = (struct hash_entry *)SDL_calloc(1, bytes);
122136
if (!hash->entries) {
123137
SDL_free(hash->table);
124138
SDL_free(hash);

0 commit comments

Comments
 (0)