Skip to content

Commit 731d37a

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Allow domain transition without execve().
To be able to split permissions for Apache's CGI programs which are executed without execve(), add special domain transition which is performed by writing a TOMOYO's domainname to /sys/kernel/security/tomoyo/self_domain interface. This is an API for TOMOYO-aware userland applications. However, since I expect TOMOYO and other LSM modules to run in parallel, this patch does not use /proc/self/attr/ interface in order to avoid conflicts with other LSM modules when it became possible to run multiple LSM modules in parallel. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 1f067a6 commit 731d37a

4 files changed

Lines changed: 210 additions & 28 deletions

File tree

‎security/tomoyo/common.c‎

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,48 @@ static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
10101010
return true;
10111011
}
10121012

1013+
/**
1014+
* tomoyo_same_task_acl - Check for duplicated "struct tomoyo_task_acl" entry.
1015+
*
1016+
* @a: Pointer to "struct tomoyo_acl_info".
1017+
* @b: Pointer to "struct tomoyo_acl_info".
1018+
*
1019+
* Returns true if @a == @b, false otherwise.
1020+
*/
1021+
static bool tomoyo_same_task_acl(const struct tomoyo_acl_info *a,
1022+
const struct tomoyo_acl_info *b)
1023+
{
1024+
const struct tomoyo_task_acl *p1 = container_of(a, typeof(*p1), head);
1025+
const struct tomoyo_task_acl *p2 = container_of(b, typeof(*p2), head);
1026+
return p1->domainname == p2->domainname;
1027+
}
1028+
1029+
/**
1030+
* tomoyo_write_task - Update task related list.
1031+
*
1032+
* @param: Pointer to "struct tomoyo_acl_param".
1033+
*
1034+
* Returns 0 on success, negative value otherwise.
1035+
*
1036+
* Caller holds tomoyo_read_lock().
1037+
*/
1038+
static int tomoyo_write_task(struct tomoyo_acl_param *param)
1039+
{
1040+
int error = -EINVAL;
1041+
if (tomoyo_str_starts(&param->data, "manual_domain_transition ")) {
1042+
struct tomoyo_task_acl e = {
1043+
.head.type = TOMOYO_TYPE_MANUAL_TASK_ACL,
1044+
.domainname = tomoyo_get_domainname(param),
1045+
};
1046+
if (e.domainname)
1047+
error = tomoyo_update_domain(&e.head, sizeof(e), param,
1048+
tomoyo_same_task_acl,
1049+
NULL);
1050+
tomoyo_put_name(e.domainname);
1051+
}
1052+
return error;
1053+
}
1054+
10131055
/**
10141056
* tomoyo_delete_domain - Delete a domain.
10151057
*
@@ -1068,11 +1110,12 @@ static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns,
10681110
static const struct {
10691111
const char *keyword;
10701112
int (*write) (struct tomoyo_acl_param *);
1071-
} tomoyo_callback[4] = {
1113+
} tomoyo_callback[5] = {
10721114
{ "file ", tomoyo_write_file },
10731115
{ "network inet ", tomoyo_write_inet_network },
10741116
{ "network unix ", tomoyo_write_unix_network },
10751117
{ "misc ", tomoyo_write_misc },
1118+
{ "task ", tomoyo_write_task },
10761119
};
10771120
u8 i;
10781121

@@ -1343,6 +1386,12 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
13431386
if (first)
13441387
return true;
13451388
tomoyo_print_name_union(head, &ptr->name);
1389+
} else if (acl_type == TOMOYO_TYPE_MANUAL_TASK_ACL) {
1390+
struct tomoyo_task_acl *ptr =
1391+
container_of(acl, typeof(*ptr), head);
1392+
tomoyo_set_group(head, "task ");
1393+
tomoyo_set_string(head, "manual_domain_transition ");
1394+
tomoyo_set_string(head, ptr->domainname->name);
13461395
} else if (head->r.print_transition_related_only) {
13471396
return true;
13481397
} else if (acl_type == TOMOYO_TYPE_PATH2_ACL) {
@@ -2178,26 +2227,6 @@ static void tomoyo_read_version(struct tomoyo_io_buffer *head)
21782227
}
21792228
}
21802229

2181-
/**
2182-
* tomoyo_read_self_domain - Get the current process's domainname.
2183-
*
2184-
* @head: Pointer to "struct tomoyo_io_buffer".
2185-
*
2186-
* Returns the current process's domainname.
2187-
*/
2188-
static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
2189-
{
2190-
if (!head->r.eof) {
2191-
/*
2192-
* tomoyo_domain()->domainname != NULL
2193-
* because every process belongs to a domain and
2194-
* the domain's name cannot be NULL.
2195-
*/
2196-
tomoyo_io_printf(head, "%s", tomoyo_domain()->domainname->name);
2197-
head->r.eof = true;
2198-
}
2199-
}
2200-
22012230
/* String table for /sys/kernel/security/tomoyo/stat interface. */
22022231
static const char * const tomoyo_policy_headers[TOMOYO_MAX_POLICY_STAT] = {
22032232
[TOMOYO_STAT_POLICY_UPDATES] = "update:",
@@ -2328,10 +2357,6 @@ int tomoyo_open_control(const u8 type, struct file *file)
23282357
head->poll = tomoyo_poll_log;
23292358
head->read = tomoyo_read_log;
23302359
break;
2331-
case TOMOYO_SELFDOMAIN:
2332-
/* /sys/kernel/security/tomoyo/self_domain */
2333-
head->read = tomoyo_read_self_domain;
2334-
break;
23352360
case TOMOYO_PROCESS_STATUS:
23362361
/* /sys/kernel/security/tomoyo/.process_status */
23372362
head->write = tomoyo_write_pid;

‎security/tomoyo/common.h‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ enum tomoyo_acl_entry_type_index {
227227
TOMOYO_TYPE_INET_ACL,
228228
TOMOYO_TYPE_UNIX_ACL,
229229
TOMOYO_TYPE_ENV_ACL,
230+
TOMOYO_TYPE_MANUAL_TASK_ACL,
230231
};
231232

232233
/* Index numbers for access controls with one pathname. */
@@ -295,7 +296,6 @@ enum tomoyo_securityfs_interface_index {
295296
TOMOYO_EXCEPTIONPOLICY,
296297
TOMOYO_PROCESS_STATUS,
297298
TOMOYO_STAT,
298-
TOMOYO_SELFDOMAIN,
299299
TOMOYO_AUDIT,
300300
TOMOYO_VERSION,
301301
TOMOYO_PROFILE,
@@ -480,6 +480,9 @@ struct tomoyo_request_info {
480480
unsigned long flags;
481481
int need_dev;
482482
} mount;
483+
struct {
484+
const struct tomoyo_path_info *domainname;
485+
} task;
483486
} param;
484487
struct tomoyo_acl_info *matched_acl;
485488
u8 param_type;
@@ -679,6 +682,15 @@ struct tomoyo_domain_info {
679682
atomic_t users; /* Number of referring credentials. */
680683
};
681684

685+
/*
686+
* Structure for "task manual_domain_transition" directive.
687+
*/
688+
struct tomoyo_task_acl {
689+
struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MANUAL_TASK_ACL */
690+
/* Pointer to domainname. */
691+
const struct tomoyo_path_info *domainname;
692+
};
693+
682694
/*
683695
* Structure for "file execute", "file read", "file write", "file append",
684696
* "file unlink", "file getattr", "file rmdir", "file truncate",
@@ -935,6 +947,8 @@ const char *tomoyo_get_exe(void);
935947
const char *tomoyo_yesno(const unsigned int value);
936948
const struct tomoyo_path_info *tomoyo_compare_name_union
937949
(const struct tomoyo_path_info *name, const struct tomoyo_name_union *ptr);
950+
const struct tomoyo_path_info *tomoyo_get_domainname
951+
(struct tomoyo_acl_param *param);
938952
const struct tomoyo_path_info *tomoyo_get_name(const char *name);
939953
const struct tomoyo_path_info *tomoyo_path_matches_group
940954
(const struct tomoyo_path_info *pathname, const struct tomoyo_group *group);

‎security/tomoyo/securityfs_if.c‎

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,124 @@
77
#include <linux/security.h>
88
#include "common.h"
99

10+
/**
11+
* tomoyo_check_task_acl - Check permission for task operation.
12+
*
13+
* @r: Pointer to "struct tomoyo_request_info".
14+
* @ptr: Pointer to "struct tomoyo_acl_info".
15+
*
16+
* Returns true if granted, false otherwise.
17+
*/
18+
static bool tomoyo_check_task_acl(struct tomoyo_request_info *r,
19+
const struct tomoyo_acl_info *ptr)
20+
{
21+
const struct tomoyo_task_acl *acl = container_of(ptr, typeof(*acl),
22+
head);
23+
return !tomoyo_pathcmp(r->param.task.domainname, acl->domainname);
24+
}
25+
26+
/**
27+
* tomoyo_write_self - write() for /sys/kernel/security/tomoyo/self_domain interface.
28+
*
29+
* @file: Pointer to "struct file".
30+
* @buf: Domainname to transit to.
31+
* @count: Size of @buf.
32+
* @ppos: Unused.
33+
*
34+
* Returns @count on success, negative value otherwise.
35+
*
36+
* If domain transition was permitted but the domain transition failed, this
37+
* function returns error rather than terminating current thread with SIGKILL.
38+
*/
39+
static ssize_t tomoyo_write_self(struct file *file, const char __user *buf,
40+
size_t count, loff_t *ppos)
41+
{
42+
char *data;
43+
int error;
44+
if (!count || count >= TOMOYO_EXEC_TMPSIZE - 10)
45+
return -ENOMEM;
46+
data = kzalloc(count + 1, GFP_NOFS);
47+
if (!data)
48+
return -ENOMEM;
49+
if (copy_from_user(data, buf, count)) {
50+
error = -EFAULT;
51+
goto out;
52+
}
53+
tomoyo_normalize_line(data);
54+
if (tomoyo_correct_domain(data)) {
55+
const int idx = tomoyo_read_lock();
56+
struct tomoyo_path_info name;
57+
struct tomoyo_request_info r;
58+
name.name = data;
59+
tomoyo_fill_path_info(&name);
60+
/* Check "task manual_domain_transition" permission. */
61+
tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
62+
r.param_type = TOMOYO_TYPE_MANUAL_TASK_ACL;
63+
r.param.task.domainname = &name;
64+
tomoyo_check_acl(&r, tomoyo_check_task_acl);
65+
if (!r.granted)
66+
error = -EPERM;
67+
else {
68+
struct tomoyo_domain_info *new_domain =
69+
tomoyo_assign_domain(data, true);
70+
if (!new_domain) {
71+
error = -ENOENT;
72+
} else {
73+
struct cred *cred = prepare_creds();
74+
if (!cred) {
75+
error = -ENOMEM;
76+
} else {
77+
struct tomoyo_domain_info *old_domain =
78+
cred->security;
79+
cred->security = new_domain;
80+
atomic_inc(&new_domain->users);
81+
atomic_dec(&old_domain->users);
82+
commit_creds(cred);
83+
error = 0;
84+
}
85+
}
86+
}
87+
tomoyo_read_unlock(idx);
88+
} else
89+
error = -EINVAL;
90+
out:
91+
kfree(data);
92+
return error ? error : count;
93+
}
94+
95+
/**
96+
* tomoyo_read_self - read() for /sys/kernel/security/tomoyo/self_domain interface.
97+
*
98+
* @file: Pointer to "struct file".
99+
* @buf: Domainname which current thread belongs to.
100+
* @count: Size of @buf.
101+
* @ppos: Bytes read by now.
102+
*
103+
* Returns read size on success, negative value otherwise.
104+
*/
105+
static ssize_t tomoyo_read_self(struct file *file, char __user *buf,
106+
size_t count, loff_t *ppos)
107+
{
108+
const char *domain = tomoyo_domain()->domainname->name;
109+
loff_t len = strlen(domain);
110+
loff_t pos = *ppos;
111+
if (pos >= len || !count)
112+
return 0;
113+
len -= pos;
114+
if (count < len)
115+
len = count;
116+
if (copy_to_user(buf, domain + pos, len))
117+
return -EFAULT;
118+
*ppos += len;
119+
return len;
120+
}
121+
122+
/* Operations for /sys/kernel/security/tomoyo/self_domain interface. */
123+
static const struct file_operations tomoyo_self_operations = {
124+
.write = tomoyo_write_self,
125+
.read = tomoyo_read_self,
126+
};
127+
10128
/**
11129
* tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
12130
*
@@ -135,8 +253,6 @@ static int __init tomoyo_initerface_init(void)
135253
TOMOYO_EXCEPTIONPOLICY);
136254
tomoyo_create_entry("audit", 0400, tomoyo_dir,
137255
TOMOYO_AUDIT);
138-
tomoyo_create_entry("self_domain", 0400, tomoyo_dir,
139-
TOMOYO_SELFDOMAIN);
140256
tomoyo_create_entry(".process_status", 0600, tomoyo_dir,
141257
TOMOYO_PROCESS_STATUS);
142258
tomoyo_create_entry("stat", 0644, tomoyo_dir,
@@ -147,6 +263,8 @@ static int __init tomoyo_initerface_init(void)
147263
TOMOYO_MANAGER);
148264
tomoyo_create_entry("version", 0400, tomoyo_dir,
149265
TOMOYO_VERSION);
266+
securityfs_create_file("self_domain", 0666, tomoyo_dir, NULL,
267+
&tomoyo_self_operations);
150268
return 0;
151269
}
152270

‎security/tomoyo/util.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,31 @@ char *tomoyo_read_token(struct tomoyo_acl_param *param)
158158
return pos;
159159
}
160160

161+
/**
162+
* tomoyo_get_domainname - Read a domainname from a line.
163+
*
164+
* @param: Pointer to "struct tomoyo_acl_param".
165+
*
166+
* Returns a domainname on success, NULL otherwise.
167+
*/
168+
const struct tomoyo_path_info *tomoyo_get_domainname
169+
(struct tomoyo_acl_param *param)
170+
{
171+
char *start = param->data;
172+
char *pos = start;
173+
while (*pos) {
174+
if (*pos++ != ' ' || *pos++ == '/')
175+
continue;
176+
pos -= 2;
177+
*pos++ = '\0';
178+
break;
179+
}
180+
param->data = pos;
181+
if (tomoyo_correct_domain(start))
182+
return tomoyo_get_name(start);
183+
return NULL;
184+
}
185+
161186
/**
162187
* tomoyo_parse_ulong - Parse an "unsigned long" value.
163188
*

0 commit comments

Comments
 (0)