aboutsummaryrefslogtreecommitdiff
path: root/security/trustees/fs.c
blob: 6463c64631a03087bbc126d16816829a8c096d62 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * Trustees ACL Project
 *
 * Copyright (c) 1999-2000 Vyacheslav Zavadsky
 * Copyright (c) 2004 Andrew Ruder (aeruder@ksu.edu)
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License as
 *	published by the Free Software Foundation, version 2.
 *
 * This code handles the virtual filesystem for trustees.
 *
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/vmalloc.h>
#include <linux/security.h>
#include <asm/atomic.h>
#include <asm/uaccess.h>

#include "internal.h"


/* initialization code for the trustees filesystem */

/* File operations
 *
 * this is all the code for handling the file operations done on the few files
 * in the trustees filesystem
 */
static int trustees_open(struct inode *inode, struct file *filp);
static ssize_t trustees_read_bogus(struct file *filp, char __user * buf,
				   size_t count, loff_t * offset);
static ssize_t trustees_write_bogus(struct file *filp,
				    const char __user * buf, size_t count,
				    loff_t * offset);
static ssize_t trustees_read_status(struct file *filp, char __user * buf,
				    size_t count, loff_t * offset);
static ssize_t trustees_read_apiversion(struct file *filp, char __user * buf,
				    size_t count, loff_t * offset);
static ssize_t trustees_write_trustees(struct file *filp,
				       const char __user * buf,
				       size_t count, loff_t * offset);
static int trustees_open_trustees(struct inode *inode, struct file *file);
static int trustees_release_trustees(struct inode *inode, struct file *file);

/* Various structs
 */

static struct file_operations trustees_ops_apiversion = {
	.open = trustees_open,
	.read = trustees_read_apiversion,
	.write = trustees_write_bogus,
};

static struct file_operations trustees_ops_status = {
	.open = trustees_open,
	.read = trustees_read_status,
	.write = trustees_write_bogus
};

static struct file_operations trustees_ops_trustees = {
	.open = trustees_open_trustees,
	.read = trustees_read_bogus,
	.write = trustees_write_trustees,
	.release = trustees_release_trustees
};

static struct trustees_file_info {
	const char *name;
	struct file_operations *fops;
	int mode;
	struct dentry *dentry;
} trustees_files[] = {
	{.name = "device",
	 .fops = &trustees_ops_trustees,
	 .mode = S_IWUSR,
	 .dentry = 0
	 },
	{.name = "status",
	 .fops = &trustees_ops_status,
	 .mode = S_IRUSR,
	 .dentry = 0
	 },
	{.name = "apiversion",
	 .fops = &trustees_ops_apiversion,
	 .mode = S_IRUSR | S_IRGRP | S_IROTH,
	 .dentry = 0
	 },
	{"", NULL, 0, 0}
};

struct trustee_command_reader {
	struct trustee_command command;
	unsigned curarg;
	void *arg[TRUSTEE_MAX_ARGS];
	size_t argsize[TRUSTEE_MAX_ARGS];
};


static struct dentry *toplevel = NULL;

int trustees_init_fs(void)
{
	struct trustees_file_info *iter;
	toplevel = securityfs_create_dir("trustees", NULL);
	if (!toplevel) trustees_deinit_fs();
	for (iter = trustees_files; iter->fops && toplevel; iter++) {
		iter->dentry = securityfs_create_file(
		   iter->name, iter->mode, toplevel, NULL, iter->fops);
		if (!iter->dentry) trustees_deinit_fs();
	}
	return !toplevel;
}

void trustees_deinit_fs(void)
{
	struct trustees_file_info *iter;
	for (iter = trustees_files; iter->fops; iter++) {
		securityfs_remove(iter->dentry);
		iter->dentry = NULL;
	}
	securityfs_remove(toplevel);
	toplevel = NULL;
}

/*
 * They're opening the file...
 */

static int trustees_open(struct inode *inode, struct file *filp)
{
	return 0;
}

