Greenbone Vulnerability Management Libraries 22.32.0
drop_privileges.h File Reference

Privilege dropping header file. More...

#include <glib.h>
Include dependency graph for drop_privileges.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define GVM_DROP_PRIVILEGES    g_quark_from_static_string ("gvm-drop-privileges-error-quark")
 The GQuark for privilege dropping errors.
#define GVM_DROP_PRIVILEGES_ERROR_ALREADY_SET   -1
 Definition of the return code ERROR_ALREADY_SET.
#define GVM_DROP_PRIVILEGES_OK   0
 Definition of the return code OK.
#define GVM_DROP_PRIVILEGES_FAIL_NOT_ROOT   1
 Definition of the return code FAIL_NOT_ROOT.
#define GVM_DROP_PRIVILEGES_FAIL_UNKNOWN_USER   2
 Definition of the return code FAIL_UNKNOWN_USER.
#define GVM_DROP_PRIVILEGES_FAIL_DROP_GID   3
 Definition of the return code FAIL_DROP_GID.
#define GVM_DROP_PRIVILEGES_FAIL_DROP_UID   4
 Definition of the return code FAIL_DROP_UID.
#define GVM_DROP_PRIVILEGES_FAIL_SUPPLEMENTARY   5
 Definition of the return code FAIL_SUPPLEMENTARY.

Functions

int drop_privileges (gchar *username, GError **error)
 Drop privileges.

Detailed Description

Privilege dropping header file.

Definition in file drop_privileges.h.

Macro Definition Documentation

◆ GVM_DROP_PRIVILEGES

#define GVM_DROP_PRIVILEGES    g_quark_from_static_string ("gvm-drop-privileges-error-quark")

The GQuark for privilege dropping errors.

Definition at line 19 of file drop_privileges.h.

19#define GVM_DROP_PRIVILEGES \
20 g_quark_from_static_string ("gvm-drop-privileges-error-quark")

Referenced by drop_privileges(), and drop_privileges_error().

◆ GVM_DROP_PRIVILEGES_ERROR_ALREADY_SET

#define GVM_DROP_PRIVILEGES_ERROR_ALREADY_SET   -1

Definition of the return code ERROR_ALREADY_SET.

Definition at line 25 of file drop_privileges.h.

Referenced by drop_privileges().

◆ GVM_DROP_PRIVILEGES_FAIL_DROP_GID

#define GVM_DROP_PRIVILEGES_FAIL_DROP_GID   3

Definition of the return code FAIL_DROP_GID.

Definition at line 45 of file drop_privileges.h.

Referenced by drop_privileges().

◆ GVM_DROP_PRIVILEGES_FAIL_DROP_UID

#define GVM_DROP_PRIVILEGES_FAIL_DROP_UID   4

Definition of the return code FAIL_DROP_UID.

Definition at line 50 of file drop_privileges.h.

Referenced by drop_privileges().

◆ GVM_DROP_PRIVILEGES_FAIL_NOT_ROOT

#define GVM_DROP_PRIVILEGES_FAIL_NOT_ROOT   1

Definition of the return code FAIL_NOT_ROOT.

Definition at line 35 of file drop_privileges.h.

Referenced by drop_privileges().

◆ GVM_DROP_PRIVILEGES_FAIL_SUPPLEMENTARY

#define GVM_DROP_PRIVILEGES_FAIL_SUPPLEMENTARY   5

Definition of the return code FAIL_SUPPLEMENTARY.

Definition at line 55 of file drop_privileges.h.

Referenced by drop_privileges().

◆ GVM_DROP_PRIVILEGES_FAIL_UNKNOWN_USER

#define GVM_DROP_PRIVILEGES_FAIL_UNKNOWN_USER   2

Definition of the return code FAIL_UNKNOWN_USER.

Definition at line 40 of file drop_privileges.h.

Referenced by drop_privileges().

◆ GVM_DROP_PRIVILEGES_OK

#define GVM_DROP_PRIVILEGES_OK   0

Definition of the return code OK.

Definition at line 30 of file drop_privileges.h.

Referenced by drop_privileges().

Function Documentation

◆ drop_privileges()

int drop_privileges ( gchar * username,
GError ** error )

Drop privileges.

We try to drop our (root) privileges and setuid to username to minimize the risk of privilege escalation. The current implementation is linux-specific and may not work on other platforms.

Parameters
[in]usernameThe user to become. Its safe to pass "NULL", in which case it will default to "nobody".
[out]errorReturn location for errors or NULL if not interested in errors.
Returns
GVM_DROP_PRIVILEGES_OK in case of success. Sets error otherwise and returns the error code.

Definition at line 58 of file drop_privileges.c.

59{
60 g_return_val_if_fail (*error == NULL, GVM_DROP_PRIVILEGES_ERROR_ALREADY_SET);
61
62 if (username == NULL)
63 username = "nobody";
64
65 if (geteuid () == 0)
66 {
67 struct passwd *user_pw;
68
69 user_pw = getpwnam (username);
70 if (user_pw)
71 {
72 if (initgroups (username, user_pw->pw_gid) != 0)
75 "Failed to drop supplementary groups privileges!\n");
76 if (setgid (user_pw->pw_gid) != 0)
77 return drop_privileges_error (error,
79 "Failed to drop group privileges!\n");
80 if (setuid (user_pw->pw_uid) != 0)
81 return drop_privileges_error (error,
83 "Failed to drop user privileges!\n");
84 }
85 else
86 {
87 g_set_error (error, GVM_DROP_PRIVILEGES,
89 "Failed to get gid and uid for user %s.", username);
91 }
93 }
94 else
95 {
97 "Only root can drop its privileges.");
98 }
99}
static gint drop_privileges_error(GError **error, gint errorcode, const gchar *message)
Sets an error and return errorcode.
#define GVM_DROP_PRIVILEGES_FAIL_DROP_GID
Definition of the return code FAIL_DROP_GID.
#define GVM_DROP_PRIVILEGES_FAIL_UNKNOWN_USER
Definition of the return code FAIL_UNKNOWN_USER.
#define GVM_DROP_PRIVILEGES_ERROR_ALREADY_SET
Definition of the return code ERROR_ALREADY_SET.
#define GVM_DROP_PRIVILEGES_FAIL_DROP_UID
Definition of the return code FAIL_DROP_UID.
#define GVM_DROP_PRIVILEGES_FAIL_SUPPLEMENTARY
Definition of the return code FAIL_SUPPLEMENTARY.
#define GVM_DROP_PRIVILEGES_OK
Definition of the return code OK.
#define GVM_DROP_PRIVILEGES
The GQuark for privilege dropping errors.
#define GVM_DROP_PRIVILEGES_FAIL_NOT_ROOT
Definition of the return code FAIL_NOT_ROOT.

References drop_privileges_error(), GVM_DROP_PRIVILEGES, GVM_DROP_PRIVILEGES_ERROR_ALREADY_SET, GVM_DROP_PRIVILEGES_FAIL_DROP_GID, GVM_DROP_PRIVILEGES_FAIL_DROP_UID, GVM_DROP_PRIVILEGES_FAIL_NOT_ROOT, GVM_DROP_PRIVILEGES_FAIL_SUPPLEMENTARY, GVM_DROP_PRIVILEGES_FAIL_UNKNOWN_USER, and GVM_DROP_PRIVILEGES_OK.

Here is the call graph for this function: