Ticket #88: max-perm.2

File max-perm.2, 7.1 KB (added by matthijs, 3 years ago)

A patch adding a MAX_PERMISSIONS configuration value

Line 
1Index: vuurmuur/libvuurmuur/src/config.c
2===================================================================
3--- vuurmuur.orig/libvuurmuur/src/config.c      2009-04-25 10:49:57.000000000 +0200
4+++ vuurmuur/libvuurmuur/src/config.c   2009-04-25 11:12:52.000000000 +0200
5@@ -393,7 +393,38 @@
6     }
7     fclose(fp);
8 
9-    /* check if we like the configfile */
10+    /* MAX_PERMISSION
11+     * First (even before calling stat_ok to check the config file),
12+     * load the MAX_PERMISSION value. Allow allow any permissions at
13+     * first, since ask_configfile uses the max_permission value. */
14+    cnf->max_permission = ANY_PERMISSION;
15+    result = ask_configfile(askconfig_debuglvl, "MAX_PERMISSION", answer, cnf->configfile, sizeof(answer));
16+    if(result == 1)
17+    {
18+        char *endptr;
19+        /* ok, found, parse it as an octal mode */
20+        cnf->max_permission = strtol(answer, &endptr, 8);
21+
22+        /* If strol fails, it will set endptr to answer. Also check that
23+         * there was no trailing garbage at the end of the string. */
24+        if (endptr == answer || *endptr != '\0')
25+        {
26+            (void)vrprint.warning("Warning", "Invalid MAX_PERMISSION setting: %s. It should be an octal permission number. Using default (%o).", answer, DEFAULT_MAX_PERMISSION);
27+            cnf->max_permission = DEFAULT_MAX_PERMISSION;
28+
29+            retval = VR_CNF_W_ILLEGAL_VAR;
30+        }
31+    }
32+    else if(result == 0)
33+    {
34+        /* ignore missing, use default */
35+        cnf->max_permission = DEFAULT_MAX_PERMISSION;
36+    }
37+    else
38+        return(VR_CNF_E_UNKNOWN_ERR);
39+
40+    /* Now that we know the maximum permission a config file can have,
41+     * check if we like the configfile */
42     if(!(stat_ok(debuglvl, cnf->configfile, STATOK_WANT_FILE, STATOK_VERBOSE, STATOK_MUST_EXIST)))
43         return(VR_CNF_E_FILE_PERMISSION);
44 
45Index: vuurmuur/libvuurmuur/src/io.c
46===================================================================
47--- vuurmuur.orig/libvuurmuur/src/io.c  2009-04-25 10:49:57.000000000 +0200
48+++ vuurmuur/libvuurmuur/src/io.c       2009-04-25 11:12:52.000000000 +0200
49@@ -104,7 +104,7 @@
50 stat_ok(const int debuglvl, const char *file_loc, char type, char output, char must_exist)
51 {
52     struct stat stat_buf;
53-    mode_t      mode = 0600;
54+    mode_t max, perm;
55 
56     /* safety */
57     if(file_loc == NULL)
58@@ -160,15 +160,6 @@
59         return(0);
60     }
61 
62-    /* if a file is writable by someone other than root, we refuse to open it */
63-    if(stat_buf.st_mode & S_IWGRP || stat_buf.st_mode & S_IWOTH)
64-    {
65-        if(output == STATOK_VERBOSE)
66-            (void)vrprint.error(-1, "Error", "opening '%s': For security reasons Vuurmuur will not open files that are writable by 'group' or 'other'. Check the file content & permissions.", file_loc);
67-
68-        return(0);
69-    }
70-
71     /* we demand that all files are owned by root */
72     if(stat_buf.st_uid != 0 || stat_buf.st_gid != 0)
73     {
74@@ -178,43 +169,25 @@
75         return(0);
76     }
77 
78-    int fixperm = 0;
79-    /* some warnings about the permissions being too relax */
80-    if(stat_buf.st_mode & S_IRGRP)
81-    {
82-        (void)vrprint.info("Info", "'%s' is readable by 'group'. This is not recommended. ", file_loc);
83-        fixperm = 1;
84-    }
85-    if(stat_buf.st_mode & S_IROTH)
86-    {
87-        (void)vrprint.info("Info", "'%s' is readable by and 'other'. This is not recommended.", file_loc);
88-        fixperm = 1;
89-    }
90-
91-    if(stat_buf.st_mode & S_IXGRP)
92-    {
93-        (void)vrprint.info("Info", "'%s' is executable by 'group'. This is not recommended.", file_loc);
94-        fixperm = 1;
95-    }
96-    if(stat_buf.st_mode & S_IXOTH)
97+    if (conf.max_permission != ANY_PERMISSION)
98     {
99-        (void)vrprint.info("Info", "'%s' is executable by 'other'. This is not recommended.", file_loc);
100-        fixperm = 1;
101-    }
102-
103-    if (fixperm) {
104-        /* for dirs */
105-        if(S_ISDIR(stat_buf.st_mode))
106-            mode = 0700;
107-        /* for files */
108-        else if(S_ISREG(stat_buf.st_mode))
109-            mode = 0600;
110+        /* Extract the permission bits from the mode */
111+        perm = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
112+        /* Maximum permissions. Remove +x for files */
113+        max = conf.max_permission;
114+        if (S_ISREG(stat_buf.st_mode) == 1)
115+            max &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
116 
117-        (void)vrprint.info("Info", "Resetting permissions of '%s' to %o.", file_loc, mode);
118-        if(chmod(file_loc, mode) == -1)
119+        /* See if the file mode has more bits set than the maximum allowed */
120+        if(perm & ~max)
121         {
122-            (void)vrprint.error(-1, "Error", "failed to repair permissions for '%s': %s.", file_loc, strerror(errno));
123-            return(0);
124+            (void)vrprint.info("Info", "'%s' has mode %o, which is more than maximum allowed mode %o. Resetting to %o.", file_loc, perm, max, max);
125+
126+            if(chmod(file_loc, max) == -1)
127+            {
128+                (void)vrprint.error(-1, "Error", "failed to repair permissions for '%s': %s.", file_loc, strerror(errno));
129+                return(0);
130+            }
131         }
132     }
133 
134Index: vuurmuur/libvuurmuur/src/vuurmuur.h
135===================================================================
136--- vuurmuur.orig/libvuurmuur/src/vuurmuur.h    2009-04-25 10:50:05.000000000 +0200
137+++ vuurmuur/libvuurmuur/src/vuurmuur.h 2009-04-25 11:12:52.000000000 +0200
138@@ -145,6 +145,8 @@
139 #define DEFAULT_LOAD_MODULES            TRUE                /* default we load modules */
140 #define DEFAULT_MODULES_WAITTIME        0                   /* default we don't wait */
141 
142+#define DEFAULT_MAX_PERMISSION          0700                /* default only allow user rwx */
143+
144 #define MAX_LOGRULE_SIZE                512
145 #define MAX_PIPE_COMMAND                512                 /* maximum lenght of the pipe command */
146 #define MAX_RULECOMMENT_LEN             64                  /* length in characters (for widec) */
147@@ -152,7 +154,9 @@
148 #define PROC_IPCONNTRACK                "/proc/net/ip_conntrack"
149 #define PROC_NFCONNTRACK                "/proc/net/nf_conntrack"
150 
151-
152+/* Special permission value, meaning don't check permissions. The value
153+ * is simply all ones. */
154+#define ANY_PERMISSION                  (~((mode_t)0))
155 /*
156     regexes
157 */
158@@ -427,6 +431,11 @@
159     /* this is detected at runtime */
160     char            use_nfconntrack;
161 
162+       /* Maximum permissions for files and directories used by vuurmuur
163+          (config & log files). This should include x bits, which are
164+          filtered out for files. */
165+       mode_t          max_permission;
166+
167 } conf;
168 
169 
170Index: vuurmuur/vuurmuur/skel/etc/vuurmuur/config.conf.sample
171===================================================================
172--- vuurmuur.orig/vuurmuur/skel/etc/vuurmuur/config.conf.sample 2009-04-25 11:13:02.000000000 +0200
173+++ vuurmuur/vuurmuur/skel/etc/vuurmuur/config.conf.sample      2009-04-25 11:13:05.000000000 +0200
174@@ -75,4 +75,10 @@
175 # Ignore echo-broadcasts? (yes/no)
176 PROTECT_ECHOBROADCAST="Yes"
177 
178+# Don't allow config and log files and directories to be accessable by
179+# anyone but root. For files, the execute bits are automatically
180+# stripped from this value. This should be an octal number describing
181+# the maximum allowable permissions.
182+MAX_PERMISSION="700"
183+
184 # end of file