Skip to content

Commit 4c3e9e2

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Add numeric values grouping support.
This patch adds numeric values grouping support, which is useful for grouping numeric values such as file's UID, DAC's mode, ioctl()'s cmd number. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <jmorris@namei.org>
1 parent babcd37 commit 4c3e9e2

6 files changed

Lines changed: 454 additions & 1 deletion

File tree

‎security/tomoyo/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
obj-y = common.o realpath.o tomoyo.o domain.o file.o gc.o path_group.o
1+
obj-y = common.o realpath.o tomoyo.o domain.o file.o gc.o path_group.o number_group.o

‎security/tomoyo/common.c‎

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,159 @@ static bool tomoyo_print_name_union(struct tomoyo_io_buffer *head,
118118
return tomoyo_io_printf(head, " %s", ptr->filename->name);
119119
}
120120

121+
/**
122+
* tomoyo_parse_ulong - Parse an "unsigned long" value.
123+
*
124+
* @result: Pointer to "unsigned long".
125+
* @str: Pointer to string to parse.
126+
*
127+
* Returns value type on success, 0 otherwise.
128+
*
129+
* The @src is updated to point the first character after the value
130+
* on success.
131+
*/
132+
u8 tomoyo_parse_ulong(unsigned long *result, char **str)
133+
{
134+
const char *cp = *str;
135+
char *ep;
136+
int base = 10;
137+
if (*cp == '0') {
138+
char c = *(cp + 1);
139+
if (c == 'x' || c == 'X') {
140+
base = 16;
141+
cp += 2;
142+
} else if (c >= '0' && c <= '7') {
143+
base = 8;
144+
cp++;
145+
}
146+
}
147+
*result = simple_strtoul(cp, &ep, base);
148+
if (cp == ep)
149+
return 0;
150+
*str = ep;
151+
switch (base) {
152+
case 16:
153+
return TOMOYO_VALUE_TYPE_HEXADECIMAL;
154+
case 8:
155+
return TOMOYO_VALUE_TYPE_OCTAL;
156+
default:
157+
return TOMOYO_VALUE_TYPE_DECIMAL;
158+
}
159+
}
160+
161+
/**
162+
* tomoyo_print_ulong - Print an "unsigned long" value.
163+
*
164+
* @buffer: Pointer to buffer.
165+
* @buffer_len: Size of @buffer.
166+
* @value: An "unsigned long" value.
167+
* @type: Type of @value.
168+
*
169+
* Returns nothing.
170+
*/
171+
void tomoyo_print_ulong(char *buffer, const int buffer_len,
172+
const unsigned long value, const u8 type)
173+
{
174+
if (type == TOMOYO_VALUE_TYPE_DECIMAL)
175+
snprintf(buffer, buffer_len, "%lu", value);
176+
else if (type == TOMOYO_VALUE_TYPE_OCTAL)
177+
snprintf(buffer, buffer_len, "0%lo", value);
178+
else if (type == TOMOYO_VALUE_TYPE_HEXADECIMAL)
179+
snprintf(buffer, buffer_len, "0x%lX", value);
180+
else
181+
snprintf(buffer, buffer_len, "type(%u)", type);
182+
}
183+
184+
/**
185+
* tomoyo_print_number_union - Print a tomoyo_number_union.
186+
*
187+
* @head: Pointer to "struct tomoyo_io_buffer".
188+
* @ptr: Pointer to "struct tomoyo_number_union".
189+
*
190+
* Returns true on success, false otherwise.
191+
*/
192+
bool tomoyo_print_number_union(struct tomoyo_io_buffer *head,
193+
const struct tomoyo_number_union *ptr)
194+
{
195+
unsigned long min;
196+
unsigned long max;
197+
u8 min_type;
198+
u8 max_type;
199+
if (!tomoyo_io_printf(head, " "))
200+
return false;
201+
if (ptr->is_group)
202+
return tomoyo_io_printf(head, "@%s",
203+
ptr->group->group_name->name);
204+
min_type = ptr->min_type;
205+
max_type = ptr->max_type;
206+
min = ptr->values[0];
207+
max = ptr->values[1];
208+
switch (min_type) {
209+
case TOMOYO_VALUE_TYPE_HEXADECIMAL:
210+
if (!tomoyo_io_printf(head, "0x%lX", min))
211+
return false;
212+
break;
213+
case TOMOYO_VALUE_TYPE_OCTAL:
214+
if (!tomoyo_io_printf(head, "0%lo", min))
215+
return false;
216+
break;
217+
default:
218+
if (!tomoyo_io_printf(head, "%lu", min))
219+
return false;
220+
break;
221+
}
222+
if (min == max && min_type == max_type)
223+
return true;
224+
switch (max_type) {
225+
case TOMOYO_VALUE_TYPE_HEXADECIMAL:
226+
return tomoyo_io_printf(head, "-0x%lX", max);
227+
case TOMOYO_VALUE_TYPE_OCTAL:
228+
return tomoyo_io_printf(head, "-0%lo", max);
229+
default:
230+
return tomoyo_io_printf(head, "-%lu", max);
231+
}
232+
}
233+
234+
/**
235+
* tomoyo_parse_number_union - Parse a tomoyo_number_union.
236+
*
237+
* @data: Number or number range or number group.
238+
* @ptr: Pointer to "struct tomoyo_number_union".
239+
*
240+
* Returns true on success, false otherwise.
241+
*/
242+
bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num)
243+
{
244+
u8 type;
245+
unsigned long v;
246+
memset(num, 0, sizeof(*num));
247+
if (data[0] == '@') {
248+
if (!tomoyo_is_correct_path(data, 0, 0, 0))
249+
return false;
250+
num->group = tomoyo_get_number_group(data + 1);
251+
num->is_group = true;
252+
return num->group != NULL;
253+
}
254+
type = tomoyo_parse_ulong(&v, &data);
255+
if (!type)
256+
return false;
257+
num->values[0] = v;
258+
num->min_type = type;
259+
if (!*data) {
260+
num->values[1] = v;
261+
num->max_type = type;
262+
return true;
263+
}
264+
if (*data++ != '-')
265+
return false;
266+
type = tomoyo_parse_ulong(&v, &data);
267+
if (!type || *data)
268+
return false;
269+
num->values[1] = v;
270+
num->max_type = type;
271+
return true;
272+
}
273+
121274
/**
122275
* tomoyo_is_byte_range - Check whether the string isa \ooo style octal value.
123276
*
@@ -1750,6 +1903,8 @@ static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
17501903
return tomoyo_write_no_rewrite_policy(data, is_delete);
17511904
if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_PATH_GROUP))
17521905
return tomoyo_write_path_group_policy(data, is_delete);
1906+
if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_NUMBER_GROUP))
1907+
return tomoyo_write_number_group_policy(data, is_delete);
17531908
return -EINVAL;
17541909
}
17551910

@@ -1812,6 +1967,12 @@ static int tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
18121967
head->read_var2 = NULL;
18131968
head->read_step = 10;
18141969
case 10:
1970+
if (!tomoyo_read_number_group_policy(head))
1971+
break;
1972+
head->read_var1 = NULL;
1973+
head->read_var2 = NULL;
1974+
head->read_step = 11;
1975+
case 11:
18151976
head->read_eof = true;
18161977
break;
18171978
default:

‎security/tomoyo/common.h‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,20 @@ struct linux_binprm;
5555
#define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN "no_initialize_domain "
5656
#define TOMOYO_KEYWORD_NO_KEEP_DOMAIN "no_keep_domain "
5757
#define TOMOYO_KEYWORD_PATH_GROUP "path_group "
58+
#define TOMOYO_KEYWORD_NUMBER_GROUP "number_group "
5859
#define TOMOYO_KEYWORD_SELECT "select "
5960
#define TOMOYO_KEYWORD_USE_PROFILE "use_profile "
6061
#define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "ignore_global_allow_read"
6162
/* A domain definition starts with <kernel>. */
6263
#define TOMOYO_ROOT_NAME "<kernel>"
6364
#define TOMOYO_ROOT_NAME_LEN (sizeof(TOMOYO_ROOT_NAME) - 1)
6465

