Add some debugging printfs when malloc fails and we don't have an

explicit call to sudo_warnx().
This commit is contained in:
Todd C. Miller
2015-07-14 15:28:01 -06:00
parent 108bfb7af3
commit d4211081c0
11 changed files with 179 additions and 84 deletions

View File

@@ -66,6 +66,8 @@ sudo_lbuf_destroy_v1(struct sudo_lbuf *lbuf)
static bool
sudo_lbuf_expand(struct sudo_lbuf *lbuf, int extra)
{
debug_decl(sudo_lbuf_expand, SUDO_DEBUG_UTIL)
if (lbuf->len + extra + 1 >= lbuf->size) {
char *new_buf;
int new_size = lbuf->size;
@@ -74,13 +76,15 @@ sudo_lbuf_expand(struct sudo_lbuf *lbuf, int extra)
new_size += 256;
} while (lbuf->len + extra + 1 >= new_size);
if ((new_buf = realloc(lbuf->buf, new_size)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
lbuf->error = 1;
return false;
debug_return_bool(false);
}
lbuf->buf = new_buf;
lbuf->size = new_size;
}
return true;
debug_return_bool(true);
}
/*

View File

@@ -393,8 +393,11 @@ sudo_setenv2(const char *var, const char *val, bool dupcheck, bool overwrite)
debug_decl(sudo_setenv2, SUDOERS_DEBUG_ENV)
esize = strlen(var) + 1 + strlen(val) + 1;
if ((estring = malloc(esize)) == NULL)
if ((estring = malloc(esize)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_int(-1);
}
/* Build environment string and insert it. */
if (strlcpy(estring, var, esize) >= esize ||
@@ -851,6 +854,8 @@ rebuild_env(void)
env.old_envp = env.envp;
env.envp = reallocarray(NULL, env.env_size, sizeof(char *));
if (env.envp == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
env.env_size = 0;
goto bad;
}
@@ -1178,6 +1183,8 @@ read_env_file(const char *path, int overwrite)
}
if ((cp = malloc(var_len + 1 + val_len + 1)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
/* XXX - no undo on failure */
rval = false;
break;
@@ -1208,6 +1215,8 @@ init_envtables(void)
for (p = initial_badenv_table; *p; p++) {
cur = calloc(1, sizeof(struct list_member));
if (cur == NULL || (cur->value = strdup(*p)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
free(cur);
debug_return_bool(false);
}
@@ -1218,6 +1227,8 @@ init_envtables(void)
for (p = initial_checkenv_table; *p; p++) {
cur = calloc(1, sizeof(struct list_member));
if (cur == NULL || (cur->value = strdup(*p)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
free(cur);
debug_return_bool(false);
}
@@ -1228,6 +1239,8 @@ init_envtables(void)
for (p = initial_keepenv_table; *p; p++) {
cur = calloc(1, sizeof(struct list_member));
if (cur == NULL || (cur->value = strdup(*p)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
free(cur);
debug_return_bool(false);
}

View File

@@ -740,15 +740,18 @@ new_default(char *var, char *val, int op)
struct defaults *d;
debug_decl(new_default, SUDOERS_DEBUG_PARSER)
d = calloc(1, sizeof(struct defaults));
if (d != NULL) {
if ((d = calloc(1, sizeof(struct defaults))) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_ptr(NULL);
}
d->var = var;
d->val = val;
/* d->type = 0; */
d->op = op;
/* d->binding = NULL */
HLTQ_INIT(d, entries);
}
debug_return_ptr(d);
}
@@ -759,12 +762,15 @@ new_member(char *name, int type)
struct member *m;
debug_decl(new_member, SUDOERS_DEBUG_PARSER)
m = calloc(1, sizeof(struct member));
if (m != NULL) {
if ((m = calloc(1, sizeof(struct member))) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_ptr(NULL);
}
m->name = name;
m->type = type;
HLTQ_INIT(m, entries);
}
debug_return_ptr(m);
}
@@ -775,15 +781,20 @@ new_digest(int digest_type, const char *digest_str)
struct sudo_digest *dig;
debug_decl(new_digest, SUDOERS_DEBUG_PARSER)
dig = malloc(sizeof(*dig));
if (dig != NULL) {
if ((dig = malloc(sizeof(*dig))) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_ptr(NULL);
}
dig->digest_type = digest_type;
dig->digest_str = strdup(digest_str);
if (dig->digest_str == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
free(dig);
dig = NULL;
}
}
debug_return_ptr(dig);
}
@@ -804,8 +815,11 @@ add_defaults(int type, struct member *bmem, struct defaults *defs)
/*
* We use a single binding for each entry in defs.
*/
if ((binding = malloc(sizeof(*binding))) == NULL)
if ((binding = malloc(sizeof(*binding))) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_bool(false);
}
if (bmem != NULL)
HLTQ_TO_TAILQ(binding, bmem, entries);
else
@@ -835,8 +849,11 @@ add_userspec(struct member *members, struct privilege *privs)
struct userspec *u;
debug_decl(add_userspec, SUDOERS_DEBUG_PARSER)
if ((u = calloc(1, sizeof(*u))) == NULL)
if ((u = calloc(1, sizeof(*u))) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_bool(false);
}
HLTQ_TO_TAILQ(&u->users, members, entries);
HLTQ_TO_TAILQ(&u->privileges, privs, entries);
TAILQ_INSERT_TAIL(&userspecs, u, entries);
@@ -983,7 +1000,7 @@ init_parser(const char *path, bool quiet)
debug_return_bool(rval);
}
#line 934 "gram.c"
#line 951 "gram.c"
/* allocate initial stack or double stack size, up to YYMAXDEPTH */
#if defined(__cplusplus) || defined(__STDC__)
static int yygrowstack(void)
@@ -2063,7 +2080,7 @@ case 113:
}
}
break;
#line 2014 "gram.c"
#line 2031 "gram.c"
}
yyssp -= yym;
yystate = *yyssp;

View File

@@ -885,15 +885,18 @@ new_default(char *var, char *val, int op)
struct defaults *d;
debug_decl(new_default, SUDOERS_DEBUG_PARSER)
d = calloc(1, sizeof(struct defaults));
if (d != NULL) {
if ((d = calloc(1, sizeof(struct defaults))) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_ptr(NULL);
}
d->var = var;
d->val = val;
/* d->type = 0; */
d->op = op;
/* d->binding = NULL */
HLTQ_INIT(d, entries);
}
debug_return_ptr(d);
}
@@ -904,12 +907,15 @@ new_member(char *name, int type)
struct member *m;
debug_decl(new_member, SUDOERS_DEBUG_PARSER)
m = calloc(1, sizeof(struct member));
if (m != NULL) {
if ((m = calloc(1, sizeof(struct member))) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_ptr(NULL);
}
m->name = name;
m->type = type;
HLTQ_INIT(m, entries);
}
debug_return_ptr(m);
}
@@ -920,15 +926,20 @@ new_digest(int digest_type, const char *digest_str)
struct sudo_digest *dig;
debug_decl(new_digest, SUDOERS_DEBUG_PARSER)
dig = malloc(sizeof(*dig));
if (dig != NULL) {
if ((dig = malloc(sizeof(*dig))) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_ptr(NULL);
}
dig->digest_type = digest_type;
dig->digest_str = strdup(digest_str);
if (dig->digest_str == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
free(dig);
dig = NULL;
}
}
debug_return_ptr(dig);
}
@@ -949,8 +960,11 @@ add_defaults(int type, struct member *bmem, struct defaults *defs)
/*
* We use a single binding for each entry in defs.
*/
if ((binding = malloc(sizeof(*binding))) == NULL)
if ((binding = malloc(sizeof(*binding))) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_bool(false);
}
if (bmem != NULL)
HLTQ_TO_TAILQ(binding, bmem, entries);
else
@@ -980,8 +994,11 @@ add_userspec(struct member *members, struct privilege *privs)
struct userspec *u;
debug_decl(add_userspec, SUDOERS_DEBUG_PARSER)
if ((u = calloc(1, sizeof(*u))) == NULL)
if ((u = calloc(1, sizeof(*u))) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_bool(false);
}
HLTQ_TO_TAILQ(&u->users, members, entries);
HLTQ_TO_TAILQ(&u->privileges, privs, entries);
TAILQ_INSERT_TAIL(&userspecs, u, entries);

