2 * Routines for handling privileges, e.g. set-UID and set-GID on UNIX.
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 2006 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #if defined(HAVE_SETRESUID) || defined(HAVE_SETREGUID)
28 #define _GNU_SOURCE /* Otherwise [sg]etres[gu]id won't be defined on Linux */
33 #include "privileges.h"
41 * Called when the program starts, to save whatever credential information
42 * we'll need later, and to do whatever other specialized platform-dependent
43 * initialization we want.
46 init_process_policies(void)
48 HMODULE kernel32Handle;
49 typedef BOOL (WINAPI *SetProcessDEPPolicyHandler)(DWORD);
50 SetProcessDEPPolicyHandler PSetProcessDEPPolicy;
52 #ifndef PROCESS_DEP_ENABLE
53 #define PROCESS_DEP_ENABLE 1
57 * If we have SetProcessDEPPolicy(), turn "data execution
58 * prevention" on - i.e., if the MMU lets you set execute
59 * permission on a per-page basis, turn execute permission
60 * off on most data pages. PSetProcessDEPPolicy() fails on
61 * 64-bit Windows (it's *always* on there), but if it fails,
62 * we don't care (we did our best), so we don't check for
65 * XXX - if the GetModuleHandle() call fails, should we report
66 * an error? That "shouldn't happen" - it's the equivalent
67 * of libc.{so,sl,a} or libSystem.dylib being missing on UN*X.
69 kernel32Handle = GetModuleHandle(_T("kernel32.dll"));
70 if (kernel32Handle != NULL) {
71 PSetProcessDEPPolicy = (SetProcessDEPPolicyHandler) GetProcAddress(kernel32Handle, "SetProcessDEPPolicy");
72 if (PSetProcessDEPPolicy) {
73 PSetProcessDEPPolicy(PROCESS_DEP_ENABLE);
81 * For now, we say the program wasn't started with special privileges.
82 * There are ways of running programs with credentials other than those
83 * for the session in which it's run, but I don't know whether that'd be
84 * done with Wireshark/TShark or not.
87 started_with_special_privs(void)
93 * For now, we say the program isn't running with special privileges.
94 * There are ways of running programs with credentials other than those
95 * for the session in which it's run, but I don't know whether that'd be
96 * done with Wireshark/TShark or not.
99 running_with_special_privs(void)
105 * For now, we don't do anything when asked to relinquish special privileges.
108 relinquish_special_privs_perm(void)
113 * Get the current username. String must be g_free()d after use.
116 get_cur_username(void) {
118 username = g_strdup("UNKNOWN");
123 * Get the current group. String must be g_free()d after use.
126 get_cur_groupname(void) {
128 groupname = g_strdup("UNKNOWN");
133 * If npf.sys is running, return TRUE.
136 npf_sys_is_running() {
137 SC_HANDLE h_scm, h_serv;
140 h_scm = OpenSCManager(NULL, NULL, 0);
144 h_serv = OpenService(h_scm, _T("npf"), SC_MANAGER_CONNECT|SERVICE_QUERY_STATUS);
148 if (QueryServiceStatus(h_serv, &ss)) {
149 if (ss.dwCurrentState & SERVICE_RUNNING)
158 #ifdef HAVE_SYS_TYPES_H
159 # include <sys/types.h>
178 static uid_t ruid, euid;
179 static gid_t rgid, egid;
180 static gboolean init_process_policies_called = FALSE;
183 * Called when the program starts, to save whatever credential information
184 * we'll need later, and to do whatever other specialized platform-dependent
185 * initialization we want.
187 * The credential information we'll need later on UNIX is the real and
188 * effective UID and GID.
190 * XXX - do any UN*Xes have opt-in "no execute on data pages by default"
191 * permission? This would be the place to request it.
194 init_process_policies(void)
201 init_process_policies_called = TRUE;
205 * "Started with special privileges" means "started out set-UID or set-GID",
206 * or run as the root user or group.
209 started_with_special_privs(void)
211 g_assert(init_process_policies_called);
212 #ifdef HAVE_ISSETUGID
215 return (ruid != euid || rgid != egid || ruid == 0 || rgid == 0);
220 * Return TRUE if the real, effective, or saved (if we can check it) user
224 running_with_special_privs(void)
226 #ifdef HAVE_SETRESUID
229 #ifdef HAVE_SETRESGID
233 #ifdef HAVE_SETRESUID
234 getresuid(&ru, &eu, &su);
235 if (ru == 0 || eu == 0 || su == 0)
238 if (getuid() == 0 || geteuid() == 0)
241 #ifdef HAVE_SETRESGID
242 getresgid(&rg, &eg, &sg);
243 if (rg == 0 || eg == 0 || sg == 0)
246 if (getgid() == 0 || getegid() == 0)
253 * Permanently relinquish set-UID and set-GID privileges.
254 * If error, abort since we probably shouldn't continue
255 * with elevated privileges.
256 * Note that if this error occurs when dumpcap is called from
257 * wireshark or tshark, the message seen will be
258 * "Child dumpcap process died:". This is obscure but we'll
259 * consider it acceptable since it should be highly unlikely
260 * that this error will occur.
264 setxid_fail(const gchar *str)
266 g_error("Attempt to relinguish privileges failed [%s()] - aborting: %s\n",
267 str, g_strerror(errno));
271 relinquish_special_privs_perm(void)
274 * If we were started with special privileges, set the
275 * real and effective group and user IDs to the original
276 * values of the real and effective group and user IDs.
277 * If we're not, don't bother - doing so seems to mung
278 * our group set, at least in OS X 10.5.
280 * (Set the effective UID last - that takes away our
281 * rights to set anything else.)
283 if (started_with_special_privs()) {
284 #ifdef HAVE_SETRESGID
285 if (setresgid(rgid, rgid, rgid) == -1) {setxid_fail("setresgid");}
287 if (setgid(rgid) == -1) {setxid_fail("setgid"); }
288 if (setegid(rgid) == -1) {setxid_fail("setegid");}
291 #ifdef HAVE_SETRESUID
292 if (setresuid(ruid, ruid, ruid) == -1) {setxid_fail("setresuid");}
294 if (setuid(ruid) == -1) {setxid_fail("setuid"); }
295 if (seteuid(ruid) == -1) {setxid_fail("seteuid");}
301 * Get the current username. String must be g_free()d after use.
304 get_cur_username(void) {
306 struct passwd *pw = getpwuid(getuid());
309 username = g_strdup(pw->pw_name);
311 username = g_strdup("UNKNOWN");
318 * Get the current group. String must be g_free()d after use.
321 get_cur_groupname(void) {
323 struct group *gr = getgrgid(getgid());
326 groupname = g_strdup(gr->gr_name);
328 groupname = g_strdup("UNKNOWN");
342 * indent-tabs-mode: t
345 * ex: set shiftwidth=8 tabstop=8 noexpandtab:
346 * :indentSize=8:tabSize=8:noTabs=false: