Use sudo_mmap_alloc functions instead of private versions.
We no longer need to keep track of the allocation size.
This commit is contained in:
@@ -47,8 +47,6 @@
|
|||||||
!defined(HAVE_VASPRINTF) || !defined(HAVE_ASPRINTF) || \
|
!defined(HAVE_VASPRINTF) || !defined(HAVE_ASPRINTF) || \
|
||||||
defined(PREFER_PORTABLE_SNPRINTF)
|
defined(PREFER_PORTABLE_SNPRINTF)
|
||||||
|
|
||||||
#include <sys/mman.h>
|
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#ifdef HAVE_NL_LANGINFO
|
#ifdef HAVE_NL_LANGINFO
|
||||||
# include <langinfo.h>
|
# include <langinfo.h>
|
||||||
@@ -71,6 +69,7 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
#include "sudo_compat.h"
|
#include "sudo_compat.h"
|
||||||
|
#include "sudo_util.h"
|
||||||
|
|
||||||
/* Avoid printf format attacks by ignoring the %n escape. */
|
/* Avoid printf format attacks by ignoring the %n escape. */
|
||||||
#define NO_PRINTF_PERCENT_N
|
#define NO_PRINTF_PERCENT_N
|
||||||
@@ -107,51 +106,10 @@ union arg {
|
|||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
static int __find_arguments(const char *fmt0, va_list ap, union arg **argtable,
|
static int __find_arguments(const char *fmt0, va_list ap, union arg **argtable);
|
||||||
size_t *argtablesiz);
|
|
||||||
static int __grow_type_table(unsigned char **typetable, int *tablesize);
|
static int __grow_type_table(unsigned char **typetable, int *tablesize);
|
||||||
static int xxxprintf(char **, size_t, int, const char *, va_list);
|
static int xxxprintf(char **, size_t, int, const char *, va_list);
|
||||||
|
|
||||||
#if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
|
|
||||||
# define MAP_ANON MAP_ANONYMOUS
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef MAP_FAILED
|
|
||||||
# define MAP_FAILED ((void *) -1)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Allocate "size" bytes via mmap.
|
|
||||||
*/
|
|
||||||
static void *
|
|
||||||
mmap_alloc(size_t size)
|
|
||||||
{
|
|
||||||
void *p;
|
|
||||||
#ifndef MAP_ANON
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
if ((fd = open("/dev/zero", O_RDWR)) == -1)
|
|
||||||
return NULL;
|
|
||||||
p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
|
|
||||||
close(fd);
|
|
||||||
#else
|
|
||||||
p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
|
|
||||||
#endif
|
|
||||||
if (p == MAP_FAILED)
|
|
||||||
return NULL;
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Unmap "size" bytes of the ptr.
|
|
||||||
*/
|
|
||||||
static void
|
|
||||||
mmap_free(void *ptr, size_t size)
|
|
||||||
{
|
|
||||||
if (ptr != NULL)
|
|
||||||
munmap(ptr, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef PRINTF_WIDE_CHAR
|
#ifdef PRINTF_WIDE_CHAR
|
||||||
/*
|
/*
|
||||||
* Convert a wide character string argument for the %ls format to a multibyte
|
* Convert a wide character string argument for the %ls format to a multibyte
|
||||||
@@ -330,7 +288,6 @@ xxxprintf(char **strp, size_t strsize, int alloc, const char *fmt0, va_list ap)
|
|||||||
char *estr; /* pointer to last char in str */
|
char *estr; /* pointer to last char in str */
|
||||||
union arg *argtable; /* args, built due to positional arg */
|
union arg *argtable; /* args, built due to positional arg */
|
||||||
union arg statargtable[STATIC_ARG_TBL_SIZE];
|
union arg statargtable[STATIC_ARG_TBL_SIZE];
|
||||||
size_t argtablesiz;
|
|
||||||
int nextarg; /* 1-based argument index */
|
int nextarg; /* 1-based argument index */
|
||||||
va_list orgap; /* original argument pointer */
|
va_list orgap; /* original argument pointer */
|
||||||
#ifdef PRINTF_WIDE_CHAR
|
#ifdef PRINTF_WIDE_CHAR
|
||||||
@@ -442,7 +399,7 @@ xxxprintf(char **strp, size_t strsize, int alloc, const char *fmt0, va_list ap)
|
|||||||
int hold = nextarg; \
|
int hold = nextarg; \
|
||||||
if (argtable == NULL) { \
|
if (argtable == NULL) { \
|
||||||
argtable = statargtable; \
|
argtable = statargtable; \
|
||||||
__find_arguments(fmt0, orgap, &argtable, &argtablesiz); \
|
__find_arguments(fmt0, orgap, &argtable); \
|
||||||
} \
|
} \
|
||||||
nextarg = n2; \
|
nextarg = n2; \
|
||||||
val = GETARG(int); \
|
val = GETARG(int); \
|
||||||
@@ -562,7 +519,7 @@ reswitch: switch (ch) {
|
|||||||
if (argtable == NULL) {
|
if (argtable == NULL) {
|
||||||
argtable = statargtable;
|
argtable = statargtable;
|
||||||
__find_arguments(fmt0, orgap,
|
__find_arguments(fmt0, orgap,
|
||||||
&argtable, &argtablesiz);
|
&argtable);
|
||||||
}
|
}
|
||||||
goto rflag;
|
goto rflag;
|
||||||
}
|
}
|
||||||
@@ -588,7 +545,7 @@ reswitch: switch (ch) {
|
|||||||
if (argtable == NULL) {
|
if (argtable == NULL) {
|
||||||
argtable = statargtable;
|
argtable = statargtable;
|
||||||
__find_arguments(fmt0, orgap,
|
__find_arguments(fmt0, orgap,
|
||||||
&argtable, &argtablesiz);
|
&argtable);
|
||||||
}
|
}
|
||||||
goto rflag;
|
goto rflag;
|
||||||
}
|
}
|
||||||
@@ -1076,7 +1033,7 @@ finish:
|
|||||||
__freedtoa(dtoaresult);
|
__freedtoa(dtoaresult);
|
||||||
#endif
|
#endif
|
||||||
if (argtable != NULL && argtable != statargtable) {
|
if (argtable != NULL && argtable != statargtable) {
|
||||||
mmap_free(argtable, argtablesiz);
|
sudo_mmap_free(argtable);
|
||||||
argtable = NULL;
|
argtable = NULL;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
@@ -1124,8 +1081,7 @@ finish:
|
|||||||
* problematic since we have nested functions..)
|
* problematic since we have nested functions..)
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
__find_arguments(const char *fmt0, va_list ap, union arg **argtable,
|
__find_arguments(const char *fmt0, va_list ap, union arg **argtable)
|
||||||
size_t *argtablesiz)
|
|
||||||
{
|
{
|
||||||
char *fmt; /* format string */
|
char *fmt; /* format string */
|
||||||
int ch; /* character from fmt */
|
int ch; /* character from fmt */
|
||||||
@@ -1354,8 +1310,8 @@ done:
|
|||||||
* Build the argument table.
|
* Build the argument table.
|
||||||
*/
|
*/
|
||||||
if (tablemax >= STATIC_ARG_TBL_SIZE) {
|
if (tablemax >= STATIC_ARG_TBL_SIZE) {
|
||||||
*argtablesiz = sizeof(union arg) * (tablemax + 1);
|
*argtable = sudo_mmap_allocarray(tablemax + 1,
|
||||||
*argtable = mmap_alloc(*argtablesiz);
|
sizeof(union arg));
|
||||||
if (*argtable == NULL)
|
if (*argtable == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -1453,7 +1409,7 @@ overflow:
|
|||||||
|
|
||||||
finish:
|
finish:
|
||||||
if (typetable != NULL && typetable != stattypetable) {
|
if (typetable != NULL && typetable != stattypetable) {
|
||||||
mmap_free(typetable, *argtablesiz);
|
sudo_mmap_free(typetable);
|
||||||
typetable = NULL;
|
typetable = NULL;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
@@ -1472,16 +1428,16 @@ __grow_type_table(unsigned char **typetable, int *tablesize)
|
|||||||
newsize = sysconf(_SC_PAGESIZE);
|
newsize = sysconf(_SC_PAGESIZE);
|
||||||
|
|
||||||
if (*tablesize == STATIC_ARG_TBL_SIZE) {
|
if (*tablesize == STATIC_ARG_TBL_SIZE) {
|
||||||
*typetable = mmap_alloc(newsize);
|
*typetable = sudo_mmap_alloc(newsize);
|
||||||
if (*typetable == NULL)
|
if (*typetable == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
memcpy(*typetable, oldtable, *tablesize);
|
memcpy(*typetable, oldtable, *tablesize);
|
||||||
} else {
|
} else {
|
||||||
unsigned char *new = mmap_alloc(newsize);
|
unsigned char *new = sudo_mmap_alloc(newsize);
|
||||||
if (new == NULL)
|
if (new == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
memmove(new, *typetable, *tablesize);
|
memmove(new, *typetable, *tablesize);
|
||||||
mmap_free(*typetable, *tablesize);
|
sudo_mmap_free(*typetable);
|
||||||
*typetable = new;
|
*typetable = new;
|
||||||
}
|
}
|
||||||
memset(*typetable + *tablesize, T_UNUSED, (newsize - *tablesize));
|
memset(*typetable + *tablesize, T_UNUSED, (newsize - *tablesize));
|
||||||
|
Reference in New Issue
Block a user