Skip to content

Commit

Permalink
tar2qfile: Avoid integer overflow
Browse files Browse the repository at this point in the history
If there were too many directories already sent, an integer overflow
would occur, with undefined results.  In practice the most likely result
is a failure to realloc a stupendous amount of memory.

(cherry picked from commit 5d62fab)
  • Loading branch information
DemiMarie authored and marmarek committed Nov 5, 2024
1 parent d49f251 commit a4a4b09
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion qubes-rpc/tar2qfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/

#define _GNU_SOURCE /* For O_NOFOLLOW. */
#include <limits.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
Expand Down Expand Up @@ -750,7 +751,10 @@ ustar_rd (int fd, struct file_header * untrusted_hdr, char *buf, struct stat * s
#ifdef DEBUG
fprintf(stderr,"Inserting %s into register\n",path);
#endif
dirs_headers_sent = realloc(dirs_headers_sent, sizeof (char*) * (++n_dirs));
size_t new_alloc_size;
if (n_dirs >= INT_MAX || __builtin_mul_overflow(sizeof(char *), ++n_dirs, &new_alloc_size))
gui_fatal("Too many directories already sent");
dirs_headers_sent = realloc(dirs_headers_sent, new_alloc_size);
if (dirs_headers_sent == NULL)
return MEMORY_ALLOC_FAILED;
dirs_headers_sent[n_dirs-1] = malloc(sizeof (char) * (strlen(path)+1));
Expand Down

0 comments on commit a4a4b09

Please sign in to comment.