2016-12-02 11:58:11 +11:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "sway/config.h"
|
|
|
|
#include "sway/security.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
2016-12-02 13:36:43 +11:00
|
|
|
struct feature_policy *alloc_feature_policy(const char *program) {
|
2016-12-04 04:38:42 +11:00
|
|
|
uint32_t default_policy = 0;
|
|
|
|
for (int i = 0; i < config->feature_policies->length; ++i) {
|
|
|
|
struct feature_policy *policy = config->feature_policies->items[i];
|
|
|
|
if (strcmp(policy->program, "*") == 0) {
|
|
|
|
default_policy = policy->features;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 13:36:43 +11:00
|
|
|
struct feature_policy *policy = malloc(sizeof(struct feature_policy));
|
2016-12-16 10:10:29 +11:00
|
|
|
if (!policy) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-12-02 13:36:43 +11:00
|
|
|
policy->program = strdup(program);
|
2016-12-16 10:10:29 +11:00
|
|
|
if (!policy->program) {
|
|
|
|
free(policy);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-12-04 04:38:42 +11:00
|
|
|
policy->features = default_policy;
|
2016-12-02 13:36:43 +11:00
|
|
|
return policy;
|
|
|
|
}
|
|
|
|
|
2016-12-03 00:10:03 +11:00
|
|
|
struct command_policy *alloc_command_policy(const char *command) {
|
|
|
|
struct command_policy *policy = malloc(sizeof(struct command_policy));
|
2016-12-16 10:10:29 +11:00
|
|
|
if (!policy) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-12-03 00:10:03 +11:00
|
|
|
policy->command = strdup(command);
|
2016-12-16 10:10:29 +11:00
|
|
|
if (!policy->command) {
|
|
|
|
free(policy);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-12-04 04:38:42 +11:00
|
|
|
policy->context = 0;
|
2016-12-03 00:10:03 +11:00
|
|
|
return policy;
|
|
|
|
}
|
|
|
|
|
2016-12-02 11:58:11 +11:00
|
|
|
enum secure_feature get_feature_policy(pid_t pid) {
|
2016-12-08 23:34:08 +11:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
const char *fmt = "/proc/%d/file";
|
|
|
|
#else
|
2016-12-02 11:58:11 +11:00
|
|
|
const char *fmt = "/proc/%d/exe";
|
2016-12-08 23:34:08 +11:00
|
|
|
#endif
|
2016-12-02 11:58:11 +11:00
|
|
|
int pathlen = snprintf(NULL, 0, fmt, pid);
|
|
|
|
char *path = malloc(pathlen + 1);
|
2016-12-16 10:10:29 +11:00
|
|
|
if (path) {
|
|
|
|
snprintf(path, pathlen + 1, fmt, pid);
|
|
|
|
}
|
2016-12-02 11:58:11 +11:00
|
|
|
static char link[2048];
|
|
|
|
|
2016-12-04 04:38:42 +11:00
|
|
|
uint32_t default_policy = 0;
|
2016-12-02 11:58:11 +11:00
|
|
|
|
2016-12-16 10:10:29 +11:00
|
|
|
ssize_t len = !path ? -1 : readlink(path, link, sizeof(link));
|
2016-12-02 11:58:11 +11:00
|
|
|
if (len < 0) {
|
|
|
|
sway_log(L_INFO,
|
|
|
|
"WARNING: unable to read %s for security check. Using default policy.",
|
|
|
|
path);
|
|
|
|
strcpy(link, "*");
|
|
|
|
} else {
|
|
|
|
link[len] = '\0';
|
|
|
|
}
|
2016-12-03 10:57:10 +11:00
|
|
|
free(path);
|
2016-12-02 11:58:11 +11:00
|
|
|
|
|
|
|
for (int i = 0; i < config->feature_policies->length; ++i) {
|
|
|
|
struct feature_policy *policy = config->feature_policies->items[i];
|
2016-12-02 13:51:07 +11:00
|
|
|
if (strcmp(policy->program, "*") == 0) {
|
2016-12-02 11:58:11 +11:00
|
|
|
default_policy = policy->features;
|
|
|
|
}
|
|
|
|
if (strcmp(policy->program, link) == 0) {
|
|
|
|
return policy->features;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return default_policy;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum command_context get_command_policy(const char *cmd) {
|
2016-12-04 04:38:42 +11:00
|
|
|
uint32_t default_policy = 0;
|
2016-12-02 11:58:11 +11:00
|
|
|
|
|
|
|
for (int i = 0; i < config->command_policies->length; ++i) {
|
|
|
|
struct command_policy *policy = config->command_policies->items[i];
|
2016-12-04 04:38:42 +11:00
|
|
|
if (strcmp(policy->command, "*") == 0) {
|
|
|
|
default_policy = policy->context;
|
|
|
|
}
|
2016-12-02 11:58:11 +11:00
|
|
|
if (strcmp(policy->command, cmd) == 0) {
|
|
|
|
return policy->context;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return default_policy;
|
|
|
|
}
|
2016-12-03 00:17:45 +11:00
|
|
|
|
|
|
|
const char *command_policy_str(enum command_context context) {
|
|
|
|
switch (context) {
|
|
|
|
case CONTEXT_ALL:
|
|
|
|
return "all";
|
|
|
|
case CONTEXT_CONFIG:
|
|
|
|
return "config";
|
|
|
|
case CONTEXT_BINDING:
|
|
|
|
return "binding";
|
|
|
|
case CONTEXT_IPC:
|
|
|
|
return "IPC";
|
|
|
|
case CONTEXT_CRITERIA:
|
|
|
|
return "criteria";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|