#include "guard.h" #include #include #include #include void defaults(config_t *c) { memset(c, 0, sizeof (*c)); c->syslog_enabled = 1; c->syslog_facility = 5; c->pre_s = 1; c->pressure_s = 2; c->critical_s = 2; c->recovery_s = 15; c->critical_term_s = 5; c->emergency_term_s = 1; c->floor_kill_delay_s = 0; c->max_cached_candidate_age_s = 15; c->normal_scan_s = 5; c->scan_budget_ms = 200; c->cooldown_s = 10; c->max_victims = 4; c->max_per_minute = 4; c->max_emergency_victims = 8; c->max_floor_victims = 12; c->min_rss_kib = 65536; c->log_mib = 10; c->log_keep = 5; } int uuid_valid(const char *s) { size_t i; if (strlen(s) != 36) return 0; for (i = 0; i < 36; i++) { if (i == 8 || i == 13 || i == 18 || i == 23) { if (s[i] != '-') return 0; } else if (!isdigit((unsigned char)s[i]) && !(s[i] >= 'a' && s[i] <= 'f')) return 0; } return strcmp(s, "00000000-0000-0000-0000-000000000000") != 0; } const zone_rule_t *zone_rule(const config_t *c, const char *uuid) { size_t i; for (i = 0; i < c->nzones; i++) if (!strcmp(c->zones[i].uuid, uuid)) return &c->zones[i]; return NULL; } int protected_process(const config_t *c, const process_t *p) { static const char *const builtin[] = {"init", "systemd", "svc.startd", "svc.configd", "sshd", "zoneadmd", "vminfod", "vmadmd", "qemu", "qemu-system-x86", "bhyve", "o6h-memory-guar", "o6h-memory-guard"}; const zone_rule_t *z = zone_rule(c, p->uuid); size_t i; if (p->pid <= 1 || p->ppid == 0 || p->zid <= 0 || p->initpid <= 0 || p->pid == p->initpid || p->nlwp == 0 || !p->zone_generation || !uuid_valid(p->uuid) || !z || z->policy != ALLOW || p->rss_kib < c->min_rss_kib) return 1; for (i = 0; i < sizeof (builtin) / sizeof (builtin[0]); i++) if (!strcmp(p->name, builtin[i])) return 1; for (i = 0; i < c->nnames; i++) if (!strcmp(p->name, c->protected_names[i])) return 1; for (i = 0; i < c->npids; i++) if (p->pid == c->protected_pids[i]) return 1; return 0; } static int token(const char *s, size_t max) { size_t i, n = strlen(s); if (!n || n >= max) return 0; for (i = 0; i < n; i++) if (!isalnum((unsigned char)s[i]) && !strchr("._-", s[i])) return 0; return 1; } static int number(const char *s, unsigned long lo, unsigned long hi, unsigned *v) { char *end; unsigned long n; if (!*s || *s == '-' || *s == '+') return -1; errno = 0; n = strtoul(s, &end, 10); if (errno || *end || n < lo || n > hi) return -1; *v = (unsigned)n; return 0; } int config_read(FILE *f, config_t *c, char *error, size_t cap) { char line[512], *key, *value, *extra, *ctx; unsigned lineno = 0, n, seen = 0; defaults(c); while (fgets(line, sizeof (line), f)) { char *comment; lineno++; if (!strchr(line, '\n') && !feof(f)) goto invalid; comment = strchr(line, '#'); if (comment) *comment = 0; ctx = NULL; key = strtok_r(line, " \t\r\n", &ctx); if (!key) continue; value = strtok_r(NULL, " \t\r\n", &ctx); if (!value) goto invalid; if (!strcmp(key, "zone")) { zone_rule_t *z; char *alias = strtok_r(NULL, " \t\r\n", &ctx); char *policy = strtok_r(NULL, " \t\r\n", &ctx); if (!alias || !policy || strtok_r(NULL, " \t\r\n", &ctx) || !uuid_valid(value) || !token(alias, 64) || c->nzones == MAX_ZONES || zone_rule(c, value)) goto invalid; z = &c->zones[c->nzones++]; strcpy(z->uuid, value); strcpy(z->alias, alias); if (!strcmp(policy, "allow")) z->policy = ALLOW; else if (!strcmp(policy, "protect")) z->policy = PROTECT; else if (!strcmp(policy, "observe")) z->policy = OBSERVE; else goto invalid; continue; } extra = strtok_r(NULL, " \t\r\n", &ctx); if (extra) goto invalid; if (!strcmp(key, "protect_process")) { if (!token(value, 16) || c->nnames == MAX_RULES) goto invalid; strcpy(c->protected_names[c->nnames++], value); continue; } if (!strcmp(key, "protect_pid")) { if (number(value, 1, 2147483647UL, &n) || c->npids == MAX_RULES) goto invalid; c->protected_pids[c->npids++] = (int)n; continue; } if (!strcmp(key, "ACTION_MODE")) { if (seen & 1u) goto invalid; seen |= 1u; if (!strcmp(value, "armed")) c->armed = 1; else if (strcmp(value, "dry-run")) goto invalid; continue; } if (!strcmp(key, "SYSLOG_ENABLED")) { if (seen & (1u << 19)) goto invalid; seen |= 1u << 19; if (!strcmp(value, "yes")) c->syslog_enabled = 1; else if (!strcmp(value, "no")) c->syslog_enabled = 0; else goto invalid; continue; } if (!strcmp(key, "SYSLOG_FACILITY")) { if ((seen & (1u << 20)) || strlen(value) != 6 || memcmp(value, "local", 5) || value[5] < '0' || value[5] > '7') goto invalid; seen |= 1u << 20; c->syslog_facility = value[5] - '0'; continue; } #define OPT(name, field, low, high, bit) \ if (!strcmp(key, name)) { \ if ((seen & (1u << bit)) || number(value, low, high, &n)) goto invalid; \ seen |= 1u << bit; c->field = n; continue; } OPT("PRE_SECONDS", pre_s, 1, 60, 1) OPT("PRESSURE_SECONDS", pressure_s, 2, 120, 2) OPT("CRITICAL_SECONDS", critical_s, 1, 10, 3) OPT("RECOVERY_SECONDS", recovery_s, 5, 300, 4) if (!strcmp(key, "TERM_GRACE_SECONDS") || !strcmp(key, "CRITICAL_TERM_GRACE_SECONDS")) { if ((seen & (1u << 5)) || number(value, 1, 30, &n)) goto invalid; seen |= 1u << 5; c->critical_term_s = n; continue; } OPT("NORMAL_SCAN_SECONDS", normal_scan_s, 1, 5, 6) OPT("SCAN_BUDGET_MS", scan_budget_ms, 10, 500, 7) OPT("COOLDOWN_SECONDS", cooldown_s, 1, 120, 8) OPT("MAX_VICTIMS_EPISODE", max_victims, 1, MAX_VICTIMS, 9) OPT("MAX_VICTIMS_MINUTE", max_per_minute, 1, 32, 10) OPT("MIN_RSS_KIB", min_rss_kib, 1024, 1073741824UL, 11) OPT("LOG_MAX_MIB", log_mib, 1, 100, 12) OPT("LOG_KEEP", log_keep, 1, 20, 13) OPT("EMERGENCY_TERM_GRACE_SECONDS", emergency_term_s, 0, 10, 14) OPT("FLOOR_KILL_DELAY_SECONDS", floor_kill_delay_s, 0, 5, 15) OPT("MAX_CACHED_CANDIDATE_AGE_SECONDS", max_cached_candidate_age_s, 5, 60, 16) OPT("MAX_EMERGENCY_VICTIMS_EPISODE", max_emergency_victims, 1, MAX_VICTIMS, 17) OPT("MAX_FLOOR_VICTIMS_EPISODE", max_floor_victims, 1, MAX_VICTIMS, 18) #undef OPT goto invalid; } if (ferror(f) || c->pressure_s < c->pre_s || c->max_emergency_victims < c->max_victims || c->max_floor_victims < c->max_emergency_victims) goto invalid; if (c->armed) { size_t i; int allow = 0; for (i = 0; i < c->nzones; i++) allow |= c->zones[i].policy == ALLOW; if (!allow) { snprintf(error, cap, "armed requires at least one allow UUID"); return -1; } } return 0; invalid: snprintf(error, cap, "invalid/duplicate/unknown configuration at line %u", lineno); return -1; }