66+
/* Value type definition. */
67+
#define TOMOYO_VALUE_TYPE_INVALID 0
68+
#define TOMOYO_VALUE_TYPE_DECIMAL 1
69+
#define TOMOYO_VALUE_TYPE_OCTAL 2
70+
#define TOMOYO_VALUE_TYPE_HEXADECIMAL 3
71+
6572
/* Index numbers for Access Controls. */
6673
enum tomoyo_mac_index {
6774
TOMOYO_MAC_FOR_FILE, /* domain_policy.conf */
@@ -211,6 +218,14 @@ struct tomoyo_name_union {
211218
u8 is_group;
212219
};
213220

221+
struct tomoyo_number_union {
222+
unsigned long values[2];
223+
struct tomoyo_number_group *group;
224+
u8 min_type;
225+
u8 max_type;
226+
u8 is_group;
227+
};
228+
214229
/* Structure for "path_group" directive. */
215230
struct tomoyo_path_group {
216231
struct list_head list;
@@ -219,13 +234,28 @@ struct tomoyo_path_group {
219234
atomic_t users;
220235
};
221236

237+
/* Structure for "number_group" directive. */
238+
struct tomoyo_number_group {
239+
struct list_head list;
240+
const struct tomoyo_path_info *group_name;
241+
struct list_head member_list;
242+
atomic_t users;
243+
};
244+
222245
/* Structure for "path_group" directive. */
223246
struct tomoyo_path_group_member {
224247
struct list_head list;
225248
bool is_deleted;
226249
const struct tomoyo_path_info *member_name;
227250
};
228251

252+
/* Structure for "number_group" directive. */
253+
struct tomoyo_number_group_member {
254+
struct list_head list;
255+
bool is_deleted;
256+
struct tomoyo_number_union number;
257+
};
258+
229259
/*
230260
* tomoyo_acl_info is a structure which is used for holding
231261
*
@@ -554,9 +584,18 @@ bool tomoyo_parse_name_union(const char *filename,
554584
bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
555585
const struct tomoyo_path_group *group,
556586
const bool may_use_pattern);
587+
/* Check whether the given value matches the given number_group. */
588+
bool tomoyo_number_matches_group(const unsigned long min,
589+
const unsigned long max,
590+
const struct tomoyo_number_group *group);
557591
/* Check whether the given filename matches the given pattern. */
558592
bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
559593
const struct tomoyo_path_info *pattern);
594+
595+
bool tomoyo_print_number_union(struct tomoyo_io_buffer *head,
596+
const struct tomoyo_number_union *ptr);
597+
bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num);
598+
560599
/* Read "alias" entry in exception policy. */
561600
bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head);
562601
/*
@@ -570,6 +609,8 @@ bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head);
570609
bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head);
571610
/* Read "path_group" entry in exception policy. */
572611
bool tomoyo_read_path_group_policy(struct tomoyo_io_buffer *head);
612+
/* Read "number_group" entry in exception policy. */
613+
bool tomoyo_read_number_group_policy(struct tomoyo_io_buffer *head);
573614
/* Read "allow_read" entry in exception policy. */
574615
bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head);
575616
/* Read "deny_rewrite" entry in exception policy. */
@@ -614,6 +655,8 @@ int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
614655
int tomoyo_write_pattern_policy(char *data, const bool is_delete);
615656
/* Create "path_group" entry in exception policy. */
616657
int tomoyo_write_path_group_policy(char *data, const bool is_delete);
658+
/* Create "number_group" entry in exception policy. */
659+
int tomoyo_write_number_group_policy(char *data, const bool is_delete);
617660
/* Find a domain by the given name. */
618661
struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
619662
/* Find or create a domain by the given name. */
@@ -623,6 +666,7 @@ struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
623666