static int trustees_open_trustees(struct inode *inode, struct file *file)
{
	file->private_data = vmalloc(sizeof(struct trustee_command_reader));
	if (!file->private_data)
		return -ENOMEM;

	memset(file->private_data, 0, sizeof(struct trustee_command_reader));

	return 0;
}

static int trustees_release_trustees(struct inode *inode, struct file *file)
{
	vfree(file->private_data);
	return 0;
}

/* Do a read on a bogus file.  Just return nothing :) */
static ssize_t trustees_read_bogus(struct file *filp, char __user * buf,
				   size_t count, loff_t * offset)
{
	return 0;
}

/* Similar way to handle writes.  Just say we wrote the data and return */
static ssize_t trustees_write_bogus(struct file *filp,
				    const char __user * buf, size_t count,
				    loff_t * offset)
{
	return count;
}

/* Function for handling reading of the status. */
static ssize_t trustees_read_status(struct file *filp, char __user * buf,
				    size_t count, loff_t * offset)
{
	static const char msg[] = "Damnit, it works, OK?!\n";
	unsigned long nocopy;

	if (*offset >= (sizeof(msg) - 1)) {
		return 0;
	}

	if (count > (sizeof(msg) - 1 - *offset)) {
		count = sizeof(msg) - 1 - *offset;
	}
	nocopy = copy_to_user(buf, msg, count);
	(*offset) += count;
	(*offset) -= nocopy;

	return count;
}

/* Function for handling reading of the apiversion. */
static ssize_t trustees_read_apiversion(struct file *filp, char __user * buf,
				    size_t count, loff_t * offset)
{
	static const char msg[] = TRUSTEES_APIVERSION_STR "\n";
	unsigned long nocopy;

	if (*offset >= (sizeof(msg) - 1)) {
		return 0;
	}

	if (count > (sizeof(msg) - 1 - *offset)) {
		count = sizeof(msg) - 1 - *offset;
	}
	nocopy = copy_to_user(buf, msg, count);
	(*offset) += count;
	(*offset) -= nocopy;

	return count;
}

/* Cleanup our reader (deallocate all the allocated memory) */
static void cleanup_reader(struct trustee_command_reader *reader) {
	int z;
	if (!reader) {
		TS_ERR_MSG("How does reader disappear on us?\n");
		return;
	}

	for (z = reader->curarg - 1; z >= 0; z--) {
		vfree(reader->arg[z]);
		reader->argsize[z] = 0;
	}
	reader->command.command = 0;
	reader->curarg = 0;
}

static ssize_t trustees_write_trustees(struct file *filp,
				       const char __user * buf,
				       size_t count, loff_t * offset)
{
	struct trustee_command_reader *reader = filp->private_data;

	if (reader->command.command == 0) {
		reader->curarg = 0;
		if (count != sizeof(struct trustee_command)) {
			return -EIO;
		}
		if (copy_from_user(&reader->command, buf, count)) {
			reader->command.command = 0;
			TS_ERR_MSG("copy_from_user failed on command\n");
			return -EIO;
		}
		if (reader->command.numargs > TRUSTEE_MAX_ARGS) {
			TS_ERR_MSG("Too many arguments specified for command %d\n",
			  reader->command.command);
			return -EIO;
		}
	} else {
		unsigned curarg = reader->curarg;
		if (!(reader->arg[curarg] = vmalloc(count+1))) {
			cleanup_reader(reader);
			return -EIO;
		}
		reader->argsize[curarg] = count;
		((char *)reader->arg[curarg])[count] = '\0';
		reader->curarg++;
		if (copy_from_user(reader->arg[curarg], buf, count)) {
			cleanup_reader(reader);
			TS_ERR_MSG("copy_from_user failed on arg\n");
			return -EIO;
		}
	}

	if (reader->command.command && reader->curarg == reader->command.numargs) {
		int ret = trustees_process_command(reader->command, reader->arg,
		                                   reader->argsize);
		cleanup_reader(reader);
		if (ret) return -EIO;
	}

	return count;
}