/*
* security/tomoyo/file.c
*
* Implementation of the Domain-Based Mandatory Access Control.
*
* Copyright (C) 2005-2009 NTT DATA CORPORATION
*
* Version: 2.2.0 2009/04/01
*
*/
#include "common.h"
#include "tomoyo.h"
#include "realpath.h"
#define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
/* Structure for "allow_read" keyword. */
struct tomoyo_globally_readable_file_entry {
struct list_head list;
const struct tomoyo_path_info *filename;
bool is_deleted;
};
/* Structure for "file_pattern" keyword. */
struct tomoyo_pattern_entry {
struct list_head list;
const struct tomoyo_path_info *pattern;
bool is_deleted;
};
/* Structure for "deny_rewrite" keyword. */
struct tomoyo_no_rewrite_entry {
struct list_head list;
const struct tomoyo_path_info *pattern;
bool is_deleted;
};
/* Keyword array for single path operations. */
static const char *tomoyo_sp_keyword[TOMOYO_MAX_SINGLE_PATH_OPERATION] = {
[TOMOYO_TYPE_READ_WRITE_ACL] = "read/write",
[TOMOYO_TYPE_EXECUTE_ACL] = "execute",
[TOMOYO_TYPE_READ_ACL] = "read",
[TOMOYO_TYPE_WRITE_ACL] = "write",
[TOMOYO_TYPE_CREATE_ACL] = "create",
[TOMOYO_TYPE_UNLINK_ACL] = "unlink",
[TOMOYO_TYPE_MKDIR_ACL] = "mkdir",
[TOMOYO_TYPE_RMDIR_ACL] = "rmdir",
[TOMOYO_TYPE_MKFIFO_ACL] = "mkfifo",
[TOMOYO_TYPE_MKSOCK_ACL] = "mksock",
[TOMOYO_TYPE_MKBLOCK_ACL] = "mkblock",
[TOMOYO_TYPE_MKCHAR_ACL] = "mkchar",
[TOMOYO_TYPE_TRUNCATE_ACL] = "truncate",
[TOMOYO_TYPE_SYMLINK_ACL] = "symlink",
[TOMOYO_TYPE_REWRITE_ACL] = "rewrite",
};
/* Keyword array for double path operations. */
static const char *tomoyo_dp_keyword[TOMOYO_MAX_DOUBLE_PATH_OPERATION] = {
[TOMOYO_TYPE_LINK_ACL] = "link",
[TOMOYO_TYPE_RENAME_ACL] = "rename",
};
/**
* tomoyo_sp2keyword - Get the name of single path operation.
*
* @operation: Type of operation.
*
* Returns the name of single path operation.
*/
const char *tomoyo_sp2keyword(const u8 operation)
{
return (operation < TOMOYO_MAX_SINGLE_PATH_OPERATION)
? tomoyo_sp_keyword[operation] : NULL;
}
/**
* tomoyo_dp2keyword - Get the name of double path operation.
*
* @operation: Type of operation.
*
* Returns the name of double path operation.
*/
const char *tomoyo_dp2keyword(const u8 operation)
{
return (operation < TOMOYO_MAX_DOUBLE_PATH_OPERATION)
? tomoyo_dp_keyword[operation] : NULL;
}
/**
* tomoyo_strendswith - Check whether the token ends with the given token.
*
* @name: The token to check.
* @tail: The token to find.
*
* Returns true if @name ends with @tail, false otherwise.
*/
static bool tomoyo_strendswith(const char *name, const char *tail)
{
int len;
if (!name || !tail)
return false;
len = strlen(name) - strlen(tail);
return len >= 0 && !strcmp(name + len, tail);
}
/**
* tomoyo_get_path - Get realpath.
*
* @path: Pointer to "struct path".
*
* Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
*/
static struct tomoyo_path_info *tomoyo_get_path(struct path *path