624667
/* Allocate memory for "struct tomoyo_path_group". */
625668
struct tomoyo_path_group *tomoyo_get_path_group(const char *group_name);
669+
struct tomoyo_number_group *tomoyo_get_number_group(const char *group_name);
626670

627671
/* Check mode for specified functionality. */
628672
unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
@@ -632,6 +676,8 @@ void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
632676
/* Run policy loader when /sbin/init starts. */
633677
void tomoyo_load_policy(const char *filename);
634678

679+
void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
680+
635681
/* Convert binary string to ascii string. */
636682
int tomoyo_encode(char *buffer, int buflen, const char *str);
637683

@@ -697,6 +743,7 @@ extern struct srcu_struct tomoyo_ss;
697743
extern struct list_head tomoyo_domain_list;
698744

699745
extern struct list_head tomoyo_path_group_list;
746+
extern struct list_head tomoyo_number_group_list;
700747
extern struct list_head tomoyo_domain_initializer_list;
701748
extern struct list_head tomoyo_domain_keeper_list;
702749
extern struct list_head tomoyo_alias_list;
@@ -773,6 +820,12 @@ static inline void tomoyo_put_path_group(struct tomoyo_path_group *group)
773820
atomic_dec(&group->users);
774821
}
775822

823+
static inline void tomoyo_put_number_group(struct tomoyo_number_group *group)
824+
{
825+
if (group)
826+
atomic_dec(&group->users);
827+
}
828+
776829
static inline struct tomoyo_domain_info *tomoyo_domain(void)
777830
{
778831
return current_cred()->security;
@@ -797,6 +850,14 @@ static inline bool tomoyo_is_same_name_union
797850
p1->is_group == p2->is_group;
798851
}
799852

853+
static inline bool tomoyo_is_same_number_union
854+
(const struct tomoyo_number_union *p1, const struct tomoyo_number_union *p2)
855+
{
856+
return p1->values[0] == p2->values[0] && p1->values[1] == p2->values[1]
857+
&& p1->group == p2->group && p1->min_type == p2->min_type &&
858+
p1->max_type == p2->max_type && p1->is_group == p2->is_group;
859+
}
860+
800861
static inline bool tomoyo_is_same_path_acl(const struct tomoyo_path_acl *p1,
801862
const struct tomoyo_path_acl *p2)
802863
{

‎security/tomoyo/file.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ static bool tomoyo_compare_name_union_pattern(const struct tomoyo_path_info
7676
return false;
7777
}
7878

79+
void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
80+
{
81+
if (ptr && ptr->is_group)
82+
tomoyo_put_number_group(ptr->group);
83+
}
84+
85+
bool tomoyo_compare_number_union(const unsigned long value,
86+
const struct tomoyo_number_union *ptr)
87+
{
88+
if (ptr->is_group)
89+
return tomoyo_number_matches_group(value, value, ptr->group);
90+
return value >= ptr->values[0] && value <= ptr->values[1];
91+
}
92+
7993
/**
8094
* tomoyo_path2keyword - Get the name of single path operation.
8195
*

0 commit comments

Comments
 (0)