unpriv_helpers.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include "unpriv_helpers.h"
  9. static bool get_mitigations_off(void)
  10. {
  11. char cmdline[4096], *c;
  12. int fd, ret = false;
  13. fd = open("/proc/cmdline", O_RDONLY);
  14. if (fd < 0) {
  15. perror("open /proc/cmdline");
  16. return false;
  17. }
  18. if (read(fd, cmdline, sizeof(cmdline) - 1) < 0) {
  19. perror("read /proc/cmdline");
  20. goto out;
  21. }
  22. cmdline[sizeof(cmdline) - 1] = '\0';
  23. for (c = strtok(cmdline, " \n"); c; c = strtok(NULL, " \n")) {
  24. if (strncmp(c, "mitigations=off", strlen(c)))
  25. continue;
  26. ret = true;
  27. break;
  28. }
  29. out:
  30. close(fd);
  31. return ret;
  32. }
  33. bool get_unpriv_disabled(void)
  34. {
  35. bool disabled;
  36. char buf[2];
  37. FILE *fd;
  38. fd = fopen("/proc/sys/" UNPRIV_SYSCTL, "r");
  39. if (fd) {
  40. disabled = (fgets(buf, 2, fd) == buf && atoi(buf));
  41. fclose(fd);
  42. } else {
  43. perror("fopen /proc/sys/" UNPRIV_SYSCTL);
  44. disabled = true;
  45. }
  46. return disabled ? true : get_mitigations_off();
  47. }