View File

@@ -66,8 +66,11 @@ set_interfaces(const char *ai)
*mask++ = '\0';
/* Parse addr and store in list. */
if ((ifp = calloc(1, sizeof(*ifp))) == NULL)
if ((ifp = calloc(1, sizeof(*ifp))) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
goto done;
}
if (strchr(addr, ':')) {
/* IPv6 */
#ifdef HAVE_STRUCT_IN6_ADDR

View File

@@ -932,6 +932,10 @@ sudo_getdomainname(void)
}
}
}
} else {
/* XXX - want to pass error back to caller */
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
}
initialized = true;
}

View File

@@ -109,8 +109,11 @@ sudo_make_pwitem(uid_t uid, const char *name)
total += strlen(name) + 1;
/* Allocate space for struct item, struct passwd and the strings. */
if ((pwitem = calloc(1, total)) == NULL)
if ((pwitem = calloc(1, total)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_ptr(NULL);
}
newpw = &pwitem->pw;
/*
@@ -181,8 +184,11 @@ sudo_make_gritem(gid_t gid, const char *name)
if (name != NULL)
total += strlen(name) + 1;
if ((gritem = calloc(1, total)) == NULL)
if ((gritem = calloc(1, total)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_ptr(NULL);
}
/*
* Copy in group contents and make strings relative to space
@@ -245,21 +251,30 @@ sudo_make_grlist_item(const struct passwd *pw, char * const *unused1,
if (sudo_user.max_groups > 0) {
ngids = sudo_user.max_groups;
gids = reallocarray(NULL, ngids, sizeof(GETGROUPS_T));
if (gids == NULL)
if (gids == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_ptr(NULL);
}
(void)getgrouplist(pw->pw_name, pw->pw_gid, gids, &ngids);
} else {
ngids = (int)sysconf(_SC_NGROUPS_MAX) * 2;
if (ngids < 0)
ngids = NGROUPS_MAX * 2;
gids = reallocarray(NULL, ngids, sizeof(GETGROUPS_T));
if (gids == NULL)
if (gids == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_ptr(NULL);
}
if (getgrouplist(pw->pw_name, pw->pw_gid, gids, &ngids) == -1) {
free(gids);
gids = reallocarray(NULL, ngids, sizeof(GETGROUPS_T));
if (gids == NULL)
if (gids == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_ptr(NULL);
}
if (getgrouplist(pw->pw_name, pw->pw_gid, gids, &ngids) == -1)
ngids = -1;
}
@@ -286,6 +301,8 @@ sudo_make_grlist_item(const struct passwd *pw, char * const *unused1,
again:
if ((grlitem = calloc(1, total)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
free(gids);
debug_return_ptr(NULL);
}

View File

@@ -84,7 +84,12 @@ rbcreate(int (*compar)(const void *, const void*))
struct rbtree *tree;
debug_decl(rbcreate, SUDOERS_DEBUG_RBTREE)
if ((tree = malloc(sizeof(*tree))) != NULL) {
if ((tree = malloc(sizeof(*tree))) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_ptr(NULL);
}
tree->compar = compar;
/*
@@ -102,7 +107,6 @@ rbcreate(int (*compar)(const void *, const void*))
tree->root.left = tree->root.right = tree->root.parent = &tree->nil;
tree->root.color = black;
tree->root.data = NULL;
}
debug_return_ptr(tree);
}
@@ -184,8 +188,11 @@ rbinsert(struct rbtree *tree, void *data, struct rbnode **existing)
}
node = malloc(sizeof(*node));
if (node == NULL)
if (node == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_int(-1);
}
node->data = data;
node->left = node->right = rbnil(tree);
node->parent = parent;

View File

@@ -131,8 +131,11 @@ register_hook_internal(struct sudo_hook_list *head,
struct sudo_hook_entry *hook;
debug_decl(register_hook_internal, SUDO_DEBUG_HOOKS)
if ((hook = calloc(1, sizeof(*hook))) == NULL)
if ((hook = calloc(1, sizeof(*hook))) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_int(-1);
}
hook->u.generic_fn = hook_fn;
hook->closure = closure;
SLIST_INSERT_HEAD(head, hook, entries);

View File

@@ -144,8 +144,11 @@ get_net_ifs(char **addrinfo)
if (num_interfaces == 0)
debug_return_int(0);
ailen = num_interfaces * 2 * INET6_ADDRSTRLEN;
if ((cp = malloc(ailen)) == NULL)
if ((cp = malloc(ailen)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
debug_return_int(-1);
}
*addrinfo = cp;
/* Store the IP addr/netmask pairs. */
@@ -236,6 +239,8 @@ get_net_ifs(char **addrinfo)
*/
for (;;) {
if ((ifconf_buf = malloc(buflen)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
num_interfaces = -1;
goto done;
}
@@ -264,6 +269,8 @@ get_net_ifs(char **addrinfo)
goto done;
ailen = n * 2 * INET6_ADDRSTRLEN;
if ((cp = malloc(ailen)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
num_interfaces = -1;
goto done;
}

View File

@@ -458,8 +458,11 @@ get_user_info(struct user_details *ud)
/* XXX - bound check number of entries */
user_info = reallocarray(NULL, 32, sizeof(char *));
if (user_info == NULL)
if (user_info == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
goto bad;
}
ud->pid = getpid();
ud->ppid = getppid();
@@ -753,7 +756,7 @@ command_info_to_details(char * const info[], struct command_details *details)
#endif
details->pw = getpwuid(details->euid);
if (details->pw != NULL && (details->pw = pw_dup(details->pw)) == NULL)
sudo_fatal(NULL);
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
#ifdef HAVE_SETAUTHDB
aix_restoreauthdb();
#endif
@@ -1135,15 +1138,15 @@ format_plugin_settings(struct plugin_container *plugin,
plugin_settings = ps =
reallocarray(NULL, plugin_settings_size, sizeof(char *));
if (plugin_settings == NULL)
sudo_fatal(NULL);
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
if ((*ps++ = sudo_new_key_val("plugin_path", plugin->path)) == NULL)
sudo_fatal(NULL);
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
for (setting = sudo_settings; setting->name != NULL; setting++) {
if (setting->value != NULL) {
sudo_debug_printf(SUDO_DEBUG_INFO, "settings: %s=%s",
setting->name, setting->value);
if ((*ps++ = sudo_new_key_val(setting->name, setting->value)) == NULL)
sudo_fatal(NULL);
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
}
}
if (plugin->debug_files != NULL) {
@@ -1151,7 +1154,7 @@ format_plugin_settings(struct plugin_container *plugin,
/* XXX - quote filename? */
if (asprintf(ps++, "debug_flags=%s %s", debug_file->debug_file,
debug_file->debug_flags) == -1)
sudo_fatal(NULL);
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
}
}
*ps = NULL;