Skip to content

Commit 2ca9bf4

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Allow using executable's realpath and symlink's target as conditions.
This patch adds support for permission checks using executable file's realpath upon execve() and symlink's target upon symlink(). Hooks are in the last patch of this pathset. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 8761afd commit 2ca9bf4

5 files changed

Lines changed: 228 additions & 4 deletions

File tree

‎security/tomoyo/audit.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,43 @@ char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
140140
{
141141
char *buf = NULL;
142142
const char *header = NULL;
143+
char *realpath = NULL;
144+
const char *symlink = NULL;
143145
int pos;
144146
const char *domainname = r->domain->domainname->name;
145147
header = tomoyo_print_header(r);
146148
if (!header)
147149
return NULL;
148150
/* +10 is for '\n' etc. and '\0'. */
149151
len += strlen(domainname) + strlen(header) + 10;
152+
if (r->ee) {
153+
struct file *file = r->ee->bprm->file;
154+
realpath = tomoyo_realpath_from_path(&file->f_path);
155+
if (!realpath)
156+
goto out;
157+
/* +80 is for " exec={ realpath=\"%s\" }" */
158+
len += strlen(realpath) + 80;
159+
} else if (r->obj && r->obj->symlink_target) {
160+
symlink = r->obj->symlink_target->name;
161+
/* +18 is for " symlink.target=\"%s\"" */
162+
len += 18 + strlen(symlink);
163+
}
150164
len = tomoyo_round2(len);
151165
buf = kzalloc(len, GFP_NOFS);
152166
if (!buf)
153167
goto out;
154168
len--;
155169
pos = snprintf(buf, len, "%s", header);
170+
if (realpath) {
171+
pos += snprintf(buf + pos, len - pos,
172+
" exec={ realpath=\"%s\" }", realpath);
173+
} else if (symlink)
174+
pos += snprintf(buf + pos, len - pos, " symlink.target=\"%s\"",
175+
symlink);
156176
pos += snprintf(buf + pos, len - pos, "\n%s\n", domainname);
157177
vsnprintf(buf + pos, len - pos, fmt, args);
158178
out:
179+
kfree(realpath);
159180
kfree(header);
160181
return buf;
161182
}

‎security/tomoyo/common.c‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ const char * const tomoyo_condition_keyword[TOMOYO_MAX_CONDITION_KEYWORD] = {
7979
[TOMOYO_MODE_OTHERS_READ] = "others_read",
8080
[TOMOYO_MODE_OTHERS_WRITE] = "others_write",
8181
[TOMOYO_MODE_OTHERS_EXECUTE] = "others_execute",
82+
[TOMOYO_EXEC_REALPATH] = "exec.realpath",
83+
[TOMOYO_SYMLINK_TARGET] = "symlink.target",
8284
[TOMOYO_PATH1_UID] = "path1.uid",
8385
[TOMOYO_PATH1_GID] = "path1.gid",
8486
[TOMOYO_PATH1_INO] = "path1.ino",
@@ -352,6 +354,27 @@ static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
352354
}
353355
}
354356

357+
/**
358+
* tomoyo_print_name_union_quoted - Print a tomoyo_name_union with a quote.
359+
*
360+
* @head: Pointer to "struct tomoyo_io_buffer".
361+
* @ptr: Pointer to "struct tomoyo_name_union".
362+
*
363+
* Returns nothing.
364+
*/
365+
static void tomoyo_print_name_union_quoted(struct tomoyo_io_buffer *head,
366+
const struct tomoyo_name_union *ptr)
367+
{
368+
if (ptr->group) {
369+
tomoyo_set_string(head, "@");
370+
tomoyo_set_string(head, ptr->group->group_name->name);
371+
} else {
372+
tomoyo_set_string(head, "\"");
373+
tomoyo_set_string(head, ptr->filename->name);
374+
tomoyo_set_string(head, "\"");
375+
}
376+
}
377+
355378
/**
356379
* tomoyo_print_number_union_nospace - Print a tomoyo_number_union without a space.
357380
*
@@ -1101,6 +1124,9 @@ static bool tomoyo_print_condition(struct tomoyo_io_buffer *head,
11011124
(typeof(condp)) (cond + 1);
11021125
const struct tomoyo_number_union *numbers_p =
11031126
(typeof(numbers_p)) (condp + condc);
1127+
const struct tomoyo_name_union *names_p =
1128+
(typeof(names_p))
1129+
(numbers_p + cond->numbers_count);
11041130
u16 skip;
11051131
for (skip = 0; skip < head->r.cond_index; skip++) {
11061132
const u8 left = condp->left;
@@ -1112,6 +1138,9 @@ static bool tomoyo_print_condition(struct tomoyo_io_buffer *head,
11121138
break;
11131139
}
11141140
switch (right) {
1141+
case TOMOYO_NAME_UNION:
1142+
names_p++;
1143+
break;
11151144
case TOMOYO_NUMBER_UNION:
11161145
numbers_p++;
11171146
break;
@@ -1138,6 +1167,10 @@ static bool tomoyo_print_condition(struct tomoyo_io_buffer *head,
11381167
}
11391168
tomoyo_set_string(head, match ? "=" : "!=");
11401169
switch (right) {
1170+
case TOMOYO_NAME_UNION:
1171+
tomoyo_print_name_union_quoted
1172+
(head, names_p++);
1173+
break;
11411174
case TOMOYO_NUMBER_UNION:
11421175
tomoyo_print_number_union_nospace
11431176
(head, numbers_p++);
@@ -1665,6 +1698,22 @@ static DEFINE_SPINLOCK(tomoyo_query_list_lock);
16651698
*/
16661699
static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
16671700

