Skip to content

Commit 2dfda85

Browse files
authored
Merge pull request #335 from greenbone/fix/gsad-chroot-working-directory
fix: chdir("/") after chroot to fix static path resolution
2 parents a7e6592 + 3074514 commit 2dfda85

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/gsad.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ drop_privileges (struct passwd *user_pw)
12641264
static int
12651265
chroot_drop_privileges (gboolean do_chroot, const gchar *drop, const gchar *dir)
12661266
{
1267-
struct passwd *user_pw;
1267+
struct passwd *user_pw = NULL;
12681268

12691269
if (drop)
12701270
{
@@ -1277,8 +1277,6 @@ chroot_drop_privileges (gboolean do_chroot, const gchar *drop, const gchar *dir)
12771277
return 1;
12781278
}
12791279
}
1280-
else
1281-
user_pw = NULL;
12821280

12831281
if (do_chroot)
12841282
{
@@ -1290,28 +1288,34 @@ chroot_drop_privileges (gboolean do_chroot, const gchar *drop, const gchar *dir)
12901288
return 1;
12911289
}
12921290
set_chroot_state (1);
1291+
1292+
if (chdir ("/"))
1293+
{
1294+
g_critical ("failed to change to \"/\" after chroot: %s",
1295+
strerror (errno));
1296+
return 1;
1297+
}
1298+
12931299
g_info ("Chrooted to \"%s\"", dir);
12941300
}
1295-
1296-
if (user_pw)
1301+
else
12971302
{
1298-
if (drop_privileges (user_pw) == FALSE)
1303+
if (chdir (dir))
12991304
{
1300-
g_critical ("Failed to drop privileges");
1305+
g_critical ("failed to change to \"%s\": %s", dir, strerror (errno));
13011306
return 1;
13021307
}
1303-
else
1304-
g_info ("Dropped privileges to user \"%s\" (uid: %d, gid: %d)", drop,
1305-
user_pw->pw_uid, user_pw->pw_gid);
1308+
g_info ("Serving from directory %s", dir);
13061309
}
13071310

1308-
if (!do_chroot)
1311+
if (user_pw)
13091312
{
1310-
if (chdir (dir))
1313+
if (drop_privileges (user_pw) == FALSE)
13111314
{
1312-
g_critical ("failed to change to \"%s\": %s", dir, strerror (errno));
1315+
g_critical ("Failed to drop privileges");
13131316
return 1;
13141317
}
1318+
13151319
g_debug ("Working directory is %s", dir);
13161320
}
13171321

0 commit comments

Comments
 (0)