Conservative SmartOS global-zone memory pressure guard with dry-run-first policy and SMF integration.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

184 linhas
7.3 KiB

  1. #include "guard.h"
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <errno.h>
  6. void defaults(config_t *c)
  7. {
  8. memset(c, 0, sizeof (*c));
  9. c->syslog_enabled = 1; c->syslog_facility = 5;
  10. c->pre_s = 1; c->pressure_s = 2; c->critical_s = 2;
  11. c->recovery_s = 15; c->critical_term_s = 5;
  12. c->emergency_term_s = 1; c->floor_kill_delay_s = 0;
  13. c->max_cached_candidate_age_s = 15; c->normal_scan_s = 5;
  14. c->scan_budget_ms = 200; c->cooldown_s = 10;
  15. c->max_victims = 4; c->max_per_minute = 4;
  16. c->max_emergency_victims = 8; c->max_floor_victims = 12;
  17. c->min_rss_kib = 65536; c->log_mib = 10; c->log_keep = 5;
  18. }
  19. int uuid_valid(const char *s)
  20. {
  21. size_t i;
  22. if (strlen(s) != 36) return 0;
  23. for (i = 0; i < 36; i++) {
  24. if (i == 8 || i == 13 || i == 18 || i == 23) {
  25. if (s[i] != '-') return 0;
  26. } else if (!isdigit((unsigned char)s[i]) && !(s[i] >= 'a' && s[i] <= 'f'))
  27. return 0;
  28. }
  29. return strcmp(s, "00000000-0000-0000-0000-000000000000") != 0;
  30. }
  31. const zone_rule_t *zone_rule(const config_t *c, const char *uuid)
  32. {
  33. size_t i;
  34. for (i = 0; i < c->nzones; i++)
  35. if (!strcmp(c->zones[i].uuid, uuid)) return &c->zones[i];
  36. return NULL;
  37. }
  38. int protected_process(const config_t *c, const process_t *p)
  39. {
  40. static const char *const builtin[] = {"init", "systemd", "svc.startd",
  41. "svc.configd", "sshd", "zoneadmd", "vminfod", "vmadmd", "qemu",
  42. "qemu-system-x86", "bhyve", "o6h-memory-guar", "o6h-memory-guard"};
  43. const zone_rule_t *z = zone_rule(c, p->uuid);
  44. size_t i;
  45. if (p->pid <= 1 || p->ppid == 0 || p->zid <= 0 || p->initpid <= 0 ||
  46. p->pid == p->initpid || p->nlwp == 0 || !p->zone_generation ||
  47. !uuid_valid(p->uuid) || !z || z->policy != ALLOW ||
  48. p->rss_kib < c->min_rss_kib) return 1;
  49. for (i = 0; i < sizeof (builtin) / sizeof (builtin[0]); i++)
  50. if (!strcmp(p->name, builtin[i])) return 1;
  51. for (i = 0; i < c->nnames; i++)
  52. if (!strcmp(p->name, c->protected_names[i])) return 1;
  53. for (i = 0; i < c->npids; i++)
  54. if (p->pid == c->protected_pids[i]) return 1;
  55. return 0;
  56. }
  57. static int token(const char *s, size_t max)
  58. {
  59. size_t i, n = strlen(s);
  60. if (!n || n >= max) return 0;
  61. for (i = 0; i < n; i++)
  62. if (!isalnum((unsigned char)s[i]) && !strchr("._-", s[i])) return 0;
  63. return 1;
  64. }
  65. static int number(const char *s, unsigned long lo, unsigned long hi, unsigned *v)
  66. {
  67. char *end;
  68. unsigned long n;
  69. if (!*s || *s == '-' || *s == '+') return -1;
  70. errno = 0; n = strtoul(s, &end, 10);
  71. if (errno || *end || n < lo || n > hi) return -1;
  72. *v = (unsigned)n; return 0;
  73. }
  74. int config_read(FILE *f, config_t *c, char *error, size_t cap)
  75. {
  76. char line[512], *key, *value, *extra, *ctx;
  77. unsigned lineno = 0, n, seen = 0;
  78. defaults(c);
  79. while (fgets(line, sizeof (line), f)) {
  80. char *comment;
  81. lineno++;
  82. if (!strchr(line, '\n') && !feof(f)) goto invalid;
  83. comment = strchr(line, '#'); if (comment) *comment = 0;
  84. ctx = NULL; key = strtok_r(line, " \t\r\n", &ctx);
  85. if (!key) continue;
  86. value = strtok_r(NULL, " \t\r\n", &ctx);
  87. if (!value) goto invalid;
  88. if (!strcmp(key, "zone")) {
  89. zone_rule_t *z;
  90. char *alias = strtok_r(NULL, " \t\r\n", &ctx);
  91. char *policy = strtok_r(NULL, " \t\r\n", &ctx);
  92. if (!alias || !policy || strtok_r(NULL, " \t\r\n", &ctx) ||
  93. !uuid_valid(value) || !token(alias, 64) ||
  94. c->nzones == MAX_ZONES || zone_rule(c, value)) goto invalid;
  95. z = &c->zones[c->nzones++];
  96. strcpy(z->uuid, value); strcpy(z->alias, alias);
  97. if (!strcmp(policy, "allow")) z->policy = ALLOW;
  98. else if (!strcmp(policy, "protect")) z->policy = PROTECT;
  99. else if (!strcmp(policy, "observe")) z->policy = OBSERVE;
  100. else goto invalid;
  101. continue;
  102. }
  103. extra = strtok_r(NULL, " \t\r\n", &ctx);
  104. if (extra) goto invalid;
  105. if (!strcmp(key, "protect_process")) {
  106. if (!token(value, 16) || c->nnames == MAX_RULES) goto invalid;
  107. strcpy(c->protected_names[c->nnames++], value); continue;
  108. }
  109. if (!strcmp(key, "protect_pid")) {
  110. if (number(value, 1, 2147483647UL, &n) || c->npids == MAX_RULES)
  111. goto invalid;
  112. c->protected_pids[c->npids++] = (int)n; continue;
  113. }
  114. if (!strcmp(key, "ACTION_MODE")) {
  115. if (seen & 1u) goto invalid;
  116. seen |= 1u;
  117. if (!strcmp(value, "armed")) c->armed = 1;
  118. else if (strcmp(value, "dry-run")) goto invalid;
  119. continue;
  120. }
  121. if (!strcmp(key, "SYSLOG_ENABLED")) {
  122. if (seen & (1u << 19)) goto invalid;
  123. seen |= 1u << 19;
  124. if (!strcmp(value, "yes")) c->syslog_enabled = 1;
  125. else if (!strcmp(value, "no")) c->syslog_enabled = 0;
  126. else goto invalid;
  127. continue;
  128. }
  129. if (!strcmp(key, "SYSLOG_FACILITY")) {
  130. if ((seen & (1u << 20)) || strlen(value) != 6 ||
  131. memcmp(value, "local", 5) || value[5] < '0' || value[5] > '7')
  132. goto invalid;
  133. seen |= 1u << 20; c->syslog_facility = value[5] - '0'; continue;
  134. }
  135. #define OPT(name, field, low, high, bit) \
  136. if (!strcmp(key, name)) { \
  137. if ((seen & (1u << bit)) || number(value, low, high, &n)) goto invalid; \
  138. seen |= 1u << bit; c->field = n; continue; }
  139. OPT("PRE_SECONDS", pre_s, 1, 60, 1)
  140. OPT("PRESSURE_SECONDS", pressure_s, 2, 120, 2)
  141. OPT("CRITICAL_SECONDS", critical_s, 1, 10, 3)
  142. OPT("RECOVERY_SECONDS", recovery_s, 5, 300, 4)
  143. if (!strcmp(key, "TERM_GRACE_SECONDS") ||
  144. !strcmp(key, "CRITICAL_TERM_GRACE_SECONDS")) {
  145. if ((seen & (1u << 5)) || number(value, 1, 30, &n)) goto invalid;
  146. seen |= 1u << 5; c->critical_term_s = n; continue;
  147. }
  148. OPT("NORMAL_SCAN_SECONDS", normal_scan_s, 1, 5, 6)
  149. OPT("SCAN_BUDGET_MS", scan_budget_ms, 10, 500, 7)
  150. OPT("COOLDOWN_SECONDS", cooldown_s, 1, 120, 8)
  151. OPT("MAX_VICTIMS_EPISODE", max_victims, 1, MAX_VICTIMS, 9)
  152. OPT("MAX_VICTIMS_MINUTE", max_per_minute, 1, 32, 10)
  153. OPT("MIN_RSS_KIB", min_rss_kib, 1024, 1073741824UL, 11)
  154. OPT("LOG_MAX_MIB", log_mib, 1, 100, 12)
  155. OPT("LOG_KEEP", log_keep, 1, 20, 13)
  156. OPT("EMERGENCY_TERM_GRACE_SECONDS", emergency_term_s, 0, 10, 14)
  157. OPT("FLOOR_KILL_DELAY_SECONDS", floor_kill_delay_s, 0, 5, 15)
  158. OPT("MAX_CACHED_CANDIDATE_AGE_SECONDS", max_cached_candidate_age_s, 5, 60, 16)
  159. OPT("MAX_EMERGENCY_VICTIMS_EPISODE", max_emergency_victims, 1, MAX_VICTIMS, 17)
  160. OPT("MAX_FLOOR_VICTIMS_EPISODE", max_floor_victims, 1, MAX_VICTIMS, 18)
  161. #undef OPT
  162. goto invalid;
  163. }
  164. if (ferror(f) || c->pressure_s < c->pre_s ||
  165. c->max_emergency_victims < c->max_victims ||
  166. c->max_floor_victims < c->max_emergency_victims) goto invalid;
  167. if (c->armed) {
  168. size_t i; int allow = 0;
  169. for (i = 0; i < c->nzones; i++) allow |= c->zones[i].policy == ALLOW;
  170. if (!allow) { snprintf(error, cap, "armed requires at least one allow UUID"); return -1; }
  171. }
  172. return 0;
  173. invalid:
  174. snprintf(error, cap, "invalid/duplicate/unknown configuration at line %u", lineno);
  175. return -1;
  176. }