From d4211081c0fc2bf6be9d1aaf7258b7a0cb8129c5 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Tue, 14 Jul 2015 15:28:01 -0600 Subject: [PATCH] Add some debugging printfs when malloc fails and we don't have an explicit call to sudo_warnx(). --- lib/util/lbuf.c | 8 +++-- plugins/sudoers/env.c | 15 +++++++- plugins/sudoers/gram.c | 67 ++++++++++++++++++++++------------- plugins/sudoers/gram.y | 63 ++++++++++++++++++++------------ plugins/sudoers/interfaces.c | 5 ++- plugins/sudoers/match.c | 4 +++ plugins/sudoers/pwutil_impl.c | 27 +++++++++++--- plugins/sudoers/redblack.c | 45 +++++++++++++---------- src/hooks.c | 5 ++- src/net_ifs.c | 9 ++++- src/sudo.c | 15 ++++---- 11 files changed, 179 insertions(+), 84 deletions(-) diff --git a/lib/util/lbuf.c b/lib/util/lbuf.c index 0e56ba36f..55ea422dc 100644 --- a/lib/util/lbuf.c +++ b/lib/util/lbuf.c @@ -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); } /* diff --git a/plugins/sudoers/env.c b/plugins/sudoers/env.c index 848a7e6bf..4a91ac850 100644 --- a/plugins/sudoers/env.c +++ b/plugins/sudoers/env.c @@ -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); } diff --git a/plugins/sudoers/gram.c b/plugins/sudoers/gram.c index 632fbea4f..35bdfe160 100644 --- a/plugins/sudoers/gram.c +++ b/plugins/sudoers/gram.c @@ -740,16 +740,19 @@ 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) { - d->var = var; - d->val = val; - /* d->type = 0; */ - d->op = op; - /* d->binding = NULL */ - HLTQ_INIT(d, entries); + 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,13 +762,16 @@ 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) { - m->name = name; - m->type = type; - HLTQ_INIT(m, entries); + 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,14 +781,19 @@ 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) { - dig->digest_type = digest_type; - dig->digest_str = strdup(digest_str); - if (dig->digest_str == NULL) { - free(dig); - 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; diff --git a/plugins/sudoers/gram.y b/plugins/sudoers/gram.y index 0b9a249f0..82604ba82 100644 --- a/plugins/sudoers/gram.y +++ b/plugins/sudoers/gram.y @@ -885,16 +885,19 @@ 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) { - d->var = var; - d->val = val; - /* d->type = 0; */ - d->op = op; - /* d->binding = NULL */ - HLTQ_INIT(d, entries); + 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,13 +907,16 @@ 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) { - m->name = name; - m->type = type; - HLTQ_INIT(m, entries); + 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,14 +926,19 @@ 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) { - dig->digest_type = digest_type; - dig->digest_str = strdup(digest_str); - if (dig->digest_str == NULL) { - free(dig); - 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); diff --git a/plugins/sudoers/interfaces.c b/plugins/sudoers/interfaces.c index 8643e3146..fda41669f 100644 --- a/plugins/sudoers/interfaces.c +++ b/plugins/sudoers/interfaces.c @@ -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 diff --git a/plugins/sudoers/match.c b/plugins/sudoers/match.c index 5411e3854..524ab5bd1 100644 --- a/plugins/sudoers/match.c +++ b/plugins/sudoers/match.c @@ -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; } diff --git a/plugins/sudoers/pwutil_impl.c b/plugins/sudoers/pwutil_impl.c index 03e61df48..f081ef5ab 100644 --- a/plugins/sudoers/pwutil_impl.c +++ b/plugins/sudoers/pwutil_impl.c @@ -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); } diff --git a/plugins/sudoers/redblack.c b/plugins/sudoers/redblack.c index a12085701..793f980fe 100644 --- a/plugins/sudoers/redblack.c +++ b/plugins/sudoers/redblack.c @@ -84,26 +84,30 @@ rbcreate(int (*compar)(const void *, const void*)) struct rbtree *tree; debug_decl(rbcreate, SUDOERS_DEBUG_RBTREE) - if ((tree = malloc(sizeof(*tree))) != NULL) { - tree->compar = compar; - - /* - * We use a self-referencing sentinel node called nil to simplify the - * code by avoiding the need to check for NULL pointers. - */ - tree->nil.left = tree->nil.right = tree->nil.parent = &tree->nil; - tree->nil.color = black; - tree->nil.data = NULL; - - /* - * Similarly, the fake root node keeps us from having to worry - * about splitting the root. - */ - tree->root.left = tree->root.right = tree->root.parent = &tree->nil; - tree->root.color = black; - tree->root.data = 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; + + /* + * We use a self-referencing sentinel node called nil to simplify the + * code by avoiding the need to check for NULL pointers. + */ + tree->nil.left = tree->nil.right = tree->nil.parent = &tree->nil; + tree->nil.color = black; + tree->nil.data = NULL; + + /* + * Similarly, the fake root node keeps us from having to worry + * about splitting the root. + */ + 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; diff --git a/src/hooks.c b/src/hooks.c index ae918b314..6c867094c 100644 --- a/src/hooks.c +++ b/src/hooks.c @@ -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); diff --git a/src/net_ifs.c b/src/net_ifs.c index 3f9fe6db2..f186d41af 100644 --- a/src/net_ifs.c +++ b/src/net_ifs.c @@ -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; } diff --git a/src/sudo.c b/src/sudo.c index 9a80c2df2..0c695df04 100644 --- a/src/sudo.c +++ b/src/sudo.c @@ -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; -- 2.40.0