summaryrefstree
path: root/kernel/fs/ecryptfs/ecryptfs_sdp_chamber.c
blob: 8be6721b20e8e29677cb2192d4ac23e59a75212c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
 *
 * Sensitive Data Protection
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>

#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/slab.h>

#include "ecryptfs_sdp_chamber.h"

#include <sdp/common.h>

#define CHAMBER_PATH_MAX 512
typedef struct __chamber_info {
	int partition_id;
    int engine_id;

	struct list_head list;
	char path[CHAMBER_PATH_MAX];
}chamber_info_t;

#define NO_DIRECTORY_SEPARATOR_IN_CHAMBER_PATH 1
/* Debug */
#define CHAMBER_DEBUG		0

#if CHAMBER_DEBUG
#define CHAMBER_LOGD(FMT, ...) printk("SDP_CHAMBER[%d] %s :: " FMT , current->pid, __func__, ##__VA_ARGS__)
#else
#define CHAMBER_LOGD(FMT, ...)
#endif /* PUB_CRYPTO_DEBUG */
#define CHAMBER_LOGE(FMT, ...) printk("SDP_CHAMBER[%d] %s :: " FMT , current->pid, __func__, ##__VA_ARGS__)


chamber_info_t *alloc_chamber_info(int partition_id, int engine_id, const unsigned char *path) {
	chamber_info_t *new_chamber = kmalloc(sizeof(chamber_info_t), GFP_KERNEL);

	if(new_chamber == NULL) {
		CHAMBER_LOGE("can't alloc memory for chamber_info\n");
		return NULL;
	}

    new_chamber->partition_id = partition_id;
    new_chamber->engine_id = engine_id;
	snprintf(new_chamber->path, CHAMBER_PATH_MAX, "%s", path);

	return new_chamber;
}

int add_chamber_directory(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
		int engine_id, const unsigned char *path) {
	chamber_info_t *new_chamber = NULL;

#if NO_DIRECTORY_SEPARATOR_IN_CHAMBER_PATH
	if(strchr(path, '/') != NULL) {
		CHAMBER_LOGE("Chamber directory cannot contain '/'\n");
		return -EINVAL;
	}
#endif

	new_chamber = alloc_chamber_info(mount_crypt_stat->partition_id, engine_id, path);

	if(new_chamber == NULL) {
		return -ENOMEM;
	}

	spin_lock(&(mount_crypt_stat->chamber_dir_list_lock));
	CHAMBER_LOGD("Adding %s into chamber list\n", new_chamber->path);
	list_add_tail(&new_chamber->list, &(mount_crypt_stat->chamber_dir_list));
	spin_unlock(&(mount_crypt_stat->chamber_dir_list_lock));

	return 0;
}

chamber_info_t *find_chamber_info(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
		const unsigned char *path) {
	struct list_head *entry;

	spin_lock(&(mount_crypt_stat->chamber_dir_list_lock));

	CHAMBER_LOGD("%s\n", path);

	list_for_each(entry, &mount_crypt_stat->chamber_dir_list) {
		chamber_info_t *info;
		info = list_entry(entry, chamber_info_t, list);

		// Check path
		if(!strncmp(path, info->path, CHAMBER_PATH_MAX)) {
			CHAMBER_LOGD("Found %s from chamber list\n", info->path);

			spin_unlock(&(mount_crypt_stat->chamber_dir_list_lock));
			return info;
		}
	}

	spin_unlock(&(mount_crypt_stat->chamber_dir_list_lock));
	CHAMBER_LOGD("Not found\n");

	return NULL;
}

void del_chamber_directory(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
        const unsigned char *path) {
	chamber_info_t *info = find_chamber_info(mount_crypt_stat, path);
	if(info == NULL) {
		CHAMBER_LOGD("nothing to remove\n");
		return;
	}

	spin_lock(&mount_crypt_stat->chamber_dir_list_lock);
	CHAMBER_LOGD("%s removed\n", info->path);
	list_del(&info->list);

	kfree(info);
	spin_unlock(&mount_crypt_stat->chamber_dir_list_lock);
}

int is_chamber_directory(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
		const unsigned char *path, int *engineid) {
    chamber_info_t *info;
#if NO_DIRECTORY_SEPARATOR_IN_CHAMBER_PATH
	if(strchr(path, '/') != NULL) {
		CHAMBER_LOGD("%s containes '/'\n", path);
		return 0;
	}
#endif

	info = find_chamber_info(mount_crypt_stat, path);
	if(info == NULL)
		return 0;

	if(engineid) *engineid = info->engine_id;

	return 1;
}

void set_chamber_flag(int engineid, struct inode *inode) {
	struct ecryptfs_crypt_stat *crypt_stat;

	if(inode == NULL) {
		CHAMBER_LOGE("invalid inode\n");
		return;
	}

	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;

    crypt_stat->engine_id = engineid;
    crypt_stat->flags |= ECRYPTFS_SDP_IS_CHAMBER_DIR;
	crypt_stat->flags |= ECRYPTFS_DEK_IS_SENSITIVE;
}

void clr_chamber_flag(struct inode *inode) {
    struct ecryptfs_crypt_stat *crypt_stat;

    if(inode == NULL) {
        CHAMBER_LOGE("invalid inode\n");
        return;
    }

    crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;

    crypt_stat->engine_id = -1;
    crypt_stat->flags &= ~ECRYPTFS_DEK_IS_SENSITIVE;
    crypt_stat->flags &= ~ECRYPTFS_SDP_IS_CHAMBER_DIR;
}