1 |
/* |
2 |
* fs/tomoyo_signal.c |
3 |
* |
4 |
* Implementation of the Domain-Based Mandatory Access Control. |
5 |
* |
6 |
* Copyright (C) 2005-2008 NTT DATA CORPORATION |
7 |
* |
8 |
* Version: 1.6.0-pre 2008/03/04 |
9 |
* |
10 |
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
11 |
* See README.ccs for ChangeLog. |
12 |
* |
13 |
*/ |
14 |
/***** TOMOYO Linux start. *****/ |
15 |
|
16 |
#include <linux/ccs_common.h> |
17 |
#include <linux/tomoyo.h> |
18 |
#include <linux/realpath.h> |
19 |
#include <linux/version.h> |
20 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24) |
21 |
#define find_task_by_pid find_task_by_vpid |
22 |
#endif |
23 |
|
24 |
/************************* AUDIT FUNCTIONS *************************/ |
25 |
|
26 |
static int AuditSignalLog(const int signal, const struct path_info *dest_domain, const bool is_granted, const u8 profile, const u8 mode) |
27 |
{ |
28 |
char *buf; |
29 |
int len; |
30 |
if (CanSaveAuditLog(is_granted) < 0) return -ENOMEM; |
31 |
len = dest_domain->total_len; |
32 |
if ((buf = InitAuditLog(&len, profile, mode, NULL)) == NULL) return -ENOMEM; |
33 |
snprintf(buf + strlen(buf), len - strlen(buf) - 1, KEYWORD_ALLOW_SIGNAL "%d %s\n", signal, dest_domain->name); |
34 |
return WriteAuditLog(buf, is_granted); |
35 |
} |
36 |
|
37 |
/************************* SIGNAL ACL HANDLER *************************/ |
38 |
|
39 |
static int AddSignalEntry(const int sig, const char *dest_pattern, struct domain_info *domain, const struct condition_list *condition, const bool is_delete) |
40 |
{ |
41 |
struct acl_info *ptr; |
42 |
struct signal_acl_record *acl; |
43 |
const struct path_info *saved_dest_pattern; |
44 |
const u16 hash = sig; |
45 |
int error = -ENOMEM; |
46 |
if (!domain) return -EINVAL; |
47 |
if (!dest_pattern || !IsCorrectDomain(dest_pattern, __FUNCTION__)) return -EINVAL; |
48 |
if ((saved_dest_pattern = SaveName(dest_pattern)) == NULL) return -ENOMEM; |
49 |
mutex_lock(&domain_acl_lock); |
50 |
if (!is_delete) { |
51 |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
52 |
if ((ptr->type & ~(ACL_DELETED | ACL_WITH_CONDITION)) != TYPE_SIGNAL_ACL) continue; |
53 |
if (GetConditionPart(ptr) != condition) continue; |
54 |
acl = container_of(ptr, struct signal_acl_record, head); |
55 |
if (acl->sig != hash || pathcmp(acl->domainname, saved_dest_pattern)) continue; |
56 |
error = AddDomainACL(NULL, ptr); |
57 |
goto out; |
58 |
} |
59 |
/* Not found. Append it to the tail. */ |
60 |
if ((acl = alloc_acl_element(TYPE_SIGNAL_ACL, condition)) == NULL) goto out; |
61 |
acl->sig = hash; |
62 |
acl->domainname = saved_dest_pattern; |
63 |
error = AddDomainACL(domain, &acl->head); |
64 |
} else { |
65 |
error = -ENOENT; |
66 |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
67 |
if ((ptr->type & ~ACL_WITH_CONDITION) != TYPE_SIGNAL_ACL) continue; |
68 |
if (GetConditionPart(ptr) != condition) continue; |
69 |
acl = container_of(ptr, struct signal_acl_record, head); |
70 |
if (acl->sig != hash || pathcmp(acl->domainname, saved_dest_pattern)) continue; |
71 |
error = DelDomainACL(ptr); |
72 |
break; |
73 |
} |
74 |
} |
75 |
out: ; |
76 |
mutex_unlock(&domain_acl_lock); |
77 |
return error; |
78 |
} |
79 |
|
80 |
int CheckSignalACL(const int sig, const int pid) |
81 |
{ |
82 |
struct domain_info *domain = current->domain_info; |
83 |
struct domain_info *dest = NULL; |
84 |
const char *dest_pattern; |
85 |
struct acl_info *ptr; |
86 |
const u16 hash = sig; |
87 |
const u8 profile = current->domain_info->profile; |
88 |
const u8 mode = CheckCCSFlags(CCS_TOMOYO_MAC_FOR_SIGNAL); |
89 |
const bool is_enforce = (mode == 3); |
90 |
bool found = false; |
91 |
if (!mode) return 0; |
92 |
if (!sig) return 0; /* No check for NULL signal. */ |
93 |
if (current->pid == pid) { |
94 |
AuditSignalLog(sig, domain->domainname, 1, profile, mode); |
95 |
return 0; /* No check for self. */ |
96 |
} |
97 |
{ /* Simplified checking. */ |
98 |
struct task_struct *p = NULL; |
99 |
read_lock(&tasklist_lock); |
100 |
if (pid > 0) p = find_task_by_pid((pid_t) pid); |
101 |
else if (pid == 0) p = current; |
102 |
else if (pid == -1) dest = &KERNEL_DOMAIN; |
103 |
else p = find_task_by_pid((pid_t) -pid); |
104 |
if (p) dest = p->domain_info; |
105 |
read_unlock(&tasklist_lock); |
106 |
if (!dest) return 0; /* I can't find destinatioin. */ |
107 |
} |
108 |
if (domain == dest) { |
109 |
AuditSignalLog(sig, dest->domainname, 1, profile, mode); |
110 |
return 0; |
111 |
} |
112 |
dest_pattern = dest->domainname->name; |
113 |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
114 |
struct signal_acl_record *acl; |
115 |
if ((ptr->type & ~ACL_WITH_CONDITION) != TYPE_SIGNAL_ACL) continue; |
116 |
acl = container_of(ptr, struct signal_acl_record, head); |
117 |
if (acl->sig == hash && CheckCondition(ptr, NULL)) { |
118 |
const int len = acl->domainname->total_len; |
119 |
if (strncmp(acl->domainname->name, dest_pattern, len)) continue; |
120 |
if (dest_pattern[len] != ' ' && dest_pattern[len] != '\0') continue; |
121 |
UpdateCondition(ptr); |
122 |
found = true; |
123 |
break; |
124 |
} |
125 |
} |
126 |
AuditSignalLog(sig, dest->domainname, found, profile, mode); |
127 |
if (found) return 0; |
128 |
if (TomoyoVerboseMode()) { |
129 |
printk("TOMOYO-%s: Signal %d to %s denied for %s\n", GetMSG(is_enforce), sig, GetLastName(dest), GetLastName(domain)); |
130 |
} |
131 |
if (is_enforce) return CheckSupervisor("%s\n" KEYWORD_ALLOW_SIGNAL "%d %s\n", domain->domainname->name, sig, dest_pattern); |
132 |
else if (mode == 1 && CheckDomainQuota(domain)) AddSignalEntry(sig, dest_pattern, domain, NULL, 0); |
133 |
return 0; |
134 |
} |
135 |
|
136 |
int AddSignalPolicy(char *data, struct domain_info *domain, const struct condition_list *condition, const bool is_delete) |
137 |
{ |
138 |
int sig; |
139 |
char *domainname = strchr(data, ' '); |
140 |
if (sscanf(data, "%d", &sig) == 1 && domainname && IsDomainDef(domainname + 1)) { |
141 |
return AddSignalEntry(sig, domainname + 1, domain, condition, is_delete); |
142 |
} |
143 |
return -EINVAL; |
144 |
} |
145 |
|
146 |
/***** TOMOYO Linux end. *****/ |