Debian Codegraph @

staprun/staprun_funcs.c

raw
1 /* -*- linux-c -*-
2 *
3 * staprun_funcs.c - staprun functions
4 *
5 * This file is part of systemtap, and is free software. You can
6 * redistribute it and/or modify it under the terms of the GNU General
7 * Public License (GPL); either version 2, or (at your option) any
8 * later version.
9 *
10 * Copyright (C) 2007-2018 Red Hat Inc.
11 */
12
13 #include "../config.h"
14 #include "staprun.h"
15
16 #include <sys/mount.h>
17 #include <sys/utsname.h>
18 #include <grp.h>
19 #include <pwd.h>
20 #include <assert.h>
21
22 /* The module-renaming facility only works with new enough
23 elfutils: 0.142+. */
24 #include <libelf.h>
25 #include <gelf.h>
26
27 #include <math.h>
28
29 #include "modverify.h"
30 #include "../privilege.h"
31
32 typedef int (*check_module_path_func)(const char *module_path, int module_fd);
33
34 extern long init_module(void *, unsigned long, const char *);
35 extern gid_t get_gid (const char *group_name);
36
37 /* Module errors get translated. */
38 const char *moderror(int err)
39 {
40 switch (err) {
41 case ENOEXEC:
42 return "Invalid module format";
43 case ENOENT:
44 return "Unknown symbol in module";
45 case ESRCH:
46 return "Module has wrong symbol version";
47 case EINVAL:
48 return "Invalid parameters";
49 default:
50 return strerror(err);
51 }
52 }
53
54 int insert_module(
55 const char *path,
56 const char *special_options,
57 char **options,
58 assert_permissions_func assert_permissions,
59 privilege_t *user_credentials
60 ) {
61 int i;
62 long ret, module_read;
63 void *module_file;
64 char *opts;
65 int saved_errno;
66 char module_realpath[PATH_MAX];
67 int module_fd;
68 struct stat sbuf;
69 int rename_this_module;
70
71 dbug(2, "inserting module %s\n", path);
72
73 /* Rename the script module if '-R' was passed, but not other modules
74 * like uprobes. We can tell which this is by comparing to the global
75 * modpath, but we must do it before it's transformed by realpath. */
76 rename_this_module = rename_mod && (strcmp(path, modpath) == 0);
77
78 if (special_options)
79 opts = strdup(special_options);
80 else
81 opts = strdup("");
82 if (opts == NULL) {
83 _perr("allocating memory failed");
84 return -1;
85 }
86 for (i = 0; options[i] != NULL; i++) {
87 opts = realloc(opts, strlen(opts) + strlen(options[i]) + 2);
88 if (opts == NULL) {
89 _perr("[re]allocating memory failed");
90 return -1;
91 }
92 /* Note that these strcat() calls are OK, since we just
93 * allocated space for the resulting string. */
94 strcat(opts, " ");
95 strcat(opts, options[i]);
96 }
97 dbug(2, "module options: %s\n", opts);
98
99 /* Use realpath() to canonicalize the module path. */
100 if (realpath(path, module_realpath) == NULL) {
101 perr("Unable to canonicalize path \"%s\"", path);
102 free(opts);
103 return -1;
104 }
105 dbug(2, "module path canonicalized to '%s'\n", module_realpath);
106
107 /* Use module_realpath from this point on. "Poison" 'path'
108 by setting it to NULL so that it doesn't get used again by
109 mistake. */
110 path = NULL;
111
112 /* Open the module file. Work with the open file descriptor from this
113 point on to avoid TOCTOU problems. */
114 module_fd = open(module_realpath, O_RDONLY);
115 if (module_fd < 0) {
116 perr("Couldn't open '%s'", module_realpath);
117 free(opts);
118 return -1;
119 }
120
121 /* Now that the file is open, figure out how big it is. */
122 if (fstat(module_fd, &sbuf) < 0) {
123 _perr("Error stat'ing '%s'", module_realpath);
124 close(module_fd);
125 free(opts);
126 return -1;
127 }
128
129 /* Allocate memory for the entire module. */
130 module_file = calloc(1, sbuf.st_size);
131 if (module_file == NULL) {
132 _perr("Error allocating memory to read '%s'", module_realpath);
133 close(module_fd);
134 free(opts);
135 return -1;
136 }
137
138 /* Read in the entire module. Work with this copy of the data from this
139 point on to avoid a TOCTOU race between path and signature checking
140 below and module loading. */
141 module_read = 0;
142 while (module_read < sbuf.st_size) {
143 ret = read(module_fd, module_file + module_read,
144 sbuf.st_size - module_read);
145 if (ret > 0)
146 module_read += ret;
147 else if (ret == 0) {
148 _err("Unexpected EOF reading '%s'", module_realpath);
149 free(module_file);
150 close(module_fd);
151 free(opts);
152 return -1;
153 } else if (errno != EINTR) {
154 _perr("Error reading '%s'", module_realpath);
155 free(module_file);
156 close(module_fd);
157 free(opts);
158 return -1;
159 }
160 }
161
162 /* Check whether this module can be loaded by the current user.
163 * check_permissions will exit(-1) if permissions are insufficient*/
164 assert_permissions (module_realpath, module_fd, module_file, sbuf.st_size, user_credentials);
165
166 /* Rename Module if '-R' was passed */
167 if (rename_this_module) {
168 dbug(2,"Renaming module '%s'\n", modname);
169 if(rename_module(module_file, sbuf.st_size) < 0) {
170
171 _err("Error renaming module");
172 close(module_fd);
173 free(opts);
174 return -1;
175 }
176 dbug(2,"Renamed module to '%s'\n", modname);
177 }
178
179 /* Now that the module has been renamed (if we are going to),
180 * set an environment variable that lets child processes know
181 * what the module name is. */
182 if (setenv("SYSTEMTAP_MODULE", modname, 1)) {
183 _perr("SYSTEMTAP_MODULE setenv failed");
184 // Keep going...
185 }
186
187 /* As a debugging aid for testing risky stap modules. */
188 if (getenv ("SYSTEMTAP_SYNC") != NULL)
189 sync();
190
191 dbug(1,"Module %s inserted from file %s\n", modname, module_realpath);
192 PROBE1(staprun, insert__module, (char*)module_realpath);
193 /* Actually insert the module */
194 ret = init_module(module_file, sbuf.st_size, opts);
195 saved_errno = errno;
196
197 /* Cleanup. */
198 free(opts);
199 free(module_file);
200 close(module_fd);
201
202 if (ret != 0) {
203 errno = saved_errno;
204 return -errno;
205 }
206 return 0;
207 }
208
209 static Elf_Scn *
210 find_section_in_module(const void* module_file, const __off_t st_size, const char *section_name)
211 {
212 char *name;
213 size_t shstrndx;
214 Elf* elf;
215 Elf_Scn *scn = 0;
216 GElf_Shdr shdr_mem;
217
218 /* Create descriptor for memory region. */
219 if((elf = elf_memory ((void *)module_file, st_size))== NULL) {
220 _err("Error creating Elf object.\n");
221 return NULL;
222 }
223
224 /* Get the string section index */
225 if(elf_getshdrstrndx (elf, &shstrndx) < 0) {
226 _err("Error getting section index.\n");
227 return NULL;
228 }
229
230 /* Go through the sections looking for the given section" */
231 while ((scn = elf_nextscn (elf, scn))) {
232 if((gelf_getshdr (scn, &shdr_mem))==NULL) {
233 _err("Error getting section header.\n");
234 return NULL;
235 }
236 name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
237 if (name == NULL) {
238 _err("Error getting section name.\n");
239 return NULL;
240 }
241 if(strcmp(name, section_name) == 0) {
242 break;
243 }
244 }
245 return scn;
246 }
247
248 int
249 rename_module(void* module_file, const __off_t st_size)
250 {
251 int length_to_replace;
252 char newname[MODULE_NAME_LEN];
253 char *p;
254 pid_t pid;
255 Elf_Scn *scn = 0;
256 Elf_Data *data = 0;
257
258 scn = find_section_in_module (module_file, st_size, ".gnu.linkonce.this_module");
259 if(!scn) {
260 _err("Section name \".gnu.linkonce.this_module\" not found in module.\n");
261 return -1;
262 }
263
264 /* Get access to raw data from section; do not translate/copy. */
265 if ((data = elf_rawdata (scn, data)) == NULL) {
266 _err("Error getting Elf data from section.\n");
267 return -1;
268 }
269
270 /* Generate new module name with the same length as the old name.
271 The new name is of the form: stap_<FirstPartOfOldname>_<pid>*/
272 if (strlen(modname) >= MODULE_NAME_LEN) {
273 _err("Old module name is too long.\n");
274 return -1;
275 }
276 pid = getpid();
277 length_to_replace = (int)strlen(modname)-((int)log10(pid)+1) - 1;
278 if(length_to_replace < 0 || length_to_replace > (int)strlen(modname)) {
279 // don't treat this is as an error condition
280 warn("Old module name '%s' is too short for renaming, ignoring -R.\n", modname);
281 return -0;
282 }
283 if (snprintf(newname, sizeof(newname), "%.*s_%d", length_to_replace, modname, pid) < 0) {
284 _err("Creating newname failed./n");
285 return -1;
286 }
287
288 /* Find where it is in the module structure.
289 To our knowledge, this section is always completely zeroed apart
290 from the module name, so a simple search and replace should suffice.
291 A signed module from any stapusr or stapsys user will already have been proven
292 untampered. An unsigned module from a stapdev user could try to do
293 naughty things, but we're already trusting these users so much
294 that they can shoot their feet however they like.
295 */
296 for (p = data->d_buf; p < (char *)data->d_buf + data->d_size - strlen(modname); p++) {
297 if (memcmp(p, modname, strlen(modname)) == 0) {
298 strncpy(p, newname, strlen(p)); /* Actually replace the oldname in memory with the newname */
299 modname = strdup(newname); /* This is just to update the global variable containing the current module name */
300 if (modname == NULL) {
301 _perr("allocating memory failed");
302 return -1;
303 }
304 return 0;
305 }
306 }
307 _err("Could not find old name to replace!\n");
308 return -1;
309 }
310
311
312 int mountfs(void)
313 {
314 struct stat sb;
315 struct statfs st;
316 int rc;
317
318 /* If the debugfs dir is already mounted correctly, we're done. */
319 if (statfs(DEBUGFSDIR, &st) == 0
320 && (int) st.f_type == (int) DEBUGFS_MAGIC)
321 return 0;
322
323 /* If DEBUGFSDIR exists (and is a directory), try to mount
324 * DEBUGFSDIR. */
325 rc = stat(DEBUGFSDIR, &sb);
326 if (rc == 0 && S_ISDIR(sb.st_mode)) {
327 /* If we can mount the debugfs dir correctly, we're done. */
328 rc = mount ("debugfs", DEBUGFSDIR, "debugfs", 0, NULL);
329 if (rc == 0)
330 return 0;
331 /* If we got ENODEV, that means that debugfs isn't
332 * supported, so we'll need try try relayfs. If we
333 * didn't get ENODEV, we got a real error. */
334 else if (errno != ENODEV) {
335 perr("Couldn't mount %s", DEBUGFSDIR);
336 return -1;
337 }
338 }
339
340 /* DEBUGFSDIR couldn't be mounted. So, try RELAYFSDIR. */
341
342 /* If the relayfs dir is already mounted correctly, we're done. */
343 if (statfs(RELAYFSDIR, &st) == 0
344 && (int)st.f_type == (int)RELAYFS_MAGIC)
345 return 0;
346
347 /* Ensure that RELAYFSDIR exists and is a directory. */
348 rc = stat(RELAYFSDIR, &sb);
349 if (rc == 0 && ! S_ISDIR(sb.st_mode)) {
350 err("%s exists but isn't a directory.\n", RELAYFSDIR);
351 return -1;
352 }
353 else if (rc < 0) {
354 mode_t old_umask;
355 int saved_errno;
356 gid_t gid = getgid();
357 uid_t uid = getuid();
358
359 /* To ensure the directory gets created with the proper
360 * permissions, set umask to a known value. */
361 old_umask = umask(0002);
362
363 /* To ensure the directory gets created with the
364 * proper group, we'll have to temporarily switch to
365 * root. */
366 /* XXX: Why not just chown() the thing? */
367 if (setuid (0) < 0) {
368 _perr("Couldn't change user while creating %s", RELAYFSDIR);
369 return -1;
370 }
371 if (setgid (0) < 0) {
372 _perr("Couldn't change group while creating %s", RELAYFSDIR);
373 return -1;
374 }
375
376 /* Try to create the directory, saving the return
377 * status and errno value. */
378 rc = mkdir(RELAYFSDIR, 0755);
379 saved_errno = errno;
380
381 /* Restore everything we changed. */
382 if (setgid (gid) < 0) {
383 _perr("Couldn't restore group while creating %s", RELAYFSDIR);
384 return -1;
385 }
386 if (setuid (uid) < 0) {
387 _perr("Couldn't restore user while creating %s", RELAYFSDIR);
388 return -1;
389 }
390 umask(old_umask);
391
392 /* If creating the directory failed, error out. */
393 if (rc < 0) {
394 err("Couldn't create %s: %s\n", RELAYFSDIR, strerror(saved_errno));
395 return -1;
396 }
397 }
398
399 /* Now that we're sure the directory exists, try mounting RELAYFSDIR. */
400 if (mount ("relayfs", RELAYFSDIR, "relayfs", 0, NULL) < 0) {
401 perr("Couldn't mount %s", RELAYFSDIR);
402 return -1;
403 }
404 return 0;
405 }
406
407 /*
408 * Check the signature of the given module.
409 *
410 * Returns: -1 on errors, 0 on failure, 1 on success.
411 */
412 #if ! HAVE_NSS
413 static int
414 check_signature(const char *path __attribute__((unused)),
415 const void *module_data __attribute__((unused)),
416 off_t module_size __attribute__((unused)))
417 {
418 return MODULE_UNTRUSTED;
419 }
420 #else
421 static int
422 check_signature(const char *path, const void *module_data, off_t module_size)
423 {
424 char signature_realpath[PATH_MAX];
425 int rc;
426
427 dbug(2, "checking signature for %s\n", path);
428
429 /* Add the .sgn suffix to the canonicalized module path to get the signature
430 file path. */
431 if (strlen (path) >= PATH_MAX - 5) {
432 err("Path \"%s.sgn\" is too long.", path);
433 return -1;
434 }
435 /* This use of sprintf() is OK, since we just checked the final
436 * string's length. */
437 sprintf (signature_realpath, "%s.sgn", path);
438
439 rc = verify_module (signature_realpath, path, module_data, module_size);
440 dbug(2, "verify_module returns %d\n", rc);
441
442 return rc;
443 }
444 #endif /* HAVE_NSS */
445
446 /*
447 * For stap modules which have not been signed by a trusted signer,
448 * members of the 'stapusr' group can only use the "blessed" modules -
449 * ones in the '/lib/modules/KVER/systemtap' directory. Make sure the
450 * module path is in that directory.
451 *
452 * Returns: -1 on errors, 1 on success.
453 */
454 static int
455 check_stap_module_path(const char *module_path, int module_fd)
456 {
457 char staplib_dir_path[PATH_MAX];
458 char staplib_dir_realpath[PATH_MAX];
459 struct utsname utsbuf;
460 struct stat sb;
461 int rc = 1;
462
463 /* First, we need to figure out what the kernel
464 * version is and build the '/lib/modules/KVER/systemtap' path. */
465 if (uname(&utsbuf) != 0) {
466 _perr("ERROR: Unable to determine kernel version, uname failed");
467 return -1;
468 }
469 if (sprintf_chk(staplib_dir_path, "/lib/modules/%s/systemtap", utsbuf.release))
470 return -1;
471 /* Use realpath() to canonicalize the module directory path. */
472 if (realpath(staplib_dir_path, staplib_dir_realpath) == NULL) {
473 perr("Members of the \"stapusr\" and \"stapsys\" groups can only use unsigned modules within\n"
474 " the \"%s\" directory.\n"
475 " Unable to canonicalize that directory",
476 staplib_dir_path);
477 return -1;
478 }
479
480 /* To make sure the user can't specify something like
481 * /lib/modules/`uname -r`/systemtapmod.ko, put a '/' on the
482 * end of staplib_dir_realpath. */
483 if (strlen(staplib_dir_realpath) < (PATH_MAX - 1))
484 /* Note that this strcat() is OK, since we just
485 * checked the length of the resulting string. */
486 strcat(staplib_dir_realpath, "/");
487 else {
488 err("Path \"%s\" is too long.", staplib_dir_realpath);
489 return -1;
490 }
491
492 /* Validate /lib/modules/KVER/systemtap. No need to use fstat on
493 an open file descriptor to avoid TOCTOU, since the path will
494 not be used to access the file system. */
495 if (stat(staplib_dir_path, &sb) < 0) {
496 perr("Members of the \"stapusr\" and \"stapsys\" groups can only use unsigned modules within\n"
497 " the \"%s\" directory.\n"
498 " Error getting information on that directory",
499 staplib_dir_path);
500 return -1;
501 }
502 /* Make sure it is a directory. */
503 if (! S_ISDIR(sb.st_mode)) {
504 err("Members of the \"stapusr\" and \"stapsys\" groups can only use unsigned modules within\n"
505 " the \"%s\" directory.\n"
506 " That path must refer to a directory.\n",
507 staplib_dir_path);
508 return -1;
509 }
510 /* Make sure it is owned by root. */
511 if (sb.st_uid != 0) {
512 err("Members of the \"stapusr\" and \"stapsys\" groups can only use unsigned modules within\n"
513 " the \"%s\" directory.\n"
514 " That directory should be owned by root.\n",
515 staplib_dir_path);
516 return -1;
517 }
518 /* Make sure it isn't world writable. */
519 if (sb.st_mode & S_IWOTH) {
520 err("Members of the \"stapusr\" and \"stapsys\" groups can only use unsigned modules within\n"
521 " the \"%s\" directory.\n"
522 " That directory should not be world writable.\n",
523 staplib_dir_path);
524 return -1;
525 }
526
527 /* Now we've got two canonicalized paths. Make sure
528 * module_path starts with staplib_dir_realpath. */
529 if (strncmp(staplib_dir_realpath, module_path,
530 strlen(staplib_dir_realpath)) != 0) {
531 err("Members of the \"stapusr\" and \"stapsys\" groups can only use unsigned modules within\n"
532 " the \"%s\" directory.\n"
533 " Module \"%s\" does not exist within that directory.\n",
534 staplib_dir_path, module_path);
535 return -1;
536 }
537
538 /* Validate the module permisions. */
539 if (fstat(module_fd, &sb) < 0) {
540 perr("Couldn't get information on the module\"%s\"", module_path);
541 return -1;
542 }
543 /* Make sure it is owned by root. */
544 if (sb.st_uid != 0) {
545 err("The module \"%s\" must be owned by root.\n", module_path);
546 rc = -1;
547 }
548 /* Make sure it isn't world writable. */
549 if (sb.st_mode & S_IWOTH) {
550 err("The module \"%s\" must not be world writable.\n", module_path);
551 rc = -1;
552 }
553
554 return rc;
555 }
556
557 /*
558 * Don't allow path-based authorization for the uprobes module at all.
559 * Members of the 'stapusr' group can load a signed uprobes module, but
560 * nothing else. Later we could consider allowing specific paths, like
561 * the installed runtime or /lib/modules/...
562 *
563 * Returns: -1 on errors, 0 on failure, 1 on success.
564 */
565 static int
566 check_uprobes_module_path (
567 const char *module_path __attribute__ ((unused)),
568 int module_fd __attribute__ ((unused))
569 )
570 {
571 return 0;
572 }
573
574 /*
575 * Obtain the privilege credentials required to load the given module from the module itself.
576 *
577 * Returns the required credentials if they can be determined or the default safe required
578 * credentials otherwise.
579 */
580 static privilege_t get_module_required_credentials (
581 const char *module_path,
582 const void* module_file __attribute__ ((unused)),
583 const __off_t st_size __attribute__ ((unused))
584 )
585 {
586 Elf_Scn *scn = 0;
587 Elf_Data *data = 0;
588 GElf_Shdr shdr;
589 privilege_t privilege;
590
591 /* Look for the section containing the privilege information in the module. If we can't
592 find the section, then we can assume that it is an old stap module, correctly signed, because:
593 1) We only check privilege credentials for correctly signed modules
594 2) A newer module would contain the required section.
595 We can therefore assume that pr_stapusr is the required privilege level.
596 */
597 scn = find_section_in_module (module_file, st_size, STAP_PRIVILEGE_SECTION);
598 if (! scn) {
599 if (verbose >= 1) {
600 eprintf ("Section name \"%s\" not found in module %s.\n", STAP_PRIVILEGE_SECTION,
601 module_path);
602 eprintf ("Assuming required privilege level of %s.\n", pr_name (pr_stapusr));
603 }
604 return pr_stapusr;
605 }
606
607 /* From here on if there is an error in the data, then it is most likely caused by a newer
608 module containing data that we don't understand. We must then assume the highest privilege
609 requirement.
610 Get the section header. */
611 if (gelf_getshdr (scn, & shdr) == NULL) {
612 if (verbose >= 1) {
613 eprintf ("Error getting section header from section %s in module %s.\n", STAP_PRIVILEGE_SECTION,
614 module_path);
615 eprintf ("Assuming required privilege level of %s.\n", pr_name (pr_highest));
616 }
617 return pr_highest;
618 }
619
620 /* The section should have at least one data item. */
621 if (shdr.sh_size < 1) {
622 if (verbose >= 1) {
623 eprintf ("Section header from section %s in module %s has no items\n", STAP_PRIVILEGE_SECTION,
624 module_path);
625 eprintf ("Assuming required privilege level of %s.\n", pr_name (pr_highest));
626 }
627 return pr_highest;
628 }
629
630 /* The first data item contains the privilege requirement of the module. */
631 if ((data = elf_getdata (scn, data)) == NULL) {
632 if (verbose >= 1) {
633 eprintf ("Error getting data from section %s in module %s\n", STAP_PRIVILEGE_SECTION,
634 module_path);
635 eprintf ("Assuming required privilege level of %s.\n", pr_name (pr_highest));
636 }
637 return pr_highest;
638 }
639
640 /* Make sure the data is the correct size. */
641 if (data->d_size != sizeof (privilege)) {
642 if (verbose >= 1) {
643 eprintf ("Data in section %s is in module %s not the correct size\n", STAP_PRIVILEGE_SECTION,
644 module_path);
645 eprintf ("Assuming required privilege level of %s.\n", pr_name (pr_highest));
646 }
647 return pr_highest;
648 }
649
650 /* Obtain the data. It should already be in host byte order. */
651 privilege = *(privilege_t*)data->d_buf;
652
653 /* Make sure that the data is coherent. */
654 switch (privilege) {
655 case pr_stapusr:
656 case pr_stapsys:
657 case pr_stapdev:
658 break; /* ok */
659 default:
660 if (verbose >= 1) {
661 eprintf ("Unknown privilege data, 0x%x in section %s in module %s\n",
662 (int)privilege, STAP_PRIVILEGE_SECTION, module_path);
663 err ("Assuming required privilege level of %s.\n", pr_name (pr_highest));
664 }
665 return pr_highest;
666 }
667
668 /* ALl is ok. Return the extracted privilege data. */
669 return privilege;
670 }
671
672 /*
673 * Check the user's group membership.
674 *
675 * o root and members of stapdev can do anything
676 * o members of stapsys and stapusr can load signed modules compiled for their privilege levels
677 * o all users can load modules from certain module-specific paths
678 *
679 * Returns:
680 * -2 user has no privilege credentials
681 * 0 on failure
682 * 1 on success
683 */
684 static int
685 check_groups (
686 const char *module_path,
687 int module_fd,
688 int module_signature_status,
689 check_module_path_func check_path,
690 const void *module_data,
691 off_t module_size,
692 privilege_t *assigned_user_credentials
693 )
694 {
695 privilege_t user_credentials, module_required_credentials;
696 int rc;
697
698 /* Lookup the user's privilege credentials. */
699 user_credentials = get_privilege_credentials ();
700 if (assigned_user_credentials)
701 *assigned_user_credentials = user_credentials;
702
703 /* Users with stapdev credentials (includes root) can do anything. */
704 if (pr_contains (user_credentials, pr_stapdev))
705 return 1;
706
707 /* Users with stapsys and stapusr credentials may be able to load a signed module. */
708 if (module_signature_status == MODULE_OK) {
709 /* We must verify that the module was compiled for that privilege level. */
710 module_required_credentials = get_module_required_credentials (module_path, module_data, module_size);
711 if (pr_contains (user_credentials, module_required_credentials)) {
712 /* Our credentials are sufficient */
713 return 1;
714 }
715
716 /* Our credentials are insufficient to load this module. */
717 err("Your privilege credentials (%s) are insufficient to load the module %s (%s required)\n",
718 pr_name (user_credentials), module_path, pr_name (module_required_credentials));
719
720 if (user_credentials == pr_none)
721 return -2;
722
723 return 0;
724 }
725
726 /* Not fatal. The module could still be on a blessed path. */
727 assert (module_signature_status == MODULE_UNTRUSTED ||
728 module_signature_status == MODULE_CHECK_ERROR);
729
730 /* Check whether this module can be loaded based on its path. check_path is a pointer to a
731 module-specific function which will do this. If we can load this module due to its path,
732 then assign all privilege credentials to the user, since the module may require them. */
733 rc = check_path (module_path, module_fd);
734 if (rc == 1) {
735 if (assigned_user_credentials)
736 *assigned_user_credentials = pr_all;
737 }
738 else
739 err("Unable to verify the signature for the module %s.\n", module_path);
740
741 return rc;
742 }
743
744 /*
745 * Check the user's permissions. Is he allowed to run staprun, or is
746 * he limited to "blessed" modules?
747 *
748 * There are several levels of possible permission:
749 *
750 * 1) root can do anything
751 * 2) members of stapdev can do anything
752 * 3) members of stapsys and stapusr can load signed modules compiled for their privilege levels
753 * 4) all users can load unsigned modules from /lib/modules/KVER/systemtap
754 *
755 * It is only an error if all 4 levels of checking fail
756 */
757 void assert_stap_module_permissions(
758 const char *module_path,
759 int module_fd,
760 const void *module_data,
761 off_t module_size,
762 privilege_t *user_credentials
763 ) {
764 int check_groups_rc;
765 int check_signature_rc;
766
767 /* Attempt to verify the module against its signature. Exit
768 immediately if the module has been tampered with (altered). */
769 check_signature_rc = check_signature (module_path, module_data, module_size);
770 if (check_signature_rc == MODULE_ALTERED)
771 exit(-1);
772
773 /* If we're root, we can do anything. */
774 if (getuid() == 0) {
775 /* ... like overriding the real UID */
776 const char *env_id = getenv("SYSTEMTAP_REAL_UID");
777 if (env_id && setreuid(atoi(env_id), -1))
778 warn("Couldn't set staprun UID to '%s': %s",
779 env_id, strerror(errno));
780
781 /* ... or overriding the real GID */
782 env_id = getenv("SYSTEMTAP_REAL_GID");
783 if (env_id && setregid(atoi(env_id), -1))
784 warn("Couldn't set staprun GID to '%s': %s",
785 env_id, strerror(errno));
786
787 if (user_credentials)
788 *user_credentials = pr_all;
789 return;
790 }
791
792 /* Check permissions for group membership. */
793 check_groups_rc = check_groups (module_path, module_fd, check_signature_rc,
794 check_stap_module_path, module_data, module_size,
795 user_credentials);
796 if (check_groups_rc == 1)
797 return;
798
799 /* Are we are an ordinary user?. */
800 if (check_groups_rc == -2) {
801 err("You are trying to run systemtap as a normal user.\n"
802 "You should either be root, or be part of "
803 "group \"stapusr\" and possibly the groups \"stapsys\" or \"stapdev\".\n");
804 }
805
806 exit(-1);
807 }
808
809 /*
810 * Check the user's permissions. Is he allowed to load the uprobes module?
811 *
812 * There are several levels of possible permission:
813 *
814 * 1) root can do anything
815 * 2) members of stapdev can do anything
816 * 3) members of stapusr can load a uprobes module which has been signed by a
817 * trusted signer
818 *
819 * It is only an error if all 3 levels of checking fail
820 */
821 void assert_uprobes_module_permissions(
822 const char *module_path,
823 int module_fd,
824 const void *module_data,
825 off_t module_size,
826 privilege_t *user_credentials
827 ) {
828 int check_groups_rc;
829 int check_signature_rc;
830
831 /* Attempt to verify the module against its signature. Return failure
832 if the module has been tampered with (altered). */
833 check_signature_rc = check_signature (module_path, module_data, module_size);
834 if (check_signature_rc == MODULE_ALTERED)
835 exit(-1);
836
837 /* root can still load this module. */
838 if (getuid() == 0) {
839 if (user_credentials)
840 *user_credentials = pr_all;
841 return;
842 }
843
844 /* Members of the groups stapdev and stapusr can still load this module. */
845 check_groups_rc = check_groups (module_path, module_fd, check_signature_rc,
846 check_uprobes_module_path, module_data, module_size,
847 user_credentials);
848 if (check_groups_rc == 1)
849 return;
850
851 /* Check permissions for group membership. */
852 if (check_groups_rc == -2) {
853 err("You are trying to load the module %s as a normal user.\n"
854 "You should either be root, or be part of "
855 "group \"stapusr\" and possibly the groups \"stapsys\" or \"stapdev\".\n",
856 module_path);
857 }
858
859 exit(-1);
860 }