Whamcloud - gitweb
LU-14286 osd-ldiskfs: fallocate() should zero new blocks
[fs/lustre-release.git] / lustre / utils / l_getsepol.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see http://www.gnu.org/licenses
18  *
19  * GPL HEADER END
20  */
21
22 /*
23  * Copyright (c) 2016 DDN Storage
24  * Author: Sebastien Buisson sbuisson@ddn.com
25  */
26
27 /*
28  * lustre/utils/l_getsepol.c
29  * Userland helper to retrieve SELinux policy information.
30  */
31
32 #include <sys/types.h>
33 #include <stdbool.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <libgen.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <limits.h>
42 #include <syslog.h>
43 #include <stdarg.h>
44 #include <fcntl.h>
45 #include <stddef.h>
46 #include <ctype.h>
47 #include <dirent.h>
48 #include <getopt.h>
49
50 #include <openssl/evp.h>
51
52 #include <selinux/selinux.h>
53
54 #include <libcfs/util/param.h>
55 #include <linux/lustre/lustre_user.h>
56 #include <linux/lustre/lustre_idl.h>
57
58
59 static char *progname;
60 static char *obd_type = NULL, *obd_name = NULL;
61 static time_t ref_pol_mtime = 0;
62 static char ref_selinux_mode = -1;
63
64 static void errlog(const char *fmt, ...)
65 {
66         va_list args;
67
68         openlog(progname, LOG_PID, LOG_AUTHPRIV);
69
70         va_start(args, fmt);
71         vsyslog(LOG_NOTICE, fmt, args);
72         va_end(args);
73
74         closelog();
75 }
76
77 /* Retrieve name of policy loaded, and version */
78 static int sepol_get_policy_info(char **policyname)
79 {
80         char *pol_path;
81
82         /* Name of loaded policy can be retrieved from policy root path */
83         pol_path = strdup(selinux_policy_root());
84
85         if (!pol_path) {
86                 *policyname = NULL;
87                 errlog("can't get policy name: %s\n", strerror(errno));
88                 return -errno;
89         }
90
91         *policyname = strdup(basename(pol_path));
92         free(pol_path);
93
94         return 0;
95 }
96
97 /* Read binary SELinux policy, and compute hash */
98 static int sepol_get_policy_data(const char *pol_bin_path,
99                                  unsigned char **mdval, unsigned int *mdsize)
100 {
101         int fd;
102         char buffer[1024];
103         ssize_t count = 1024;
104         EVP_MD_CTX *mdctx;
105         const EVP_MD *md = EVP_sha256(); /* use SHA-256 */
106         int rc;
107
108         /* Open policy file */
109         fd = open(pol_bin_path, O_RDONLY);
110         if (fd < 0) {
111                 errlog("can't open SELinux policy file %s: %s\n", pol_bin_path,
112                        strerror(errno));
113                 rc = -ENOENT;
114                 goto out;
115         }
116
117         /* Read policy file */
118         mdctx = EVP_MD_CTX_create();
119         EVP_DigestInit_ex(mdctx, md, NULL);
120         while (count == 1024) {
121                 count = read(fd, buffer, count);
122                 if (count < 0) {
123                         errlog("can't read SELinux policy file %s\n",
124                                pol_bin_path);
125                         rc = -errno;
126                         close(fd);
127                         goto out;
128                 }
129                 EVP_DigestUpdate(mdctx, buffer, count);
130         }
131
132         /* Close policy file */
133         rc = close(fd);
134         if (rc < 0) {
135                 rc = -errno;
136                 goto out;
137         }
138
139         *mdsize = EVP_MD_size(md);
140         *mdval = malloc(*mdsize);
141         if (*mdval == NULL) {
142                 rc = -ENOMEM;
143                 goto out;
144         }
145
146         EVP_DigestFinal_ex(mdctx, *mdval, NULL);
147         EVP_MD_CTX_destroy(mdctx);
148
149 out:
150         return rc;
151 }
152
153 int get_opts(int argc, char *const argv[])
154 {
155         static struct option long_opts[] = {
156                 { .val = 'o', .name =  "obd_type",
157                   .has_arg = required_argument},
158                 { .val = 'n', .name =  "obd_name",
159                   .has_arg = required_argument},
160                 { .val = 't', .name =  "sel_mtime",
161                   .has_arg = required_argument},
162                 { .val = 'm', .name =  "sel_mode",
163                   .has_arg = required_argument},
164                 { .name = NULL } };
165         char *short_opts = "o:n:t:m:";
166         int opt;
167         int longidx;
168         char *sel_mtime = NULL, *sel_mode = NULL;
169         char *res;
170
171         optind = 0;
172         while ((opt = getopt_long(argc, argv, short_opts, long_opts,
173                                   &longidx)) != EOF) {
174                 switch (opt) {
175                 case 'o':
176                         obd_type = optarg;
177                         break;
178                 case 'n':
179                         obd_name = optarg;
180                         break;
181                 case 't':
182                         sel_mtime = optarg;
183                         break;
184                 case 'm':
185                         sel_mode = optarg;
186                         break;
187                 default:
188                         if (opt != '?')
189                                 fprintf(stderr, "Unknown option '%c'\n", opt);
190                         return -EINVAL;
191                 }
192         }
193
194         if (optind != argc) {
195                 errlog("incorrect arguments\n");
196                 return -EINVAL;
197         }
198
199         if (!obd_type || !obd_name)
200                 /* called without arg (presumably from command line):
201                  * ignore everything */
202                 return 0;
203
204         if (sel_mtime) {
205                 ref_pol_mtime = (time_t)strtoul(sel_mtime, &res, 0);
206                 if (*res != '\0') {
207                         /* not a valid number */
208                         errlog("invalid sel_mtime");
209                         return -EINVAL;
210                 }
211         }
212
213         if (sel_mode) {
214                 ref_selinux_mode = sel_mode[0] - '0';
215                 if (ref_selinux_mode != 0 && ref_selinux_mode != 1) {
216                         /* not a valid enforcing mode */
217                         errlog("invalid sel_mode");
218                         return -EINVAL;
219                 }
220         }
221
222         return 0;
223 }
224
225 #define sepol_downcall(type_t, magic) ({ \
226         glob_t path; \
227         int fd, size; \
228         struct type_t *data; \
229         int idx; \
230         char *p; \
231         \
232         size = offsetof(struct type_t, \
233                         sdd_sepol[LUSTRE_NODEMAP_SEPOL_LENGTH + 1]); \
234         data = malloc(size); \
235         if (!data) { \
236                 errlog("malloc sepol downcall data(%d) failed!\n", size); \
237                 rc = -ENOMEM; \
238                 goto out_mdval; \
239         } \
240         memset(data, 0, size); \
241         \
242         /* Put all info together and generate string \
243          * to represent SELinux policy information \
244          */ \
245         rc = snprintf(data->sdd_sepol, LUSTRE_NODEMAP_SEPOL_LENGTH + 1, \
246                       "%.1d:%s:%u:", enforce, policy_type, policyver); \
247         if (rc >= LUSTRE_NODEMAP_SEPOL_LENGTH + 1) { \
248                 rc = -EMSGSIZE; \
249                 goto out_data_ ## type_t ; \
250         } \
251         \
252         p = data->sdd_sepol + strlen(data->sdd_sepol); \
253         size = LUSTRE_NODEMAP_SEPOL_LENGTH + 1 - strlen(data->sdd_sepol); \
254         for (idx = 0; idx < mdsize; idx++) { \
255                 rc = snprintf(p, size, "%02x", \
256                               (unsigned char)(mdval[idx])); \
257                 p += 2; \
258                 size -= 2; \
259                 if (size < 0 || rc >= size) { \
260                         rc = -EMSGSIZE; \
261                         goto out_data_ ## type_t ; \
262                 } \
263         } \
264         data->sdd_sepol_len = p - data->sdd_sepol; \
265         \
266         size = offsetof(struct type_t, \
267                         sdd_sepol[data->sdd_sepol_len]); \
268         \
269         if (!obd_type || !obd_name) { \
270                 /* called without arg (presumably from command line): \
271                  * print SELinux status and exit \
272                  */ \
273                 printf("SELinux status info: %.*s\n", \
274                        data->sdd_sepol_len, data->sdd_sepol); \
275                 return 0; \
276         } \
277         \
278         data->sdd_magic = magic; \
279         data->sdd_sepol_mtime = policymtime; \
280         /* Send SELinux policy info to kernelspace */ \
281         rc = cfs_get_param_paths(&path, "%s/%s/srpc_sepol", obd_type, \
282                                  obd_name); \
283         if (rc != 0) { \
284                 errlog("can't get param '%s/%s/srpc_sepol': %s\n", \
285                        obd_type, obd_name, strerror(errno)); \
286                 rc = -errno; \
287                 goto out_data_ ## type_t ; \
288         } \
289         \
290         fd = open(path.gl_pathv[0], O_WRONLY); \
291         if (fd < 0) { \
292                 errlog("can't open file '%s':%s\n", path.gl_pathv[0], \
293                         strerror(errno)); \
294                 rc = -errno; \
295                 goto out_params_ ## type_t ; \
296         } \
297         \
298         rc = write(fd, data, size); \
299         close(fd); \
300         if (rc != size) { \
301                 errlog("partial write ret %d: %s\n", rc, strerror(errno)); \
302                 rc = -errno; \
303         } else { \
304                 rc = 0; \
305         } \
306         \
307         out_params_ ## type_t :     \
308         cfs_free_param_data(&path); \
309         out_data_ ## type_t :       \
310         free(data); \
311 })
312
313 /**
314  * Calculate SELinux status information.
315  * String that represents SELinux status info has the following format:
316  * <mode>:<policy name>:<policy version>:<policy hash>
317  * <mode> is a digit equal to 0 for SELinux Permissive mode,
318  * and 1 for Enforcing mode.
319  * When called from kernel space, it requires 4 args:
320  * - obd type
321  * - obd name
322  * - SELinux policy mtime
323  * - SELinux enforcing mode
324  * When called from command line (in this case without proper args), it prints
325  * SELinux status info to stdout.
326  */
327 int main(int argc, char **argv)
328 {
329         int policyver = 0;
330         char pol_bin_path[PATH_MAX + 1];
331         struct stat st;
332         time_t policymtime = 0;
333         int enforce;
334         char *policy_type = NULL;
335         unsigned char *mdval = NULL;
336         unsigned int mdsize = 0;
337         int rc;
338
339         progname = basename(argv[0]);
340
341         rc = get_opts(argc, argv);
342         if (rc < 0)
343                 goto out;
344
345         /* Max version of loaded policy */
346         policyver = security_policyvers();
347         if (policyver < 0) {
348                 errlog("unknown policy version: %s\n", strerror(errno));
349                 rc = -errno;
350                 goto out;
351         }
352
353         while (policymtime == 0) {
354                 /* Path of binary policy file */
355                 snprintf(pol_bin_path, sizeof(pol_bin_path), "%s.%d",
356                          selinux_binary_policy_path(), policyver);
357
358                 /* Stat binary policy file */
359                 if (stat(pol_bin_path, &st)) {
360                         if (policyver > 0) {
361                                 policyver--;
362                         } else {
363                                 errlog("can't stat %s.*: %s\n",
364                                        selinux_binary_policy_path(),
365                                        strerror(errno));
366                                 rc = -errno;
367                                 goto out;
368                         }
369                 } else {
370                         policymtime = st.st_mtime;
371                 }
372         }
373
374         /* Determine if SELinux is in permissive or enforcing mode */
375         enforce = security_getenforce();
376         if (enforce < 0) {
377                 errlog("can't getenforce: %s\n", strerror(errno));
378                 rc = -errno;
379                 goto out;
380         }
381
382         if (ref_pol_mtime == policymtime && ref_selinux_mode == enforce) {
383                 /* Policy has not changed: return immediately */
384                 rc = 0;
385                 goto out;
386         }
387
388         /* Now we need to calculate SELinux status information */
389         /* Get policy name */
390         rc = sepol_get_policy_info(&policy_type);
391         if (rc < 0)
392                 goto out;
393
394         /* Read binary SELinux policy, and compute hash */
395         rc = sepol_get_policy_data(pol_bin_path, &mdval, &mdsize);
396         if (rc < 0)
397                 goto out_poltyp;
398
399         sepol_downcall(sepol_downcall_data, SEPOL_DOWNCALL_MAGIC);
400 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
401         if (rc == -EINVAL)
402                 /* try with old magic */
403                 sepol_downcall(sepol_downcall_data_old,
404                                SEPOL_DOWNCALL_MAGIC_OLD);
405 #endif
406
407 out_mdval:
408         free(mdval);
409 out_poltyp:
410         free(policy_type);
411 out:
412         if (isatty(STDIN_FILENO))
413                 /* we are called from the command line */
414                 return rc < 0 ? -rc : rc;
415         else
416                 return rc;
417 }