Skip to content

Commit

Permalink
KRB5_CHILD: keep 'set-user-ID' in k5c_become_user()
Browse files Browse the repository at this point in the history
Keep saved set-user-ID in `k5c_become_user()` so that 'sssd_be'
running under SSSD_USER could signal it.

Resolves: #5536
  • Loading branch information
alexey-tikhonov committed Apr 10, 2024
1 parent 9c47251 commit f55460b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/monitor/monitor_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ int bootstrap_monitor_process(uid_t target_uid, gid_t target_gid)
*/
sss_log(SSS_LOG_WARNING, "'sssd.conf::"CONFDB_MONITOR_USER_RUNAS"' "
"option is deprecated. Run under '"SSSD_USER"' initially instead.");
ret = become_user(target_uid, target_gid); /* drops all caps */
ret = become_user(target_uid, target_gid, false); /* drops all caps */
if (ret != 0) {
sss_log(SSS_LOG_ALERT, "Failed to change uid:gid");
return 1;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/krb5/krb5_child.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ static errno_t k5c_become_user(uid_t uid, gid_t gid, bool is_posix)
"Will not drop privileges for a non-POSIX user\n");
return EOK;
}
return become_user(uid, gid);
return become_user(uid, gid, true);
}

static krb5_error_code set_lifetime_options(struct cli_opts *cli_opts,
Expand Down
4 changes: 2 additions & 2 deletions src/tests/cwrap/test_become_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void test_become_user(void **state)
pid = fork();
if (pid == 0) {
/* Change the UID in a child */
ret = become_user(sssd->pw_uid, sssd->pw_gid);
ret = become_user(sssd->pw_uid, sssd->pw_gid, false);
assert_int_equal(ret, EOK);

/* Make sure we have the requested UID and GID now and there
Expand All @@ -55,7 +55,7 @@ void test_become_user(void **state)
assert_int_equal(getgid(), sssd->pw_gid);

/* Another become_user is a no-op */
ret = become_user(sssd->pw_uid, sssd->pw_gid);
ret = become_user(sssd->pw_uid, sssd->pw_gid, false);
assert_int_equal(ret, EOK);

assert_int_equal(getgroups(0, NULL), 0);
Expand Down
19 changes: 11 additions & 8 deletions src/util/become_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
#include "util/util.h"
#include <grp.h>

errno_t become_user(uid_t uid, gid_t gid)
errno_t become_user(uid_t uid, gid_t gid, bool keep_set_uid)
{
uid_t cuid;
int ret;
int ret = EOK;

DEBUG(SSSDBG_FUNC_DATA,
"Trying to become user [%"SPRIuid"][%"SPRIgid"].\n", uid, gid);
Expand All @@ -37,7 +37,7 @@ errno_t become_user(uid_t uid, gid_t gid)
cuid = geteuid();
if (uid == cuid) {
DEBUG(SSSDBG_FUNC_DATA, "Already user [%"SPRIuid"].\n", uid);
return EOK;
goto done;
}

/* drop supplementary groups first */
Expand All @@ -46,7 +46,7 @@ errno_t become_user(uid_t uid, gid_t gid)
ret = errno;
DEBUG(SSSDBG_CRIT_FAILURE,
"setgroups failed [%d][%s].\n", ret, strerror(ret));
return ret;
goto done;
}

/* change GID so that root cannot be regained (changes saved GID too) */
Expand All @@ -55,20 +55,23 @@ errno_t become_user(uid_t uid, gid_t gid)
ret = errno;
DEBUG(SSSDBG_CRIT_FAILURE,
"setresgid failed [%d][%s].\n", ret, strerror(ret));
return ret;
goto done;
}

/* change UID so that root cannot be regained (changes saved UID too) */
/* this call also takes care of dropping CAP_SETUID, so this is a PNR */
ret = setresuid(uid, uid, uid);
ret = setresuid(uid, uid, (keep_set_uid ? -1 : uid));
if (ret == -1) {
ret = errno;
DEBUG(SSSDBG_CRIT_FAILURE,
"setresuid failed [%d][%s].\n", ret, strerror(ret));
return ret;
goto done;
}

return EOK;
done:
sss_drop_all_caps();

return ret;
}

struct sss_creds {
Expand Down
2 changes: 1 addition & 1 deletion src/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ errno_t mod_defaults_list(TALLOC_CTX *mem_ctx, const char **defaults_list,
char **mod_list, char ***_list);

/* from become_user.c */
errno_t become_user(uid_t uid, gid_t gid);
errno_t become_user(uid_t uid, gid_t gid, bool keep_set_uid);
struct sss_creds;
errno_t switch_creds(TALLOC_CTX *mem_ctx,
uid_t uid, gid_t gid,
Expand Down

0 comments on commit f55460b

Please sign in to comment.