Skip to content

Commit fd4b369

Browse files
committed
sudoers_init: initialize ret to 0, not -1
This lets us more easily tell when sudoers has already been initialized (or attempted to be initialized). sudoers_init() returns 1 on success and -1 on error (a 0 return value is never actually used). Found by the ZeroPath AI Security Engineer <https://zeropath.com>
1 parent 3226ef7 commit fd4b369

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

plugins/sudoers/audit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ sudoers_audit_open(unsigned int version, sudo_conv_t conversation,
181181
info.plugin_args = plugin_options;
182182
ret = sudoers_init(&info, log_parse_error, submit_envp);
183183

184-
if (ret == true) {
184+
if (ret == 1) {
185185
/* Unset close function if we don't need it to avoid extra process. */
186186
#ifdef SUDOERS_LOG_CLIENT
187187
if (SLIST_EMPTY(&def_log_servers))

plugins/sudoers/sudoers.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,20 @@ sudoers_reinit_defaults(struct sudoers_context *ctx)
175175
debug_return_bool(true);
176176
}
177177

178+
/*
179+
* Initialize sudoers data structures and parse sudoers sources.
180+
* Returns 1 on success and -1 on error.
181+
*/
178182
int
179183
sudoers_init(void *info, sudoers_logger_t logger, char * const envp[])
180184
{
181185
struct sudo_nss *nss, *nss_next;
182186
int oldlocale, sources = 0;
183-
static int ret = -1;
187+
static int ret = 0;
184188
debug_decl(sudoers_init, SUDOERS_DEBUG_PLUGIN);
185189

186-
/* Only initialize once. */
187-
if (snl != NULL)
190+
/* Only initialize once, don't re-initialize on error (-1). */
191+
if (ret != 0)
188192
debug_return_int(ret);
189193

190194
bindtextdomain("sudoers", LOCALEDIR);
@@ -197,29 +201,29 @@ sudoers_init(void *info, sudoers_logger_t logger, char * const envp[])
197201

198202
/* Initialize environment functions (including replacements). */
199203
if (!env_init(envp))
200-
debug_return_int(-1);
204+
goto done;
201205

202206
/* Setup defaults data structures. */
203207
if (!init_defaults()) {
204208
sudo_warnx("%s", U_("unable to initialize sudoers default values"));
205-
debug_return_int(-1);
209+
goto done;
206210
}
207211

208212
/* Parse info from front-end. */
209213
sudoers_ctx.mode = sudoers_policy_deserialize_info(&sudoers_ctx, info,
210214
&initial_defaults);
211215
if (ISSET(sudoers_ctx.mode, MODE_ERROR))
212-
debug_return_int(-1);
216+
goto done;
213217

214218
if (!init_vars(&sudoers_ctx, envp))
215-
debug_return_int(-1);
219+
goto done;
216220

217221
/* Parse nsswitch.conf for sudoers order. */
218222
snl = sudo_read_nss();
219223

220224
/* LDAP or NSS may modify the euid so we need to be root for the open. */
221225
if (!set_perms(NULL, PERM_ROOT))
222-
debug_return_int(-1);
226+
goto done;
223227

224228
/* Use the C locale unless another is specified in sudoers. */
225229
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
@@ -257,7 +261,7 @@ sudoers_init(void *info, sudoers_logger_t logger, char * const envp[])
257261

258262
/* Set login class if applicable (after sudoers is parsed). */
259263
if (set_loginclass(&sudoers_ctx))
260-
ret = true;
264+
ret = 1;
261265

262266
cleanup:
263267
mail_parse_errors(&sudoers_ctx);
@@ -269,6 +273,9 @@ sudoers_init(void *info, sudoers_logger_t logger, char * const envp[])
269273
sudo_warn_set_locale_func(NULL);
270274
sudoers_setlocale(oldlocale, NULL);
271275

276+
done:
277+
if (ret == 0)
278+
ret = -1;
272279
debug_return_int(ret);
273280
}
274281

0 commit comments

Comments
 (0)