capabilities.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 2015 Etienne CHAMPETIER <champetier.etienne@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define _GNU_SOURCE 1
  14. #include <syslog.h>
  15. #include <sys/prctl.h>
  16. #include <libubox/blobmsg.h>
  17. #include <libubox/blobmsg_json.h>
  18. #include "log.h"
  19. #include "../capabilities-names.h"
  20. #include "capabilities.h"
  21. static int find_capabilities(const char *name)
  22. {
  23. int i;
  24. for (i = 0; i <= CAP_LAST_CAP; i++)
  25. if (capabilities_names[i] && !strcmp(capabilities_names[i], name))
  26. return i;
  27. return -1;
  28. }
  29. int drop_capabilities(const char *file)
  30. {
  31. enum {
  32. CAP_KEEP,
  33. CAP_DROP,
  34. __CAP_MAX
  35. };
  36. static const struct blobmsg_policy policy[__CAP_MAX] = {
  37. [CAP_KEEP] = { .name = "cap.keep", .type = BLOBMSG_TYPE_ARRAY },
  38. [CAP_DROP] = { .name = "cap.drop", .type = BLOBMSG_TYPE_ARRAY },
  39. };
  40. struct blob_buf b = { 0 };
  41. struct blob_attr *tb[__CAP_MAX];
  42. struct blob_attr *cur;
  43. int rem, cap;
  44. char *name;
  45. uint64_t capdrop = 0LLU;
  46. DEBUG("dropping capabilities\n");
  47. blob_buf_init(&b, 0);
  48. if (!blobmsg_add_json_from_file(&b, file)) {
  49. ERROR("failed to load %s\n", file);
  50. return -1;
  51. }
  52. blobmsg_parse(policy, __CAP_MAX, tb, blob_data(b.head), blob_len(b.head));
  53. if (!tb[CAP_KEEP] && !tb[CAP_DROP]) {
  54. ERROR("failed to parse %s\n", file);
  55. return -1;
  56. }
  57. blobmsg_for_each_attr(cur, tb[CAP_KEEP], rem) {
  58. name = blobmsg_get_string(cur);
  59. if (!name) {
  60. ERROR("invalid capability name in cap.keep\n");
  61. return -1;
  62. }
  63. cap = find_capabilities(name);
  64. if (cap == -1) {
  65. ERROR("unknown capability %s in cap.keep\n", name);
  66. return -1;
  67. }
  68. capdrop |= (1LLU << cap);
  69. }
  70. if (capdrop == 0LLU) {
  71. DEBUG("cap.keep empty -> only dropping capabilities from cap.drop (blacklist)\n");
  72. capdrop = 0xffffffffffffffffLLU;
  73. } else {
  74. DEBUG("cap.keep has at least one capability -> dropping every capabilities not in cap.keep (whitelist)\n");
  75. }
  76. blobmsg_for_each_attr(cur, tb[CAP_DROP], rem) {
  77. name = blobmsg_get_string(cur);
  78. if (!name) {
  79. ERROR("invalid capability name in cap.drop\n");
  80. return -1;
  81. }
  82. cap = find_capabilities(name);
  83. if (cap == -1) {
  84. ERROR("unknown capability %s in cap.drop\n", name);
  85. return -1;
  86. }
  87. capdrop &= ~(1LLU << cap);
  88. }
  89. for (cap = 0; cap <= CAP_LAST_CAP; cap++) {
  90. if ( (capdrop & (1LLU << cap)) == 0) {
  91. DEBUG("dropping capability %s (%d)\n", capabilities_names[cap], cap);
  92. if (prctl(PR_CAPBSET_DROP, cap, 0, 0, 0)) {
  93. ERROR("prctl(PR_CAPBSET_DROP, %d) failed: %m\n", cap);
  94. return errno;
  95. }
  96. } else {
  97. DEBUG("keeping capability %s (%d)\n", capabilities_names[cap], cap);
  98. }
  99. }
  100. return 0;
  101. }