Skip to content

Commit d5ca172

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Simplify profile structure.
Remove global preference from profile structure in order to make code simpler. Due to this structure change, printk() warnings upon policy violation are temporarily disabled. They will be replaced by /sys/kernel/security/tomoyo/audit by next patch. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 0d2171d commit d5ca172

3 files changed

Lines changed: 83 additions & 168 deletions

File tree

‎security/tomoyo/common.c‎

Lines changed: 69 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@
1111
#include <linux/security.h>
1212
#include "common.h"
1313

14-
static struct tomoyo_profile tomoyo_default_profile = {
15-
.learning = &tomoyo_default_profile.preference,
16-
.permissive = &tomoyo_default_profile.preference,
17-
.enforcing = &tomoyo_default_profile.preference,
18-
.preference.enforcing_verbose = true,
19-
.preference.learning_max_entry = 2048,
20-
.preference.learning_verbose = false,
21-
.preference.permissive_verbose = true
22-
};
23-
2414
/* Profile version. Currently only 20090903 is defined. */
2515
static unsigned int tomoyo_profile_version;
2616

@@ -61,6 +51,11 @@ static const char *tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
6151
[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAC_CATEGORY_FILE] = "file",
6252
};
6353

54+
/* String table for PREFERENCE keyword. */
55+
static const char * const tomoyo_pref_keywords[TOMOYO_MAX_PREF] = {
56+
[TOMOYO_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
57+
};
58+
6459
/* Permit policy management by non-root user? */
6560
static bool tomoyo_manage_by_non_root;
6661

@@ -71,11 +66,22 @@ static bool tomoyo_manage_by_non_root;
7166
*
7267
* @value: Bool value.
7368
*/
69+
/*
7470
static const char *tomoyo_yesno(const unsigned int value)
7571
{
7672
return value ? "yes" : "no";
7773
}
74+
*/
7875

76+
/**
77+
* tomoyo_addprintf - strncat()-like-snprintf().
78+
*
79+
* @buffer: Buffer to write to. Must be '\0'-terminated.
80+
* @len: Size of @buffer.
81+
* @fmt: The printf()'s format string, followed by parameters.
82+
*
83+
* Returns nothing.
84+
*/
7985
static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...)
8086
{
8187
va_list args;
@@ -294,12 +300,10 @@ static struct tomoyo_profile *tomoyo_assign_profile(const unsigned int profile)
294300
ptr = tomoyo_profile_ptr[profile];
295301
if (!ptr && tomoyo_memory_ok(entry)) {
296302
ptr = entry;
297-
ptr->learning = &tomoyo_default_profile.preference;
298-
ptr->permissive = &tomoyo_default_profile.preference;
299-
ptr->enforcing = &tomoyo_default_profile.preference;
300303
ptr->default_config = TOMOYO_CONFIG_DISABLED;
301304
memset(ptr->config, TOMOYO_CONFIG_USE_DEFAULT,
302305
sizeof(ptr->config));
306+
ptr->pref[TOMOYO_PREF_MAX_LEARNING_ENTRY] = 2048;
303307
mb(); /* Avoid out-of-order execution. */
304308
tomoyo_profile_ptr[profile] = ptr;
305309
entry = NULL;
@@ -319,13 +323,22 @@ static struct tomoyo_profile *tomoyo_assign_profile(const unsigned int profile)
319323
*/
320324
struct tomoyo_profile *tomoyo_profile(const u8 profile)
321325
{
326+
static struct tomoyo_profile tomoyo_null_profile;
322327
struct tomoyo_profile *ptr = tomoyo_profile_ptr[profile];
323-
if (!tomoyo_policy_loaded)
324-
return &tomoyo_default_profile;
325-
BUG_ON(!ptr);
328+
if (!ptr)
329+
ptr = &tomoyo_null_profile;
326330
return ptr;
327331
}
328332

333+
/**
334+
* tomoyo_find_yesno - Find values for specified keyword.
335+
*
336+
* @string: String to check.
337+
* @find: Name of keyword.
338+
*
339+
* Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
340+
*/
341+
/*
329342
static s8 tomoyo_find_yesno(const char *string, const char *find)
330343
{
331344
const char *cp = strstr(string, find);
@@ -338,19 +351,17 @@ static s8 tomoyo_find_yesno(const char *string, const char *find)
338351
}
339352
return -1;
340353
}
354+
*/
341355

342-
static void tomoyo_set_bool(bool *b, const char *string, const char *find)
343-
{
344-
switch (tomoyo_find_yesno(string, find)) {
345-
case 1:
346-
*b = true;
347-
break;
348-
case 0:
349-
*b = false;
350-
break;
351-
}
352-
}
353-
356+
/**
357+
* tomoyo_set_uint - Set value for specified preference.
358+
*
359+
* @i: Pointer to "unsigned int".
360+
* @string: String to check.
361+
* @find: Name of keyword.
362+
*
363+
* Returns nothing.
364+
*/
354365
static void tomoyo_set_uint(unsigned int *i, const char *string,
355366
const char *find)
356367
{
@@ -359,51 +370,16 @@ static void tomoyo_set_uint(unsigned int *i, const char *string,
359370
sscanf(cp + strlen(find), "=%u", i);
360371
}
361372

362-
static void tomoyo_set_pref(const char *name, const char *value,
363-
const bool use_default,
364-
struct tomoyo_profile *profile)
365-
{
366-
struct tomoyo_preference **pref;
367-
bool *verbose;
368-
if (!strcmp(name, "enforcing")) {
369-
if (use_default) {
370-
pref = &profile->enforcing;
371-
goto set_default;
372-
}
373-
profile->enforcing = &profile->preference;
374-
verbose = &profile->preference.enforcing_verbose;
375-
goto set_verbose;
376-
}
377-
if (!strcmp(name, "permissive")) {
378-
if (use_default) {
379-
pref = &profile->permissive;
380-
goto set_default;
381-
}
382-
profile->permissive = &profile->preference;
383-
verbose = &profile->preference.permissive_verbose;
384-
goto set_verbose;
385-
}
386-
if (!strcmp(name, "learning")) {
387-
if (use_default) {
388-
pref = &profile->learning;
389-
goto set_default;
390-
}
391-
profile->learning = &profile->preference;
392-
tomoyo_set_uint(&profile->preference.learning_max_entry, value,
393-
"max_entry");
394-
verbose = &profile->preference.learning_verbose;
395-
goto set_verbose;
396-
}
397-
return;
398-
set_default:
399-
*pref = &tomoyo_default_profile.preference;
400-
return;
401-
set_verbose:
402-
tomoyo_set_bool(verbose, value, "verbose");
403-
}
404-
373+
/**
374+
* tomoyo_set_mode - Set mode for specified profile.
375+
*
376+
* @name: Name of functionality.
377+
* @value: Mode for @name.
378+
* @profile: Pointer to "struct tomoyo_profile".
379+
*
380+
* Returns 0 on success, negative value otherwise.
381+
*/
405382
static int tomoyo_set_mode(char *name, const char *value,
406-
const bool use_default,
407383
struct tomoyo_profile *profile)
408384
{
409385
u8 i;
@@ -425,7 +401,7 @@ static int tomoyo_set_mode(char *name, const char *value,
425401
} else {
426402
return -EINVAL;
427403
}
428-
if (use_default) {
404+
if (strstr(value, "use_default")) {
429405
config = TOMOYO_CONFIG_USE_DEFAULT;
430406
} else {
431407
u8 mode;
@@ -455,34 +431,21 @@ static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
455431
{
456432
char *data = head->write_buf;
457433
unsigned int i;
458-
bool use_default = false;
459434
char *cp;
460435
struct tomoyo_profile *profile;
461436
if (sscanf(data, "PROFILE_VERSION=%u", &tomoyo_profile_version) == 1)
462437
return 0;
463438
i = simple_strtoul(data, &cp, 10);
464-
if (data == cp) {
465-
profile = &tomoyo_default_profile;
466-
} else {
467-
if (*cp != '-')
468-
return -EINVAL;
469-
data = cp + 1;
470-
profile = tomoyo_assign_profile(i);
471-
if (!profile)
472-
return -EINVAL;
473-
}
439+
if (*cp != '-')
440+
return -EINVAL;
441+
data = cp + 1;
442+
profile = tomoyo_assign_profile(i);
443+
if (!profile)
444+
return -EINVAL;
474445
cp = strchr(data, '=');
475446
if (!cp)
476447
return -EINVAL;
477448
*cp++ = '\0';
478-
if (profile != &tomoyo_default_profile)
479-
use_default = strstr(cp, "use_default") != NULL;
480-
if (tomoyo_str_starts(&data, "PREFERENCE::")) {
481-
tomoyo_set_pref(data, cp, use_default, profile);
482-
return 0;
483-
}
484-
if (profile == &tomoyo_default_profile)
485-
return -EINVAL;
486449
if (!strcmp(data, "COMMENT")) {
487450
static DEFINE_SPINLOCK(lock);
488451
const struct tomoyo_path_info *new_comment
@@ -497,48 +460,13 @@ static int tomoyo_write_profile(struct tomoyo_io_buffer *head)
497460
tomoyo_put_name(old_comment);
498461
return 0;
499462
}
500-
return tomoyo_set_mode(data, cp, use_default, profile);
501-
}
502-
503-
static void tomoyo_print_preference(struct tomoyo_io_buffer *head,
504-
const int idx)
505-
{
506-
struct tomoyo_preference *pref = &tomoyo_default_profile.preference;
507-
const struct tomoyo_profile *profile = idx >= 0 ?
508-
tomoyo_profile_ptr[idx] : NULL;
509-
char buffer[16] = "";
510-
if (profile) {
511-
buffer[sizeof(buffer) - 1] = '\0';
512-
snprintf(buffer, sizeof(buffer) - 1, "%u-", idx);
513-
}
514-
if (profile) {
515-
pref = profile->learning;
516-
if (pref == &tomoyo_default_profile.preference)
517-
goto skip1;
518-
}
519-
tomoyo_io_printf(head, "%sPREFERENCE::%s={ "
520-
"verbose=%s max_entry=%u }\n",
521-
buffer, "learning",
522-
tomoyo_yesno(pref->learning_verbose),
523-
pref->learning_max_entry);
524-
skip1:
525-
if (profile) {
526-
pref = profile->permissive;
527-
if (pref == &tomoyo_default_profile.preference)
528-
goto skip2;
529-
}
530-
tomoyo_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
531-
buffer, "permissive",
532-
tomoyo_yesno(pref->permissive_verbose));
533-
skip2:
534-
if (profile) {
535-
pref = profile->enforcing;
536-
if (pref == &tomoyo_default_profile.preference)
537-
return;
463+
if (!strcmp(data, "PREFERENCE")) {
464+
for (i = 0; i < TOMOYO_MAX_PREF; i++)
465+
tomoyo_set_uint(&profile->pref[i], cp,
466+
tomoyo_pref_keywords[i]);
467+
return 0;
538468
}
539-
tomoyo_io_printf(head, "%sPREFERENCE::%s={ verbose=%s }\n",
540-
buffer, "enforcing",
541-
tomoyo_yesno(pref->enforcing_verbose));
469+
return tomoyo_set_mode(data, cp, profile);
542470
}
543471

544472
static void tomoyo_print_config(struct tomoyo_io_buffer *head, const u8 config)
@@ -561,7 +489,6 @@ static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
561489
switch (head->r.step) {
562490
case 0:
563491
tomoyo_io_printf(head, "PROFILE_VERSION=%s\n", "20090903");
564-
tomoyo_print_preference(head, -1);
565492
head->r.step++;
566493
break;
567494
case 1:
@@ -575,11 +502,18 @@ static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
575502
break;
576503
case 2:
577504
{
505+
u8 i;
578506
const struct tomoyo_path_info *comment =
579507
profile->comment;
580508
tomoyo_io_printf(head, "%u-COMMENT=", index);
581509
tomoyo_set_string(head, comment ? comment->name : "");
582510
tomoyo_set_lf(head);
511+
tomoyo_io_printf(head, "%u-PREFERENCE={ ", index);
512+
for (i = 0; i < TOMOYO_MAX_PREF; i++)
513+
tomoyo_io_printf(head, "%s=%u ",
514+
tomoyo_pref_keywords[i],
515+
profile->pref[i]);
516+
tomoyo_set_string(head, "}\n");
583517
head->r.step++;
584518
}
585519
break;
@@ -606,7 +540,6 @@ static void tomoyo_read_profile(struct tomoyo_io_buffer *head)
606540
}
607541
if (head->r.bit == TOMOYO_MAX_MAC_INDEX
608542
+ TOMOYO_MAX_MAC_CATEGORY_INDEX) {
609-
tomoyo_print_preference(head, index);
610543
head->r.index++;
611544
head->r.step = 1;
612545
}
@@ -1777,7 +1710,7 @@ static int tomoyo_write_answer(struct tomoyo_io_buffer *head)
17771710
static void tomoyo_read_version(struct tomoyo_io_buffer *head)
17781711
{
17791712
if (!head->r.eof) {
1780-
tomoyo_io_printf(head, "2.3.0");
1713+
tomoyo_io_printf(head, "2.4.0");
17811714
head->r.eof = true;
17821715
}
17831716
}

‎security/tomoyo/common.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@ enum tomoyo_mac_category_index {
211211
*/
212212
#define TOMOYO_RETRY_REQUEST 1
213213

214+
/* Index numbers for profile's PREFERENCE values. */
215+
enum tomoyo_pref_index {
216+
TOMOYO_PREF_MAX_LEARNING_ENTRY,
217+
TOMOYO_MAX_PREF
218+
};
219+
214220
/********** Structure definitions. **********/
215221

216222
/* Common header for holding ACL entries. */
@@ -497,6 +503,7 @@ struct tomoyo_profile {
497503
struct tomoyo_preference preference;
498504
u8 default_config;
499505
u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
506+
unsigned int pref[TOMOYO_MAX_PREF];
500507
};
501508

502509
/********** Function prototypes. **********/

0 commit comments

Comments
 (0)