Skip to content

Commit a294a8b

Browse files
committed
check_user: refactor the "running as self" check into its own function
1 parent 28837b2 commit a294a8b

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

plugins/sudoers/check.c

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ get_authpw(struct sudoers_context *ctx, unsigned int mode)
8686
debug_return_ptr(pw);
8787
}
8888

89+
/*
90+
* Returns true if the user is running the command as themselves
91+
* and no SELinux type/role, AppArmor profile or Solaris privilege
92+
* was specified.
93+
*/
94+
static bool
95+
running_as_user(struct sudoers_context *ctx)
96+
{
97+
return ctx->user.uid == ctx->runas.pw->pw_uid && (ctx->runas.gr == NULL ||
98+
user_in_group(ctx->user.pw, ctx->runas.gr->gr_name)) &&
99+
ctx->runas.role == NULL && ctx->runas.type == NULL &&
100+
ctx->runas.apparmor_profile == NULL &&
101+
ctx->runas.privs == NULL && ctx->runas.limitprivs == NULL;
102+
}
103+
89104
/*
90105
* Returns AUTH_SUCCESS if the user successfully authenticates,
91106
* AUTH_FAILURE if not or AUTH_ERROR on error.
@@ -124,29 +139,22 @@ check_user(struct sudoers_context *ctx, unsigned int validated,
124139
}
125140
closure.ctx = ctx;
126141

127-
/*
128-
* Don't prompt for the root passwd or if the user is exempt.
129-
* If the user is not changing uid/gid, no need for a password.
130-
*/
131142
if (!def_authenticate || user_is_exempt(ctx)) {
132143
sudo_debug_printf(SUDO_DEBUG_INFO, "%s: %s", __func__,
133144
!def_authenticate ? "authentication disabled" :
134145
"user exempt from authentication");
135146
exempt = true;
136-
ret = AUTH_SUCCESS;
137-
goto done;
147+
goto success;
138148
}
139-
if (ctx->user.uid == 0 || (ISSET(mode, MODE_RUN|MODE_EDIT) &&
140-
ctx->user.uid == ctx->runas.pw->pw_uid && (ctx->runas.gr == NULL ||
141-
user_in_group(ctx->user.pw, ctx->runas.gr->gr_name)))) {
142-
if (ctx->runas.role == NULL && ctx->runas.type == NULL &&
143-
ctx->runas.apparmor_profile == NULL &&
144-
ctx->runas.privs == NULL && ctx->runas.limitprivs == NULL) {
145-
sudo_debug_printf(SUDO_DEBUG_INFO,
146-
"%s: user running command as self", __func__);
147-
ret = AUTH_SUCCESS;
148-
goto done;
149-
}
149+
if (ctx->user.uid == ROOT_UID) {
150+
/* Do not prompt for the root password. */
151+
goto success;
152+
}
153+
if ((ISSET(mode, MODE_RUN|MODE_EDIT) && running_as_user(ctx))) {
154+
/* If the user is not changing uid/gid, no need for a password. */
155+
sudo_debug_printf(SUDO_DEBUG_INFO,
156+
"%s: user running command as self", __func__);
157+
goto success;
150158
}
151159

152160
/* Construct callback for getpass function. */
@@ -201,8 +209,8 @@ check_user(struct sudoers_context *ctx, unsigned int validated,
201209
break;
202210
}
203211

204-
done:
205212
if (ret == AUTH_SUCCESS) {
213+
success:
206214
/* The approval function may disallow a user post-authentication. */
207215
ret = sudo_auth_approval(ctx, closure.auth_pw, validated, exempt);
208216

@@ -215,6 +223,7 @@ check_user(struct sudoers_context *ctx, unsigned int validated,
215223
(void)timestamp_update(closure.cookie, closure.auth_pw);
216224
}
217225
}
226+
done:
218227
timestamp_close(closure.cookie);
219228
sudo_auth_cleanup(ctx, closure.auth_pw, !ISSET(validated, VALIDATE_SUCCESS));
220229
sudo_pw_delref(closure.auth_pw);

0 commit comments

Comments
 (0)