1701+
/**
1702+
* tomoyo_truncate - Truncate a line.
1703+
*
1704+
* @str: String to truncate.
1705+
*
1706+
* Returns length of truncated @str.
1707+
*/
1708+
static int tomoyo_truncate(char *str)
1709+
{
1710+
char *start = str;
1711+
while (*(unsigned char *) str > (unsigned char) ' ')
1712+
str++;
1713+
*str = '\0';
1714+
return strlen(start) + 1;
1715+
}
1716+
16681717
/**
16691718
* tomoyo_add_entry - Add an ACL to current thread's domain. Used by learning mode.
16701719
*
@@ -1676,6 +1725,8 @@ static atomic_t tomoyo_query_observers = ATOMIC_INIT(0);
16761725
static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
16771726
{
16781727
char *buffer;
1728+
char *realpath = NULL;
1729+
char *symlink = NULL;
16791730
char *cp = strchr(header, '\n');
16801731
int len;
16811732
if (!cp)
@@ -1685,10 +1736,25 @@ static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header)
16851736
return;
16861737
*cp++ = '\0';
16871738
len = strlen(cp) + 1;
1739+
/* strstr() will return NULL if ordering is wrong. */
1740+
if (*cp == 'f') {
1741+
realpath = strstr(header, " exec={ realpath=\"");
1742+
if (realpath) {
1743+
realpath += 8;
1744+
len += tomoyo_truncate(realpath) + 6;
1745+
}
1746+
symlink = strstr(header, " symlink.target=\"");
1747+
if (symlink)
1748+
len += tomoyo_truncate(symlink + 1) + 1;
1749+
}
16881750
buffer = kmalloc(len, GFP_NOFS);
16891751
if (!buffer)
16901752
return;
16911753
snprintf(buffer, len - 1, "%s", cp);
1754+
if (realpath)
1755+
tomoyo_addprintf(buffer, len, " exec.%s", realpath);
1756+
if (symlink)
1757+
tomoyo_addprintf(buffer, len, "%s", symlink);
16921758
tomoyo_normalize_line(buffer);
16931759
if (!tomoyo_write_domain2(domain->ns, &domain->acl_info_list, buffer,
16941760
false))

‎security/tomoyo/common.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ enum tomoyo_conditions_index {
7373
TOMOYO_MODE_OTHERS_READ, /* S_IROTH */
7474
TOMOYO_MODE_OTHERS_WRITE, /* S_IWOTH */
7575
TOMOYO_MODE_OTHERS_EXECUTE, /* S_IXOTH */
76+
TOMOYO_EXEC_REALPATH,
77+
TOMOYO_SYMLINK_TARGET,
7678
TOMOYO_PATH1_UID,
7779
TOMOYO_PATH1_GID,
7880
TOMOYO_PATH1_INO,
@@ -101,6 +103,7 @@ enum tomoyo_conditions_index {
101103
TOMOYO_PATH2_PARENT_PERM,
102104
TOMOYO_MAX_CONDITION_KEYWORD,
103105
TOMOYO_NUMBER_UNION,
106+
TOMOYO_NAME_UNION,
104107
};
105108

106109

@@ -351,6 +354,11 @@ struct tomoyo_request_info {
351354
* NULL if not dealing files.
352355
*/
353356
struct tomoyo_obj_info *obj;
357+
/*
358+
* For holding parameters specific to execve() request.
359+
* NULL if not dealing do_execve().
360+
*/
361+
struct tomoyo_execve *ee;
354362
struct tomoyo_domain_info *domain;
355363
/* For holding parameters. */
356364
union {
@@ -476,6 +484,20 @@ struct tomoyo_obj_info {
476484
* parent directory.
477485
*/
478486
struct tomoyo_mini_stat stat[TOMOYO_MAX_PATH_STAT];
487+
/*
488+
* Content of symbolic link to be created. NULL for operations other
489+
* than symlink().
490+
*/
491+
struct tomoyo_path_info *symlink_target;
492+
};
493+
494+
/* Structure for execve() operation. */
495+
struct tomoyo_execve {
496+
struct tomoyo_request_info r;
497+
struct tomoyo_obj_info obj;
498+
struct linux_binprm *bprm;
499+
/* For temporary use. */
500+
char *tmp; /* Size is TOMOYO_EXEC_TMPSIZE bytes */
479501
};
480502

481503
/* Structure for entries which follows "struct tomoyo_condition". */
@@ -494,9 +516,11 @@ struct tomoyo_condition {
494516
u32 size; /* Memory size allocated for this entry. */
495517
u16 condc; /* Number of conditions in this struct. */
496518
u16 numbers_count; /* Number of "struct tomoyo_number_union values". */
519+
u16 names_count; /* Number of "struct tomoyo_name_union names". */
497520
/*
498521
* struct tomoyo_condition_element condition[condc];
499522
* struct tomoyo_number_union values[numbers_count];
523+
* struct tomoyo_name_union names[names_count];
500524
*/
501525
};
502526

0 commit comments

Comments
 (0)