Ticket #7832: 36_setgroups_too.3

File 36_setgroups_too.3, 1.8 KB (added by superm1@…, 14 years ago)
Line 
1setgroups() before setgid/setuid/home
2Author: Mario Limonciello <superm1@ubuntu.com>
3
4---
5--- mythtv-0.23.0~trunk23567.orig/programs/mythbackend/main.cpp
6+++ mythtv-0.23.0~trunk23567/programs/mythbackend/main.cpp
7@@ -7,6 +7,7 @@
8 #include <libgen.h>
9 #include <signal.h>
10 #include <pwd.h>
11+#include <grp.h>
12 
13 #include "mythconfig.h"
14 #if CONFIG_DARWIN
15@@ -878,6 +879,34 @@ int main(int argc, char **argv)
16         }
17         else if (!user_id && user_info)
18         {
19+            int ngroups = 1;
20+            gid_t *groups;
21+            groups = (gid_t*) malloc(ngroups * sizeof (gid_t));
22+            if (groups == NULL)
23+            {
24+                VERBOSE(VB_IMPORTANT,
25+                        QString("Error allocating memory for %1 groups.").arg(ngroups));
26+                return BACKEND_EXIT_PERMISSIONS_ERROR;
27+            }
28+            if (getgrouplist(user_info->pw_name, user_info->pw_gid, groups, &ngroups) == -1)
29+            {
30+                free(groups);
31+                groups = (gid_t*) malloc(ngroups * sizeof (gid_t));
32+                if (groups == NULL)
33+                {
34+                    VERBOSE(VB_IMPORTANT,
35+                            QString("Error allocating memory for %1 groups.").arg(ngroups));
36+                    return BACKEND_EXIT_PERMISSIONS_ERROR;
37+                }
38+                getgrouplist(user_info->pw_name, user_info->pw_gid, groups, &ngroups);
39+            }
40+            if (setgroups(ngroups, groups) == -1)
41+            {
42+                free(groups);
43+                VERBOSE(VB_IMPORTANT, "Error setting groups.");
44+                return BACKEND_EXIT_PERMISSIONS_ERROR;
45+            }
46+            free(groups);
47             if (setenv("HOME", user_info->pw_dir,1) == -1)
48             {
49                 VERBOSE(VB_IMPORTANT, "Error setting home directory.");