Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/lib/iolog/iolog_swapids.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2009-2021 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#include <config.h>
20
21
#include <stdio.h>
22
#include <stdlib.h>
23
#ifdef HAVE_STDBOOL_H
24
# include <stdbool.h>
25
#else
26
# include <compat/stdbool.h>
27
#endif
28
#include <time.h>
29
#include <unistd.h>
30
31
#include <sudo_compat.h>
32
#include <sudo_debug.h>
33
#include <sudo_fatal.h>
34
#include <sudo_gettext.h>
35
#include <sudo_iolog.h>
36
37
/*
38
* Set effective user and group-IDs to iolog_uid and iolog_gid.
39
* If restore flag is set, swap them back.
40
*/
41
bool
42
iolog_swapids(bool restore)
43
{
44
#ifdef HAVE_SETEUID
45
static uid_t user_euid = (uid_t)-1;
46
static gid_t user_egid = (gid_t)-1;
47
const uid_t iolog_uid = iolog_get_uid();
48
const gid_t iolog_gid = iolog_get_gid();
49
debug_decl(io_swapids, SUDO_DEBUG_UTIL);
50
51
if (user_euid == (uid_t)-1)
52
user_euid = geteuid();
53
if (user_egid == (gid_t)-1)
54
user_egid = getegid();
55
56
if (user_euid == iolog_uid && user_egid == iolog_gid) {
57
sudo_debug_printf(SUDO_DEBUG_NOTICE,
58
"%s: effective uid/gid matches iolog uid/gid, nothing to do",
59
__func__);
60
debug_return_bool(true);
61
}
62
63
if (restore) {
64
if (seteuid(user_euid) == -1) {
65
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
66
"%s: unable to restore effective uid to %d", __func__,
67
(int)user_euid);
68
sudo_warn("seteuid() %d -> %d", (int)iolog_uid, (int)user_euid);
69
debug_return_bool(false);
70
}
71
if (setegid(user_egid) == -1) {
72
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
73
"%s: unable to restore effective gid to %d", __func__,
74
(int)user_egid);
75
sudo_warn("setegid() %d -> %d", (int)iolog_gid, (int)user_egid);
76
debug_return_bool(false);
77
}
78
} else {
79
/* Fail silently if the user has insufficient privileges. */
80
if (setegid(iolog_gid) == -1) {
81
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
82
"%s: unable to set effective gid to %d", __func__,
83
(int)iolog_gid);
84
debug_return_bool(false);
85
}
86
if (seteuid(iolog_uid) == -1) {
87
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
88
"%s: unable to set effective uid to %d", __func__,
89
(int)iolog_uid);
90
debug_return_bool(false);
91
}
92
}
93
debug_return_bool(true);
94
#else
95
return false;
96
#endif
97
}
98
99